aboutsummaryrefslogtreecommitdiffstats
path: root/api/direct_bt/BTSecurityRegistry.hpp
blob: d3e507b9784517f1e3e2d7fa7659e2fc918abd89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef DBT_SEC_SETTINGS_HPP_
#define DBT_SEC_SETTINGS_HPP_

#include <string>
#include <cstdio>

#include <direct_bt/DirectBT.hpp>

namespace direct_bt {

    /**
     * Application toolkit providing BT security setup and its device association
     * on a pattern matching basis, i.e. EUI48Sub or name-sub.
     */
    namespace BTSecurityRegistry {

        struct Entry {
            static constexpr int NO_PASSKEY = -1;

            EUI48Sub addrSub;
            std::string nameSub;

            BTSecurityLevel sec_level { BTSecurityLevel::UNSET };
            SMPIOCapability io_cap { SMPIOCapability::UNSET };
            SMPIOCapability io_cap_auto { SMPIOCapability::UNSET };
            int passkey = NO_PASSKEY;

            Entry(const EUI48Sub& addrSub_)
            : addrSub(addrSub_), nameSub() {}

            Entry(const std::string& nameSub_)
            : addrSub(EUI48Sub::ALL_DEVICE), nameSub(nameSub_) {}

            constexpr bool isSecLevelOrIOCapSet() const noexcept {
                return SMPIOCapability::UNSET != io_cap ||  BTSecurityLevel::UNSET != sec_level;
            }
            constexpr const BTSecurityLevel& getSecLevel() const noexcept { return sec_level; }
            constexpr const SMPIOCapability& getIOCap() const noexcept { return io_cap; }

            constexpr bool isSecurityAutoEnabled() const noexcept {
                return SMPIOCapability::UNSET != io_cap_auto;
            }
            constexpr const SMPIOCapability& getSecurityAutoIOCap() const noexcept { return io_cap_auto; }

            constexpr int getPairingPasskey() const noexcept { return passkey; }

            constexpr bool getPairingNumericComparison() const noexcept { return true; }

            std::string toString() const noexcept {
                const std::string id = addrSub == EUI48Sub::ALL_DEVICE ? "'"+nameSub+"'" : addrSub.toString();
                return "BTSecurityDetail["+id+", lvl "+
                        to_string(sec_level)+
                        ", io "+to_string(io_cap)+
                        ", auto-io "+to_string(io_cap_auto)+
                        ", passkey "+std::to_string(passkey)+"]";
            }
        };

        /**
         * Function for user defined EUI48 address and name BTSecurityRegistry::Entry matching criteria and algorithm.
         * <p>
         * Return {@code true} if the given {@code address} or {@code name} matches
         * with the BTSecurityRegistry::Entry.
         * </p>
         *
         * @param address EUI48 address
         * @param name optional name, maybe empty
         * @param e Entry entry
         */
        typedef bool (*AddressNameEntryMatchFunc)(const EUI48& address, const std::string& name, const Entry& e);

        /**
         * Function for user defined EUI48Sub addressSub and name BTSecurityRegistry::Entry matching criteria and algorithm.
         * <p>
         * Return {@code true} if the given {@code addressSub} or {@code name} matches
         * with the BTSecurityRegistry::Entry.
         * </p>
         *
         * @param addressSub EUI48Sub address
         * @param name optional name, maybe empty
         * @param e Entry entry
         */
        typedef bool (*AddressSubNameEntryMatchFunc)(const EUI48Sub& addressSub, const std::string& name, const Entry& e);

        /**
         * Function for user defined std::string name BTSecurityRegistry::Entry matching criteria and algorithm.
         * <p>
         * Return {@code true} if the given {@code name} matches
         * with the BTSecurityRegistry::Entry.
         * </p>
         *
         * @param name
         * @param e Entry entry
         */
        typedef bool (*NameEntryMatchFunc)(const std::string& name, const Entry& e);

        /**
         * Returns a matching BTSecurityRegistry::Entry with the given {@code addr} and/or {@code name}.
         * <p>
         * Matching criteria and algorithm is defined by the given AddressNameEntryMatchFunc.
         * </p>
         */
        Entry* get(const EUI48& addr, const std::string& name, AddressNameEntryMatchFunc m) noexcept;

        /**
         * Returns a matching BTSecurityRegistry::Entry with the given {@code addrSub} and/or {@code name}.
         * <p>
         * Matching criteria and algorithm is defined by the given AddressSubNameEntryMatchFunc.
         * </p>
         */
        Entry* get(const EUI48Sub& addrSub, const std::string& name, AddressSubNameEntryMatchFunc m) noexcept;

        /**
         * Returns a matching BTSecurityRegistry::Entry with the given {@code name}.
         * <p>
         * Matching criteria and algorithm is defined by the given NameEntryMatchFunc.
         * </p>
         */
        Entry* get(const std::string& name, NameEntryMatchFunc m) noexcept;

        /**
         * Returns a matching Entry,
         * - which Entry::addrSub is set and the given {@code addr} starts with Entry::addrSub, or
         * - which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
         *
         * Otherwise {@code null} is returned.
         */
        inline Entry* getStartOf(const EUI48& addr, const std::string& name) noexcept {
            return get(addr, name, [](const EUI48& a, const std::string& n, const Entry& e)->bool {
               return ( e.addrSub.length > 0 && 0 == a.indexOf(e.addrSub) ) ||
                      ( e.nameSub.length() > 0 && 0 == n.find(e.nameSub) );
            });
        }
        /**
         * Returns a matching Entry,
         * - which Entry::addrSub is set and the given {@code addrSub} starts with Entry::addrSub, or
         * - which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
         *
         * Otherwise {@code null} is returned.
         */
        inline Entry* getStartOf(const EUI48Sub& addrSub, const std::string& name) noexcept {
            return get(addrSub, name, [](const EUI48Sub& as, const std::string& n, const Entry& e)->bool {
               return ( e.addrSub.length > 0 && 0 == as.indexOf(e.addrSub) ) ||
                      ( e.nameSub.length() > 0 && 0 == n.find(e.nameSub) );
            });
        }
        /**
         * Returns a matching Entry,
         * which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
         *
         * Otherwise {@code null} is returned.
         */
        inline Entry* getStartOf(const std::string& name) noexcept {
            return get(name, [](const std::string& n, const Entry& e)->bool {
               return e.nameSub.length() > 0 && 0 == n.find(e.nameSub);
            });
        }

        /**
         * Returns a matching Entry,
         * - which Entry::addrSub is set and the given {@code addrSub} starts with Entry::addrSub, or
         * - which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
         *
         * Otherwise {@code null} is returned.
         */
        inline Entry* getEqual(const EUI48Sub& addrSub, const std::string& name) noexcept {
            return get(addrSub, name, [](const EUI48Sub& as, const std::string& n, const Entry& e)->bool {
               return ( e.addrSub.length > 0 && as == e.addrSub ) ||
                      ( e.nameSub.length() > 0 && n == e.nameSub );
            });
        }
        /**
         * Returns a matching Entry,
         * which Entry::nameSub is set and the given {@code name} starts with Entry::nameSub.
         *
         * Otherwise {@code null} is returned.
         */
        inline Entry* getEqual(const std::string& name) noexcept {
            return get(name, [](const std::string& n, const Entry& e)->bool {
               return e.nameSub.length() > 0 && n == e.nameSub;
            });
        }

        /**
         * Returns the reference of the current list of Entry, not a copy.
         */
        jau::darray<Entry>& getEntries() noexcept;

        /**
         * Determines whether the given {@code addrOrNameSub} is a EUI48Sub or just a {@code name}
         * and retrieves an entry. If no entry exists, creates a new entry.
         * <p>
         * Implementation uses getEqual() to find a pre-existing entry.
         * </p>
         * @param addrOrNameSub either a EUI48Sub or just a name
         * @return new or existing instance
         */
        Entry* getOrCreate(const std::string& addrOrNameSub) noexcept;

        /**
         * Clears internal list
         */
        void clear() noexcept;

        std::string allToString() noexcept;

    } // namespace BTSecurityRegistry

} // namespace direct_bt

#endif /* DBT_SEC_SETTINGS_HPP_ */