From dadfdb85a3a330ec8c38cfff5bad35f3cb3c9ba5 Mon Sep 17 00:00:00 2001 From: Liu_Weichao Date: Tue, 17 May 2022 17:05:40 +0800 Subject: [PATCH] feat support bluetooth on xidatong bsp --- .../Framework/connection/adapter_agent.c | 41 ++++------ .../connection/bluetooth/adapter_bluetooth.c | 7 +- .../connection/bluetooth/hc08/hc08.c | 75 +++++++++++++++++-- 3 files changed, 89 insertions(+), 34 deletions(-) diff --git a/APP_Framework/Framework/connection/adapter_agent.c b/APP_Framework/Framework/connection/adapter_agent.c index 15d1f7641..f5559e0bf 100755 --- a/APP_Framework/Framework/connection/adapter_agent.c +++ b/APP_Framework/Framework/connection/adapter_agent.c @@ -178,7 +178,6 @@ int ATOrderSend(ATAgentType agent, uint32_t timeout_s, ATReplyType reply, const } __out: - // agent->reply = NULL; return result; } @@ -306,7 +305,6 @@ int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s) } PrivMutexObtain(&agent->lock); - printf("EntmRecv once len %d.\n", agent->entm_recv_len); memcpy(rev_buffer, agent->entm_recv_buf, agent->entm_recv_len); @@ -335,29 +333,24 @@ static int GetCompleteATReply(ATAgentType agent) PrivMutexAbandon(&agent->lock); - while (1) - { + while (1) { PrivRead(agent->fd, &ch, 1); #ifdef CONNECTION_FRAMEWORK_DEBUG - if(ch != 0){ + if(ch != 0) { printf(" %c (0x%x)\n", ch, ch); } #endif PrivMutexObtain(&agent->lock); - if (agent->receive_mode == ENTM_MODE) - { - if (agent->entm_recv_len < ENTM_RECV_MAX) - { + if (agent->receive_mode == ENTM_MODE) { + if (agent->entm_recv_len < ENTM_RECV_MAX) { agent->entm_recv_buf[agent->entm_recv_len] = ch; agent->entm_recv_len++; - if(agent->entm_recv_len < agent->read_len){ + if(agent->entm_recv_len < agent->read_len) { PrivMutexAbandon(&agent->lock); continue; - } - else - { + } else { printf("ENTM_MODE recv %d Bytes done.\n",agent->entm_recv_len); agent->receive_mode = DEFAULT_MODE; PrivSemaphoreAbandon(&agent->entm_rx_notice); @@ -367,12 +360,9 @@ static int GetCompleteATReply(ATAgentType agent) printf("entm_recv_buf is_full ...\n"); } } - else if (agent->receive_mode == AT_MODE) - { - if (read_len < agent->maintain_max) - { - if(ch != 0) ///< if the char is null then do not save it to the buff - { + else if (agent->receive_mode == AT_MODE) { + if (read_len < agent->maintain_max) { + if(ch != 0) { ///< if the char is null then do not save it to the buff agent->maintain_buffer[read_len] = ch; read_len++; agent->maintain_len = read_len; @@ -386,7 +376,7 @@ static int GetCompleteATReply(ATAgentType agent) if (((ch == '\n') && (last_ch == '\r') && (agent->reply_lr_end)) || ((ch == agent->reply_end_char) && (agent->reply_end_char) && (last_ch == agent->reply_end_last_char) && (agent->reply_end_last_char)) || - ((read_len == agent->reply_char_num) && (agent->reply_char_num))){ + ((read_len == agent->reply_char_num) && (agent->reply_char_num))) { if (is_full) { printf("read line failed. The line data length is out of buffer size(%d)!", agent->maintain_max); memset(agent->maintain_buffer, 0x00, agent->maintain_max); @@ -471,10 +461,9 @@ static void *ATAgentReceiveProcess(void *param) const struct at_urc *urc; while (1) { - if (GetCompleteATReply(agent) > 0) - { + if (GetCompleteATReply(agent) > 0) { PrivMutexObtain(&agent->lock); - if (agent->reply != NULL){ + if (agent->reply != NULL) { ATReplyType reply = agent->reply; agent->maintain_buffer[agent->maintain_len] = '\0'; @@ -513,13 +502,13 @@ static int ATAgentInit(ATAgentType agent) printf("ATAgentInit create entm sem error\n"); goto __out; } - printf("create entm_rx_notice_sem %d\n ",agent->entm_rx_notice); + result = PrivSemaphoreCreate(&agent->rsp_sem, 0, 0); if (result < 0) { printf("ATAgentInit create rsp sem error\n"); goto __out; } - printf("create rsp_sem %d\n ",agent->rsp_sem); + if(PrivMutexCreate(&agent->lock, 0) < 0) { printf("AdapterFrameworkInit mutex create failed.\n"); goto __out; @@ -539,7 +528,7 @@ static int ATAgentInit(ATAgentType agent) #endif PrivTaskCreate(&agent->at_handler, &attr, ATAgentReceiveProcess, agent); - printf("create agent->at_handler = %d\n",agent->at_handler); + return result; __out: diff --git a/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c b/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c index 8b2c87bbb..03d116376 100644 --- a/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c +++ b/APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c @@ -90,12 +90,15 @@ int AdapterBlueToothTest(void) #ifdef ADAPTER_HC08 AdapterDeviceOpen(adapter); + + //if bluetooth master and slave have already match, no need to AdapterDeviceControl and AdapterDeviceConnect AdapterDeviceControl(adapter, OPE_INT, &baud_rate); + //AdapterDeviceConnect(adapter, adapter->net_role, NULL, NULL, 0); len = strlen(bluetooth_msg); while (1) { - AdapterDeviceRecv(adapter, bluetooth_recv_msg, 128); + AdapterDeviceRecv(adapter, bluetooth_recv_msg, 8); printf("bluetooth_recv_msg %s\n", bluetooth_recv_msg); AdapterDeviceSend(adapter, bluetooth_msg, len); printf("send %s after recv\n", bluetooth_msg); @@ -111,5 +114,5 @@ int AdapterBlueToothTest(void) MSH_CMD_EXPORT(AdapterBlueToothTest,a bt adpter sample); #endif #ifdef ADD_XIZI_FETURES -SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, AdapterWifiTest, AdapterWifiTest, show adapter wifi information); +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, AdapterBlueToothTest, AdapterBlueToothTest, show adapter bluetooth information); #endif \ No newline at end of file diff --git a/APP_Framework/Framework/connection/bluetooth/hc08/hc08.c b/APP_Framework/Framework/connection/bluetooth/hc08/hc08.c index bc20ef8bc..be7ba9e2a 100644 --- a/APP_Framework/Framework/connection/bluetooth/hc08/hc08.c +++ b/APP_Framework/Framework/connection/bluetooth/hc08/hc08.c @@ -37,6 +37,12 @@ #define HC08_SET_ADDR_CMD "AT+ADDR=%s" #define HC08_GET_NAME_CMD "AT+NAME=%s" #define HC08_SET_NAME_CMD "AT+NAME=?" +#define HC08_GET_LUUID_CMD "AT+LUUID=?" +#define HC08_SET_LUUID_CMD "AT+LUUID=%u" +#define HC08_GET_SUUID_CMD "AT+SUUID=?" +#define HC08_SET_SUUID_CMD "AT+SUUID=%u" +#define HC08_GET_TUUID_CMD "AT+TUUID=?" +#define HC08_SET_TUUID_CMD "AT+TUUID=%u" #define HC08_OK_RESP "OK" @@ -60,6 +66,12 @@ enum Hc08AtCmd HC08_AT_CMD_GET_ADDR, HC08_AT_CMD_SET_NAME, HC08_AT_CMD_GET_NAME, + HC08_AT_CMD_SET_LUUID, + HC08_AT_CMD_GET_LUUID, + HC08_AT_CMD_SET_SUUID, + HC08_AT_CMD_GET_SUUID, + HC08_AT_CMD_SET_TUUID, + HC08_AT_CMD_GET_TUUID, HC08_AT_CMD_END, }; @@ -85,7 +97,7 @@ static int Hc08AtConfigure(ATAgentType agent, enum Hc08AtCmd hc08_at_cmd, void * { const char *result_buf; char *connectable, *role; - unsigned int baudrate; + unsigned int baudrate, luuid; char reply_ok_flag = 1; char cmd_str[HC08_CMD_STR_DEFAULT_SIZE] = {0}; @@ -166,6 +178,18 @@ static int Hc08AtConfigure(ATAgentType agent, enum Hc08AtCmd hc08_at_cmd, void * ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_NAME_CMD); reply_ok_flag = 0; break; + case HC08_AT_CMD_GET_LUUID: + AtSetReplyCharNum(agent, 13); + ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_LUUID_CMD); + reply_ok_flag = 0; + break; + case HC08_AT_CMD_SET_LUUID: + luuid = *(unsigned int *)param; + sprintf(cmd_str, HC08_SET_LUUID_CMD, luuid); + AtSetReplyCharNum(agent, 13); + ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd_str); + reply_ok_flag = 0; + break; default: printf("hc08 do not support no.%d cmd\n", hc08_at_cmd); DeleteATReply(reply); @@ -205,10 +229,29 @@ static int Hc08Open(struct Adapter *adapter) return -1; } + struct SerialDataCfg serial_cfg; + memset(&serial_cfg, 0 ,sizeof(struct SerialDataCfg)); + serial_cfg.serial_baud_rate = 9600; + serial_cfg.serial_data_bits = DATA_BITS_8; + serial_cfg.serial_stop_bits = STOP_BITS_1; + serial_cfg.serial_buffer_size = SERIAL_RB_BUFSZ; + serial_cfg.serial_parity_mode = PARITY_NONE; + serial_cfg.serial_bit_order = STOP_BITS_1; + serial_cfg.serial_invert_mode = NRZ_NORMAL; +#ifdef ADAPTER_HC08_DRIVER_EXT_PORT + serial_cfg.ext_uart_no = ADAPTER_HC08_DRIVER_EXT_PORT; + serial_cfg.port_configure = PORT_CFG_INIT; +#endif + + struct PrivIoctlCfg ioctl_cfg; + ioctl_cfg.ioctl_driver_type = SERIAL_TYPE; + ioctl_cfg.args = &serial_cfg; + PrivIoctl(adapter->fd, OPE_INT, &ioctl_cfg); + //step2: init AT agent if (!adapter->agent) { char *agent_name = "bluetooth_uart_client"; - printf("InitATAgent agent_name %s fd %u\n", agent_name, adapter->fd); + if (0 != InitATAgent(agent_name, adapter->fd, 512)) { printf("at agent init failed !\n"); return -1; @@ -239,8 +282,8 @@ static int Hc08Ioctl(struct Adapter *adapter, int cmd, void *args) return -1; } - char hc08_baudrate[HC08_RESP_DEFAULT_SIZE] = {0}; uint32_t baud_rate = *((uint32_t *)args); + uint32_t luuid; struct SerialDataCfg serial_cfg; memset(&serial_cfg, 0 ,sizeof(struct SerialDataCfg)); @@ -256,6 +299,8 @@ static int Hc08Ioctl(struct Adapter *adapter, int cmd, void *args) serial_cfg.port_configure = PORT_CFG_INIT; #endif + serial_cfg.serial_timeout = -1; + struct PrivIoctlCfg ioctl_cfg; ioctl_cfg.ioctl_driver_type = SERIAL_TYPE; ioctl_cfg.args = &serial_cfg; @@ -271,11 +316,29 @@ static int Hc08Ioctl(struct Adapter *adapter, int cmd, void *args) return -1; } - PrivTaskDelay(200); + //Step3 : clear hc08 configure + if (MASTER == adapter->net_role) { + PrivTaskDelay(300); + if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_CLEAR, NULL, NULL) < 0) { + return -1; + } + } + + PrivTaskDelay(500); //Step3 : show hc08 device info, hc08_get send "AT+RX" response device info - char device_info[HC08_RESP_DEFAULT_SIZE * 2] = {0}; - if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_DEVICE_INFO, NULL, device_info) < 0) { + // char device_info[HC08_RESP_DEFAULT_SIZE * 2] = {0}; + // if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_DEVICE_INFO, NULL, device_info) < 0) { + // return -1; + // } + + //Step4 : set LUUID、SUUID、TUUID, slave and master need to have same uuid param + luuid = 1234; + if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_SET_LUUID, &luuid, NULL) < 0) { + return -1; + } + + if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_LUUID, NULL, NULL) < 0) { return -1; }