272 lines
8.6 KiB
C
272 lines
8.6 KiB
C
#include "nh_shared_if.h"
|
|
|
|
#include "string.h"
|
|
|
|
#define NH_XFER_REQ_TIMEOUT 5000
|
|
#define NH_XFER_MAX_SIZE 1600
|
|
#define NH_XFER_QUEUE_SIZE 1
|
|
|
|
#ifdef __GNUC__
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
#define H_TO_LE16(x) (x)
|
|
#define LE16_TO_H(x) (x)
|
|
#else
|
|
#define H_TO_LE16(x) (((x & 0xFFU) << 8U) | (x >> 8U))
|
|
#endif
|
|
#else
|
|
#warning "This code only supports GCC for now."
|
|
#endif
|
|
|
|
typedef enum {
|
|
NH_SHARED_IF_TYPE_STA = 0U,
|
|
NH_SHARED_IF_TYPE_AP = 1U,
|
|
NH_SHARED_IF_TYPE_SERIAL = 2U,
|
|
NH_SHARED_IF_TYPE_PRIV = 4U,
|
|
} nh_xfer_type_t;
|
|
|
|
typedef struct {
|
|
nh_xfer_type_t type;
|
|
uint8_t *buf;
|
|
uint32_t buf_len;
|
|
} nh_xfer_queue_item_t;
|
|
|
|
typedef struct __attribute__((packed)) {
|
|
uint8_t if_type : 4; /* Offset: 0x00 */
|
|
uint8_t if_num : 4; /* Offset: 0x00 */
|
|
uint8_t flags; /* Offset: 0x01 */
|
|
uint16_t len; /* Offset: 0x02 */
|
|
uint16_t offset; /* Offset: 0x04 */
|
|
uint16_t checksum; /* Offset: 0x06 */
|
|
uint16_t seq_num; /* Offset: 0x08 */
|
|
uint8_t reserved2; /* Offset: 0x0A */
|
|
union {
|
|
uint8_t reserved3;
|
|
uint8_t hci_pkt_type;
|
|
uint8_t priv_pkt_type;
|
|
}; /* Offset: 0x0B */
|
|
} nh_xfer_pkt_hdr_t; /* Size: 0x0C (12 bytes) */
|
|
|
|
static uint16_t nh_shared_if_checksum_calculate(nh_xfer_pkt_hdr_t *hdr);
|
|
static bool nh_shared_if_checksum_verify(nh_xfer_pkt_hdr_t *hdr);
|
|
|
|
nh_ret_t nh_shared_if_init(nh_shared_if_t *shared_if) {
|
|
nh_ret_t ret;
|
|
|
|
ret = shared_if->osa->sem_create(shared_if->osa->user_data, &shared_if->p_semaphore_xfer_req);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return ret;
|
|
}
|
|
|
|
ret = shared_if->osa->queue_create(shared_if->osa->user_data, &shared_if->p_queue_tx, sizeof(nh_xfer_queue_item_t),
|
|
NH_XFER_QUEUE_SIZE);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
goto err_free_sem_xfer;
|
|
}
|
|
|
|
ret = shared_if->osa->queue_create(shared_if->osa->user_data, &shared_if->p_queue_rx_data,
|
|
sizeof(nh_xfer_queue_item_t), NH_XFER_QUEUE_SIZE);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
goto err_free_queue_tx;
|
|
}
|
|
|
|
ret = shared_if->osa->queue_create(shared_if->osa->user_data, &shared_if->p_queue_rx_ctrl,
|
|
sizeof(nh_xfer_queue_item_t), NH_XFER_QUEUE_SIZE);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
goto err_free_queue_rx_data;
|
|
}
|
|
|
|
ret = shared_if->osa->buf_allocate(shared_if->osa->user_data, &shared_if->p_buf_frame_tx, NH_XFER_MAX_SIZE);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
goto err_free_queue_rx_ctrl;
|
|
}
|
|
|
|
ret = shared_if->osa->buf_allocate(shared_if->osa->user_data, &shared_if->p_buf_frame_rx, NH_XFER_MAX_SIZE);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
goto err_free_tx_buf;
|
|
}
|
|
|
|
return ret;
|
|
|
|
err_free_tx_buf:
|
|
shared_if->osa->buf_free(shared_if->osa->user_data, shared_if->p_buf_frame_tx);
|
|
|
|
err_free_queue_rx_ctrl:
|
|
shared_if->osa->queue_destroy(shared_if->osa->user_data, shared_if->p_queue_rx_ctrl);
|
|
|
|
err_free_queue_rx_data:
|
|
shared_if->osa->queue_destroy(shared_if->osa->user_data, shared_if->p_queue_rx_data);
|
|
|
|
err_free_queue_tx:
|
|
shared_if->osa->queue_destroy(shared_if->osa->user_data, shared_if->p_queue_tx);
|
|
|
|
err_free_sem_xfer:
|
|
shared_if->osa->sem_destroy(shared_if->osa->user_data, shared_if->p_semaphore_xfer_req);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void nh_shared_if_inject_data_ready(nh_shared_if_t *shared_if) {
|
|
shared_if->osa->sem_give(shared_if->osa->user_data, shared_if->p_semaphore_xfer_req);
|
|
}
|
|
|
|
nh_ret_t nh_shared_if_ctrl_send(nh_shared_if_t *shared_if, uint8_t *tx_payload, uint32_t len, uint32_t timeout_ms) {
|
|
nh_ret_t ret = NH_RET_SUCCESS;
|
|
|
|
nh_xfer_queue_item_t item = {
|
|
.type = NH_SHARED_IF_TYPE_SERIAL,
|
|
.buf_len = len,
|
|
};
|
|
|
|
ret = shared_if->osa->buf_allocate(shared_if->osa->user_data, &item.buf, len);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return ret;
|
|
}
|
|
|
|
ret = shared_if->osa->queue_enqueue(shared_if->osa->user_data, shared_if->p_queue_tx, &item, timeout_ms);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
shared_if->osa->buf_free(shared_if->osa->user_data, item.buf);
|
|
|
|
return ret;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
nh_ret_t nh_shared_if_ctrl_recv(nh_shared_if_t *shared_if, uint8_t **rx_payload, uint32_t *len, uint32_t timeout_ms) {
|
|
nh_ret_t ret = NH_RET_SUCCESS;
|
|
|
|
nh_xfer_queue_item_t item;
|
|
|
|
ret = shared_if->osa->queue_dequeue(shared_if->osa->user_data, shared_if->p_queue_rx_ctrl, &item, timeout_ms);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return ret;
|
|
}
|
|
|
|
*rx_payload = item.buf;
|
|
*len = item.buf_len;
|
|
|
|
return ret;
|
|
}
|
|
|
|
nh_ret_t nh_shared_if_ctrl_free(nh_shared_if_t *shared_if, uint8_t *rx_payload) {
|
|
return shared_if->osa->buf_free(shared_if->osa->user_data, rx_payload);
|
|
}
|
|
|
|
void nh_shared_if_task(nh_shared_if_t *shared_if) {
|
|
nh_ret_t ret;
|
|
|
|
ret = shared_if->osa->sem_take(shared_if->osa->user_data, shared_if->p_semaphore_xfer_req, NH_XFER_REQ_TIMEOUT);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return;
|
|
}
|
|
|
|
/* Taken the xfer request semaphore. The possible reasons are:
|
|
* 1: Some data (could from any interfaces) needs to be sent
|
|
* 2: Data ready pin is high when HS at rising, some data needs to be received
|
|
*/
|
|
|
|
bool rx_available = false;
|
|
|
|
/* Check if new data is available */
|
|
ret = shared_if->ops.drdy_read(shared_if->user_data, &rx_available);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return;
|
|
}
|
|
|
|
nh_xfer_queue_item_t item;
|
|
|
|
/* Clear TX header */
|
|
memset(shared_if->p_buf_frame_tx, 0U, sizeof(nh_xfer_pkt_hdr_t));
|
|
|
|
/* Check if we have data to send */
|
|
ret = shared_if->osa->queue_dequeue(shared_if->user_data, shared_if->p_queue_tx, &item, 0U);
|
|
if (ret == NH_RET_SUCCESS) {
|
|
/* Write header and payload since we have data to send */
|
|
nh_xfer_pkt_hdr_t *hdr = (nh_xfer_pkt_hdr_t *)shared_if->p_buf_frame_tx;
|
|
|
|
hdr->if_type = item.type;
|
|
hdr->offset = H_TO_LE16(sizeof(nh_xfer_pkt_hdr_t)); /* Payload follows header... */
|
|
hdr->len = H_TO_LE16(item.buf_len);
|
|
hdr->checksum = H_TO_LE16(nh_shared_if_checksum_calculate(hdr));
|
|
|
|
/* Copy payload */
|
|
memcpy(&shared_if->p_buf_frame_tx[sizeof(nh_xfer_pkt_hdr_t)], item.buf, item.buf_len);
|
|
|
|
/* Queue consumer owns the buffer. */
|
|
shared_if->osa->buf_free(shared_if->osa->user_data, item.buf);
|
|
}
|
|
|
|
/* Exchange buffers */
|
|
ret = shared_if->ops.xfer(shared_if->user_data, shared_if->p_buf_frame_tx, shared_if->p_buf_frame_rx,
|
|
NH_XFER_MAX_SIZE);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return;
|
|
}
|
|
|
|
/* If we have RX data... */
|
|
if (rx_available) {
|
|
nh_xfer_pkt_hdr_t *hdr = (nh_xfer_pkt_hdr_t *)shared_if->p_buf_frame_rx;
|
|
if (!nh_shared_if_checksum_verify(hdr)) {
|
|
return;
|
|
}
|
|
|
|
item.type = LE16_TO_H(hdr->if_type);
|
|
item.buf_len = LE16_TO_H(hdr->len);
|
|
|
|
uint16_t payload_offset = LE16_TO_H(hdr->offset);
|
|
|
|
if (item.type == NH_SHARED_IF_TYPE_SERIAL) {
|
|
if (item.buf_len) {
|
|
/* Allocate payload */
|
|
ret = shared_if->osa->buf_allocate(shared_if->osa->user_data, &item.buf, item.buf_len);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
return;
|
|
}
|
|
|
|
memcpy(item.buf, &shared_if->p_buf_frame_rx[payload_offset], item.buf_len);
|
|
}
|
|
|
|
/* Enqueue response */
|
|
ret = shared_if->osa->queue_enqueue(shared_if->osa->user_data, shared_if->p_queue_rx_ctrl, &item, 0);
|
|
if (ret != NH_RET_SUCCESS) {
|
|
/* Queue is full, maybe the ctrl interface is not active. */
|
|
shared_if->osa->buf_free(shared_if->osa->user_data, item.buf);
|
|
}
|
|
} else if (item.type == NH_SHARED_IF_TYPE_PRIV) {
|
|
/* Event is using PRIV interface. */
|
|
if (item.buf_len) {
|
|
if (shared_if->cb.event) {
|
|
/* Note: This is a blocking callback !! */
|
|
shared_if->cb.event(shared_if->user_data, &shared_if->p_buf_frame_rx[payload_offset]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static uint16_t nh_shared_if_checksum_calculate(nh_xfer_pkt_hdr_t *hdr) {
|
|
uint16_t checksum = 0U;
|
|
|
|
uint16_t checksum_length = sizeof(nh_xfer_pkt_hdr_t) + LE16_TO_H(hdr->len);
|
|
|
|
for (size_t i = 0; i < checksum_length; i++) {
|
|
checksum += ((uint8_t *)hdr)[i];
|
|
}
|
|
|
|
return checksum;
|
|
}
|
|
|
|
static bool nh_shared_if_checksum_verify(nh_xfer_pkt_hdr_t *hdr) {
|
|
bool ret = true;
|
|
|
|
uint16_t checksum = LE16_TO_H(hdr->checksum);
|
|
hdr->checksum = 0U;
|
|
|
|
if (checksum != nh_shared_if_checksum_calculate(hdr)) {
|
|
return false;
|
|
}
|
|
|
|
hdr->checksum = H_TO_LE16(checksum);
|
|
|
|
return ret;
|
|
} |