1 /*
2 * Copyright (c) 2022 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 "daudio_ipc_callback.h"
17
18 #include "daudio_constants.h"
19 #include "daudio_errorcode.h"
20 #include "daudio_log.h"
21 #include "daudio_util.h"
22
23 #undef DH_LOG_TAG
24 #define DH_LOG_TAG "DAudioIpcCallback"
25
26 namespace OHOS {
27 namespace DistributedHardware {
OnNotifyRegResult(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,const std::string & resultData)28 int32_t DAudioIpcCallback::OnNotifyRegResult(const std::string &devId, const std::string &dhId,
29 const std::string &reqId, int32_t status, const std::string &resultData)
30 {
31 DHLOGI("On notify the registration result, devId: %s, dhId: %s, status: %d, resultData: %s, reqId: %s",
32 GetAnonyString(devId).c_str(), dhId.c_str(), status, resultData.c_str(), reqId.c_str());
33
34 if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN ||
35 reqId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
36 return ERR_DH_AUDIO_SA_DEVID_ILLEGAL;
37 }
38 std::lock_guard<std::mutex> registerLck(registerMapMtx_);
39 auto iter = registerCallbackMap_.find(reqId);
40 if (iter != registerCallbackMap_.end()) {
41 iter->second->OnRegisterResult(devId, dhId, status, resultData);
42 registerCallbackMap_.erase(reqId);
43 return DH_SUCCESS;
44 }
45
46 return ERR_DH_AUDIO_SA_REGISTERCALLBACK_NOT_FOUND;
47 }
48
OnNotifyUnregResult(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,const std::string & resultData)49 int32_t DAudioIpcCallback::OnNotifyUnregResult(const std::string &devId, const std::string &dhId,
50 const std::string &reqId, int32_t status, const std::string &resultData)
51 {
52 DHLOGI("On notify the unregistration result, devId: %s, dhId: %s, status: %d, resultData: %s, reqId: %s",
53 GetAnonyString(devId).c_str(), dhId.c_str(), status, resultData.c_str(), reqId.c_str());
54
55 if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN ||
56 reqId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
57 return ERR_DH_AUDIO_SA_DEVID_ILLEGAL;
58 }
59 std::lock_guard<std::mutex> registerLck(unregisterMapMtx_);
60 auto iter = unregisterCallbackMap_.find(reqId);
61 if (iter != unregisterCallbackMap_.end()) {
62 iter->second->OnUnregisterResult(devId, dhId, status, resultData);
63 unregisterCallbackMap_.erase(reqId);
64 return DH_SUCCESS;
65 }
66 return ERR_DH_AUDIO_SA_UNREGISTERCALLBACK_NOT_FOUND;
67 }
68
PushRegisterCallback(const std::string & reqId,const std::shared_ptr<RegisterCallback> & callback)69 void DAudioIpcCallback::PushRegisterCallback(const std::string &reqId,
70 const std::shared_ptr<RegisterCallback> &callback)
71 {
72 DHLOGD("Push register callback, reqId: %s", reqId.c_str());
73 std::lock_guard<std::mutex> registerLck(registerMapMtx_);
74 registerCallbackMap_.emplace(reqId, callback);
75 }
76
PopRegisterCallback(const std::string & reqId)77 void DAudioIpcCallback::PopRegisterCallback(const std::string &reqId)
78 {
79 DHLOGD("Pop register callback, reqId: %s", reqId.c_str());
80 std::lock_guard<std::mutex> registerLck(registerMapMtx_);
81 registerCallbackMap_.erase(reqId);
82 }
83
PushUnregisterCallback(const std::string & reqId,const std::shared_ptr<UnregisterCallback> & callback)84 void DAudioIpcCallback::PushUnregisterCallback(const std::string &reqId,
85 const std::shared_ptr<UnregisterCallback> &callback)
86 {
87 DHLOGD("Push unregister callback, reqId: %s", reqId.c_str());
88 std::lock_guard<std::mutex> registerLck(unregisterMapMtx_);
89 unregisterCallbackMap_.emplace(reqId, callback);
90 }
91
PopUnregisterCallback(const std::string & reqId)92 void DAudioIpcCallback::PopUnregisterCallback(const std::string &reqId)
93 {
94 DHLOGD("Pop unregister callback, reqId: %s", reqId.c_str());
95 std::lock_guard<std::mutex> registerLck(unregisterMapMtx_);
96 unregisterCallbackMap_.erase(reqId);
97 }
98 } // DistributedHardware
99 } // OHOS