• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "mechbody_controller_log.h"
17 #include "js_mech_manager_service.h"
18 
19 namespace OHOS {
20 namespace MechBodyController {
21 namespace {
22     const std::string TAG = "JsMechManagerService";
23 }
24 
GetInstance()25 JsMechManagerService& JsMechManagerService::GetInstance()
26 {
27     static auto instance = new JsMechManagerService();
28     return *instance;
29 }
30 
JsMechManagerService()31 JsMechManagerService::JsMechManagerService() {
32 }
33 
~JsMechManagerService()34 JsMechManagerService::~JsMechManagerService() {
35     HILOGI("~JsMechManagerService.");
36 }
37 
AttachStateChangeCallback(const AttachmentState & attachmentState,const std::shared_ptr<MechInfo> & mechInfo)38 int32_t JsMechManagerService::AttachStateChangeCallback(const AttachmentState &attachmentState,
39     const std::shared_ptr<MechInfo> &mechInfo)
40 {
41     CHECK_POINTER_RETURN_VALUE(mechInfo, INVALID_PARAMETERS_ERR, "mechInfo");
42     for (const auto &item: attachStateChangeCallback_) {
43         auto task = [item, attachmentState, mechInfo]() {
44             napi_handle_scope scope;
45             napi_open_handle_scope(item.env, &scope);
46 
47             napi_value jsEvent;
48             napi_create_object(item.env, &jsEvent);
49 
50             napi_value jsState;
51             napi_create_int32(item.env, static_cast<int32_t>(attachmentState), &jsState);
52             napi_set_named_property(item.env, jsEvent, "state", jsState);
53 
54             napi_value jsMechInfo;
55             napi_create_object(item.env, &jsMechInfo);
56             napi_value jsMechId;
57             napi_value jsMechType;
58             napi_value jsMechName;
59             napi_create_int32(item.env, mechInfo->mechId, &jsMechId);
60             napi_create_int32(item.env, static_cast<int32_t>(mechInfo->mechType), &jsMechType);
61             napi_create_string_utf8(item.env, mechInfo->mechName.c_str(), NAPI_AUTO_LENGTH, &jsMechName);
62             napi_set_named_property(item.env, jsMechInfo, "mechId", jsMechId);
63             napi_set_named_property(item.env, jsMechInfo, "mechType", jsMechType);
64             napi_set_named_property(item.env, jsMechInfo, "mechName", jsMechName);
65             napi_set_named_property(item.env, jsEvent, "mechInfo", jsMechInfo);
66 
67             napi_value callback;
68             napi_get_reference_value(item.env, item.callbackRef, &callback);
69 
70             napi_value global;
71             napi_get_global(item.env, &global);
72 
73             napi_value result;
74             napi_call_function(item.env, global, callback, 1, &jsEvent, &result);
75 
76             napi_close_handle_scope(item.env, scope);
77         };
78         napi_send_event(item.env, task, napi_eprio_high);
79     }
80     HILOGI("end");
81     return ERR_OK;
82 }
83 
TrackingEventCallback(const int32_t & mechId,const TrackingEvent & trackingEvent)84 int32_t JsMechManagerService::TrackingEventCallback(const int32_t &mechId,
85     const TrackingEvent &trackingEvent)
86 {
87     for (const auto &item: trackingEventCallback) {
88         auto task = [item, mechId, trackingEvent]() {
89             napi_handle_scope scope;
90             napi_open_handle_scope(item.env, &scope);
91 
92             napi_value jsEvent;
93             napi_create_object(item.env, &jsEvent);
94 
95             napi_value jsMechId;
96             napi_create_int32(item.env, mechId, &jsMechId);
97             napi_set_named_property(item.env, jsEvent, "mechId", jsMechId);
98 
99             napi_value jsTrackingEvent;
100             napi_create_int32(item.env, static_cast<int32_t>(trackingEvent), &jsTrackingEvent);
101             napi_set_named_property(item.env, jsEvent, "event", jsTrackingEvent);
102 
103             napi_value callback;
104             napi_get_reference_value(item.env, item.callbackRef, &callback);
105 
106             napi_value global;
107             napi_get_global(item.env, &global);
108 
109             napi_value result;
110             napi_call_function(item.env, global, callback, 1, &jsEvent, &result);
111 
112             napi_close_handle_scope(item.env, scope);
113         };
114         napi_send_event(item.env, task, napi_eprio_high);
115     }
116 
117     HILOGI("end");
118     return ERR_OK;
119 }
120 
RotationAxesStatusChangeCallback(const int32_t & mechId,const std::shared_ptr<RotationAxesStatus> & rotationAxesStatus)121 int32_t JsMechManagerService::RotationAxesStatusChangeCallback(const int32_t &mechId,
122     const std::shared_ptr<RotationAxesStatus> &rotationAxesStatus)
123 {
124     CHECK_POINTER_RETURN_VALUE(rotationAxesStatus, INVALID_PARAMETERS_ERR, "rotationAxesStatus");
125     for (const auto &item: rotateAxisStatusChangeCallback) {
126         auto task = [item, mechId, rotationAxesStatus]() {
127             napi_handle_scope scope;
128             napi_open_handle_scope(item.env, &scope);
129 
130             napi_value jsEvent;
131             napi_create_object(item.env, &jsEvent);
132             napi_value jsMechId;
133             napi_create_int32(item.env, mechId, &jsMechId);
134             napi_set_named_property(item.env, jsEvent, "mechId", jsMechId);
135 
136             napi_value jsStatus;
137             napi_create_object(item.env, &jsStatus);
138             napi_value jsYawEnabled;
139             napi_value jsRollEnabled;
140             napi_value jsPitchEnabled;
141             napi_value jsYawLimited;
142             napi_value jsRollLimited;
143             napi_value jsPitchLimited;
144             napi_get_boolean(item.env, rotationAxesStatus->yawEnabled, &jsYawEnabled);
145             napi_get_boolean(item.env, rotationAxesStatus->rollEnabled, &jsRollEnabled);
146             napi_get_boolean(item.env, rotationAxesStatus->pitchEnabled, &jsPitchEnabled);
147             napi_create_int32(item.env, static_cast<int32_t>(rotationAxesStatus->yawLimited), &jsYawLimited);
148             napi_create_int32(item.env, static_cast<int32_t>(rotationAxesStatus->rollLimited), &jsRollLimited);
149             napi_create_int32(item.env, static_cast<int32_t>(rotationAxesStatus->pitchLimited),
150                               &jsPitchLimited);
151             napi_set_named_property(item.env, jsStatus, "yawEnabled", jsYawEnabled);
152             napi_set_named_property(item.env, jsStatus, "rollEnabled", jsRollEnabled);
153             napi_set_named_property(item.env, jsStatus, "pitchEnabled", jsPitchEnabled);
154             napi_set_named_property(item.env, jsStatus, "yawLimited", jsYawLimited);
155             napi_set_named_property(item.env, jsStatus, "rollLimited", jsRollLimited);
156             napi_set_named_property(item.env, jsStatus, "pitchLimited", jsPitchLimited);
157             napi_set_named_property(item.env, jsEvent, "status", jsStatus);
158 
159             napi_value callback;
160             napi_get_reference_value(item.env, item.callbackRef, &callback);
161 
162             napi_value global;
163             napi_get_global(item.env, &global);
164 
165             napi_value result;
166             napi_call_function(item.env, global, callback, 1, &jsEvent, &result);
167 
168             napi_close_handle_scope(item.env, scope);
169         };
170         napi_send_event(item.env, task, napi_eprio_high);
171     }
172 
173     HILOGI("end");
174     return ERR_OK;
175 }
176 
RotatePromiseFulfillment(const std::string & cmdId,const int32_t & result)177 int32_t JsMechManagerService::RotatePromiseFulfillment(const std::string &cmdId,
178     const int32_t &result)
179 {
180     HILOGI("RotatePrimiseFulfillmentParam cmdId: %{public}s", cmdId.c_str());
181     std::shared_ptr<RotatePrimiseFulfillmentParam> param = JsMechManagerService::promiseParams_[cmdId];
182     if(param == nullptr){
183         HILOGE("RotatePrimiseFulfillmentParam is nullptr, cmdId: %{public}s", cmdId.c_str());
184         return ERR_OK;
185     }
186     auto task = [cmdId, param, result]() {
187         napi_handle_scope scope;
188         napi_open_handle_scope(param->env, &scope);
189 
190         // 创建 JavaScript 返回值
191         napi_value jsResult;
192         napi_create_int32(param->env, result, &jsResult);
193 
194         // 解析 Promise
195         napi_status status = napi_resolve_deferred(param->env, param->deferred, jsResult);
196         if (status != napi_ok) {
197             HILOGI("Promise Fulfillment failed for cmdId: %{public}s", cmdId.c_str());
198         }
199         JsMechManagerService::GetInstance().promiseParams_.erase(cmdId);
200         napi_close_handle_scope(param->env, scope);
201     };
202     napi_status status = napi_send_event(param->env, task, napi_eprio_high);
203     HILOGI("RotatePromise send result: %{public}s", status == napi_ok ? "success" : "failed");
204     return ERR_OK;
205 }
206 
207 } // namespace MechManager
208 } // namespace OHOS