Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2019 Google LLC.
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
21 : * Definitions for chip BLE service advertisement data.
22 : */
23 :
24 : #pragma once
25 :
26 : #ifndef _CHIP_BLE_BLE_H
27 : #error "Please include <ble/Ble.h> instead!"
28 : #endif
29 :
30 : #include <cstdint>
31 :
32 : #include <lib/core/CHIPEncoding.h>
33 :
34 : namespace chip {
35 : namespace Ble {
36 :
37 : /**
38 : * chip data block types that may appear with chip BLE service advertisement data.
39 : */
40 : enum chipBLEServiceDataType
41 : {
42 : kchipBLEServiceDataType_DeviceIdentificationInfo = 0x01,
43 : kchipBLEServiceDataType_TokenIdentificationInfo = 0x02,
44 : };
45 :
46 : /**
47 : * chip BLE Device Identification Information Block
48 : *
49 : * Defines the over-the-air encoded format of the device identification information block that appears
50 : * within chip BLE service advertisement data.
51 : */
52 : struct ChipBLEDeviceIdentificationInfo
53 : {
54 : constexpr static uint16_t kDiscriminatorMask = 0xfff;
55 : constexpr static uint8_t kAdditionalDataFlagMask = 0x1;
56 : constexpr static uint8_t kExtendedAnnouncementFlagMask = 0x2;
57 : constexpr static uint8_t kAdvertisementVersionMask = 0xf0;
58 : constexpr static uint8_t kAdvertisementVersionShiftBits = 4u;
59 :
60 : uint8_t OpCode;
61 : // DeviceDiscriminatorAndAdvVersion[0] contains the low 8 bits of the 12-bit discriminator.
62 : // DeviceDiscriminatorAndAdvVersion[1] contains the high 8 bits of the 12-bit discriminator in its low 4 bits and
63 : // the 4 bits of the advertisement version in its high 4 bits.
64 : uint8_t DeviceDiscriminatorAndAdvVersion[2];
65 : uint8_t DeviceVendorId[2];
66 : uint8_t DeviceProductId[2];
67 : uint8_t AdditionalDataFlag;
68 :
69 0 : void Init() { memset(this, 0, sizeof(*this)); }
70 :
71 : uint16_t GetVendorId() const { return chip::Encoding::LittleEndian::Get16(DeviceVendorId); }
72 :
73 0 : void SetVendorId(uint16_t vendorId) { chip::Encoding::LittleEndian::Put16(DeviceVendorId, vendorId); }
74 :
75 : uint16_t GetProductId() const { return chip::Encoding::LittleEndian::Get16(DeviceProductId); }
76 :
77 0 : void SetProductId(uint16_t productId) { chip::Encoding::LittleEndian::Put16(DeviceProductId, productId); }
78 :
79 : uint8_t GetAdvertisementVersion() const
80 : {
81 : uint8_t advertisementVersion = static_cast<uint8_t>((DeviceDiscriminatorAndAdvVersion[1] & kAdvertisementVersionMask) >>
82 : kAdvertisementVersionShiftBits);
83 : return advertisementVersion;
84 : }
85 :
86 : // Use only 4 bits to set advertisement version
87 0 : void SetAdvertisementVersion(uint8_t advertisementVersion)
88 : {
89 : // Advertisement Version is 4 bit long from 12th to 15th
90 0 : advertisementVersion =
91 0 : static_cast<uint8_t>((advertisementVersion << kAdvertisementVersionShiftBits) & kAdvertisementVersionMask);
92 0 : DeviceDiscriminatorAndAdvVersion[1] =
93 0 : static_cast<uint8_t>((DeviceDiscriminatorAndAdvVersion[1] & ~kAdvertisementVersionMask) | advertisementVersion);
94 0 : }
95 :
96 0 : uint16_t GetDeviceDiscriminator() const
97 : {
98 0 : return chip::Encoding::LittleEndian::Get16(DeviceDiscriminatorAndAdvVersion) & kDiscriminatorMask;
99 : }
100 :
101 0 : void SetDeviceDiscriminator(uint16_t deviceDiscriminator)
102 : {
103 : // Discriminator is 12-bit long, so don't overwrite bits 12th through 15th
104 0 : auto advVersion = static_cast<uint16_t>(DeviceDiscriminatorAndAdvVersion[1] << 8u & ~kDiscriminatorMask);
105 0 : deviceDiscriminator = static_cast<uint16_t>(advVersion | (deviceDiscriminator & kDiscriminatorMask));
106 0 : chip::Encoding::LittleEndian::Put16(DeviceDiscriminatorAndAdvVersion, deviceDiscriminator);
107 0 : }
108 :
109 : uint8_t GetAdditionalDataFlag() const { return (AdditionalDataFlag & kAdditionalDataFlagMask); }
110 :
111 : void SetAdditionalDataFlag(bool flag)
112 : {
113 : if (flag)
114 : {
115 : AdditionalDataFlag |= kAdditionalDataFlagMask;
116 : }
117 : else
118 : {
119 : AdditionalDataFlag &= static_cast<uint8_t>(~kAdditionalDataFlagMask);
120 : }
121 : }
122 :
123 : void SetExtendedAnnouncementFlag(bool flag)
124 : {
125 : if (flag)
126 : {
127 : AdditionalDataFlag |= kExtendedAnnouncementFlagMask;
128 : }
129 : else
130 : {
131 : AdditionalDataFlag &= static_cast<uint8_t>(~kExtendedAnnouncementFlagMask);
132 : }
133 : }
134 :
135 : } __attribute__((packed));
136 :
137 : } /* namespace Ble */
138 : } /* namespace chip */
|