1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "communication_adapter/include/sa_async_handler.h"
17 #include "communication_adapter/include/sa_client.h"
18 #include "communication_adapter/include/sa_client_adapter.h"
19 #include "protocol/retcode_inner/aie_retcode_inner.h"
20 #include "utils/log/aie_log.h"
21 
22 namespace OHOS {
23 namespace AI {
SaClientCallback(int sessionId,const DataInfo & result,int resultCode,int requestId)24 void SaClientCallback(int sessionId, const DataInfo &result, int resultCode, int requestId)
25 {
26     AsyncHandler *asyncHandler = SaAsyncHandler::GetInstance();
27     if (asyncHandler == nullptr) {
28         HILOGE("[SaAsyncHandler][callback]AsyncHandler is nullptr. session id[%d]", sessionId);
29         return;
30     }
31     HILOGD("[SaAsyncHandler][callback]End to get instance of asyncHandler, session id[%d]", sessionId);
32     asyncHandler->OnResult(sessionId, result, resultCode, requestId);
33 }
34 
SaDeathCallback()35 void SaDeathCallback()
36 {
37     SaClientAdapter *clientAdapter = SaClientAdapter::GetInstance();
38     if (clientAdapter == nullptr) {
39         HILOGE("[SaAsyncHandler][DeathCallback]The clientAdapter is nullptr");
40         return;
41     }
42     clientAdapter->ResetClient();
43     AsyncHandler *asyncHandler = SaAsyncHandler::GetInstance();
44     if (asyncHandler == nullptr) {
45         HILOGE("[SaAsyncHandler][DeathCallback]AsyncHandler is nullptr");
46         return;
47     }
48     HILOGD("[SaAsyncHandler][DeathCallback]Push death callback to client executor");
49     asyncHandler->OnDead();
50 }
51 
52 std::mutex SaAsyncHandler::instance_mutex_;
53 SaAsyncHandler *SaAsyncHandler::instance_ = nullptr;
54 
SaAsyncHandler()55 SaAsyncHandler::SaAsyncHandler()
56 {
57 }
58 
~SaAsyncHandler()59 SaAsyncHandler::~SaAsyncHandler()
60 {
61 }
62 
GetInstance()63 SaAsyncHandler *SaAsyncHandler::GetInstance()
64 {
65     CHK_RET(instance_ != nullptr, instance_);
66     std::lock_guard<std::mutex> lock(instance_mutex_);
67     CHK_RET(instance_ != nullptr, instance_);
68     AIE_NEW(instance_, SaAsyncHandler);
69     return instance_;
70 }
71 
ReleaseInstance()72 void SaAsyncHandler::ReleaseInstance()
73 {
74     std::lock_guard<std::mutex> lock(instance_mutex_);
75     AIE_DELETE(instance_);
76 }
77 
RegisterAsyncClientCb()78 int SaAsyncHandler::RegisterAsyncClientCb()
79 {
80     SaClient *saClient = SaClient::GetInstance();
81     CHK_RET(saClient == nullptr, RETCODE_NULL_PARAM);
82     saClient->RegisterSaClientCb(SaClientCallback);
83     return RETCODE_SUCCESS;
84 }
85 
UnRegisterAsyncClientCb()86 int SaAsyncHandler::UnRegisterAsyncClientCb()
87 {
88     SaClient *saClient = SaClient::GetInstance();
89     CHK_RET(saClient == nullptr, RETCODE_NULL_PARAM);
90     saClient->UnRegisterSaClientCb();
91     return RETCODE_SUCCESS;
92 }
93 
RegisterServiceDeathCb()94 int SaAsyncHandler::RegisterServiceDeathCb()
95 {
96     SaClient *saClient = SaClient::GetInstance();
97     CHK_RET(saClient == nullptr, RETCODE_NULL_PARAM);
98     saClient->RegisterSaDeathCb(SaDeathCallback);
99     return RETCODE_SUCCESS;
100 }
101 
UnRegisterServiceDeathCb()102 int SaAsyncHandler::UnRegisterServiceDeathCb()
103 {
104     SaClient *saClient = SaClient::GetInstance();
105     CHK_RET(saClient == nullptr, RETCODE_NULL_PARAM);
106     saClient->UnRegisterSaDeathCb();
107     return RETCODE_SUCCESS;
108 }
109 } // namespace AI
110 } // namespace OHOS