1 /* 2 * Copyright (C) 2023 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 #ifndef NDEF_BT_DATA_PARSER_H 16 #define NDEF_BT_DATA_PARSER_H 17 18 #include <string> 19 #include "bluetooth_def.h" 20 #include "bluetooth_device_class.h" 21 #include "uuid.h" 22 23 namespace OHOS { 24 namespace NFC { 25 namespace TAG { 26 class BtData { 27 public: 28 bool isValid_ = false; 29 std::string name_ = ""; 30 std::string oobData_ = ""; 31 bool carrierActivating_ = false; 32 uint8_t transport_ = Bluetooth::GATT_TRANSPORT_TYPE_AUTO; 33 std::vector<Bluetooth::UUID> uuids_ {}; 34 Bluetooth::BluetoothDeviceClass btClass_ {}; 35 36 std::string macAddress_ = ""; 37 std::string macAddrOrg_ = ""; 38 std::string vendorSerialNum_ = ""; 39 std::string vendorPayload_ = ""; 40 }; 41 42 class NdefBtDataParser { 43 public: 44 NdefBtDataParser(); ~NdefBtDataParser()45 ~NdefBtDataParser() {} 46 static std::shared_ptr<BtData> CheckBtRecord(const std::string& msg); 47 static bool IsVendorPayloadValid(const std::string& payload); 48 49 private: 50 static std::shared_ptr<BtData> ParseBtRecord(const std::string& payload); 51 static std::shared_ptr<BtData> ParseBleRecord(const std::string& payload); 52 static std::vector<Bluetooth::UUID> GetUuidFromPayload(const std::string& payload, uint32_t& offset, 53 uint32_t type, uint32_t len); 54 static std::string GetDataFromPayload(const std::string& payload, uint32_t& offset, uint32_t datalen); 55 static std::string FormatBtMacAddr(const std::string& orgBtMac); 56 static std::string GetOrgBtMacFromPayload(const std::string& payload, uint32_t& offset); 57 static bool GetBtDevClass(const std::string& payload, uint32_t& offset, 58 Bluetooth::BluetoothDeviceClass& btClass); 59 static std::string RevertUuidStr(const std::string& uuid); 60 static Bluetooth::UUID FormatUuidTo128Bit(const std::string& uuid); 61 62 const static int VENDOR_PAYLOAD_MAX_LEN = 0xFF; // vendor payload length is 1 byte 63 }; 64 } // namespace TAG 65 } // namespace NFC 66 } // namespace OHOS 67 #endif // NDEF_BT_DATA_PARSER_H