From 8062d40755bc42b068f506920f3ce33234cdfaca Mon Sep 17 00:00:00 2001 From: Juha Ylinen Date: Tue, 13 Jun 2023 16:02:12 +0300 Subject: [PATCH] net: lib: nrf_provisioning: Fix AT command Fix error when AT%KEYGEN command is retried after increasing buffer size. Security tag must be cleared before new KEYGEN command. Add command to delete credential Update documentation in nrf_provisioning_at.h Signed-off-by: Juha Ylinen --- .../include/nrf_provisioning_at.h | 21 ++++++++++++------- .../src/nrf_provisioning_at.c | 5 +++++ .../src/nrf_provisioning_codec.c | 12 ++++++++++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/subsys/net/lib/nrf_provisioning/include/nrf_provisioning_at.h b/subsys/net/lib/nrf_provisioning/include/nrf_provisioning_at.h index 13c420583c34..26d39ba08a9f 100644 --- a/subsys/net/lib/nrf_provisioning/include/nrf_provisioning_at.h +++ b/subsys/net/lib/nrf_provisioning/include/nrf_provisioning_at.h @@ -53,7 +53,7 @@ bool nrf_provisioning_at_cmee_is_active(void); /** * @brief Sets mobile termination error reporting to a given state. * - * @param state Buffer to receive the time into. + * @param state State for error reporting. * * @returns Zero on success, negative error code on failure. */ @@ -62,9 +62,6 @@ int nrf_provisioning_at_cmee_control(enum nrf_provisioning_at_cmee_state state); /** * @brief Enables mobile termination error reporting state. * - * @param buff Buffer to receive the time into. - * @param size Buffer size. - * * @returns Previous state */ bool nrf_provisioning_at_cmee_enable(void); @@ -72,15 +69,25 @@ bool nrf_provisioning_at_cmee_enable(void); /** * @brief Send any AT command. * - * @param buff Buffer to receive the response into. - * @param size Buffer size. - * @param size Command to be send. + * @param resp Buffer to receive the response into. + * @param resp_sz Buffer size. + * @param cmd AT command to be send. * * @returns Zero on success, negative error code on failure or positive error code when CMEE code * needs to be checked. */ int nrf_provisioning_at_cmd(void *resp, size_t resp_sz, const char *cmd); +/** + * @brief Delete credential. + * + * @param tag The security tag of the credential. + * @param type The credential type. + * + * @returns Zero on success, negative error code on failure. + */ +int nrf_provisioning_at_del_credential(int sec_tag, int type); + #ifdef __cplusplus } #endif diff --git a/subsys/net/lib/nrf_provisioning/src/nrf_provisioning_at.c b/subsys/net/lib/nrf_provisioning/src/nrf_provisioning_at.c index 114f28606b9d..d42b11a4a735 100644 --- a/subsys/net/lib/nrf_provisioning/src/nrf_provisioning_at.c +++ b/subsys/net/lib/nrf_provisioning/src/nrf_provisioning_at.c @@ -69,3 +69,8 @@ int nrf_provisioning_at_cmd(void *resp, size_t resp_sz, const char *cmd) { return nrf_modem_at_cmd(resp, resp_sz, "%s", cmd); } + +int nrf_provisioning_at_del_credential(int tag, int type) +{ + return nrf_modem_at_printf("AT%%CMNG=3,%d,%d", tag, type); +} diff --git a/subsys/net/lib/nrf_provisioning/src/nrf_provisioning_codec.c b/subsys/net/lib/nrf_provisioning/src/nrf_provisioning_codec.c index 87fc0eaaa0ac..4e15ad25aa10 100644 --- a/subsys/net/lib/nrf_provisioning/src/nrf_provisioning_codec.c +++ b/subsys/net/lib/nrf_provisioning/src/nrf_provisioning_codec.c @@ -5,6 +5,7 @@ */ #include +#include #include #include @@ -287,10 +288,19 @@ static int exec_at_cmd(struct command *cmd_req, struct cdc_out_fmt_data *out) ret = nrf_provisioning_at_cmd(resp, resp_sz, out->at_buff); if (ret == -E2BIG) { + int tag = 0; + int type = 0; + LOG_DBG("Buffer too small for AT response, retrying"); k_free(resp); resp = NULL; - + if (sscanf(out->at_buff, "AT%%KEYGEN=%d,%d,%*s", &tag, &type) == 2) { + LOG_DBG("Clear sec_tag %d, type %d", tag, type); + ret = nrf_provisioning_at_del_credential(tag, type); + if (ret < 0) { + LOG_ERR("AT cmd failed, error: %d", ret); + } + } resp_sz *= 2; /* Previous size wasn't sufficient */ if (resp_sz > AT_RESP_MAX_SIZE) { LOG_ERR("Key or CSR too big");