diff --git a/BMLite_example/inc/hcp_tiny.h b/BMLite_example/inc/hcp_tiny.h index c1a4b6a..4977f39 100644 --- a/BMLite_example/inc/hcp_tiny.h +++ b/BMLite_example/inc/hcp_tiny.h @@ -10,7 +10,6 @@ /** Communication acknowledge definition */ #define FPC_BEP_ACK 0x7f01ff7f - typedef struct { /** Send data to BM-Lite */ fpc_bep_result_t (*write) (uint16_t, const uint8_t *, uint32_t, void *); @@ -19,19 +18,44 @@ typedef struct { /** Receive timeout (msec). Applys ONLY to receiving packet from BM-Lite on physical layer */ uint32_t phy_rx_timeout; /** Data buffer for application layer */ - uint8_t *data_buffer; + uint8_t *pkt_buffer; /** Size of data buffer */ - uint32_t data_size_max; + uint32_t pkt_size_max; /** Current size of incoming or outcoming command packet */ - uint32_t data_size; + uint32_t pkt_size; /** Buffer of MTU size for transport layer */ uint8_t *txrx_buffer; } HCP_comm_t; +/** + * @brief Helper function for sending HCP commands + * + * @param chain HCP communication chain + * @param command_id command to send + * @param arg_key1 first key to add to the command + * @param arg_data1 first argument data to add + * @param arg_data1_length first data length of argument data + * @param arg_key2 second key to add to the command + * @param arg_data2 second argument data to add + * @param arg_data2_length second data length of argument data + * @return ::fpc_bep_result_t + */ fpc_bep_result_t send_command_args2(HCP_comm_t *chain, fpc_hcp_cmd_t command_id, fpc_hcp_arg_t arg_key1, void *arg_data1, uint16_t arg_data1_length, fpc_hcp_arg_t arg_key2, void *arg_data2, uint16_t arg_data2_length); +/** + * @brief Helper function for receiving HCP commands + + * @param command_id command to send + * @param arg_key1 first key to receive + * @param arg_data1 first argument data + * @param arg_data1_length first argument data length + * @param arg_key2 second key to receive + * @param arg_data2 second argument data + * @param arg_data2_length second argument + * @return ::fpc_bep_result_t + */ fpc_bep_result_t receive_result_args2(HCP_comm_t *chain, fpc_hcp_arg_t arg_key1, void *arg_data1, uint16_t arg_data1_length, fpc_hcp_arg_t arg_key2, void *arg_data2, uint16_t arg_data2_length); diff --git a/BMLite_example/src/bep_host_if.c b/BMLite_example/src/bep_host_if.c index cddc610..cccb324 100644 --- a/BMLite_example/src/bep_host_if.c +++ b/BMLite_example/src/bep_host_if.c @@ -45,65 +45,6 @@ static const uint8_t MAX_CAPTURE_ATTEMPTS = 15U; static const uint16_t CAPTURE_TIMEOUT = 3000; -#if 0 -/** - * @brief Helper function for sending HCP commands - * - * @param chain HCP communication chain - * @param command_id command to send - * @param arg_key1 first key to add to the command - * @param arg_data1 first argument data to add - * @param arg_data1_length first data length of argument data - * @param arg_key2 second key to add to the command - * @param arg_data2 second argument data to add - * @param arg_data2_length second data length of argument data - * @return ::fpc_bep_result_t - */ -static fpc_bep_result_t send_command_args2(HCP_comm_t *chain, fpc_hcp_cmd_t command_id, - fpc_hcp_arg_t arg_key1, void *arg_data1, uint16_t arg_data1_length, - fpc_hcp_arg_t arg_key2, void *arg_data2, uint16_t arg_data2_length) -{ - fpc_hcp_packet_t command; - fpc_bep_result_t bep_result; - fpc_bep_result_t com_result; - fpc_hcp_arg_data_t args_tx[10] = {{ 0 }}; - - memset(&command, 0x0, sizeof(command)); - command.arguments = args_tx; - command.num_args = ARRAY_SIZE(args_tx); - command.id = command_id; - - if (arg_key1 != ARG_NONE) { - if (!fpc_hcp_arg_add(&command, arg_key1, arg_data1_length, false, arg_data1)) { - log_error("%s:%u Could not add arg:%u\n", __func__, __LINE__, arg_key1); - bep_result = FPC_BEP_RESULT_NO_MEMORY; - goto exit; - } - } - - if (arg_key2 != ARG_NONE) { - if (!fpc_hcp_arg_add(&command, arg_key2, arg_data2_length, false, arg_data2)) { - log_error("%s:%u Could not add arg:%u\n", __func__, __LINE__, arg_key2); - bep_result = FPC_BEP_RESULT_NO_MEMORY; - goto exit; - } - } - - com_result = fpc_hcp_transmit(&command, chain); - bep_result = com_to_bep_result(com_result); - - if (bep_result != FPC_BEP_RESULT_OK) { - log_error("%s:%u ERROR %d\n", __func__, __LINE__, bep_result); - } - -exit: - fpc_hcp_free(chain, &command); - - return bep_result; -} - -#endif - static fpc_bep_result_t send_command_no_args(HCP_comm_t *chain, fpc_hcp_cmd_t command_id) { return send_command_args2(chain, command_id, ARG_NONE, NULL, 0, ARG_NONE, NULL, 0); @@ -116,83 +57,6 @@ static fpc_bep_result_t send_command(HCP_comm_t *chain, fpc_hcp_cmd_t command_id ARG_NONE, NULL, 0); } -#if 0 -/** - * @brief Helper function for receiving HCP commands - - * @param command_id command to send - * @param arg_key1 first key to receive - * @param arg_data1 first argument data - * @param arg_data1_length first argument data length - * @param arg_key2 second key to receive - * @param arg_data2 second argument data - * @param arg_data2_length second argument - * @return ::fpc_bep_result_t - */ -static fpc_bep_result_t receive_result_args2(HCP_comm_t *chain, - fpc_hcp_arg_t arg_key1, void *arg_data1, uint16_t arg_data1_length, - fpc_hcp_arg_t arg_key2, void *arg_data2, uint16_t arg_data2_length) -{ - fpc_hcp_packet_t response; - fpc_hcp_arg_data_t args_rx[10] = {{ 0 }}; - fpc_bep_result_t bep_result = FPC_BEP_RESULT_GENERAL_ERROR; - fpc_hcp_arg_data_t *arg_data; - - memset(&response, 0x0, sizeof(fpc_hcp_cmd_t)); - response.arguments = args_rx; - response.num_args = ARRAY_SIZE(args_rx); - - do { - fpc_bep_result_t com_result = fpc_hcp_receive(&response, chain); - bep_result = com_to_bep_result(com_result); - } while (bep_result == FPC_BEP_RESULT_TIMEOUT); - - if (bep_result != FPC_BEP_RESULT_OK) { - goto exit; - } - - /* Check bep result first */ - arg_data = fpc_hcp_arg_get(&response, ARG_RESULT); - if (arg_data) { - bep_result = *(int8_t *)arg_data->data; - } else { - log_error("%s Result argument missing\n", __func__); - bep_result = FPC_BEP_RESULT_INVALID_ARGUMENT; - } - if (bep_result != FPC_BEP_RESULT_OK) { - goto exit; - } - - /* Get first argument */ - if (arg_key1 != ARG_NONE) { - arg_data = fpc_hcp_arg_get(&response, arg_key1); - if (arg_data && arg_data->size <= arg_data1_length) { - memcpy(arg_data1, arg_data->data, arg_data->size); - } else { - log_error("%s %d argument missing\n", __func__, arg_key1); - bep_result = FPC_BEP_RESULT_INVALID_ARGUMENT; - goto exit; - } - } - - /* Get second argument */ - if (arg_key2 != ARG_NONE) { - arg_data = fpc_hcp_arg_get(&response, arg_key2); - if (arg_data && arg_data->size <= arg_data2_length) { - memcpy(arg_data2, arg_data->data, arg_data->size); - } else { - /* Not an error since the second argument is optional */ - log_debug("%s %d argument missing\n", __func__, arg_key2); - } - } - -exit: - fpc_hcp_free(chain, &response); - - return bep_result; -} -#endif - static fpc_bep_result_t receive_result_no_args(HCP_comm_t *chain) { return receive_result_args2(chain, ARG_NONE, NULL, 0, ARG_NONE, NULL, 0); diff --git a/BMLite_example/src/hcp_tiny.c b/BMLite_example/src/hcp_tiny.c index 17b5d6e..8c6fc24 100644 --- a/BMLite_example/src/hcp_tiny.c +++ b/BMLite_example/src/hcp_tiny.c @@ -1,14 +1,10 @@ -#include #include #include -#include -#include #include #include "bep_host_if.h" #include "platform.h" #include "fpc_crc.h" - #include "fpc_hcp_common.h" #include "hcp_tiny.h" @@ -48,37 +44,37 @@ typedef struct { fpc_bep_result_t hcp_init_cmd(HCP_comm_t *hcp_comm, uint16_t cmd) { - _HCP_cmd_t *out = (_HCP_cmd_t *)hcp_comm->data_buffer; + _HCP_cmd_t *out = (_HCP_cmd_t *)hcp_comm->pkt_buffer; out->cmd = cmd; out->args_nr = 0; - hcp_comm->data_size = 4; + hcp_comm->pkt_size = 4; return FPC_BEP_RESULT_OK; } fpc_bep_result_t hcp_add_arg(HCP_comm_t *hcp_comm, uint16_t arg, uint8_t *data, uint16_t size) { - if(hcp_comm->data_size + 4 + size > hcp_comm->data_size_max) { + if(hcp_comm->pkt_size + 4 + size > hcp_comm->pkt_size_max) { return FPC_BEP_RESULT_NO_MEMORY; } - ((_HCP_cmd_t *)hcp_comm->data_buffer)->args_nr++; - _CMD_arg_t *args = (_CMD_arg_t *)(&hcp_comm->data_buffer[hcp_comm->data_size]); + ((_HCP_cmd_t *)hcp_comm->pkt_buffer)->args_nr++; + _CMD_arg_t *args = (_CMD_arg_t *)(&hcp_comm->pkt_buffer[hcp_comm->pkt_size]); args->arg = arg; args->size = size; if(size) { memcpy(&args->pld, data, size); } - hcp_comm->data_size += 4 + size; + hcp_comm->pkt_size += 4 + size; return FPC_BEP_RESULT_OK; } fpc_bep_result_t hcp_get_arg(HCP_comm_t *hcp_comm, uint16_t arg_type, uint16_t *size, uint8_t **payload) { uint16_t i = 0; - uint8_t *buffer = hcp_comm->data_buffer; + uint8_t *buffer = hcp_comm->pkt_buffer; uint16_t args_nr = ((_HCP_cmd_t *)(buffer))->args_nr; uint8_t *pdata = (uint8_t *)&((_HCP_cmd_t *)(buffer))->args; - while (i < args_nr && (pdata - buffer) <= hcp_comm->data_size) { + while (i < args_nr && (pdata - buffer) <= hcp_comm->pkt_size) { _CMD_arg_t *parg = (_CMD_arg_t *)pdata; if(parg->arg == arg_type) { *size = parg->size; @@ -94,120 +90,112 @@ fpc_bep_result_t hcp_get_arg(HCP_comm_t *hcp_comm, uint16_t arg_type, uint16_t * static fpc_bep_result_t _rx_application(HCP_comm_t *hcp_comm) { - fpc_bep_result_t status = FPC_BEP_RESULT_OK; - fpc_bep_result_t com_result = FPC_BEP_RESULT_OK; - uint16_t seq_nr = 0; - uint16_t seq_len = 1; - uint16_t len; - uint8_t *p = hcp_comm->data_buffer; - _HPC_pkt_t *pkt = (_HPC_pkt_t *)hcp_comm->txrx_buffer; - uint16_t buf_len = 0; + fpc_bep_result_t status = FPC_BEP_RESULT_OK; + fpc_bep_result_t com_result = FPC_BEP_RESULT_OK; + uint16_t seq_nr = 0; + uint16_t seq_len = 1; + uint16_t len; + uint8_t *p = hcp_comm->pkt_buffer; + _HPC_pkt_t *pkt = (_HPC_pkt_t *)hcp_comm->txrx_buffer; + uint16_t buf_len = 0; - while(seq_nr < seq_len) { - status = _rx_link(hcp_comm); + while(seq_nr < seq_len) { + status = _rx_link(hcp_comm); - if (!status) { - len = pkt->lnk_size - 4; - seq_nr = pkt->t_seq_nr; - seq_len = pkt->t_seq_len; - if(buf_len + len < hcp_comm->data_size_max) { - memcpy(p, &pkt->t_pld, len); - p += len; - buf_len += len; - } else { - com_result = FPC_BEP_RESULT_NO_MEMORY; - } - if (seq_len > 1) - DEBUG("S: Seqence %d of %d\n", seq_nr, seq_len); + if (!status) { + len = pkt->lnk_size - 4; + seq_nr = pkt->t_seq_nr; + seq_len = pkt->t_seq_len; + if(buf_len + len < hcp_comm->pkt_size_max) { + memcpy(p, &pkt->t_pld, len); + p += len; + buf_len += len; } else { - DEBUG("S: Receiving chunk error %d\n", status); - return status; + com_result = FPC_BEP_RESULT_NO_MEMORY; } + if (seq_len > 1) + DEBUG("S: Seqence %d of %d\n", seq_nr, seq_len); + } else { + DEBUG("S: Receiving chunk error %d\n", status); + return status; } - hcp_comm->data_size = buf_len; - return com_result; + } + + hcp_comm->pkt_size = buf_len; + return com_result; } static fpc_bep_result_t _rx_link(HCP_comm_t *hcp_comm) { - // Get size, msg and CRC - uint16_t result = hcp_comm->read(4, hcp_comm->txrx_buffer, hcp_comm->phy_rx_timeout, NULL); - _HPC_pkt_t *pkt = (_HPC_pkt_t *)hcp_comm->txrx_buffer; - uint16_t size; + // Get size, msg and CRC + uint16_t result = hcp_comm->read(4, hcp_comm->txrx_buffer, hcp_comm->phy_rx_timeout, NULL); + _HPC_pkt_t *pkt = (_HPC_pkt_t *)hcp_comm->txrx_buffer; + uint16_t size; - if (result) { - //DEBUG("Timed out waiting for response.\n"); - return result; - } + if (result) { + //DEBUG("Timed out waiting for response.\n"); + return result; + } - size = pkt->lnk_size; + size = pkt->lnk_size; - // Check if size plus header and crc is larger than max package size. - if (MTU < size + 8) { - DEBUG("S: Invalid size %d, larger than MTU %d.\n", size, MTU); - return FPC_BEP_RESULT_IO_ERROR; - } + // Check if size plus header and crc is larger than max package size. + if (MTU < size + 8) { + DEBUG("S: Invalid size %d, larger than MTU %d.\n", size, MTU); + return FPC_BEP_RESULT_IO_ERROR; + } - hcp_comm->read(size + 4, hcp_comm->txrx_buffer + 4, 100, NULL); -#ifdef DBG - // Print received data to log - DEBUG("SRX: Received %d bytes\n", size + 4); - DEBUG("SRX: Received data:"); + hcp_comm->read(size + 4, hcp_comm->txrx_buffer + 4, 100, NULL); - for (int i=0; itxrx_buffer[i]); - } - DEBUG("\n"); -#endif - uint32_t crc = *(uint32_t *)(hcp_comm->txrx_buffer + 4 + size); - uint32_t crc_calc = fpc_crc(0, hcp_comm->txrx_buffer+4, size); + uint32_t crc = *(uint32_t *)(hcp_comm->txrx_buffer + 4 + size); + uint32_t crc_calc = fpc_crc(0, hcp_comm->txrx_buffer+4, size); - if (crc_calc != crc) { - DEBUG("S: CRC mismatch. Calculated %08X, received %08X\n", crc_calc, crc); - return FPC_BEP_RESULT_IO_ERROR; - } + if (crc_calc != crc) { + DEBUG("S: CRC mismatch. Calculated %08X, received %08X\n", crc_calc, crc); + return FPC_BEP_RESULT_IO_ERROR; + } - // Send Ack - hcp_comm->write(4, (uint8_t *)&fpc_com_ack, 0, NULL); + // Send Ack + hcp_comm->write(4, (uint8_t *)&fpc_com_ack, 0, NULL); - return 0; + return 0; } static fpc_bep_result_t _tx_application(HCP_comm_t *hcp_comm) { - uint16_t seq_nr = 1; - fpc_bep_result_t status = FPC_BEP_RESULT_OK; - uint16_t data_left = hcp_comm->data_size; - uint8_t *p = hcp_comm->data_buffer; + uint16_t seq_nr = 1; + fpc_bep_result_t status = FPC_BEP_RESULT_OK; + uint16_t data_left = hcp_comm->pkt_size; + uint8_t *p = hcp_comm->pkt_buffer; - _HPC_pkt_t *pkt = (_HPC_pkt_t *)hcp_comm->txrx_buffer; + _HPC_pkt_t *pkt = (_HPC_pkt_t *)hcp_comm->txrx_buffer; - // Application MTU size is PHY MTU - (Transport and Link overhead) - uint16_t app_mtu = MTU - 6 - 8; + // Application MTU size is PHY MTU - (Transport and Link overhead) + uint16_t app_mtu = MTU - 6 - 8; - // Calculate sequence length - uint16_t seq_len = (data_left / app_mtu) + 1; + // Calculate sequence length + uint16_t seq_len = (data_left / app_mtu) + 1; - pkt->lnk_chn = 0; - pkt->t_seq_len = seq_len; + pkt->lnk_chn = 0; + pkt->t_seq_len = seq_len; - for (seq_nr = 1; seq_nr <= seq_len && !status; seq_nr++) { - pkt->t_seq_nr = seq_nr; - if (data_left < app_mtu) { - pkt->t_size = data_left; - memcpy(hcp_comm->txrx_buffer + 10, p, data_left); - pkt->lnk_size = data_left + 6; - } else { - pkt->t_size = app_mtu; - memcpy(hcp_comm->txrx_buffer + 10, p, app_mtu); - pkt->lnk_size = app_mtu + 6; - p += app_mtu; - } - - status = _tx_link(hcp_comm); + for (seq_nr = 1; seq_nr <= seq_len && !status; seq_nr++) { + pkt->t_seq_nr = seq_nr; + if (data_left < app_mtu) { + pkt->t_size = data_left; + memcpy(hcp_comm->txrx_buffer + 10, p, data_left); + pkt->lnk_size = data_left + 6; + } else { + pkt->t_size = app_mtu; + memcpy(hcp_comm->txrx_buffer + 10, p, app_mtu); + pkt->lnk_size = app_mtu + 6; + p += app_mtu; } - return status; + status = _tx_link(hcp_comm); + } + + return status; } fpc_bep_result_t _tx_link(HCP_comm_t *hcp_comm) @@ -226,16 +214,6 @@ fpc_bep_result_t _tx_link(HCP_comm_t *hcp_comm) return result; } -#ifdef DBG - // Print sent data to log - DEBUG("STX: Sent %d bytes\n", size); - DEBUG("STX: Sent data: "); - - for (int i=0; i < size; i++) { - DEBUG(" 0x%02X", hcp_comm->txrx_buffer[i]); - } - DEBUG("\n"); -#endif // Wait for ACK uint32_t ack; result = hcp_comm->read(4, (uint8_t *)&ack, 100, NULL); @@ -285,45 +263,42 @@ fpc_bep_result_t receive_result_args2(HCP_comm_t *chain, fpc_hcp_arg_t arg_key1, void *arg_data1, uint16_t arg_data1_length, fpc_hcp_arg_t arg_key2, void *arg_data2, uint16_t arg_data2_length) { - fpc_bep_result_t bep_result; - uint16_t size; - uint8_t *pld; + fpc_bep_result_t bep_result; + uint16_t size; + uint8_t *pld; - bep_result = _rx_application(chain); - if(bep_result) { - DEBUG("S: Receive err: %d\n", bep_result); - return bep_result; - } + bep_result = _rx_application(chain); + if(bep_result) { + DEBUG("S: Receive err: %d\n", bep_result); + return bep_result; + } - if (arg_key1 != ARG_NONE) { - bep_result = hcp_get_arg(chain, arg_key1, &size, &pld); - if(bep_result == FPC_BEP_RESULT_OK) { - if(arg_data1 == NULL) { - return FPC_BEP_RESULT_NO_MEMORY; - } - memcpy(arg_data1, pld, arg_data1_length); - } else { - DEBUG("Arg1 0x%04X not found\n", arg_key1); - return FPC_BEP_RESULT_INVALID_ARGUMENT; + if (arg_key1 != ARG_NONE) { + bep_result = hcp_get_arg(chain, arg_key1, &size, &pld); + if(bep_result == FPC_BEP_RESULT_OK) { + if(arg_data1 == NULL) { + return FPC_BEP_RESULT_NO_MEMORY; } + memcpy(arg_data1, pld, arg_data1_length); + } else { + DEBUG("Arg1 0x%04X not found\n", arg_key1); + return FPC_BEP_RESULT_INVALID_ARGUMENT; } + } - if (arg_key2 != ARG_NONE) { - bep_result = hcp_get_arg(chain, arg_key2, &size, &pld); - if(bep_result == FPC_BEP_RESULT_OK) { - if(arg_data2 == NULL) { - return FPC_BEP_RESULT_NO_MEMORY; - } - memcpy(arg_data2, pld, arg_data2_length); - } else { - DEBUG("Arg2 0x%04X not found\n", arg_key2); - //return FPC_BEP_RESULT_INVALID_ARGUMENT; + if (arg_key2 != ARG_NONE) { + bep_result = hcp_get_arg(chain, arg_key2, &size, &pld); + if(bep_result == FPC_BEP_RESULT_OK) { + if(arg_data2 == NULL) { + return FPC_BEP_RESULT_NO_MEMORY; } + memcpy(arg_data2, pld, arg_data2_length); + } else { + DEBUG("Arg2 0x%04X not found\n", arg_key2); + //return FPC_BEP_RESULT_INVALID_ARGUMENT; } + } return FPC_BEP_RESULT_OK; - } - - diff --git a/BMLite_example/src/main.c b/BMLite_example/src/main.c index 9f35901..a01c6ee 100644 --- a/BMLite_example/src/main.c +++ b/BMLite_example/src/main.c @@ -41,10 +41,10 @@ static uint8_t hcp_data_buffer[DATA_BUFFER_SIZE]; static HCP_comm_t hcp_chain = { .read = platform_spi_receive, .write = platform_spi_send, - .data_buffer = hcp_data_buffer, + .pkt_buffer = hcp_data_buffer, .txrx_buffer = hcp_txrx_buffer, - .data_size = 0, - .data_size_max = sizeof(hcp_data_buffer), + .pkt_size = 0, + .pkt_size_max = sizeof(hcp_data_buffer), .phy_rx_timeout = 2000, };