This example describes an OCap usage scenario in which the resource provider provides the following methods for accessing its resources:
OpenResource()
– opens access to the resource.UseResource()
– uses the resource.CloseResource()
– closes access to the resource.The resource consumer uses these methods.
IDL description:
package SimpleOCap
interface {
OpenResource(in UInt32 ID, out Handle handle);
UseResource(in Handle handle, in UInt8 param, out UInt8 result);
CloseResource(in Handle handle);
}
The scenario includes the following steps:
KnHandleCreateUserObject()
function to create the resource handle. The resource provider saves the resource handle in the user resource context.OpenResource()
method to open access to the resource.KnHandleCreateBadge()
function to create a resource transfer context object and configure the notification receiver to receive notifications regarding the closure and deletion of the resource transfer context object. The resource provider saves the handle of the resource transfer context object and the pointer to the user resource context in the resource transfer context.nk_handle_desc()
macro to package the resource handle, permissions mask of the handle, and pointer to the resource transfer context object into the handle transport container.OpenResource()
method call completes successfully. The resource consumer extracts the handle and permissions mask of the handle from the handle transport container by using the nk_get_handle()
and nk_get_rights()
functions, respectively. The handle permissions mask is not required by the resource consumer to query the resource, but is transferred so that the resource consumer can find out its permissions for accessing the resource.UseResource()
method to utilize the resource.UseResource()
method. Before calling this method, the resource consumer uses the nk_handle_desc()
macro to package the handle into the handle transport container.nk_is_handle_dereferenced()
function to verify that the dereferencing operation was completed instead of a handle transfer.nk_get_badge_op()
function, which extracts the pointer to the resource transfer context from the handle transport container if the received permissions mask has the corresponding flags set for the requested operation.UseResource()
method call completes successfully. The resource consumer receives the results of the operation performed with the resource.CloseResource()
method to close access to the resource.CloseResource()
method. Before calling this method, the resource consumer uses the nk_handle_desc()
macro to package the handle into the handle transport container. After the CloseResource()
method is called, the resource consumer uses the KnHandleClose()
function to close the handle.nk_is_handle_dereferenced()
function to verify that the dereferencing operation was completed instead of a handle transfer.nk_get_badge()
function to extract the pointer to the resource transfer context from the handle transport container.KnHandleRevokeSubtree()
function to revoke the handle owned by the resource consumer. The resource handle owned by the resource provider and the handle of the resource transfer context object are used as parameters of this function. The resource provider obtains access to these handles through the pointer to the resource transfer context. (Technically, the handle owned by the resource consumer does not have to be revoked because the resource consumer already closed it. However, the revoke operation is performed in case the resource provider is not sure if the resource consumer actually closed the handle).CloseResource()
method call completes successfully.KnNoticeGetEvent()
function to receive a notification that the resource transfer context object was closed, and uses the KnHandleClose()
function to close the handle of the resource transfer context object.KnNoticeGetEvent()
function to receive a notification that the resource transfer context object was deleted, and frees up the memory that was allocated for the resource transfer context.KnHandleClose()
function to close the resource handle and free up the memory that was allocated for the user resource context.