1 /*
2  * Copyright (C) 2021 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 "classic_bluetooth_data.h"
17 
18 #include <cstring>
19 #include "classic_defs.h"
20 #include "log.h"
21 #include "securec.h"
22 
23 namespace OHOS {
24 namespace bluetooth {
ClassicBluetoothData()25 ClassicBluetoothData::ClassicBluetoothData()
26 {}
27 
~ClassicBluetoothData()28 ClassicBluetoothData::~ClassicBluetoothData()
29 {
30     offset_ = 0;
31     maxLength_ = 0;
32 }
33 
GetClassicBluetoothData() const34 std::vector<uint8_t> ClassicBluetoothData::GetClassicBluetoothData() const
35 {
36     int offset = 0;
37     std::vector<uint8_t> btData(maxLength_);
38     for (auto &data : dataStructList_) {
39         ClassicDataStructure dataStructure = data;
40         btData[offset] = dataStructure.GetLength();
41         btData[offset + EXTEND_INQUIRY_RESPONSE_LENGTH_SIZE] = dataStructure.GetType();
42         std::vector<uint8_t> dataVec = dataStructure.GetDataValue();
43         errno_t ret =
44             memcpy_s(&btData[offset + EXTEND_INQUIRY_RESPONSE_LENGTH_SIZE + EXTEND_INQUIRY_RESPONSE_TYPE_SIZE],
45                 (maxLength_ - offset - EXTEND_INQUIRY_RESPONSE_TYPE_SIZE - EXTEND_INQUIRY_RESPONSE_LENGTH_SIZE),
46                 &dataVec[0],
47                 (dataStructure.GetLength() - 1));
48         if (ret != EOK) {
49             LOG_ERROR("%{public}s::memcpy failed!", __func__);
50         }
51         offset = offset + dataStructure.GetLength() + 1;
52     }
53 
54     return btData;
55 }
56 
SetDataMaxLength(int len)57 void ClassicBluetoothData::SetDataMaxLength(int len)
58 {
59     maxLength_ = len;
60 }
61 
AddDataStructure(ClassicDataStructure dataStruct)62 bool ClassicBluetoothData::AddDataStructure(ClassicDataStructure dataStruct)
63 {
64     int offset = offset_ + dataStruct.GetLength() + 1;
65     if (offset > maxLength_) {
66         LOG_ERROR("%{public}s::data structure over max length!", __func__);
67         return false;
68     } else {
69         dataStructList_.push_back(dataStruct);
70         offset_ = offset;
71         return true;
72     }
73 }
74 
ParserData(const std::vector<uint8_t> & data) const75 std::vector<ClassicDataStructure> ClassicBluetoothData::ParserData(const std::vector<uint8_t> &data) const
76 {
77     int offset = 0;
78     std::vector<ClassicDataStructure> dataList;
79 
80     while (offset < maxLength_) {
81         int length = data[offset];
82         if (length == 0) {
83             break;
84         } else {
85             int type = data[offset + EXTEND_INQUIRY_RESPONSE_LENGTH_SIZE];
86             std::vector<uint8_t> value;
87             int valueStartIdx = offset + EXTEND_INQUIRY_RESPONSE_TYPE_SIZE + EXTEND_INQUIRY_RESPONSE_LENGTH_SIZE;
88             int valueEndIdx = valueStartIdx + length - 1;
89             value.insert(value.begin(), (data.begin() + valueStartIdx), (data.begin() + valueEndIdx));
90             ClassicDataStructure dataBlock(length, type, value);
91             dataList.push_back(dataBlock);
92             offset = offset + length + 1;
93         }
94     }
95 
96     return dataList;
97 }
98 }  // namespace bluetooth
99 }  // namespace OHOS