-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.py
executable file
·86 lines (66 loc) · 2.3 KB
/
test.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
#!/usr/bin/env python3
import blufi
import time
import sys
################################################################################
# Options for misc. tests
################################################################################
# Enable/Disable negotiateSecurity
TEST_SECURITY = True
# Enable/Disable getVersion
TEST_VERSION = True
# Enable/Disable get SSID list
TEST_SCAN = False
# Enable/Disable send SSID/Pass
TEST_POST_WIFI = False
TEST_POST_WIFI_CREDS = {
'ssid': 'yourssid',
'pass': 'yourpass'
}
# Test sending custom data
TEST_CUSTOM_DATA = False
# Test behavior when disabling rx notifications
TEST_NOTIFY = False
################################################################################
# Create client instance
client = blufi.BlufiClient()
# connect to BLUFI_DEVICE
# NOTE: atexit is used internally to send disconnect before script exits
client.connectByName('BLUFI_DEVICE')
# Cap pkt size. See README.md about MTU
client.setPostPackageLengthLimit(256)
if TEST_SECURITY:
client.negotiateSecurity()
if TEST_VERSION:
client.requestVersion()
print('Version: ', client.getVersion())
if TEST_SCAN:
# Reset STA state in case its attempting to connect
client.postDeviceMode(blufi.OP_MODE_NULL)
client.postDeviceMode(blufi.OP_MODE_STA)
client.requestDeviceScan()
print('SSIDs:')
for item in client.getSSIDList():
print(item)
if TEST_POST_WIFI:
client.postDeviceMode(blufi.OP_MODE_STA)
client.postStaWifiInfo(TEST_POST_WIFI_CREDS)
if TEST_CUSTOM_DATA:
client.postCustomData(data=bytes.fromhex('010203'))
client.wait(0.5)
# Test sending large payload
# NOTE: esp32 receives large payloads fine, but appears to break if response
# payload is >= 1984 bytes, at least if echoed inside the event handler
repeatStr = lambda s, count: ''.join([s for n in range(count)])
genStr = lambda count: ''.join([ repeatStr('%X' % n, count) for n in range(16) ])
client.postCustomData(data=bytes.fromhex(genStr(240))) # 240 max for echo
client.wait(3)
if TEST_NOTIFY:
client.stopNotify()
client.postDeviceMode(blufi.OP_MODE_NULL)
client.postDeviceMode(blufi.OP_MODE_NULL)
client.startNotify()
client.postDeviceMode(blufi.OP_MODE_NULL)
client.postDeviceMode(blufi.OP_MODE_NULL)
sys.exit(0)
print('Exiting')