Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
BTSecurityRegistry.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#ifndef DBT_SEC_SETTINGS_HPP_
27#define DBT_SEC_SETTINGS_HPP_
28
29#include <string>
30#include <cstdio>
31
33
34namespace direct_bt {
35
36 /** \addtogroup DBTUserAPI
37 *
38 * @{
39 */
40
41 /**
42 * Application toolkit providing BT security setup and its device association
43 * on a pattern matching basis, i.e. EUI48Sub or name-sub.
44 */
45 namespace BTSecurityRegistry {
46
47 /** \addtogroup DBTUserAPI
48 *
49 * @{
50 */
51
52 struct Entry {
53 static constexpr int NO_PASSKEY = -1;
54
56 std::string nameSub;
57
62
63 Entry(const EUI48Sub& addrSub_)
64 : addrSub(addrSub_), nameSub() {}
65
66 Entry(std::string nameSub_)
67 : addrSub(EUI48Sub::ALL_DEVICE), nameSub(std::move(nameSub_)) {}
68
69 constexpr bool isSecLevelOrIOCapSet() const noexcept {
71 }
72 constexpr const BTSecurityLevel& getSecLevel() const noexcept { return sec_level; }
73 constexpr const SMPIOCapability& getIOCap() const noexcept { return io_cap; }
74
75 constexpr bool isSecurityAutoEnabled() const noexcept {
77 }
78 constexpr const SMPIOCapability& getSecurityAutoIOCap() const noexcept { return io_cap_auto; }
79
80 constexpr int getPairingPasskey() const noexcept { return passkey; }
81
82 constexpr bool getPairingNumericComparison() const noexcept { return true; }
83
84 std::string toString() const noexcept {
85 const std::string id = addrSub == EUI48Sub::ALL_DEVICE ? "'"+nameSub+"'" : addrSub.toString();
86 return "BTSecurityDetail["+id+", lvl "+
88 ", io "+to_string(io_cap)+
89 ", auto-io "+to_string(io_cap_auto)+
90 ", passkey "+std::to_string(passkey)+"]";
91 }
92 };
93
94 /**
95 * Function for user defined EUI48 address and name BTSecurityRegistry::Entry matching criteria and algorithm.
96 * <p>
97 * Return {@code true} if the given {@code address} or {@code name} matches
98 * with the BTSecurityRegistry::Entry.
99 * </p>
100 *
101 * @param address EUI48 address
102 * @param name optional name, maybe empty
103 * @param e Entry entry
104 */
105 typedef bool (*AddressNameEntryMatchFunc)(const EUI48& address, const std::string& name, const Entry& e);
106
107 /**
108 * Function for user defined EUI48Sub addressSub and name BTSecurityRegistry::Entry matching criteria and algorithm.
109 * <p>
110 * Return {@code true} if the given {@code addressSub} or {@code name} matches
111 * with the BTSecurityRegistry::Entry.
112 * </p>
113 *
114 * @param addressSub EUI48Sub address
115 * @param name optional name, maybe empty
116 * @param e Entry entry
117 */
118 typedef bool (*AddressSubNameEntryMatchFunc)(const EUI48Sub& addressSub, const std::string& name, const Entry& e);
119
120 /**
121 * Function for user defined std::string name BTSecurityRegistry::Entry matching criteria and algorithm.
122 * <p>
123 * Return {@code true} if the given {@code name} matches
124 * with the BTSecurityRegistry::Entry.
125 * </p>
126 *
127 * @param name
128 * @param e Entry entry
129 */
130 typedef bool (*NameEntryMatchFunc)(const std::string& name, const Entry& e);
131
132 /**
133 * Returns a matching BTSecurityRegistry::Entry with the given {@code addr} and/or {@code name}.
134 * <p>
135 * Matching criteria and algorithm is defined by the given AddressNameEntryMatchFunc.
136 * </p>
137 */
138 Entry* get(const EUI48& addr, const std::string& name, AddressNameEntryMatchFunc m) noexcept;
139
140 /**
141 * Returns a matching BTSecurityRegistry::Entry with the given {@code addrSub} and/or {@code name}.
142 * <p>
143 * Matching criteria and algorithm is defined by the given AddressSubNameEntryMatchFunc.
144 * </p>
145 */
146 Entry* get(const EUI48Sub& addrSub, const std::string& name, AddressSubNameEntryMatchFunc m) noexcept;
147
148 /**
149 * Returns a matching BTSecurityRegistry::Entry with the given {@code name}.
150 * <p>
151 * Matching criteria and algorithm is defined by the given NameEntryMatchFunc.
152 * </p>
153 */
154 Entry* get(const std::string& name, NameEntryMatchFunc m) noexcept;
155
156 /**
157 * Returns a matching Entry,
158 * - which Entry::addrSub is set and the given {@code addr} starts with Entry::addrSub, or
159 * - which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
160 *
161 * Otherwise {@code null} is returned.
162 */
163 inline Entry* getStartOf(const EUI48& addr, const std::string& name) noexcept {
164 return get(addr, name, [](const EUI48& a, const std::string& n, const Entry& e)->bool {
165 return ( e.addrSub.length > 0 && 0 == a.indexOf(e.addrSub, jau::lb_endian_t::big) ) ||
166 ( e.nameSub.length() > 0 && 0 == n.find(e.nameSub) );
167 });
168 }
169 /**
170 * Returns a matching Entry,
171 * - which Entry::addrSub is set and the given {@code addrSub} starts with Entry::addrSub, or
172 * - which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
173 *
174 * Otherwise {@code null} is returned.
175 */
176 inline Entry* getStartOf(const EUI48Sub& addrSub, const std::string& name) noexcept {
177 return get(addrSub, name, [](const EUI48Sub& as, const std::string& n, const Entry& e)->bool {
178 return ( e.addrSub.length > 0 && 0 == as.indexOf(e.addrSub, jau::lb_endian_t::big) ) ||
179 ( e.nameSub.length() > 0 && 0 == n.find(e.nameSub) );
180 });
181 }
182 /**
183 * Returns a matching Entry,
184 * which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
185 *
186 * Otherwise {@code null} is returned.
187 */
188 inline Entry* getStartOf(const std::string& name) noexcept {
189 return get(name, [](const std::string& n, const Entry& e)->bool {
190 return e.nameSub.length() > 0 && 0 == n.find(e.nameSub);
191 });
192 }
193
194 /**
195 * Returns a matching Entry,
196 * - which Entry::addrSub is set and the given {@code addrSub} starts with Entry::addrSub, or
197 * - which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
198 *
199 * Otherwise {@code null} is returned.
200 */
201 inline Entry* getEqual(const EUI48Sub& addrSub, const std::string& name) noexcept {
202 return get(addrSub, name, [](const EUI48Sub& as, const std::string& n, const Entry& e)->bool {
203 return ( e.addrSub.length > 0 && as == e.addrSub ) ||
204 ( e.nameSub.length() > 0 && n == e.nameSub );
205 });
206 }
207 /**
208 * Returns a matching Entry,
209 * which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
210 *
211 * Otherwise {@code null} is returned.
212 */
213 inline Entry* getEqual(const std::string& name) noexcept {
214 return get(name, [](const std::string& n, const Entry& e)->bool {
215 return e.nameSub.length() > 0 && n == e.nameSub;
216 });
217 }
218
219 /**
220 * Returns the reference of the current list of Entry, not a copy.
221 */
222 jau::darray<Entry>& getEntries() noexcept;
223
224 /**
225 * Determines whether the given {@code addrOrNameSub} is a EUI48Sub or just a {@code name}
226 * and retrieves an entry. If no entry exists, creates a new entry.
227 * <p>
228 * Implementation uses getEqual() to find a pre-existing entry.
229 * </p>
230 * @param addrOrNameSub either a EUI48Sub or just a name
231 * @return new or existing instance
232 */
233 Entry* getOrCreate(const std::string& addrOrNameSub) noexcept;
234
235 /**
236 * Clears internal list
237 */
238 void clear() noexcept;
239
240 std::string allToString() noexcept;
241
242 /**@}*/
243
244 } // namespace BTSecurityRegistry
245
246 /**@}*/
247
248} // namespace direct_bt
249
250#endif /* DBT_SEC_SETTINGS_HPP_ */
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:148
@ big
Identifier for big endian, equivalent to endian::big.
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
SMPIOCapability
Vol 3, Part H, 2.3.2 IO capabilities.
Definition: SMPTypes.hpp:209
@ UNSET
Denoting unset value, i.e.
Entry * getStartOf(const EUI48 &addr, const std::string &name) noexcept
Returns a matching Entry,.
bool(* NameEntryMatchFunc)(const std::string &name, const Entry &e)
Function for user defined std::string name BTSecurityRegistry::Entry matching criteria and algorithm.
Entry * getOrCreate(const std::string &addrOrNameSub) noexcept
Determines whether the given addrOrNameSub is a EUI48Sub or just a name and retrieves an entry.
void clear() noexcept
Clears internal list.
bool(* AddressNameEntryMatchFunc)(const EUI48 &address, const std::string &name, const Entry &e)
Function for user defined EUI48 address and name BTSecurityRegistry::Entry matching criteria and algo...
std::string to_string(const DiscoveryPolicy v) noexcept
Definition: BTAdapter.cpp:58
bool(* AddressSubNameEntryMatchFunc)(const EUI48Sub &addressSub, const std::string &name, const Entry &e)
Function for user defined EUI48Sub addressSub and name BTSecurityRegistry::Entry matching criteria an...
Entry * get(const EUI48 &addr, const std::string &name, AddressNameEntryMatchFunc m) noexcept
Returns a matching BTSecurityRegistry::Entry with the given addr and/or name.
Entry * getEqual(const EUI48Sub &addrSub, const std::string &name) noexcept
Returns a matching Entry,.
std::string allToString() noexcept
BTSecurityLevel
Bluetooth Security Level.
Definition: BTTypes0.hpp:267
jau::darray< Entry > & getEntries() noexcept
Returns the reference of the current list of Entry, not a copy.
@ UNSET
Security Level not set, value 0.
STL namespace.
constexpr bool isSecLevelOrIOCapSet() const noexcept
constexpr const BTSecurityLevel & getSecLevel() const noexcept
constexpr int getPairingPasskey() const noexcept
constexpr bool getPairingNumericComparison() const noexcept
constexpr const SMPIOCapability & getSecurityAutoIOCap() const noexcept
constexpr const SMPIOCapability & getIOCap() const noexcept
constexpr bool isSecurityAutoEnabled() const noexcept
std::string toString() const noexcept
A 48 bit EUI-48 sub-identifier, see EUI48.
Definition: eui48.hpp:51
jau::nsize_t length
The actual length in bytes of the EUI48 sub-address, less or equal 6 bytes.
Definition: eui48.hpp:68
static jau::snsize_t indexOf(const uint8_t haystack_b[], const jau::nsize_t haystack_length, const uint8_t needle_b[], const jau::nsize_t needle_length, const lb_endian_t byte_order) noexcept
Find index of needle within haystack in the given byte order.
Definition: eui48.cpp:129
std::string toString() const noexcept
Returns the EUI48 sub-string representation with MSB first (lb_endian::big), less or equal 17 charact...
Definition: eui48.cpp:36
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
Definition: eui48.hpp:324
jau::snsize_t indexOf(const EUI48Sub &needle, const lb_endian_t byte_order) const noexcept
Definition: eui48.hpp:324