758 lines
31 KiB
C
758 lines
31 KiB
C
#include "McuTaskBlueTooth.h"
|
||
#include "McuE104BT52Drv.h"
|
||
#include "ZthyToolCheck.h"
|
||
#include "McuBspUart.h"
|
||
#include "McuBspGpio.h"
|
||
#include "McuTaskFlash.h"
|
||
#include "McuTaskImu.h"
|
||
#include "McuTaskInPara.h"
|
||
#include "McuTaskManyPara.h"
|
||
#include "McuTaskPosition.h"
|
||
#include "McuBspRtc.h"
|
||
#include "McuBspTimer.h"
|
||
#include "McuSelfDestructionDrv.h"
|
||
#include "McuTaskLowPower.h"
|
||
#include "BeiDouHtyDrv.h"
|
||
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
|
||
/* 控制引脚配置 */
|
||
#define MCU_TASK_BLUETOOTH_ONOFF_GPIO_PORT GPIOD
|
||
#define MCU_TASK_BLUETOOTH_ONOFF_GPIO_PIN GPIO_PIN_7
|
||
#define MCU_TASK_BLUETOOTH_ONOFF_GPIO_MODE GPIO_MODE_OUTPUT_PP
|
||
#define MCU_TASK_BLUETOOTH_ONOFF_GPIO_SPEED GPIO_SPEED_FREQ_LOW
|
||
#define MCU_TASK_BLUETOOTH_ONOFF_GPIO_PULL GPIO_PULLDOWN
|
||
|
||
/* DATA引脚配置 */
|
||
#define MCU_TASK_BLUETOOTH_DATA_GPIO_PORT GPIOD
|
||
#define MCU_TASK_BLUETOOTH_DATA_GPIO_PIN GPIO_PIN_3
|
||
#define MCU_TASK_BLUETOOTH_DATA_GPIO_MODE GPIO_MODE_INPUT
|
||
#define MCU_TASK_BLUETOOTH_DATA_GPIO_SPEED GPIO_SPEED_FREQ_LOW
|
||
#define MCU_TASK_BLUETOOTH_DATA_GPIO_PULL GPIO_PULLDOWN
|
||
|
||
/* LINK引脚配置 */
|
||
#define MCU_TASK_BLUETOOTH_LINK_GPIO_PORT GPIOD
|
||
#define MCU_TASK_BLUETOOTH_LINK_GPIO_PIN GPIO_PIN_4
|
||
#define MCU_TASK_BLUETOOTH_LINK_GPIO_MODE GPIO_MODE_IT_RISING
|
||
#define MCU_TASK_BLUETOOTH_LINK_GPIO_SPEED GPIO_SPEED_FREQ_HIGH
|
||
#define MCU_TASK_BLUETOOTH_LINK_GPIO_PULL GPIO_PULLDOWN
|
||
|
||
/* WKP引脚配置 */
|
||
#define MCU_TASK_BLUETOOTH_WKP_GPIO_PORT GPIOD
|
||
#define MCU_TASK_BLUETOOTH_WKP_GPIO_PIN GPIO_PIN_5
|
||
#define MCU_TASK_BLUETOOTH_WKP_GPIO_MODE GPIO_MODE_OUTPUT_PP
|
||
#define MCU_TASK_BLUETOOTH_WKP_GPIO_SPEED GPIO_SPEED_FREQ_LOW
|
||
#define MCU_TASK_BLUETOOTH_WKP_GPIO_PULL GPIO_PULLUP
|
||
|
||
/* E_RST引脚配置 */
|
||
#define MCU_TASK_BLUETOOTH_RST_GPIO_PORT GPIOD
|
||
#define MCU_TASK_BLUETOOTH_RST_GPIO_PIN GPIO_PIN_6
|
||
#define MCU_TASK_BLUETOOTH_RST_GPIO_MODE GPIO_MODE_OUTPUT_PP
|
||
#define MCU_TASK_BLUETOOTH_RST_GPIO_SPEED GPIO_SPEED_FREQ_LOW
|
||
#define MCU_TASK_BLUETOOTH_RST_GPIO_PULL GPIO_PULLUP
|
||
|
||
/* 蓝牙的通讯模式 */
|
||
#define MCU_TASK_BLUETOOTH_MODE_AT 0
|
||
#define MCU_TASK_BLUETOOTH_MODE_TRANS 1
|
||
|
||
/* 透传模式下的蓝牙接收缓存大小定义 */
|
||
#define TASK_BLUETOOTH_RECV_BUF_NUM 30
|
||
#define TASK_BLUETOOTH_RECV_BUF_LEN_MAX 220
|
||
#define TASK_BLUETOOTH_SEND_BUF_LEN_MAX 220
|
||
|
||
/* 蓝牙接收buf的状态宏定义 */
|
||
#define TASK_BLUETOOTH_RECV_BUF_IDLE_STATE 0
|
||
#define TASK_BLUETOOTH_RECV_BUF_SATRT_STATE 1
|
||
#define TASK_BLUETOOTH_RECV_BUF_END_STATE 2
|
||
|
||
#define MCU_TASK_BLUETOOTH_UART_ID MCU_BSP_UART5_ID
|
||
|
||
static uint32_t gDeviceType = 0; // 设备类型
|
||
static uint32_t gDeviceId = 3; // 设备ID
|
||
|
||
static uint8_t gBlueToothMode = MCU_TASK_BLUETOOTH_MODE_AT; // 蓝牙默认处在的模式
|
||
|
||
/* AT模式下的蓝牙接收buf */
|
||
static uint8_t gAtBufBlueTooth[32] = {0};
|
||
static uint8_t gAtBlueToothCount = 0;
|
||
|
||
/* 透传模式下的蓝牙buf */
|
||
static uint8_t gTransRBufBlueTooth[TASK_BLUETOOTH_RECV_BUF_NUM][TASK_BLUETOOTH_RECV_BUF_LEN_MAX] = {{0}};
|
||
static uint16_t gTransRBlueToothCount[TASK_BLUETOOTH_RECV_BUF_NUM] = {0};
|
||
static uint8_t gTransBlueToothState[TASK_BLUETOOTH_RECV_BUF_NUM] = {0};
|
||
static uint8_t gTransTBufBlueTooth[TASK_BLUETOOTH_RECV_BUF_LEN_MAX] = {0};
|
||
|
||
/* 工作模式 */
|
||
static uint8_t gAppWorkMode = 0; // 0表示工作模式
|
||
static uint8_t gBlueToothWorkMode = 0; // 0表示蓝牙工作模式
|
||
|
||
static E104BT52_OBJ TaskBlueToothObj = {0}; // 蓝牙对象
|
||
|
||
static MCU_BSP_UART_CONF gMcuTaskBlueToothUartConf = {0};// 串口通信配置对象
|
||
|
||
static MCU_BSP_TIMER_TASK gMcuTaskBlueToothTaskTimer = {0};
|
||
static int gMcuTaskBlueToothTimerId = -1;
|
||
|
||
static uint16_t gMcuTaskBlueToothJoinLowPowerSerial = 0;
|
||
static uint16_t gMcuTaskBlueToothNotice = 0;
|
||
|
||
static void McuTaskBlueToothRead(uint8_t Data);
|
||
static int McuTaskBlueToolUartWrite(unsigned char *Buf, unsigned int BufLen);
|
||
static int McuTaskBlueToolUartRead(unsigned char *Buf, unsigned int BufLen);
|
||
/* 清除接收buf */
|
||
static void TaskBlueToothClearRecvState(uint8_t Num);
|
||
static void TaskBlueToothNormalAck(uint16_t PlatNum, uint16_t PlatId, uint8_t Result);
|
||
static void TaskBlueToothModeDisable(void);
|
||
static void TaskBlueToothRestart(void);
|
||
|
||
int McuTaskBlueToothInit(void)
|
||
{
|
||
int rt = -1;
|
||
/* 初始化通信口 */
|
||
gMcuTaskBlueToothUartConf.BaudRate = 115200;
|
||
gMcuTaskBlueToothUartConf.WordLength = UART_WORDLENGTH_8B;
|
||
gMcuTaskBlueToothUartConf.StopBits = UART_STOPBITS_1;
|
||
gMcuTaskBlueToothUartConf.Parity = UART_PARITY_NONE;
|
||
gMcuTaskBlueToothUartConf.Mode = UART_MODE_TX_RX;
|
||
rt = McuBspUartInit(MCU_TASK_BLUETOOTH_UART_ID, &gMcuTaskBlueToothUartConf,
|
||
NULL, McuTaskBlueToothRead);
|
||
/* 初始化电源管理口 */
|
||
McuBspGpioInit(MCU_TASK_BLUETOOTH_ONOFF_GPIO_PORT, MCU_TASK_BLUETOOTH_ONOFF_GPIO_PIN,
|
||
MCU_TASK_BLUETOOTH_ONOFF_GPIO_MODE, MCU_TASK_BLUETOOTH_ONOFF_GPIO_SPEED,
|
||
MCU_TASK_BLUETOOTH_ONOFF_GPIO_PULL, 0, NULL);
|
||
McuBspGpioSetLevel(MCU_TASK_BLUETOOTH_ONOFF_GPIO_PORT, MCU_TASK_BLUETOOTH_ONOFF_GPIO_PIN, MCU_BSP_GPIO_OUT_HIGH);
|
||
|
||
/* 初始化唤醒引脚 */
|
||
McuBspGpioInit(MCU_TASK_BLUETOOTH_WKP_GPIO_PORT, MCU_TASK_BLUETOOTH_WKP_GPIO_PIN,
|
||
MCU_TASK_BLUETOOTH_WKP_GPIO_MODE, MCU_TASK_BLUETOOTH_WKP_GPIO_SPEED,
|
||
MCU_TASK_BLUETOOTH_WKP_GPIO_PULL, 0, NULL);
|
||
/* 初始化连接引脚 */
|
||
McuBspGpioInit(MCU_TASK_BLUETOOTH_LINK_GPIO_PORT, MCU_TASK_BLUETOOTH_LINK_GPIO_PIN,
|
||
MCU_TASK_BLUETOOTH_LINK_GPIO_MODE, MCU_TASK_BLUETOOTH_LINK_GPIO_SPEED,
|
||
MCU_TASK_BLUETOOTH_LINK_GPIO_PULL, MCU_BSP_GPIO_EXTI4, McuTaskLowPowerSetGpioCallback);
|
||
|
||
/* 初始化数据引脚 */
|
||
McuBspGpioInit(MCU_TASK_BLUETOOTH_DATA_GPIO_PORT, MCU_TASK_BLUETOOTH_DATA_GPIO_PIN,
|
||
MCU_TASK_BLUETOOTH_DATA_GPIO_MODE, MCU_TASK_BLUETOOTH_DATA_GPIO_SPEED,
|
||
MCU_TASK_BLUETOOTH_DATA_GPIO_PULL, 0, NULL);
|
||
|
||
/* 初始化RST引脚 */
|
||
McuBspGpioInit(MCU_TASK_BLUETOOTH_RST_GPIO_PORT, MCU_TASK_BLUETOOTH_RST_GPIO_PIN,
|
||
MCU_TASK_BLUETOOTH_RST_GPIO_MODE, MCU_TASK_BLUETOOTH_RST_GPIO_SPEED,
|
||
MCU_TASK_BLUETOOTH_RST_GPIO_PULL, 0, NULL);
|
||
McuBspGpioSetLevel(MCU_TASK_BLUETOOTH_RST_GPIO_PORT, MCU_TASK_BLUETOOTH_RST_GPIO_PIN, MCU_BSP_GPIO_OUT_HIGH);
|
||
|
||
McuBspUartStartRecv(MCU_TASK_BLUETOOTH_UART_ID);
|
||
McuTaskBlueToothSleep();
|
||
McuTaskBlueToothWakeUp();
|
||
TaskBlueToothObj.BlueToothId = gDeviceId - 1;
|
||
TaskBlueToothObj.BluetoothRecv = McuTaskBlueToolUartRead;
|
||
TaskBlueToothObj.BluetoothSend = McuTaskBlueToolUartWrite;
|
||
TaskBlueToothObj.DelayMs = HAL_Delay;
|
||
while (1) {
|
||
rt = McuE104BT52DrvInit(&TaskBlueToothObj);
|
||
if (rt == 0) {
|
||
gBlueToothMode = MCU_TASK_BLUETOOTH_MODE_TRANS;
|
||
break;
|
||
}
|
||
}
|
||
///* 对蓝牙模块进行重新上电,因为不这么做无法找到该蓝牙 */
|
||
TaskBlueToothRestart();
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
/* 睡眠 */
|
||
void McuTaskBlueToothSleep(void)
|
||
{
|
||
McuBspGpioSetLevel(MCU_TASK_BLUETOOTH_WKP_GPIO_PORT, MCU_TASK_BLUETOOTH_WKP_GPIO_PIN, MCU_BSP_GPIO_OUT_LOW);
|
||
HAL_Delay(600);
|
||
McuBspGpioSetLevel(MCU_TASK_BLUETOOTH_WKP_GPIO_PORT, MCU_TASK_BLUETOOTH_WKP_GPIO_PIN, MCU_BSP_GPIO_OUT_HIGH);
|
||
HAL_Delay(600);
|
||
}
|
||
|
||
/* 唤醒 */
|
||
void McuTaskBlueToothWakeUp(void)
|
||
{
|
||
McuBspGpioSetLevel(MCU_TASK_BLUETOOTH_WKP_GPIO_PORT, MCU_TASK_BLUETOOTH_WKP_GPIO_PIN, MCU_BSP_GPIO_OUT_HIGH);
|
||
HAL_Delay(600);
|
||
McuBspGpioSetLevel(MCU_TASK_BLUETOOTH_WKP_GPIO_PORT, MCU_TASK_BLUETOOTH_WKP_GPIO_PIN, MCU_BSP_GPIO_OUT_LOW);
|
||
HAL_Delay(600);
|
||
}
|
||
|
||
/* 通信处理任务 */
|
||
int McuTaskBlueTooth(void)
|
||
{
|
||
int k;
|
||
int i = 0;
|
||
int j = 0;
|
||
uint8_t crc = 0;
|
||
uint32_t TmpId;
|
||
uint32_t TmpType;
|
||
uint16_t MessageId;
|
||
uint16_t MessageNum;
|
||
uint16_t MessageLen;
|
||
while (1) {
|
||
for (k = 0; k < TASK_BLUETOOTH_RECV_BUF_NUM; k++) {
|
||
if (gTransBlueToothState[k] == TASK_BLUETOOTH_RECV_BUF_END_STATE) {
|
||
break;
|
||
}
|
||
}
|
||
if (k >= TASK_BLUETOOTH_RECV_BUF_NUM) {
|
||
break;//没有还需要处理的数据
|
||
}
|
||
/* 消息处理 */
|
||
for (i = 1; i < gTransRBlueToothCount[k] - 1; i++) {
|
||
if (gTransRBufBlueTooth[k][i] == 0x7f) {
|
||
TaskBlueToothClearRecvState(k);
|
||
break;
|
||
} else if (gTransRBufBlueTooth[k][i] == 0x7e) {
|
||
i++;
|
||
if (gTransRBufBlueTooth[k][i] == 0x01) {
|
||
gTransRBufBlueTooth[k][j] = 0x7e;
|
||
} else if (gTransRBufBlueTooth[k][i] == 0x02) {
|
||
gTransRBufBlueTooth[k][j] = 0x7f;
|
||
} else {
|
||
TaskBlueToothClearRecvState(k);
|
||
break;
|
||
}
|
||
} else {
|
||
gTransRBufBlueTooth[k][j] = gTransRBufBlueTooth[k][i];
|
||
}
|
||
j++;
|
||
}
|
||
if (i < (gTransRBlueToothCount[k] - 1)) {
|
||
continue; // 可能还存在需要处理的数据
|
||
}
|
||
/* 提取消息中的终端设备类型 */
|
||
TmpType = (((uint32_t)gTransRBufBlueTooth[k][2]) << 24) |
|
||
(((uint32_t)gTransRBufBlueTooth[k][3]) << 16) |
|
||
(((uint32_t)gTransRBufBlueTooth[k][4]) << 8) |
|
||
((uint32_t)gTransRBufBlueTooth[k][5]);
|
||
/* 提取消息中的设备ID */
|
||
TmpId = (((uint32_t)gTransRBufBlueTooth[k][6]) << 24) |
|
||
(((uint32_t)gTransRBufBlueTooth[k][7]) << 16) |
|
||
(((uint32_t)gTransRBufBlueTooth[k][8]) << 8) |
|
||
((uint32_t)gTransRBufBlueTooth[k][9]);
|
||
/* 提取消息中的平台消息ID */
|
||
MessageId = (((uint16_t)gTransRBufBlueTooth[k][0]) << 8) |
|
||
((uint16_t)gTransRBufBlueTooth[k][1]);
|
||
/* 提取消息中的平台消息流水号 */
|
||
MessageNum = (((uint16_t)gTransRBufBlueTooth[k][14]) << 8) |
|
||
((uint16_t)gTransRBufBlueTooth[k][15]);
|
||
/* 消息长度 */
|
||
MessageLen = (((uint16_t)(gTransRBufBlueTooth[k][10] & 0x03)) << 8) | ((uint16_t)gTransRBufBlueTooth[k][11]);
|
||
if ((TmpType != gDeviceType) || (TmpId != gDeviceId)) {
|
||
TaskBlueToothClearRecvState(k);
|
||
continue;
|
||
}
|
||
crc = 0;
|
||
if ((gTransRBufBlueTooth[k][10] & 0x0c) == 0x0c) {
|
||
ZthyToolCheckCrc8_0x31(gTransRBufBlueTooth[k], j - 1, &crc);
|
||
if (crc != gTransRBufBlueTooth[k][j - 1]) {
|
||
TaskBlueToothClearRecvState(k);
|
||
continue;
|
||
}
|
||
} else {
|
||
TaskBlueToothClearRecvState(k);
|
||
continue;
|
||
}
|
||
if ((gTransRBufBlueTooth[k][10] & 0x20) == 0x20) {
|
||
if (MessageLen != (j - 19)) {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_ERR);
|
||
TaskBlueToothClearRecvState(k);
|
||
continue;
|
||
} else {
|
||
i = 18;
|
||
}
|
||
} else {
|
||
if (MessageLen != (j - 17)) {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_ERR);
|
||
TaskBlueToothClearRecvState(k);
|
||
continue;
|
||
} else {
|
||
i = 16;
|
||
}
|
||
}
|
||
if (gAppWorkMode != 0) {
|
||
if (MessageId == PLAT_ACK_MESSAGE_ID) {
|
||
uint16_t AckSerial;
|
||
uint16_t AckId;
|
||
uint8_t AckResult;
|
||
if (MessageLen != 5) {
|
||
TaskBlueToothClearRecvState(k);
|
||
continue;
|
||
}
|
||
AckSerial = (((uint16_t)gTransRBufBlueTooth[k][i]) << 8) |
|
||
((uint16_t)gTransRBufBlueTooth[k][i + 1]);
|
||
AckId = (((uint16_t)gTransRBufBlueTooth[k][i + 2]) << 8) |
|
||
((uint16_t)gTransRBufBlueTooth[k][i + 3]);
|
||
AckResult = gTransRBufBlueTooth[k][i + 4];
|
||
if (AckResult != 0) {
|
||
continue;
|
||
}
|
||
/* 分析是哪个报文的回答 */
|
||
if (AckId == MCU_TASK_FLASH_SEND_MESSAGE_ID) {
|
||
McuTaskFlashAckOk(AckSerial);
|
||
}
|
||
TaskBlueToothClearRecvState(k);
|
||
} else if (MessageId == PLAT_ASK_TERMINAL_DATA_MESSAGE_ID) {
|
||
uint16_t AskId;
|
||
AskId = (((uint16_t)gTransRBufBlueTooth[k][i]) << 8) |
|
||
((uint16_t)gTransRBufBlueTooth[k][i + 1]);
|
||
if (AskId == MCU_TASK_IMU_SEND_MESSAGE_ID) {
|
||
McuTaskImuBlueToothAsk();
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
} else if (AskId == MCU_TASK_INPARA_SEND_MESSAGE_ID) {
|
||
McuTaskInParaBlueToothAsk();
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
} else if (AskId == MCU_TASK_MANYPARA_SEND_MESSAGE_ID) {
|
||
McuTaskManyParaBlueToothAsk();
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
} else if (AskId == MCU_TASK_POSITION_SEND_MESSAGE_ID) {
|
||
McuTaskPositionBlueToothAsk();
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
} else {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_ERR);
|
||
}
|
||
TaskBlueToothClearRecvState(k);
|
||
} else if (MessageId == PLAT_ASK_TERMINAL_UPDATA_MESSAGE_ID) {
|
||
McuTaskFlashAskUpdate(gTransRBufBlueTooth[k][i]);
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
TaskBlueToothClearRecvState(k);
|
||
} else if (MessageId == PLAT_ASK_TERMINAL_RESTART_MESSAGE_ID) {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
TaskBlueToothClearRecvState(k);
|
||
HAL_Delay(1000);
|
||
__set_FAULTMASK(1);//关闭所有中断
|
||
NVIC_SystemReset();//复位函数
|
||
} else if (MessageId == PLAT_ASK_TERMINAL_SELF_DESTROYING_MESSAGE_ID) {
|
||
/* 自毁模式设置 */
|
||
McuTaskSetSelfStructionMode(gTransRBufBlueTooth[k][i]);
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
TaskBlueToothClearRecvState(k);
|
||
} else if (MessageId == PLAT_ASK_CHANGE_BEIDOU_SEND_MODE) {
|
||
uint64_t BeiDouNum = 0;
|
||
/* 设置北斗通讯的周期 */
|
||
if (gTransRBufBlueTooth[k][i] != 0xff) {
|
||
if (0 > McuTaskFlashSetWakeUpPeriod(gTransRBufBlueTooth[k][i])) {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_ERR);
|
||
TaskBlueToothClearRecvState(k);
|
||
continue;
|
||
} else {
|
||
McuTaskLowPowerSetPeriod(gTransRBufBlueTooth[k][i]);
|
||
}
|
||
}
|
||
/* 设置北斗重发次数 */
|
||
if (gTransRBufBlueTooth[k][i + 1] != 0xff) {
|
||
if (0 > McuTaskFlashSetWakeUpPeriod(gTransRBufBlueTooth[k][i + 1])) {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_ERR);
|
||
TaskBlueToothClearRecvState(k);
|
||
continue;
|
||
} else {
|
||
McuTaskFlashSetBeiDouRetryTime(gTransRBufBlueTooth[k][i + 1]);
|
||
}
|
||
}
|
||
/* 设置北斗卡号 */
|
||
BeiDouNum = (((uint64_t)gTransRBufBlueTooth[k][2]) << 56) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][3]) << 48) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][4]) << 40) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][5]) << 32) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][6]) << 24) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][7]) << 16) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][8]) << 8) |
|
||
((uint64_t)gTransRBufBlueTooth[k][9]);
|
||
if (BeiDouNum != 0xffffffffffffffff) {
|
||
char BeiDouNumBuf[20] = {0};
|
||
sprintf(BeiDouNumBuf, "%lld", BeiDouNum);
|
||
if (0 > McuDrvBeiDouHtyConfOpposite(BeiDouNumBuf, strlen(BeiDouNumBuf))) {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_ERR);
|
||
TaskBlueToothClearRecvState(k);
|
||
continue;
|
||
}
|
||
}
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
TaskBlueToothClearRecvState(k);
|
||
} else if (MessageId == PLAT_ASK_CHANGE_WORK_MODE) {
|
||
uint64_t Time;
|
||
MCU_BSP_CLOCK RtcTime;
|
||
gAppWorkMode = gTransRBufBlueTooth[k][i];
|
||
if (gTransRBufBlueTooth[k][i] > 2) {
|
||
gTransRBufBlueTooth[k][i] = 0;
|
||
}
|
||
Time = (((uint64_t)gTransRBufBlueTooth[k][i + 1]) << 56) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 2]) << 48) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 3]) << 40) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 4]) << 32) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 5]) << 24) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 6]) << 16) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 7]) << 8) |
|
||
((uint64_t)gTransRBufBlueTooth[k][i + 8]);
|
||
RtcTime.Year = Time / 10000000000;
|
||
RtcTime.Month = (Time / 100000000) % 100;
|
||
RtcTime.Date = (Time / 1000000) % 100;
|
||
RtcTime.Hours = (Time / 10000) % 100;
|
||
RtcTime.Minutes = (Time / 100) % 100;
|
||
RtcTime.Seconds = Time % 100;
|
||
McuBspRtcSetTime(&RtcTime);
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
TaskBlueToothClearRecvState(k);
|
||
} else if (MessageId == PLAT_ASK_ASK_FLASH_DATA) {
|
||
if (0 > McuTaskFlashSetAsk(gTransRBufBlueTooth[k][i])) {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_ERR);
|
||
} else {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
}
|
||
TaskBlueToothClearRecvState(k);
|
||
} else if (MessageId == PLAT_ASK_UPDATE_FILE_DATA) {
|
||
uint16_t buflen;
|
||
uint16_t AllCount = (((uint16_t)gTransRBufBlueTooth[k][i]) << 8) |
|
||
((uint16_t)gTransRBufBlueTooth[k][i + 1]);
|
||
uint16_t LocalCount = (((uint16_t)gTransRBufBlueTooth[k][i + 2]) << 8) |
|
||
((uint16_t)gTransRBufBlueTooth[k][i + 3]);
|
||
buflen = MessageLen - 4;
|
||
if (0 > McuTaskFlashUpdateProgram(LocalCount, AllCount, &(gTransRBufBlueTooth[k][i + 4]), buflen)) {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_ERR);
|
||
} else {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
if (AllCount == LocalCount) {
|
||
HAL_Delay(1000);
|
||
__set_FAULTMASK(1);//关闭所有中断
|
||
NVIC_SystemReset();//复位函数
|
||
}
|
||
}
|
||
TaskBlueToothClearRecvState(k);
|
||
}
|
||
} else {
|
||
if (MessageId == PLAT_ASK_CHANGE_WORK_MODE) {
|
||
uint64_t Time;
|
||
MCU_BSP_CLOCK RtcTime;
|
||
if (gTransRBufBlueTooth[k][i] > 2) {
|
||
gTransRBufBlueTooth[k][i] = 0;
|
||
}
|
||
gAppWorkMode = gTransRBufBlueTooth[k][i];
|
||
Time = (((uint64_t)gTransRBufBlueTooth[k][i + 1]) << 56) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 2]) << 48) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 3]) << 40) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 4]) << 32) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 5]) << 24) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 6]) << 16) |
|
||
(((uint64_t)gTransRBufBlueTooth[k][i + 7]) << 8) |
|
||
((uint64_t)gTransRBufBlueTooth[k][i + 8]);
|
||
RtcTime.Year = Time / 10000000000;
|
||
RtcTime.Month = (Time / 100000000) % 100;
|
||
RtcTime.Date = (Time / 1000000) % 100;
|
||
RtcTime.Hours = (Time / 10000) % 100;
|
||
RtcTime.Minutes = (Time / 100) % 100;
|
||
RtcTime.Seconds = Time % 100;
|
||
McuBspRtcSetTime(&RtcTime);
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_OK);
|
||
TaskBlueToothClearRecvState(k);
|
||
} else {
|
||
TaskBlueToothNormalAck(MessageNum, MessageId, TASK_ACK_ERR);
|
||
TaskBlueToothClearRecvState(k);
|
||
}
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/* 发送函数 */
|
||
uint32_t TaskBlueToothSendMessage(uint16_t MessageId, uint16_t *Serial, uint8_t *SendBuf, uint32_t SendBufLen)
|
||
{
|
||
uint32_t i = 0;
|
||
uint32_t j = 0;
|
||
uint32_t k = 0;
|
||
uint32_t e =0;
|
||
uint32_t RealSendLen = 0;
|
||
uint16_t SendPiece = 0; // 发送条数,用来记录是否具有分包
|
||
uint16_t MessageProperties = 0; // 消息属性
|
||
uint8_t SendBufTmp[1050] = {0};
|
||
if (Serial == NULL) {
|
||
return 0;
|
||
}
|
||
SendPiece = (SendBufLen / 1023) + 1; // 计算总共需要发几条信息
|
||
for (i = 0; i < SendPiece; i++) {
|
||
if (SendPiece != 1) {
|
||
if (i != (SendPiece - 1)) {
|
||
MessageProperties = 0x2000 | 0x0c00 | 1023;
|
||
} else {
|
||
MessageProperties = 0x2000 | 0x0c00 | (SendBufLen % 1023);
|
||
}
|
||
} else {
|
||
MessageProperties = 0x0000 | 0x0c00 | SendBufLen;
|
||
}
|
||
j = 0;
|
||
/* 消息ID */
|
||
SendBufTmp[j] = (uint8_t)((MessageId & 0xff00) >> 8);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)(MessageId & 0x00ff);
|
||
j++;
|
||
/* 设备类型 */
|
||
SendBufTmp[j] = (uint8_t)((gDeviceType & 0xff000000) >> 24);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)((gDeviceType & 0x00ff0000) >> 16);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)((gDeviceType & 0x0000ff00) >> 8);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)(gDeviceType & 0x000000ff);
|
||
j++;
|
||
/* 设备ID号 */
|
||
SendBufTmp[j] = (uint8_t)((gDeviceId & 0xff000000) >> 24);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)((gDeviceId & 0x00ff0000) >> 16);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)((gDeviceId & 0x0000ff00) >> 8);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)(gDeviceId & 0x000000ff);
|
||
j++;
|
||
/* 消息体属性 */
|
||
SendBufTmp[j] = (uint8_t)((MessageProperties & 0xff00) >> 8);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)(MessageProperties & 0x00ff);
|
||
j++;
|
||
/* 协议版本号 */
|
||
SendBufTmp[j] = 0x00;
|
||
j++;
|
||
SendBufTmp[j] = 0x01;
|
||
j++;
|
||
/* 消息流水号 */
|
||
SendBufTmp[j] = (uint8_t)(((*Serial) & 0xff00) >> 8);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)((*Serial) & 0x00ff);
|
||
j++;
|
||
/* 消息包装项 + 消息内容 */
|
||
if (SendPiece != 1) {
|
||
/* 消息总包数 */
|
||
SendBufTmp[j] = (uint8_t)((SendPiece & 0xff00) >> 8);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)(SendPiece & 0x00ff);
|
||
j++;
|
||
/* 包序号 */
|
||
SendBufTmp[j] = (uint8_t)(((i + 1) & 0xff00) >> 8);
|
||
j++;
|
||
SendBufTmp[j] = (uint8_t)((i + 1) & 0x00ff);
|
||
j++;
|
||
if (i != (SendPiece - 1)) {
|
||
memcpy(&SendBufTmp[j], &(SendBuf[1023 * i]), 1023);
|
||
j += 1023;
|
||
} else {
|
||
memcpy(&SendBufTmp[j], &(SendBuf[1023 * i]), SendBufLen % 1023);
|
||
j += (SendBufLen % 1023);
|
||
}
|
||
} else {
|
||
if ((SendBuf != NULL) && (SendBufLen != 0)) {
|
||
memcpy(&SendBufTmp[j], SendBuf, SendBufLen);
|
||
j += SendBufLen;
|
||
}
|
||
}
|
||
/* 进行校验 */
|
||
SendBufTmp[j] = 0;
|
||
ZthyToolCheckCrc8_0x31(SendBufTmp, j, &SendBufTmp[j]);
|
||
j++;
|
||
/* 进行转义 */
|
||
e = 0;
|
||
gTransTBufBlueTooth[e] = 0x7f; // 标志位
|
||
e++;
|
||
for (k = 0; k < j; k++) {
|
||
if (SendBufTmp[k] == 0x7e) {
|
||
gTransTBufBlueTooth[e] = 0x7e;
|
||
e++;
|
||
gTransTBufBlueTooth[e] = 0x01;
|
||
e++;
|
||
} else if (SendBufTmp[k] == 0x7f) {
|
||
gTransTBufBlueTooth[e] = 0x7e;
|
||
e++;
|
||
gTransTBufBlueTooth[e] = 0x02;
|
||
e++;
|
||
} else {
|
||
gTransTBufBlueTooth[e] = SendBufTmp[k];
|
||
e++;
|
||
}
|
||
}
|
||
gTransTBufBlueTooth[e] = 0x7f; // 标志位
|
||
e++;
|
||
/* 发送数据 */
|
||
if (gAppWorkMode != 0 && (gBlueToothWorkMode == 1 || gMcuTaskBlueToothNotice == 1)) {
|
||
McuBspUartSend(MCU_TASK_BLUETOOTH_UART_ID, gTransTBufBlueTooth, e);
|
||
}
|
||
RealSendLen += e;
|
||
}
|
||
return RealSendLen;
|
||
}
|
||
|
||
/* 发送通知 */
|
||
void TaskBlueToothJoinLowPowerNoticePlat(void)
|
||
{
|
||
gMcuTaskBlueToothNotice = 1;
|
||
TaskBlueToothSendMessage(DEVICE_NOTICE_PLAT_BLUETOOTH_JOIN_LOWPOWER, &gMcuTaskBlueToothJoinLowPowerSerial, NULL, 0);
|
||
gMcuTaskBlueToothJoinLowPowerSerial++;
|
||
gMcuTaskBlueToothNotice = 0;
|
||
}
|
||
|
||
unsigned char TaskBlueToothGetAppMode(void)
|
||
{
|
||
return gAppWorkMode;
|
||
}
|
||
|
||
unsigned char TaskBlueToothGetBluetoothMode(void)
|
||
{
|
||
return gBlueToothWorkMode;
|
||
}
|
||
|
||
/* 新增定时器 */
|
||
void TaskBlueToothNewTimer(void)
|
||
{
|
||
if (gAppWorkMode != 0) {
|
||
if (gMcuTaskBlueToothTimerId < 0) {
|
||
gBlueToothWorkMode = 1;
|
||
gMcuTaskBlueToothTaskTimer.TaskPeriod = 60;
|
||
gMcuTaskBlueToothTaskTimer.Callback = TaskBlueToothModeDisable;
|
||
gMcuTaskBlueToothTimerId = McuBspTimerAddTask(&gMcuTaskBlueToothTaskTimer);
|
||
} else if (gMcuTaskBlueToothTimerId >= 0) {
|
||
McuBspTimerTaskRefresh(gMcuTaskBlueToothTimerId);
|
||
}
|
||
}
|
||
}
|
||
|
||
/* 删除一个定时器 */
|
||
void TaskBlueToothDelTimer(void)
|
||
{
|
||
if (gMcuTaskBlueToothTimerId >= 0) {
|
||
gBlueToothWorkMode = 0;
|
||
McuBspTimerDelTask(gMcuTaskBlueToothTimerId);
|
||
gMcuTaskBlueToothTimerId = -1;
|
||
}
|
||
}
|
||
|
||
/* 重启蓝牙模块 */
|
||
static void TaskBlueToothRestart(void)
|
||
{
|
||
McuBspGpioSetLevel(MCU_TASK_BLUETOOTH_ONOFF_GPIO_PORT, MCU_TASK_BLUETOOTH_ONOFF_GPIO_PIN, MCU_BSP_GPIO_OUT_LOW);
|
||
McuBspGpioSetLevel(MCU_TASK_BLUETOOTH_RST_GPIO_PORT, MCU_TASK_BLUETOOTH_RST_GPIO_PIN, MCU_BSP_GPIO_OUT_LOW);
|
||
HAL_Delay(4000);
|
||
McuBspGpioSetLevel(MCU_TASK_BLUETOOTH_RST_GPIO_PORT, MCU_TASK_BLUETOOTH_RST_GPIO_PIN, MCU_BSP_GPIO_OUT_HIGH);
|
||
McuBspGpioSetLevel(MCU_TASK_BLUETOOTH_ONOFF_GPIO_PORT, MCU_TASK_BLUETOOTH_ONOFF_GPIO_PIN, MCU_BSP_GPIO_OUT_HIGH);
|
||
}
|
||
|
||
/* 失能蓝牙 */
|
||
static void TaskBlueToothModeDisable(void)
|
||
{
|
||
gMcuTaskBlueToothTimerId = -1;
|
||
if (0 == (0x02 & McuTaskImuAskStatus()) && 0 == (0x02 & McuTaskInParaAskStatus()) &&
|
||
0 == (0x02 & McuTaskManyParaAskStatus()) && 0 == (0x02 & McuTaskPositionAskStatus()) &&
|
||
0 == McuTaskFlashGetAsk()) {
|
||
gBlueToothWorkMode = 0;
|
||
} else {
|
||
if (gMcuTaskBlueToothTimerId < 0) {
|
||
gBlueToothWorkMode = 1;
|
||
gMcuTaskBlueToothTaskTimer.TaskPeriod = 60;
|
||
gMcuTaskBlueToothTaskTimer.Callback = TaskBlueToothModeDisable;
|
||
gMcuTaskBlueToothTimerId = McuBspTimerAddTask(&gMcuTaskBlueToothTaskTimer);
|
||
}
|
||
}
|
||
}
|
||
|
||
static void TaskBlueToothNormalAck(uint16_t PlatNum, uint16_t PlatId, uint8_t Result)
|
||
{
|
||
uint8_t Ack[5] = {0};
|
||
static uint16_t Serial = 0;
|
||
Ack[0] = (uint8_t)((PlatNum & 0xff00) >> 8);
|
||
Ack[1] = (uint8_t)(PlatNum & 0x00ff);
|
||
Ack[2] = (uint8_t)((PlatId & 0xff00) >> 8);
|
||
Ack[3] = (uint8_t)(PlatId & 0x00ff);
|
||
Ack[4] = Result;
|
||
gMcuTaskBlueToothNotice = 1;
|
||
TaskBlueToothSendMessage(0x0000, &Serial, Ack, sizeof(Ack));
|
||
gMcuTaskBlueToothNotice = 0;
|
||
Serial++;
|
||
}
|
||
|
||
static int McuTaskBlueToolUartWrite(unsigned char *Buf, unsigned int BufLen)
|
||
{
|
||
if (gBlueToothMode == MCU_TASK_BLUETOOTH_MODE_AT) {
|
||
gAtBlueToothCount = 0;
|
||
}
|
||
return McuBspUartSend(MCU_TASK_BLUETOOTH_UART_ID, Buf, BufLen);
|
||
}
|
||
|
||
static int McuTaskBlueToolUartRead(unsigned char *Buf, unsigned int BufLen)
|
||
{
|
||
int i = 0;
|
||
if (BufLen >= 32) {
|
||
return -1;
|
||
}
|
||
|
||
/* 等待接收完成 */
|
||
while (gAtBlueToothCount < BufLen) {
|
||
HAL_Delay(1);
|
||
i++;
|
||
/* 超时 */
|
||
if (i >= 300) {
|
||
break;
|
||
}
|
||
}
|
||
for (i = 0; i < gAtBlueToothCount; i++) {
|
||
Buf[i] = gAtBufBlueTooth[i];
|
||
}
|
||
return i;
|
||
}
|
||
|
||
static void McuTaskBlueToothRead(uint8_t Data)
|
||
{
|
||
if (gBlueToothMode != MCU_TASK_BLUETOOTH_MODE_AT) {
|
||
if (Data == 0x7f) {
|
||
int i;
|
||
for (i = 0; i < TASK_BLUETOOTH_RECV_BUF_NUM; i++) {
|
||
if (gTransBlueToothState[i] == TASK_BLUETOOTH_RECV_BUF_SATRT_STATE) {
|
||
if (gTransRBlueToothCount[i] < 18) {
|
||
/* 说明接收的包不全,或者包出现问题,该0x7f不是结束标志位,是开始标志位,
|
||
所以将该接收队列清零,并跳出该循环执行0x7f是开始标志的逻辑 */
|
||
gTransBlueToothState[i] = TASK_BLUETOOTH_RECV_BUF_IDLE_STATE;
|
||
gTransRBlueToothCount[i] = 0;
|
||
break;
|
||
} else {
|
||
gTransRBufBlueTooth[i][gTransRBlueToothCount[i]] = Data;
|
||
gTransRBlueToothCount[i]++;
|
||
gTransRBlueToothCount[i] = gTransRBlueToothCount[i] % TASK_BLUETOOTH_RECV_BUF_LEN_MAX;
|
||
gTransBlueToothState[i] = TASK_BLUETOOTH_RECV_BUF_END_STATE;
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
for (i = 0; i < TASK_BLUETOOTH_RECV_BUF_NUM; i++) {
|
||
if (gTransBlueToothState[i] == TASK_BLUETOOTH_RECV_BUF_IDLE_STATE) {
|
||
gTransRBufBlueTooth[i][gTransRBlueToothCount[i]] = Data;
|
||
gTransRBlueToothCount[i]++;
|
||
gTransBlueToothState[i] = TASK_BLUETOOTH_RECV_BUF_SATRT_STATE;
|
||
break;
|
||
}
|
||
}
|
||
} else {
|
||
int i;
|
||
for (i = 0; i < TASK_BLUETOOTH_RECV_BUF_NUM; i++) {
|
||
if (gTransBlueToothState[i] == TASK_BLUETOOTH_RECV_BUF_SATRT_STATE) {
|
||
gTransRBufBlueTooth[i][gTransRBlueToothCount[i]] = Data;
|
||
gTransRBlueToothCount[i]++;
|
||
gTransRBlueToothCount[i] = gTransRBlueToothCount[i] % TASK_BLUETOOTH_RECV_BUF_LEN_MAX;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
gAtBufBlueTooth[gAtBlueToothCount] = Data;
|
||
gAtBlueToothCount++;
|
||
gAtBlueToothCount = gAtBlueToothCount % 32;
|
||
}
|
||
}
|
||
|
||
/* 清除接收buf */
|
||
static void TaskBlueToothClearRecvState(uint8_t Num)
|
||
{
|
||
gTransRBlueToothCount[Num] = 0;
|
||
gTransBlueToothState[Num] = TASK_BLUETOOTH_RECV_BUF_IDLE_STATE;
|
||
}
|