Exportable version of WizziLab's modem driver.
Diff: src/alp_payload.cpp
- Revision:
- 65:c7749f9b6009
- Parent:
- 64:7c338f388d82
diff -r 7c338f388d82 -r c7749f9b6009 src/alp_payload.cpp --- a/src/alp_payload.cpp Thu Sep 23 15:06:02 2021 +0000 +++ b/src/alp_payload.cpp Fri Oct 01 08:46:44 2021 +0000 @@ -40,7 +40,7 @@ void alp_payload_free(alp_payload_t* alp) { - while (alp != NULL) + while (NULL != alp) { alp_payload_t* alp_next = alp->next; FREE(alp); @@ -65,21 +65,10 @@ /// @return alp_payload_t* Appended payloads /// @note: XXX payloads MUST be malloced //====================================================================== -static void _alp_payload_append(alp_payload_t* alp_1, alp_payload_t* alp_2) +alp_payload_t* alp_payload_append(alp_payload_t* alp_1, alp_payload_t* alp_2) { - if (NULL == alp_1->next) - { - // Append payload - alp_1->next = alp_2; - - return; - } - - _alp_payload_append(alp_1->next, alp_2); -} - -alp_payload_t* alp_payload_append(alp_payload_t* alp_1, alp_payload_t* alp_2) -{ + alp_payload_t* alp = alp_1; + if (NULL == alp_1) { return alp_2; @@ -89,9 +78,18 @@ { return alp_1; } + + while (NULL != alp) + { + if (NULL == alp->next) + { + alp->next = alp_2; + return alp_1; + } + + alp = alp->next; + } - _alp_payload_append(alp_1, alp_2); - return alp_1; } @@ -100,9 +98,9 @@ u32 action = 0; u32 i; - while (alp != NULL) + while (NULL != alp) { - PRINT("ALP[%d]:", action); + PRINT("ALP[%d]: (0x%x)", action, alp); PRINT_DATA("", " %02X", alp->d, alp->len, "\n"); FLUSH(); @@ -114,23 +112,23 @@ PRINT("---\n"); } -u32 alp_payload_to_buf(alp_payload_t* alp, u8* buf, u32 offset, u32 max) +u32 alp_payload_to_buf(alp_payload_t* alp, u8* buf, u32 max) { - if (NULL == alp) + u32 offset = 0; + + while (NULL != alp) { // End of payload - ALP_ASSERT((offset <= max), "ALP payload too big for buffer (%d\%d)\n", offset, max); - return offset; + ALP_ASSERT((offset + alp->len) <= max, "ALP payload too big for buffer (%d\%d)\n", offset + alp->len, max); + + // Copy into buffer + memcpy(&(buf[offset]), alp->d, alp->len); + offset += alp->len; + + alp = alp->next; } - if ((offset + alp->len) <= max) - { - // Copy into buffer - memcpy(&(buf[offset]), alp->d, alp->len); - } - - // Next - return alp_payload_to_buf(alp->next, buf, offset+alp->len, max); + return offset; } //====================================================================== @@ -150,7 +148,7 @@ // Calculate hash on payload kal_sha256_init(); - while (alp_hash != NULL) + while (NULL != alp_hash) { kal_sha256_update(alp_hash->d, alp_hash->len); alp_hash = alp_hash->next; @@ -190,7 +188,7 @@ // Calculate hash on payload kal_sha256_init(); - while (alp_hash != NULL) + while (NULL != alp_hash) { kal_sha256_update(alp_hash->d, alp_hash->len); alp_hash = alp_hash->next; @@ -242,7 +240,7 @@ // Calculate hash on payload kal_sha256_init(); - while (alp_hash != NULL) + while (NULL != alp_hash) { kal_sha256_update(alp_hash->d, alp_hash->len); alp_hash = alp_hash->next; @@ -757,19 +755,19 @@ } alp_payload_t* alp_payload_get(alp_payload_t* alp, u8 op) -{ - if (NULL == alp) +{ + while (NULL != alp) { - return NULL; + if (alp->d[0] == op) + { + // Return it + return alp; + } + + alp = alp->next; } - - if (alp->d[0] == op) - { - // Return it - return alp; - } - - return alp_payload_get(alp->next, op); + + return NULL; } bool alp_payload_extract_data(alp_payload_t** alp, u8 op, void* data) @@ -779,9 +777,8 @@ if (alp_data) { alp_parsed_chunk_t r; - u8* p = alp_data->d; - alp_parse_chunk(&p, &r); + alp_payload_parse_chunk(alp_data, &r); switch (op) { @@ -812,63 +809,72 @@ ERR_PRIO_NONE, }; -static int _alp_payload_get_err(alp_payload_t* alp, int err, u8 err_prio) +int alp_payload_get_err(alp_payload_t* alp) { + int err = ALP_ERR_NONE; + uint8_t err_prio = ERR_PRIO_NONE; alp_parsed_chunk_t r; - u8* p = alp->d; - - if (NULL == alp) + + while (NULL != alp) { - return err; + switch (alp->d[0]) + { + case ALP_OPCODE_RSP_TAG: // Fallthrough + case ALP_OPCODE_RSP_EOPTAG: // Fallthrough + case ALP_OPCODE_RSP_ERRTAG: + { + alp_payload_parse_chunk(alp, &r); + if (ERR_PRIO_TAG < err_prio && r.meta.tag.err) + { + err = ALP_ERR_UNKNOWN; + err_prio = ERR_PRIO_TAG; + } + break; + } + case ALP_OPCODE_RSP_STATUS: + { + alp_payload_parse_chunk(alp, &r); + if (ERR_PRIO_STATUS < err_prio && r.meta.status.code < ALP_ERR_NONE) + { + err = r.meta.status.code; + err_prio = ERR_PRIO_STATUS; + } + break; + } + case ALP_OPCODE_RSP_EOPISTATUS: + { + alp_payload_parse_chunk(alp, &r); + if (ERR_PRIO_EOPISTATUS < err_prio && r.meta.istatus.err < ALP_ERR_NONE) + { + err = r.meta.istatus.err + ALP_ERR_ITF_START; + err_prio = ERR_PRIO_EOPISTATUS; + } + break; + } + default: + break; + } + + alp = alp->next; } - switch (alp->d[0]) - { - case ALP_OPCODE_RSP_TAG: - { - alp_parse_chunk(&p, &r); - if (ERR_PRIO_TAG < err_prio && r.meta.tag.err) - { - err = ALP_ERR_UNKNOWN; - err_prio = ERR_PRIO_TAG; - } - break; - } - case ALP_OPCODE_RSP_STATUS: - { - alp_parse_chunk(&p, &r); - if (ERR_PRIO_STATUS < err_prio && r.meta.status.code < ALP_ERR_NONE) - { - err = r.meta.status.code; - err_prio = ERR_PRIO_STATUS; - } - break; - } - case ALP_OPCODE_RSP_EOPISTATUS: - { - alp_parse_chunk(&p, &r); - if (ERR_PRIO_EOPISTATUS < err_prio && r.meta.istatus.err < ALP_ERR_NONE) - { - err = r.meta.istatus.err + ALP_ERR_ITF_START; - err_prio = ERR_PRIO_EOPISTATUS; - } - break; - } - default: - break; - } - - return _alp_payload_get_err(alp->next, err, err_prio); + return err; } -int alp_payload_get_err(alp_payload_t* alp) +int alp_payload_parse_chunk(alp_payload_t* alp, alp_parsed_chunk_t* r) { - return _alp_payload_get_err(alp, ALP_ERR_NONE, ERR_PRIO_NONE); + if (NULL == alp) + { + return 0; + } + + uint8_t* p = alp->d; + return alp_parse_chunk(&p, r); } -alp_payload_t* alp_payload_parse(u8* p, int length) +alp_payload_t* alp_payload_parse(u8* d, int length) { - u8* d; + u8* data_start; int len; alp_parsed_chunk_t r; alp_payload_t* alp = NULL; @@ -876,15 +882,15 @@ while (length > 0) { - d = p; - len = (int)alp_parse_chunk(&p, &r); + data_start = d; + len = (int)alp_parse_chunk(&d, &r); // Malloc payload alp_new = alp_payload_new(len); // Fill payload alp_new->len = len; - memcpy(alp_new->d, d, len); + memcpy(alp_new->d, data_start, len); alp = alp_payload_append(alp, alp_new);