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