Skip to content

Commit 6cdc5d8

Browse files
committed
Add python helper scripts for example tasks
* Setting time when the WINC1500 can't locate an NTP that responds (most often because of firewall settings) * Extract the pem formatted public key
1 parent cb0f733 commit 6cdc5d8

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

scripts/at_kit_base.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
#!/usr/bin/env python
2+
13
import re
24
import binascii
5+
import crcmod.predefined
6+
import struct
37

48
#DEVICE_HID_VID = 0x04d8
59
#DEVICE_HID_PID = 0x0f32
@@ -10,6 +14,13 @@
1014
KIT_APP_COMMAND_SET_TIME = 0
1115

1216

17+
def kit_crc(data):
18+
"""Return bytes object of the crc based on the input data bytes"""
19+
crc16 = crcmod.predefined.Crc('crc-16')
20+
crc16.update(bytes(data))
21+
return struct.pack('<H', int('{:016b}'.format(crc16.crcValue)[::-1], 2))
22+
23+
1324
class KitError(Exception):
1425
def __init__(self, error_info):
1526
self.error_code = error_info['error_code']
@@ -23,7 +34,7 @@ def __init__(self, device):
2334
self.report_size = 64
2435
self.next_app_cmd_id = 0
2536
self.app_responses = {}
26-
self.kit_reply_regex = re.compile('^([0-9a-zA-Z]{2})\\(([^)]*)\\)')
37+
self.kit_reply_regex = re.compile('([0-9a-zA-Z]{2})\\(([^)]*)\\)')
2738

2839
def open(self, vendor_id=DEVICE_HID_VID, product_id=DEVICE_HID_PID):
2940
"""Opens HID device for the Kit. Adjusts default VID/PID for the kit."""
@@ -58,7 +69,7 @@ def kit_read(self, timeout_ms=0):
5869
data = data[:data.index(10)+1] # Trim any data after the newline
5970
return ''.join(map(chr, data)) # Convert data from list of integers into string
6071

61-
def parse_kit_reply(self, data):
72+
def kit_parse_reply(self, data):
6273
"""Perform basic parsing of the kit protocol replies.
6374
6475
- XX(YYZZ...)
@@ -68,3 +79,26 @@ def parse_kit_reply(self, data):
6879
if match is None:
6980
raise ValueError('Unable to parse kit protocol reply: %s' % data)
7081
return {'status': int(match.group(1), 16), 'data': match.group(2)}
82+
83+
def kit_list(self, idx):
84+
"""Request the address of the device associated with the index"""
85+
self.kit_write('b:d', struct.pack("<B", idx))
86+
return int(self.kit_parse_reply(self.kit_read(0))['data'], 16)
87+
88+
def kit_select(self, dev):
89+
"""Select the device with the given address"""
90+
self.kit_write('e:p:s', struct.pack("<B", dev))
91+
return self.kit_read(0)
92+
93+
def kit_command(self, opcode, param1, param2, data=b'', timeout_ms=0):
94+
l2s = len(data) + 7 # length(1) + opcode(1) + param1(1) + param2(2) + crc(2)
95+
# Make Packet
96+
d2s = struct.pack("<BBBH", l2s, opcode, param1, param2)
97+
# Append CRC
98+
d2s += kit_crc(d2s)
99+
# Send the command
100+
self.kit_write('e:t', d2s)
101+
# Get the response
102+
resp = self.kit_read(timeout_ms=timeout_ms)
103+
# Parse the response
104+
return self.kit_parse_reply(resp)

scripts/at_pubkey.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
import hid
3+
from at_kit_base import *
4+
import base64
5+
6+
# Initialize the Kit Instance
7+
dev = KitDevice(hid.device())
8+
dev.open()
9+
10+
id = dev.kit_list(0)
11+
12+
# Select the device to communicate with
13+
dev.kit_select(id)
14+
15+
# Send the command
16+
resp = dev.kit_command(64, 0, 0, timeout_ms=5000)
17+
18+
public_key = bytearray.fromhex('3059301306072A8648CE3D020106082A8648CE3D03010703420004') + bytearray.fromhex(resp['data'][2:-4])
19+
public_key = '-----BEGIN PUBLIC KEY-----\n' + base64.b64encode(public_key).decode('ascii') + '\n-----END PUBLIC KEY-----'
20+
21+
print(public_key)

scripts/at_set_time.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python3
1+
#!/usr/bin/env python
22

33
import hid
44
import struct

scripts/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
crcmod
2+
cryptography
3+
hidapi

0 commit comments

Comments
 (0)