Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
dbt_base_client_server.hpp
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef DBT_BASE_CLIENY_SERVER_HPP_
26#define DBT_BASE_CLIENY_SERVER_HPP_
27
28#include "dbt_utils.hpp"
29
30#include "dbt_constants.hpp"
31
33
34#include <jau/simple_timer.hpp>
35
36using namespace direct_bt;
37
39 private:
40 static constexpr const bool debug = false;
41
42 BTManagerRef manager;
43
44 jau::fraction_i64 test_timeout = 0_s;
45
46 jau::simple_timer timeout_timer = jau::simple_timer("DBTTrial-Timeout", 1_s /* shutdown timeout */);
47 jau::sc_atomic_bool timedout = false;
48
49 jau::fraction_i64 timeout_func(jau::simple_timer& timer) {
50 if( !timer.shall_stop() ) {
51 fprintf(stderr, "\n***** DBTTrial Error: Timeout %s sec -> abort *****\n\n", test_timeout.to_string(true).c_str());
52 timedout = true;
53 }
54 return 0_s;
55 }
56
57 BaseDBTClientServer(const bool btmanager_hold_and_close) noexcept {
58 if( debug ) {
59 setenv("direct_bt.debug", "true", 1 /* overwrite */);
60 // setenv("direct_bt.debug", "true,gatt.data", 1 /* overwrite */);
61 }
62
65
66 if( btmanager_hold_and_close ) {
67 manager = BTManager::get();
68 }
69 }
70
71 void close() {
72 timeout_timer.stop();
73 if( nullptr != manager ) {
74 manager->close();
75 }
76 }
77
78 public:
79 static BaseDBTClientServer& get(const bool btmanager_hold_and_close = true) {
80 MgmtEnv::get(); // initialize the environment first for 'direct_bt' key
81 static BaseDBTClientServer s(btmanager_hold_and_close);
82 return s;
83 }
84
85 jau::fraction_i64 get_timeout_value() const { return test_timeout; }
86
87 bool is_timedout() const noexcept { return timedout; }
88
89 /**
90 * Ensure
91 * - all adapter are powered off
92 * - manager being shutdown
93 */
94 ~BaseDBTClientServer() noexcept { close(); }
95
96 private:
97 void resetStates() {
98 std::shared_ptr<BTManager> mngr = BTManager::get();
99 jau::darray<BTAdapterRef> adapters = mngr->getAdapters();
100 for(const BTAdapterRef& a : adapters) {
101 a->removeAllStatusListener();
102 a->stopAdvertising();
103 a->stopDiscovery();
104 REQUIRE( a->setPowered(false) );
105 }
106 mngr->removeAllChangedAdapterSetCallbacks();
110 }
111
112 public:
113 /**
114 * Ensure
115 * - all adapter are powered off
116 */
117 void setupTest(const jau::fraction_i64 timeout = 0_s) {
118 timeout_timer.stop();
119 test_timeout = timeout;
120 resetStates();
121 if( !timeout.is_zero() ) {
122 timeout_timer.start(timeout, jau::bind_member(this, &BaseDBTClientServer::timeout_func));
123 }
124 }
125
126 /**
127 * Ensure
128 * - remove all status listener from all adapter
129 * - all adapter are powered off
130 * - clear BTDeviceRegistry
131 * - clear BTSecurityRegistry
132 */
133 void cleanupTest() {
134 timeout_timer.stop();
135 test_timeout = 0_s;
136 resetStates();
137 }
138
140 std::shared_ptr<BTManager> mngr = BTManager::get();
141 mngr->close();
142 }
143};
144
145#endif /* DBT_BASE_CLIENY_SERVER_HPP_ */
~BaseDBTClientServer() noexcept
Ensure.
bool is_timedout() const noexcept
void setupTest(const jau::fraction_i64 timeout=0_s)
Ensure.
jau::fraction_i64 get_timeout_value() const
static BaseDBTClientServer & get(const bool btmanager_hold_and_close=true)
static bool mkdirKeyFolder()
Definition: dbt_utils.hpp:41
static bool rmKeyFolder()
Definition: dbt_utils.hpp:51
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:148
std::string to_string(const bool show_double=false) const noexcept
Returns a string representation of this fraction.
A simple timer for timeout and interval applications, using one dedicated service_runner thread per i...
bool start(const fraction_i64 &duration_, Timer_func tofunc) noexcept
Start the timer with given user Timer_func function and initial duration.
bool stop() noexcept
Stop timer, see service_runner::stop()
bool shall_stop() const noexcept
Returns true if timer shall stop.
std::shared_ptr< BTManager > BTManagerRef
Definition: BTAdapter.hpp:61
void clear() noexcept
Clears internal list.
std::shared_ptr< BTAdapter > BTAdapterRef
Definition: BTAdapter.hpp:1354
Entry * get(const EUI48 &addr, const std::string &name, AddressNameEntryMatchFunc m) noexcept
Returns a matching BTSecurityRegistry::Entry with the given addr and/or name.
jau::function< R(A...)> bind_member(C1 *base, R(C0::*mfunc)(A...)) noexcept
Bind given class instance and non-void member function to an anonymous function using func_member_tar...
void clearProcessedDevices() noexcept
Clears internal list.
void clearWaitForDevices() noexcept
Clears internal list.