diff --git a/subsys/mgmt/osdp/src/osdp_cp.c b/subsys/mgmt/osdp/src/osdp_cp.c index d60aebeaa82..1e1716e0b7a 100644 --- a/subsys/mgmt/osdp/src/osdp_cp.c +++ b/subsys/mgmt/osdp/src/osdp_cp.c @@ -10,8 +10,6 @@ LOG_MODULE_DECLARE(osdp, CONFIG_OSDP_LOG_LEVEL); #include "osdp_common.h" -#define TAG "CP: " - #define OSDP_PD_POLL_TIMEOUT_MS (1000 / CONFIG_OSDP_PD_POLL_RATE) #define OSDP_CMD_RETRY_WAIT_MS (CONFIG_OSDP_CMD_RETRY_WAIT_SEC * 1000) @@ -50,11 +48,16 @@ LOG_MODULE_DECLARE(osdp, CONFIG_OSDP_LOG_LEVEL); #define REPLY_FMT_DATA_LEN 3 /* variable length command */ #define REPLY_BUSY_DATA_LEN 0 -#define OSDP_CP_ERR_GENERIC -1 -#define OSDP_CP_ERR_NO_DATA 1 -#define OSDP_CP_ERR_RETRY_CMD 2 -#define OSDP_CP_ERR_CAN_YIELD 3 -#define OSDP_CP_ERR_INPROG 4 +enum osdp_cp_error_e { + OSDP_CP_ERR_NONE = 0, + OSDP_CP_ERR_GENERIC = -1, + OSDP_CP_ERR_NO_DATA = -2, + OSDP_CP_ERR_RETRY_CMD = -3, + OSDP_CP_ERR_CAN_YIELD = -4, + OSDP_CP_ERR_INPROG = -5, + OSDP_CP_ERR_UNKNOWN = -6, +}; + int osdp_extract_address(int *address) { @@ -77,18 +80,11 @@ int osdp_extract_address(int *address) return (pd_offset == CONFIG_OSDP_NUM_CONNECTED_PD) ? 0 : -1; } -/** - * Returns: - * +ve: length of command - * -ve: error - */ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len) { struct osdp_cmd *cmd = NULL; - int data_off, i, ret = -1, len = 0; - - data_off = osdp_phy_packet_get_data_offset(pd, buf); - + int i, ret = -1, len = 0; + int data_off = osdp_phy_packet_get_data_offset(pd, buf); #ifdef CONFIG_OSDP_SC_ENABLED uint8_t *smb = osdp_phy_packet_get_smb(pd, buf); #endif @@ -218,8 +214,8 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len) #ifdef CONFIG_OSDP_SC_ENABLED case CMD_KEYSET: if (!ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE)) { - LOG_ERR(TAG "Cannot perform KEYSET without SC!"); - return -1; + LOG_ERR("Cannot perform KEYSET without SC!"); + return OSDP_CP_ERR_GENERIC; } if (max_len < CMD_KEYSET_LEN) { break; @@ -240,8 +236,9 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len) smb[1] = SCS_11; /* type */ smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1; buf[len++] = pd->cmd_id; - for (i = 0; i < 8; i++) + for (i = 0; i < 8; i++) { buf[len++] = pd->sc.cp_random[i]; + } ret = 0; break; case CMD_SCRYPT: @@ -253,13 +250,14 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len) smb[1] = SCS_13; /* type */ smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1; buf[len++] = pd->cmd_id; - for (i = 0; i < 16; i++) + for (i = 0; i < 16; i++) { buf[len++] = pd->sc.cp_cryptogram[i]; + } ret = 0; break; #endif /* CONFIG_OSDP_SC_ENABLED */ default: - LOG_ERR(TAG "Unknown/Unsupported command %02x", pd->cmd_id); + LOG_ERR("Unknown/Unsupported CMD(%02x)", pd->cmd_id); return OSDP_CP_ERR_GENERIC; } @@ -275,7 +273,7 @@ static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len) } #endif /* CONFIG_OSDP_SC_ENABLED */ if (ret < 0) { - LOG_ERR(TAG "Unable to build command %02x", pd->cmd_id); + LOG_ERR("Unable to build CMD(%02x)", pd->cmd_id); return OSDP_CP_ERR_GENERIC; } @@ -301,14 +299,15 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len) if (len != REPLY_ACK_DATA_LEN) { break; } - ret = 0; + ret = OSDP_CP_ERR_NONE; break; case REPLY_NAK: if (len != REPLY_NAK_DATA_LEN) { break; } - LOG_ERR(TAG "PD replied with NAK code %d", buf[pos]); - ret = 0; + LOG_WRN("PD replied with NAK(%d) for CMD(%02x)", + buf[pos], pd->cmd_id); + ret = OSDP_CP_ERR_NONE; break; case REPLY_PDID: if (len != REPLY_PDID_DATA_LEN) { @@ -321,29 +320,31 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len) pd->id.model = buf[pos++]; pd->id.version = buf[pos++]; - pd->id.serial_number = buf[pos++]; + pd->id.serial_number = buf[pos++]; pd->id.serial_number |= buf[pos++] << 8; pd->id.serial_number |= buf[pos++] << 16; pd->id.serial_number |= buf[pos++] << 24; - pd->id.firmware_version = buf[pos++] << 16; + pd->id.firmware_version = buf[pos++] << 16; pd->id.firmware_version |= buf[pos++] << 8; pd->id.firmware_version |= buf[pos++]; - ret = 0; + ret = OSDP_CP_ERR_NONE; break; case REPLY_PDCAP: if ((len % REPLY_PDCAP_ENTITY_LEN) != 0) { - break; + LOG_ERR("PDCAP response length is not a multiple of 3"); + return OSDP_CP_ERR_GENERIC; } while (pos < len) { t1 = buf[pos++]; /* func_code */ if (t1 >= OSDP_PD_CAP_SENTINEL) { break; } - pd->cap[t1].function_code = t1; + pd->cap[t1].function_code = t1; pd->cap[t1].compliance_level = buf[pos++]; - pd->cap[t1].num_items = buf[pos++]; + pd->cap[t1].num_items = buf[pos++]; } + /* post-capabilities hooks */ t2 = OSDP_PD_CAP_COMMUNICATION_SECURITY; if (pd->cap[t2].compliance_level & 0x01) { @@ -351,7 +352,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len) } else { CLEAR_FLAG(pd, PD_FLAG_SC_CAPABLE); } - ret = 0; + ret = OSDP_CP_ERR_NONE; break; case REPLY_LSTATR: if (len != REPLY_LSTATR_DATA_LEN) { @@ -367,7 +368,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len) } else { CLEAR_FLAG(pd, PD_FLAG_POWER); } - ret = 0; + ret = OSDP_CP_ERR_NONE; break; case REPLY_RSTATR: if (len != REPLY_RSTATR_DATA_LEN) { @@ -378,27 +379,27 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len) } else { CLEAR_FLAG(pd, PD_FLAG_R_TAMPER); } - ret = 0; + ret = OSDP_CP_ERR_NONE; break; case REPLY_COM: if (len != REPLY_COM_DATA_LEN) { break; } t1 = buf[pos++]; - temp32 = buf[pos++]; + temp32 = buf[pos++]; temp32 |= buf[pos++] << 8; temp32 |= buf[pos++] << 16; temp32 |= buf[pos++] << 24; - LOG_WRN(TAG "COMSET responded with ID:%d baud:%d", t1, temp32); + LOG_WRN("COMSET responded with ID:%d Baud:%d", t1, temp32); pd->address = t1; pd->baud_rate = temp32; - ret = 0; + ret = OSDP_CP_ERR_NONE; break; case REPLY_KEYPPAD: if (len < REPLY_KEYPPAD_DATA_LEN) { break; } - pos++; /* reader number; skip */ + pos++; /* reader number; skip */ t1 = buf[pos++]; /* key length */ if ((len - REPLY_KEYPPAD_DATA_LEN) != t1) { break; @@ -409,13 +410,13 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len) cp->notifier.keypress(pd->offset, t2); } } - ret = 0; + ret = OSDP_CP_ERR_NONE; break; case REPLY_RAW: if (len < REPLY_RAW_DATA_LEN) { break; } - pos++; /* reader number; skip */ + pos++; /* reader number; skip */ t1 = buf[pos++]; /* format */ t2 = buf[pos++]; /* length LSB */ t2 |= buf[pos++] << 8; /* length MSB */ @@ -425,7 +426,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len) if (cp->notifier.cardread) { cp->notifier.cardread(pd->offset, t1, buf + pos, t2); } - ret = 0; + ret = OSDP_CP_ERR_NONE; break; case REPLY_FMT: if (len < REPLY_FMT_DATA_LEN) { @@ -441,7 +442,7 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len) cp->notifier.cardread(pd->offset, OSDP_CARD_FMT_ASCII, buf + pos, t1); } - ret = 0; + ret = OSDP_CP_ERR_NONE; break; case REPLY_BUSY: /* PD busy; signal upper layer to retry command */ @@ -466,10 +467,10 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len) } osdp_compute_session_keys(TO_CTX(pd)); if (osdp_verify_pd_cryptogram(pd) != 0) { - LOG_ERR(TAG "failed to verify PD_crypt"); - return -1; + LOG_ERR("Failed to verify PD cryptogram"); + return OSDP_CP_ERR_GENERIC; } - ret = 0; + ret = OSDP_CP_ERR_NONE; break; case REPLY_RMAC_I: if (len != REPLY_RMAC_I_DATA_LEN) { @@ -479,22 +480,17 @@ static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len) pd->sc.r_mac[i] = buf[pos++]; } SET_FLAG(pd, PD_FLAG_SC_ACTIVE); - ret = 0; + ret = OSDP_CP_ERR_NONE; break; #endif /* CONFIG_OSDP_SC_ENABLED */ default: - LOG_DBG(TAG "unexpected reply: 0x%02x", pd->reply_id); - return OSDP_CP_ERR_GENERIC; - } - - if (ret == OSDP_CP_ERR_GENERIC) { - LOG_ERR(TAG "REPLY %02x for CMD %02x format error!", + LOG_WRN("Unexpected REPLY(%02x) for CMD(%02x)", pd->cmd_id, pd->reply_id); - return OSDP_CP_ERR_GENERIC; + return OSDP_CP_ERR_UNKNOWN; } if (pd->cmd_id != CMD_POLL) { - LOG_DBG(TAG "CMD: %02x REPLY: %02x", pd->cmd_id, pd->reply_id); + LOG_DBG("CMD(%02x) REPLY(%02x)", pd->cmd_id, pd->reply_id); } return ret; @@ -507,32 +503,36 @@ static int cp_send_command(struct osdp_pd *pd) /* init packet buf with header */ len = osdp_phy_packet_init(pd, pd->rx_buf, sizeof(pd->rx_buf)); if (len < 0) { - return -1; + return OSDP_CP_ERR_GENERIC; } /* fill command data */ ret = cp_build_command(pd, pd->rx_buf, sizeof(pd->rx_buf)); if (ret < 0) { - return -1; + return OSDP_CP_ERR_GENERIC; } len += ret; /* finalize packet */ len = osdp_phy_packet_finalize(pd, pd->rx_buf, len, sizeof(pd->rx_buf)); if (len < 0) { - return -1; + return OSDP_CP_ERR_GENERIC; } ret = pd->channel.send(pd->channel.data, pd->rx_buf, len); + if (ret != len) { + LOG_ERR("Channel send for %d bytes failed! ret: %d", len, ret); + return OSDP_CP_ERR_GENERIC; + } if (IS_ENABLED(CONFIG_OSDP_PACKET_TRACE)) { if (pd->cmd_id != CMD_POLL) { - LOG_DBG(TAG "bytes sent"); + LOG_DBG("bytes sent"); osdp_dump(NULL, pd->rx_buf, len); } } - return (ret == len) ? 0 : -1; + return OSDP_CP_ERR_NONE; } static int cp_process_reply(struct osdp_pd *pd) @@ -551,7 +551,7 @@ static int cp_process_reply(struct osdp_pd *pd) if (IS_ENABLED(CONFIG_OSDP_PACKET_TRACE)) { if (pd->cmd_id != CMD_POLL) { - LOG_DBG(TAG "bytes received"); + LOG_DBG("bytes received"); osdp_dump(NULL, pd->rx_buf, pd->rx_buf_len); } } @@ -559,7 +559,7 @@ static int cp_process_reply(struct osdp_pd *pd) /* Valid OSDP packet in buffer */ ret = osdp_phy_decode_packet(pd, pd->rx_buf, pd->rx_buf_len); if (ret == OSDP_ERR_PKT_FMT) { - return -1; /* fatal errors */ + return OSDP_CP_ERR_GENERIC; /* fatal errors */ } else if (ret == OSDP_ERR_PKT_WAIT) { /* rx_buf_len != pkt->len; wait for more data */ return OSDP_CP_ERR_NO_DATA; @@ -612,12 +612,9 @@ static void cp_reset_channel(struct osdp_pd *pd) } } -/** - * Note: This method must not dequeue cmd unless it reaches an invalid state. - */ static int cp_phy_state_update(struct osdp_pd *pd) { - int ret = OSDP_CP_ERR_INPROG, tmp; + int rc, ret = OSDP_CP_ERR_CAN_YIELD; struct osdp_cmd *cmd = NULL; switch (pd->phy_state) { @@ -626,7 +623,7 @@ static int cp_phy_state_update(struct osdp_pd *pd) break; case OSDP_CP_PHY_STATE_IDLE: if (osdp_cmd_dequeue(pd, &cmd)) { - ret = 0; + ret = OSDP_CP_ERR_NONE; /* command queue is empty */ break; } pd->cmd_id = cmd->id; @@ -636,34 +633,35 @@ static int cp_phy_state_update(struct osdp_pd *pd) /* fall-thru */ case OSDP_CP_PHY_STATE_SEND_CMD: if ((cp_send_command(pd)) < 0) { - LOG_ERR(TAG "send command error"); + LOG_ERR("Failed to send CMD(%d)", pd->cmd_id); pd->phy_state = OSDP_CP_PHY_STATE_ERR; ret = OSDP_CP_ERR_GENERIC; break; } + ret = OSDP_CP_ERR_INPROG; pd->phy_state = OSDP_CP_PHY_STATE_REPLY_WAIT; pd->rx_buf_len = 0; /* reset buf_len for next use */ pd->phy_tstamp = osdp_millis_now(); break; case OSDP_CP_PHY_STATE_REPLY_WAIT: - tmp = cp_process_reply(pd); - if (tmp == 0) { /* success */ + rc = cp_process_reply(pd); + if (rc == OSDP_CP_ERR_NONE) { pd->phy_state = OSDP_CP_PHY_STATE_CLEANUP; break; } - if (tmp == OSDP_CP_ERR_RETRY_CMD) { - LOG_INF(TAG "PD busy; retry last command"); + if (rc == OSDP_CP_ERR_RETRY_CMD) { + LOG_INF("PD busy; retry last command"); pd->phy_tstamp = osdp_millis_now(); pd->phy_state = OSDP_CP_PHY_STATE_WAIT; - ret = 2; break; } - if (tmp == OSDP_CP_ERR_GENERIC) { + if (rc == OSDP_CP_ERR_GENERIC) { pd->phy_state = OSDP_CP_PHY_STATE_ERR; + ret = OSDP_CP_ERR_GENERIC; break; } if (osdp_millis_since(pd->phy_tstamp) > OSDP_RESP_TOUT_MS) { - LOG_ERR(TAG "CMD: %02x - response timeout", pd->cmd_id); + LOG_ERR("CMD: %02x - response timeout", pd->cmd_id); pd->phy_state = OSDP_CP_PHY_STATE_ERR; } break; @@ -681,26 +679,19 @@ static int cp_phy_state_update(struct osdp_pd *pd) break; case OSDP_CP_PHY_STATE_CLEANUP: pd->phy_state = OSDP_CP_PHY_STATE_IDLE; - ret = OSDP_CP_ERR_CAN_YIELD; /* in between commands */ break; } return ret; } -/** - * Returns: - * 0: nothing done - * 1: dispatched - * -1: error - */ static int cp_cmd_dispatcher(struct osdp_pd *pd, int cmd) { struct osdp_cmd *c; if (ISSET_FLAG(pd, PD_FLAG_AWAIT_RESP)) { CLEAR_FLAG(pd, PD_FLAG_AWAIT_RESP); - return 0; + return OSDP_CP_ERR_NONE; /* nothing to be done here */ } c = osdp_cmd_alloc(pd); @@ -716,12 +707,13 @@ static int cp_cmd_dispatcher(struct osdp_pd *pd, int cmd) static int state_update(struct osdp_pd *pd) { - int phy_state, soft_fail; + int phy_state; + bool soft_fail; phy_state = cp_phy_state_update(pd); if (phy_state == OSDP_CP_ERR_INPROG || phy_state == OSDP_CP_ERR_CAN_YIELD) { - return OSDP_CP_ERR_GENERIC; + return phy_state; } /* Certain states can fail without causing PD offline */ @@ -729,8 +721,9 @@ static int state_update(struct osdp_pd *pd) /* phy state error -- cleanup */ if (pd->state != OSDP_CP_STATE_OFFLINE && - phy_state == OSDP_CP_ERR_GENERIC && soft_fail == 0) { + phy_state == OSDP_CP_ERR_GENERIC && !soft_fail) { cp_set_offline(pd); + return OSDP_CP_ERR_CAN_YIELD; } /* command queue is empty and last command was successful */ @@ -738,10 +731,10 @@ static int state_update(struct osdp_pd *pd) switch (pd->state) { case OSDP_CP_STATE_ONLINE: #ifdef CONFIG_OSDP_SC_ENABLED - if (ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE) == false && - ISSET_FLAG(pd, PD_FLAG_SC_CAPABLE) == true && + if (ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE) == false && + ISSET_FLAG(pd, PD_FLAG_SC_CAPABLE) == true && osdp_millis_since(pd->sc_tstamp) > OSDP_PD_SC_RETRY_MS) { - LOG_INF("retry SC after retry timeout"); + LOG_INF("Retry SC after retry timeout"); cp_set_state(pd, OSDP_CP_STATE_SC_INIT); break; } @@ -760,21 +753,25 @@ static int state_update(struct osdp_pd *pd) break; case OSDP_CP_STATE_INIT: cp_set_state(pd, OSDP_CP_STATE_IDREQ); - /* FALLTHRU */ + __fallthrough; case OSDP_CP_STATE_IDREQ: if (cp_cmd_dispatcher(pd, CMD_ID) != 0) { break; } if (pd->reply_id != REPLY_PDID) { + LOG_ERR("Unexpected REPLY(%02x) for cmd " + STRINGIFY(CMD_CAP), pd->reply_id); cp_set_offline(pd); } cp_set_state(pd, OSDP_CP_STATE_CAPDET); - /* FALLTHRU */ + __fallthrough; case OSDP_CP_STATE_CAPDET: if (cp_cmd_dispatcher(pd, CMD_CAP) != 0) { break; } if (pd->reply_id != REPLY_PDCAP) { + LOG_ERR("Unexpected REPLY(%02x) for cmd " + STRINGIFY(CMD_CAP), pd->reply_id); cp_set_offline(pd); } #ifdef CONFIG_OSDP_SC_ENABLED @@ -791,14 +788,14 @@ static int state_update(struct osdp_pd *pd) case OSDP_CP_STATE_SC_INIT: osdp_sc_init(pd); cp_set_state(pd, OSDP_CP_STATE_SC_CHLNG); - /* FALLTHRU */ + __fallthrough; case OSDP_CP_STATE_SC_CHLNG: if (cp_cmd_dispatcher(pd, CMD_CHLNG) != 0) { break; } if (phy_state < 0) { if (ISSET_FLAG(pd, PD_FLAG_SC_SCBKD_DONE)) { - LOG_INF(TAG "SC Failed; online without SC"); + LOG_INF("SC Failed. Online without SC"); pd->sc_tstamp = osdp_millis_now(); cp_set_state(pd, OSDP_CP_STATE_ONLINE); break; @@ -807,33 +804,33 @@ static int state_update(struct osdp_pd *pd) SET_FLAG(pd, PD_FLAG_SC_SCBKD_DONE); cp_set_state(pd, OSDP_CP_STATE_SC_INIT); pd->phy_state = 0; /* soft reset phy state */ - LOG_WRN(TAG "SC Failed; retry with SCBK-D"); + LOG_WRN("SC Failed. Retry with SCBK-D"); break; } if (pd->reply_id != REPLY_CCRYPT) { - LOG_ERR(TAG "CHLNG failed. Online without SC"); + LOG_ERR("CHLNG failed. Online without SC"); pd->sc_tstamp = osdp_millis_now(); cp_set_state(pd, OSDP_CP_STATE_ONLINE); break; } cp_set_state(pd, OSDP_CP_STATE_SC_SCRYPT); - /* FALLTHRU */ + __fallthrough; case OSDP_CP_STATE_SC_SCRYPT: if (cp_cmd_dispatcher(pd, CMD_SCRYPT) != 0) { break; } if (pd->reply_id != REPLY_RMAC_I) { - LOG_ERR(TAG "SCRYPT failed. Online without SC"); + LOG_ERR("SCRYPT failed. Online without SC"); pd->sc_tstamp = osdp_millis_now(); cp_set_state(pd, OSDP_CP_STATE_ONLINE); break; } if (ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD)) { - LOG_WRN(TAG "SC ACtive with SCBK-D; Set SCBK"); + LOG_WRN("SC ACtive with SCBK-D. Set SCBK"); cp_set_state(pd, OSDP_CP_STATE_SET_SCBK); break; } - LOG_INF(TAG "SC Active"); + LOG_INF("SC Active"); pd->sc_tstamp = osdp_millis_now(); cp_set_state(pd, OSDP_CP_STATE_ONLINE); break; @@ -842,11 +839,11 @@ static int state_update(struct osdp_pd *pd) break; } if (pd->reply_id == REPLY_NAK) { - LOG_WRN(TAG "Failed to set SCBK; continue with SCBK-D"); + LOG_WRN("Failed to set SCBK; continue with SCBK-D"); cp_set_state(pd, OSDP_CP_STATE_ONLINE); break; } - LOG_INF(TAG "SCBK set; restarting SC to verify new SCBK"); + LOG_INF("SCBK set; restarting SC to verify new SCBK"); CLEAR_FLAG(pd, PD_FLAG_SC_USE_SCBKD); CLEAR_FLAG(pd, PD_FLAG_SC_ACTIVE); cp_set_state(pd, OSDP_CP_STATE_SC_INIT); @@ -857,7 +854,7 @@ static int state_update(struct osdp_pd *pd) break; } - return 0; + return OSDP_CP_ERR_CAN_YIELD; } #ifdef CONFIG_OSDP_SC_ENABLED @@ -869,7 +866,7 @@ static int osdp_cp_send_command_keyset(struct osdp_cmd_keyset *cmd) struct osdp *ctx = osdp_get_ctx(); if (osdp_get_sc_status_mask() != PD_MASK(ctx)) { - LOG_WRN(TAG "CMD_KEYSET can be sent only when all PDs are " + LOG_WRN("CMD_KEYSET can be sent only when all PDs are " "ONLINE and SC_ACTIVE."); return 1; } @@ -906,7 +903,7 @@ int osdp_setup(struct osdp *ctx, uint8_t *key) #ifdef CONFIG_OSDP_SC_ENABLED if (key == NULL) { - LOG_ERR(TAG "Master key cannot be null"); + LOG_ERR("Master key cannot be null"); return -1; } memcpy(ctx->sc_master_key, key, 16); @@ -942,11 +939,11 @@ int osdp_cp_send_command(int pd, struct osdp_cmd *cmd) int cmd_id; if (pd < 0 || pd >= NUM_PD(ctx)) { - LOG_ERR(TAG "Invalid PD number"); + LOG_ERR("Invalid PD number"); return -1; } if (TO_PD(ctx, pd)->state != OSDP_CP_STATE_ONLINE) { - LOG_WRN(TAG "PD not online"); + LOG_WRN("PD not online"); return -1; } @@ -971,7 +968,7 @@ int osdp_cp_send_command(int pd, struct osdp_cmd *cmd) return osdp_cp_send_command_keyset(&cmd->keyset); #endif default: - LOG_ERR(TAG "Invalid command ID %d", cmd->id); + LOG_ERR("Invalid CMD_ID:%d", cmd->id); return -1; } diff --git a/subsys/mgmt/osdp/src/osdp_pd.c b/subsys/mgmt/osdp/src/osdp_pd.c index 0736728c86a..874e5df6427 100644 --- a/subsys/mgmt/osdp/src/osdp_pd.c +++ b/subsys/mgmt/osdp/src/osdp_pd.c @@ -12,8 +12,6 @@ LOG_MODULE_DECLARE(osdp, CONFIG_OSDP_LOG_LEVEL); #include "osdp_common.h" -#define TAG "PD: " - #define CMD_POLL_DATA_LEN 0 #define CMD_LSTAT_DATA_LEN 0 #define CMD_ISTAT_DATA_LEN 0 @@ -41,6 +39,14 @@ LOG_MODULE_DECLARE(osdp, CONFIG_OSDP_LOG_LEVEL); #define REPLY_CCRYPT_LEN 33 #define REPLY_RMAC_I_LEN 17 +enum osdp_pd_error_e { + OSDP_PD_ERR_NONE = 0, + OSDP_PD_ERR_NO_DATA = -1, + OSDP_PD_ERR_GENERIC = -2, + OSDP_PD_ERR_REPLY = -3, + OSDP_PD_ERR_IGNORE = -4, +}; + static struct osdp_pd_id osdp_pd_id = { .version = CONFIG_OSDP_PD_ID_VERSION, .model = CONFIG_OSDP_PD_ID_MODEL, @@ -105,9 +111,10 @@ static struct osdp_pd_cap osdp_pd_cap[] = { { -1, 0, 0 } /* Sentinel */ }; -static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) +static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) { - int i, ret = -1, pos = 0; + int ret = OSDP_PD_ERR_GENERIC; + int i, pos = 0; struct osdp_cmd *cmd; pd->reply_id = 0; @@ -120,35 +127,35 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) break; } pd->reply_id = REPLY_ACK; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_LSTAT: if (len != CMD_LSTAT_DATA_LEN) { break; } pd->reply_id = REPLY_LSTATR; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_ISTAT: if (len != CMD_ISTAT_DATA_LEN) { break; } pd->reply_id = REPLY_ISTATR; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_OSTAT: if (len != CMD_OSTAT_DATA_LEN) { break; } pd->reply_id = REPLY_OSTATR; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_RSTAT: if (len != CMD_RSTAT_DATA_LEN) { break; } pd->reply_id = REPLY_RSTATR; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_ID: if (len != CMD_ID_DATA_LEN) { @@ -156,7 +163,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) } pos++; /* Skip reply type info. */ pd->reply_id = REPLY_PDID; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_CAP: if (len != CMD_CAP_DATA_LEN) { @@ -164,7 +171,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) } pos++; /* Skip reply type info. */ pd->reply_id = REPLY_PDCAP; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_OUT: if (len != CMD_OUT_DATA_LEN) { @@ -172,7 +179,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) } cmd = osdp_cmd_alloc(pd); if (cmd == NULL) { - LOG_ERR(TAG "cmd alloc error"); + LOG_ERR("cmd alloc error"); break; } cmd->id = OSDP_CMD_OUTPUT; @@ -182,7 +189,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) cmd->output.timer_count |= buf[pos++] << 8; osdp_cmd_enqueue(pd, cmd); pd->reply_id = REPLY_ACK; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_LED: if (len != CMD_LED_DATA_LEN) { @@ -190,7 +197,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) } cmd = osdp_cmd_alloc(pd); if (cmd == NULL) { - LOG_ERR(TAG "cmd alloc error"); + LOG_ERR("cmd alloc error"); break; } cmd->id = OSDP_CMD_LED; @@ -212,7 +219,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) cmd->led.permanent.off_color = buf[pos++]; osdp_cmd_enqueue(pd, cmd); pd->reply_id = REPLY_ACK; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_BUZ: if (len != CMD_BUZ_DATA_LEN) { @@ -220,7 +227,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) } cmd = osdp_cmd_alloc(pd); if (cmd == NULL) { - LOG_ERR(TAG "cmd alloc error"); + LOG_ERR("cmd alloc error"); break; } cmd->id = OSDP_CMD_BUZZER; @@ -231,7 +238,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) cmd->buzzer.rep_count = buf[pos++]; osdp_cmd_enqueue(pd, cmd); pd->reply_id = REPLY_ACK; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_TEXT: if (len < CMD_TEXT_DATA_LEN) { @@ -239,7 +246,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) } cmd = osdp_cmd_alloc(pd); if (cmd == NULL) { - LOG_ERR(TAG "cmd alloc error"); + LOG_ERR("cmd alloc error"); break; } cmd->id = OSDP_CMD_TEXT; @@ -260,7 +267,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) } osdp_cmd_enqueue(pd, cmd); pd->reply_id = REPLY_ACK; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case CMD_COMSET: if (len != CMD_COMSET_DATA_LEN) { @@ -268,7 +275,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) } cmd = osdp_cmd_alloc(pd); if (cmd == NULL) { - LOG_ERR(TAG "cmd alloc error"); + LOG_ERR("cmd alloc error"); break; } cmd->id = OSDP_CMD_COMSET; @@ -281,18 +288,18 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) (cmd->comset.baud_rate != 9600 && cmd->comset.baud_rate != 38400 && cmd->comset.baud_rate != 115200)) { - LOG_ERR(TAG "COMSET Failed! command discarded"); + LOG_ERR("COMSET Failed! command discarded"); cmd->comset.address = pd->address; cmd->comset.baud_rate = pd->baud_rate; } osdp_cmd_enqueue(pd, cmd); pd->reply_id = REPLY_COM; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; #ifdef CONFIG_OSDP_SC_ENABLED case CMD_KEYSET: if (len != CMD_KEYSET_DATA_LEN) { - LOG_ERR(TAG "CMD_KEYSET length mismatch! %d/18", len); + LOG_ERR("CMD_KEYSET length mismatch! %d/18", len); break; } /** @@ -302,18 +309,18 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) if (ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE) == 0) { pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_SC_COND; - LOG_ERR(TAG "Keyset with SC inactive"); + LOG_ERR("Keyset with SC inactive"); break; } /* only key_type == 1 (SCBK) and key_len == 16 is supported */ if (buf[pos] != 1 || buf[pos + 1] != 16) { - LOG_ERR(TAG "Keyset invalid len/type: %d/%d", - buf[pos], buf[pos + 1]); + LOG_ERR("Keyset invalid len/type: %d/%d", + buf[pos], buf[pos + 1]); break; } cmd = osdp_cmd_alloc(pd); if (cmd == NULL) { - LOG_ERR(TAG "cmd alloc error"); + LOG_ERR("cmd alloc error"); break; } cmd->id = OSDP_CMD_KEYSET; @@ -325,12 +332,9 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) CLEAR_FLAG(pd, PD_FLAG_SC_USE_SCBKD); CLEAR_FLAG(pd, PD_FLAG_INSTALL_MODE); pd->reply_id = REPLY_ACK; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; - case CMD_CHLNG: - /* Workaround for error: a label can only be part of a - * statement and a declaration is not a statement */ - ; + case CMD_CHLNG: { int tmp = OSDP_PD_CAP_COMMUNICATION_SECURITY; if (pd->cap[tmp].compliance_level == 0) { pd->reply_id = REPLY_NAK; @@ -338,7 +342,7 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) break; } if (len != CMD_CHLNG_DATA_LEN) { - LOG_ERR(TAG "CMD_CHLNG length mismatch! %d/8", len); + LOG_ERR("CMD_CHLNG length mismatch! %d/8", len); break; } osdp_sc_init(pd); @@ -347,37 +351,40 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) pd->sc.cp_random[i] = buf[pos++]; } pd->reply_id = REPLY_CCRYPT; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; + } case CMD_SCRYPT: if (len != CMD_SCRYPT_DATA_LEN) { - LOG_ERR(TAG "CMD_SCRYPT length mismatch! %d/16", len); + LOG_ERR("CMD_SCRYPT length mismatch! %d/16", len); break; } for (i = 0; i < 16; i++) { pd->sc.cp_cryptogram[i] = buf[pos++]; } pd->reply_id = REPLY_RMAC_I; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; #endif /* CONFIG_OSDP_SC_ENABLED */ default: + LOG_ERR("Unknown CMD(%02x)", pd->cmd_id); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_CMD_UNKNOWN; - ret = 0; - break; + return OSDP_PD_ERR_REPLY; } if (ret != 0) { - LOG_ERR(TAG "Invalid command structure. CMD: %02x, Len: %d", + LOG_ERR("Invalid command structure. CMD: %02x, Len: %d", pd->cmd_id, len); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_CMD_LEN; } if (pd->cmd_id != CMD_POLL) { - LOG_DBG(TAG "CMD: %02x REPLY: %02x", pd->cmd_id, pd->reply_id); + LOG_DBG("CMD: %02x REPLY: %02x", pd->cmd_id, pd->reply_id); } + + return ret; } /** @@ -387,7 +394,8 @@ static void pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len) */ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) { - int i, data_off, len = 0, ret = -1; + int ret = OSDP_PD_ERR_GENERIC; + int i, data_off, len = 0; struct osdp_cmd *cmd; data_off = osdp_phy_packet_get_data_offset(pd, buf); @@ -397,22 +405,22 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) buf += data_off; max_len -= data_off; if (max_len <= 0) { - LOG_ERR(TAG "Out of buffer space!"); + LOG_ERR("Out of buffer space!"); return -1; } switch (pd->reply_id) { case REPLY_ACK: if (max_len < REPLY_ACK_LEN) { - LOG_ERR(TAG "Out of buffer space!"); + LOG_ERR("Out of buffer space!"); break; } buf[len++] = pd->reply_id; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case REPLY_PDID: if (max_len < REPLY_PDID_LEN) { - LOG_ERR(TAG "Out of buffer space!"); + LOG_ERR("Out of buffer space!"); break; } buf[len++] = pd->reply_id; @@ -432,11 +440,11 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) buf[len++] = BYTE_3(pd->id.firmware_version); buf[len++] = BYTE_2(pd->id.firmware_version); buf[len++] = BYTE_1(pd->id.firmware_version); - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case REPLY_PDCAP: if (max_len < REPLY_PDCAP_LEN) { - LOG_ERR(TAG "Out of buffer space!"); + LOG_ERR("Out of buffer space!"); break; } buf[len++] = pd->reply_id; @@ -445,7 +453,7 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) continue; } if (max_len < REPLY_PDCAP_ENTITY_LEN) { - LOG_ERR(TAG "Out of buffer space!"); + LOG_ERR("Out of buffer space!"); break; } buf[len++] = i; @@ -453,30 +461,30 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) buf[len++] = pd->cap[i].num_items; max_len -= REPLY_PDCAP_ENTITY_LEN; } - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case REPLY_LSTATR: if (max_len < REPLY_LSTATR_LEN) { - LOG_ERR(TAG "Out of buffer space!"); + LOG_ERR("Out of buffer space!"); break; } buf[len++] = pd->reply_id; buf[len++] = ISSET_FLAG(pd, PD_FLAG_TAMPER); buf[len++] = ISSET_FLAG(pd, PD_FLAG_POWER); - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case REPLY_RSTATR: if (max_len < REPLY_RSTATR_LEN) { - LOG_ERR(TAG "Out of buffer space!"); + LOG_ERR("Out of buffer space!"); break; } buf[len++] = pd->reply_id; buf[len++] = ISSET_FLAG(pd, PD_FLAG_R_TAMPER); - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case REPLY_COM: if (max_len < REPLY_COM_LEN) { - LOG_ERR(TAG "Out of buffer space!"); + LOG_ERR("Out of buffer space!"); break; } /** @@ -491,7 +499,7 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) */ cmd = osdp_cmd_get_last(pd); if (cmd == NULL || cmd->id != OSDP_CMD_COMSET) { - LOG_ERR(TAG "Failed to fetch queue tail for COMSET"); + LOG_ERR("Failed to fetch queue tail for COMSET"); break; } @@ -506,16 +514,16 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) pd->baud_rate = (int)cmd->comset.baud_rate; LOG_INF("COMSET Succeeded! New PD-Addr: %d; Baud: %d", pd->address, pd->baud_rate); - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case REPLY_NAK: if (max_len < REPLY_NAK_LEN) { - LOG_ERR(TAG "Fatal: insufficient space for sending NAK"); + LOG_ERR("Fatal: insufficient space for sending NAK"); return -1; } buf[len++] = pd->reply_id; buf[len++] = pd->cmd_data[0]; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; #ifdef CONFIG_OSDP_SC_ENABLED case REPLY_CCRYPT: @@ -523,7 +531,7 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) break; } if (max_len < REPLY_CCRYPT_LEN) { - LOG_ERR(TAG "Out of buffer space!"); + LOG_ERR("Out of buffer space!"); return -1; } osdp_fill_random(pd->sc.pd_random, 8); @@ -542,14 +550,14 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) smb[0] = 3; /* length */ smb[1] = SCS_12; /* type */ smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1; - ret = 0; + ret = OSDP_PD_ERR_NONE; break; case REPLY_RMAC_I: if (smb == NULL) { break; } if (max_len < REPLY_RMAC_I_LEN) { - LOG_ERR(TAG "Out of buffer space!"); + LOG_ERR("Out of buffer space!"); return -1; } osdp_compute_rmac_i(pd); @@ -563,15 +571,15 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) smb[2] = 1; /* CP auth succeeded */ SET_FLAG(pd, PD_FLAG_SC_ACTIVE); if (ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD)) { - LOG_WRN(TAG "SC Active with SCBK-D"); + LOG_WRN("SC Active with SCBK-D"); } else { - LOG_INF(TAG "SC Active"); + LOG_INF("SC Active"); } } else { smb[2] = 0; /* CP auth failed */ - LOG_WRN(TAG "failed to verify CP_crypt"); + LOG_WRN("failed to verify CP_crypt"); } - ret = 0; + ret = OSDP_PD_ERR_NONE; break; #endif /* CONFIG_OSDP_SC_ENABLED */ } @@ -585,10 +593,10 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) if (ret != 0) { /* catch all errors and report it as a RECORD error to CP */ - LOG_ERR(TAG "ReplyID unknown or insufficient space or some other" - "error. Sending NAK"); + LOG_ERR("Failed to build REPLY(%02x); Sending NAK instead!", + pd->reply_id); if (max_len < REPLY_NAK_LEN) { - LOG_ERR(TAG "Fatal: insufficient space for sending NAK"); + LOG_ERR("Fatal: insufficient space for sending NAK"); return -1; } buf[0] = REPLY_NAK; @@ -599,12 +607,6 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len) return len; } -/** - * pd_send_reply - blocking send; doesn't handle partials - * Returns: - * 0 - success - * -1 - failure - */ static int pd_send_reply(struct osdp_pd *pd) { int ret, len; @@ -612,23 +614,27 @@ static int pd_send_reply(struct osdp_pd *pd) /* init packet buf with header */ len = osdp_phy_packet_init(pd, pd->rx_buf, sizeof(pd->rx_buf)); if (len < 0) { - return -1; + return OSDP_PD_ERR_GENERIC; } /* fill reply data */ ret = pd_build_reply(pd, pd->rx_buf, sizeof(pd->rx_buf)); if (ret <= 0) { - return -1; + return OSDP_PD_ERR_GENERIC; } len += ret; /* finalize packet */ len = osdp_phy_packet_finalize(pd, pd->rx_buf, len, sizeof(pd->rx_buf)); if (len < 0) { - return -1; + return OSDP_PD_ERR_GENERIC; } ret = pd->channel.send(pd->channel.data, pd->rx_buf, len); + if (ret != len) { + LOG_ERR("Channel send for %d bytes failed! ret: %d", len, ret); + return OSDP_PD_ERR_GENERIC; + } if (IS_ENABLED(CONFIG_OSDP_PACKET_TRACE)) { if (pd->cmd_id != CMD_POLL) { @@ -636,17 +642,9 @@ static int pd_send_reply(struct osdp_pd *pd) } } - return (ret == len) ? 0 : -1; + return OSDP_PD_ERR_NONE; } -/** - * pd_receve_packet - received buffer from serial stream handling partials - * Returns: - * 0: success - * 1: no data yet - * -1: fatal errors - * -2: phy layer errors that need to reply with NAK - */ static int pd_receve_packet(struct osdp_pd *pd) { uint8_t *buf; @@ -658,7 +656,7 @@ static int pd_receve_packet(struct osdp_pd *pd) rec_bytes = pd->channel.recv(pd->channel.data, buf, max_len); if (rec_bytes <= 0) { - return 1; + return OSDP_PD_ERR_NO_DATA; } if (was_empty && rec_bytes > 0) { /* Start of message */ @@ -683,22 +681,24 @@ static int pd_receve_packet(struct osdp_pd *pd) ret = osdp_phy_decode_packet(pd, pd->rx_buf, pd->rx_buf_len); if (ret == OSDP_ERR_PKT_FMT) { if (pd->reply_id != 0) { - return -2; /* Send a NAK */ + pd->reply_id = REPLY_NAK; + pd->cmd_data[0] = OSDP_PD_NAK_RECORD; + return OSDP_PD_ERR_REPLY; } - return -1; /* fatal errors */ + return OSDP_PD_ERR_GENERIC; /* fatal errors */ } else if (ret == OSDP_ERR_PKT_WAIT) { /* rx_buf_len != pkt->len; wait for more data */ - return 1; + return OSDP_PD_ERR_NO_DATA; } else if (ret == OSDP_ERR_PKT_SKIP) { /* soft fail - discard this message */ pd->rx_buf_len = 0; if (pd->channel.flush) { pd->channel.flush(pd->channel.data); } - return 1; + return OSDP_PD_ERR_NO_DATA; } pd->rx_buf_len = ret; - return 0; + return OSDP_PD_ERR_NONE; } void osdp_update(struct osdp *ctx) @@ -709,27 +709,27 @@ void osdp_update(struct osdp *ctx) switch (pd->state) { case OSDP_PD_STATE_IDLE: ret = pd_receve_packet(pd); - if (ret == -1 || ((pd->rx_buf_len > 0 || + if (ret == OSDP_PD_ERR_GENERIC || ((pd->rx_buf_len > 0 || ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE)) && osdp_millis_since(pd->tstamp) > OSDP_RESP_TOUT_MS)) { /** * When we receive a command from PD after a timeout, * any established secure channel must be discarded. */ - LOG_ERR(TAG "receive errors/timeout"); + LOG_ERR("receive errors/timeout"); pd->state = OSDP_PD_STATE_ERR; break; } - if (ret == 1) { + if (ret == OSDP_PD_ERR_NO_DATA) { break; } - if (ret == 0) { + if (ret == OSDP_PD_ERR_NONE) { pd_decode_command(pd, pd->rx_buf, pd->rx_buf_len); } pd->state = OSDP_PD_STATE_SEND_REPLY; __fallthrough; case OSDP_PD_STATE_SEND_REPLY: - if (pd_send_reply(pd) == -1) { + if (pd_send_reply(pd) == OSDP_PD_ERR_GENERIC) { pd->state = OSDP_PD_STATE_ERR; break; } @@ -784,7 +784,7 @@ int osdp_setup(struct osdp *ctx, uint8_t *key) SET_FLAG(pd, PD_FLAG_PD_MODE); #ifdef CONFIG_OSDP_SC_ENABLED if (key == NULL) { - LOG_WRN(TAG "SCBK not provided. PD is in INSTALL_MODE"); + LOG_WRN("SCBK not provided. PD is in INSTALL_MODE"); SET_FLAG(pd, PD_FLAG_INSTALL_MODE); } else { memcpy(pd->sc.scbk, key, 16); diff --git a/subsys/mgmt/osdp/src/osdp_phy.c b/subsys/mgmt/osdp/src/osdp_phy.c index ef3f50a761d..2e2813bb4ad 100644 --- a/subsys/mgmt/osdp/src/osdp_phy.c +++ b/subsys/mgmt/osdp/src/osdp_phy.c @@ -10,8 +10,6 @@ LOG_MODULE_DECLARE(osdp, CONFIG_OSDP_LOG_LEVEL); #include #include "osdp_common.h" -#define TAG "PHY: " - #define OSDP_PKT_MARK 0xFF #define OSDP_PKT_SOM 0x53 #define PKT_CONTROL_SQN 0x03 @@ -95,7 +93,7 @@ int osdp_phy_packet_init(struct osdp_pd *pd, uint8_t *buf, int max_len) pd_mode = ISSET_FLAG(pd, PD_FLAG_PD_MODE); exp_len = sizeof(struct osdp_packet_header) + 64; /* 64 is estimated */ if (max_len < exp_len) { - LOG_INF(TAG "packet_init: out of space! CMD: %02x", pd->cmd_id); + LOG_ERR("packet_init: out of space! CMD: %02x", pd->cmd_id); return OSDP_ERR_PKT_FMT; } @@ -136,8 +134,7 @@ int osdp_phy_packet_finalize(struct osdp_pd *pd, uint8_t *buf, /* Do a sanity check only as we expect expect header to be prefilled */ if (buf[0] != OSDP_PKT_MARK || buf[1] != OSDP_PKT_SOM) { - LOG_ERR(TAG "packet_finalize: header validation failed! " - "CMD: %02x", pd->cmd_id); + LOG_ERR("PKT_F: Invalid header"); return OSDP_ERR_PKT_FMT; } pkt = (struct osdp_packet_header *)buf; @@ -209,8 +206,7 @@ int osdp_phy_packet_finalize(struct osdp_pd *pd, uint8_t *buf, return len; out_of_space_error: - LOG_ERR(TAG "packet_finalize: Out of buffer space! " - "CMD: %02x", pd->cmd_id); + LOG_ERR("PKT_F: Out of buffer space! CMD(%02x)", pd->cmd_id); return OSDP_ERR_PKT_FMT; } @@ -232,12 +228,12 @@ int osdp_phy_decode_packet(struct osdp_pd *pd, uint8_t *buf, int len) /* validate packet header */ if (pkt->mark != OSDP_PKT_MARK || pkt->som != OSDP_PKT_SOM) { - LOG_ERR(TAG "invalid MARK/SOM"); + LOG_ERR("invalid MARK/SOM"); return OSDP_ERR_PKT_FMT; } if (!pd_mode && !(pkt->pd_address & 0x80)) { - LOG_ERR(TAG "reply without MSB set 0x%02x", pkt->pd_address); + LOG_ERR("Reply without address MSB set!"); return OSDP_ERR_PKT_FMT; } @@ -253,10 +249,10 @@ int osdp_phy_decode_packet(struct osdp_pd *pd, uint8_t *buf, int len) if (pd_addr != pd->address && pd_addr != 0x7F) { /* not addressed to us and was not broadcasted */ if (!pd_mode) { - LOG_ERR(TAG "invalid pd address %d", pd_addr); + LOG_ERR("Invalid pd address %d", pd_addr); return OSDP_ERR_PKT_FMT; } - LOG_DBG(TAG "cmd for PD[%d] discarded", pd_addr); + LOG_DBG("cmd for PD[%d] discarded", pd_addr); return OSDP_ERR_PKT_SKIP; } @@ -278,14 +274,14 @@ int osdp_phy_decode_packet(struct osdp_pd *pd, uint8_t *buf, int len) * TODO: PD must resend the last response if CP send the same * sequence number again. */ - LOG_ERR(TAG "seq-repeat reply-resend feature not supported!"); + LOG_ERR("seq-repeat/reply-resend feature not supported!"); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_SEQ_NUM; return OSDP_ERR_PKT_FMT; } comp = osdp_phy_get_seq_number(pd, pd_mode); if (comp != cur && !ISSET_FLAG(pd, PD_FLAG_SKIP_SEQ_CHECK)) { - LOG_ERR(TAG "packet seq mismatch %d/%d", comp, cur); + LOG_ERR("packet seq mismatch %d/%d", comp, cur); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_SEQ_NUM; return OSDP_ERR_PKT_FMT; @@ -297,7 +293,7 @@ int osdp_phy_decode_packet(struct osdp_pd *pd, uint8_t *buf, int len) cur = (buf[pkt_len] << 8) | buf[pkt_len - 1]; comp = osdp_compute_crc16(buf + 1, pkt_len - 2); if (comp != cur) { - LOG_ERR(TAG "invalid crc 0x%04x/0x%04x", comp, cur); + LOG_ERR("invalid crc 0x%04x/0x%04x", comp, cur); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_MSG_CHK; return OSDP_ERR_PKT_FMT; @@ -307,7 +303,7 @@ int osdp_phy_decode_packet(struct osdp_pd *pd, uint8_t *buf, int len) } else { comp = osdp_compute_checksum(buf + 1, pkt_len - 1); if (comp != buf[len - 1]) { - LOG_ERR(TAG "invalid checksum %02x/%02x", comp, cur); + LOG_ERR("invalid checksum %02x/%02x", comp, cur); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_MSG_CHK; return OSDP_ERR_PKT_FMT; @@ -324,13 +320,13 @@ int osdp_phy_decode_packet(struct osdp_pd *pd, uint8_t *buf, int len) if (pkt->control & PKT_CONTROL_SCB) { if (pd_mode && !ISSET_FLAG(pd, PD_FLAG_SC_CAPABLE)) { - LOG_ERR(TAG "PD is not SC capable"); + LOG_ERR("PD is not SC capable"); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_SC_UNSUP; return OSDP_ERR_PKT_FMT; } if (pkt->data[1] < SCS_11 || pkt->data[1] > SCS_18) { - LOG_ERR(TAG "invalid SB Type"); + LOG_ERR("Invalid SB Type"); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_SC_COND; return OSDP_ERR_PKT_FMT; @@ -352,7 +348,7 @@ int osdp_phy_decode_packet(struct osdp_pd *pd, uint8_t *buf, int len) len -= pkt->data[0]; /* consume security block */ } else { if (ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE)) { - LOG_ERR(TAG "Received plain-text message in SC"); + LOG_ERR("Received plain-text message in SC"); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_SC_COND; return OSDP_ERR_PKT_FMT; @@ -367,7 +363,7 @@ int osdp_phy_decode_packet(struct osdp_pd *pd, uint8_t *buf, int len) osdp_compute_mac(pd, is_cmd, buf + 1, mac_offset); mac = is_cmd ? pd->sc.c_mac : pd->sc.r_mac; if (memcmp(buf + 1 + mac_offset, mac, 4) != 0) { - LOG_ERR(TAG "invalid MAC"); + LOG_ERR("Invalid MAC; discarding SC"); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_SC_COND; return OSDP_ERR_PKT_FMT; @@ -388,7 +384,7 @@ int osdp_phy_decode_packet(struct osdp_pd *pd, uint8_t *buf, int len) */ len = osdp_decrypt_data(pd, is_cmd, data + 1, len - 1); if (len <= 0) { - LOG_ERR(TAG "failed at decrypt"); + LOG_ERR("Failed at decrypt; discarding SC"); pd->reply_id = REPLY_NAK; pd->cmd_data[0] = OSDP_PD_NAK_SC_COND; return OSDP_ERR_PKT_FMT; diff --git a/subsys/mgmt/osdp/src/osdp_sc.c b/subsys/mgmt/osdp/src/osdp_sc.c index ac14d013932..e72c5be20bf 100644 --- a/subsys/mgmt/osdp/src/osdp_sc.c +++ b/subsys/mgmt/osdp/src/osdp_sc.c @@ -10,8 +10,6 @@ LOG_MODULE_DECLARE(osdp, CONFIG_OSDP_LOG_LEVEL); #include #include "osdp_common.h" -#define TAG "SC: " - #define OSDP_SC_EOM_MARKER 0x80 /* End of Message Marker */ /* Default key as specified in OSDP specification */ @@ -146,7 +144,7 @@ int osdp_decrypt_data(struct osdp_pd *pd, int is_cmd, uint8_t *data, int length) uint8_t iv[16]; if (length % 16 != 0) { - LOG_ERR(TAG "decrypt_pkt invalid len:%d", length); + LOG_ERR("decrypt_pkt invalid len:%d", length); return -1; }