1 /*
2  * Copyright (c) 2022-2024 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 "dscreen_source_callback.h"
17 
18 #include "dscreen_errcode.h"
19 #include "dscreen_log.h"
20 #include "dscreen_util.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
OnNotifyRegResult(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,const std::string & data)24 int32_t DScreenSourceCallback::OnNotifyRegResult(const std::string &devId, const std::string &dhId,
25     const std::string &reqId, int32_t status, const std::string &data)
26 {
27     DHLOGI("DScreenSourceCallback OnNotifyRegResult devId: %{public}s dhId: %{public}s status: %{public}" PRId32,
28         GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), status);
29     std::lock_guard<std::mutex> lock(registerMutex_);
30     const auto iter = registerCallbackMap_.find(reqId);
31     if (iter != registerCallbackMap_.end()) {
32         if (iter->second == nullptr) {
33             DHLOGE("DScreenSourceCallback Regcallback is null.");
34             return ERR_DH_SCREEN_SA_REGISTERCALLBACK_NOT_FOUND;
35         }
36         iter->second->OnRegisterResult(devId, dhId, status, data);
37         registerCallbackMap_.erase(reqId);
38         return DH_SUCCESS;
39     }
40 
41     return ERR_DH_SCREEN_SA_REGISTERCALLBACK_NOT_FOUND;
42 }
43 
OnNotifyUnregResult(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,const std::string & data)44 int32_t DScreenSourceCallback::OnNotifyUnregResult(const std::string &devId, const std::string &dhId,
45     const std::string &reqId, int32_t status, const std::string &data)
46 {
47     DHLOGI("DScreenSourceCallback OnNotifyUnregResult devId: %{public}s dhId: %{public}s status: %{public}" PRId32,
48         GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), status);
49     std::lock_guard<std::mutex> lock(unregisterMutex_);
50     const auto iter = unregisterCallbackMap_.find(reqId);
51     if (iter != unregisterCallbackMap_.end()) {
52         if (iter->second == nullptr) {
53             DHLOGE("DScreenSourceCallback Unregcallback is null.");
54             return ERR_DH_SCREEN_SA_UNREGISTERCALLBACK_NOT_FOUND;
55         }
56         iter->second->OnUnregisterResult(devId, dhId, status, data);
57         unregisterCallbackMap_.erase(reqId);
58         return DH_SUCCESS;
59     }
60 
61     return ERR_DH_SCREEN_SA_UNREGISTERCALLBACK_NOT_FOUND;
62 }
63 
PushRegRegisterCallback(const std::string & reqId,const std::shared_ptr<RegisterCallback> & callback)64 void DScreenSourceCallback::PushRegRegisterCallback(const std::string &reqId,
65     const std::shared_ptr<RegisterCallback> &callback)
66 {
67     DHLOGD("PushRegRegisterCallback");
68     std::lock_guard<std::mutex> lock(registerMutex_);
69     registerCallbackMap_.emplace(reqId, callback);
70 }
71 
PopRegRegisterCallback(const std::string & reqId)72 void DScreenSourceCallback::PopRegRegisterCallback(const std::string &reqId)
73 {
74     DHLOGD("PopRegRegisterCallback");
75     std::lock_guard<std::mutex> lock(registerMutex_);
76     registerCallbackMap_.erase(reqId);
77 }
78 
PushUnregisterCallback(const std::string & reqId,const std::shared_ptr<UnregisterCallback> & callback)79 void DScreenSourceCallback::PushUnregisterCallback(const std::string &reqId,
80     const std::shared_ptr<UnregisterCallback> &callback)
81 {
82     DHLOGD("PushUnregisterCallback");
83     std::lock_guard<std::mutex> lock(unregisterMutex_);
84     unregisterCallbackMap_.emplace(reqId, callback);
85 }
86 
PopUnregisterCallback(const std::string & reqId)87 void DScreenSourceCallback::PopUnregisterCallback(const std::string &reqId)
88 {
89     DHLOGD("PopUnregisterCallback");
90     std::lock_guard<std::mutex> lock(unregisterMutex_);
91     unregisterCallbackMap_.erase(reqId);
92 }
93 }  // namespace DistributedHardware
94 }  // namespace OHOS
95