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_set_mech_camera_tracking_enable_cmd.h"
17
18 #include "mechbody_controller_log.h"
19
20 namespace OHOS {
21 namespace MechBodyController {
22 namespace {
23 const std::string TAG = "SetMechCameraTrackingEnableCmd";
24 }
25
SetMechCameraTrackingEnableCmd(MechTrackingStatus status)26 SetMechCameraTrackingEnableCmd::SetMechCameraTrackingEnableCmd(MechTrackingStatus status)
27 : status_(status)
28 {
29 cmdSet_ = CMD_SET;
30 cmdId_ = CMD_ID;
31 reqSize_ = REQ_SIZE;
32 rspSize_ = RSP_SIZE;
33 needResponse_ = (RSP_SIZE > 0);
34 timeoutMs_ = MECHBODY_MSG_TIMEOUT;
35 retryTimes_ = CMD_PRIORITY_HIGH;
36 }
37
Marshal() const38 std::shared_ptr<MechDataBuffer> SetMechCameraTrackingEnableCmd::Marshal() const
39 {
40 HILOGI("start.");
41 auto buffer = std::make_shared<MechDataBuffer>(reqSize_ + BIT_OFFSET_2);
42 if (buffer == nullptr) {
43 HILOGE("Failed to allocate memory for Marshal buffer");
44 return nullptr;
45 }
46
47 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdSet_), nullptr, "append cmdSet_");
48 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(cmdId_), nullptr, "append cmdId_");
49
50 CHECK_ERR_RETURN_VALUE(buffer->AppendUint8(static_cast<uint8_t>(status_)), nullptr, "append status_");
51
52 HILOGI("end.");
53 return buffer;
54 }
55
TriggerResponse(std::shared_ptr<MechDataBuffer> data)56 void SetMechCameraTrackingEnableCmd::TriggerResponse(std::shared_ptr<MechDataBuffer> data)
57 {
58 if (data == nullptr || data->Size() < RSP_SIZE + BIT_OFFSET_2) {
59 HILOGE("Invalid input data for response");
60 return;
61 }
62
63 if (data->ReadUint8(BIT_OFFSET_2, result_) != ERR_OK) {
64 HILOGE("read result failed");
65 return;
66 }
67 HILOGI("response code: %{public}u", result_);
68
69 if (responseCb_) {
70 HILOGI("trigger response callback.");
71 responseCb_();
72 }
73 }
74
GetStatus() const75 MechTrackingStatus SetMechCameraTrackingEnableCmd::GetStatus() const
76 {
77 return status_;
78 }
79
GetResult() const80 uint8_t SetMechCameraTrackingEnableCmd::GetResult() const
81 {
82 return result_;
83 }
84 } // namespace MechBodyController
85 } // namespace OHOS
86