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: %s, dhId: %s, status: %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: %s dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
57 prctl(PR_SET_NAME, REGISTER_SERVICE_NOTIFY.c_str());
58 DCameraIndex camIndex(devId, dhId);
59 DistributedCameraSourceService::CamDevErase(camIndex);
60 if (callbackProxy_ == nullptr) {
61 DHLOGE("callbackProxy_ is nullptr");
62 return;
63 }
64 int32_t ret = callbackProxy_->OnNotifyRegResult(devId, dhId, reqId, status, data);
65 if (ret != DCAMERA_OK) {
66 DHLOGE("OnNotifyRegResult failed: %d", ret);
67 }
68 }).detach();
69 } else {
70 if (callbackProxy_ == nullptr) {
71 DHLOGE("callbackProxy_ is nullptr");
72 return DCAMERA_BAD_VALUE;
73 }
74 int32_t ret = callbackProxy_->OnNotifyRegResult(devId, dhId, reqId, status, data);
75 if (ret != DCAMERA_OK) {
76 DHLOGE("OnNotifyRegResult failed: %d", ret);
77 }
78 }
79 return DCAMERA_OK;
80 }
81
OnUnregisterNotify(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,std::string & data)82 int32_t DCameraServiceStateListener::OnUnregisterNotify(const std::string& devId, const std::string& dhId,
83 const std::string& reqId, int32_t status, std::string& data)
84 {
85 DHLOGI("OnUnregisterNotify devId: %s, dhId: %s, status: %d",
86 GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), status);
87 std::lock_guard<std::mutex> autoLock(proxyMutex_);
88 if (callbackProxy_ == nullptr) {
89 DHLOGE("callbackProxy_ is nullptr");
90 return DCAMERA_BAD_VALUE;
91 }
92
93 if (status == DCAMERA_OK) {
94 std::thread([=]() mutable {
95 DHLOGI("thread delete devId: %s dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
96 prctl(PR_SET_NAME, UNREGISTER_SERVICE_NOTIFY.c_str());
97 DCameraIndex camIndex(devId, dhId);
98 DistributedCameraSourceService::CamDevErase(camIndex);
99
100 int32_t ret = callbackProxy_->OnNotifyUnregResult(devId, dhId, reqId, status, data);
101 if (ret != DCAMERA_OK) {
102 DHLOGE("OnNotifyUnregResult failed, ret: %d", ret);
103 }
104 }).detach();
105 } else {
106 int32_t ret = callbackProxy_->OnNotifyUnregResult(devId, dhId, reqId, status, data);
107 if (ret != DCAMERA_OK) {
108 DHLOGE("OnNotifyUnregResult failed, ret: %d", ret);
109 }
110 }
111
112 return DCAMERA_OK;
113 }
114 } // namespace DistributedHardware
115 } // namespace OHOS
116