123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- /************************************************************************************************
- * Include *
- ************************************************************************************************/
- #include "ble.h"
- #include "ble_cmd.h"
- #include "ble_sn2model.h"
- #include "stm32f4xx_gpio.h"
- #include "systick.h"
- #include "uart.h"
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- /************************************************************************************************
- * Config *
- ************************************************************************************************/
- uint8_t zero_sn[];
- extern BLE_CMD_ASYN g_ble_cmd_asyn_table[];
- static uint8_t BUSY_IND = 0; // 0、空闲;1、繁忙
- static uint8_t AUTH_IND = 0; // 0、未认证;1、认证
- static uint8_t BLE_CONN_FLG = 0;
- uint8_t g_ble_mac[MAC_LEN] = {0};
- uint8_t g_ble_name[BLE_NAME_LEN + 1] = {0};
- uint8_t g_ble_adv_data[28] = {0};
- uint8_t g_ble_rst = 0; // 0、运行;1、复位
- BLE_CMD_CACHE g_ble_cmd_cache; // 命令缓存
- /************************************************************************************************
- * Data structs *
- ************************************************************************************************/
- void ble_reset(void)
- {
- GPIO_ResetBits(BT_RST_PORT, BT_RST_PIN);
- ms_delay(1);
- GPIO_SetBits(BT_RST_PORT, BT_RST_PIN);
- }
- void ble_init(void)
- {
- usart_config_init(&usart2_context, 9600);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- GPIO_InitTypeDef GPIO_StructInit;
- GPIO_StructInit.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
- GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_StructInit.GPIO_Pin = BT_RST_PIN;
- GPIO_Init(BT_RST_PORT, &GPIO_StructInit);
- ble_reset();
- GPIO_StructInit.GPIO_Mode = GPIO_Mode_IN;
- GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
- GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_StructInit.GPIO_Pin = BT_BUSY_IND_PIN;
- GPIO_Init(BT_BUSY_IND_PORT, &GPIO_StructInit);
- GPIO_StructInit.GPIO_Mode = GPIO_Mode_IN;
- GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
- GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_StructInit.GPIO_Pin = BT_CONN_IND_PIN;
- GPIO_Init(BT_CONN_IND_PORT, &GPIO_StructInit);
- }
- // 蓝牙名称
- int32_t ble_cmd08_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
- {
- RSP_CMD08 *ptr;
- if (status != 0)
- {
- return status;
- }
- ptr = (RSP_CMD08 *)data;
- strcpy(g_ble_name, ptr->name);
- return 0;
- }
- // MAC地址
- int32_t ble_cmd18_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
- {
- RSP_CMD18 *ptr;
- if (status != 0)
- {
- return status;
- }
- ptr = (RSP_CMD18 *)data;
- if (ptr->item0.item == 3)
- {
- memcpy(g_ble_mac, ptr->item3.mac, sizeof(g_ble_mac));
- // OPP_SWAP_N(g_ble_mac, mac_len);
- }
- return 0;
- }
- // 广播数据
- int32_t ble_cmd0e_action_asyn(uint8_t cmd, uint8_t status, uint8_t *data, uint8_t len)
- {
- RSP_CMD0E *ptr;
- if (status != 0)
- {
- return status;
- }
- ptr = (RSP_CMD0E *)data;
- memcpy(g_ble_adv_data, ptr->adv_data, sizeof(g_ble_adv_data));
- return 0;
- }
- // 序列初始化
- int32_t ble_cfg_init_asyn(EN_BLE_INIT_CMD cmd)
- {
- int32_t ret;
- REQ_CMD07 cmd07;
- REQ_CMD08 cmd08;
- uint8_t SN[SN_LEN] = {0};
- uint8_t UPP[SN_LEN] = {0};
- uint8_t model[MODEL_LEN] = {0};
- uint8_t upp_model[MODEL_LEN] = {0};
- uint8_t buff[64] = {0};
- if (g_ble_cmd_cache.is_used == 1)
- {
- return 1;
- }
- if (cmd == BLE_INIT_CMD_NAME_QRY)
- {
- g_ble_cmd_cache.is_used = 1;
- g_ble_cmd_cache.cmd = 0x08;
- g_ble_cmd_cache.len = sizeof(REQ_CMD08);
- memcpy(g_ble_cmd_cache.data, &cmd08, sizeof(REQ_CMD08));
- g_ble_cmd_cache.p_func = ble_cmd08_action_asyn;
- g_ble_cmd_cache.tick = 0;
- }
- if (cmd == BLE_INIT_CMD_NAME_CFG)
- {
- // int32_t len = alientek_sn_get(SN);
- int32_t len = 12;
- ret = sn_2_model(SN, 1, model);
- if (ret == 0)
- {
- len = sprintf(buff, "%s", model);
- // upp_sn_get(UPP);
- if (memcpy(UPP, zero_sn, SN_LEN) != 0)
- {
- ret = sn_2_model((uint8_t *)UPP, 1, upp_model);
- if (ret == 0)
- {
- sprintf(buff + len, " %s", upp_model);
- }
- }
- if (strcpy(g_ble_name, buff) != 0)
- {
- strcpy(cmd07.name, buff);
- g_ble_cmd_cache.cmd = 0x07;
- g_ble_cmd_cache.len = strlen(buff);
- strcpy(&g_ble_cmd_cache.data, buff);
- g_ble_cmd_cache.p_func = NULL;
- g_ble_cmd_cache.tick = 0;
- }
- }
- }
- if (cmd == BLE_INIT_CMD_TXWPR_CFG)
- {
- REQ_CMD0B req0b;
- req0b.tx_power = 8;
- ret = ble_exec_cmd0b(&req0b);
- if (ret == 0)
- {
- }
- }
- return 0;
- }
- int32_t ble_cfg_init_do_asyn(void)
- {
- static EN_BLE_INIT_CMD cmd = BLE_INIT_CMD_NAME_QRY;
- int32_t ret;
- ret = ble_cfg_init_asyn(cmd);
- if (ret == 0)
- {
- cmd++;
- }
- if (cmd == BLE_INIT_CMD_MAX)
- {
- cmd = BLE_INIT_CMD_NAME_QRY;
- return 0;
- }
- return 1;
- }
- int32_t ble_cfg_init(void)
- {
- int32_t ret;
- REQ_CMD07 cmd07;
- REQ_CMD08 cmd08;
- RSP_CMD08 rsp08;
- uint8_t SN[SN_LEN] = {0};
- uint8_t model[MODEL_LEN] = {0};
- memset(&rsp08, 0, sizeof(RSP_CMD08));
- ret = ble_exec_cmd08(&cmd08, &rsp08);
- if (ret == 0)
- {
- }
- int32_t len = 12;
- // int32_t len = alientek_sn_get(SN);
- ret = sn_2_model(SN, 1, model);
- if (ret == 0)
- {
- if (strcmp(rsp08.name, model) != 0)
- {
- strcpy(cmd07.name, model);
- ret = ble_exec_cmd07(&cmd07, CMD_T0);
- if (ret != 0)
- {
- }
- }
- }
- else
- {
- }
- return 0;
- }
- int32_t ble_cfg_query_asyn(EN_BLE_INIT_CMD cmd)
- {
- REQ_CMD18 cmd18;
- REQ_CMD0E cmd0e;
- uint8_t buff[64] = {0};
- int32_t len = 0;
- if (g_ble_cmd_cache.is_used == 1)
- {
- return 1;
- }
- if (cmd == BLE_QRY_CMD_ADV_QRY)
- {
- g_ble_cmd_cache.is_used = 1;
- g_ble_cmd_cache.cmd = 0x0e;
- g_ble_cmd_cache.len = sizeof(REQ_CMD0E);
- memcpy(g_ble_cmd_cache.data, &cmd0e, sizeof(REQ_CMD0E));
- g_ble_cmd_cache.p_func = ble_cmd0e_action_asyn;
- g_ble_cmd_cache.tick = 0;
- }
- if (cmd == BLE_QRY_CMD_MAC_QRY)
- {
- cmd18.item = 3;
- g_ble_cmd_cache.is_used = 1;
- g_ble_cmd_cache.cmd = 0x18;
- g_ble_cmd_cache.len = sizeof(REQ_CMD18);
- memcpy(g_ble_cmd_cache.data, &cmd0e, sizeof(REQ_CMD18));
- g_ble_cmd_cache.p_func = ble_cmd18_action_asyn;
- g_ble_cmd_cache.tick = 0;
- }
- if (cmd == BLE_QRY_CMD_ADV_CFG)
- {
- if (memcpy(g_ble_mac, g_ble_adv_data, MAC_LEN) != 0)
- {
- buff[len++] = 1;
- buff[len++] = 1 + MAC_LEN;
- buff[len++] = 0xFF;
- memcpy(&buff[len], g_ble_mac, MAC_LEN);
- len += MAC_LEN;
- g_ble_cmd_cache.is_used = 1;
- g_ble_cmd_cache.cmd = 0x0d;
- g_ble_cmd_cache.len = len;
- memcpy(g_ble_cmd_cache.data, buff, len);
- g_ble_cmd_cache.p_func = NULL;
- g_ble_cmd_cache.tick = 0;
- }
- }
- return 0;
- }
- int32_t ble_cfg_query_do_asyn(void)
- {
- static EN_BLE_QRY_CMD cmd = BLE_QRY_CMD_ADV_QRY;
- int32_t ret;
- ret = ble_cfg_query_asyn(cmd);
- if (ret == 0)
- {
- cmd++;
- }
- if (cmd == BLE_QRY_CMD_MAX)
- {
- cmd = BLE_QRY_CMD_ADV_QRY;
- return 0;
- }
- return 1;
- }
- int32_t ble_cfg_query(void)
- {
- int32_t ret;
- REQ_CMD18 cmd18;
- RSP_CMD18 rsp18;
- REQ_CMD0A cmd0a;
- RSP_CMD0A rsp0a;
- REQ_CMD0C cmd0c;
- RSP_CMD0C rsp0c;
- REQ_CMD0E cmd0e;
- RSP_CMD0E rsp0e;
- uint8_t buff[64] = {0};
- int32_t len = 0;
- int32_t adv_data_len = 0;
- uint8_t *adv_data_ptr = NULL;
- uint8_t adv_data_uuid = 0;
- ret = ble_exec_cmd0e(&cmd0e, &rsp0e);
- if (ret == 0)
- {
- adv_data_len = rsp0e.adv_data[0];
- adv_data_uuid = rsp0e.adv_data[1];
- adv_data_ptr = &rsp0e.adv_data[2];
- // len += sprintf(buff+len)
- }
- cmd18.item = 3;
- ret = ble_exec_cmd18(&cmd18, &rsp18);
- if (ret == 0)
- {
- memcpy(g_ble_mac, rsp18.item3.mac, len);
- // OPP_SWAP_N(g_ble_mac, mac_len);
- if (adv_data_len == 0 || adv_data_ptr == NULL || memcmp(rsp18.item3.mac, adv_data_ptr, MAC_LEN) != 0)
- {
- ret = ble_exec_cmd0d(1, rsp18.item3.mac, MAC_LEN);
- if (ret != 0)
- {
- }
- }
- }
- else
- {
- }
- cmd18.item = 5;
- ret = ble_exec_cmd18(&cmd18, &rsp18);
- if (ret == 0)
- {
- }
- ret = ble_exec_cmd0a(&cmd0a, &rsp0a);
- if (ret == 0)
- {
- }
- ret = ble_exec_cmd0c(&cmd0c, &rsp0c);
- if (ret == 0)
- {
- }
- }
- int32_t BUSY_IND_LOOP(void)
- {
- static uint32_t tick = 0;
- static uint32_t cnt = 0;
- uint8_t busy;
- if (tick == 0)
- {
- tick = get_systick_ms();
- }
- if (get_systick_ms() - tick > 50)
- {
- if (cnt++ < 2)
- {
- return 0;
- }
- else
- {
- cnt = 0;
- }
- if (GPIO_ReadInputDataBit(BT_BUSY_IND_PORT, BT_BUSY_IND_PIN) == 0)
- {
- busy = 1;
- }
- else
- {
- busy = 0;
- }
- if (BUSY_IND != busy)
- {
- BUSY_IND = busy;
- }
- }
- return 0;
- }
- int32_t CONN_IND_LOOP(void)
- {
- static uint32_t tick = 0;
- static uint32_t cnt = 0;
- uint8_t conn;
- if (tick == 0)
- {
- tick = get_systick_ms();
- }
- if (get_systick_ms() - tick > 50)
- {
- if (cnt++ < 2)
- {
- return 0;
- }
- else
- {
- cnt = 0;
- }
- if (GPIO_ReadInputDataBit(BT_CONN_IND_PORT, BT_CONN_IND_PIN) == 0)
- {
- conn = 1;
- }
- else
- {
- conn = 0;
- }
- if (BLE_CONN_FLG != conn)
- {
- BLE_CONN_FLG = conn;
- if (BLE_CONN_FLG == 0)
- {
- AUTH_IND = 0;
- }
- }
- }
- return 0;
- }
- int32_t auth_ind_set(uint8_t auth)
- {
- AUTH_IND = auth;
- return 0;
- }
- int32_t ble_is_conn(void)
- {
- return BLE_CONN_FLG;
- }
- int32_t ble_get_busy_port_sts(void)
- {
- return BUSY_IND;
- }
- int32_t ble_is_busy(void)
- {
- if (BLE_CONN_FLG == 0 || BUSY_IND == 1 || g_ble_cmd_cache.is_used == 1)
- {
- return 1;
- }
- return 0;
- }
- int32_t ble_is_auth(void)
- {
- return AUTH_IND;
- }
- int32_t ble_data_recv(uint8_t data[], uint8_t length, int32_t ms)
- {
- static uint32_t tick = 0;
- uint8_t rdata[BLE_MTU];
- int32_t rlen;
- if (tick == 0)
- {
- tick = get_systick_ms();
- }
- do
- {
- rlen = usart_read_ble_format(&usart2_context);
- if (rlen > 0)
- {
- usart_read_update(&usart2_context, rlen, (uint8_t *)rdata);
- if (rdata[2] == g_ble_cmd_cache.cmd)
- {
- if (g_ble_cmd_cache.p_func != NULL)
- {
- g_ble_cmd_cache.p_func(rdata[2], rdata[4], rdata, rlen);
- }
- memset(&g_ble_cmd_cache, 0, sizeof(BLE_CMD_CACHE));
- }
- if (rdata[2] == 0x42)
- {
- // ble_2_app((uint8_t *)&rdata[4], rlen - 4);
- tick = 0;
- return 0;
- }
- else
- {
- memcpy(data, rdata, rlen);
- tick = 0;
- return rlen;
- }
- }
- else
- {
- }
- } while (get_systick_ms() - tick < ms);
- tick = 0;
- return -1;
- }
- int32_t ble_send_data(uint8_t data[], int32_t length)
- {
- if (length > BLE_MTU)
- {
- return 1;
- }
- g_ble_cmd_cache.is_used = 1;
- g_ble_cmd_cache.cmd = 0x41;
- g_ble_cmd_cache.len = length;
- memcpy(g_ble_cmd_cache.data, data, length);
- g_ble_cmd_cache.tick = 0;
- g_ble_cmd_cache.p_func = NULL;
- return 0;
- }
- int32_t ble_rcv_loop(void)
- {
- uint8_t rdata[BLE_MTU];
- ble_data_recv(rdata, sizeof(rdata), 0);
- return 0;
- }
- int32_t ble_name_modify(void)
- {
- static uint8_t last_sn[SN_LEN] = {0};
- if (ble_is_conn())
- {
- return 0;
- }
- }
- int32_t ble_tx_loop(void)
- {
- static uint8_t count = 0;
- if (g_ble_cmd_cache.is_used == 1)
- {
- if (g_ble_cmd_cache.tick == 0)
- {
- g_ble_cmd_cache.tick = get_systick_ms();
- count = 0;
- }
- if (count == 0 || get_systick_ms() - g_ble_cmd_cache.tick > 3000)
- {
- for (int32_t i = 0; i < countof_asyn_cmd_table(); i++)
- {
- if (g_ble_cmd_asyn_table[i].cmd == g_ble_cmd_cache.cmd && g_ble_cmd_asyn_table[i].p_func != NULL)
- {
- g_ble_cmd_asyn_table[i].p_func(g_ble_cmd_cache.cmd, g_ble_cmd_cache.data, g_ble_cmd_cache.len);
- }
- }
- g_ble_cmd_cache.tick = get_systick_ms();
- count++;
- }
- if (count > 10)
- {
- ble_reset();
- g_ble_rst = 1;
- memset(&g_ble_cmd_cache, 0, sizeof(BLE_CMD_CACHE));
- count = 0;
- }
- }
- return 0;
- }
- int32_t ble_loop(void)
- {
- static uint8_t init = 0;
- static uint32_t tick = 0;
- static uint8_t init1 = 0;
- static uint32_t tick1 = 0;
- int32_t ret;
- if (g_ble_rst == 1)
- {
- init = 0;
- init1 = 0;
- g_ble_rst = 0;
- }
- if (!init)
- {
- if (tick == 0)
- tick = get_systick_ms();
- if (get_systick_ms() - tick > BLE_CFG_DELAY_MS)
- {
- ret = ble_cfg_init_do_asyn();
- if (ret == 0)
- {
- init = 1;
- }
- }
- }
- if (init && !init1)
- {
- if (tick1 == 0)
- tick1 = get_systick_ms();
- if (get_systick_ms() - tick1 > BLE_QRY_DELAY_MS)
- {
- ret = ble_cfg_query_do_asyn();
- if (ret == 0)
- {
- init1 = 1;
- }
- }
- }
- ble_rcv_loop();
- if (init && init1)
- {
- BUSY_IND_LOOP();
- CONN_IND_LOOP();
- ble_name_modify();
- }
- ble_tx_loop();
- return 0;
- }
- /************************************************************************************************
- * implements *
- ************************************************************************************************/
|