Refactored code of hcp_tiny

Change-Id: Ib5afe008bb1a6c9690f1450adc16da6dd5041aae
This commit is contained in:
Andrey Perminov 2020-04-30 15:47:17 -07:00
parent fce76eee9e
commit 367e224df3
4 changed files with 145 additions and 282 deletions

View File

@ -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);

View File

@ -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);

View File

@ -1,14 +1,10 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#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;
@ -99,7 +95,7 @@ static fpc_bep_result_t _rx_application(HCP_comm_t *hcp_comm)
uint16_t seq_nr = 0;
uint16_t seq_len = 1;
uint16_t len;
uint8_t *p = hcp_comm->data_buffer;
uint8_t *p = hcp_comm->pkt_buffer;
_HPC_pkt_t *pkt = (_HPC_pkt_t *)hcp_comm->txrx_buffer;
uint16_t buf_len = 0;
@ -110,7 +106,7 @@ static fpc_bep_result_t _rx_application(HCP_comm_t *hcp_comm)
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) {
if(buf_len + len < hcp_comm->pkt_size_max) {
memcpy(p, &pkt->t_pld, len);
p += len;
buf_len += len;
@ -124,7 +120,8 @@ static fpc_bep_result_t _rx_application(HCP_comm_t *hcp_comm)
return status;
}
}
hcp_comm->data_size = buf_len;
hcp_comm->pkt_size = buf_len;
return com_result;
}
@ -149,16 +146,7 @@ static fpc_bep_result_t _rx_link(HCP_comm_t *hcp_comm)
}
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:");
for (int i=0; i<size + 4; i++) {
DEBUG(" 0x%02X", hcp_comm->txrx_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);
@ -177,8 +165,8 @@ 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 data_left = hcp_comm->pkt_size;
uint8_t *p = hcp_comm->pkt_buffer;
_HPC_pkt_t *pkt = (_HPC_pkt_t *)hcp_comm->txrx_buffer;
@ -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);
@ -323,7 +301,4 @@ fpc_bep_result_t receive_result_args2(HCP_comm_t *chain,
}
return FPC_BEP_RESULT_OK;
}

View File

@ -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,
};