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