-
Notifications
You must be signed in to change notification settings - Fork 20
/
BTScan.py
235 lines (201 loc) · 8.72 KB
/
BTScan.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from bluepy.btle import Scanner, DefaultDelegate, BTLEDisconnectError, Peripheral, BTLEException
from queue import Queue
from threading import Thread
import logging
_LOGGER = logging.getLogger(__name__)
class OperationType:
LOCK_FULL = 1
LOCK_ONE = 2
LOCK_NIGHT_MODE = 1
UNLOCK = 4
GET_REPORT = 5
ADJUST_NUMBER_OF_ROTATION = 8
START_LEARNING_MODE = 9
DELETE_ALL_CONTROLLERS = 10
GET_READY_FOR_DELETE_USER = 11
BUZZER_REPORT = 12
GET_USERS = 13
GET_INFORMATION = 14
START_UPDATE_MODE = 15
GET_KEY = "!"
WAIT = "&"
DISCONNECT = "#"
GET_CHECK_IN_OUT_TIMES = "?"
GET_AUTO_LOCK_DAY_TIMES = "+"
LEARN_SUCCESS = "*"
class BleServicesAndChracteristicsChars:
CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb"
BLE_SERVICES = ["00035b03-58e6-07dd-021a-08123a000300","49535343-FE7D-4AE5-8FA9-9FAFD205E455","6e400001-b5a3-f393-e0a9-e50e24dcca9e"]
BLE_WRITE_CHARACTERISTICS = {"00035b03-58e6-07dd-021a-08123a000301","49535343-1E4D-4BD9-BA61-23C647249616","6e400002-b5a3-f393-e0a9-e50e24dcca9e"}
BLE_READ_CHARACTERISTICS = {"00035b03-58e6-07dd-021a-08123a0003ff","49535343-1E4D-4BD9-BA61-23C647249616","6e400003-b5a3-f393-e0a9-e50e24dcca9e"}
DEVICE_NAME_CONTENT = "UTOPIC"
class cDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
if isNewDev:
print("Discovered device", dev.addr)
elif isNewData:
print("Received new data from", dev.addr)
class Discovery():
def __init__(self):
scanner = Scanner().withDelegate(cDelegate())
self.devices = scanner.scan(10.0)
self.utopicdevice = []
def getDevices(self, address = None):
if address is None:
for dev in self.devices:
print("Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi))
utdv = UtopicDevice(dev)
if utdv.getDevice() is not None:
self.utopicdevice.append(utdv)
else:
print("return device address")
return self.utopicdevice
class UtopicDevice():
def __init__(self, device):
self.device = device
self.utopicdevice = None
self.writeChar = None
self.readChar = None
self.notChar = None
for (adtype, desc, value) in device.getScanData():
print(" %s = %s" % (desc, value))
if value == BleServicesAndChracteristicsChars.DEVICE_NAME_CONTENT:
self.utopicdevice = self
if value in BleServicesAndChracteristicsChars.BLE_SERVICES:
self.serviceuuid = value
def getAddress(self):
return self.device.addr
def getServiceUUID(self):
return self.serviceuuid
def getDevice(self):
return self.utopicdevice
def setWriteCharact(self, writeChar):
self.writeChar = writeChar
def getWriteCharact(self):
return self.writeChar
def setReadCharact(self, readChar):
self.readChar = readChar
def getReadCharact(self):
return self.readChar
def setNotifyCharact(self, notChar):
self.notChar = notChar
def getNotifyCharact(self):
return self.notChar
class BLEMagic(DefaultDelegate):
def __init__(self):
super().__init__()
discover = Discovery()
self.devices = discover.getDevices()
self.utopicKey = None
self.utopicdevices = []
self.periph = None
# create the TX queue
self._tx_queue = Queue()
# start the bluepy IO thread
self._bluepy_thread = Thread(target=self._bluepy_handler)
self._bluepy_thread.name = "bluepy_handler"
self._bluepy_thread.daemon = True
self._bluepy_thread.start()
def handleNotification(self, cHandle, data):
"""This is the notification delegate function from DefaultDelegate
"""
print("\nReceived Notification: %s Handle: %s",str(data), cHandle)
def _bluepy_handler(self):
"""This is the bluepy IO thread
:return:
"""
#ADDRESS OF MAGIC: 04:91:62:25:cb:6f
for utopicdevice in self.devices:
serviceuuid = utopicdevice.getServiceUUID()
address = utopicdevice.getAddress()
# address = "04:91:62:25:cb:6f"
print(address)
if serviceuuid in BleServicesAndChracteristicsChars.BLE_SERVICES:
try:
self.periph = Peripheral(address.upper())
self.periph.withDelegate(self)
print("conecta")
except BTLEDisconnectError as e:
_LOGGER.error("Disconected: %s", e)
except BTLEException as e:
_LOGGER.error("Error: %s", e)
if self.periph is not None:
service = self.periph.getServiceByUUID(serviceuuid)
print(service)
#for charact in service.getCharacteristics():
descs = service.getDescriptors()
for desc in descs:
str_uuid = str(desc.uuid).lower()
print(str_uuid, desc.handle)
if str_uuid in BleServicesAndChracteristicsChars.BLE_WRITE_CHARACTERISTICS:
utopicdevice.setWriteCharact(desc.handle)
if str_uuid in BleServicesAndChracteristicsChars.CLIENT_CHARACTERISTIC_CONFIG:
utopicdevice.setNotifyCharact(desc.handle)
if str_uuid in BleServicesAndChracteristicsChars.BLE_READ_CHARACTERISTICS:
#state = self.kv2dict(charact.read().decode())
#print(state)
utopicdevice.setReadCharact(desc.handle)
subscribe_bytes = b'\x01\x00'
if utopicdevice.getReadCharact() is not None and utopicdevice.getWriteCharact() is not None:
response = self.periph.writeCharacteristic(utopicdevice.getNotifyCharact(), subscribe_bytes, withResponse=True)
print(response)
# now that we're subscribed for notifications, waiting for TX/RX...
while True:
while not self._tx_queue.empty():
msg = self._tx_queue.get_nowait()
msg_bytes = bytes(msg, encoding="utf-8")
self.periph.writeCharacteristic(utopicdevice.getWriteCharact(), msg_bytes)
self.periph.waitForNotifications(1.0)
else:
print("not read")
self.utopicdevices.append(utopicdevice)
def kv2dict(kvstr, sep=";"):
result = {}
for x in kvstr.split(sep, 50):
(k, v) = x.split("=", 2)
result[k] = v
return result
def get_key(self):
return self.utopicKey
def getDevices(self):
return self.utopicdevices
def onDataReceived(self, data):
#String data = new String(dataRaw);
if(data.contains("DEV_KEY:")):
key = data.substring(8,data.length())
self.utopicKey = key
# Database.getInstance().addNewBarrel(UtopicDevice.SelectedUtopicDevice.getMacId(),UtopicDevice.SelectedUtopicDevice.getName(),SelectedTag);
# RecognitionState = eRecognitionState.S2_SENDING_OPEN;
# Utopic
def create_operation(self, type):
if type in (OperationType.GET_KEY, OperationType.DISCONNECT, OperationType.GET_CHECK_IN_OUT_TIMES, OperationType.GET_AUTO_LOCK_DAY_TIMES, OperationType.LEARN_SUCCESS):
return type
else:
utopicKey = self.utopicdevices.getAddress()
myMacAdress = '22:33:44:55:66:77' #GetMacAdress()
userKey = ""
for pos in myMacAdress.split(":"):
userKey += pos
print(userKey)
counter=0
counter+=1
# hashit = counter + type + settings
# self.key = base64.b64encode(hashlib.md5(hashit.encode()).digest())[:16]
# try:
# auth.write("M=0;K=".encode() + self.key, True)
# except:
# print("ERROR: failed to unlock %s - wrong pincode?" % self.name)
# return not self.locked
# Encryption encryp = new Encryption(new BigInteger(userKey.trim(), 16).longValue(), utopicKey);
# byte[] key = encryp.encrypt(counter, operation, settings);
# return key;
def send(self, message):
"""Call this function to send a BLE message over the UART service
:param message: Message to send
:return:
"""
# put the message in the TX queue
self._tx_queue.put_nowait(message)
Ble = BLEMagic()