函数实现详解
IPC消息处理函数
static void HandleIpc(const Request *request, const Response *response)
{
void *ipcMsg = (void *)request->data;
Endpoint *endpoint = (Endpoint *)response->data;
Router *router = VECTOR_At(&endpoint->routers, request->msgId);
if (ipcMsg == NULL)
{
return;
}
if (router == NULL || router->proxy == NULL || router->proxy->Invoke == NULL)
{
FreeBuffer(endpoint->context, ipcMsg);
HILOG_ERROR(HILOG_MODULE_SAMGR, "Invalid IPC router<%p>!", router);
return;
}
uid_t uid = GetCallingUid(ipcMsg);
if ((strcmp(router->saName.service, SAMGR_SERVICE) != 0) &&
!JudgePolicy(uid, (const PolicyTrans *)(router->policy), router->policyNum))
{
FreeBuffer(endpoint->context, ipcMsg);
HILOG_ERROR(HILOG_MODULE_SAMGR, "Consumer uid<%d> has no permission to access<%s, %d, %d>!",
uid, router->saName.service, router->identity.serviceId, router->identity.featureId);
return;
}
IpcIo req;
IpcIoInitFromMsg(&req, ipcMsg);
IpcIo reply;
uint8 data[IPC_IO_DATA_MAX];
IpcIoInit(&reply, data, IPC_IO_DATA_MAX, MAX_OBJECT_NUM);
router->proxy->Invoke(router->proxy, request->msgValue, ipcMsg, &req, &reply);
uint32_t flag = 0;
GetFlag(ipcMsg, &flag);
if (flag == LITEIPC_FLAG_DEFAULT)
{
SendReply(endpoint->context, ipcMsg, &reply);
}
else
{
FreeBuffer(endpoint->context, ipcMsg);
}
}
注册服务和功能的访问地址
static int RegisterIdentity(const IpcContext *context, const SaName *saName, SvcIdentity *saInfo,
PolicyTrans **policy, uint32 *policyNum)
{
IpcIo req;
uint8 data[MAX_DATA_LEN];
IpcIoInit(&req, data, MAX_DATA_LEN, 0);
IpcIoPushUint32(&req, RES_FEATURE);
IpcIoPushUint32(&req, OP_PUT);
IpcIoPushString(&req, saName->service);
IpcIoPushBool(&req, saName->feature == NULL);
if (saName->feature != NULL)
{
IpcIoPushString(&req, saName->feature);
}
IpcIoPushUint32(&req, saInfo->token);
IpcIo reply;
void *replyBuf = NULL;
SvcIdentity samgr = {SAMGR_HANDLE, SAMGR_TOKEN, SAMGR_COOKIE};
int ret = Transact(context, samgr, INVALID_INDEX, &req, &reply, LITEIPC_FLAG_DEFAULT, (uintptr_t *)&replyBuf);
ret = -ret;
if (ret == LITEIPC_OK)
{
ret = IpcIoPopInt32(&reply);
}
if (ret == EC_SUCCESS)
{
saInfo = IpcIoPopSvc(&reply);
GetRemotePolicy(&reply, policy, policyNum);
}
if (replyBuf != NULL)
{
FreeBuffer(context, replyBuf);
}
return ret;
}
DD一下:欢迎大家关注公众号<程序猿百晓生>,可以了解到一下知识点。
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案)
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......
注册feature
//注册Feature,返回注册失败的个数
static int RegisterRemoteFeatures(Endpoint *endpoint)
{
int nums = 0
//获取router个数
int size = VECTOR_Size(&endpoint->routers)
int i
SvcIdentity identity
//遍历router
for (i = 0
{
//根据下标返回指定router
Router *router = VECTOR_At(&endpoint->routers, i)
if (router == NULL)
{
continue
}
identity.handle = endpoint->identity.handle
//token值即router在routers集合中的下标
identity.token = i
//注册并获取指定服务和功能的访问地址
int ret = RegisterIdentity(endpoint->context, &(router->saName), &identity, &(router->policy),
&(router->policyNum))
if (ret == EC_SUCCESS)
{
//注册成功
++nums
}
HILOG_DEBUG(HILOG_MODULE_SAMGR, "RegisterRemoteFeatures<%s, %s> ret:%d",
router->saName.service, router->saName.feature, ret)
}
//返回注册失败的个数
return VECTOR_Num(&endpoint->routers) - nums
}
注册endpoint
static int RegisterRemoteEndpoint(const IpcContext *context, SvcIdentity *identity)
{
IpcIo req;
uint8 data[MAX_DATA_LEN];
IpcIoInit(&req, data, MAX_DATA_LEN, 0);
IpcIoPushUint32(&req, RES_ENDPOINT);
IpcIoPushUint32(&req, OP_POST);
uint8 retry = 0;
while (retry < MAX_RETRY_TIMES)
{
++retry;
IpcIo reply;
void *replyBuf = NULL;
SvcIdentity samgr = {SAMGR_HANDLE, SAMGR_TOKEN, SAMGR_COOKIE};
int err = Transact(context, samgr, INVALID_INDEX, &req, &reply, LITEIPC_FLAG_DEFAULT, (uintptr_t *)&replyBuf);
if (err == LITEIPC_OK)
{
identity->handle = IpcIoPopUint32(&reply);
if (replyBuf != NULL)
{
FreeBuffer(context, replyBuf);
}
if (identity->handle == (uint32)INVALID_INDEX)
{
continue;
}
return EC_SUCCESS;
}
sleep(RETRY_INTERVAL);
}
return EC_FAILURE;
}