mba32a.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 L1Worked = false;
  19. bool L2Worked = false;
  20. bool L3Worked = false;
  21. bool L4Worked = false;
  22. bool L5Worked = false;
  23. bool L6Worked = false;
  24. bool L7Worked = false;
  25. bool L8Worked = false;
  26. //*字符串
  27. char autoRe[1024];
  28. //*外部方法
  29. uint16_t read_flash_16(uint32_t addr);
  30. void Write_Information(uint32_t addr, uint16_t newValue);
  31. //*本模块方法
  32. uint8_t sendCmd_BT(char *pCmd,char *pRes, uint32_t timeOut, uint8_t sendNum);
  33. void MBA32AReset(void);
  34. uint16_t resetdev[2];
  35. void open_lock(BtFrame btframe);
  36. void close_lock(BtFrame btframe);
  37. extern TipsFlag tipsflag;
  38. /**
  39. * @breaf 蓝牙启动位读写检查
  40. */
  41. void Start_Read_resetFlag16(void)
  42. {
  43. resetdev[0] = 0xAAAA;
  44. Flash_ReadBytes(&resetdev[1], resetDevAddr, 2);
  45. resetFlag16 = resetdev[1];
  46. if(resetFlag16==0xFFFF)//开机检测(首次启动)
  47. {
  48. if(sendCmd_BT("AT+RESET=1\r\n","OK",1,1))
  49. {
  50. Flash_WriteBytes(resetdev,resetDevAddr,1);
  51. printf("###复位设备成功\r\n");
  52. btstep = UUIDS;
  53. }
  54. }
  55. else if(resetFlag16==0xAAAA)//开机检测(大于1次启动)
  56. {
  57. printf("###蓝牙模块非首次启动\r\n");
  58. if (!sendCmd_BT("AT+UUIDS?\r\n","+UUIDS:0002",1,1))
  59. {
  60. PRINT_NUUID("UUIDS");//UUID项不符合
  61. btstep = UUIDS;
  62. }
  63. else
  64. {
  65. PRINT_UUID("UUID项及发射功率");//符合设置项
  66. btstep = BtStepDefault;
  67. }
  68. }
  69. }
  70. /**
  71. * @breaf 蓝牙初始化,工作接口
  72. */
  73. void MBA32A_Init(void)
  74. {
  75. btframe.userId = 0x64747531;
  76. btframe.Autoseal = 0xFF; //自动施封时间
  77. //Flash_ReadBytes((uint16_t*)&btframe.serialNum, serialNumAddr, 2);//读取4字节流水号
  78. //if(btframe.serialNum==0xFFFFFFFF)
  79. //{
  80. btframe.serialNum = 0;//u32
  81. //}
  82. printf("\t###01\tBT_init%d...\r\n", resetcount_bt);
  83. Start_Read_resetFlag16();/** 启动位读写检查 **/
  84. if(sendCmd_BT("AT+DISCONN=0\r\n","ERROR",1,1))//断开连接
  85. {
  86. if(sendCmd_BT("AT+DISCONN=1\r\n","ERROR",1,1))//断开连接
  87. {
  88. printf("#已无主设备连接\n");
  89. }
  90. printf("#已无从设备连接\n");
  91. }
  92. switch(btstep)
  93. {
  94. case BtStepDefault:
  95. break;
  96. case UUIDS:
  97. {
  98. if(sendCmd_BT("AT+UUIDS?\r\n","+UUIDS:FFF0",1,1))//蓝牙芯片默认值,需要修改
  99. {
  100. if(sendCmd_BT("AT+UUIDS=0002\r\n","OK",1,1))
  101. {
  102. printf("#01设置主服务成功\n");
  103. btstep++;
  104. }
  105. }
  106. else
  107. { //ERROR 其他值
  108. //MBA32AReset();
  109. break;
  110. }
  111. }
  112. case UUIDN:
  113. {
  114. if(sendCmd_BT("AT+UUIDN?\r\n","+UUIDN:FFF1",1,1))//蓝牙芯片默认值,需要修改
  115. {
  116. if(sendCmd_BT("AT+UUIDN=FF04\r\n","OK",1,1))
  117. {
  118. printf("#02设置读服务成功\n");
  119. btstep++;
  120. }
  121. }
  122. else
  123. { //ERROR 其他值
  124. //MBA32AReset();
  125. break;
  126. }
  127. }
  128. case UUIDW:
  129. {
  130. if(sendCmd_BT("AT+UUIDW?\r\n","+UUIDW:FFF2",1,1))//蓝牙芯片默认值,需要修改
  131. {
  132. if(sendCmd_BT("AT+UUIDW=FFF5\r\n","OK",1,1))
  133. {
  134. printf("#03设置写服务成功\n");
  135. btstep++;
  136. }
  137. }
  138. else
  139. { //ERROR 其他值
  140. //MBA32AReset();
  141. break;
  142. }
  143. }
  144. case TxPower:
  145. {
  146. if(sendCmd_BT("AT+TXPOWER?\r\n","+TXPOWER:0",1,1))//蓝牙芯片默认值,需要修改
  147. {
  148. if(sendCmd_BT("AT+TXPOWER=10\r\n","OK",1,1))
  149. {
  150. printf("#04设置发射功率成功\n");
  151. btstep++;
  152. }
  153. }
  154. else
  155. { //ERROR 其他值
  156. //MBA32AReset();
  157. break;
  158. }
  159. }
  160. }
  161. //menu.current=0;
  162. //refresh=true;
  163. }
  164. /**
  165. * @breaf 串口发送命令子函数
  166. */
  167. void USART_SendString(UART_HandleTypeDef *huart,char *str)
  168. {
  169. //printf("\r\nAT_CMD:\t");
  170. HAL_UART_Transmit(huart, (uint8_t*)str, strlen(str), 0xFFFF); //工作信息串口发送
  171. HAL_UART_Transmit(&huart1, (uint8_t*)str, strlen(str), 0xFFFF); //485打印log
  172. //HAL_UART_Transmit(&huart3, (uint8_t*)str, strlen(str), 0xFFFF); //TTL打印log
  173. }
  174. /**
  175. * @breaf 串口发送命令,有接收数组比较函数
  176. */
  177. uint8_t sendCmd_BT(char *pCmd,char *pRes, uint32_t timeOut, uint8_t sendNum)
  178. {
  179. uint8_t i = 0;
  180. uint32_t time;
  181. for(i = 0; i < sendNum; i++)
  182. {
  183. time = timeOut * 10;
  184. USART_SendString(&huart5, pCmd);
  185. //HAL_Delay(30);
  186. //printf("------->%s",g_MBA32A_8buf_Down);
  187. while(time--)
  188. {
  189. if(strstr((const char *)U5_BTrecvBuff, pRes) != NULL) // 如果检索到关键词
  190. {
  191. memset(U5_BTrecvBuff,0,50);
  192. return 1;
  193. }
  194. HAL_Delay(100);
  195. }
  196. }
  197. return 0;
  198. }
  199. /**
  200. * @breaf 模块复位函数(接收数组清0)
  201. */
  202. void MBA32AReset(void)
  203. {
  204. resetcount_bt+=1;
  205. printf("Bt reset\r\n");
  206. HAL_GPIO_WritePin(REST_BT_GPIO_Port,REST_BT_Pin,GPIO_PIN_RESET);
  207. HAL_Delay(4000);
  208. btstep = UUIDS;
  209. resetBTmodule=true;//freertos监测模块重启位
  210. }
  211. /**
  212. * @breaf 蓝牙开锁信息帧
  213. */
  214. void open_lock(BtFrame btframe) //蓝牙开锁信息帧
  215. {
  216. uint8_t header[2] = {0x66, 0x01};//Jl_lock帧头
  217. uint8_t open_CMD[] = {0x02, 0x0C, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, \
  218. 0x00,0x00,0x00,0x00, 0x00};//开锁命令帧
  219. open_CMD[2]=(btframe.serialNum>>24) & 0xff;
  220. open_CMD[3]=(btframe.serialNum>>16) & 0xff;
  221. open_CMD[4]=(btframe.serialNum>>8) & 0xff;
  222. open_CMD[5]= btframe.serialNum & 0xff;
  223. uint32_t userid = btframe.userId;//用户id
  224. open_CMD[6]=(userid>>24) & 0xff;
  225. open_CMD[7]=(userid>>16) & 0xff;
  226. open_CMD[8]=(userid>>8) & 0xff;
  227. open_CMD[9]= userid & 0xff;
  228. uint32_t ntime = dblocptr->location.timeStamp;//现在时间
  229. open_CMD[10]=(ntime>>24) & 0xff;
  230. open_CMD[11]=(ntime>>16) & 0xff;
  231. open_CMD[12]=(ntime>>8) & 0xff;
  232. open_CMD[13]= ntime & 0xff;
  233. open_CMD[14] = btframe.Autoseal;//自动施封时间
  234. sendCombinedArray(header,sizeof(header),open_CMD,sizeof(open_CMD),1);//加密
  235. }
  236. /**
  237. * @breaf 蓝牙关锁信息帧
  238. */
  239. void close_lock(BtFrame btframe) //蓝牙关锁信息帧
  240. {
  241. uint8_t header[2] = {0x66, 0x01};//Jl_lock帧头
  242. uint8_t close_CMD[] = {0x03, 0x0B, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, \
  243. 0x00,0x00,0x00,0x00};//关锁命令帧
  244. close_CMD[2]=(btframe.serialNum>>24) & 0xff;
  245. close_CMD[3]=(btframe.serialNum>>16) & 0xff;
  246. close_CMD[4]=(btframe.serialNum>>8) & 0xff;
  247. close_CMD[5]= btframe.serialNum & 0xff;
  248. uint32_t userid = btframe.userId;//用户id
  249. close_CMD[6]=(userid>>24) & 0xff;
  250. close_CMD[7]=(userid>>16) & 0xff;
  251. close_CMD[8]=(userid>>8) & 0xff;
  252. close_CMD[9]= userid & 0xff;
  253. uint32_t ntime = dblocptr->location.timeStamp;//现在时间
  254. close_CMD[10]=(ntime>>24) & 0xff;
  255. close_CMD[11]=(ntime>>16) & 0xff;
  256. close_CMD[12]=(ntime>>8) & 0xff;
  257. close_CMD[13]= ntime & 0xff;
  258. sendCombinedArray(header,sizeof(header),close_CMD,sizeof(close_CMD),1);
  259. }
  260. int workedLock = 0; // 用于跟踪已连接的设备的数量
  261. int devices[8] = {0}; // 数组用于跟踪设备的工作状态
  262. void connectDevices(bool openClose)
  263. {
  264. char* snName = NULL;
  265. char* snConn = NULL;
  266. //char* snEd = NULL;
  267. while(workedLock < macsetptr->macCount)//当所以操作完成跳出循环
  268. {
  269. for(int i=0;i<macsetptr->macCount;i++)//循环遍历所有设备空间
  270. {
  271. if (devices[i] == 0) // 检查设备是否完成操作
  272. {
  273. //*获取当前连接字符串
  274. snName = lockinfo.SNname[i];
  275. snConn = macsetptr->SNCONN[i];
  276. //snEd = macsetptr->SNED[i];
  277. printf("设备%d执行操作->%s\n", i, snName);//0-7:1-8
  278. if (snName != NULL && sendCmd_BT(snConn, "ERROR", 1, 1)) //进行连接 macsetptr->Btconn[workedLock]
  279. {
  280. btframe.serialNum+=1;
  281. if (openClose) {
  282. close_lock(btframe); //施封操作
  283. printf("已发送施封报文至设备-%s!,流水号%04X\n", snName, btframe.serialNum);
  284. }
  285. else if(!openClose) {
  286. open_lock(btframe); //解封操作
  287. printf("已发送解封报文至设备-%s!,流水号%04X\n", snName, btframe.serialNum);
  288. }
  289. }
  290. }
  291. }
  292. printf("延时1000\r\n");
  293. HAL_Delay(1000);
  294. }
  295. }
  296. bool openOrClose =false;
  297. void open_close_lock(void)
  298. {
  299. connectDevices(openOrClose);
  300. }