mba32a.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #include "mba32a.h"
  2. #include "usart.h"
  3. #include "interface.h"
  4. #include "key.h"
  5. #include "tt808.h"
  6. #include "ml307a.h"
  7. //*结构体
  8. BtStep btstep;
  9. BtFrame btframe;
  10. extern Menu_table menu;
  11. extern TT808 tt808;
  12. extern TipsFlag tipsflag;
  13. //*整型
  14. uint8_t resetcount_bt=0;
  15. uint16_t resetFlag16=0;
  16. //*布尔
  17. bool resetBTmodule = false;
  18. bool L1Working = false;
  19. bool L2Working = false;
  20. bool L3Working = false;
  21. bool L4Working = false;
  22. //*字符串
  23. char autoRe[1024];
  24. //*外部方法
  25. uint16_t read_flash_16(uint32_t addr);
  26. void Write_Information(uint32_t addr, uint16_t newValue);
  27. //*本模块方法
  28. uint8_t sendCmd_BT(char *pCmd,char *pRes, uint32_t timeOut, uint8_t sendNum);
  29. void MBA32AReset(void);
  30. uint16_t resetdev[2];
  31. /**
  32. * @breaf 蓝牙启动位读写检查
  33. */
  34. void Start_Read_resetFlag16(void)
  35. {
  36. resetdev[0] = 0xAAAA;
  37. //resetFlag16 = read_flash_16(resetDevAddr);
  38. Flash_ReadBytes(&resetdev[1], resetDevAddr, 2);
  39. resetFlag16 = resetdev[1];
  40. if(resetFlag16==0xFFFF)//开机检测(首次启动)
  41. {
  42. if(sendCmd_BT("AT+RESET=1\r\n","OK",1,1))
  43. {
  44. //Write_Information(resetDevAddr, 0xAAAA);//写入0xAAAA
  45. Flash_WriteBytes(resetdev,resetDevAddr,1);
  46. printf("###复位设备成功\r\n");
  47. btstep = UUIDS;
  48. }
  49. }
  50. else if(resetFlag16==0xAAAA)//开机检测(大于1次启动)
  51. {
  52. printf("###蓝牙模块非首次启动\r\n");
  53. if (!sendCmd_BT("AT+UUIDS?\r\n","+UUIDS:0002",1,1))
  54. {
  55. PRINT_NUUID("UUIDS");//UUID项不符合
  56. btstep = UUIDS;
  57. }
  58. // else if (!sendCmd_BT("AT+UUIDN?\r\n","+UUIDN:FF04",1,1))
  59. // {
  60. // PRINT_NUUID("UUIDN");//UUID项不符合
  61. // btstep = UUIDN;
  62. // }
  63. // else if (!sendCmd_BT("AT+UUIDW?\r\n","+UUIDW:FFF5",1,1))
  64. // {
  65. // PRINT_NUUID("UUIDW");//UUID项不符合
  66. // btstep = UUIDW;
  67. // }
  68. // else if(!sendCmd_BT("AT+TXPOWER?\r\n","+TXPOWER:10",1,1))
  69. // {
  70. // PRINT_NUUID("TXPOWER");//TXPOWER项不符合
  71. // btstep = TxPower;
  72. // }
  73. else
  74. {
  75. PRINT_UUID("UUID项及发射功率");//符合设置项
  76. btstep = BtStepDefault;
  77. }
  78. }
  79. }
  80. /**
  81. * @breaf 蓝牙初始化,工作接口
  82. */
  83. void MBA32A_Init(void)
  84. {
  85. btframe.userId = 0x64747531;
  86. btframe.Autoseal = 0xFF; //自动施封时间
  87. printf("\t###01\tBT_init%d...\r\n", resetcount_bt);
  88. Start_Read_resetFlag16();/** 启动位读写检查 **/
  89. // if(sendCmd_BT("AT+DISCONN=0\r\n","ERROR",1,1))//断开连接
  90. // {
  91. // if(sendCmd_BT("AT+DISCONN=1\r\n","ERROR",1,1))//断开连接
  92. // {
  93. // printf("#已无主设备连接\n");
  94. // }
  95. // printf("#已无从设备连接\n");
  96. //
  97. // }
  98. switch(btstep)
  99. {
  100. case BtStepDefault:
  101. break;
  102. case UUIDS:
  103. {
  104. if(sendCmd_BT("AT+UUIDS?\r\n","+UUIDS:FFF0",1,1))//蓝牙芯片默认值,需要修改
  105. {
  106. if(sendCmd_BT("AT+UUIDS=0002\r\n","OK",1,1))
  107. {
  108. printf("#01设置主服务成功\n");
  109. btstep++;
  110. }
  111. }
  112. else
  113. { //ERROR 其他值
  114. MBA32AReset();
  115. break;
  116. }
  117. }
  118. case UUIDN:
  119. {
  120. if(sendCmd_BT("AT+UUIDN?\r\n","+UUIDN:FFF1",1,1))//蓝牙芯片默认值,需要修改
  121. {
  122. if(sendCmd_BT("AT+UUIDN=FF04\r\n","OK",1,1))
  123. {
  124. printf("#02设置读服务成功\n");
  125. btstep++;
  126. }
  127. }
  128. else
  129. { //ERROR 其他值
  130. MBA32AReset();
  131. break;
  132. }
  133. }
  134. case UUIDW:
  135. {
  136. if(sendCmd_BT("AT+UUIDW?\r\n","+UUIDW:FFF2",1,1))//蓝牙芯片默认值,需要修改
  137. {
  138. if(sendCmd_BT("AT+UUIDW=FFF5\r\n","OK",1,1))
  139. {
  140. printf("#03设置写服务成功\n");
  141. btstep++;
  142. }
  143. }
  144. else
  145. { //ERROR 其他值
  146. MBA32AReset();
  147. break;
  148. }
  149. }
  150. case TxPower:
  151. {
  152. if(sendCmd_BT("AT+TXPOWER?\r\n","+TXPOWER:0",1,1))//蓝牙芯片默认值,需要修改
  153. {
  154. if(sendCmd_BT("AT+TXPOWER=10\r\n","OK",1,1))
  155. {
  156. printf("#04设置发射功率成功\n");
  157. btstep++;
  158. }
  159. }
  160. else
  161. { //ERROR 其他值
  162. MBA32AReset();
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * @breaf 串口发送命令子函数
  170. */
  171. void USART_SendString(UART_HandleTypeDef *huart,char *str)
  172. {
  173. //printf("\r\nAT_CMD:\t");
  174. HAL_UART_Transmit(huart, (uint8_t*)str, strlen(str), 0xFFFF); //工作信息串口发送
  175. HAL_UART_Transmit(&huart1, (uint8_t*)str, strlen(str), 0xFFFF); //485打印log
  176. //HAL_UART_Transmit(&huart3, (uint8_t*)str, strlen(str), 0xFFFF); //TTL打印log
  177. }
  178. /**
  179. * @breaf 串口发送命令,有接收数组比较函数
  180. */
  181. uint8_t sendCmd_BT(char *pCmd,char *pRes, uint32_t timeOut, uint8_t sendNum)
  182. {
  183. uint8_t i = 0;
  184. uint32_t time;
  185. for(i = 0; i < sendNum; i++)
  186. {
  187. time = timeOut * 10;
  188. USART_SendString(&huart5, pCmd);
  189. //HAL_Delay(30);
  190. //printf("------->%s",g_MBA32A_8buf_Down);
  191. while(time--)
  192. {
  193. if(strstr((const char *)U5_BTrecvBuff, pRes) != NULL) // 如果检索到关键词
  194. {
  195. memset(U5_BTrecvBuff,0,50);
  196. return 1;
  197. }
  198. HAL_Delay(100);
  199. }
  200. }
  201. return 0;
  202. }
  203. /**
  204. * @breaf 模块复位函数(接收数组清0)
  205. */
  206. void MBA32AReset(void)
  207. {
  208. resetcount_bt+=1;
  209. printf("Bt reset\r\n");
  210. HAL_GPIO_WritePin(REST_BT_GPIO_Port,REST_BT_Pin,GPIO_PIN_RESET);
  211. HAL_Delay(4000);
  212. btstep = UUIDS;
  213. resetBTmodule=true;//freertos监测模块重启位
  214. }
  215. void event_lock(void)
  216. {
  217. }
  218. /**
  219. * @breaf 蓝牙关锁信息帧
  220. */
  221. void close_lock(void) //蓝牙关锁信息帧
  222. {
  223. Time_inquiry(1);
  224. uint8_t header[2] = {0x66, 0x01};//Jl_lock帧头
  225. uint8_t close_CMD[] = {0x03, 0x0B, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, \
  226. 0x00,0x00,0x00,0x00};//关锁命令帧
  227. uint32_t serialNum;
  228. if(preworkUp==3)
  229. {
  230. serialNum = btframe.serialNum1;//流水号
  231. btframe.serialNum1+=1;
  232. }
  233. if(preworkUp==2)
  234. {
  235. serialNum = btframe.serialNum2;//流水号
  236. btframe.serialNum2+=1;
  237. }
  238. if(preworkUp==1)
  239. {
  240. serialNum = btframe.serialNum3;//流水号
  241. btframe.serialNum3+=1;
  242. }
  243. if(preworkUp==0)
  244. {
  245. serialNum = btframe.serialNum4;//流水号
  246. btframe.serialNum4+=1;
  247. }
  248. close_CMD[2]=(serialNum>>24) & 0xff;
  249. close_CMD[3]=(serialNum>>16) & 0xff;
  250. close_CMD[4]=(serialNum>>8) & 0xff;
  251. close_CMD[5]= serialNum & 0xff;
  252. uint32_t userid = btframe.userId;//用户id
  253. close_CMD[6]=(userid>>24) & 0xff;
  254. close_CMD[7]=(userid>>16) & 0xff;
  255. close_CMD[8]=(userid>>8) & 0xff;
  256. close_CMD[9]= userid & 0xff;
  257. RTCtime();
  258. close_CMD[10]= ((dblocptr->location.timeStamp)>>24) & 0xff;
  259. close_CMD[11]= ((dblocptr->location.timeStamp)>>16) & 0xff;
  260. close_CMD[12]= ((dblocptr->location.timeStamp)>>8) & 0xff;
  261. close_CMD[13]= (dblocptr->location.timeStamp) & 0xff;
  262. sendCombinedArray(header,sizeof(header),close_CMD,sizeof(close_CMD),1);
  263. //Echo_BT(20);
  264. }
  265. /**
  266. * @breaf 蓝牙开锁信息帧
  267. */
  268. void open_lock(void) //蓝牙开锁信息帧
  269. {
  270. uint8_t header[2] = {0x66, 0x01};//Jl_lock帧头
  271. uint8_t open_CMD[] = {0x02, 0x0C, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, \
  272. 0x00,0x00,0x00,0x00, 0x00};//开锁命令帧
  273. uint32_t serialNum;
  274. if(menu.current==4)
  275. {
  276. serialNum = btframe.serialNum1;//流水号
  277. }
  278. if(menu.current==5)
  279. {
  280. serialNum = btframe.serialNum2;//流水号
  281. }
  282. if(menu.current==6)
  283. {
  284. serialNum = btframe.serialNum3;//流水号
  285. }
  286. if(menu.current==7)
  287. {
  288. serialNum = btframe.serialNum4;//流水号
  289. }
  290. open_CMD[2]=(serialNum>>24) & 0xff;
  291. open_CMD[3]=(serialNum>>16) & 0xff;
  292. open_CMD[4]=(serialNum>>8) & 0xff;
  293. open_CMD[5]= serialNum & 0xff;
  294. uint32_t userid = btframe.userId;//用户id
  295. open_CMD[6]=(userid>>24) & 0xff;
  296. open_CMD[7]=(userid>>16) & 0xff;
  297. open_CMD[8]=(userid>>8) & 0xff;
  298. open_CMD[9]= userid & 0xff;
  299. RTCtime();
  300. open_CMD[10]= ((dblocptr->location.timeStamp)>>24) & 0xff;
  301. open_CMD[11]= ((dblocptr->location.timeStamp)>>16) & 0xff;
  302. open_CMD[12]= ((dblocptr->location.timeStamp)>>8) & 0xff;
  303. open_CMD[13]= (dblocptr->location.timeStamp) & 0xff;
  304. open_CMD[14] = btframe.Autoseal;//自动施封时间
  305. sendCombinedArray(header,sizeof(header),open_CMD,sizeof(open_CMD),1);//加密
  306. //Echo_BT(20);
  307. }
  308. uint16_t serialNum1[2];
  309. void Lock_OandC(void)
  310. {
  311. if(L1Working) //在四个界面,按下确认
  312. {
  313. if(presjfUp==1)//施封
  314. {
  315. close_lock(); //施封操作需要写入0x40
  316. }
  317. else if(presjfUp==0)//施封
  318. {
  319. open_lock(); //解封操作 需要写入0x60
  320. }
  321. serialNum1[0] = (uint16_t)((btframe.serialNum1 >> 24) & 0xff) << 8 | ((btframe.serialNum1 >> 16) & 0xff);
  322. serialNum1[1] = (uint16_t)((btframe.serialNum1 >> 8) & 0xff) << 8 | (btframe.serialNum1 & 0xff);
  323. Flash_WriteBytes(serialNum1,serialNum1Addr,2);
  324. L1Working = false;
  325. }
  326. }
  327. void ReU5Proces(void)
  328. {
  329. if(autoRe[9]==0x54 && autoRe[10]==0x49 && autoRe[11]==0x4D && autoRe[12]==0x45 && autoRe[13]==0x4F && autoRe[14]==0x55 && autoRe[15]==0x54)//TIMEOUT
  330. {
  331. tipsflag = Timeout;
  332. HAL_Delay(1000);
  333. if(preworkUp==3)
  334. {
  335. SET_MENU_STATUS(4,1,0,3);
  336. }
  337. else if(preworkUp==2)
  338. {
  339. SET_MENU_STATUS(5,1,0,3);
  340. }
  341. else if(preworkUp==1)
  342. {
  343. SET_MENU_STATUS(6,1,0,3);
  344. }
  345. else if(preworkUp==0)
  346. {
  347. SET_MENU_STATUS(7,1,0,3);
  348. }
  349. memset(autoRe,0,1024);
  350. }
  351. 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 \
  352. && autoRe[10]==0x3A)//CONNECTED:1 做从设备 0:做主设备
  353. {
  354. if(sys_mode[1]==DTU_MODE && autoRe[11]==0x31)
  355. {
  356. tipsflag = ConnOK;
  357. menu.current = 0xBB;
  358. btconn_dtu=true;
  359. refresh=true;
  360. }
  361. else if(sys_mode[1]==PDA_MODE && autoRe[11]==0x30)
  362. {
  363. tipsflag = ConnOK;
  364. refresh=true;
  365. HAL_Delay(1000);
  366. tipsflag = Working;
  367. refresh=true;
  368. HAL_Delay(1000);
  369. if(preworkUp==3)
  370. {
  371. L1Working= true;
  372. }
  373. else if(preworkUp==2)
  374. {
  375. L2Working= true;
  376. }
  377. else if(preworkUp==1)
  378. {
  379. L3Working= true;
  380. }
  381. else if(preworkUp==0)
  382. {
  383. L4Working= true;
  384. }
  385. }
  386. memset(autoRe,0,1024);
  387. }
  388. //*44 49 53 43 4f 4e 4e 3a 31
  389. 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:做主设备
  390. {
  391. if(sys_mode[1]==DTU_MODE && autoRe[9]==0x31)
  392. {
  393. tipsflag = Disconn;
  394. menu.current = 0xBB;
  395. btconn_dtu=false;
  396. refresh=true;
  397. }
  398. else if(sys_mode[1]==PDA_MODE && autoRe[9]==0x30)
  399. {
  400. tipsflag = ConnOK;
  401. refresh=true;
  402. HAL_Delay(1000);
  403. tipsflag = Working;
  404. refresh=true;
  405. HAL_Delay(1000);
  406. if(preworkUp==3)
  407. {
  408. L1Working= true;
  409. }
  410. else if(preworkUp==2)
  411. {
  412. L2Working= true;
  413. }
  414. else if(preworkUp==1)
  415. {
  416. L3Working= true;
  417. }
  418. else if(preworkUp==0)
  419. {
  420. L4Working= true;
  421. }
  422. }
  423. memset(autoRe,0,1024);
  424. }
  425. else if(autoRe[0]==0x45 && autoRe[1]==0x52 && autoRe[2]==0x52 && autoRe[3]==0x4f && autoRe[4]==0x52)//ERROR
  426. {
  427. tipsflag = ConnFail;
  428. HAL_Delay(1000);
  429. //SET_MENU_STATUS(3,3,0,2);//回到操作界面
  430. //USART_SendString(&huart5, "AT+DISCONN=0\r\n");//断开连接
  431. memset(autoRe,0,1024);
  432. }
  433. //*接收PDA信息
  434. else if(autoRe[0]==0x66)
  435. {
  436. for (int i = 0; i < U5_BTrecvLength; i++)
  437. {
  438. PDA_pushArr[i] = (uint16_t)(autoRe[2*i] << 8) | autoRe[2*i+1];
  439. }
  440. memset(autoRe,0,1024);
  441. }
  442. }