summaryrefslogtreecommitdiffstats
path: root/trial/direct_bt/dbt_client_server1x.hpp
blob: 4d6b4523b6902ca5bf9c16bba82916968634acd4 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
 * 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 "dbt_base_client_server.hpp"

#include "dbt_server01.hpp"
#include "dbt_client01.hpp"

using namespace direct_bt;

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

/**
 * Testing a full Bluetooth server and client lifecycle of operations, requiring two BT adapter:
 * - start server advertising
 * - start client discovery and connect to server when discovered
 * - client/server processing of connection when ready
 * - client disconnect
 * - server stop advertising
 * - security-level: NONE, ENC_ONLY freshly-paired and ENC_ONLY pre-paired
 * - reuse server-adapter for client-mode discovery (just toggle on/off)
 */
class DBTClientServer1x {
  private:
    // timeout check: timeout_value < test_duration + timeout_preempt_diff; // let's timeout here before our timeout timer
    static constexpr const fraction_i64 timeout_preempt_diff = 500_ms;

    std::mutex mtx_sync;
    BTDeviceRef lastCompletedDevice = nullptr;
    PairingMode lastCompletedDevicePairingMode = PairingMode::NONE;
    BTSecurityLevel lastCompletedDeviceSecurityLevel = BTSecurityLevel::NONE;
    EInfoReport lastCompletedDeviceEIR;
    int client_power_down_count = 0;
    int client_power_up_count = 0;
    bool client_reset_at_ready = false;
    bool server_reset_at_ready = false;
    bool client_reset_test = false;
    bool server_reset_test = false;

  public:
    void set_client_reset_at_ready(const bool v) noexcept { client_reset_at_ready = v; client_reset_test = v; }
    void set_server_reset_at_ready(const bool v) noexcept { server_reset_at_ready = v; server_reset_test = v; }

    void test8x_fullCycle(const std::string& suffix, const int protocolSessionCount, const bool server_client_order,
                          const bool serverSC, const BTSecurityLevel secLevelServer, const ExpectedPairing serverExpPairing,
                          const BTSecurityLevel secLevelClient, const ExpectedPairing clientExpPairing)
    {
        std::shared_ptr<DBTServer01> server = std::make_shared<DBTServer01>("S-"+suffix, EUI48::ALL_DEVICE, BTMode::DUAL,
                                                                            serverSC, secLevelServer);
        std::shared_ptr<DBTClient01> client = std::make_shared<DBTClient01>("C-"+suffix, EUI48::ALL_DEVICE, BTMode::DUAL);

        server->setProtocolSessionsLeft( protocolSessionCount );

        client->setProtocolSessionsLeft( protocolSessionCount );
        client->setDisconnectDevice( true ); // default, auto-disconnect after work is done
        client->setRemoveDevice( false ); // default, test side-effects
        client->setDiscoveryPolicy( DiscoveryPolicy::PAUSE_CONNECTED_UNTIL_DISCONNECTED );

        test8x_fullCycle(suffix,
                         DBTConstants::max_connections_per_session, true /* expSuccess */,
                         server_client_order,
                         server, secLevelServer, serverExpPairing,
                         client, secLevelClient, clientExpPairing);
    }

    void test8x_fullCycle(const std::string& suffix,
                          const int max_connections_per_session, const bool expSuccess,
                          const bool server_client_order,
                          std::shared_ptr<DBTServerTest> server, const BTSecurityLevel secLevelServer, const ExpectedPairing serverExpPairing,
                          std::shared_ptr<DBTClientTest> client, const BTSecurityLevel secLevelClient, const ExpectedPairing clientExpPairing)
    {
        (void)secLevelServer;
        (void)serverExpPairing;

        const int protocolSessionCount = std::min(server->getProtocolSessionsLeft(), client->getProtocolSessionsLeft());
        const jau::fraction_timespec t0 = jau::getMonotonicTime();

        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() >= 2 );
        }

        ChangedAdapterSetCallback myChangedAdapterSetFunc = DBTEndpoint::initChangedAdapterSetListener(manager,
                server_client_order ? std::vector<DBTEndpointRef>{ server, client } : std::vector<DBTEndpointRef>{ client, server } );

        const std::string serverName = server->getName();
        BTDeviceRegistry::addToWaitForDevices( serverName );
        {
            BTSecurityRegistry::Entry * sec = BTSecurityRegistry::getOrCreate(serverName);
            sec->sec_level = secLevelClient;
        }

        {
            const std::lock_guard<std::mutex> lock(mtx_sync); // RAII-style acquire and relinquish via destructor
            lastCompletedDevice = nullptr;
            lastCompletedDevicePairingMode = PairingMode::NONE;
            lastCompletedDeviceSecurityLevel = BTSecurityLevel::NONE;
            lastCompletedDeviceEIR.clear();
        }
        class MyAdapterStatusListener : public AdapterStatusListener {
          private:
            DBTClientServer1x& parent;
          public:
            MyAdapterStatusListener(DBTClientServer1x& p) : parent(p) {}

            void adapterSettingsChanged(BTAdapter &a, const AdapterSetting oldmask, const AdapterSetting newmask,
                                        const AdapterSetting changedmask, const uint64_t timestamp) override {
                const bool initialSetting = AdapterSetting::NONE == oldmask;
                if( !initialSetting && isAdapterSettingBitSet(changedmask, AdapterSetting::POWERED) ) {
                    const std::lock_guard<std::mutex> lock(parent.mtx_sync); // RAII-style acquire and relinquish via destructor
                    if( isAdapterSettingBitSet(newmask, AdapterSetting::POWERED) ) {
                        ++parent.client_power_up_count;
                    } else {
                        ++parent.client_power_down_count;
                    }
                }
                (void)timestamp;
                (void)a;
            }

            void deviceReady(BTDeviceRef device, const uint64_t timestamp) override {
                (void)timestamp;
                const std::lock_guard<std::mutex> lock(parent.mtx_sync); // RAII-style acquire and relinquish via destructor
                parent.lastCompletedDevice = device;
                parent.lastCompletedDevicePairingMode = device->getPairingMode();
                parent.lastCompletedDeviceSecurityLevel = device->getConnSecurityLevel();
                parent.lastCompletedDeviceEIR = *device->getEIR();
                fprintf_td(stderr, "XXXXXX Client Ready: %s\n", device->toString(true).c_str());
                if( parent.client_reset_at_ready ) {
                    parent.client_reset_at_ready = false;
                    fprintf_td(stderr, "XXXXXX Client Reset.0: %s\n", device->toString(true).c_str());
                    const HCIStatusCode rr = device->getAdapter().reset();
                    fprintf_td(stderr, "XXXXXX Client Reset.X: %s: %s\n", direct_bt::to_string(rr).c_str(), device->toString(true).c_str());
                }
            }

            std::string toString() const noexcept override { return "DBTClientServer1x::Client"; }
        };
        std::shared_ptr<AdapterStatusListener> clientAdapterStatusListener = std::make_shared<MyAdapterStatusListener>(*this);
        REQUIRE( true == client->getAdapter()->addStatusListener(clientAdapterStatusListener) );

        //
        // Server start
        //
        DBTEndpoint::checkInitializedState(server);
        DBTServerTest::startAdvertising(server, false /* current_exp_advertising_state */, "test"+suffix+"_startAdvertising");

        //
        // Client start
        //
        DBTEndpoint::checkInitializedState(client);
        DBTClientTest::startDiscovery(client, false /* current_exp_discovering_state */, "test"+suffix+"_startDiscovery");

        BaseDBTClientServer& framework = BaseDBTClientServer::get();
        const jau::fraction_i64 timeout_value = framework.get_timeout_value();
        jau::fraction_i64 test_duration = 0_s;
        bool done = false;
        bool timeout = false;
        bool max_connections_hit = false;
        do {
            {
                const std::lock_guard<std::mutex> lock(mtx_sync); // RAII-style acquire and relinquish via destructor
                done = ! ( protocolSessionCount > server->getProtocolSessionsDoneSuccess() ||
                           protocolSessionCount > client->getProtocolSessionsDoneSuccess() ||
                           nullptr == lastCompletedDevice ||
                           lastCompletedDevice->getConnected() );
            }
            max_connections_hit = ( protocolSessionCount * max_connections_per_session ) <= server->getDisconnectCount();
            test_duration = ( jau::getMonotonicTime() - t0 ).to_fraction_i64();
            timeout = framework.is_timedout() ||
                      ( 0_s < timeout_value && timeout_value <= test_duration + timeout_preempt_diff ); // let's timeout here before our timeout timer
            if( !done && !max_connections_hit && !timeout ) {
                jau::sleep_for( 88_ms );
            }
        } while( !done && !max_connections_hit && !timeout );
        test_duration = ( jau::getMonotonicTime() - t0 ).to_fraction_i64();

        fprintf_td(stderr, "\n\n");
        fprintf_td(stderr, "****** Test Stats: duration %" PRIi64 " ms, timeout[hit %d, value %s sec], max_connections hit %d\n",
                test_duration.to_ms(), timeout, timeout_value.to_string(true).c_str(), max_connections_hit);
        fprintf_td(stderr, "  Server ProtocolSessions[success %d/%d total, requested %d], disconnects %d of %d max\n",
                server->getProtocolSessionsDoneSuccess(), server->getProtocolSessionsDoneTotal(), protocolSessionCount,
                server->getDisconnectCount(), ( protocolSessionCount * max_connections_per_session ));
        fprintf_td(stderr, "  Client ProtocolSessions[success %d/%d total, requested %d], disconnects %d of %d max, power[down %d, up %d]\n",
                client->getProtocolSessionsDoneSuccess(), client->getProtocolSessionsDoneTotal(), protocolSessionCount,
                client->getDisconnectCount(), ( protocolSessionCount * max_connections_per_session ),
                client_power_down_count, client_power_up_count);
        fprintf_td(stderr, "\n\n");

        if( expSuccess ) {
            REQUIRE( false == max_connections_hit );
            REQUIRE( false == timeout );
            {
                const std::lock_guard<std::mutex> lock(mtx_sync); // RAII-style acquire and relinquish via destructor
                REQUIRE( protocolSessionCount <= server->getProtocolSessionsDoneTotal() );
                REQUIRE( protocolSessionCount == server->getProtocolSessionsDoneSuccess() );
                REQUIRE( protocolSessionCount <= client->getProtocolSessionsDoneTotal() );
                REQUIRE( protocolSessionCount == client->getProtocolSessionsDoneSuccess() );
                REQUIRE( nullptr != lastCompletedDevice );
                REQUIRE( EIRDataType::NONE != lastCompletedDeviceEIR.getEIRDataMask() );
                REQUIRE( false == lastCompletedDevice->getConnected() );
                REQUIRE( ( protocolSessionCount * max_connections_per_session ) > server->getDisconnectCount() );
                if( client_reset_test ) {
                    REQUIRE( 1 == client_power_down_count );
                    REQUIRE( 1 == client_power_up_count );
                } else {
                    REQUIRE( 0 == client_power_down_count );
                    REQUIRE( 0 == client_power_up_count );
                }
            }
        }

        //
        // Client stop
        //
        const bool current_exp_discovering_state = expSuccess ? true : client->getAdapter()->isDiscovering();
        DBTClientTest::stopDiscovery(client, current_exp_discovering_state, "test"+suffix+"_stopDiscovery");
        client->close("test"+suffix+"_close");

        //
        // Server stop
        //
        DBTServerTest::stop(server, "test"+suffix+"_stop");

        if( expSuccess ) {
            //
            // Validating Security Mode
            //
            SMPKeyBin clientKeys = SMPKeyBin::read(DBTConstants::CLIENT_KEY_PATH, *lastCompletedDevice, true /* verbose */);
            REQUIRE( true == clientKeys.isValid() );
            const BTSecurityLevel clientKeysSecLevel = clientKeys.getSecLevel();
            REQUIRE( secLevelClient == clientKeysSecLevel);
            {
                if( ExpectedPairing::PREPAIRED == clientExpPairing && BTSecurityLevel::NONE < secLevelClient ) {
                    // Using encryption: pre-paired
                    REQUIRE( PairingMode::PRE_PAIRED == lastCompletedDevicePairingMode );
                    REQUIRE( BTSecurityLevel::ENC_ONLY == lastCompletedDeviceSecurityLevel ); // pre-paired fixed level, no auth
                } else if( ExpectedPairing::NEW_PAIRING == clientExpPairing && BTSecurityLevel::NONE < secLevelClient ) {
                    // Using encryption: Newly paired
                    REQUIRE( PairingMode::PRE_PAIRED != lastCompletedDevicePairingMode );
                    REQUIRE_MSG( "PairingMode client "+to_string(lastCompletedDevicePairingMode)+" not > NONE", PairingMode::NONE < lastCompletedDevicePairingMode );
                    REQUIRE_MSG( "SecurityLevel client "+to_string(lastCompletedDeviceSecurityLevel)+" not >= "+to_string(secLevelClient), secLevelClient <= lastCompletedDeviceSecurityLevel );
                } else if( ExpectedPairing::DONT_CARE == clientExpPairing && BTSecurityLevel::NONE < secLevelClient ) {
                    // Any encryption, any pairing
                    REQUIRE_MSG( "PairingMode client "+to_string(lastCompletedDevicePairingMode)+" not > NONE", PairingMode::NONE < lastCompletedDevicePairingMode );
                    REQUIRE_MSG( "SecurityLevel client "+to_string(lastCompletedDeviceSecurityLevel)+" not >= "+to_string(secLevelClient), secLevelClient <= lastCompletedDeviceSecurityLevel );
                } else {
                    // No encryption: No pairing
                    REQUIRE( PairingMode::NONE == lastCompletedDevicePairingMode );
                    REQUIRE( BTSecurityLevel::NONE == lastCompletedDeviceSecurityLevel );
                }
            }

            //
            // Validating EIR
            //
            {
                const std::lock_guard<std::mutex> lock(mtx_sync); // RAII-style acquire and relinquish via destructor
                fprintf_td(stderr, "lastCompletedDevice.connectedEIR: %s\n", lastCompletedDeviceEIR.toString().c_str());
                REQUIRE( EIRDataType::NONE != lastCompletedDeviceEIR.getEIRDataMask() );
                REQUIRE( true == lastCompletedDeviceEIR.isSet(EIRDataType::FLAGS) );
                REQUIRE( true == lastCompletedDeviceEIR.isSet(EIRDataType::SERVICE_UUID) );
                REQUIRE( true == lastCompletedDeviceEIR.isSet(EIRDataType::NAME) );
                REQUIRE( true == lastCompletedDeviceEIR.isSet(EIRDataType::CONN_IVAL) );
                REQUIRE( serverName == lastCompletedDeviceEIR.getName() );
                {
                    const EInfoReport eir = *lastCompletedDevice->getEIR();
                    fprintf_td(stderr, "lastCompletedDevice.currentEIR: %s\n", eir.toString().c_str());
                    REQUIRE( EIRDataType::NONE == eir.getEIRDataMask() );
                    REQUIRE( 0 == eir.getName().length());
                }
            }

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

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

                DBTEndpoint::stopDiscovery(adapter, true /* current_exp_discovering_state */);
            }
        }
        const int count = manager->removeChangedAdapterSetCallback(myChangedAdapterSetFunc);
        fprintf_td(stderr, "****** EOL Removed ChangedAdapterSetCallback %d\n", count);
    }
};