|
@@ -0,0 +1,151 @@
|
|
1
|
+/*
|
|
2
|
+ * ble.c
|
|
3
|
+ *
|
|
4
|
+ * https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/standalone/client.c
|
|
5
|
+ *
|
|
6
|
+ * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
|
|
7
|
+ *
|
|
8
|
+ * This program is free software: you can redistribute it and/or modify
|
|
9
|
+ * it under the terms of the GNU General Public License as published by
|
|
10
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+ * (at your option) any later version.
|
|
12
|
+ *
|
|
13
|
+ * This program is distributed in the hope that it will be useful,
|
|
14
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+ * GNU General Public License for more details.
|
|
17
|
+ *
|
|
18
|
+ * See <http://www.gnu.org/licenses/>.
|
|
19
|
+ */
|
|
20
|
+
|
|
21
|
+#include "btstack.h"
|
|
22
|
+#include "pico/cyw43_arch.h"
|
|
23
|
+
|
|
24
|
+#include "config.h"
|
|
25
|
+#include "log.h"
|
|
26
|
+#include "ble.h"
|
|
27
|
+
|
|
28
|
+enum ble_state {
|
|
29
|
+ TC_OFF = 0,
|
|
30
|
+ TC_IDLE,
|
|
31
|
+ TC_W4_SCAN_RESULT,
|
|
32
|
+ TC_W4_CONNECT,
|
|
33
|
+ TC_W4_SERVICE_RESULT,
|
|
34
|
+ TC_W4_CHARACTERISTIC_RESULT,
|
|
35
|
+ TC_W4_ENABLE_NOTIFICATIONS_COMPLETE,
|
|
36
|
+ TC_W4_READY
|
|
37
|
+};
|
|
38
|
+
|
|
39
|
+static btstack_packet_callback_registration_t hci_event_callback_registration;
|
|
40
|
+static enum ble_state state = TC_OFF;
|
|
41
|
+
|
|
42
|
+static bd_addr_t server_addr;
|
|
43
|
+static bd_addr_type_t server_addr_type;
|
|
44
|
+
|
|
45
|
+static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
|
|
46
|
+ UNUSED(size);
|
|
47
|
+ UNUSED(channel);
|
|
48
|
+
|
|
49
|
+ if (packet_type != HCI_EVENT_PACKET) {
|
|
50
|
+ debug("unexpected packet 0x%02X", packet_type);
|
|
51
|
+ return;
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ switch (hci_event_packet_get_type(packet)) {
|
|
55
|
+ case BTSTACK_EVENT_STATE:
|
|
56
|
+ if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) {
|
|
57
|
+ bd_addr_t local_addr;
|
|
58
|
+ gap_local_bd_addr(local_addr);
|
|
59
|
+ debug("BTstack up with %s", bd_addr_to_str(local_addr));
|
|
60
|
+ state = TC_IDLE;
|
|
61
|
+ } else {
|
|
62
|
+ debug("BTstack down (%d)", btstack_event_state_get_state(packet));
|
|
63
|
+ state = TC_OFF;
|
|
64
|
+ }
|
|
65
|
+ break;
|
|
66
|
+
|
|
67
|
+ case HCI_EVENT_COMMAND_COMPLETE:
|
|
68
|
+ case HCI_EVENT_TRANSPORT_PACKET_SENT:
|
|
69
|
+ break;
|
|
70
|
+
|
|
71
|
+ case GAP_EVENT_ADVERTISING_REPORT:
|
|
72
|
+ if (state != TC_W4_SCAN_RESULT) {
|
|
73
|
+ debug("scan result in invalid state %d", state);
|
|
74
|
+ return;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ gap_event_advertising_report_get_address(packet, server_addr);
|
|
78
|
+ server_addr_type = gap_event_advertising_report_get_address_type(packet);
|
|
79
|
+ debug("Found device with addr %s", bd_addr_to_str(server_addr));
|
|
80
|
+ break;
|
|
81
|
+
|
|
82
|
+ case HCI_EVENT_LE_META:
|
|
83
|
+ switch (hci_event_le_meta_get_subevent_code(packet)) {
|
|
84
|
+ case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
|
|
85
|
+ break;
|
|
86
|
+
|
|
87
|
+ case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
|
|
88
|
+ debug("connection complete?!");
|
|
89
|
+ break;
|
|
90
|
+
|
|
91
|
+ default:
|
|
92
|
+ debug("unexpected LE meta event 0x%02X", hci_event_le_meta_get_subevent_code(packet));
|
|
93
|
+ break;
|
|
94
|
+ }
|
|
95
|
+ break;
|
|
96
|
+
|
|
97
|
+ default:
|
|
98
|
+ debug("unexpected event 0x%02X", hci_event_packet_get_type(packet));
|
|
99
|
+ break;
|
|
100
|
+ }
|
|
101
|
+}
|
|
102
|
+
|
|
103
|
+void ble_init(void) {
|
|
104
|
+ l2cap_init();
|
|
105
|
+ sm_init();
|
|
106
|
+ sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
|
|
107
|
+
|
|
108
|
+ gatt_client_init();
|
|
109
|
+
|
|
110
|
+ hci_event_callback_registration.callback = &hci_event_handler;
|
|
111
|
+ hci_add_event_handler(&hci_event_callback_registration);
|
|
112
|
+
|
|
113
|
+ hci_power_control(HCI_POWER_ON);
|
|
114
|
+}
|
|
115
|
+
|
|
116
|
+void ble_scan(enum ble_scan_mode mode) {
|
|
117
|
+ switch (mode) {
|
|
118
|
+ case BLE_SCAN_OFF:
|
|
119
|
+ debug("stopping BLE scan");
|
|
120
|
+ state = TC_IDLE;
|
|
121
|
+ gap_stop_scan();
|
|
122
|
+ break;
|
|
123
|
+
|
|
124
|
+ case BLE_SCAN_ON:
|
|
125
|
+ debug("starting BLE scan");
|
|
126
|
+ state = TC_W4_SCAN_RESULT;
|
|
127
|
+ gap_set_scan_parameters(0,0x0030, 0x0030);
|
|
128
|
+ gap_start_scan();
|
|
129
|
+ break;
|
|
130
|
+
|
|
131
|
+ case BLE_SCAN_TOGGLE:
|
|
132
|
+ switch (state) {
|
|
133
|
+ case TC_W4_SCAN_RESULT:
|
|
134
|
+ ble_scan(0);
|
|
135
|
+ break;
|
|
136
|
+
|
|
137
|
+ case TC_IDLE:
|
|
138
|
+ ble_scan(1);
|
|
139
|
+ break;
|
|
140
|
+
|
|
141
|
+ default:
|
|
142
|
+ debug("invalid state %d", state);
|
|
143
|
+ break;
|
|
144
|
+ }
|
|
145
|
+ break;
|
|
146
|
+
|
|
147
|
+ default:
|
|
148
|
+ debug("invalid mode %d", mode);
|
|
149
|
+ break;
|
|
150
|
+ }
|
|
151
|
+}
|