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_protocol_convertor.h"
17
18 #include <securec.h>
19
20 #include "mechbody_controller_log.h"
21
22 namespace OHOS {
23 namespace MechBodyController {
24 namespace {
25 const std::string TAG = "ProtocolConverter";
26 constexpr size_t MAX_DATA_SIZE = 251;
27 constexpr size_t MAX_SERVICE_NAME_SIZE = 0xFF;
28 constexpr size_t BT_HEADER_LENTH = 8;
29 constexpr size_t BT_SERVICE_HEADER_LENTH = 4;
30 constexpr size_t BT_COMMAND_HEADER_LENTH = 2;
31 constexpr uint8_t BT_PROTOCOL_VERSION = 0;
32 constexpr uint8_t BT_PROTOCOL_MSG_ID = 0;
33 constexpr uint8_t BT_PROTOCOL_TOTAL_FRAME = 1;
34 constexpr uint8_t BT_PROTOCOL_FRAME_SEQ = 0;
35 constexpr uint8_t BT_PROTOCOL_REV = 0;
36 constexpr uint8_t BT_PROTOCOL_ENCRY = 0x00;
37 constexpr uint16_t BT_PROTOCOL_RETURN = 0x00;
38 constexpr uint8_t BT_PAYLOAD_DATA_FORMAT = 0x02;
39 constexpr uint8_t BT_PROTOCOL_CMD_TYPE_MASK = 0x0F;
40 }
41
Convert(OptType optType,uint16_t seqNo,std::shared_ptr<MechDataBuffer> data) const42 std::shared_ptr<MechDataBuffer> ProtocolConverter::Convert(
43 OptType optType, uint16_t seqNo, std::shared_ptr<MechDataBuffer> data) const
44 {
45 HILOGD("start.");
46 if (data == nullptr) {
47 HILOGE("data is nullptr!");
48 return nullptr;
49 }
50
51 if (serviceName_.size() > MAX_SERVICE_NAME_SIZE) {
52 HILOGE("serviceName size %{public}zu exceeds maximum size %{public}zu!", serviceName_.size(), MAX_DATA_SIZE);
53 return nullptr;
54 }
55
56 size_t protocolSize =
57 BT_HEADER_LENTH + BT_SERVICE_HEADER_LENTH + serviceName_.size() + BT_COMMAND_HEADER_LENTH + data->Size();
58 if (protocolSize > MAX_DATA_SIZE) {
59 HILOGE("protocol size %{public}zu exceeds maximum size %{public}zu!", data->Size(), MAX_DATA_SIZE);
60 return nullptr;
61 }
62 std::shared_ptr<MechDataBuffer> pclData = std::make_shared<MechDataBuffer>(protocolSize);
63
64 /** Header defination (BT_HEADER_LENTH + payload)
65 * | Version | CmdType | MsgID | TotalFrame | FrameSeq | Rev | Encry | Return | Payload |
66 * | 0.5 | 0.5 | 1 | 1 | 1 | 1 | 1 | 2 | n |
67 */
68 uint8_t versionAndType = (BT_PROTOCOL_VERSION << BIT_OFFSET_4) | static_cast<uint8_t>(ProtocolCmdType::REQ);
69 CHECK_ERR_RETURN_VALUE(pclData->AppendUint8(versionAndType), nullptr, "append versionAndType");
70 CHECK_ERR_RETURN_VALUE(pclData->AppendUint8(BT_PROTOCOL_MSG_ID), nullptr, "append MsgID");
71 CHECK_ERR_RETURN_VALUE(pclData->AppendUint8(BT_PROTOCOL_TOTAL_FRAME), nullptr, "append TotalFrame");
72 CHECK_ERR_RETURN_VALUE(pclData->AppendUint8(BT_PROTOCOL_FRAME_SEQ), nullptr, "append Rev");
73 CHECK_ERR_RETURN_VALUE(pclData->AppendUint8(BT_PROTOCOL_REV), nullptr, "append Encry");
74 CHECK_ERR_RETURN_VALUE(pclData->AppendUint8(BT_PROTOCOL_ENCRY), nullptr, "append Return");
75 CHECK_ERR_RETURN_VALUE(pclData->AppendUint16(BT_PROTOCOL_RETURN), nullptr, "append Payload");
76
77 /** Payload defination (BT_SERVICE_HEADER_LENTH + nameLength(n) + Body)
78 * | DataFormat | OptType | ServiceLenth | ServiceName | BodyLenth | Body |
79 * | 0.5 | 0.5 | 1 | n | 2 | N |
80 */
81 uint8_t formatAndOptType = (BT_PAYLOAD_DATA_FORMAT << BIT_OFFSET_4) | static_cast<uint8_t>(optType);
82 CHECK_ERR_RETURN_VALUE(pclData->AppendUint8(formatAndOptType), nullptr, "append formatAndOptType");
83 CHECK_ERR_RETURN_VALUE(pclData->AppendUint8(
84 static_cast<uint8_t>(serviceName_.size())), nullptr, "append ServiceLenth");
85 for (char ch : serviceName_) {
86 CHECK_ERR_RETURN_VALUE(pclData->AppendUint8(static_cast<uint8_t>(ch)), nullptr, "append ServiceName");
87 }
88 CHECK_ERR_RETURN_VALUE(pclData->AppendUint16(static_cast<uint16_t>(data->Size()) + BT_COMMAND_HEADER_LENTH),
89 nullptr, "append BodyLenth");
90
91 /** Body defination (BT_COMMAND_HEADER_LENTH + n)
92 * | SeqNo | Data |
93 * | 2 | n |
94 */
95 CHECK_ERR_RETURN_VALUE(pclData->AppendUint16(seqNo), nullptr, "append SeqNo");
96 CHECK_ERR_RETURN_VALUE(pclData->AppendDataBuffer(data), nullptr, "append Data");
97
98 HILOGD("end.");
99 return pclData;
100 }
101
Convert(OptType optType,uint16_t seqNo,std::shared_ptr<MechDataBuffer> data,std::string serviceName)102 std::shared_ptr<MechDataBuffer> ProtocolConverter::Convert(
103 OptType optType, uint16_t seqNo, std::shared_ptr<MechDataBuffer> data, std::string serviceName)
104 {
105 serviceName_ = serviceName;
106 return Convert(optType, seqNo, data);
107 }
108
GetData(std::shared_ptr<MechDataBuffer> pclData,uint16_t & seqNo,bool & isAck) const109 std::shared_ptr<MechDataBuffer> ProtocolConverter::GetData(
110 std::shared_ptr<MechDataBuffer> pclData, uint16_t &seqNo, bool &isAck) const
111 {
112 HILOGD("start.");
113 CHECK_POINTER_RETURN_VALUE(pclData, nullptr, "pclData");
114 uint8_t versionAndType = 0;
115 CHECK_ERR_RETURN_VALUE(pclData->ReadUint8(0, versionAndType), nullptr, "read cmdType");
116 uint8_t cmdType = versionAndType & BT_PROTOCOL_CMD_TYPE_MASK;
117 switch (cmdType) {
118 case static_cast<uint8_t>(ProtocolCmdType::RSP):
119 isAck = true;
120 break;
121 default:
122 isAck = false;
123 break;
124 }
125
126 // Get seqNo
127 size_t seqNoOffset = BT_HEADER_LENTH + BT_SERVICE_HEADER_LENTH + serviceName_.size();
128 CHECK_ERR_RETURN_VALUE(pclData->ReadUint16(seqNoOffset, seqNo), nullptr, "read seqNo");
129
130 // Get data lenth
131 size_t dataLenOffset = BT_HEADER_LENTH + BT_SERVICE_HEADER_LENTH + serviceName_.size() - BIT_OFFSET_2;
132 uint16_t dataLen = 0;
133 CHECK_ERR_RETURN_VALUE(pclData->ReadUint16(dataLenOffset, dataLen), nullptr, "read dataLen");
134
135 if (dataLen > pclData->Size() - seqNoOffset) {
136 HILOGE("datalen larger than actual length!");
137 return nullptr;
138 }
139
140 // Copy data
141 std::shared_ptr<MechDataBuffer> data = std::make_shared<MechDataBuffer>(static_cast<size_t>(dataLen));
142 data->SetRange(0, static_cast<size_t>(dataLen));
143 if (memcpy_s(
144 data->Data(), data->Capacity(), pclData->Data() + seqNoOffset + BT_COMMAND_HEADER_LENTH, dataLen) != 0) {
145 HILOGE("memcpy failed.");
146 return nullptr;
147 }
148 HILOGD("end.");
149 return data;
150 }
151
Validate(const uint8_t * data,size_t length) const152 bool ProtocolConverter::Validate(const uint8_t* data, size_t length) const
153 {
154 if (data == nullptr) {
155 HILOGE("data is nullptr!");
156 return false;
157 }
158 if (length == 0 || length > MAX_DATA_SIZE) {
159 HILOGE("data size %{public}zu is 0 or exceeds maximum size %{public}zu!", length, MAX_DATA_SIZE);
160 return false;
161 }
162 return true;
163 }
164 } // namespace MechBodyController
165 } // namespace OHOS
166