• 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 OHOS_EB_MOTION_MANAGER_H
17 #define OHOS_EB_MOTION_MANAGER_H
18 
19 #include <string>
20 #include <mutex>
21 #include <thread>
22 #include <sys/prctl.h>
23 
24 #include "event_handler.h"
25 #include "mc_send_adapter.h"
26 #include "iremote_broker.h"
27 #include "ipc_skeleton.h"
28 #include "mechbody_controller_types.h"
29 #include "key_event.h"
30 #include "input_manager.h"
31 #include "mc_command_factory.h"
32 
33 namespace OHOS {
34 namespace MechBodyController {
35 
36 struct DeviceStatus {
37     bool isEnabled = true;
38     MechStateInfo stateInfo;
39     LayoutParams layoutParams;
40     int32_t duration = 500;
41     bool yawLimit = false;
42     bool pitchLimit = false;
43     bool rollLimit = false;
44     EulerAngles eulerAngles;
45     RotateDegreeLimit rotationLimit;
46     RotateDegreeLimit rotationLimitWithOffset;
47     RotateSpeedLimit rotateSpeedLimit;
48     MechBaseInfo mechBaseInfo;
49     MechCapabilityInfo mechCapabilityInfo;
50     RotationAxesStatus rotationAxesStatus;
51 };
52 
53 struct MechNapiCommandCallbackInfo {
54     MechNapiCommandCallbackInfo(uint32_t &tokenId, std::string &napiCmdId);
55 
56     uint32_t tokenId;
57     std::string napiCmdId;
58     bool willLimitChange = false;
59 
ToStringMechNapiCommandCallbackInfo60     std::string ToString() const
61     {
62         return "napiCmdId: " + napiCmdId +
63                ", willLimitChange: " + std::to_string(willLimitChange);
64     }
65 };
66 
67 class MotionManager : public IMechEventListener, public std::enable_shared_from_this<MotionManager> {
68 public:
69     MotionManager(const std::shared_ptr<TransportSendAdapter> sendAdapter, int32_t mechId);
70 
71 public:
72     ~MotionManager();
73     MotionManager(const MotionManager&) = delete;
74     MotionManager& operator=(const MotionManager&) = delete;
75 
76     void RegisterEventListener();
77     int32_t Rotate(std::shared_ptr<RotateParam> rotateParam, uint32_t &tokenId, std::string &napiCmdId);
78     int32_t RotateBySpeed(std::shared_ptr<RotateBySpeedParam> rotateSpeedParam,
79                           uint32_t &tokenId, std::string &napiCmdId);
80     int32_t StopRotate(uint32_t &tokenId, std::string &napiCmdId);
81 
82     int32_t GetSpeedControlTimeLimit(std::shared_ptr<TimeLimit> &timeLimit);
83     int32_t GetRotateSpeedLimit(RotateSpeedLimit &speedLimit);
84     int32_t GetCurrentPosition(std::shared_ptr<EulerAngles> &eulerAngles);
85     int32_t GetRotationLimit(RotateDegreeLimit &rotationLimit);
86     int32_t SetMechCameraTrackingFrame(const std::shared_ptr<TrackingFrameParams> trackingFrameParams);
87     int32_t GetMechCameraTrackingEnabled(bool &isEnabled);
88     int32_t SetMechCameraTrackingEnabled(bool &isEnabled);
89     int32_t SetMechCameraTrackingLayout(const std::shared_ptr<LayoutParams> layoutParams);
90     int32_t GetMechCameraTrackingLayout(std::shared_ptr<LayoutParams> &layoutParams);
91     int32_t SetMechCameraInfo(const CameraInfoParams &mechCameraInfo);
92     int32_t GetMechBaseInfo(std::shared_ptr<MechBaseInfo> &mechBaseInfo);
93     int32_t GetMechCapabilityInfo(std::shared_ptr<MechCapabilityInfo> &mechCapabilityInfo);
94     int32_t GetRotationAxesStatus(const int32_t &mechId, RotationAxesStatus &axesStatus);
95     int32_t PerformPresetAction(PresetAction action, int32_t delay);
96     void MechAttitudeNotify(const std::shared_ptr<RegisterMechPositionInfoCmd> &cmd);
97     void MechParamNotify(const std::shared_ptr<RegisterMechStateInfoCmd> &cmd);
98     void MechExecutionResultNotify(const std::shared_ptr<RegisterMechControlResultCmd> &cmd);
99     void MechWheelZoomNotify(const std::shared_ptr<RegisterMechWheelDataCmd> &cmd);
100     void MechButtonEventNotify(const std::shared_ptr<RegisterMechCameraKeyEventCmd> &cmd);
101     void MechTrackingStatusNotify(const std::shared_ptr<RegisterMechTrackingEnableCmd> &cmd);
102     void UnRegisterNotifyEvent();
103 
104 private:
105     void MMIKeyEvent(CameraKeyEvent eventType);
106     void JudgingYawLimit(RotateDegreeLimit& limit);
107     void AbsolutelyEulerAnglesJudgingLimitLocked(EulerAngles& eulerAngles);
108     uint8_t CreateResponseTaskId();
109     void FormatLimit(RotateDegreeLimit &params);
110     bool IsLimited();
111     bool IsLimited(const float &negMax, const float &posMax, const float &position);
112     void LimitCalculationLocked(EulerAngles& position, RotationAxesStatus& status, bool callback);
113     static int64_t GetSysClockTime();
114     std::shared_ptr<MMI::KeyEvent> CreateKeyEvent(int32_t keyCode, int32_t keyAction);
115     void StartEvent();
116     void HandleMechPlacementChange(bool isPhoneOn);
117     void HandelRotateParam(std::shared_ptr<RotateParam> &rotateParam, bool &willLimitChange);
118     void CheckYawDegree(std::shared_ptr<RotateParam> &rotateParam, const RotateDegreeLimit &limit, float yawResult);
119     void CheckRollDegree(std::shared_ptr<RotateParam> &rotateParam, const RotateDegreeLimit &limit, float rollResult);
120     void CheckPitchDegree(std::shared_ptr<RotateParam> &rotateParam, const RotateDegreeLimit &limit, float pitchResult);
121     void HandelRotateSpeedParam(std::shared_ptr<RotateBySpeedParam> &rotateBySpeedParam, bool &willLimitChange);
122     void CheckPitchSpeed(const std::shared_ptr<RotateBySpeedParam> &rotateBySpeedParam, const RotateDegreeLimit &limit,
123         float pitchResult);
124     void CheckRollSpeed(const std::shared_ptr<RotateBySpeedParam> &rotateBySpeedParam, const RotateDegreeLimit &limit,
125         float rollResult);
126     void CheckYawSpeed(const std::shared_ptr<RotateBySpeedParam> &rotateBySpeedParam, const RotateDegreeLimit &limit,
127         float yawResult);
128 private:
129     std::shared_ptr<DeviceStatus> deviceStatus_;
130     std::mutex deviceStatusMutex_;
131     RotationAxesStatus status_{};
132     std::shared_ptr<TransportSendAdapter> sendAdapter_;
133 
134     std::vector<uint16_t> notifyListenerType_;
135     std::mutex notifyListenerMutex_;
136     int32_t mechId_;
137 
138     std::map<uint8_t, MechNapiCommandCallbackInfo> seqCallbacks_;
139     std::mutex seqCallbackMutex_;
140 
141     std::shared_ptr<IMechEventListener> mechEventListener_;
142     std::mutex eventListenerMutex_;
143 
144     std::thread eventThread_;
145     std::condition_variable eventCon_;
146     std::mutex eventMutex_;
147     std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_ = nullptr;
148     int32_t wheelFilterCnt_ = 0;
149     int32_t wheelCorretCnt_ = 0;
150     std::shared_mutex taskIdMutex_;
151     uint8_t lastTaskId_ = 0;
152     CommandFactory factory;
153 };
154 
155 class MechEventListenerImpl : public IMechEventListener {
156 public:
157     MechEventListenerImpl(std::shared_ptr<MotionManager> motionManager);
158     ~MechEventListenerImpl();
159     void MechAttitudeNotify(const std::shared_ptr<RegisterMechPositionInfoCmd> &cmd) override;
160     void MechButtonEventNotify(const std::shared_ptr<RegisterMechCameraKeyEventCmd> &cmd) override;
161     void MechParamNotify(const std::shared_ptr<RegisterMechStateInfoCmd> &cmd) override;
162     void MechExecutionResultNotify(const std::shared_ptr<RegisterMechControlResultCmd> &cmd) override;
163     void MechWheelZoomNotify(const std::shared_ptr<RegisterMechWheelDataCmd> &cmd) override;
164     void MechTrackingStatusNotify(const std::shared_ptr<RegisterMechTrackingEnableCmd> &cmd) override;
165 
166 private:
167     std::shared_ptr<MotionManager> motionManager_;
168 };
169 } // namespace MechBodyController
170 } // namespace OHOS
171 #endif // OHOS_EB_MOTION_MANAGER_H
172