-
Notifications
You must be signed in to change notification settings - Fork 22
/
bt.py
171 lines (140 loc) · 5.84 KB
/
bt.py
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
import bluetooth
import struct
from micropython import const
# This module is the cleaned merge of 2 micropython examples released under MIT
# license: ble_uart_peripheral.py and ble_advertising.py.
# Copyright (c) 2013-2022 Damien P. George
# Copyright (c) 2023 Salvatore Sanfilippo <[email protected]>
# 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.
# Advertising payloads are repeated packets of the following form:
# 1 byte data length (N + 1)
# 1 byte type (see constants below)
# N bytes type-specific data
_ADV_TYPE_FLAGS = const(0x01)
_ADV_TYPE_NAME = const(0x09)
_ADV_TYPE_UUID16_COMPLETE = const(0x3)
_ADV_TYPE_UUID32_COMPLETE = const(0x5)
_ADV_TYPE_UUID128_COMPLETE = const(0x7)
_ADV_TYPE_UUID16_MORE = const(0x2)
_ADV_TYPE_UUID32_MORE = const(0x4)
_ADV_TYPE_UUID128_MORE = const(0x6)
_ADV_TYPE_APPEARANCE = const(0x19)
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_WRITE = const(3)
_FLAG_WRITE = const(0x0008)
_FLAG_NOTIFY = const(0x0010)
_UART_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
_UART_TX = (
bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"),
_FLAG_NOTIFY,
)
_UART_RX = (
bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"),
_FLAG_WRITE,
)
_UART_SERVICE = (
_UART_UUID,
(_UART_TX, _UART_RX),
)
def pack_adv_data(adv_type, value):
return struct.pack("BB", len(value) + 1, adv_type) + value
def pack_adv_service(uuid):
b = bytes(uuid)
if len(b) == 2:
return pack_adv_data(_ADV_TYPE_UUID16_COMPLETE, b)
elif len(b) == 4:
return pack_adv_data(_ADV_TYPE_UUID32_COMPLETE, b)
elif len(b) == 16:
return pack_adv_data(_ADV_TYPE_UUID128_COMPLETE, b)
# Generate a payload to advertise as primary payload
def gen_advertising_payload(name=None):
payload = bytearray()
# General discoverable + BR/EDR not supported.
payload += pack_adv_data(_ADV_TYPE_FLAGS, struct.pack("B", 0x06))
if name: payload += pack_adv_data(_ADV_TYPE_NAME, name)
# Apparence: generic computer (128)
payload += pack_adv_data(_ADV_TYPE_APPEARANCE, struct.pack("<h", 128))
return payload
# Generate a response payload with further information to
# return to the scanning device.
def gen_resp_payload():
return pack_adv_service(_UART_UUID)
class BLEUART:
def __init__(self, ble, name="mpy-uart", rxbuf=100):
name = name[:16]
self._ble = ble
if not self._ble.active(): self._ble.active(True)
self._ble.irq(self.irq_handler)
((self._tx_handle, self._rx_handle),) = self._ble.gatts_register_services((_UART_SERVICE,))
# Increase the size of the rx buffer and enable append mode.
self._ble.gatts_set_buffer(self._rx_handle, rxbuf, True)
self._connections = set()
self._rx_buffer = bytearray()
self._handler = None
self._payload = gen_advertising_payload(name=name)
self._resp = gen_resp_payload()
self._advertise()
def set_callback(self, handler):
self._handler = handler
def irq_handler(self, event, data):
# Track connections so we can send notifications.
if event == _IRQ_CENTRAL_CONNECT:
conn_handle, _, _ = data
self._connections.add(conn_handle)
elif event == _IRQ_CENTRAL_DISCONNECT:
conn_handle, _, _ = data
if conn_handle in self._connections:
self._connections.remove(conn_handle)
# Start advertising again to allow a new connection.
self._advertise()
elif event == _IRQ_GATTS_WRITE:
conn_handle, value_handle = data
if conn_handle in self._connections and value_handle == self._rx_handle:
self._rx_buffer += self._ble.gatts_read(self._rx_handle)
if self._handler:
self._handler()
def any(self):
return len(self._rx_buffer)
def read(self, sz=None):
if not sz:
sz = len(self._rx_buffer)
result = self._rx_buffer[0:sz]
self._rx_buffer = self._rx_buffer[sz:]
return result
def write(self, data):
for conn_handle in self._connections:
self._ble.gatts_notify(conn_handle, self._tx_handle, data)
# Like write() but adds a newline at the end.
def print(self, value):
self.write(str(value)+"\n")
def close(self):
for conn_handle in self._connections:
self._ble.gap_disconnect(conn_handle)
self._connections.clear()
def _advertise(self, interval_us=200000):
self._ble.gap_advertise(interval_us, adv_data=self._payload, resp_data=self._resp)
if __name__ == "__main__":
import time
def receive_callback():
data = uart.read().decode()
print("From BLE",data)
uart.print(data)
ble = bluetooth.BLE()
uart = BLEUART(ble, name="ble_test")
uart.set_callback(receive_callback)
while True: time.sleep(1)