mba32a.c 10 KB

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