• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "dcamera_source_callback.h"
17 
18 #include "anonymous_string.h"
19 #include "distributed_camera_errno.h"
20 #include "distributed_hardware_log.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
~DCameraSourceCallback()24 DCameraSourceCallback::~DCameraSourceCallback()
25 {
26     regCallbacks_.clear();
27     unregCallbacks_.clear();
28 }
29 
OnNotifyRegResult(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,std::string & data)30 int32_t DCameraSourceCallback::OnNotifyRegResult(const std::string& devId, const std::string& dhId,
31     const std::string& reqId, int32_t status, std::string& data)
32 {
33     DHLOGI("DCameraSourceCallback OnNotifyRegResult devId: %s dhId: %s",
34         GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
35     std::lock_guard<std::mutex> lock(mapMutex_);
36     auto iter = regCallbacks_.find(reqId);
37     if (iter == regCallbacks_.end()) {
38         DHLOGE("DCameraSourceCallback OnNotifyRegResult not found devId: %s dhId: %s",
39             GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
40         return DCAMERA_NOT_FOUND;
41     }
42     int32_t ret = iter->second->OnRegisterResult(devId, dhId, status, data);
43     if (ret != DCAMERA_OK) {
44         DHLOGE("DCameraSourceCallback OnNotifyRegResult failed, devId: %s dhId: %s ret: %d",
45             GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), ret);
46     }
47     regCallbacks_.erase(iter);
48     return ret;
49 }
50 
OnNotifyUnregResult(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,std::string & data)51 int32_t DCameraSourceCallback::OnNotifyUnregResult(const std::string& devId, const std::string& dhId,
52     const std::string& reqId, int32_t status, std::string& data)
53 {
54     DHLOGI("DCameraSourceCallback OnNotifyUnregResult devId: %s dhId: %s",
55         GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
56     std::lock_guard<std::mutex> lock(mapMutex_);
57     auto iter = unregCallbacks_.find(reqId);
58     if (iter == unregCallbacks_.end()) {
59         DHLOGE("DCameraSourceCallback OnNotifyUnregResult not found devId: %s dhId: %s",
60             GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
61         return DCAMERA_NOT_FOUND;
62     }
63     int32_t ret = iter->second->OnUnregisterResult(devId, dhId, status, data);
64     if (ret != DCAMERA_OK) {
65         DHLOGE("DCameraSourceCallback OnNotifyUnregResult failed, devId: %s dhId: %s ret: %d",
66             GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), ret);
67     }
68     unregCallbacks_.erase(iter);
69     return ret;
70 }
71 
PushRegCallback(std::string & reqId,std::shared_ptr<RegisterCallback> & callback)72 void DCameraSourceCallback::PushRegCallback(std::string& reqId, std::shared_ptr<RegisterCallback>& callback)
73 {
74     std::lock_guard<std::mutex> lock(mapMutex_);
75     regCallbacks_.emplace(reqId, callback);
76 }
77 
PopRegCallback(std::string & reqId)78 void DCameraSourceCallback::PopRegCallback(std::string& reqId)
79 {
80     std::lock_guard<std::mutex> lock(mapMutex_);
81     regCallbacks_.erase(reqId);
82 }
83 
PushUnregCallback(std::string & reqId,std::shared_ptr<UnregisterCallback> & callback)84 void DCameraSourceCallback::PushUnregCallback(std::string& reqId, std::shared_ptr<UnregisterCallback>& callback)
85 {
86     std::lock_guard<std::mutex> lock(mapMutex_);
87     unregCallbacks_.emplace(reqId, callback);
88 }
89 
PopUnregCallback(std::string & reqId)90 void DCameraSourceCallback::PopUnregCallback(std::string& reqId)
91 {
92     std::lock_guard<std::mutex> lock(mapMutex_);
93     unregCallbacks_.erase(reqId);
94 }
95 } // namespace DistributedHardware
96 } // namespace OHOS
97