Procházet zdrojové kódy

add ble scanning test

Thomas Buck před 6 měsíci
rodič
revize
47d270deb2
6 změnil soubory, kde provedl 194 přidání a 3 odebrání
  1. 1
    0
      CMakeLists.txt
  2. 31
    0
      include/ble.h
  3. 0
    2
      include/lipo.h
  4. 151
    0
      src/ble.c
  5. 4
    0
      src/console.c
  6. 7
    1
      src/main.c

+ 1
- 0
CMakeLists.txt Zobrazit soubor

@@ -55,6 +55,7 @@ target_sources(gadget PUBLIC
55 55
     src/debug.c
56 56
     src/buttons.c
57 57
     src/lipo.c
58
+    src/ble.c
58 59
 
59 60
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
60 61
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c

+ 31
- 0
include/ble.h Zobrazit soubor

@@ -0,0 +1,31 @@
1
+/*
2
+ * ble.h
3
+ *
4
+ * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * See <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+#ifndef __BLE_H__
20
+#define __BLE_H__
21
+
22
+void ble_init(void);
23
+
24
+enum ble_scan_mode {
25
+    BLE_SCAN_OFF    = 0,
26
+    BLE_SCAN_ON     = 1,
27
+    BLE_SCAN_TOGGLE = 2,
28
+};
29
+void ble_scan(enum ble_scan_mode mode);
30
+
31
+#endif // __BLE_H__

+ 0
- 2
include/lipo.h Zobrazit soubor

@@ -1,8 +1,6 @@
1 1
 /*
2 2
  * lipo.h
3 3
  *
4
- * https://github.com/raspberrypi/pico-examples/blob/master/adc/read_vsys/power_status.c
5
- *
6 4
  * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
7 5
  *
8 6
  * This program is free software: you can redistribute it and/or modify

+ 151
- 0
src/ble.c Zobrazit soubor

@@ -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
+}

+ 4
- 0
src/console.c Zobrazit soubor

@@ -29,6 +29,7 @@
29 29
 #include "debug.h"
30 30
 #include "console.h"
31 31
 #include "lipo.h"
32
+#include "ble.h"
32 33
 
33 34
 #define CNSL_BUFF_SIZE 1024
34 35
 #define CNSL_REPEAT_MS 500
@@ -73,6 +74,7 @@ static void cnsl_interpret(const char *line) {
73 74
         println("   help - print this message");
74 75
         println("  mount - make mass storage medium (un)available");
75 76
         println("  power - show Lipo battery status");
77
+        println("   scan - start or stop BLE scan");
76 78
         println("Press Enter with no input to repeat last command.");
77 79
         println("Use repeat to continuously execute last command.");
78 80
         println("Stop this by calling repeat again.");
@@ -89,6 +91,8 @@ static void cnsl_interpret(const char *line) {
89 91
         println("Battery: %.2fV = %.1f%% @ %s",
90 92
                 volt, lipo_percentage(volt),
91 93
                 lipo_charging() ? "charging" : "draining");
94
+    } else if (strcmp(line, "scan") == 0) {
95
+        ble_scan(2);
92 96
     } else {
93 97
         println("unknown command \"%s\"", line);
94 98
     }

+ 7
- 1
src/main.c Zobrazit soubor

@@ -28,6 +28,7 @@
28 28
 #include "usb.h"
29 29
 #include "fat_disk.h"
30 30
 #include "buttons.h"
31
+#include "ble.h"
31 32
 
32 33
 int main(void) {
33 34
     heartbeat_init();
@@ -43,10 +44,15 @@ int main(void) {
43 44
     // required for LiPo voltage reading
44 45
     adc_init();
45 46
 
47
+    // required for BLE and LiPo voltage reading
48
+    debug("cyw43_arch_init");
46 49
     if (cyw43_arch_init()) {
47
-        debug("cyw43_arch failed");
50
+        debug("cyw43_arch_init failed");
48 51
     }
49 52
 
53
+    debug("ble_init");
54
+    ble_init();
55
+
50 56
     debug("fat_disk_init");
51 57
     fat_disk_init();
52 58
 

Loading…
Zrušit
Uložit