Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Rob Walker | e812e67 | 2020-03-31 17:51:57 -0700 | [diff] [blame] | 3 | * Copyright (c) 2020 Project CHIP Authors |
| 4 | * Copyright (c) 2019 Google LLC. |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * @file |
Pankaj Garg | fe3bbc8 | 2020-03-10 14:27:26 -0700 | [diff] [blame] | 21 | * Definitions for chip BLE service advertisement data. |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Andrei Litvin | 1873e8c | 2020-10-12 10:52:26 -0400 | [diff] [blame] | 24 | #pragma once |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 25 | |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 26 | #include <lib/core/CHIPEncoding.h> |
Pankaj Garg | e26cd7a | 2020-03-24 13:22:28 -0700 | [diff] [blame] | 27 | |
Pankaj Garg | fe3bbc8 | 2020-03-10 14:27:26 -0700 | [diff] [blame] | 28 | namespace chip { |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 29 | namespace Ble { |
| 30 | |
| 31 | /** |
Pankaj Garg | fe3bbc8 | 2020-03-10 14:27:26 -0700 | [diff] [blame] | 32 | * chip data block types that may appear with chip BLE service advertisement data. |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 33 | */ |
Pankaj Garg | fe3bbc8 | 2020-03-10 14:27:26 -0700 | [diff] [blame] | 34 | enum chipBLEServiceDataType |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 35 | { |
Martin Turon | cd17ad4 | 2020-03-18 20:03:53 -0700 | [diff] [blame] | 36 | kchipBLEServiceDataType_DeviceIdentificationInfo = 0x01, |
| 37 | kchipBLEServiceDataType_TokenIdentificationInfo = 0x02, |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | /** |
Pankaj Garg | fe3bbc8 | 2020-03-10 14:27:26 -0700 | [diff] [blame] | 41 | * chip BLE Device Identification Information Block |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 42 | * |
| 43 | * Defines the over-the-air encoded format of the device identification information block that appears |
Pankaj Garg | fe3bbc8 | 2020-03-10 14:27:26 -0700 | [diff] [blame] | 44 | * within chip BLE service advertisement data. |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 45 | */ |
Pankaj Garg | e26cd7a | 2020-03-24 13:22:28 -0700 | [diff] [blame] | 46 | struct ChipBLEDeviceIdentificationInfo |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 47 | { |
Rohit Jadhav | 53443ec | 2022-02-25 19:56:22 +0530 | [diff] [blame] | 48 | constexpr static uint16_t kDiscriminatorMask = 0xfff; |
| 49 | constexpr static uint8_t kAdditionalDataFlagMask = 0x1; |
| 50 | constexpr static uint8_t kAdvertisementVersionMask = 0xf0; |
| 51 | constexpr static uint8_t kAdvertisementVersionShiftBits = 4u; |
Damian Królik | 4bf2d50 | 2020-09-29 18:39:14 +0200 | [diff] [blame] | 52 | |
Michael Spang | a9bcacd | 2021-03-26 15:05:49 -0400 | [diff] [blame] | 53 | uint8_t OpCode; |
Rohit Jadhav | 53443ec | 2022-02-25 19:56:22 +0530 | [diff] [blame] | 54 | // DeviceDiscriminatorAndAdvVersion[0] contains the low 8 bits of the 12-bit discriminator. |
| 55 | // DeviceDiscriminatorAndAdvVersion[1] contains the high 8 bits of the 12-bit discriminator in its low 4 bits and |
| 56 | // the 4 bits of the advertisement version in its high 4 bits. |
| 57 | uint8_t DeviceDiscriminatorAndAdvVersion[2]; |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 58 | uint8_t DeviceVendorId[2]; |
| 59 | uint8_t DeviceProductId[2]; |
Shubham Patil | 175218b | 2022-01-04 21:31:30 +0530 | [diff] [blame] | 60 | uint8_t AdditionalDataFlag; |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 61 | |
Jiacheng Guo | e1729df | 2020-07-22 02:58:47 +0800 | [diff] [blame] | 62 | void Init() { memset(this, 0, sizeof(*this)); } |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 63 | |
Damian Królik | 4bf2d50 | 2020-09-29 18:39:14 +0200 | [diff] [blame] | 64 | uint16_t GetVendorId() const { return chip::Encoding::LittleEndian::Get16(DeviceVendorId); } |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 65 | |
Martin Turon | cd17ad4 | 2020-03-18 20:03:53 -0700 | [diff] [blame] | 66 | void SetVendorId(uint16_t vendorId) { chip::Encoding::LittleEndian::Put16(DeviceVendorId, vendorId); } |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 67 | |
Damian Królik | 4bf2d50 | 2020-09-29 18:39:14 +0200 | [diff] [blame] | 68 | uint16_t GetProductId() const { return chip::Encoding::LittleEndian::Get16(DeviceProductId); } |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 69 | |
Martin Turon | cd17ad4 | 2020-03-18 20:03:53 -0700 | [diff] [blame] | 70 | void SetProductId(uint16_t productId) { chip::Encoding::LittleEndian::Put16(DeviceProductId, productId); } |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 71 | |
Rohit Jadhav | 53443ec | 2022-02-25 19:56:22 +0530 | [diff] [blame] | 72 | uint8_t GetAdvertisementVersion() const |
| 73 | { |
| 74 | uint8_t advertisementVersion = static_cast<uint8_t>((DeviceDiscriminatorAndAdvVersion[1] & kAdvertisementVersionMask) >> |
| 75 | kAdvertisementVersionShiftBits); |
| 76 | return advertisementVersion; |
| 77 | } |
| 78 | |
| 79 | // Use only 4 bits to set advertisement version |
| 80 | void SetAdvertisementVersion(uint8_t advertisementVersion) |
| 81 | { |
| 82 | // Advertisement Version is 4 bit long from 12th to 15th |
| 83 | advertisementVersion = |
| 84 | static_cast<uint8_t>((advertisementVersion << kAdvertisementVersionShiftBits) & kAdvertisementVersionMask); |
| 85 | DeviceDiscriminatorAndAdvVersion[1] = |
| 86 | static_cast<uint8_t>((DeviceDiscriminatorAndAdvVersion[1] & ~kAdvertisementVersionMask) | advertisementVersion); |
| 87 | } |
| 88 | |
Damian Królik | 4bf2d50 | 2020-09-29 18:39:14 +0200 | [diff] [blame] | 89 | uint16_t GetDeviceDiscriminator() const |
Jiacheng Guo | e1729df | 2020-07-22 02:58:47 +0800 | [diff] [blame] | 90 | { |
Rohit Jadhav | 53443ec | 2022-02-25 19:56:22 +0530 | [diff] [blame] | 91 | return chip::Encoding::LittleEndian::Get16(DeviceDiscriminatorAndAdvVersion) & kDiscriminatorMask; |
Jiacheng Guo | e1729df | 2020-07-22 02:58:47 +0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void SetDeviceDiscriminator(uint16_t deviceDiscriminator) |
| 95 | { |
Damian Królik | 4bf2d50 | 2020-09-29 18:39:14 +0200 | [diff] [blame] | 96 | // Discriminator is 12-bit long, so don't overwrite bits 12th through 15th |
Arkadiusz Bokowy | a7060e0 | 2022-12-09 14:27:23 +0100 | [diff] [blame] | 97 | auto advVersion = static_cast<uint16_t>(DeviceDiscriminatorAndAdvVersion[1] << 8u & ~kDiscriminatorMask); |
| 98 | deviceDiscriminator = static_cast<uint16_t>(advVersion | (deviceDiscriminator & kDiscriminatorMask)); |
Rohit Jadhav | 53443ec | 2022-02-25 19:56:22 +0530 | [diff] [blame] | 99 | chip::Encoding::LittleEndian::Put16(DeviceDiscriminatorAndAdvVersion, deviceDiscriminator); |
Jiacheng Guo | e1729df | 2020-07-22 02:58:47 +0800 | [diff] [blame] | 100 | } |
Shubham Patil | 175218b | 2022-01-04 21:31:30 +0530 | [diff] [blame] | 101 | |
| 102 | uint8_t GetAdditionalDataFlag() const { return (AdditionalDataFlag & kAdditionalDataFlagMask); } |
| 103 | |
| 104 | void SetAdditionalDataFlag(bool flag) |
| 105 | { |
| 106 | if (flag) |
| 107 | { |
| 108 | AdditionalDataFlag |= kAdditionalDataFlagMask; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | AdditionalDataFlag &= static_cast<uint8_t>(~kAdditionalDataFlagMask); |
| 113 | } |
| 114 | } |
Pankaj Garg | 362e2a3 | 2020-03-10 13:54:37 -0700 | [diff] [blame] | 115 | } __attribute__((packed)); |
| 116 | |
| 117 | } /* namespace Ble */ |
Pankaj Garg | fe3bbc8 | 2020-03-10 14:27:26 -0700 | [diff] [blame] | 118 | } /* namespace chip */ |