123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- #include "mba32a.h"
- #include "usart.h"
- #include "interface.h"
- #include "key.h"
- #include "tt808.h"
- #include "ml307a.h"
- //*结构体
- BtStep btstep;
- BtFrame btframe;
- extern Menu_table menu;
- extern TT808 tt808;
- extern TipsFlag tipsflag;
- //*整型
- uint8_t resetcount_bt=0;
- uint16_t resetFlag16=0;
- //*布尔
- bool resetBTmodule = false;
- bool L1Working = false;
- bool L2Working = false;
- bool L3Working = false;
- bool L4Working = false;
- //*字符串
- char autoRe[1024];
- //*外部方法
- uint16_t read_flash_16(uint32_t addr);
- void Write_Information(uint32_t addr, uint16_t newValue);
- //*本模块方法
- uint8_t sendCmd_BT(char *pCmd,char *pRes, uint32_t timeOut, uint8_t sendNum);
- void MBA32AReset(void);
- uint16_t resetdev[2];
- /**
- * @breaf 蓝牙启动位读写检查
- */
- void Start_Read_resetFlag16(void)
- {
- resetdev[0] = 0xAAAA;
- //resetFlag16 = read_flash_16(resetDevAddr);
-
- Flash_ReadBytes(&resetdev[1], resetDevAddr, 2);
- resetFlag16 = resetdev[1];
-
- if(resetFlag16==0xFFFF)//开机检测(首次启动)
- {
- if(sendCmd_BT("AT+RESET=1\r\n","OK",1,1))
- {
- //Write_Information(resetDevAddr, 0xAAAA);//写入0xAAAA
-
- Flash_WriteBytes(resetdev,resetDevAddr,1);
- printf("###复位设备成功\r\n");
- btstep = UUIDS;
- }
- }
- else if(resetFlag16==0xAAAA)//开机检测(大于1次启动)
- {
- printf("###蓝牙模块非首次启动\r\n");
- if (!sendCmd_BT("AT+UUIDS?\r\n","+UUIDS:0002",1,1))
- {
- PRINT_NUUID("UUIDS");//UUID项不符合
- btstep = UUIDS;
- }
- else
- {
-
- PRINT_UUID("UUID项及发射功率");//符合设置项
- btstep = BtStepDefault;
- }
- }
- }
- /**
- * @breaf 蓝牙初始化,工作接口
- */
- void MBA32A_Init(void)
- {
-
- btframe.userId = 0x64747531;
- btframe.Autoseal = 0xFF; //自动施封时间
-
- printf("\t###01\tBT_init%d...\r\n", resetcount_bt);
-
- Start_Read_resetFlag16();/** 启动位读写检查 **/
- if(sendCmd_BT("AT+DISCONN=0\r\n","ERROR",1,1))//断开连接
- {
- if(sendCmd_BT("AT+DISCONN=1\r\n","ERROR",1,1))//断开连接
- {
- printf("#已无主设备连接\n");
- }
- printf("#已无从设备连接\n");
-
- }
- switch(btstep)
- {
- case BtStepDefault:
- break;
- case UUIDS:
- {
- if(sendCmd_BT("AT+UUIDS?\r\n","+UUIDS:FFF0",1,1))//蓝牙芯片默认值,需要修改
- {
- if(sendCmd_BT("AT+UUIDS=0002\r\n","OK",1,1))
- {
- printf("#01设置主服务成功\n");
- btstep++;
- }
- }
- else
- { //ERROR 其他值
- MBA32AReset();
- break;
- }
- }
- case UUIDN:
- {
- if(sendCmd_BT("AT+UUIDN?\r\n","+UUIDN:FFF1",1,1))//蓝牙芯片默认值,需要修改
- {
- if(sendCmd_BT("AT+UUIDN=FF04\r\n","OK",1,1))
- {
- printf("#02设置读服务成功\n");
- btstep++;
- }
- }
- else
- { //ERROR 其他值
- MBA32AReset();
- break;
- }
- }
- case UUIDW:
- {
- if(sendCmd_BT("AT+UUIDW?\r\n","+UUIDW:FFF2",1,1))//蓝牙芯片默认值,需要修改
- {
- if(sendCmd_BT("AT+UUIDW=FFF5\r\n","OK",1,1))
- {
- printf("#03设置写服务成功\n");
- btstep++;
- }
- }
- else
- { //ERROR 其他值
- MBA32AReset();
- break;
- }
- }
- case TxPower:
- {
- if(sendCmd_BT("AT+TXPOWER?\r\n","+TXPOWER:0",1,1))//蓝牙芯片默认值,需要修改
- {
- if(sendCmd_BT("AT+TXPOWER=10\r\n","OK",1,1))
- {
- printf("#04设置发射功率成功\n");
- btstep++;
- }
-
-
- }
- else
- { //ERROR 其他值
- MBA32AReset();
- break;
- }
- }
-
- }
-
- }
- /**
- * @breaf 串口发送命令子函数
- */
- void USART_SendString(UART_HandleTypeDef *huart,char *str)
- {
- //printf("\r\nAT_CMD:\t");
- HAL_UART_Transmit(huart, (uint8_t*)str, strlen(str), 0xFFFF); //工作信息串口发送
- HAL_UART_Transmit(&huart1, (uint8_t*)str, strlen(str), 0xFFFF); //485打印log
- //HAL_UART_Transmit(&huart3, (uint8_t*)str, strlen(str), 0xFFFF); //TTL打印log
-
- }
- /**
- * @breaf 串口发送命令,有接收数组比较函数
- */
- uint8_t sendCmd_BT(char *pCmd,char *pRes, uint32_t timeOut, uint8_t sendNum)
- {
- uint8_t i = 0;
- uint32_t time;
- for(i = 0; i < sendNum; i++)
- {
- time = timeOut * 10;
- USART_SendString(&huart5, pCmd);
- //HAL_Delay(30);
- //printf("------->%s",g_MBA32A_8buf_Down);
-
- while(time--)
- {
- if(strstr((const char *)U5_BTrecvBuff, pRes) != NULL) // 如果检索到关键词
- {
- memset(U5_BTrecvBuff,0,50);
- return 1;
- }
- HAL_Delay(100);
- }
- }
- return 0;
- }
- /**
- * @breaf 模块复位函数(接收数组清0)
- */
- void MBA32AReset(void)
- {
- resetcount_bt+=1;
- printf("Bt reset\r\n");
- HAL_GPIO_WritePin(REST_BT_GPIO_Port,REST_BT_Pin,GPIO_PIN_RESET);
- HAL_Delay(4000);
- btstep = UUIDS;
- resetBTmodule=true;//freertos监测模块重启位
- }
- void event_lock(void)
- {
- }
- /**
- * @breaf 蓝牙关锁信息帧
- */
- void close_lock(void) //蓝牙关锁信息帧
- {
- Time_inquiry(1);
- uint8_t header[2] = {0x66, 0x01};//Jl_lock帧头
- uint8_t close_CMD[] = {0x03, 0x0B, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, \
- 0x00,0x00,0x00,0x00};//关锁命令帧
- uint32_t serialNum;
- if(preworkUp==3)
- {
- serialNum = btframe.serialNum1;//流水号
- btframe.serialNum1+=1;
- }
- if(preworkUp==2)
- {
- serialNum = btframe.serialNum2;//流水号
- btframe.serialNum2+=1;
- }
- if(preworkUp==1)
- {
- serialNum = btframe.serialNum3;//流水号
- btframe.serialNum3+=1;
- }
- if(preworkUp==0)
- {
- serialNum = btframe.serialNum4;//流水号
- btframe.serialNum4+=1;
- }
-
-
- close_CMD[2]=(serialNum>>24) & 0xff;
- close_CMD[3]=(serialNum>>16) & 0xff;
- close_CMD[4]=(serialNum>>8) & 0xff;
- close_CMD[5]= serialNum & 0xff;
-
- uint32_t userid = btframe.userId;//用户id
- close_CMD[6]=(userid>>24) & 0xff;
- close_CMD[7]=(userid>>16) & 0xff;
- close_CMD[8]=(userid>>8) & 0xff;
- close_CMD[9]= userid & 0xff;
-
- RTCtime();
- close_CMD[10]= ((dblocptr->location.timeStamp)>>24) & 0xff;
- close_CMD[11]= ((dblocptr->location.timeStamp)>>16) & 0xff;
- close_CMD[12]= ((dblocptr->location.timeStamp)>>8) & 0xff;
- close_CMD[13]= (dblocptr->location.timeStamp) & 0xff;
-
- sendCombinedArray(header,sizeof(header),close_CMD,sizeof(close_CMD),1);
- //Echo_BT(20);
- }
- /**
- * @breaf 蓝牙开锁信息帧
- */
- void open_lock(void) //蓝牙开锁信息帧
- {
- uint8_t header[2] = {0x66, 0x01};//Jl_lock帧头
- uint8_t open_CMD[] = {0x02, 0x0C, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, \
- 0x00,0x00,0x00,0x00, 0x00};//开锁命令帧
-
- uint32_t serialNum;
- if(menu.current==4)
- {
- serialNum = btframe.serialNum1;//流水号
- }
- if(menu.current==5)
- {
- serialNum = btframe.serialNum2;//流水号
- }
- if(menu.current==6)
- {
- serialNum = btframe.serialNum3;//流水号
- }
- if(menu.current==7)
- {
- serialNum = btframe.serialNum4;//流水号
- }
- open_CMD[2]=(serialNum>>24) & 0xff;
- open_CMD[3]=(serialNum>>16) & 0xff;
- open_CMD[4]=(serialNum>>8) & 0xff;
- open_CMD[5]= serialNum & 0xff;
-
- uint32_t userid = btframe.userId;//用户id
- open_CMD[6]=(userid>>24) & 0xff;
- open_CMD[7]=(userid>>16) & 0xff;
- open_CMD[8]=(userid>>8) & 0xff;
- open_CMD[9]= userid & 0xff;
-
- RTCtime();
- open_CMD[10]= ((dblocptr->location.timeStamp)>>24) & 0xff;
- open_CMD[11]= ((dblocptr->location.timeStamp)>>16) & 0xff;
- open_CMD[12]= ((dblocptr->location.timeStamp)>>8) & 0xff;
- open_CMD[13]= (dblocptr->location.timeStamp) & 0xff;
-
- open_CMD[14] = btframe.Autoseal;//自动施封时间
-
- sendCombinedArray(header,sizeof(header),open_CMD,sizeof(open_CMD),1);//加密
- //Echo_BT(20);
- }
- uint16_t serialNum1[2];
- void Lock_OandC(void)
- {
- if(L1Working) //在四个界面,按下确认
- {
- if(presjfUp==1)//施封
- {
- close_lock(); //施封操作需要写入0x40
- }
- else if(presjfUp==0)//施封
- {
- open_lock(); //解封操作 需要写入0x60
- }
- serialNum1[0] = (uint16_t)((btframe.serialNum1 >> 24) & 0xff) << 8 | ((btframe.serialNum1 >> 16) & 0xff);
- serialNum1[1] = (uint16_t)((btframe.serialNum1 >> 8) & 0xff) << 8 | (btframe.serialNum1 & 0xff);
-
- Flash_WriteBytes(serialNum1,serialNum1Addr,2);
- L1Working = false;
-
- }
- }
- void ReU5Proces(void)
- {
- if(autoRe[9]==0x54 && autoRe[10]==0x49 && autoRe[11]==0x4D && autoRe[12]==0x45 && autoRe[13]==0x4F && autoRe[14]==0x55 && autoRe[15]==0x54)//TIMEOUT
- {
- tipsflag = Timeout;
- HAL_Delay(1000);
- if(preworkUp==3)
- {
- SET_MENU_STATUS(4,1,0,3);
- }
- else if(preworkUp==2)
- {
- SET_MENU_STATUS(5,1,0,3);
- }
- else if(preworkUp==1)
- {
- SET_MENU_STATUS(6,1,0,3);
- }
- else if(preworkUp==0)
- {
- SET_MENU_STATUS(7,1,0,3);
- }
- memset(autoRe,0,1024);
- }
- else if(autoRe[1]==0x43 && autoRe[2]==0x4F && autoRe[3]==0x4E && autoRe[4]==0x4E && autoRe[5]==0x45 && autoRe[6]==0x43 && autoRe[7]==0x54 && autoRe[8]==0x45 && autoRe[9]==0x44 \
- && autoRe[10]==0x3A)//CONNECTED:1 做从设备 0:做主设备
- {
- if(sys_mode[1]==DTU_MODE && autoRe[11]==0x31)
- {
- tipsflag = ConnOK;
- menu.current = 0xBB;
- btconn_dtu=true;
- refresh=true;
- }
- else if(sys_mode[1]==PDA_MODE && autoRe[11]==0x30)
- {
- tipsflag = ConnOK;
- refresh=true;
- HAL_Delay(1000);
- tipsflag = Working;
- refresh=true;
- HAL_Delay(1000);
- if(preworkUp==3)
- {
- L1Working= true;
- }
- else if(preworkUp==2)
- {
- L2Working= true;
- }
- else if(preworkUp==1)
- {
- L3Working= true;
- }
- else if(preworkUp==0)
- {
- L4Working= true;
- }
- }
- memset(autoRe,0,1024);
- }
- //*44 49 53 43 4f 4e 4e 3a 31
- else if(autoRe[1]==0x44 && autoRe[2]==0x49 && autoRe[3]==0x53 && autoRe[4]==0x43 && autoRe[5]==0x4f && autoRe[6]==0x4e && autoRe[7]==0x4e && autoRe[8]==0x3a)//disCONNECTED:1 做从设备 0:做主设备
- {
- if(sys_mode[1]==DTU_MODE && autoRe[9]==0x31)
- {
- tipsflag = Disconn;
- menu.current = 0xBB;
-
- btconn_dtu=false;
- refresh=true;
- }
- else if(sys_mode[1]==PDA_MODE && autoRe[9]==0x30)
- {
- tipsflag = ConnOK;
- refresh=true;
- HAL_Delay(1000);
- tipsflag = Working;
- refresh=true;
- HAL_Delay(1000);
- if(preworkUp==3)
- {
- L1Working= true;
- }
- else if(preworkUp==2)
- {
- L2Working= true;
- }
- else if(preworkUp==1)
- {
- L3Working= true;
- }
- else if(preworkUp==0)
- {
- L4Working= true;
- }
- }
- memset(autoRe,0,1024);
- }
- else if(autoRe[0]==0x45 && autoRe[1]==0x52 && autoRe[2]==0x52 && autoRe[3]==0x4f && autoRe[4]==0x52)//ERROR
- {
- tipsflag = ConnFail;
- HAL_Delay(1000);
- //SET_MENU_STATUS(3,3,0,2);//回到操作界面
- //USART_SendString(&huart5, "AT+DISCONN=0\r\n");//断开连接
- memset(autoRe,0,1024);
- }
-
-
- //*接收PDA信息
- else if(autoRe[0]==0x66)
- {
- for (int i = 0; i < U5_BTrecvLength; i++)
- {
- PDA_pushArr[i] = (uint16_t)(autoRe[2*i] << 8) | autoRe[2*i+1];
- }
-
- memset(autoRe,0,1024);
- }
-
- }
|