• 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 JS_MECH_MANAGER_SERVICE_H
17 #define JS_MECH_MANAGER_SERVICE_H
18 
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 #include <mutex>
25 #include <set>
26 
27 #include "napi/native_api.h"
28 #include "napi/native_node_api.h"
29 
30 #include "mechbody_controller_types.h"
31 
32 #include "js_mech_menager_interface.h"
33 #include "mechbody_controller_ipc_interface_code.h"
34 
35 namespace OHOS {
36 namespace MechBodyController {
37 
38 struct RotatePrimiseFulfillmentParam {
39     std::string cmdId;
40     napi_env env;
41     napi_deferred deferred;
42 };
43 
44 struct CallbackFunctionInfo {
45     napi_env env;
46     napi_ref callbackRef;
47 
48     bool operator<(const CallbackFunctionInfo &other) const {
49         napi_value storedCallback;
50         napi_get_reference_value(env, callbackRef, &storedCallback);
51 
52         napi_value compareCallback;
53         napi_get_reference_value(other.env, other.callbackRef, &compareCallback);
54 
55         bool isSame = false;
56         napi_strict_equals(env, storedCallback, compareCallback, &isSame);
57         return !isSame && (callbackRef < other.callbackRef);
58     }
59 };
60 
61 class JsMechManagerService {
62 public:
63     static JsMechManagerService& GetInstance();
64 private:
65     JsMechManagerService(const JsMechManagerService&) = delete;
66     JsMechManagerService& operator= (const JsMechManagerService&) = delete;
67     JsMechManagerService(JsMechManagerService&&) = delete;
68     JsMechManagerService& operator= (JsMechManagerService&&) = delete;
69 
70 public:
71 
72     JsMechManagerService();
73 
74     ~JsMechManagerService();
75 
76     int32_t AttachStateChangeCallback(const AttachmentState &attachmentState,
77         const std::shared_ptr<MechInfo> &mechInfo);
78 
79     int32_t TrackingEventCallback(const int32_t &mechId, const TrackingEvent &trackingEvent);
80 
81     int32_t RotationAxesStatusChangeCallback(const int32_t &mechId,
82         const std::shared_ptr<RotationAxesStatus> &rotationAxesStatus);
83 
84     int32_t RotatePromiseFulfillment(const std::string &cmdId,
85         const int32_t &result);
86 
87 public:
88     std::mutex attachStateChangeCallbackMutex_;
89     std::set<CallbackFunctionInfo> attachStateChangeCallback_;
90     std::mutex trackingEventCallbackMutex_;
91     std::set<CallbackFunctionInfo> trackingEventCallback;
92     std::mutex rotateAxisStatusChangeCallbackMutex_;
93     std::set<CallbackFunctionInfo> rotateAxisStatusChangeCallback;
94     std::mutex promiseParamsMutex_;
95     std::map<std::string, std::shared_ptr<RotatePrimiseFulfillmentParam>> promiseParams_;
96 };
97 } // namespace MechManager
98 } // namespace OHOS
99 
100 #endif //JS_MECH_MANAGER_SERVICE_H