Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
test_client_server00.cpp
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#include <iostream>
26#include <cassert>
27#include <cinttypes>
28#include <cstring>
29
31
33
34#include "dbt_server01.hpp"
35
36#include <jau/os/os_support.hpp>
37
38using namespace direct_bt;
39
40// Singleton test framework, alive until test program ends
41static BaseDBTClientServer& base_test_framework = BaseDBTClientServer::get( false /* btmanager_hold_and_close */ );
42
43/**
44 * Testing BTManager bring up:
45 * - test loading native libraries
46 * - test that at least one adapter are present
47 * - validating basic default adapter status
48 */
49TEST_CASE( "BTManager Bringup Trial 00.1", "[trial][BTManager][bringup]" ) {
51
52 jau::fprintf_td(stderr, "Direct-BT Native Version %s (API %s)\n", DIRECT_BT_VERSION, DIRECT_BT_VERSION_API);
53 jau::fprintf_td(stderr, "%s\n", jau::os::get_platform_info().c_str());
54
55 std::shared_ptr<BTManager> manager = BTManager::get();
56 jau::darray<BTAdapterRef> adapters = manager->getAdapters();
57 {
58 jau::fprintf_td(stderr, "Adapter: Count %u\n", adapters.size());
59
60 for(jau::nsize_t i=0; i<adapters.size(); i++) {
61 jau::fprintf_td(stderr, "%u: %s\n", i, adapters[i]->toString().c_str());
62 }
63 REQUIRE( adapters.size() >= 1 );
64 }
65
66 jau::fprintf_td(stderr, "Adapter: Status Checks\n");
67 for(const BTAdapterRef& a : adapters) {
68 REQUIRE( false == a->isInitialized() );
69 REQUIRE( false == a->isPowered() );
70 REQUIRE( BTRole::Master == a->getRole() ); // default role
71 REQUIRE( 4 <= a->getBTMajorVersion() );
72 }
73
75}
76
77/**
78 * Testing BTManager bring up:
79 * - test loading native libraries
80 * - test that at least one adapter are present
81 * - validating basic default adapter status
82 */
83TEST_CASE( "Server StartStop and SwitchRole Trial 00.2", "[trial][startstop][switchrole]" ) {
85
86 std::shared_ptr<BTManager> manager = BTManager::get();
87 {
88 jau::darray<BTAdapterRef> adapters = manager->getAdapters();
89 jau::fprintf_td(stderr, "Adapter: Count %u\n", adapters.size());
90
91 for(jau::nsize_t i=0; i<adapters.size(); i++) {
92 jau::fprintf_td(stderr, "%u: %s\n", i, adapters[i]->toString().c_str());
93 }
94 REQUIRE( adapters.size() >= 1 );
95 }
96 REQUIRE( manager->getAdapterCount() >= 1 );
97
98 const std::string serverName = "TestDBTCS00-S-T10";
99 std::shared_ptr<DBTServer01> server = std::make_shared<DBTServer01>(serverName, EUI48::ALL_DEVICE, BTMode::DUAL,
100 true /* SC */, BTSecurityLevel::NONE, false /* do_disconnect_ */);
101 server->setProtocolSessionsLeft(1);
102
104
105 //
106 // Server start
107 //
109 DBTServerTest::startAdvertising(server, false /* current_exp_advertising_state */, "test10_startAdvertising");
110
111 //
112 // Server stop
113 //
114 DBTServerTest::stop(server, "test10_stop");
115
116 //
117 // Now reuse adapter for client mode -> Start discovery + Stop Discovery
118 //
119 {
120 BTAdapterRef adapter = server->getAdapter();
121 { // if( false ) {
122 adapter->removeAllStatusListener();
123 }
124
125 DBTEndpoint::startDiscovery(adapter, false /* current_exp_discovering_state */);
126
127 DBTEndpoint::stopDiscovery(adapter, true /* current_exp_discovering_state */);
128 }
129
130 REQUIRE( 1 == manager->removeChangedAdapterSetCallback(myChangedAdapterSetFunc) );
131
133}
134
135TEST_CASE( "BTManager::close() Trial 00.X", "[trial][btmanager_shutdown]" ) {
137}
138
void setupTest(const jau::fraction_i64 timeout=0_s)
Ensure.
static BaseDBTClientServer & get(const bool btmanager_hold_and_close=true)
static void checkInitializedState(const DBTEndpointRef &endp)
static void stopDiscovery(const BTAdapterRef &adapter, const bool current_exp_discovering_state)
static ChangedAdapterSetCallback initChangedAdapterSetListener(const BTManagerRef &manager, std::vector< DBTEndpointRef > endpts)
static void startDiscovery(const BTAdapterRef &adapter, const bool current_exp_discovering_state)
virtual HCIStatusCode startAdvertising(const std::string &msg)=0
static void stop(const DBTServerTestRef &server, const std::string &msg)
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:148
constexpr size_type size() const noexcept
Like std::vector::size().
Definition: darray.hpp:763
Class template jau::function is a general-purpose static-polymorphic function wrapper.
static void myChangedAdapterSetFunc(const bool added, std::shared_ptr< BTAdapter > &adapter)
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.
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
Definition: int_types.hpp:53
std::string get_platform_info(std::string &sb) noexcept
Definition: os_support.cpp:207
int fprintf_td(const uint64_t elapsed_ms, FILE *stream, const char *format,...) noexcept
Convenient fprintf() invocation, prepending the given elapsed_ms timestamp.
Definition: debug.cpp:270
static BaseDBTClientServer & base_test_framework
TEST_CASE("BTManager Bringup Trial 00.1", "[trial][BTManager][bringup]")
Testing BTManager bring up: