forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
310 lines (259 loc) · 9.64 KB
/
utils.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Copyright (c) 2009-2021 Arm Limited
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import logging
import platform
import random
import re
import shlex
import sys
from chip import exceptions
from chip.setup_payload import SetupPayload
if platform.system() == 'Darwin':
from chip.ChipCoreBluetoothMgr import CoreBluetoothManager as BleManager
elif sys.platform.startswith('linux'):
from chip.ChipBluezMgr import BluezManager as BleManager
log = logging.getLogger(__name__)
class ParsingError(exceptions.ChipStackException):
def __init__(self, msg=None):
self.msg = "Parsing Error: " + msg
def __str__(self):
return self.msg
def get_device_details(device):
"""
Get device details from logs
:param device: serial device instance
:return: device details dictionary or None
"""
ret = device.wait_for_output("SetupQRCode")
if ret is None or len(ret) < 2:
return None
qr_code = re.sub(
r"[\[\]]", "", ret[-1].partition("SetupQRCode:")[2]).strip()
try:
device_details = dict(SetupPayload().ParseQrCode(
"VP:vendorpayload%{}".format(qr_code)).attributes)
except exceptions.ChipStackError as ex:
log.error(ex.msg)
return None
return device_details
def ParseEncodedString(value):
if value.find(":") < 0:
raise ParsingError(
"Value should be encoded in encoding:encodedvalue format")
enc, encValue = value.split(":", 1)
if enc == "str":
return encValue.encode("utf-8") + b'\x00'
elif enc == "hex":
return bytes.fromhex(encValue)
raise ParsingError("Only str and hex encoding is supported")
def ParseValueWithType(value, type):
if type == 'int':
return int(value)
elif type == 'str':
return value
elif type == 'bytes':
return ParseEncodedString(value)
elif type == 'bool':
return (value.upper() not in ['F', 'FALSE', '0'])
else:
raise ParsingError('Cannot recognize type: {}'.format(type))
def FormatZCLArguments(args, command):
commandArgs = {}
for kvPair in args:
if kvPair.find("=") < 0:
raise ParsingError("Argument should in key=value format")
key, value = kvPair.split("=", 1)
valueType = command.get(key, None)
commandArgs[key] = ParseValueWithType(value, valueType)
return commandArgs
def send_zcl_command(devCtrl, line):
"""
Send ZCL message to device:
<cluster> <command> <nodeid> <endpoint> <groupid> [key=value]...
:param devCtrl: device controller instance
:param line: command line
:return: error code and command responde
"""
res = None
err = 0
try:
args = shlex.split(line)
all_commands = devCtrl.ZCLCommandList()
if len(args) < 5:
raise exceptions.InvalidArgumentCount(5, len(args))
cluster = args[0]
command = args[1]
if cluster not in all_commands:
raise exceptions.UnknownCluster(cluster)
commandObj = all_commands.get(cluster).get(command, None)
# When command takes no arguments, (not command) is True
if commandObj is None:
raise exceptions.UnknownCommand(cluster, command)
try:
req = commandObj(**FormatZCLArguments(args[5:], commandObj))
except BaseException:
raise exceptions.UnknownCommand(cluster, command)
nodeid = int(args[2])
endpoint = int(args[3])
try:
res = asyncio.run(devCtrl.SendCommand(nodeid, endpoint, req))
logging.debug(f"CommandResponse {res}")
if res is not None:
log.info("Success, received command response:")
log.info(res)
else:
log.info("Success, no command response.")
except exceptions.InteractionModelError as ex:
return (int(ex.status), None)
log.error("Failed to send ZCL command [{}] {}.".format(int(ex.status), None))
except exceptions.ChipStackException as ex:
log.error("An exception occurred during processing ZCL command:")
log.error(str(ex))
err = -1
except Exception as ex:
log.error("An exception occurred during processing input:")
log.error(str(ex))
err = -1
return (err, res)
def scan_chip_ble_devices(devCtrl):
"""
BLE scan CHIP device
BLE scanning for 10 seconds and collect the results
:param devCtrl: device controller instance
:return: List of visible BLE devices
"""
devices = []
bleMgr = BleManager(devCtrl)
bleMgr.scan("-t 10")
for device in bleMgr.peripheral_list:
devIdInfo = bleMgr.get_peripheral_devIdInfo(device)
if devIdInfo:
devInfo = devIdInfo.__dict__
devInfo["name"] = device.Name
devices.append(devInfo)
return devices
def check_chip_ble_devices_advertising(devCtrl, name, deviceDetails=None):
"""
Check if CHIP device advertise
BLE scanning for 10 seconds and compare with device details
:param devCtrl: device controller instance
:param name: device advertising name
:param name: device details
:return: True if device advertise else False
"""
ble_chip_device = scan_chip_ble_devices(devCtrl)
if ble_chip_device is None or len(ble_chip_device) == 0:
log.info("No BLE CHIP device found")
return False
chip_device_found = False
for ble_device in ble_chip_device:
if deviceDetails is not None:
if (ble_device["name"] == name and
int(ble_device["discriminator"]) == int(deviceDetails["Discriminator"]) and
int(ble_device["vendorId"]) == int(deviceDetails["VendorID"]) and
int(ble_device["productId"]) == int(deviceDetails["ProductID"])):
chip_device_found = True
break
else:
if (ble_device["name"] == name):
chip_device_found = True
break
return chip_device_found
def connect_device_over_ble(devCtrl, discriminator, pinCode, nodeId=None):
"""
Connect to Matter accessory device over BLE
:param devCtrl: device controller instance
:param discriminator: CHIP device discriminator
:param pinCode: CHIP device pin code
:param nodeId: default value of node ID
:return: node ID is provisioning successful, otherwise None
"""
if nodeId is None:
nodeId = random.randint(1, 1000000)
try:
devCtrl.ConnectBLE(int(discriminator), int(pinCode), int(nodeId))
except exceptions.ChipStackException as ex:
log.error("Connect device over BLE failed: {}".format(str(ex)))
return None
return nodeId
def close_connection(devCtrl, nodeId):
"""
Close the BLE connection
:param devCtrl: device controller instance
:return: true if successful, otherwise false
"""
try:
devCtrl.MarkSessionDefunct(nodeId)
except exceptions.ChipStackException as ex:
log.error("Close session failed: {}".format(str(ex)))
return False
return True
def close_ble(devCtrl):
"""
Close the BLE connection
:param devCtrl: device controller instance
:return: true if successful, otherwise false
"""
try:
devCtrl.CloseBLEConnection()
except exceptions.ChipStackException as ex:
log.error("Close BLE connection failed: {}".format(str(ex)))
return False
return True
def commissioning_wifi(devCtrl, ssid, password, nodeId):
"""
Commissioning a Wi-Fi device
:param devCtrl: device controller instance
:param ssid: network ssid
:param password: network password
:param nodeId: value of node ID
:return: error code
"""
# Inject the credentials to the device
err, res = send_zcl_command(devCtrl,
"NetworkCommissioning "
"AddOrUpdateWiFiNetwork {} 0 0 "
"ssid=str:{} credentials=str:{} breadcrumb=0 timeoutMs=1000".format(
nodeId,
ssid,
password
))
if err != 0 and res["Status"] != 0:
log.error("Set Wi-Fi credentials failed [{}]".format(err))
return err
# Enable the Wi-Fi interface
err, res = send_zcl_command(
devCtrl, "NetworkCommissioning ConnectNetwork {} 0 0 networkID=str:{} breadcrumb=0 timeoutMs=1000".format(nodeId, ssid))
if err != 0 and res["Status"] != 0:
log.error("Enable Wi-Fi failed [{}]".format(err))
return err
return err
def resolve_device(devCtrl, nodeId):
"""
Discover IP address and port of the device.
:param devCtrl: device controller instance
:param nodeId: value of node ID
:return: device IP address and port if successful, otherwise None
"""
ret = None
try:
devCtrl.ResolveNode(int(nodeId))
ret = devCtrl.GetAddressAndPort(int(nodeId))
if ret is None:
log.error("Get address and port failed")
except exceptions.ChipStackException as ex:
log.error("Resolve node failed {}".format(str(ex)))
return ret