Direct-BT  2.3.1
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 
26 #include <BTSecurityRegistry.hpp>
27 
28 #include <jau/darray.hpp>
29 
30 using 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
direct_bt::BTSecurityRegistry::getOrCreate
Entry * getOrCreate(const std::string &addrOrNameSub) noexcept
Determines whether the given.
Definition: BTSecurityRegistry.cpp:71
darray.hpp
direct_bt::BTSecurityRegistry::allToString
std::string allToString() noexcept
Definition: BTSecurityRegistry.cpp:93
direct_bt::BTSecurityRegistry::NameEntryMatchFunc
bool(* NameEntryMatchFunc)(const std::string &name, const Entry &e)
Function for user defined std::string name BTSecurityRegistry::Entry matching criteria and algorithm.
Definition: BTSecurityRegistry.hpp:120
direct_bt::BTSecurityRegistry
Application toolkit providing BT security setup and its device association on a pattern matching basi...
Definition: BTSecurityRegistry.hpp:40
direct_bt::BTSecurityRegistry::devicesSecDetails
static jau::darray< Entry > devicesSecDetails
Definition: BTSecurityRegistry.cpp:34
direct_bt::BTSecurityRegistry::Entry
Definition: BTSecurityRegistry.hpp:42
direct_bt
Definition: ATTPDUTypes.hpp:171
direct_bt::EUI48Sub::scanEUI48Sub
static bool scanEUI48Sub(const std::string &str, EUI48Sub &dest, std::string &errmsg)
Fills given EUI48Sub instance via given string representation.
Definition: BTTypes0.cpp:186
direct_bt::BTSecurityRegistry::Entry::toString
std::string toString() const noexcept
Definition: BTSecurityRegistry.hpp:74
direct_bt::EUI48Sub
A 48 bit EUI-48 sub-identifier, see EUI48.
Definition: BTAddress.hpp:154
direct_bt::BTSecurityRegistry::AddressNameEntryMatchFunc
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...
Definition: BTSecurityRegistry.hpp:95
jau::darray
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:102
direct_bt::BTSecurityRegistry::getEntries
jau::darray< Entry > & getEntries() noexcept
Returns the reference of the current list of Entry, not a copy.
Definition: BTSecurityRegistry.cpp:67
direct_bt::BTSecurityRegistry::getEqual
Entry * getEqual(const EUI48Sub &addrSub, const std::string &name) noexcept
Returns a matching Entry,.
Definition: BTSecurityRegistry.hpp:191
direct_bt::BTSecurityRegistry::get
Entry * get(const EUI48 &addr, const std::string &name, AddressNameEntryMatchFunc m) noexcept
Returns a matching BTSecurityRegistry::Entry with the given.
Definition: BTSecurityRegistry.cpp:36
direct_bt::BTSecurityRegistry::AddressSubNameEntryMatchFunc
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...
Definition: BTSecurityRegistry.hpp:108
BTSecurityRegistry.hpp
direct_bt::EUI48
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
Definition: BTAddress.hpp:388
direct_bt::BTSecurityRegistry::clear
void clear() noexcept
Clears internal list.
Definition: BTSecurityRegistry.cpp:90