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)82 napi_value JsCoordinationManager::ActivateCooperateWithOptions(napi_env env, const std::string &remoteNetworkId,
83 int32_t startDeviceId, const CooperateOptions &cooperateOptions)
84 {
85 CALL_INFO_TRACE;
86 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
87 CHKPP(cb);
88 napi_value handle = nullptr;
89 napi_value result = CreateCallbackInfo(env, handle, cb);
90 auto callback = [this, cb](const std::string &remoteNetworkId, const CoordinationMsgInfo &msgInfo) {
91 this->EmitJsActivate(cb, remoteNetworkId, msgInfo);
92 };
93 int32_t errCode = INTERACTION_MGR->ActivateCooperateWithOptions(remoteNetworkId, startDeviceId,
94 callback, cooperateOptions);
95 if (errCode != RET_OK) {
96 UtilNapiError::HandleExecuteResult(env, errCode, "activateCooperateWithOptions", COOPERATE_PERMISSION);
97 RELEASE_CALLBACKINFO(cb->env, cb->ref);
98 }
99 return result;
100 }
101
Deactivate(napi_env env,bool isUnchained,bool isCompatible,napi_value handle)102 napi_value JsCoordinationManager::Deactivate(napi_env env,
103 bool isUnchained, bool isCompatible, napi_value handle)
104 {
105 CALL_INFO_TRACE;
106 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
107 CHKPP(cb);
108 napi_value result = CreateCallbackInfo(env, handle, cb);
109 auto callback = [this, cb](const std::string &networkId, const CoordinationMsgInfo &msgInfo) {
110 this->EmitJsDeactivate(cb, networkId, msgInfo);
111 };
112 int32_t errCode = INTERACTION_MGR->DeactivateCoordination(isUnchained, callback, isCompatible);
113 if (errCode != RET_OK) {
114 UtilNapiError::HandleExecuteResult(env, errCode, "deactivate", COOPERATE_PERMISSION);
115 RELEASE_CALLBACKINFO(cb->env, cb->ref);
116 }
117 return result;
118 }
119
GetCrossingSwitchState(napi_env env,const std::string & networkId,bool isCompatible,napi_value handle)120 napi_value JsCoordinationManager::GetCrossingSwitchState(napi_env env,
121 const std::string &networkId, bool isCompatible, napi_value handle)
122 {
123 CALL_INFO_TRACE;
124 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
125 CHKPP(cb);
126 napi_value result = CreateCallbackInfo(env, handle, cb);
127 auto callback = [this, cb](bool state) {
128 this->EmitJsGetCrossingSwitchState(cb, state);
129 };
130 int32_t errCode = INTERACTION_MGR->GetCoordinationState(networkId, callback, isCompatible);
131 if (errCode != RET_OK) {
132 UtilNapiError::HandleExecuteResult(env, errCode, "getCrossingSwitchState", COOPERATE_PERMISSION);
133 RELEASE_CALLBACKINFO(cb->env, cb->ref);
134 }
135 return result;
136 }
137
RegisterListener(napi_env env,const std::string & type,napi_value handle)138 void JsCoordinationManager::RegisterListener(napi_env env, const std::string &type, napi_value handle)
139 {
140 CALL_INFO_TRACE;
141 AddListener(env, type, handle);
142 }
143
UnregisterListener(napi_env env,const std::string & type,napi_value handle)144 void JsCoordinationManager::UnregisterListener(napi_env env, const std::string &type, napi_value handle)
145 {
146 CALL_INFO_TRACE;
147 RemoveListener(env, type, handle);
148 }
149
RegisterListener(napi_env env,const std::string & type,const std::string & networkId,napi_value handle)150 void JsCoordinationManager::RegisterListener(napi_env env, const std::string &type, const std::string &networkId,
151 napi_value handle)
152 {
153 CALL_INFO_TRACE;
154 AddListener(env, type, networkId, handle);
155 }
156
UnregisterListener(napi_env env,const std::string & type,const std::string & networkId,napi_value handle)157 void JsCoordinationManager::UnregisterListener(napi_env env, const std::string &type, const std::string &networkId,
158 napi_value handle)
159 {
160 CALL_INFO_TRACE;
161 RemoveListener(env, type, networkId, handle);
162 }
163
ResetEnv()164 void JsCoordinationManager::ResetEnv()
165 {
166 CALL_INFO_TRACE;
167 JsEventTarget::ResetEnv();
168 }
169 } // namespace DeviceStatus
170 } // namespace Msdp
171 } // namespace OHOS
172