aboutsummaryrefslogtreecommitdiffstats
path: root/trial/direct_bt/test_client_server00.cpp
blob: 67152786da755d04b3ce519a574bf869020061a5 (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
/**
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2022 Gothel Software e.K.
 *
 * 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.
 */

#include <iostream>
#include <cassert>
#include <cinttypes>
#include <cstring>

#include <jau/test/catch2_ext.hpp>

#include "dbt_base_client_server.hpp"

#include "dbt_server01.hpp"

using namespace direct_bt;

// Singleton test framework, alive until test program ends
static BaseDBTClientServer& base_test_framework = BaseDBTClientServer::get( false /* btmanager_hold_and_close */ );

/**
 * Testing BTManager bring up:
 * - test loading native libraries
 * - test that at least one adapter are present
 * - validating basic default adapter status
 */
TEST_CASE( "BTManager Bringup Trial 00.1", "[trial][BTManager][bringup]" ) {
    base_test_framework.setupTest( 5_s );

    jau::fprintf_td(stderr, "Direct-BT Native Version %s (API %s)\n", DIRECT_BT_VERSION, DIRECT_BT_VERSION_API);

    std::shared_ptr<BTManager> manager = BTManager::get();
    jau::darray<BTAdapterRef> adapters = manager->getAdapters();
    {
        jau::fprintf_td(stderr, "Adapter: Count %u\n", adapters.size());

        for(jau::nsize_t i=0; i<adapters.size(); i++) {
            jau::fprintf_td(stderr, "%u: %s\n", i, adapters[i]->toString().c_str());
        }
        REQUIRE( adapters.size() >= 1 );
    }

    jau::fprintf_td(stderr, "Adapter: Status Checks\n");
    for(const BTAdapterRef& a : adapters) {
        REQUIRE( false == a->isInitialized() );
        REQUIRE( false == a->isPowered() );
        REQUIRE( BTRole::Master == a->getRole() ); // default role
        REQUIRE( 4 <= a->getBTMajorVersion() );
    }

    base_test_framework.cleanupTest();
}

/**
 * Testing BTManager bring up:
 * - test loading native libraries
 * - test that at least one adapter are present
 * - validating basic default adapter status
 */
TEST_CASE( "Server StartStop and SwitchRole Trial 00.2", "[trial][startstop][switchrole]" ) {
    base_test_framework.setupTest( 5_s );

    std::shared_ptr<BTManager> manager = BTManager::get();
    {
        jau::darray<BTAdapterRef> adapters = manager->getAdapters();
        jau::fprintf_td(stderr, "Adapter: Count %u\n", adapters.size());

        for(jau::nsize_t i=0; i<adapters.size(); i++) {
            jau::fprintf_td(stderr, "%u: %s\n", i, adapters[i]->toString().c_str());
        }
        REQUIRE( adapters.size() >= 1 );
    }
    REQUIRE( manager->getAdapterCount() >= 1 );

    const std::string serverName = "TestDBTCS00-S-T10";
    std::shared_ptr<DBTServer01> server = std::make_shared<DBTServer01>(serverName, EUI48::ALL_DEVICE, BTMode::DUAL,
                                                                        true /* SC */, BTSecurityLevel::NONE, false /* do_disconnect_ */);
    server->setProtocolSessionsLeft(1);

    ChangedAdapterSetCallback myChangedAdapterSetFunc = DBTEndpoint::initChangedAdapterSetListener(manager, { server });

    //
    // Server start
    //
    DBTEndpoint::checkInitializedState(server);
    DBTServerTest::startAdvertising(server, false /* current_exp_advertising_state */, "test10_startAdvertising");

    //
    // Server stop
    //
    DBTServerTest::stop(server, "test10_stop");

    //
    // Now reuse adapter for client mode -> Start discovery + Stop Discovery
    //
    {
        BTAdapterRef adapter = server->getAdapter();
        { // if( false ) {
            adapter->removeAllStatusListener();
        }

        DBTEndpoint::startDiscovery(adapter, false /* current_exp_discovering_state */);

        DBTEndpoint::stopDiscovery(adapter, true /* current_exp_discovering_state */);
    }

    REQUIRE( 1 == manager->removeChangedAdapterSetCallback(myChangedAdapterSetFunc) );

    base_test_framework.cleanupTest();
}

TEST_CASE( "BTManager::close() Trial 00.X", "[trial][btmanager_shutdown]" ) {
    base_test_framework.closeBTManager();
}