Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
BTSecurityRegistry.cpp
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
27
28#include <jau/darray.hpp>
29
30using namespace direct_bt;
31
33
35
36 Entry* get(const EUI48& addr, const std::string& name, AddressNameEntryMatchFunc m) noexcept {
37 auto first = devicesSecDetails.begin();
38 auto last = devicesSecDetails.end();
39 for (; first != last; ++first) {
40 if( m(addr, name, *first) ) {
41 return &(*first);
42 }
43 }
44 return nullptr;
45 }
46 Entry* get(const EUI48Sub& addrSub, const std::string& name, AddressSubNameEntryMatchFunc m) noexcept {
47 auto first = devicesSecDetails.begin();
48 auto last = devicesSecDetails.end();
49 for (; first != last; ++first) {
50 if( m(addrSub, name, *first) ) {
51 return &(*first);
52 }
53 }
54 return nullptr;
55 }
56 Entry* get(const std::string& name, NameEntryMatchFunc m) noexcept {
57 auto first = devicesSecDetails.begin();
58 auto last = devicesSecDetails.end();
59 for (; first != last; ++first) {
60 if( m(name, *first) ) {
61 return &(*first);
62 }
63 }
64 return nullptr;
65 }
66
68 return devicesSecDetails;
69 }
70
71 Entry* getOrCreate(const std::string& addrOrNameSub) noexcept {
72 EUI48Sub addr1;
73 std::string errmsg;
74 Entry* sec = nullptr;
75 if( EUI48Sub::scanEUI48Sub(addrOrNameSub, addr1, errmsg) ) {
76 sec = getEqual(addr1, "");
77 if( nullptr == sec ) {
78 Entry& r = devicesSecDetails.emplace_back( addr1 );
79 sec = &r;
80 }
81 } else {
82 sec = getEqual(addrOrNameSub);
83 if( nullptr == sec ) {
84 Entry& r = devicesSecDetails.emplace_back( addrOrNameSub );
85 sec = &r;
86 }
87 }
88 return sec;
89 }
90 void clear() noexcept {
91 devicesSecDetails.clear();
92 }
93 std::string allToString() noexcept {
94 std::string res;
95 int i=0;
96 for(auto iter = devicesSecDetails.cbegin(); iter != devicesSecDetails.cend(); ++iter, ++i) {
97 const Entry& sec = *iter;
98 if( 0 < i ) {
99 res += ", ";
100 }
101 res += sec.toString();
102 }
103 return res;
104 }
105
106} // namespace direct_bt::BTSecurityRegistry
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:148
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...
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
jau::darray< Entry > & getEntries() noexcept
Returns the reference of the current list of Entry, not a copy.
Application toolkit providing BT security setup and its device association on a pattern matching basi...
static jau::darray< Entry > devicesSecDetails
std::string toString() const noexcept
A 48 bit EUI-48 sub-identifier, see EUI48.
Definition: eui48.hpp:51
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
Definition: eui48.hpp:324