blob: 965ae74307448bb443246ec58150b19580d982ca [file] [log] [blame]
Pankaj Garg362e2a32020-03-10 13:54:37 -07001/*
2 *
Rob Walkere812e672020-03-31 17:51:57 -07003 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019 Google LLC.
Pankaj Garg362e2a32020-03-10 13:54:37 -07005 *
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 Gargfe3bbc82020-03-10 14:27:26 -070021 * Definitions for chip BLE service advertisement data.
Pankaj Garg362e2a32020-03-10 13:54:37 -070022 */
23
Andrei Litvin1873e8c2020-10-12 10:52:26 -040024#pragma once
Pankaj Garg362e2a32020-03-10 13:54:37 -070025
Zang MingJie53dd5832021-09-03 03:05:16 +080026#include <lib/core/CHIPEncoding.h>
Pankaj Garge26cd7a2020-03-24 13:22:28 -070027
Pankaj Gargfe3bbc82020-03-10 14:27:26 -070028namespace chip {
Pankaj Garg362e2a32020-03-10 13:54:37 -070029namespace Ble {
30
31/**
Pankaj Gargfe3bbc82020-03-10 14:27:26 -070032 * chip data block types that may appear with chip BLE service advertisement data.
Pankaj Garg362e2a32020-03-10 13:54:37 -070033 */
Pankaj Gargfe3bbc82020-03-10 14:27:26 -070034enum chipBLEServiceDataType
Pankaj Garg362e2a32020-03-10 13:54:37 -070035{
Martin Turoncd17ad42020-03-18 20:03:53 -070036 kchipBLEServiceDataType_DeviceIdentificationInfo = 0x01,
37 kchipBLEServiceDataType_TokenIdentificationInfo = 0x02,
Pankaj Garg362e2a32020-03-10 13:54:37 -070038};
39
40/**
Pankaj Gargfe3bbc82020-03-10 14:27:26 -070041 * chip BLE Device Identification Information Block
Pankaj Garg362e2a32020-03-10 13:54:37 -070042 *
43 * Defines the over-the-air encoded format of the device identification information block that appears
Pankaj Gargfe3bbc82020-03-10 14:27:26 -070044 * within chip BLE service advertisement data.
Pankaj Garg362e2a32020-03-10 13:54:37 -070045 */
Pankaj Garge26cd7a2020-03-24 13:22:28 -070046struct ChipBLEDeviceIdentificationInfo
Pankaj Garg362e2a32020-03-10 13:54:37 -070047{
Rohit Jadhav53443ec2022-02-25 19:56:22 +053048 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ólik4bf2d502020-09-29 18:39:14 +020052
Michael Spanga9bcacd2021-03-26 15:05:49 -040053 uint8_t OpCode;
Rohit Jadhav53443ec2022-02-25 19:56:22 +053054 // 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 Garg362e2a32020-03-10 13:54:37 -070058 uint8_t DeviceVendorId[2];
59 uint8_t DeviceProductId[2];
Shubham Patil175218b2022-01-04 21:31:30 +053060 uint8_t AdditionalDataFlag;
Pankaj Garg362e2a32020-03-10 13:54:37 -070061
Jiacheng Guoe1729df2020-07-22 02:58:47 +080062 void Init() { memset(this, 0, sizeof(*this)); }
Pankaj Garg362e2a32020-03-10 13:54:37 -070063
Damian Królik4bf2d502020-09-29 18:39:14 +020064 uint16_t GetVendorId() const { return chip::Encoding::LittleEndian::Get16(DeviceVendorId); }
Pankaj Garg362e2a32020-03-10 13:54:37 -070065
Martin Turoncd17ad42020-03-18 20:03:53 -070066 void SetVendorId(uint16_t vendorId) { chip::Encoding::LittleEndian::Put16(DeviceVendorId, vendorId); }
Pankaj Garg362e2a32020-03-10 13:54:37 -070067
Damian Królik4bf2d502020-09-29 18:39:14 +020068 uint16_t GetProductId() const { return chip::Encoding::LittleEndian::Get16(DeviceProductId); }
Pankaj Garg362e2a32020-03-10 13:54:37 -070069
Martin Turoncd17ad42020-03-18 20:03:53 -070070 void SetProductId(uint16_t productId) { chip::Encoding::LittleEndian::Put16(DeviceProductId, productId); }
Pankaj Garg362e2a32020-03-10 13:54:37 -070071
Rohit Jadhav53443ec2022-02-25 19:56:22 +053072 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ólik4bf2d502020-09-29 18:39:14 +020089 uint16_t GetDeviceDiscriminator() const
Jiacheng Guoe1729df2020-07-22 02:58:47 +080090 {
Rohit Jadhav53443ec2022-02-25 19:56:22 +053091 return chip::Encoding::LittleEndian::Get16(DeviceDiscriminatorAndAdvVersion) & kDiscriminatorMask;
Jiacheng Guoe1729df2020-07-22 02:58:47 +080092 }
93
94 void SetDeviceDiscriminator(uint16_t deviceDiscriminator)
95 {
Damian Królik4bf2d502020-09-29 18:39:14 +020096 // Discriminator is 12-bit long, so don't overwrite bits 12th through 15th
Arkadiusz Bokowya7060e02022-12-09 14:27:23 +010097 auto advVersion = static_cast<uint16_t>(DeviceDiscriminatorAndAdvVersion[1] << 8u & ~kDiscriminatorMask);
98 deviceDiscriminator = static_cast<uint16_t>(advVersion | (deviceDiscriminator & kDiscriminatorMask));
Rohit Jadhav53443ec2022-02-25 19:56:22 +053099 chip::Encoding::LittleEndian::Put16(DeviceDiscriminatorAndAdvVersion, deviceDiscriminator);
Jiacheng Guoe1729df2020-07-22 02:58:47 +0800100 }
Shubham Patil175218b2022-01-04 21:31:30 +0530101
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 Garg362e2a32020-03-10 13:54:37 -0700115} __attribute__((packed));
116
117} /* namespace Ble */
Pankaj Gargfe3bbc82020-03-10 14:27:26 -0700118} /* namespace chip */