demo/secure_element/resources/generation_scripts/secure_element_device.py

1 #
2 # EVALUATION AND USE OF THIS SOFTWARE IS SUBJECT TO THE TERMS AND
3 # CONDITIONS OF THE CONTROLLING LICENSE AGREEMENT FOUND AT LICENSE.md
4 # IN THIS SDK. IF YOU DO NOT AGREE TO THE LICENSE TERMS AND CONDITIONS,
5 # PLEASE RETURN ALL SOURCE FILES TO SILICON LABORATORIES.
6 # (c) Copyright 2018, Silicon Laboratories Inc. All rights reserved.
7 #
8 
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
14 
15 
16 
17 class secure_element_device(gos_jlink_cli):
18 
19  ''' ******************************************************************************************* '''
20  def __init__(self):
21  gos_jlink_cli.__init__(self)
22 
23 
24  ''' ******************************************************************************************* '''
25  def get_status(self):
26  return self._send_cmd('get se.status')
27 
28 
29  ''' ******************************************************************************************* '''
30  def reboot(self):
31  # Set the timeout to 0 so we ignore the response from this command
32  return self._send_cmd('reboot', timeout=0)
33 
34 
35  ''' ******************************************************************************************* '''
36  def init(self):
37  return self._send_cmd('se_init')
38 
39 
40  ''' ******************************************************************************************* '''
41  def gen_csr(self):
42  return self._send_cmd('se_gen_csr')
43 
44 
45  ''' ******************************************************************************************* '''
46  def save_credentials(self, device_cert, signer_cert, signer_ca_pub, hostname):
47  request = {
48  'signer_ca_pub': bytearray(signer_ca_pub),
49  'signer_cert': bytearray(signer_cert),
50  'device_cert': bytearray(device_cert),
51  'hostname': hostname
52  }
53 
54  data = umsgpack.dumps(request)
55 
56  return self._send_cmd('se_save_creds %d' % len(data), data)
57 
58 
59  ''' ******************************************************************************************* '''
60  def _send_cmd(self, cmd, data=None, timeout=7.0):
61  for i in range(3):
62  print(' Issuing cmd: %s (attempt %d of 3)' % (cmd, i+1))
63 
64  try:
65  return self._send_cmd_loop(cmd, data, timeout)
66  except Exception as e:
67  if i == 2:
68  raise e
69 
70 
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)
74 
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)
77 
78  if timeout == 0:
79  return
80 
81  try:
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))
85 
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)
89 
90  else:
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)
93 
94 
95  if result_code == gos_jlink_cli.RESULT_SUCCESS:
96  return resp_obj
97 
98  else:
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)
101