• 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 #ifndef MECH_MANAGER_NAPI_H
17 #define MECH_MANAGER_NAPI_H
18 
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27 #include "ipc_skeleton.h"
28 #include "js_mech_manager_client.h"
29 #include "js_mech_manager_stub.h"
30 #include "js_mech_manager_service.h"
31 
32 namespace OHOS {
33 namespace MechBodyController {
34 
35 class MechManager {
36 public:
37     static napi_value On(napi_env env, napi_callback_info info);
38 
39     static napi_value Off(napi_env env, napi_callback_info info);
40 
41     static napi_value GetAttachedDevices(napi_env env, napi_callback_info info);
42 
43     static napi_value SetUserOperation(napi_env env, napi_callback_info info);
44 
45     static napi_value SetCameraTrackingEnabled(napi_env env, napi_callback_info info);
46 
47     static napi_value GetCameraTrackingEnabled(napi_env env, napi_callback_info info);
48 
49     static napi_value SetCameraTrackingLayout(napi_env env, napi_callback_info info);
50 
51     static napi_value GetCameraTrackingLayout(napi_env env, napi_callback_info info);
52 
53     static napi_value Rotate(napi_env env, napi_callback_info info);
54 
55     static napi_value RotateToEulerAngles(napi_env env, napi_callback_info info);
56 
57     static napi_value GetMaxRotationTime(napi_env env, napi_callback_info info);
58 
59     static napi_value GetMaxRotationSpeed(napi_env env, napi_callback_info info);
60 
61     static napi_value RotateBySpeed(napi_env env, napi_callback_info info);
62 
63     static napi_value StopMoving(napi_env env, napi_callback_info info);
64 
65     static napi_value GetCurrentAngles(napi_env env, napi_callback_info info);
66 
67     static napi_value GetRotationLimits(napi_env env, napi_callback_info info);
68 
69     static napi_value GetRotationAxesStatus(napi_env env, napi_callback_info info);
70 
71 private:
72     static int32_t ExecuteOn(std::string &eventType, CallbackFunctionInfo &callbackFunctionInfo);
73     static int32_t ExecuteOnForAttachStateChange(const CallbackFunctionInfo &callbackFunctionInfo);
74     static int32_t ExecuteOnForTrackingEvent(const CallbackFunctionInfo &callbackFunctionInfo);
75     static int32_t ExecuteOnForRotationAxesStatusChange(const CallbackFunctionInfo &callbackFunctionInfo);
76     static bool InitAttachStateChangeStub();
77     static void ProcessOnResultCode(napi_env env, int32_t &result);
78 
79     static int32_t ExecuteOff(std::string &eventType);
80     static int32_t ExecuteOff(std::string &eventType, CallbackFunctionInfo &callbackFunctionInfo);
81     static int32_t ExecuteOffForAttachStateChange(CallbackFunctionInfo &callbackFunctionInfo);
82     static int32_t ExecuteOffForTrackingEvent(CallbackFunctionInfo &callbackFunctionInfo);
83     static int32_t ExecuteOffForRotationAxesStatusChange(CallbackFunctionInfo &callbackFunctionInfo);
84     static bool HasSameCallbackFunction(std::set<CallbackFunctionInfo> &cacheInfo,
85         CallbackFunctionInfo &callbackFunctionInfo);
86     static bool RemoveSameCallbackFunction(std::set<CallbackFunctionInfo> &cacheInfo,
87         CallbackFunctionInfo &callbackFunctionInfo);
88     static void ProcessOffResultCode(napi_env env, int32_t &result);
89 
90     static napi_value MechInfoToNapiObject(napi_env env, const std::shared_ptr<MechInfo> &info);
91 
92     static bool RegisterCmdChannel();
93 
94     static bool GetRotateParam(napi_env env, napi_callback_info info,
95         RotateByDegreeParam &rotateParam, int32_t &mechId);
96 
97     static bool GetRotateToEulerAnglesParam(napi_env env, napi_callback_info info,
98         RotateToEulerAnglesParam &rotateToEulerAnglesParam, int32_t &mechId);
99     static napi_value RotateSpeedToNapi(napi_env env, const RotateSpeed &speed);
100 
101     static bool GetRotateBySpeedParam(napi_env env, napi_callback_info info,
102         RotateBySpeedParam &rotateBySpeedParam, int32_t &mechId);
103 
104     static napi_value CreateEulerAngles(napi_env env, const EulerAngles &degree);
105 
106     static bool InitTrackingEventStub();
107 
108     static bool InitRotationAxesStatusChangeStub();
109 
110     static napi_value CreateRotationLimit(napi_env env, const RotateDegreeLimit &limit);
111 
112     static napi_value CreateRotationAxesStatus(napi_env env, const RotationAxesStatus &status);
113 
114     static bool IsSystemApp();
115 
116     static bool InitMechClient();
117 
118     static std::string GenerateUniqueID();
119 
120 private:
121     static std::mutex attachStateChangeStubMutex_;
122     static sptr<JsMechManagerStub> attachStateChangeStub_;
123     static std::mutex trackingEventStubMutex_;
124     static sptr<JsMechManagerStub> trackingEventStub_;
125     static std::mutex rotationAxesStatusChangeMutex_;
126     static sptr<JsMechManagerStub> rotationAxesStatusChangeStub_;
127     static std::mutex cmdChannelMutex_;
128     static sptr<JsMechManagerStub> cmdChannel_;
129     static std::mutex mechClientMutex_;
130     static std::shared_ptr<MechClient> mechClient_;
131 
132     class AttachStateChangeStubDeathListener : public IRemoteObject::DeathRecipient {
133     public:
134         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
135 
136     };
137 
138     class TrackingEventStubDeathListener : public IRemoteObject::DeathRecipient {
139     public:
140         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
141 
142     };
143 
144     class RotationAxesStatusChangeStubDeathListener : public IRemoteObject::DeathRecipient {
145     public:
146         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
147 
148     };
149 
150     class CmdChannelDeathListener : public IRemoteObject::DeathRecipient {
151     public:
152         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
153 
154     };
155 
156 };
157 
158 napi_value Init(napi_env env, napi_value exports);
159 
160 } // namespace MechManager
161 } // namespace OHOS
162 
163 #endif // MECH_MANAGER_NAPI_H