自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
函式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
HIDL 介面中的函式會對應至自動產生的 IFoo
C++ 類別宣告中的函式。在 C++ 中,每個函式的名稱都會保持不變;以下各節將說明如何將 HIDL 引數和傳回值轉譯為 C++。
函式參數
.hal
檔案中列出的引數會對應至 C++ 資料類型。未對應至原始 C++ 類型的引數,會透過 const 參照傳遞。
對於每個具有傳回值 (含有 generates
陳述式) 的 HIDL 函式,該函式的 C++ 參數清單都會包含額外的引數:使用 HIDL 函式的傳回值呼叫的回呼函式。例外狀況:如果 generates
子句包含直接對應至 C++ 原始類型的單一參數,系統會使用回呼 省略 (回呼會遭到移除,且傳回值會透過一般 return
陳述式從函式傳回)。
函式傳回值
下列函式具有傳回值。
傳輸錯誤和傳回類型
generates
陳述式可產生三種類型的函式簽名:
- 如果只有一個傳回值是 C++ 原始值,
generates
傳回值會由 Return<T>
物件中函式的值傳回。
- 在較複雜的情況下,
generates
傳回值會透過函式呼叫本身提供的回呼參數傳回,而函式會傳回 Return<void>
。
- 如果沒有
generates
陳述式,函式會傳回 Return<void>
。
RPC 呼叫偶爾會遇到傳輸錯誤,例如伺服器停止運作、傳輸資源不足以完成呼叫,或是傳遞的參數不允許完成呼叫 (例如缺少必要的回呼函式)。Return
物件會儲存傳輸錯誤指示,以及 T
值 (Return<void>
除外)。
由於用戶端和伺服器端函式具有相同的簽章,因此即使伺服器端函式的實作不會傳送傳輸錯誤信號,它仍必須傳回 Return
類型。Return<T>
物件是使用 Return(myTValue)
建構 (或可從 mTValue
隱含建構,例如在 return
陳述式中),而 Return<void>
物件則是使用 Void()
建構。
Return<T>
物件會隱含轉換為 T
值,並從該值轉換回來。您可以呼叫 isOk()
方法,檢查 Return
物件是否有傳輸錯誤。這項檢查並非必要,但如果發生錯誤,且在 Return
物件銷毀前未檢查,或是嘗試進行 T
值轉換,則會終止用戶端程序並記錄錯誤。如果 isOk()
指出傳輸錯誤,或是由於開發人員程式碼中的邏輯錯誤 (例如將 nullptr
傳遞為同步回呼) 而導致呼叫失敗,則可在 Return 物件上呼叫 description()
,以便傳回適合記錄的字串。在這種情況下,我們無法判斷因呼叫失敗而導致在伺服器上執行的程式碼量。系統也會提供 isDeadObject()
方法。這個方法表示 !isOk()
是因為遠端物件已異常終止或不存在。isDeadObject()
一律會暗示 !isOk()
。
依值傳回
如果 generates
陳述式對應至單一 C++ 原始元素,參數清單中就不會有回呼參數。相反地,實作會在 Return<T>
物件中提供傳回值 T
,而這可從原始類型 T
隱含產生。例如:
Return<uint32_t> someMethod() {
uint32_t return_data = ...; // Compute return_data
return return_data;
};
系統也會提供 Return<*>::withDefault
方法。當傳回值為 !isOk()
時,這個方法會提供值。這個方法也會自動將傳回物件標示為正常,因此不會終止用戶端程序。
使用回呼參數傳回
回呼可以將 HIDL 函式的傳回值傳回給呼叫端。回呼的原型是 std::function
物件,其中的參數 (取自 generates
陳述式) 會對應至 C++ 類型。其回傳值為 void,因為回呼本身不會傳回值。
具有回呼參數的 C++ 函式傳回值具有 Return<void>
類型。伺服器實作項目只負責提供傳回值。由於傳回值已透過回呼傳送,因此 T
範本參數為 void
:
Return<void> someMethod(someMethod_cb _cb);
從 C++ 實作項目,伺服器實作項目應傳回 Void()
,這是會傳回 Return<void>
物件的靜態內嵌函式。以下是含回呼參數的典型伺服器方法實作範例:
Return<void> someMethod(someMethod_cb _cb) {
// Do some processing, then call callback with return data
hidl_vec<uint32_t> vec = ...
_cb(vec);
return Void();
};
不傳回值的函式
沒有 generates
陳述式的函式 C++ 簽名,在參數清單中不會有回呼參數。其傳回類型為 Return<void>.
Oneway 函式
標示為 oneway
關鍵字的函式為非同步函式 (用戶端不會阻斷執行),且沒有傳回值。oneway
函式的 C++ 簽章在參數清單中不會有回呼參數,其 C++ 回傳值會是 Return<void>
。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Functions in a HIDL interface are mapped to methods in the autogenerated\n`IFoo` C++ class declaration. The name of each function remains the\nsame in C++; the following sections describe how HIDL arguments and return\nvalues are translated to C++.\n\nFunction parameters\n-------------------\n\nThe arguments listed in the `.hal` file map to C++ data types.\nArguments that do not map to a primitive C++ type are passed by const\nreference.\n\nFor every HIDL function that has a return value (has a `generates`\nstatement), the C++ parameter list for that function has an additional argument:\na callback function that is called with the return values of the HIDL function.\nThere is **one exception** : If the `generates` clause\ncontains a single parameter that directly maps to a C++ primitive, callback\n*elision* is used (the callback is removed and the return value is\nreturned from the function through a normal `return` statement).\n\nFunction return values\n----------------------\n\nThe following functions have return values.\n\n### Transport errors and return type\n\nThe `generates` statement can result in three types of function\nsignatures:\n\n- For only one return value that is a C++ primitive, the `generates` return value is returned by value from the function in a `Return\u003cT\u003e` object.\n- For more complicated cases, the `generates` return value(s) are returned through the callback parameter provided with the function call itself, and the function returns `Return\u003cvoid\u003e`.\n- For when no `generates` statement exists, the function returns `Return\u003cvoid\u003e`.\n\nRPC calls can occasionally encounter transport errors, e.g. when the server\ndies, when transport resources are insufficient to complete the call, or when\nthe parameters passed do not permit completing the call (such as missing a\nrequired callback function). `Return` objects store transport error\nindications as well as a `T` value (except\n`Return\u003cvoid\u003e`).\n\nAs the client-side and server-side functions have the same signature, the\nserver-side function must return a `Return` type even though its\nimplementation does not signal transport errors. `Return\u003cT\u003e`\nobjects are constructed with `Return(myTValue)` (or can be implicitly\nconstructed from `mTValue`, such as in `return`\nstatements) and `Return\u003cvoid\u003e` objects are constructed with\n`Void()`.\n\n`Return\u003cT\u003e` objects have implicit conversion to and from\ntheir `T` value. The `Return` object can be checked for\ntransport errors by calling its `isOk()` method. This check is not\nrequired; however, if an error occurs and is not checked by the time the\n`Return` object is destroyed, or a `T` value conversion is\nattempted, the client process will be killed and an error logged. If\n`isOk()` indicates a transport error or a call failure due to a logic\nerror in developer code (such as passing `nullptr` as a synchronous\ncallback), then `description()` can be called on the Return object to\nreturn a string suitable for logging. In such cases, there is no way to\ndetermine how much code may have executed on the server as a result of the\nfailed call. The method `isDeadObject()` is also provided. This\nmethod indicates that `!isOk()` is because the remote object has\ncrashed or no longer exists. `isDeadObject()` always implies\n`!isOk()`.\n\n### Return by value\n\nIf the `generates` statement maps to a single C++ primitive, no\ncallback parameter is in the parameter list. Instead, an implementation provides\nthe return value `T` in a `Return\u003cT\u003e` object, which\ncan be implicitly generated from the primitive type `T`. For\nexample: \n\n```c++\nReturn\u003cuint32_t\u003e someMethod() {\n uint32_t return_data = ...; // Compute return_data\n return return_data;\n};\n```\n\nThe method `Return\u003c*\u003e::withDefault` is also provided. This\nmethod provides a value in cases where the return value is `!isOk()`.\nThis method also automatically marks the return object as okay so the client\nprocess will not be killed.\n\n### Return using callback parameter\n\nA callback can pass the return value of the HIDL function back to the caller.\nThe prototype of the callback is a `std::function` object with\nparameters (taken from the `generates` statement) mapped to C++\ntypes. Its return value is void---the callback itself doesn't return a value.\n\nThe return value of a C++ function with a callback parameter has type\n`Return\u003cvoid\u003e`. The server implementation is responsible only\nfor providing the return value. As the return values are already transferred\nusing the callback, the `T` template parameter is `void`: \n\n```c++\nReturn\u003cvoid\u003e someMethod(someMethod_cb _cb);\n```\n\nFrom their C++ implementation, server implementations should return\n`Void()`, which is a static inlined function returning a\n`Return\u003cvoid\u003e` object. Example of a typical server method\nimplementation with a callback parameter: \n\n```c++\nReturn\u003cvoid\u003e someMethod(someMethod_cb _cb) {\n // Do some processing, then call callback with return data\n hidl_vec\u003cuint32_t\u003e vec = ...\n _cb(vec);\n return Void();\n};\n```\n\nFunctions without return values\n-------------------------------\n\nThe C++ signature of a function without a `generates` statement\nwill not have a callback parameter in the parameter list. Its return type will\nbe `Return\u003cvoid\u003e.`\n\nOneway functions\n----------------\n\nFunctions marked with the `oneway` keyword are asynchronous\nfunctions (clients won't block on their execution) and do not have return\nvalues. The C++ signature of a `oneway` function will not have a\ncallback parameter in the parameter list, and its C++ return value will be\n`Return\u003cvoid\u003e`."]]