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 "mc_command_factory.h"
17 #include "mechbody_controller_enums.h"
18 #include "mechbody_controller_log.h"
19
20 namespace OHOS {
21 namespace MechBodyController {
22 namespace {
23 const std::string TAG = "CommandFactory";
24 constexpr uint8_t CREATE_FROM_DATA_MIN_SIZE = 2;
25 }
26
CreateGetMechCameraTrackingLayoutCmd()27 std::shared_ptr<GetMechCameraTrackingLayoutCmd> CommandFactory::CreateGetMechCameraTrackingLayoutCmd()
28 {
29 return std::make_shared<GetMechCameraTrackingLayoutCmd>();
30 }
31
CreateGetMechCapabilityInfoCmd()32 std::shared_ptr<GetMechCapabilityInfoCmd> CommandFactory::CreateGetMechCapabilityInfoCmd()
33 {
34 return std::make_shared<GetMechCapabilityInfoCmd>();
35 }
36
CreateSetMechCameraInfoCmd(const CameraInfoParams & params)37 std::shared_ptr<SetMechCameraInfoCmd> CommandFactory::CreateSetMechCameraInfoCmd(
38 const CameraInfoParams& params)
39 {
40 return std::make_shared<SetMechCameraInfoCmd>(params);
41 }
42
CreateSetMechCameraTrackingEnableCmd(MechTrackingStatus status)43 std::shared_ptr<SetMechCameraTrackingEnableCmd> CommandFactory::CreateSetMechCameraTrackingEnableCmd(
44 MechTrackingStatus status)
45 {
46 return std::make_shared<SetMechCameraTrackingEnableCmd>(status);
47 }
48
CreateSetMechCameraTrackingFrameCmd(const TrackingFrameParams & params)49 std::shared_ptr<SetMechCameraTrackingFrameCmd> CommandFactory::CreateSetMechCameraTrackingFrameCmd(
50 const TrackingFrameParams& params)
51 {
52 return std::make_shared<SetMechCameraTrackingFrameCmd>(params);
53 }
54
CreateSetMechCameraTrackingLayoutCmd(const LayoutParams & params)55 std::shared_ptr<SetMechCameraTrackingLayoutCmd> CommandFactory::CreateSetMechCameraTrackingLayoutCmd(
56 const LayoutParams& params)
57 {
58 return std::make_shared<SetMechCameraTrackingLayoutCmd>(params);
59 }
60
CreateSetMechConfigCmd(uint8_t configVersion)61 std::shared_ptr<SetMechConfigCmd> CommandFactory::CreateSetMechConfigCmd(
62 uint8_t configVersion)
63 {
64 return std::make_shared<SetMechConfigCmd>(configVersion);
65 }
66
CreateSetMechHidPreemptiveCmd(bool isPreemptive)67 std::shared_ptr<SetMechHidPreemptiveCmd> CommandFactory::CreateSetMechHidPreemptiveCmd(
68 bool isPreemptive)
69 {
70 return std::make_shared<SetMechHidPreemptiveCmd>(isPreemptive);
71 }
72
CreateSetMechRotationBySpeedCmd(const RotateBySpeedParam & params)73 std::shared_ptr<SetMechRotationBySpeedCmd> CommandFactory::CreateSetMechRotationBySpeedCmd(
74 const RotateBySpeedParam& params)
75 {
76 return std::make_shared<SetMechRotationBySpeedCmd>(params);
77 }
78
CreateSetMechRotationCmd(const RotateParam & params)79 std::shared_ptr<SetMechRotationCmd> CommandFactory::CreateSetMechRotationCmd(
80 const RotateParam& params)
81 {
82 return std::make_shared<SetMechRotationCmd>(params);
83 }
84
CreateSetMechRotationTraceCmd(const std::vector<RotateParam> & params)85 std::shared_ptr<SetMechRotationTraceCmd> CommandFactory::CreateSetMechRotationTraceCmd(
86 const std::vector<RotateParam>& params)
87 {
88 return std::make_shared<SetMechRotationTraceCmd>(params);
89 }
90
CreateSetMechStopCmd()91 std::shared_ptr<SetMechStopCmd> CommandFactory::CreateSetMechStopCmd()
92 {
93 return std::make_shared<SetMechStopCmd>();
94 }
95
96
CreateRegisterMechCameraKeyEventCmd()97 std::shared_ptr<RegisterMechCameraKeyEventCmd> CommandFactory::CreateRegisterMechCameraKeyEventCmd()
98 {
99 return std::make_shared<RegisterMechCameraKeyEventCmd>();
100 }
101
CreateRegisterMechControlResultCmd()102 std::shared_ptr<RegisterMechControlResultCmd> CommandFactory::CreateRegisterMechControlResultCmd()
103 {
104 return std::make_shared<RegisterMechControlResultCmd>();
105 }
106
CreateRegisterMechPositionInfoCmd()107 std::shared_ptr<RegisterMechPositionInfoCmd> CommandFactory::CreateRegisterMechPositionInfoCmd()
108 {
109 return std::make_shared<RegisterMechPositionInfoCmd>();
110 }
111
CreateRegisterMechStateInfoCmd()112 std::shared_ptr<RegisterMechStateInfoCmd> CommandFactory::CreateRegisterMechStateInfoCmd()
113 {
114 return std::make_shared<RegisterMechStateInfoCmd>();
115 }
116
CreateRegisterMechWheelDataCmd()117 std::shared_ptr<RegisterMechWheelDataCmd> CommandFactory::CreateRegisterMechWheelDataCmd()
118 {
119 return std::make_shared<RegisterMechWheelDataCmd>();
120 }
121
CreateRegisterMechTrackingEnableCmd()122 std::shared_ptr<RegisterMechTrackingEnableCmd> CommandFactory::CreateRegisterMechTrackingEnableCmd()
123 {
124 return std::make_shared<RegisterMechTrackingEnableCmd>();
125 }
126
CreateFromData(std::shared_ptr<MechDataBuffer> data)127 std::shared_ptr<CommandBase> CommandFactory::CreateFromData(std::shared_ptr<MechDataBuffer> data)
128 {
129 if (data->Size() < CREATE_FROM_DATA_MIN_SIZE) {
130 return nullptr;
131 }
132
133 uint8_t cmdType = 0;
134 uint8_t cmdId = 0;
135 CHECK_ERR_RETURN_VALUE(data->ReadUint8(0, cmdType), nullptr, "read cmdType");
136 CHECK_ERR_RETURN_VALUE(data->ReadUint8(BIT_OFFSET_1, cmdId), nullptr, "read cmdId");
137
138 uint16_t type = (static_cast<uint16_t>(cmdType) << BIT_OFFSET_8) | cmdId;
139 HILOGD("CmdType 0x%{public}x", type);
140
141 switch (type) {
142 case CMD_TYPE_BUTTON_EVENT_NOTIFY:
143 return CreateAndUnmarshal<RegisterMechCameraKeyEventCmd>(data);
144 case CMD_TYPE_PARAM_NOTIFY:
145 return CreateAndUnmarshal<RegisterMechStateInfoCmd>(data);
146 case CMD_TYPE_ATTITUDE_NOTIFY:
147 return CreateAndUnmarshal<RegisterMechPositionInfoCmd>(data);
148 case CMD_TYPE_EXE_RESULT_NOTIFY:
149 return CreateAndUnmarshal<RegisterMechControlResultCmd>(data);
150 case CMD_TYPE_WHEEL_DATA_NOTIFY:
151 return CreateAndUnmarshal<RegisterMechWheelDataCmd>(data);
152 case CMD_TYPE_TRACKING_ENABLED_NOTIFY:
153 return CreateAndUnmarshal<RegisterMechTrackingEnableCmd>(data);
154 default:
155 return nullptr;
156 }
157 return nullptr;
158 }
159 } // namespace MechBodyController
160 } // namespace OHOS
161