Files
kunlun/app/iot_ge_ext_app/iot_cus_ports.c
2024-09-28 14:24:04 +08:00

381 lines
9.3 KiB
C

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
/* os_ship header files */
#include "iot_module_api.h"
#include "iot_errno_api.h"
#include "iot_cus_ports.h"
#include "os_mem_api.h"
#include "iot_cus_task.h"
#include "iot_uart_api.h"
#include "iot_board_api.h"
#define CUS_PORTS_MAX PID_MAX
#define CUS_PORTS_SEND_BUF_SIZE 200
#define CUS_PORTS_RECV_BUF_SIZE 512
#define PORT_CFG_UART NULL
#define PORT_DRV_UART NULL
#if IOT_CUS_PORT_RF_ENABLE
#error "PORT_CFG_RF undefine!!"
#else
#define PORT_CFG_RF NULL
#define PORT_DRV_RF NULL
#endif
#if IOT_CUS_PORT_BLE_ENABLE
#include "iot_cus_port_ble.h"
#define PORT_CUS_BLE_UART_ID UART_CLI_PORT
extern iot_cus_ports_api_t iot_cus_port_ble_api;
#define PORT_DRV_BLE (&iot_cus_port_ble_api)
static iot_cus_port_ble_config_t ble_config = {
.pin_mode = GPIO_NO_VALID,
.role = PORT_BLE_ROLE_AUTO,
.uart_port = UART_EXT_CHIP,
.baudrate = PORT_BLE_UART_BD_RATE
};
#define PORT_CFG_BLE (&ble_config)
#else
#define PORT_CFG_BLE NULL
#define PORT_DRV_BLE NULL
#endif
#if IOT_CUS_PORT_LORA_ENABLE
#include "iot_lora_ext.h"
extern lora_e32_module_cfg_t iot_lora_default_cfg;
extern iot_cus_ports_api_t iot_lora_func_table;
#define PORT_CFG_LORA &iot_lora_default_cfg
#define PORT_DRV_LORA &iot_lora_func_table
#else
#define PORT_CFG_LORA NULL
#define PORT_DRV_LORA NULL
#endif
#if IOT_CUS_PORT_ZIGBEE_ENABLE
#include "iot_cus_port_zigbee.h"
#define PORT_CUS_ZIGBEE_UART_ID UART_CLI_PORT
extern iot_cus_ports_api_t iot_cus_port_zigbee_api;
#define PORT_DRV_ZIGBEE (&iot_cus_port_zigbee_api)
static iot_cus_port_zigbee_config_t zigbee_config = {
.pin_mode = GPIO_NO_VALID,
.role = PORT_ZIGBEE_ROLE_COORDINATOR,
.uart_port = UART_EXT_CHIP,
.baudrate = PORT_ZIGBEE_UART_BD_RATE
};
#define PORT_CFG_ZIGBEE (&zigbee_config)
#else
#define PORT_CFG_ZIGBEE NULL
#define PORT_DRV_ZIGBEE NULL
#endif
#if IOT_CUS_PORT_ETH_ENABLE
#include "httpd_socket_client.h"
#include "iot_cus_port_eth.h"
extern iot_cus_ports_api_t iot_cus_port_eth_api;
#define PORT_DRV_ETH (&iot_cus_port_eth_api)
static iot_eth_config_t eth_config = {
// .local_ip = { 10,0,1,187 },
.local_ip = { 192,168,1,5 },
.mask = { 0xFF,0xFF,0xFF,0x00 },
.gateway = { 192,168,1,1 },
// .gateway = { 10,0,1,2 },
.server_ip = 0x278911DA,//218.17.137.39
// .server_ip = 0x480F000A,//10.0.15.72
.port=8586
// .port=18111
};
#define PORT_CFG_ETH (&eth_config)
#else
#define PORT_CFG_ETH NULL
#define PORT_DRV_ETH NULL
#endif
#if IOT_CUS_APP_MULTI_PORTS
typedef struct _iot_cus_buffer_t {
uint8_t *s; /* start pointer */
uint8_t *e; /* end pointer */
uint8_t *r; /* read pointer */
uint8_t *w; /* write pointer */
}iot_cus_buf_t;
typedef struct _iot_cus_port_t {
void *config;
iot_cus_ports_api_t *drv;
int events;
iot_cus_buf_t *buf;
}iot_cus_port_t;
static iot_cus_port_t ports[PID_MAX] = {
{PORT_CFG_UART, PORT_DRV_UART},
{PORT_CFG_RF, PORT_DRV_RF},
{PORT_CFG_BLE, PORT_DRV_BLE},
{PORT_CFG_LORA, PORT_DRV_LORA},
{PORT_CFG_ZIGBEE, PORT_DRV_ZIGBEE},
{PORT_CFG_ETH, PORT_DRV_ETH},
};
uint8_t rcv_buf[CUS_PORTS_RECV_BUF_SIZE];
#define get_drv(p) (port_valid(p) ? ports[p].drv : NULL)
#define get_ent(p) (port_valid(p) ? &ports[p] : NULL)
#define event_valid(e) ((e) > EVT_INVALID && (e) < EVT_MAX)
void iot_cus_buf_shakeup(iot_cus_buf_t *c_buf)
{
uint32_t dlen;
if ((NULL != c_buf)
&& (c_buf->r > c_buf->s)) {
dlen = c_buf->w - c_buf->r;
os_mem_cpy(c_buf->s, c_buf->r, dlen);
c_buf->w = c_buf->s + dlen;
c_buf->r = c_buf->s;
}
return;
}
uint32_t iot_cus_buf_puts(iot_cus_buf_t *c_buf, uint8_t *buf, uint32_t len)
{
uint32_t size;
if ((NULL == c_buf)
|| (NULL == buf)
|| (0 == len)) {
return 0;
}
size = c_buf->e - c_buf->w;
size = size > len ? len : size;
os_mem_cpy(c_buf->w, buf, size);
c_buf->w += size;
return size;
}
uint8_t *iot_cus_buf_gets(iot_cus_buf_t *c_buf, uint32_t len)
{
uint8_t *org_r;
if (NULL == c_buf) {
return NULL;
}
if (c_buf->w - c_buf->r < len) {
return NULL;
}
org_r = c_buf->r;
c_buf->r += len;
return org_r;
}
uint32_t iot_cus_buf_dlen(iot_cus_buf_t *c_buf)
{
if (NULL == c_buf) {
return 0;
}
return c_buf->w - c_buf->r;
}
iot_cus_buf_t *iot_cus_buf_alloc(uint32_t len)
{
uint8_t *mem;
iot_cus_buf_t *buf;
uint32_t size = sizeof(*buf) + len;
if (NULL == (mem = os_mem_malloc(IOT_CUS_TASK_ID, size))) {
return NULL;
}
buf = (iot_cus_buf_t *)mem;
buf->s = buf->r = buf->w = mem + sizeof(*buf);
buf->e = mem + size;
return buf;
}
uint32_t iot_cus_ports_send_buf(uint32_t port)
{
uint8_t *buf;
uint32_t size;
iot_cus_port_t *p_ent = get_ent(port);
if (NULL == p_ent
|| 0 == iot_cus_buf_dlen(p_ent->buf)
|| (!p_ent->drv->p_ready())) {
return 0;
}
/* Get buf */
buf = iot_cus_buf_gets(p_ent->buf, 0);
/* Get length */
size = iot_cus_buf_dlen(p_ent->buf);
/* Send to port */
size = p_ent->drv->p_write((char *)buf, size);
/* Remove data from buffer */
iot_cus_buf_gets(p_ent->buf, size);
iot_cus_buf_shakeup(p_ent->buf);
return size;
}
uint32_t iot_cus_ports_send(uint32_t port, uint8_t *buf, uint32_t len)
{
int send_bytes = 0;
iot_cus_port_t *p_ent = get_ent(port);
if ((NULL == p_ent) || (NULL == buf) || (0 == len)) {
return send_bytes;
}
if (p_ent->drv->p_ready() && (iot_cus_buf_dlen(p_ent->buf) == 0)) {
/* No pending data, send it now. */
send_bytes = p_ent->drv->p_write((char *)buf, len);
}
if (send_bytes < len) {
/* Left data will be sent next time, store it in buffer. */
send_bytes += iot_cus_buf_puts(p_ent->buf,
buf + send_bytes, len - send_bytes);
}
/* Try to send data. */
iot_cus_ports_send_buf(port);
return send_bytes;
}
uint32_t iot_cus_ports_receive(uint32_t port)
{
uint8_t *buf = rcv_buf;
int size, t_size = 0, len = CUS_PORTS_RECV_BUF_SIZE;
iot_cus_ports_api_t *p_drv = get_drv(port);
if ((NULL == p_drv)
&& (!p_drv->p_ready())) {
return 0;
}
do {
size = p_drv->p_read((char *)buf, len);
iot_cus_task_ports_data_receive(port, buf, size);
t_size += size;
}while (size >= len);
return t_size;
}
void iot_cus_ports_timer_handle(uint32_t delta_time)
{
uint32_t port_idx;
iot_cus_ports_api_t *p_drv;
for (port_idx = 0; port_idx < CUS_PORTS_MAX; port_idx++) {
if (NULL != (p_drv = get_drv(port_idx))) {
p_drv->p_timer(delta_time);
}
}
return;
}
void iot_cus_ports_event_post(uint32_t pid, uint32_t events)
{
iot_cus_port_t *p_ent = get_ent(pid);
if ((!event_valid(events))
|| (NULL == p_ent)) {
return;
}
p_ent->events |= events;
/* Post to cus task && call iot_cus_ports_event_handle */
iot_cus_task_event_post(IOT_CUS_TASK_EID_EXT_PORTS);
return;
}
void iot_cus_ports_event_handle(void)
{
int port_idx, events;
iot_cus_port_t *p_ent;
for (port_idx = 0; port_idx < CUS_PORTS_MAX; port_idx++) {
p_ent = get_ent(port_idx);
events = p_ent->events;
/* Clear events. */
p_ent->events = 0;
if (events & EVT_DOUT_DONE) {
iot_cus_ports_send_buf(port_idx);
} else if (events & EVT_DIN_DONE) {
iot_cus_ports_receive(port_idx);
}
}
return;
}
void iot_cus_all_ports_write(uint8_t *data, uint32_t data_len)
{
int port_idx;
iot_cus_port_t *p_ent;
for (port_idx = 0; port_idx < CUS_PORTS_MAX; port_idx++) {
p_ent = get_ent(port_idx);
if (NULL != p_ent->drv) {
iot_cus_ports_send(port_idx, data, data_len);
}
}
return;
}
uint32_t iot_cus_ports_init(void)
{
uint32_t port_idx;
iot_cus_port_t *p_ent;
for (port_idx = 0; port_idx < CUS_PORTS_MAX; port_idx++) {
/* Will not be NULL */
p_ent = get_ent(port_idx);
if (NULL != p_ent->drv) {
if (ERR_OK != p_ent->drv->p_init(port_idx, p_ent->config)) {
return ERR_FAIL;
}
if (NULL == (p_ent->buf = iot_cus_buf_alloc
(CUS_PORTS_SEND_BUF_SIZE))) {
/* Just return, not roll back installed ports. */
return ERR_FAIL;
}
}
}
return ERR_OK;
}
#endif /* IOT_CUS_APP_MULTI_PORTS */