赞
踩
由于平台资源有限,且硬件平台多样,因此需要屏蔽不同硬件架构和平台资源的不同、以及运行形态的不同,提供统一化的系统服务开发框架。根据RISC-V、Cortex-M、Cortex-A不同硬件平台,分为两种硬件平台,以下简称M核、A核。
系统服务框架基于面向服务的架构,提供了服务开发、服务的子功能开发、对外接口的开发、以及多服务共进程、进程间服务调用等开发能力。其中:
图 1 面向服务的架构
图 2 系统服务开发框架主体对象:
表 1 系统服务框架源代码目录结构
名称 | 描述 |
---|---|
interfaces/kits/samgr_lite/samgr | M核和A核系统服务框架对外接口定义。 |
interfaces/kits/samgr_lite/registry | A核进程间服务调用的对外接口定义。 |
interfaces/kits/samgr_lite/communication/broadcast | M核和A核进程内事件广播服务的对外接口定义。 |
services/samgr_lite/samgr/adapter | POSIX和CMSIS接口适配层来屏蔽A核M核接口差异。 |
services/samgr_lite/samgr/registry | M核服务注册发现的桩函数。 |
services/samgr_lite/samgr/source | M核和A核系统服务开发框架基础代码。 |
services/samgr_lite/samgr_client | A核进程间服务调用的注册与发现。 |
services/samgr_lite/samgr_server | A核进程间服务调用的IPC地址管理和访问控制。 |
services/samgr_lite/samgr_endpoint | A核IPC通信消息收发包管理。 |
services/samgr_lite/communication/broadcast | M核和A核进程内事件广播服务。 |
typedef struct ExampleService {
INHERIT_SERVICE;
INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
Identity identity;
} ExampleService;
static const char *GetName(Service *service) { return EXAMPLE_SERVICE; } static BOOL Initialize(Service *service, Identity identity) { ExampleService *example = (ExampleService *)service; // 保存服务的唯一身份标识,用来自己的IUnknown接口对服务发消息时使用。 example->identity = identity; return TRUE; } static BOOL MessageHandle(Service *service, Request *msg) { ExampleService *example = (ExampleService *)service; switch (msg->msgId) { case MSG_SYNC: // 业务处理 break; default:break; } return FALSE; } static TaskConfig GetTaskConfig(Service *service) { TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, 0x800, 20, SHARED_TASK}; return config; }
static ExampleService g_example = {
.GetName = GetName,
.Initialize = Initialize,
.MessageHandle = MessageHandle,
.GetTaskConfig = GetTaskConfig,
SERVER_IPROXY_IMPL_BEGIN,
.Invoke = NULL,
.SyncCall = SyncCall,
IPROXY_END,
};
static void Init(void)
{
SAMGR_GetInstance()->RegisterService((Service *)&g_example);
SAMGR_GetInstance()->RegisterDefaultFeatureApi(EXAMPLE_SERVICE, GET_IUNKNOWN(g_example));
}
SYSEX_SERVICE_INIT(Init);
typedef struct DemoFeature {
INHERIT_FEATURE;
INHERIT_IUNKNOWNENTRY(DemoApi);
Identity identity;
Service *parent;
} DemoFeature;
static const char *FEATURE_GetName(Feature *feature) { return EXAMPLE_FEATURE; } static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity) { DemoFeature *demoFeature = (DemoFeature *)feature; demoFeature->identity = identity; demoFeature->parent = parent; } static void FEATURE_OnStop(Feature *feature, Identity identity) { g_example.identity.queueId = NULL; g_example.identity.featureId = -1; g_example.identity.serviceId = -1; } static BOOL FEATURE_OnMessage(Feature *feature, Request *request) { if (request->msgId == MSG_PROC) { Response response = {.data = "Yes, you did!", .len = 0}; SAMGR_SendResponse(request, &response); return TRUE; } else { if (request->msgId == MSG_TIME_PROC) { LOS_Msleep(WAIT_FEATURE_PROC * 10); if (request->msgValue) { SAMGR_PrintServices(); } else { SAMGR_PrintOperations(); } AsyncTimeCall(GET_IUNKNOWN(g_example)); return FALSE; } } return FALSE; }
static DemoFeature g_example = {
.GetName = FEATURE_GetName,
.OnInitialize = FEATURE_OnInitialize,
.OnStop = FEATURE_OnStop,
.OnMessage = FEATURE_OnMessage,
DEFAULT_IUNKNOWN_ENTRY_BEGIN,
.AsyncCall = AsyncCall,
.AsyncTimeCall = AsyncTimeCall,
.SyncCall = SyncCall,
.AsyncCallBack = AsyncCallBack,
DEFAULT_IUNKNOWN_ENTRY_END,
.identity = {-1, -1, NULL},
};
static void Init(void){
SAMGR_GetInstance()->RegisterFeature(EXAMPLE_SERVICE, (Feature *)&g_example);
SAMGR_GetInstance()->RegisterFeatureApi(EXAMPLE_SERVICE, EXAMPLE_FEATURE, GET_IUNKNOWN(g_example));
}
SYSEX_FEATURE_INIT(Init);
typedef struct DemoApi {
INHERIT_IUNKNOWN;
BOOL (*AsyncCall)(IUnknown *iUnknown, const char *buff);
BOOL (*AsyncTimeCall)(IUnknown *iUnknown);
BOOL (*SyncCall)(IUnknown *iUnknown, struct Payload *payload);
BOOL (*AsyncCallBack)(IUnknown *iUnknown, const char *buff, Handler handler);
} DemoApi;
typedef struct DemoRefApi {
INHERIT_IUNKNOWNENTRY(DemoApi);
} DemoRefApi;
static DemoRefApi api = {
DEFAULT_IUNKNOWN_ENTRY_BEGIN,
.AsyncCall = AsyncCall,
.AsyncTimeCall = AsyncTimeCall,
.SyncCall = SyncCall,
.AsyncCallBack = AsyncCallBack,
DEFAULT_IUNKNOWN_ENTRY_END,
};
SAMGR_GetInstance()->RegisterFeatureApi(EXAMPLE_SERVICE, EXAMPLE_FEATURE, GET_IUNKNOWN(api));
DemoApi *demoApi = NULL;
IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(EXAMPLE_SERVICE, EXAMPLE_FEATURE);
if (iUnknown == NULL) {
return NULL;
}
int result = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&demoApi);
if (result != 0 || demoApi == NULL) {
return NULL;
}
if (demoApi->AsyncCallBack == NULL) {
return NULL;
}
demoApi->AsyncCallBack((IUnknown *)demoApi, "I wanna async call callback good result!", AsyncHandler);
int32 ref = demoApi->Release((IUnknown *)demoApi);
typedef struct DemoFeatureApi {
INHERIT_SERVER_IPROXY;
BOOL (*AsyncCall)(IUnknown *iUnknown, const char *buff);
BOOL (*AsyncTimeCall)(IUnknown *iUnknown);
BOOL (*SyncCall)(IUnknown *iUnknown, struct Payload *payload);
BOOL (*AsyncCallBack)(IUnknown *iUnknown, const char *buff, IOwner notify, INotifyFunc handler);
} DemoFeatureApi;
static DemoFeature g_example = {
SERVER_IPROXY_IMPL_BEGIN,
.Invoke = Invoke,
.AsyncCall = AsyncCall,
.AsyncTimeCall = AsyncTimeCall,
.SyncCall = SyncCall,
.AsyncCallBack = AsyncCallBack,
IPROXY_END,
};
static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply) { DemoFeatureApi *api = (DemoFeatureApi *)iProxy; BOOL ret; size_t len = 0; switch (funcId) { case ID_ASYNCALL: ret = api->AsyncCall((IUnknown *)iProxy, (char *)IpcIoPopString(req, &len)); IpcIoPushBool(reply, ret); break; case ID_ASYNTIMECALL: ret = api->AsyncTimeCall((IUnknown *)iProxy); IpcIoPushBool(reply, ret); break; case ID_SYNCCALL: { struct Payload payload; payload.id = IpcIoPopInt32(req); payload.value = IpcIoPopInt32(req); payload.name = (char *)IpcIoPopString(req, &len); ret = api->SyncCall((IUnknown *)iProxy, &payload); IpcIoPushString(reply, ret ? "TRUE" : "FALSE"); } break; case ID_ASYNCCALLBACK: { // convert to sync proxy IpcIoPushString(reply, "Yes, you did!"); IpcIoPushBool(reply, TRUE); } break; default: IpcIoPushBool(reply, FALSE); break; } return EC_SUCCESS; }
SAMGR_GetInstance()->RegisterFeatureApi(EXAMPLE_SERVICE, EXAMPLE_FEATURE, GET_IUNKNOWN(g_example));
IClientProxy *demoApi = NULL;
IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(EXAMPLE_SERVICE, EXAMPLE_FEATURE);
if (iUnknown == NULL) {
return NULL;
}
int result = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&demoApi);
if (result != 0 || demoApi == NULL) {
return NULL;
}
IpcIo request;char data[250];
IpcIoInit(&request, data, sizeof(data), 0);
demoApi->Invoke(demoApi, 0, &request, NULL, NULL);
int32 ref = demoApi->Release((IUnknown *)demoApi);
typedef struct DemoClientProxy {
INHERIT_CLIENT_IPROXY;
BOOL (*AsyncCall)(IUnknown *iUnknown, const char *buff);
BOOL (*AsyncTimeCall)(IUnknown *iUnknown);
BOOL (*SyncCall)(IUnknown *iUnknown, struct Payload *payload);
BOOL (*AsyncCallBack)(IUnknown *iUnknown, const char *buff, IOwner notify, INotifyFunc handler);
} DemoClientProxy;
typedef struct DemoClientEntry {
INHERIT_IUNKNOWNENTRY(DemoClientProxy);
} DemoClientEntry;
static BOOL AsyncCall(IUnknown *iUnknown, const char *buff) { DemoClientProxy *proxy = (DemoClientProxy *)iUnknown; IpcIo request; char data[MAX_DATA_LEN]; IpcIoInit(&request, data, MAX_DATA_LEN, 0); IpcIoPushString(&request, buff); int ret = proxy->Invoke((IClientProxy *)proxy, ID_ASYNCALL, &request, NULL, NULL); return ret == EC_SUCCESS; } static BOOL AsyncTimeCall(IUnknown *iUnknown) { DemoClientProxy *proxy = (DemoClientProxy *)iUnknown; IpcIo request; char data[MAX_DATA_LEN]; IpcIoInit(&request, data, MAX_DATA_LEN, 0); int ret = proxy->Invoke((IClientProxy *)proxy, ID_ASYNTIMECALL, &request, NULL, NULL); return ret == EC_SUCCESS; } static int Callback(IOwner owner, int code, IpcIo *reply) { size_t len = 0; return strcpy_s(owner, MAX_DATA_LEN, (char *)IpcIoPopString(reply, &len)); } static BOOL SyncCall(IUnknown *iUnknown, struct Payload *payload) { DemoClientProxy *proxy = (DemoClientProxy *)iUnknown; IpcIo request; char data[MAX_DATA_LEN]; IpcIoInit(&request, data, MAX_DATA_LEN, 0); IpcIoPushInt32(&request, payload->id); IpcIoPushInt32(&request, payload->value); IpcIoPushString(&request, payload->name); int ret = proxy->Invoke((IClientProxy *)proxy, ID_SYNCCALL, &request, data, Callback); data[MAX_DATA_LEN - 1] = 0; HILOG_INFO(HILOG_MODULE_APP, "[TID:0x%lx]Remote response is %s!", pthread_self(), data); return ret == EC_SUCCESS; } struct CurrentNotify { IOwner notify; INotifyFunc handler; }; static int CurrentCallback(IOwner owner, int code, IpcIo *reply) { struct CurrentNotify *notify = (struct CurrentNotify *)owner; size_t len = 0; char *response = (char *)IpcIoPopString(reply, &len); HILOG_INFO(HILOG_MODULE_APP, "[TID:0x%lx]Notify Remote response is %s!", pthread_self(), response); notify->handler(notify->notify, response); return EC_SUCCESS; } static BOOL AsyncCallBack(IUnknown *iUnknown, const char *buff, IOwner notify, INotifyFunc handler) { struct CurrentNotify owner = {notify, handler}; DemoClientProxy *proxy = (DemoClientProxy *)iUnknown; IpcIo request; char data[MAX_DATA_LEN]; IpcIoInit(&request, data, MAX_DATA_LEN, 0); IpcIoPushString(&request, buff); int ret = proxy->Invoke((IClientProxy *)proxy, ID_ASYNCCALLBACK, &request, &owner, CurrentCallback); return ret == EC_SUCCESS; }
void *DEMO_CreatClient(const char *service, const char *feature, uint32 size) { (void)service; (void)feature; uint32 len = size + sizeof(DemoClientEntry); uint8 *client = malloc(len); (void)memset_s(client, len, 0, len); DemoClientEntry *entry = (DemoClientEntry *)&client[size]; entry->ver = ((uint16)CLIENT_PROXY_VER | (uint16)DEFAULT_VERSION); entry->ref = 1; entry->iUnknown.QueryInterface = IUNKNOWN_QueryInterface; entry->iUnknown.AddRef = IUNKNOWN_AddRef; entry->iUnknown.Release = IUNKNOWN_Release; entry->iUnknown.Invoke = NULL; entry->iUnknown.AsyncCall = AsyncCall; entry->iUnknown.AsyncTimeCall = AsyncTimeCall; entry->iUnknown.SyncCall = SyncCall; entry->iUnknown.AsyncCallBack = AsyncCallBack; return client; } void DEMO_DestroyClient(const char *service, const char *feature, void *iproxy) { free(iproxy); }
SAMGR_RegisterFactory(EXAMPLE_SERVICE, EXAMPLE_FEATURE, DEMO_CreatClient, DEMO_DestroyClient);
DemoClientProxy *demoApi = NULL;
IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(EXAMPLE_SERVICE, EXAMPLE_FEATURE);
if (iUnknown == NULL) {
return NULL;
}
int result = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&demoApi);
if (result != 0 || demoApi == NULL) {
return NULL;
}
if (demoApi->AsyncCallBack == NULL) {
return NULL;
}
demoApi->AsyncCallBack((IUnknown *)demoApi,
"I wanna async call callback good result!", NULL, AsyncHandler);
int32 ref = demoApi->Release((IUnknown *)demoApi);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。