11import argparse
22import sys
33import serial
4+ import logging
45
56from nrfcredstore .exceptions import ATCommandError , NoATClientException
6- from nrfcredstore .at_client import ATClient
7+ from nrfcredstore .command_interface import ATCommandInterface
78from nrfcredstore .credstore import CredStore , CredType
9+ from nrfcredstore .comms import Comms
810
911FUN_MODE_OFFLINE = 4
1012KEY_TYPES_OR_ANY = list (map (lambda type : type .name , CredType ))
1921
2022def parse_args (in_args ):
2123 parser = argparse .ArgumentParser (description = 'Manage certificates stored in a cellular modem.' )
22- parser .add_argument ('dev' , help = 'Serial device used to communicate with the modem.' )
24+ parser .add_argument ('dev' , help = 'Device used to communicate with the modem.' )
2325 parser .add_argument ('--baudrate' , type = int , default = 115200 , help = 'Serial baudrate' )
2426 parser .add_argument ('--timeout' , type = int , default = 3 ,
2527 help = 'Serial communication timeout in seconds' )
@@ -55,6 +57,10 @@ def parse_args(in_args):
5557
5658 deleteall_parser = subparsers .add_parser ('deleteall' , help = 'Delete all keys in a secure tag' )
5759
60+ imei_parser = subparsers .add_parser ('imei' , help = 'Get IMEI of the modem' )
61+
62+ attoken_parser = subparsers .add_parser ('attoken' , help = 'Get attestation token of the modem' )
63+
5864 # add generate command and args
5965 generate_parser = subparsers .add_parser ('generate' , help = 'Generate private key' )
6066 generate_parser .add_argument ('tag' , type = int ,
@@ -68,7 +74,8 @@ def parse_args(in_args):
6874
6975def exec_cmd (args , credstore ):
7076 if args .subcommand :
71- credstore .func_mode (FUN_MODE_OFFLINE )
77+ if not credstore .func_mode (FUN_MODE_OFFLINE ):
78+ raise RuntimeError ("Failed to set modem to offline mode." )
7279
7380 if args .subcommand == 'list' :
7481 ct = CredType [args .type ]
@@ -104,30 +111,43 @@ def exec_cmd(args, credstore):
104111 credstore .keygen (args .tag , args .file , args .attributes )
105112 print (f'New private key generated in secure tag { args .tag } ' )
106113 print (f'Wrote CSR in DER format to { args .file .name } ' )
114+ elif args .subcommand == 'imei' :
115+ imei = credstore .command_interface .get_imei ()
116+ if imei is None :
117+ raise RuntimeError ("Failed to get IMEI." )
118+ print (f'IMEI: { imei } ' )
119+ elif args .subcommand == 'attoken' :
120+ attoken = credstore .command_interface .get_attestation_token ()
121+ if attoken is None :
122+ raise RuntimeError ("Failed to get attestation token." )
123+ print (f'Attestation token: { attoken } ' )
107124
108125def exit_with_msg (exitcode , msg ):
109126 print (msg )
110127 exit (exitcode )
111128
112- def main (in_args , credstore ):
113- at_client = credstore .at_client
114- try :
115- args = parse_args (in_args )
116- if args .dev :
117- at_client .connect (args .dev , args .baudrate , args .timeout )
118- at_client .verify ()
119- at_client .enable_error_codes ()
120- exec_cmd (args , credstore )
121- except NoATClientException :
122- exit_with_msg (ERR_NO_AT_CLIENT , 'The device does not respond to AT commands. Please flash at_client sample.' )
123- except ATCommandError as err :
124- exit_with_msg (ERR_AT_COMMAND , err )
125- except TimeoutError as err :
126- exit_with_msg (ERR_TIMEOUT , 'The device did not respond in time. Please try again.' )
127- except serial .SerialException as err :
128- exit_with_msg (ERR_SERIAL , f'Serial error: { err } ' )
129- except Exception as err :
130- exit_with_msg (ERR_UNKNOWN , f'Unhandled Error: { err } ' )
131-
132- def run ():
133- main (sys .argv [1 :], CredStore (ATClient (serial .Serial ())))
129+ def main (args , credstore ):
130+ credstore .command_interface .detect_shell_mode ()
131+ credstore .command_interface .enable_error_codes ()
132+ exec_cmd (args , credstore )
133+
134+ def run (argv = sys .argv ):
135+ logging .basicConfig (level = 'DEBUG' )
136+ args = parse_args (argv [1 :])
137+ comms = None
138+
139+ # use inquirer to find the device
140+ if args .dev == 'auto' :
141+ comms = Comms (list_all = True , baudrate = args .baudrate , timeout = args .timeout )
142+ elif args .dev == 'rtt' :
143+ comms = Comms (rtt = True , baudrate = args .baudrate , timeout = args .timeout )
144+ # if dev is just numbers, assume it's an rtt device
145+ elif args .dev .isdigit ():
146+ comms = Comms (rtt = True , serial_number = int (args .dev ), timeout = args .timeout )
147+ # otherwise, assume it's a serial device
148+ else :
149+ comms = Comms (port = args .dev , baudrate = args .baudrate , timeout = args .timeout )
150+
151+ cred_if = ATCommandInterface (comms )
152+
153+ main (args , CredStore (cred_if ))
0 commit comments