mba32a.c 9.3 KB

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