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_service_state_listener.h"
17
18 #include <thread>
19
20 #include "anonymous_string.h"
21 #include "dcamera_index.h"
22 #include "distributed_camera_errno.h"
23 #include "distributed_camera_source_service.h"
24 #include "distributed_hardware_log.h"
25 #include <sys/prctl.h>
26
27 namespace OHOS {
28 namespace DistributedHardware {
DCameraServiceStateListener()29 DCameraServiceStateListener::DCameraServiceStateListener()
30 {
31 DHLOGI("DCameraServiceStateListener Create");
32 }
33
~DCameraServiceStateListener()34 DCameraServiceStateListener::~DCameraServiceStateListener()
35 {
36 DHLOGI("DCameraServiceStateListener Delete");
37 callbackProxy_ = nullptr;
38 }
39
SetCallback(sptr<IDCameraSourceCallback> callback)40 void DCameraServiceStateListener::SetCallback(sptr<IDCameraSourceCallback> callback)
41 {
42 DHLOGI("enter");
43 std::lock_guard<std::mutex> autoLock(proxyMutex_);
44 callbackProxy_ = callback;
45 }
46
OnRegisterNotify(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,std::string & data)47 int32_t DCameraServiceStateListener::OnRegisterNotify(const std::string& devId, const std::string& dhId,
48 const std::string& reqId, int32_t status, std::string& data)
49 {
50 DHLOGI("OnRegisterNotify devId: %{public}s, dhId: %{public}s, status: %{public}d",
51 GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), status);
52 std::lock_guard<std::mutex> autoLock(proxyMutex_);
53
54 if (status != DCAMERA_OK) {
55 std::thread([=]() mutable {
56 DHLOGI("thread delete devId: %{public}s dhId: %{public}s", GetAnonyString(devId).c_str(),
57 GetAnonyString(dhId).c_str());
58 prctl(PR_SET_NAME, REGISTER_SERVICE_NOTIFY.c_str());
59 DCameraIndex camIndex(devId, dhId);
60 DistributedCameraSourceService::CamDevErase(camIndex);
61 if (callbackProxy_ == nullptr) {
62 DHLOGE("callbackProxy_ is nullptr");
63 return;
64 }
65 int32_t ret = callbackProxy_->OnNotifyRegResult(devId, dhId, reqId, status, data);
66 if (ret != DCAMERA_OK) {
67 DHLOGE("OnNotifyRegResult failed: %{public}d", ret);
68 }
69 }).detach();
70 } else {
71 if (callbackProxy_ == nullptr) {
72 DHLOGE("callbackProxy_ is nullptr");
73 return DCAMERA_BAD_VALUE;
74 }
75 int32_t ret = callbackProxy_->OnNotifyRegResult(devId, dhId, reqId, status, data);
76 if (ret != DCAMERA_OK) {
77 DHLOGE("OnNotifyRegResult failed: %{public}d", ret);
78 }
79 }
80 return DCAMERA_OK;
81 }
82
OnUnregisterNotify(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,std::string & data)83 int32_t DCameraServiceStateListener::OnUnregisterNotify(const std::string& devId, const std::string& dhId,
84 const std::string& reqId, int32_t status, std::string& data)
85 {
86 DHLOGI("OnUnregisterNotify devId: %{public}s, dhId: %{public}s, status: %{public}d",
87 GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), status);
88 std::lock_guard<std::mutex> autoLock(proxyMutex_);
89 if (callbackProxy_ == nullptr) {
90 DHLOGE("callbackProxy_ is nullptr");
91 return DCAMERA_BAD_VALUE;
92 }
93
94 if (status == DCAMERA_OK) {
95 std::thread([=]() mutable {
96 DHLOGI("thread delete devId: %{public}s dhId: %{public}s", GetAnonyString(devId).c_str(),
97 GetAnonyString(dhId).c_str());
98 prctl(PR_SET_NAME, UNREGISTER_SERVICE_NOTIFY.c_str());
99 DCameraIndex camIndex(devId, dhId);
100 DistributedCameraSourceService::CamDevErase(camIndex);
101
102 int32_t ret = callbackProxy_->OnNotifyUnregResult(devId, dhId, reqId, status, data);
103 if (ret != DCAMERA_OK) {
104 DHLOGE("OnNotifyUnregResult failed, ret: %{public}d", ret);
105 }
106 }).detach();
107 } else {
108 int32_t ret = callbackProxy_->OnNotifyUnregResult(devId, dhId, reqId, status, data);
109 if (ret != DCAMERA_OK) {
110 DHLOGE("OnNotifyUnregResult failed, ret: %{public}d", ret);
111 }
112 }
113
114 return DCAMERA_OK;
115 }
116
OnHardwareStateChanged(const std::string & devId,const std::string & dhId,int32_t status)117 int32_t DCameraServiceStateListener::OnHardwareStateChanged(const std::string &devId,
118 const std::string &dhId, int32_t status)
119 {
120 DHLOGI("OnHardwareStateChanged devId: %{public}s, dhId: %{public}s, status: %{public}d",
121 GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), status);
122 std::lock_guard<std::mutex> autoLock(proxyMutex_);
123 if (callbackProxy_ == nullptr) {
124 DHLOGE("callbackProxy_ is nullptr");
125 return DCAMERA_BAD_VALUE;
126 }
127 return callbackProxy_->OnHardwareStateChanged(devId, dhId, status);
128 }
129
OnDataSyncTrigger(const std::string & devId)130 int32_t DCameraServiceStateListener::OnDataSyncTrigger(const std::string &devId)
131 {
132 DHLOGI("OnDataSyncTrigger devId: %{public}s.", GetAnonyString(devId).c_str());
133 std::lock_guard<std::mutex> autoLock(proxyMutex_);
134 if (callbackProxy_ == nullptr) {
135 DHLOGE("callbackProxy_ is nullptr");
136 return DCAMERA_BAD_VALUE;
137 }
138 return callbackProxy_->OnDataSyncTrigger(devId);
139 }
140 } // namespace DistributedHardware
141 } // namespace OHOS
142