~aleteoryx/muditaos

ref: 6befeb5073f3ea8e2c99a9a5f1bf26d34b86715d muditaos/module-bluetooth/Bluetooth/interface/profiles/PAN.cpp -rw-r--r-- 5.7 KiB
6befeb50 — SP2FET [EGD-5316] Add Bluetooth run loop integration into worker 5 years ago
                                                                                
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <log/log.hpp>

extern "C"
{
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "module-bluetooth/Bluetooth/btstack_config.h"

#include <btstack_event.h>
// #include <btstack_link_key_db_fs.h>
#include <bluetooth_company_id.h>
#include <btstack_memory.h>
#include <btstack_run_loop.h>
#include <btstack_run_loop_freertos.h>
#include <btstack_stdin.h>
#include <hci.h>
#include <hci_dump.h>
    // #include <btstack_tlv_posix.h>

#include <bluetooth_sdp.h>
#include <bnep_lwip.h>
#include <btstack_chipset_cc256x.h>
#include <pan.h>
#include <sdp_util.h>
};

#include <BtCommand.hpp>
#include <Error.hpp>
#ifdef TARGET_RT1051
#include <Bluetooth/glucode/btstack_uart_block_rt1051.h>
#endif

static btstack_packet_callback_registration_t hci_event_callback_registration;

#define NETWORK_TYPE_IPv4 0x0800
#define NETWORK_TYPE_ARP  0x0806
#define NETWORK_TYPE_IPv6 0x86DD

static uint8_t pan_sdp_record[220];

static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
{
    /* LISTING_PAUSE */
    UNUSED(channel);
    UNUSED(size);

    bd_addr_t event_addr;

    switch (packet_type) {
    case HCI_EVENT_PACKET:
        switch (hci_event_packet_get_type(packet)) {

        case HCI_EVENT_PIN_CODE_REQUEST:
            // inform about pin code request
            LOG_INFO("Pin code request - using '0000'");
            hci_event_pin_code_request_get_bd_addr(packet, event_addr);
            gap_pin_code_response(event_addr, "0000");
            break;

        case HCI_EVENT_USER_CONFIRMATION_REQUEST:
            // inform about user confirmation request
            LOG_INFO("SSP User Confirmation Auto accept");
            hci_event_user_confirmation_request_get_bd_addr(packet, event_addr);
            break;

        /* @text BNEP_EVENT_CHANNEL_OPENED is received after a BNEP connection was established or
         * or when the connection fails. The status field returns the error code.
         */
        case BNEP_EVENT_CHANNEL_OPENED:
            if (bnep_event_channel_opened_get_status(packet)) {
                LOG_INFO("BNEP channel open failed, status %02x", bnep_event_channel_opened_get_status(packet));
            }
            else {
                uint16_t uuid_source = bnep_event_channel_opened_get_source_uuid(packet);
                uint16_t uuid_dest   = bnep_event_channel_opened_get_destination_uuid(packet);
                uint16_t mtu         = bnep_event_channel_opened_get_mtu(packet);
                bnep_event_channel_opened_get_remote_address(packet, event_addr);
                LOG_INFO("BNEP connection open succeeded to %s source UUID 0x%04x dest UUID: 0x%04x, max frame size %u",
                         bd_addr_to_str(event_addr),
                         uuid_source,
                         uuid_dest,
                         mtu);
                LOG_INFO("Please open 'http://192.168.7.1' in your web browser: ");
            }
            break;

        /* @text BNEP_EVENT_CHANNEL_CLOSED is received when the connection gets closed.
         */
        case BNEP_EVENT_CHANNEL_CLOSED:
            LOG_INFO("BNEP channel closed");
            break;

        default:
            break;
        }
        break;
    default:
        break;
    }
}

namespace bluetooth
{

    // Set local name with a template Bluetooth address, that will be automatically
    // replaced with a actual address once it is available, i.e. when BTstack boots
    Error set_name(std::string &name)
    {
        // name has to have storage
        constexpr uint32_t size = 64;
        static char lname[size] = {0};
        snprintf(lname, size, "%s %s", name.c_str(), "00:00:00:00:00:00");
        LOG_INFO("Setting local name: %s", lname);
        gap_set_local_name(lname);
        return Error();
    }
    namespace PAN
    {

        Error bnep_setup()
        {
            // Discoverable
            // up and starts talking to a Bluetooth module.
            gap_discoverable_control(1);

            // register for HCI events
            hci_event_callback_registration.callback = &packet_handler;
            hci_add_event_handler(&hci_event_callback_registration);

            // Initialize L2CAP
            l2cap_init();

            // Initialize BNEP
            bnep_init();

            // Init SDP
            sdp_init();
            memset(pan_sdp_record, 0, sizeof(pan_sdp_record));
            uint16_t network_packet_types[] = {NETWORK_TYPE_IPv4, NETWORK_TYPE_ARP, 0}; // 0 as end of list

            // NAP Network Access Type: Other, 1 MB/s
            pan_create_nap_sdp_record(pan_sdp_record,
                                      sdp_create_service_record_handle(),
                                      network_packet_types,
                                      NULL,
                                      NULL,
                                      BNEP_SECURITY_NONE,
                                      PAN_NET_ACCESS_TYPE_OTHER,
                                      1000000,
                                      NULL,
                                      NULL);
            sdp_register_service(pan_sdp_record);
            LOG_INFO("SDP service record size: %u", de_get_len((uint8_t *)pan_sdp_record));
            return Error();
        }

        Error bnep_start()
        {
            bnep_lwip_init();

            // Setup NAP Service via BENP lwIP adapter
            bnep_lwip_register_service(BLUETOOTH_SERVICE_CLASS_NAP, 1691);

            // register callback - to print state
            bnep_lwip_register_packet_handler(packet_handler);
            return Error();
        }

    } // namespace PAN
} // namespace Bt