1 /*
2 * Copyright (c) 2022-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 "js_coordination_manager.h"
17
18 #include "devicestatus_define.h"
19 #include "interaction_manager.h"
20 #include "util_napi_error.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "JsCoordinationManager"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28
Prepare(napi_env env,bool isCompatible,napi_value handle)29 napi_value JsCoordinationManager::Prepare(napi_env env, bool isCompatible, napi_value handle)
30 {
31 CALL_INFO_TRACE;
32 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
33 CHKPP(cb);
34 napi_value result = CreateCallbackInfo(env, handle, cb);
35 auto callback = [this, cb](const std::string &networkId, const CoordinationMsgInfo &msgInfo) {
36 this->EmitJsPrepare(cb, networkId, msgInfo);
37 };
38 int32_t errCode = INTERACTION_MGR->PrepareCoordination(callback, isCompatible);
39 if (errCode != RET_OK) {
40 UtilNapiError::HandleExecuteResult(env, errCode, "prepare", COOPERATE_PERMISSION);
41 RELEASE_CALLBACKINFO(cb->env, cb->ref);
42 }
43 return result;
44 }
45
Unprepare(napi_env env,bool isCompatible,napi_value handle)46 napi_value JsCoordinationManager::Unprepare(napi_env env, bool isCompatible, napi_value handle)
47 {
48 CALL_INFO_TRACE;
49 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
50 CHKPP(cb);
51 napi_value result = CreateCallbackInfo(env, handle, cb);
52 auto callback = [this, cb](const std::string &networkId, const CoordinationMsgInfo &msgInfo) {
53 this->EmitJsPrepare(cb, networkId, msgInfo);
54 };
55 int32_t errCode = INTERACTION_MGR->UnprepareCoordination(callback, isCompatible);
56 if (errCode != RET_OK) {
57 UtilNapiError::HandleExecuteResult(env, errCode, "unprepare", COOPERATE_PERMISSION);
58 RELEASE_CALLBACKINFO(cb->env, cb->ref);
59 }
60 return result;
61 }
62
Activate(napi_env env,const std::string & remoteNetworkId,int32_t startDeviceId,bool isCompatible,napi_value handle)63 napi_value JsCoordinationManager::Activate(napi_env env, const std::string &remoteNetworkId,
64 int32_t startDeviceId, bool isCompatible, napi_value handle)
65 {
66 CALL_INFO_TRACE;
67 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
68 CHKPP(cb);
69 napi_value result = CreateCallbackInfo(env, handle, cb);
70 auto callback = [this, cb](const std::string &remoteNetworkId, const CoordinationMsgInfo &msgInfo) {
71 this->EmitJsActivate(cb, remoteNetworkId, msgInfo);
72 };
73 int32_t errCode = INTERACTION_MGR->ActivateCoordination(
74 remoteNetworkId, startDeviceId, callback, isCompatible);
75 if (errCode != RET_OK) {
76 UtilNapiError::HandleExecuteResult(env, errCode, "activate", COOPERATE_PERMISSION);
77 RELEASE_CALLBACKINFO(cb->env, cb->ref);
78 }
79 return result;
80 }
81
ActivateCooperateWithOptions(napi_env env,const std::string & remoteNetworkId,int32_t startDeviceId,const CooperateOptions & cooperateOptions,napi_value handle)82 napi_value JsCoordinationManager::ActivateCooperateWithOptions(napi_env env, const std::string &remoteNetworkId,
83 int32_t startDeviceId, const CooperateOptions &cooperateOptions, napi_value handle)
84 {
85 CALL_INFO_TRACE;
86 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
87 CHKPP(cb);
88 napi_value result = CreateCallbackInfo(env, handle, cb);
89 auto callback = [this, cb](const std::string &remoteNetworkId, const CoordinationMsgInfo &msgInfo) {
90 this->EmitJsActivate(cb, remoteNetworkId, msgInfo);
91 };
92 int32_t errCode = INTERACTION_MGR->ActivateCooperateWithOptions(remoteNetworkId, startDeviceId,
93 callback, cooperateOptions);
94 if (errCode != RET_OK) {
95 UtilNapiError::HandleExecuteResult(env, errCode, "activateCooperateWithOptions", COOPERATE_PERMISSION);
96 RELEASE_CALLBACKINFO(cb->env, cb->ref);
97 }
98 return result;
99 }
100
Deactivate(napi_env env,bool isUnchained,bool isCompatible,napi_value handle)101 napi_value JsCoordinationManager::Deactivate(napi_env env,
102 bool isUnchained, bool isCompatible, napi_value handle)
103 {
104 CALL_INFO_TRACE;
105 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
106 CHKPP(cb);
107 napi_value result = CreateCallbackInfo(env, handle, cb);
108 auto callback = [this, cb](const std::string &networkId, const CoordinationMsgInfo &msgInfo) {
109 this->EmitJsDeactivate(cb, networkId, msgInfo);
110 };
111 int32_t errCode = INTERACTION_MGR->DeactivateCoordination(isUnchained, callback, isCompatible);
112 if (errCode != RET_OK) {
113 UtilNapiError::HandleExecuteResult(env, errCode, "deactivate", COOPERATE_PERMISSION);
114 RELEASE_CALLBACKINFO(cb->env, cb->ref);
115 }
116 return result;
117 }
118
GetCrossingSwitchState(napi_env env,const std::string & networkId,bool isCompatible,napi_value handle)119 napi_value JsCoordinationManager::GetCrossingSwitchState(napi_env env,
120 const std::string &networkId, bool isCompatible, napi_value handle)
121 {
122 CALL_INFO_TRACE;
123 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
124 CHKPP(cb);
125 napi_value result = CreateCallbackInfo(env, handle, cb);
126 auto callback = [this, cb](bool state) {
127 this->EmitJsGetCrossingSwitchState(cb, state);
128 };
129 int32_t errCode = INTERACTION_MGR->GetCoordinationState(networkId, callback, isCompatible);
130 if (errCode != RET_OK) {
131 UtilNapiError::HandleExecuteResult(env, errCode, "getCrossingSwitchState", COOPERATE_PERMISSION);
132 RELEASE_CALLBACKINFO(cb->env, cb->ref);
133 }
134 return result;
135 }
136
RegisterListener(napi_env env,const std::string & type,napi_value handle)137 void JsCoordinationManager::RegisterListener(napi_env env, const std::string &type, napi_value handle)
138 {
139 CALL_INFO_TRACE;
140 AddListener(env, type, handle);
141 }
142
UnregisterListener(napi_env env,const std::string & type,napi_value handle)143 void JsCoordinationManager::UnregisterListener(napi_env env, const std::string &type, napi_value handle)
144 {
145 CALL_INFO_TRACE;
146 RemoveListener(env, type, handle);
147 }
148
RegisterListener(napi_env env,const std::string & type,const std::string & networkId,napi_value handle)149 void JsCoordinationManager::RegisterListener(napi_env env, const std::string &type, const std::string &networkId,
150 napi_value handle)
151 {
152 CALL_INFO_TRACE;
153 AddListener(env, type, networkId, handle);
154 }
155
UnregisterListener(napi_env env,const std::string & type,const std::string & networkId,napi_value handle)156 void JsCoordinationManager::UnregisterListener(napi_env env, const std::string &type, const std::string &networkId,
157 napi_value handle)
158 {
159 CALL_INFO_TRACE;
160 RemoveListener(env, type, networkId, handle);
161 }
162
ResetEnv()163 void JsCoordinationManager::ResetEnv()
164 {
165 CALL_INFO_TRACE;
166 JsEventTarget::ResetEnv();
167 }
168 } // namespace DeviceStatus
169 } // namespace Msdp
170 } // namespace OHOS
171