-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_spp_IF820-BT900.py
executable file
·194 lines (165 loc) · 7.22 KB
/
sample_spp_IF820-BT900.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python3
import argparse
import logging
import time
import sys
sys.path.append('./common_lib/libraries')
from BT900SerialPort import BT900SerialPort
from If820Board import If820Board
import EzSerialPort as ez_port
"""
Hardware Setup
This sample requires the following hardware:
-BT900 connected to PC via USB to act as a Bluetooth Peripheral
-IF820 connected to PC via USB to act as a Bluetooth Central
"""
API_FORMAT = ez_port.EzSerialApiMode.TEXT.value
SPP_DATA = "abcdefghijklmnop"
OTA_LATENCY = 1
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-cp', '--connection_p',
required=True, help="BT900 Peripheral COM port")
parser.add_argument('-d', '--debug', action='store_true',
help="Enable verbose debug messages")
args, unknown = parser.parse_known_args()
logging.basicConfig(format='%(asctime)s [%(module)s] %(levelname)s: %(message)s', level=logging.INFO)
if args.debug:
logging.info("Debugging mode enabled")
logging.getLogger().setLevel(logging.DEBUG)
# open devices
# bt900
bt900_peripheral = BT900SerialPort()
bt900_peripheral.open(
args.connection_p, bt900_peripheral.BT900_DEFAULT_BAUD)
# IF820
if820_board_c = If820Board.get_board()
if820_board_c.open_and_init_board()
if820_board_c.p_uart.set_api_format(API_FORMAT)
logging.info("IF820 Factory Reset")
ez_rsp = if820_board_c.p_uart.send_and_wait(
if820_board_c.p_uart.CMD_FACTORY_RESET)
If820Board.check_if820_response(
if820_board_c.p_uart.CMD_FACTORY_RESET, ez_rsp)
logging.info("Wait for IF820 Reboot...")
ez_rsp = if820_board_c.p_uart.wait_event(
if820_board_c.p_uart.EVENT_SYSTEM_BOOT)
If820Board.check_if820_response(
if820_board_c.p_uart.EVENT_SYSTEM_BOOT, ez_rsp)
# bt900 query firmware version
response = bt900_peripheral.get_bt900_fw_ver()
logging.info(f"BT900 firmware version = {response}")
# bt900 get mac address of peripheral
bt900_mac = bt900_peripheral.get_bt900_bluetooth_mac()
logging.info(f"BT900 bluetooth mac addr = {bt900_mac}")
# bt900 enter command mode
bt900_peripheral.enter_command_mode()
# bt900 delete all previous bonds
response = bt900_peripheral.send(
bt900_peripheral.BT900_CMD_BTC_BOND_DEL)
BT900SerialPort.check_bt900_response(response)
# bt900 set io cap
response = bt900_peripheral.send(
bt900_peripheral.BT900_CMD_BTC_IOCAP)
BT900SerialPort.check_bt900_response(response)
# bt900 set pairable
response = bt900_peripheral.send(
bt900_peripheral.BT900_CMD_SET_BTC_PAIRABLE)
BT900SerialPort.check_bt900_response(response)
# bt900 set connectable
response = bt900_peripheral.send(
bt900_peripheral.BT900_CMD_SET_BTC_CONNECTABLE)
BT900SerialPort.check_bt900_response(response)
# bt900 set discoverable
response = bt900_peripheral.send(
bt900_peripheral.BT900_CMD_SET_BTC_DISCOVERABLE)
BT900SerialPort.check_bt900_response(response)
# bt900 open spp port
response = bt900_peripheral.send(
bt900_peripheral.BT900_CMD_SPP_OPEN)
BT900SerialPort.check_bt900_response(response)
# IF820(central) connect to BT900 (peripheral)
logging.info("IF820 Connect to BT900")
bt900_mac[1].reverse()
response = if820_board_c.p_uart.send_and_wait(command=if820_board_c.p_uart.CMD_CONNECT,
address=bt900_mac[1],
type=1)
If820Board.check_if820_response(
if820_board_c.p_uart.CMD_CONNECT, response)
# IF820 Event (Text Info contains "P")
logging.info("Wait for IF820 Pairing Requested Event.")
response = if820_board_c.p_uart.wait_event(
event=if820_board_c.p_uart.EVENT_SMP_PAIRING_REQUESTED)
If820Board.check_if820_response(
if820_board_c.p_uart.EVENT_SMP_PAIRING_REQUESTED, response)
# bt900 event (Text Info contains "Pair Req")
logging.info("Wait for BT900 Pair Request Event.")
bt900_event = bt900_peripheral.wait_for_response()
BT900SerialPort.check_bt900_response(
bt900_event, bt900_peripheral.BT900_PAIR_REQ)
# bt900 set pairable (Response is "OK")
logging.info("Send BT900 Pair Request")
response = bt900_peripheral.send(
bt900_peripheral.BT900_CMD_BTC_PAIR_RESPONSE)
BT900SerialPort.check_bt900_response(response)
# IF820 Event (Text Info contains "PR")
logging.info("Wait for IF820 SMP Pairing Result Event.")
response = if820_board_c.p_uart.wait_event(
event=if820_board_c.p_uart.EVENT_SMP_PAIRING_RESULT)
If820Board.check_if820_response(
if820_board_c.p_uart.EVENT_SMP_PAIRING_RESULT, response)
# bt900 event (Text Info contains "Pair Result")
logging.info("Wait for BT900 SMP Pair Result.")
bt900_event = bt900_peripheral.wait_for_response()
BT900SerialPort.check_bt900_response(
bt900_event, bt900_peripheral.BT900_PAIR_RESULT)
# IF820 Event (Text Info contains "ENC")
logging.info("Wait for IF820 SMP Encryption Status Event.")
response = if820_board_c.p_uart.wait_event(
event=if820_board_c.p_uart.EVENT_SMP_ENCRYPTION_STATUS)
If820Board.check_if820_response(
if820_board_c.p_uart.EVENT_SMP_PASSKEY_DISPLAY_REQUESTED, response)
# IF820 Event
logging.info("Wait for IF820 Bluetooth Connected Event.")
response = if820_board_c.p_uart.wait_event(
event=if820_board_c.p_uart.EVENT_BT_CONNECTED)
If820Board.check_if820_response(
if820_board_c.p_uart.EVENT_BT_CONNECTED, response)
# bt900 event
logging.info("Wait for BT900 SPP Connect Event.")
bt900_event = bt900_peripheral.wait_for_response()
BT900SerialPort.check_bt900_response(
bt900_event, bt900_peripheral.BT900_SPP_CONNECT)
logging.info("IF820->BT900 SPP Data")
bt900_peripheral.clear_cmd_rx_queue()
if820_board_c.p_uart.send(bytes(SPP_DATA, 'utf-8'))
while True:
try:
rx_data = bt900_peripheral.wait_for_response(OTA_LATENCY)
logging.info(f"BT900 RX Data = {bytes(rx_data, 'utf-8')}")
except:
break
logging.info("BT900->IF820 SPP Data")
if820_board_c.p_uart.clear_rx_queue()
bt900_peripheral.send(
bt900_peripheral.BT900_SPP_WRITE_PREFIX + SPP_DATA)
time.sleep(OTA_LATENCY)
rx_data = if820_board_c.p_uart.read()
logging.info(f"IF820 RX Data = {rx_data}")
logging.info("BT900 Disconnect SPP")
bt900_peripheral.send(bt900_peripheral.BT900_SPP_DISCONNECT)
logging.info("IF820 Wait for BT Disconnect Event...")
response = if820_board_c.p_uart.wait_event(
if820_board_c.p_uart.EVENT_BT_DISCONNECTED)
If820Board.check_if820_response(
if820_board_c.p_uart.EVENT_BT_DISCONNECTED, response)
logging.info("BT900 exit command mode")
bt900_peripheral.exit_command_mode()
bt900_peripheral.close()
logging.info("IF820 Factory Reset")
if820_board_c.p_uart.send_and_wait(if820_board_c.p_uart.CMD_FACTORY_RESET)
logging.info("Wait for IF820 Reboot...")
if820_board_c.p_uart.wait_event(if820_board_c.p_uart.EVENT_SYSTEM_BOOT)
logging.info("Reset IF820 DVK")
if820_board_c.close_ports_and_reset()
logging.info("Done!")