-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWFI32_SignerCert.py
164 lines (142 loc) · 4.93 KB
/
WFI32_SignerCert.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
import serial
import time
import subprocess
import os
# COM port setting
COM_PORT = "your_COM_Port"
class Delay_Non_Blocking:
def __init__(self):
self.isStarted = False
self.time_start = 0
def delay_time_start(self):
if self.isStarted == False:
self.time_start = time.time()
self.isStarted = True
def delay_sec_poll(self, delay_sec):
if time.time() - self.time_start > delay_sec :
self.isStarted = False
return True
else:
return False
class AnyCloud:
def __init__(self, port, baud, debug):
self.PORT = port
self.BAUD = baud
self.DEBUG = debug
self.SER_TIMEOUT = 0.1
self.ser_buf = ""
self.init_state = 0
self.app_state = 0
self.dev_cert = ""
self.wifi_connected = False
self.ser = serial.Serial(self.PORT, self.BAUD, timeout = self.SER_TIMEOUT)
self.delay = Delay_Non_Blocking()
self.evt_handler = None
def cmd_issue(self, command):
self.ser.write(bytearray(command, 'utf-8'))
def serial_receive(self):
read_val = self.ser.read(1)
if read_val != b'':
if self.DEBUG == True:
print(read_val)
self.ser_buf = self.ser_buf + read_val.decode('utf8', errors='backslashreplace')
if read_val == b'>':
ret_val = self.ser_buf
self.ser_buf = ""
return ret_val
return ""
def sm_initialize(self):
# start initialization
if self.init_state == 0:
print("\r\nStarting...\r\n")
self.init_state = 1
self.delay.delay_time_start()
if self.delay.delay_sec_poll(0.100) :
#enable serial command echo
if self.init_state == 1:
self.cmd_issue('ATE1\r\n')
self.init_state = self.init_state + 1
return self.init_state
if self.init_state == 2:
self.cmd_issue('AT+READCERT=4\r\n')
self.init_state = 10
return self.init_state
#delay until event changes init_state
elif self.init_state == 10:
# don't advance state automatically, do it from evt_handler
pass
# init complete
elif self.init_state == 254:
return self.init_state
else:
return 255
else:
return 0
def evt_init_error(self):
self.init_state = 254
print("Event: Error,stopping initialization")
def evt_read_certificate(self) :
cert_start = self.dev_cert.find("-----BEGIN CERTIFICATE-----")
cert_end = self.dev_cert.find("-----END CERTIFICATE-----")+ len("-----END CERTIFICATE-----")
#print("start: " + str(cert_start) + " end: "+ str(cert_end))
self.dev_cert = self.dev_cert[cert_start:cert_end]
#print(self.dev_cert)
cert_list = self.dev_cert.rsplit("\\n")
#print(cert_list)
cert = ""
for i in cert_list:
cert = cert + i +'\r\n'
#print(cert)
f = open("SignerCA.PEM", "w")
f.write(cert)
f.close()
#openssl x509 -in <certificate> -text
cmd_line = "openssl x509 -in SignerCA.PEM -text"
p = subprocess.Popen(cmd_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
if "Subject: O = Microchip Technology Inc," in line.decode():
index = line.decode().find("CN = ")
cn = line.decode()[(index + 5):]
cn = cn.rstrip()
if "Subject: O=Microchip Technology Inc," in line.decode():
index = line.decode().find("CN=")
cn = line.decode()[(index + 3):]
cn = cn.rstrip()
print(line.rstrip().decode())
print("\r\nThe Common Name in the signer certificate is: " + cn)
print("\r\n[*** The signer certificate has been saved into the file 'SignerCA.PEM' ***]")
self.init_state = 254 # end initialization.
def rx_data_process(self, received):
# if error setting echo on, bail
if ("ATE1" in received) and ("ERROR:" in received) :
self.evt_handler = self.evt_init_error
retval = 0 #error state
if ("+READCERT:4," in received):
self.dev_cert = received
self.evt_handler = self.evt_read_certificate
def runApp(self):
#top level app state machine
if self.app_state == 0: # start of application
print("\r\n--------------------------------------------------------------------------------")
print("Reading WFI32 Signer Certificate")
print("--------------------------------------------------------------------------------")
self.app_state = 1
elif self.app_state == 1: # init AnyCloud
init_resp = self.sm_initialize()
if init_resp == 254 :
self.app_state = 254
elif self.app_state == 254:
exit()
rx_data = self.serial_receive()
# parse received data
if rx_data != "":
print(rx_data)
self.rx_data_process(rx_data)
if self.evt_handler != None :
self.evt_handler()
self.evt_handler = None
def __del__(self):
self.ser.close()
ac = AnyCloud(COM_PORT, 230400, False)
while True:
ac.runApp()