Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBGattServer.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2021 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 <cstring>
27#include <string>
28#include <memory>
29#include <cstdint>
30#include <vector>
31#include <cstdio>
32
33#include <algorithm>
34
35#include <jau/debug.hpp>
36
37#include "DBGattServer.hpp"
38
39using namespace direct_bt;
40
41bool DBGattDesc::setValue(const uint8_t* source, const jau::nsize_t source_len, const jau::nsize_t dest_pos) noexcept {
42 if( hasVariableLength() ) {
43 if( value.capacity() < dest_pos + source_len ) {
44 return false;
45 }
46 if( value.size() != dest_pos + source_len ) {
47 value.resize( dest_pos + source_len );
48 }
49 } else {
50 if( value.size() < dest_pos + source_len ) {
51 return false;
52 }
53 }
54 value.put_bytes_nc(dest_pos, source, source_len);
55 return true;
56}
57
58bool DBGattChar::setValue(const uint8_t* source, const jau::nsize_t source_len, const jau::nsize_t dest_pos) noexcept {
59 if( hasVariableLength() ) {
60 if( value.capacity() < dest_pos + source_len ) {
61 return false;
62 }
63 if( value.size() != dest_pos + source_len ) {
64 value.resize( dest_pos + source_len );
65 }
66 } else {
67 if( value.size() < dest_pos + source_len ) {
68 return false;
69 }
70 }
71 value.put_bytes_nc(dest_pos, source, source_len);
72 return true;
73}
74
75std::string direct_bt::to_string(const DBGattServer::Mode m) noexcept {
76 switch(m) {
77 case DBGattServer::Mode::NOP: return "nop";
78 case DBGattServer::Mode::DB: return "db";
79 case DBGattServer::Mode::FWD: return "fwd";
80 }
81 return "Unknown mode";
82}
83
85 [](const DBGattServer::ListenerRef &a, const DBGattServer::ListenerRef &b) -> bool { return *a == *b; };
86
87bool DBGattServer::addListener(const ListenerRef& l) {
88 if( nullptr == l ) {
89 throw jau::IllegalArgumentException("Listener ref is null", E_FILE_LINE);
90 }
91 return listenerList.push_back_unique(l, _listenerRefEqComparator);
92}
93
95 if( nullptr == l ) {
96 ERR_PRINT("Listener ref is null");
97 return false;
98 }
99 const auto count = listenerList.erase_matching(l, false /* all_matching */, _listenerRefEqComparator);
100 return count > 0;
101}
102
103std::string DBGattServer::toString() const noexcept {
104 return "DBSrv[mode "+to_string(mode)+", max mtu "+std::to_string(max_att_mtu)+", "+std::to_string(services.size())+" services, "+javaObjectToString()+"]";
105}
106
static jau::cow_darray< DBGattServer::ListenerRef >::equal_comparator _listenerRefEqComparator
#define E_FILE_LINE
std::shared_ptr< Listener > ListenerRef
Mode
Operating mode of a DBGattServer instance.
bool removeListener(const ListenerRef &l)
std::string toString() const noexcept override
Implementation of a Copy-On-Write (CoW) using jau::darray as the underlying storage,...
Definition: cow_darray.hpp:127
constexpr_atomic bool push_back_unique(const value_type &x, equal_comparator comparator)
Like std::vector::push_back(), but only if the newly added element does not yet exist.
constexpr_atomic size_type erase_matching(const value_type &x, const bool all_matching, equal_comparator comparator)
Erase either the first matching element or all matching elements.
constexpr size_type size() const noexcept
Like std::vector::size().
Definition: darray.hpp:763
#define ERR_PRINT(...)
Use for unconditional error messages, prefix '[elapsed_time] Error @ FILE:LINE FUNC: '.
Definition: debug.hpp:109
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
std::string to_string(const DiscoveryPolicy v) noexcept
Definition: BTAdapter.cpp:58
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
Definition: int_types.hpp:53