• 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 MC_COMMAND_BASE_H
17 #define MC_COMMAND_BASE_H
18 
19 #include "mc_data_buffer.h"
20 #include <vector>
21 #include <string>
22 #include <cstdint>
23 
24 namespace OHOS {
25 namespace MechBodyController {
26 namespace {
27     constexpr int32_t MECHBODY_MSG_TIMEOUT = 10000;
28 }
29 using ResponseCallback = std::function<void()>;
30 using TimeoutCallback = std::function<void()>;
31 
32 class CommandBase {
33 public:
34     virtual ~CommandBase() = default;
35 
36     virtual std::shared_ptr<MechDataBuffer> Marshal() const = 0;
37     bool Unmarshal(std::shared_ptr<MechDataBuffer> data);
38 
39     void SetResponseCallback(ResponseCallback cb);
40     void SetTimeoutCallback(TimeoutCallback cb);
41     virtual void TriggerResponse(std::shared_ptr<MechDataBuffer> data) = 0;
42     void TriggerTimeout() const;
43 
44     uint8_t GetCmdSet() const;
45     uint8_t GetCmdId() const;
46     uint16_t GetCmdType() const;
47     uint16_t GetReqSize() const;
48     uint16_t GetRspSize() const;
49     uint32_t GetTimeoutMs() const;
50     int32_t GetRetryTimes() const;
51 
52     bool NeedResponse() const;
53 
54 protected:
55     uint8_t cmdSet_ = 0;
56     uint8_t cmdId_ = 0;
57     uint16_t reqSize_ = 0;
58     uint16_t rspSize_ = 0;
59     bool needResponse_ = 0;
60     int32_t retryTimes_ = 0;
61     uint32_t timeoutMs_ = MECHBODY_MSG_TIMEOUT;
62     ResponseCallback responseCb_;
63     TimeoutCallback timeoutCb_;
64 };
65 } // namespace MechBodyController
66 } // namespace OHOS
67 
68 #endif // MC_COMMAND_BASE_H
69