Skip to content

Commit 230cc51

Browse files
committed
workaround for write command
1 parent 68bc537 commit 230cc51

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/nrfcredstore/command_interface.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,13 @@ def enable_error_codes(self):
113113
def at_command(self, at_command, wait_for_result=False, suppress_errors=False):
114114
"""Write an AT command to the command interface. Optionally wait for OK"""
115115

116-
# AT commands are written directly as-is with the ATCommandInterface:
117-
at_cmd_prefix = 'at ' if self.shell else ''
118-
self.write_raw(f'{at_cmd_prefix}{at_command}')
116+
if self.shell:
117+
# transform line endings to match shell expectations
118+
at_command = at_command.replace("\r", "")
119+
at_command = at_command.replace("\n", "\\n")
120+
self.write_raw("at '" + at_command + "'")
121+
else:
122+
self.write_raw(at_command)
119123

120124
if wait_for_result:
121125
result, _ = self.comms.expect_response("OK", "ERROR", suppress_errors=suppress_errors)

src/nrfcredstore/comms.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,10 @@ def expect_response(self, ok_str=None, error_str=None, store_str=None, timeout=1
344344
line = self.read_line()
345345
if line:
346346
line = line.strip()
347-
if ok_str and ok_str == ansi_escape.sub('', line):
347+
line = ansi_escape.sub('', line) # remove ANSI escape codes
348+
if ok_str and ok_str == line:
348349
return (True, output)
349-
if error_str and error_str == ansi_escape.sub('', line):
350+
if error_str and error_str == line:
350351
return (False, output)
351352
if line.startswith('+CME ERROR'):
352353
code = int(line.replace('+CME ERROR: ', ''))

src/nrfcredstore/credstore.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def list(self, tag = None, type: CredType = CredType.ANY) -> List[Credential]:
5858
cmd = f'{cmd},{CredType(type).value}'
5959

6060
self.command_interface.at_command(cmd, wait_for_result=False)
61-
result, response = self.command_interface.comms.expect_response("OK", "ERROR", "%CMNG")
61+
result, response = self.command_interface.comms.expect_response("OK", "ERROR", "%CMNG: ")
6262
if not result:
6363
raise RuntimeError("Failed to list credentials")
6464
response_lines = response.splitlines()
@@ -101,12 +101,14 @@ def keygen(self, tag: int, file: io.BufferedIOBase, attributes: str = ''):
101101
cmd = f'{cmd},"{attributes}"'
102102

103103
self.command_interface.at_command(cmd, wait_for_result=False)
104-
result, response = self.command_interface.comms.expect_response("OK", "ERROR", "%KEYGEN")
104+
result, response = self.command_interface.comms.expect_response("OK", "ERROR", "%KEYGEN: ")
105105

106-
if result:
107-
keygen_output = response.replace('%KEYGEN: "', '')
108-
csr_der_b64 = keygen_output.split('.')[0]
109-
csr_der_bytes = base64.urlsafe_b64decode(csr_der_b64 + '===')
110-
file.write(csr_der_bytes)
106+
if not result:
107+
raise RuntimeError("Failed to generate key")
108+
109+
keygen_output = response.replace('%KEYGEN: "', '')
110+
csr_der_b64 = keygen_output.split('.')[0]
111+
csr_der_bytes = base64.urlsafe_b64decode(csr_der_b64 + '===')
112+
file.write(csr_der_bytes)
111113

112114
file.close()

0 commit comments

Comments
 (0)