1 /*
2 * Copyright (c) 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_cooperate_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 "JsCooperateManager"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28
Enable(napi_env env,bool enable,napi_value handle)29 napi_value JsCooperateManager::Enable(napi_env env, bool enable, napi_value handle)
30 {
31 CALL_INFO_TRACE;
32 sptr<JsUtilCooperate::CallbackInfo> cb = new (std::nothrow) JsUtilCooperate::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->EmitJsEnable(cb, networkId, msgInfo);
37 };
38 int32_t errCode = 0;
39 if (enable) {
40 errCode = INTERACTION_MGR->PrepareCoordination(callback);
41 } else {
42 errCode = INTERACTION_MGR->UnprepareCoordination(callback);
43 }
44 if (errCode != RET_OK) {
45 UtilNapiError::HandleExecuteResult(env, errCode, "enable", COOPERATE_PERMISSION);
46 RELEASE_CALLBACKINFO(cb->env, cb->ref);
47 }
48 return result;
49 }
50
Start(napi_env env,const std::string & remoteNetworkDescriptor,int32_t startDeviceId,napi_value handle)51 napi_value JsCooperateManager::Start(napi_env env, const std::string &remoteNetworkDescriptor,
52 int32_t startDeviceId, napi_value handle)
53 {
54 CALL_INFO_TRACE;
55 sptr<JsUtilCooperate::CallbackInfo> cb = new (std::nothrow) JsUtilCooperate::CallbackInfo();
56 CHKPP(cb);
57 napi_value result = CreateCallbackInfo(env, handle, cb);
58 auto callback = [this, cb](const std::string &remoteNetworkId, const CoordinationMsgInfo &msgInfo) {
59 this->EmitJsStart(cb, remoteNetworkId, msgInfo);
60 };
61 int32_t errCode = INTERACTION_MGR->ActivateCoordination(remoteNetworkDescriptor, startDeviceId, callback);
62 if (errCode != RET_OK) {
63 UtilNapiError::HandleExecuteResult(env, errCode, "start", COOPERATE_PERMISSION);
64 RELEASE_CALLBACKINFO(cb->env, cb->ref);
65 }
66 return result;
67 }
68
Stop(napi_env env,napi_value handle)69 napi_value JsCooperateManager::Stop(napi_env env, napi_value handle)
70 {
71 CALL_INFO_TRACE;
72 sptr<JsUtilCooperate::CallbackInfo> cb = new (std::nothrow) JsUtilCooperate::CallbackInfo();
73 CHKPP(cb);
74 napi_value result = CreateCallbackInfo(env, handle, cb);
75 auto callback = [this, cb](const std::string &networkId, const CoordinationMsgInfo &msgInfo) {
76 this->EmitJsStop(cb, networkId, msgInfo);
77 };
78 bool isUnchained = false;
79 int32_t errCode = INTERACTION_MGR->DeactivateCoordination(isUnchained, callback);
80 if (errCode != RET_OK) {
81 UtilNapiError::HandleExecuteResult(env, errCode, "stop", COOPERATE_PERMISSION);
82 RELEASE_CALLBACKINFO(cb->env, cb->ref);
83 }
84 return result;
85 }
86
GetState(napi_env env,const std::string & deviceDescriptor,napi_value handle)87 napi_value JsCooperateManager::GetState(napi_env env, const std::string &deviceDescriptor, napi_value handle)
88 {
89 CALL_INFO_TRACE;
90 sptr<JsUtilCooperate::CallbackInfo> cb = new (std::nothrow) JsUtilCooperate::CallbackInfo();
91 CHKPP(cb);
92 napi_value result = CreateCallbackInfo(env, handle, cb);
93 auto callback = [this, cb](bool state) {
94 this->EmitJsGetState(cb, state);
95 };
96 int32_t errCode = INTERACTION_MGR->GetCoordinationState(deviceDescriptor, callback);
97 if (errCode != RET_OK) {
98 UtilNapiError::HandleExecuteResult(env, errCode, "getState", COOPERATE_PERMISSION);
99 RELEASE_CALLBACKINFO(cb->env, cb->ref);
100 }
101 return result;
102 }
103
RegisterListener(napi_env env,const std::string & type,napi_value handle)104 void JsCooperateManager::RegisterListener(napi_env env, const std::string &type, napi_value handle)
105 {
106 CALL_INFO_TRACE;
107 AddListener(env, type, handle);
108 }
109
UnregisterListener(napi_env env,const std::string & type,napi_value handle)110 void JsCooperateManager::UnregisterListener(napi_env env, const std::string &type, napi_value handle)
111 {
112 CALL_INFO_TRACE;
113 RemoveListener(env, type, handle);
114 }
115
ResetEnv()116 void JsCooperateManager::ResetEnv()
117 {
118 CALL_INFO_TRACE;
119 JsEventCooperateTarget::ResetEnv();
120 }
121 } // namespace DeviceStatus
122 } // namespace Msdp
123 } // namespace OHOS
124