Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ble/Defs.cxx
Go to the documentation of this file.
1
28#include "ble/Defs.hxx"
29
30namespace ble
31{
32
33const uint8_t Defs::PRIMARY_SERVICE_UUID[2] = {0x00, 0x28};
34const uint8_t Defs::SECONDARY_SERVICE_UUID[2] = {0x01, 0x28};
35const uint8_t Defs::CHAR_DECLARATOIN_UUID[2] = {0x03, 0x28};
36const uint8_t Defs::CHAR_CLIENT_CONFIG_UUID[2] = {0x02, 0x29};
37
38const uint8_t Defs::CHAR_PROP_READ_WRITE_NOTIFY[1] =
39{
40 (1 << 1) | // read
41 (1 << 3) | // write with response
42 (1 << 4) // notify
43};
44
45const uint8_t Defs::CHAR_PROP_READ_NOTIFY_ACK[1] =
46{
47 (1 << 1) | // read
48 (1 << 5) // notify with ack
49};
50
51const uint8_t Defs::CHAR_PROP_WRITE[1] =
52{
53 (1 << 3) // write
54};
55
56//
57// Defs::adv_find_data()
58//
59ssize_t Defs::adv_find_data(std::basic_string<uint8_t> &adv,
60 AdvType type, uint8_t *size, unsigned instance)
61{
62 char t = static_cast<char>(type);
63
64 for (size_t idx = 1; instance && idx < adv.size(); ++idx)
65 {
66 uint8_t len = adv[idx - 1];
67 if (adv[idx] == t)
68 {
69 if (size)
70 {
71 *size = len - 1;
72 }
73 if (--instance == 0)
74 {
75 return idx - 1;
76 }
77 }
78 // One additional added by the four loop to skip over next length.
79 idx += len;
80 }
81
82 return -1;
83}
84
85//
86// Defs::adv_find_name_short()
87//
89 std::basic_string<uint8_t> &adv, unsigned instance)
90{
91 ssize_t pos;
92 uint8_t size;
93
94 pos = adv_find_data(adv, AdvType::NAME_SHORT, &size, instance);
95
96 return (pos < 0) ?
97 std::string() :
98 std::string((const char*)(adv.data() + pos + 2), size);
99}
100
101//
102// Defs::adv_find_name_complete()
103//
105 std::basic_string<uint8_t> &adv, unsigned instance)
106{
107 ssize_t pos;
108 uint8_t size;
109
110 pos = adv_find_data(adv, AdvType::NAME_COMPLETE, &size, instance);
111
112 return (pos < 0) ?
113 std::string() :
114 std::string((const char*)(adv.data() + pos + 2), size);
115}
116
117//
118// Defs::adv_find_service_data_128()
119//
120std::basic_string<uint8_t> Defs::adv_find_service_data_128(
121 std::basic_string<uint8_t> &adv, const uint8_t service_uuid[16],
122 unsigned instance)
123{
124 ssize_t pos;
125 unsigned inst = 1;
126 uint8_t size;
127
128 for ( ; /* forever */ ; )
129 {
130 pos = adv_find_data(adv, AdvType::SERVICE_DATA_128, &size, inst);
131 if (pos < 0)
132 {
133 // Not found.
134 break;
135 }
136 if (adv.compare(pos + 2, 16, service_uuid, 16) == 0)
137 {
138 // Match.
139 if (inst == instance)
140 {
141 // Found and instance count matches.
142 return std::basic_string<uint8_t>(adv, pos + 2 + 16, size - 16);
143 }
144 }
145 ++inst;
146 }
147
148 // Not found.
149 return std::basic_string<uint8_t>();
150}
151
152} // namespace ble
static const uint8_t CHAR_PROP_READ_WRITE_NOTIFY[1]
Characteristic read/write/notify property.
Definition ble/Defs.hxx:64
AdvType
Advertising types.
Definition ble/Defs.hxx:106
@ SERVICE_DATA_128
128-bit service UUID folloed by data
@ NAME_COMPLETE
complete local name
@ NAME_SHORT
shortened local name
static const uint8_t CHAR_PROP_READ_NOTIFY_ACK[1]
Characteristic read/write/notify property.
Definition ble/Defs.hxx:67
static const uint8_t CHAR_PROP_WRITE[1]
Characteristic read/write/notify property.
Definition ble/Defs.hxx:70
static const uint8_t PRIMARY_SERVICE_UUID[2]
Primary service UUID.
Definition ble/Defs.hxx:52
static const uint8_t CHAR_CLIENT_CONFIG_UUID[2]
Characterisitic Client Config Descriptor (CCCD) UUID.
Definition ble/Defs.hxx:61
static const uint8_t SECONDARY_SERVICE_UUID[2]
Secondary service UUID.
Definition ble/Defs.hxx:55
static std::string adv_find_name_short(std::basic_string< uint8_t > &adv, unsigned instance=1)
Find an advertisment name short type within an advertisement set.
Definition ble/Defs.cxx:88
static ssize_t adv_find_data(std::basic_string< uint8_t > &adv, AdvType type, uint8_t *size, unsigned instance=1)
Find an advertisment data within an advertisement set.
Definition ble/Defs.cxx:59
static std::basic_string< uint8_t > adv_find_service_data_128(std::basic_string< uint8_t > &adv, const uint8_t service_uuid[16], unsigned instance=1)
Find an advertisment service data 128 type within an advertisement set.
Definition ble/Defs.cxx:120
static const uint8_t CHAR_DECLARATOIN_UUID[2]
Characteristic UUID.
Definition ble/Defs.hxx:58
static std::string adv_find_name_complete(std::basic_string< uint8_t > &adv, unsigned instance=1)
Find an advertisment name complete type within an advertisement set.
Definition ble/Defs.cxx:104