diff --git a/services/client/communication_adapter/source/sa_client.cpp b/services/client/communication_adapter/source/sa_client.cpp index 6bf8008b7161552a22e86e8ae5104750d37c0008..cee11364a7d7011074720535851d835b7146eaa5 100644 --- a/services/client/communication_adapter/source/sa_client.cpp +++ b/services/client/communication_adapter/source/sa_client.cpp @@ -27,8 +27,47 @@ namespace OHOS { namespace AI { -std::mutex SaClient::instance_mutex_; -SaClient *SaClient::instance_ = nullptr; +namespace { +int32_t AsyncCallback(const IpcContext *ipcContext, void *ipcMsg, IpcIo *data, void *arg) +{ + // the code is callback function id, was defined in ai_service.h + uint32_t code; + int32_t ipcGetCodeRet = GetCode(ipcMsg, &code); + int asyncCallbackRet = IpcIoPopInt32(data); + int requestId = IpcIoPopInt32(data); + int sessionId = IpcIoPopInt32(data); + DataInfo outputInfo = { + .data = nullptr, + .length = 0, + }; + int ipcUnParcelRet = UnParcelDataInfo(data, &outputInfo); + SaClient *client = SaClient::GetInstance(); + if (client == nullptr) { + HILOGE("[SaClient]The client is nullptr, maybe out of memory."); + FreeBuffer(nullptr, ipcMsg); + FreeDataInfo(&outputInfo); + return RETCODE_FAILURE; + } + CallbackHandle callback = client->GetSaClientResultCb(); + if (callback == nullptr) { + HILOGE("[SaClient]SA client callback is nullptr, maybe Release interface is called or the callback is deleted"); + FreeBuffer(nullptr, ipcMsg); + FreeDataInfo(&outputInfo); + return RETCODE_FAILURE; + } + // The asynchronous callback retCode is used only when the IPC is normal. + int retCode = asyncCallbackRet; + if (ipcGetCodeRet != LITEIPC_OK || ipcUnParcelRet != RETCODE_SUCCESS) { + HILOGE("[SaClient]AsyncCallback failed, GetCode retCode[%d], UnParcelDataInfo retCode[%d].", ipcGetCodeRet, + ipcUnParcelRet); + // The IPC is abnormal. + retCode = RETCODE_FAILURE; + } + callback(sessionId, outputInfo, retCode, requestId); + FreeBuffer(nullptr, ipcMsg); + FreeDataInfo(&outputInfo); + return retCode; +} int32_t OnAiDead(const IpcContext *context, void *ipcMsg, IpcIo *data, void *arg) { @@ -44,6 +83,10 @@ int32_t OnAiDead(const IpcContext *context, void *ipcMsg, IpcIo *data, void *arg onDead(); return RETCODE_SUCCESS; } +} // anonymous namespaces + +std::mutex SaClient::instance_mutex_; +SaClient *SaClient::instance_ = nullptr; SaClient::SaClient() = default; @@ -172,47 +215,6 @@ int SaClient::GetOption(const ClientInfo &clientInfo, int optionType, const Data return GetOptionProxy(*proxy_, clientInfo, optionType, inputInfo, outputInfo); } -int32_t AsyncCallback(const IpcContext *ipcContext, void *ipcMsg, IpcIo *data, void *arg) -{ - // the code is callback function id, was defined in ai_service.h - uint32_t code; - int32_t ipcGetCodeRet = GetCode(ipcMsg, &code); - int asyncCallbackRet = IpcIoPopInt32(data); - int requestId = IpcIoPopInt32(data); - int sessionId = IpcIoPopInt32(data); - DataInfo outputInfo = { - .data = nullptr, - .length = 0, - }; - int ipcUnParcelRet = UnParcelDataInfo(data, &outputInfo); - SaClient *client = SaClient::GetInstance(); - if (client == nullptr) { - HILOGE("[SaClient]The client is nullptr, maybe out of memory."); - FreeBuffer(nullptr, ipcMsg); - FreeDataInfo(&outputInfo); - return RETCODE_FAILURE; - } - CallbackHandle callback = client->GetSaClientResultCb(); - if (callback == nullptr) { - HILOGE("[SaClient]SA client callback is nullptr, maybe Release interface is called or the callback is deleted"); - FreeBuffer(nullptr, ipcMsg); - FreeDataInfo(&outputInfo); - return RETCODE_FAILURE; - } - // The asynchronous callback retCode is used only when the IPC is normal. - int retCode = asyncCallbackRet; - if (ipcGetCodeRet != LITEIPC_OK || ipcUnParcelRet != RETCODE_SUCCESS) { - HILOGE("[SaClient]AsyncCallback failed, GetCode retCode[%d], UnParcelDataInfo retCode[%d].", ipcGetCodeRet, - ipcUnParcelRet); - // The IPC is abnormal. - retCode = RETCODE_FAILURE; - } - callback(sessionId, outputInfo, retCode, requestId); - FreeBuffer(nullptr, ipcMsg); - FreeDataInfo(&outputInfo); - return retCode; -} - int SaClient::RegisterCallback(const ClientInfo &clientInfo) { if (proxy_ == nullptr) { diff --git a/services/client/communication_adapter/source/sa_client_proxy.cpp b/services/client/communication_adapter/source/sa_client_proxy.cpp index e403313d064ef10e4a2e1685c3d59dbba8a3f273..f51116122e58c192f40acaf1f479299b95690881 100644 --- a/services/client/communication_adapter/source/sa_client_proxy.cpp +++ b/services/client/communication_adapter/source/sa_client_proxy.cpp @@ -28,6 +28,7 @@ namespace OHOS { namespace AI { +namespace { SvcIdentity g_sid; struct Notify { @@ -41,29 +42,7 @@ struct NotifyBuff { unsigned char *outBuff; }; -extern "C" void __attribute__((weak)) HOS_SystemInit(void) -{ -}; - -void HosInit() -{ - HOS_SystemInit(); -} - -IClientProxy *GetRemoteIUnknown(void) -{ - IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(AI_SERVICE); - if (iUnknown == nullptr) { - HILOGE("[SaClientProxy][TID:0x%lx][GetDefaultFeatureApi S:%s]: error is null.", - pthread_self(), AI_SERVICE); - return nullptr; - } - IClientProxy *proxy = nullptr; - (void)iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&proxy); - return proxy; -} - -static int Callback(void *owner, int code, IpcIo *reply) +int Callback(void *owner, int code, IpcIo *reply) { HILOGI("[SaClientProxy]Callback start."); if (owner == nullptr) { @@ -75,7 +54,7 @@ static int Callback(void *owner, int code, IpcIo *reply) return RETCODE_SUCCESS; } -static int CallbackBuff(void *owner, int code, IpcIo *reply) +int CallbackBuff(void *owner, int code, IpcIo *reply) { HILOGI("[SaClientProxy]CallbackBuff start."); if (owner == nullptr) { @@ -98,7 +77,7 @@ static int CallbackBuff(void *owner, int code, IpcIo *reply) return notify->ipcRetCode; } -static void ParcelClientInfo(IpcIo *request, const ClientInfo &clientInfo) +void ParcelClientInfo(IpcIo *request, const ClientInfo &clientInfo) { IpcIoPushInt64(request, clientInfo.clientVersion); IpcIoPushInt32(request, clientInfo.clientId); @@ -120,6 +99,29 @@ void ParcelAlgorithmInfo(IpcIo *request, const AlgorithmInfo &algorithmInfo) DataInfo dataInfo {algorithmInfo.extendMsg, algorithmInfo.extendLen}; ParcelDataInfo(request, &dataInfo); } +} // anonymous namespace + +extern "C" void __attribute__((weak)) HOS_SystemInit(void) +{ +}; + +void HosInit() +{ + HOS_SystemInit(); +} + +IClientProxy *GetRemoteIUnknown(void) +{ + IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(AI_SERVICE); + if (iUnknown == nullptr) { + HILOGE("[SaClientProxy][TID:0x%lx][GetDefaultFeatureApi S:%s]: error is null.", + pthread_self(), AI_SERVICE); + return nullptr; + } + IClientProxy *proxy = nullptr; + (void)iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&proxy); + return proxy; +} int InitSaEngine(IClientProxy &proxy, const ConfigInfo &configInfo) { diff --git a/services/server/communication_adapter/include/client_listener_handler.h b/services/server/communication_adapter/include/client_listener_handler.h index 848f362002bb0377d12b8e8bb31896b3619b60df..c8224fba80231662a9763d6d91b5f6005c6a60a0 100755 --- a/services/server/communication_adapter/include/client_listener_handler.h +++ b/services/server/communication_adapter/include/client_listener_handler.h @@ -27,9 +27,6 @@ namespace OHOS { namespace AI { -const char * const ASYNC_PROCESS_WORKER = "AsyncProcessWorker"; -const int EVENT_WAIT_TIME_MS = 1000; - class ClientListenerHandler; /** @@ -91,4 +88,4 @@ private: } // namespace AI } // namespace OHOS -#endif // CLIENT_LISTENER_HANDLER_H \ No newline at end of file +#endif // CLIENT_LISTENER_HANDLER_H diff --git a/services/server/communication_adapter/source/client_listener_handler.cpp b/services/server/communication_adapter/source/client_listener_handler.cpp index a0c4ead1fb8f54e88ec0b888552b86bb49fc5a98..a3afaea2cb26e947dff76389e075e5b45a0016a3 100755 --- a/services/server/communication_adapter/source/client_listener_handler.cpp +++ b/services/server/communication_adapter/source/client_listener_handler.cpp @@ -27,6 +27,11 @@ namespace OHOS { namespace AI { +namespace { +const char * const ASYNC_PROCESS_WORKER = "AsyncProcessWorker"; +const int EVENT_WAIT_TIME_MS = 1000; +} // anonymous namespace + AsyncProcessWorker::AsyncProcessWorker(ClientListenerHandler *handler, int clientId, SaServerAdapter *adapter) : handler_(handler), clientId_(clientId), adapter_(adapter) {