9 from __future__
import print_function
10 import import_gos_sdk_utils
11 from common.gos_jlink_cli
import gos_jlink_cli
12 from common.dict_to_object
import msgpack_to_object, object_from_dict
13 from common.third_party
import umsgpack
17 class secure_element_device(gos_jlink_cli):
19 ''' ******************************************************************************************* ''' 21 gos_jlink_cli.__init__(self)
24 ''' ******************************************************************************************* ''' 26 return self._send_cmd(
'get se.status')
29 ''' ******************************************************************************************* ''' 32 return self._send_cmd(
'reboot', timeout=0)
35 ''' ******************************************************************************************* ''' 37 return self._send_cmd(
'se_init')
40 ''' ******************************************************************************************* ''' 42 return self._send_cmd(
'se_gen_csr')
45 ''' ******************************************************************************************* ''' 46 def save_credentials(self, device_cert, signer_cert, signer_ca_pub, hostname):
48 'signer_ca_pub': bytearray(signer_ca_pub),
49 'signer_cert': bytearray(signer_cert),
50 'device_cert': bytearray(device_cert),
54 data = umsgpack.dumps(request)
56 return self._send_cmd(
'se_save_creds %d' % len(data), data)
59 ''' ******************************************************************************************* ''' 60 def _send_cmd(self, cmd, data=None, timeout=7.0):
62 print(
' Issuing cmd: %s (attempt %d of 3)' % (cmd, i+1))
65 return self._send_cmd_loop(cmd, data, timeout)
66 except Exception
as e:
71 ''' ******************************************************************************************* ''' 72 def _send_cmd_loop(self, cmd, data=None, timeout=7.0):
73 response, result_code = self.issue(cmd, data=data, exception_on_error=
False, response_data_type=
'raw', timeout=timeout)
75 if result_code == gos_jlink_cli.RESULT_UNKNOWN
or result_code == gos_jlink_cli.RESULT_UNKNOWN_OPTION:
76 raise Exception(
'Unknown command: %s, has the app been programmed to the device?' % cmd)
82 resp_obj = msgpack_to_object(response)
83 if not isinstance(resp_obj, object_from_dict):
84 raise Exception(
'Invalid msgpack response returned: %s' % str(response))
86 except Exception
as e:
87 if result_code == gos_jlink_cli.RESULT_SUCCESS:
88 raise Exception(
'Command successfully completed, but failed to convert msgpack response to object, err: %s' % e)
91 args = (result_code, gos_jlink_cli.get_cmd_result_str(result_code), e)
92 raise Exception(
'Command failed, err code:%d (%s) and also failed to convert msgpack response to object, err: %s' % args)
95 if result_code == gos_jlink_cli.RESULT_SUCCESS:
99 args = (result_code, gos_jlink_cli.get_cmd_result_str(result_code), resp_obj.msg)
100 raise Exception(
'Command failed, err code:%d (%s), err: %s' % args)