|
14 | 14 | from nrfcloud_utils.cli_helpers import ( |
15 | 15 | setup_logging, |
16 | 16 | parser_add_comms_args, |
| 17 | + user_request_open_mode, |
17 | 18 | CMD_TERM_DICT, CMD_TYPE_AUTO, CMD_TYPE_AT, CMD_TYPE_AT_SHELL, |
18 | 19 | ) |
19 | 20 | from nrfcloud_utils.device_credentials_installer import parse_mfw_ver |
@@ -152,25 +153,6 @@ def check_if_device_exists_in_csv(csv_filename, dev_id, delete_duplicates): |
152 | 153 | return duplicate_rows, row_count |
153 | 154 |
|
154 | 155 |
|
155 | | -def user_request_open_mode(filename, append): |
156 | | - mode = "a" if append else "w" |
157 | | - exists = os.path.isfile(filename) |
158 | | - if not append and exists: |
159 | | - answer = " " |
160 | | - while answer not in "yan": |
161 | | - answer = input( |
162 | | - f"--- File {filename} exists; overwrite, append, or quit (y,a,n)? " |
163 | | - ) |
164 | | - if answer == "n": |
165 | | - logger.info("File will not be overwritten") |
166 | | - return None |
167 | | - mode = "w" if answer == "y" else "a" |
168 | | - elif not exists and append: |
169 | | - mode = "w" |
170 | | - logger.warning("Append specified but file does not exist...") |
171 | | - return mode |
172 | | - |
173 | | - |
174 | 156 | def save_csv(csv_filename, append, replace, dev_id, attestation): |
175 | 157 | mode = user_request_open_mode(csv_filename, append) |
176 | 158 | if mode is None: |
@@ -239,19 +221,22 @@ def main(in_args): |
239 | 221 | if not cred_if.go_offline(): |
240 | 222 | error_exit("Failed to switch modem to offline mode") |
241 | 223 |
|
242 | | - if args.clear_sectag: |
243 | | - logger.info(f"Clearing existing credentials in sectag {args.sectag}...") |
244 | | - cred_if.delete_credential(args.sectag, 1) |
245 | | - cred_if.delete_credential(args.sectag, 2) |
246 | | - |
247 | | - logger.info(f"Generating self-signed certificate (sectag {args.sectag})...") |
248 | | - attestation = gen_self_signed_cert(cred_if, args.sectag) |
249 | | - if not attestation: |
250 | | - error_exit("Failed to generate self-signed certificate, use --clear-sectag if the slot is already occupied") |
251 | | - |
252 | | - logger.info("Returning modem to online mode...") |
253 | | - if not cred_if.at_command("AT+CFUN=1", wait_for_result=True): |
254 | | - logger.warning("Failed to return modem to online mode") |
| 224 | + try: |
| 225 | + if args.clear_sectag: |
| 226 | + logger.info(f"Clearing existing credentials in sectag {args.sectag}...") |
| 227 | + cred_if.delete_credential(args.sectag, 1) |
| 228 | + cred_if.delete_credential(args.sectag, 2) |
| 229 | + |
| 230 | + logger.info(f"Generating self-signed certificate (sectag {args.sectag})...") |
| 231 | + attestation = gen_self_signed_cert(cred_if, args.sectag) |
| 232 | + if not attestation: |
| 233 | + error_exit("Failed to generate self-signed certificate, use --clear-sectag if the slot is already occupied") |
| 234 | + finally: |
| 235 | + # Always try to return the modem to online mode, even if keygen failed, |
| 236 | + # so the user isn't left with a modem stuck in CFUN=4. |
| 237 | + logger.info("Returning modem to online mode...") |
| 238 | + if not cred_if.at_command("AT+CFUN=1", wait_for_result=True): |
| 239 | + logger.warning("Failed to return modem to online mode") |
255 | 240 |
|
256 | 241 | print(f"{dev_id},{attestation}") |
257 | 242 |
|
|
0 commit comments