bc26.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /**
  2. * author wulianwei
  3. * title BC260Y模块驱动
  4. */
  5. #include "stm32f10x.h"
  6. #include "bc26.h"
  7. #include "sys.h"
  8. #include "module_wrapper.h"
  9. #include "at.h"
  10. //C库
  11. #include <string.h>
  12. #include <stdio.h>
  13. extern uart_recv_t uart_recv;
  14. extern valid_data_t valid_data;
  15. uint8_t err_cout = MODULE_FAIL;
  16. static at_event_t* event = NULL;//当前事件
  17. //从睡眠中唤醒BC26
  18. static void bc26_wake(void)
  19. {
  20. at_send_simple_cmd(AT); //AT触发
  21. }
  22. //==========================================================
  23. // 函数名称: bc26_send_data
  24. //
  25. // 函数功能: 发送数据
  26. //
  27. // 入口参数: data:数据(数组)
  28. // len:长度
  29. //
  30. // 返回参数: 无
  31. //
  32. // 说明:
  33. //==========================================================
  34. static int bc26_send_data(const void *data, int len)
  35. {
  36. char cmdBuf[SEND_SIZE+20]={0};
  37. char hexData[SEND_SIZE] = {0};
  38. bc26_wake();
  39. //bc26_tcp_reconnect();
  40. byteToHexStr(data,hexData,len);
  41. snprintf(cmdBuf,SEND_SIZE,"AT+QISEND=0,%d,\"%s\",%d\r\n", len,hexData,0); //发送数据,返回后不释放链接
  42. // UsartPrintf(USART_DEBUG, "raisend:%s\r\n",cmdBuf);
  43. if(!at_send_cmd(cmdBuf, "SEND OK"))
  44. {
  45. // Usart_SendString(USART2, data, len); //发送设备连接请求数据
  46. return 0;
  47. }
  48. return -1;
  49. }
  50. static int bc26_send_data_rai(const void *data, int len,int rai)
  51. {
  52. char cmdBuf[SEND_SIZE+20]={0};
  53. char hexData[SEND_SIZE] = {0};
  54. bc26_wake();
  55. delay_ms(3);
  56. bc26_wake();
  57. //清空接收缓存
  58. //bc26_tcp_reconnect();
  59. len = len<=SEND_SIZE/2 ? len:SEND_SIZE/2;
  60. byteToHexStr(data,hexData, len);
  61. snprintf(cmdBuf,sizeof(cmdBuf),"AT+QISEND=0,%d,\"%s\",%d\r\n", len,hexData,rai); //发送数据,返回后释放链接
  62. //UsartPrintf(USART_DEBUG, "\r\nraisend:%s,%d\r\n",cmdBuf,strlen(cmdBuf));
  63. if(!at_send_cmd(cmdBuf, "SEND OK"))
  64. {
  65. // Usart_SendString(USART2, data, len); //发送设备连接请求数据
  66. return 0;
  67. }
  68. return -1;
  69. }
  70. /**选择通道发送数据*/
  71. static int bc26_send_data_ch(const void *data, int len,uint8_t ch,int rai)
  72. {
  73. char cmdBuf[SEND_SIZE+20]={0};
  74. char hexData[SEND_SIZE] = {0};
  75. bc26_wake();
  76. delay_ms(3);
  77. bc26_wake();
  78. //清空接收缓存
  79. //bc26_tcp_reconnect();
  80. len = len<=SEND_SIZE/2 ? len:SEND_SIZE/2;
  81. byteToHexStr(data,hexData, len);
  82. snprintf(cmdBuf,sizeof(cmdBuf),"AT+QISEND=%d,%d,\"%s\",%d\r\n", ch,len,hexData,rai); //发送数据,返回后释放链接
  83. //UsartPrintf(USART_DEBUG, "\r\nraisend:%s,%d\r\n",cmdBuf,strlen(cmdBuf));
  84. if(!at_send_cmd(cmdBuf, "SEND OK"))
  85. {
  86. // Usart_SendString(USART2, data, len); //发送设备连接请求数据
  87. return 0;
  88. }
  89. return -1;
  90. }
  91. //==========================================================
  92. // 函数名称: bc26_get_data
  93. //
  94. // 函数功能: 获取平台返回的数据
  95. //
  96. // 入口参数: data:存储有效数据(数组),len:有效数据长度,timeOut:等待的时间(ms)
  97. //
  98. // 返回参数: 平台返回的原始数据
  99. //
  100. // 说明: 不同网络设备返回的格式不同,需要去调试
  101. //==========================================================
  102. static int bc26_get_data(void *data,int len,int timeOut)
  103. {
  104. int minLen = 0;
  105. char *recvdiff[4] = {0};//分割字符串地址
  106. char recvbuf[RECV_SIZE] = {0};
  107. int data_len = 0;//有效数据长度
  108. char *recvSign = "+QIURC: \"recv\",0,";
  109. if(!uart_recv_match_wait(recvSign,timeOut))
  110. {
  111. uart_recv.start_addr = strstr(uart_recv.buf,recvSign);
  112. UsartPrintf(USART_DEBUG, "返回数据:%d,%s\r\n",uart_recv.len,uart_recv.start_addr);
  113. memcpy((char*)recvbuf, (char*)uart_recv.start_addr, uart_recv.len);
  114. split((char*)recvbuf,",",recvdiff,sizeof(recvdiff));
  115. data_len = my_atoi(recvdiff[2]);
  116. minLen = data_len < len? data_len : len;
  117. memcpy(data,(char*)recvdiff[3]+1,minLen); //有效数据前后多了一个双引号
  118. uart_recv_clear();
  119. return data_len;
  120. }
  121. uart_recv_clear();
  122. return -1;
  123. }
  124. static void bc26_restart(void)
  125. {
  126. UsartPrintf(USART_DEBUG, "自动重启\r\n");
  127. BC26_RST_HIGH();
  128. delay_ms(60);
  129. BC26_RST_LOW();
  130. uart_recv_clear();
  131. }
  132. static int bc26_config(void)
  133. {
  134. char recv[300] = {0};
  135. at_send_cmd_back_wait(AT,recv,sizeof(recv),100); //AT触发
  136. UsartPrintf(USART_DEBUG, "1.休眠\r\n");
  137. at_send_cmd_back(LIGHTSLEEP,recv,sizeof(recv));
  138. if(!strstr(recv,"OK"))
  139. {
  140. UsartPrintf(USART_DEBUG,"休眠失败\r\n");
  141. return -1;
  142. }
  143. UsartPrintf(USART_DEBUG, "2.不回显命令\r\n");
  144. at_send_cmd_back(ATE0,recv,sizeof(recv));
  145. if(!strstr(recv,"OK"))
  146. {
  147. UsartPrintf(USART_DEBUG,"禁止回显失败\r\n");
  148. return -1;
  149. }
  150. UsartPrintf(USART_DEBUG, "3.16进制发送数据\r\n");
  151. at_send_cmd_back(QICFG,recv,sizeof(recv));
  152. if(!strstr(recv,"OK"))
  153. {
  154. UsartPrintf(USART_DEBUG,"16进制发送数据失败\r\n");
  155. return -1;
  156. }
  157. module_init_status = MODULE_OK;
  158. return 0;
  159. }
  160. //==========================================================
  161. // 函数名称: bc26_init
  162. //
  163. // 函数功能: 初始化BC26
  164. //
  165. // 入口参数: 无
  166. //
  167. // 返回参数: 0 成功 1失败
  168. //
  169. // 说明:
  170. //==========================================================
  171. static int bc26_init(void)
  172. {
  173. return bc26_config();
  174. }
  175. /**
  176. * @Title 模块ue功能关闭
  177. * @Return 0:成功,-1:失败
  178. */
  179. static int bc26_ue_down(void)
  180. {
  181. bc26_wake();//唤醒
  182. delay_ms(10);
  183. if(at_send_cmd(CFUN0,"OK"))
  184. {
  185. UsartPrintf(USART_DEBUG, "UE功能关闭\r\n");
  186. return -1;
  187. }
  188. tcp_connect_status = TCP_CLOSED; //tcp断开
  189. UsartPrintf(USART_DEBUG, "UE功能关闭失败\r\n");
  190. return 0;
  191. }
  192. /**
  193. * @Title TCP连接
  194. * @Param ip:地址, port:端口
  195. */
  196. static void bc26_tcp_connect(const char *ip, const char *port)
  197. {
  198. char conCmd[200]={0};
  199. bc26_wake();//唤醒
  200. UsartPrintf(USART_DEBUG, "连接TCP服务器\r\n");
  201. snprintf(conCmd,sizeof(conCmd),QIOPEN,ip,port);
  202. at_send_simple_cmd(conCmd);
  203. tcp_connect_status = TCP_CONNECTING;
  204. }
  205. /**
  206. * @Title 链接自定义
  207. * @Param ip:地址, port:端口
  208. */
  209. static void bc26_channel_open(const char *ip, const char *port,uint8_t proto, uint8_t channel)
  210. {
  211. char conCmd[200]={0};
  212. if(channel<MIN_CHANNEL_NUM) return;
  213. bc26_wake();//唤醒
  214. UsartPrintf(USART_DEBUG, "连接服务器channel:%d\r\n",channel);
  215. if(proto == UDP)
  216. {
  217. snprintf(conCmd,sizeof(conCmd),QIOPENOPT,channel,"UDP",ip,port);
  218. }
  219. else if(proto == TCP)
  220. {
  221. snprintf(conCmd,sizeof(conCmd),QIOPENOPT,channel,"TCP",ip,port);
  222. }
  223. at_send_simple_cmd(conCmd);
  224. module_channel[channel].proto = proto;
  225. module_channel[channel].connect_status = TCP_CONNECTING;
  226. if(channel == 0)
  227. {
  228. tcp_connect_status = TCP_CONNECTING;
  229. }
  230. }
  231. /**
  232. * @Title 打开UDP端口
  233. * @Param ip:地址, port:端口
  234. */
  235. static void bc26_udp_open(const char *ip, const char *port)
  236. {
  237. char conCmd[200]={0};
  238. // bc26_wake();//唤醒
  239. snprintf(conCmd,sizeof(conCmd),QIOPENUDP,ip,port);
  240. UsartPrintf(USART_DEBUG, "连接UDP服务器\r\n");
  241. at_send_simple_cmd(conCmd);
  242. tcp_connect_status = TCP_CONNECTING;
  243. }
  244. /**
  245. * @Title TCP通道关闭
  246. * @Return 0:成功,-1:失败
  247. */
  248. static int bc26_tcp_close(void)
  249. {
  250. bc26_wake();//唤醒
  251. delay_ms(10);
  252. //UsartPrintf(USART_DEBUG, "断开TCP操作:%s\r\n",QICLOSE);
  253. if(at_send_cmd(QICLOSE,"CLOSE OK"))
  254. {
  255. return -1;
  256. }
  257. tcp_connect_status = TCP_CLOSED; //tcp断开
  258. UsartPrintf(USART_DEBUG, "断开TCP服务器\r\n");
  259. return 0;
  260. }
  261. /**
  262. * @Title TCP通道关闭
  263. * @Return 0:成功,-1:失败
  264. */
  265. static int bc26_channel_close(uint8_t channel)
  266. {
  267. char conCmd[20]={0};
  268. snprintf(conCmd,sizeof(conCmd),QICLOSECH,channel);
  269. bc26_wake();//唤醒
  270. delay_ms(10);
  271. //UsartPrintf(USART_DEBUG, "断开TCP操作:%s\r\n",QICLOSE);
  272. if(at_send_cmd(conCmd,"CLOSE OK"))
  273. {
  274. return -1;
  275. }
  276. module_channel[channel].connect_status = TCP_CLOSED; //tcp断开
  277. UsartPrintf(USART_DEBUG, "断开服务器:%d\r\n",channel);
  278. return 0;
  279. }
  280. /**
  281. * @Title 判断tcp连接状态
  282. * @Return 0 保持连接,-1 断开连接
  283. */
  284. static int bc26_tcp_state(void)
  285. {
  286. char *statebuf[8] ={0};
  287. char recvbuf[200] = {0};
  288. bc26_wake();
  289. delay_ms(15);
  290. at_send_cmd_back(QISTATE,recvbuf,sizeof(recvbuf));
  291. if(strstr(recvbuf,"OK"))
  292. {
  293. if(strstr((char *)recvbuf,"+QISTATE: 0"))
  294. {
  295. split((char*)recvbuf,",",statebuf,sizeof(statebuf));
  296. if(statebuf[5])
  297. {
  298. if(*(statebuf[5]) == '2')
  299. {
  300. tcp_connect_status = TCP_OPEN; //tcp连接
  301. UsartPrintf(USART_DEBUG, "TCP服务器保持链接\r\n");
  302. return 0;
  303. }
  304. }
  305. }
  306. }
  307. tcp_connect_status = TCP_CLOSED; //tcp断开
  308. UsartPrintf(USART_DEBUG, "TCP服务器已断开\r\n");
  309. return -1;
  310. }
  311. /**
  312. * @Title 判断通道链接状态
  313. * @Return 0 保持连接,-1 断开连接
  314. */
  315. static int bc26_channel_state(uint8_t channel)
  316. {
  317. char *statebuf[8] ={0};
  318. char recvbuf[200] = {0};
  319. char conCmd[20]={0};
  320. char signbuf[20]={0};
  321. bc26_wake();
  322. delay_ms(15);
  323. snprintf(conCmd,sizeof(conCmd),QISTATECH,channel);
  324. at_send_cmd_back(conCmd,recvbuf,sizeof(recvbuf));
  325. if(strstr(recvbuf,"OK"))
  326. {
  327. if(strstr((char *)recvbuf,"+QISTATE: "))
  328. {
  329. split((char*)recvbuf,",",statebuf,sizeof(statebuf));
  330. if(statebuf[5])
  331. {
  332. if(*(statebuf[5]) == '2')
  333. {
  334. module_channel[channel].connect_status = TCP_OPEN; //tcp连接
  335. UsartPrintf(USART_DEBUG, "通道服务器保持链接:%d\r\n",channel);
  336. return 0;
  337. }
  338. }
  339. }
  340. }
  341. module_channel[channel].connect_status = TCP_CLOSED; //tcp断开
  342. UsartPrintf(USART_DEBUG, "通道服务器已断开:%d\r\n",channel);
  343. return -1;
  344. }
  345. static void bc26_tcp_reconnect(const char *ip, const char *port)
  346. {
  347. bc26_wake();//唤醒
  348. if(bc26_tcp_state())
  349. {
  350. UsartPrintf(USART_DEBUG, "重连服务器\r\n");
  351. bc26_tcp_close();
  352. bc26_tcp_connect(ip,port);
  353. }
  354. }
  355. static void bc26_udp_reopen(const char *ip, const char *port)
  356. {
  357. bc26_wake();//唤醒
  358. if(bc26_tcp_state())
  359. {
  360. UsartPrintf(USART_DEBUG, "重连服务器\r\n");
  361. bc26_tcp_close();
  362. bc26_udp_open(ip,port);
  363. }
  364. }
  365. static void bc26_channel_reopen(const char *ip, const char *port,uint8_t proto, uint8_t channel)
  366. {
  367. bc26_wake();//唤醒
  368. if(bc26_channel_state(channel))
  369. {
  370. UsartPrintf(USART_DEBUG, "重连服务器\r\n");
  371. bc26_channel_close(channel);
  372. bc26_channel_open(ip,port,proto,channel);
  373. }
  374. }
  375. static void bc26_online_event(void)
  376. {
  377. char *recvdiff[4] = {0};//分割字符串地址
  378. uint8_t ch = 0;
  379. uint16_t result = 0;
  380. UsartPrintf(USART_DEBUG, "bc26_online_event:%s\r\n",uart_recv.start_addr);
  381. splitCharLimit((char*)(uart_recv.start_addr+strlen(event->event_header)),',',recvdiff,2);
  382. ch = my_atoi(recvdiff[0]);
  383. result = my_atoi(recvdiff[1]);
  384. if(result == 0)
  385. {
  386. module_channel[ch].connect_status = TCP_OPEN;
  387. if(ch == 0)
  388. {
  389. tcp_connect_status = TCP_OPEN;// tcp在线
  390. }
  391. }
  392. }
  393. static void bc26_close_event(void)
  394. {
  395. char *recvdiff[4] = {0};//分割字符串地址
  396. uint8_t ch = 0;
  397. UsartPrintf(USART_DEBUG, "bc26_close_event\r\n");
  398. splitCharLimit((char*)(uart_recv.start_addr+strlen(event->event_header)),',',recvdiff,2);
  399. ch = my_atoi(recvdiff[1]);
  400. module_channel[ch].connect_status = TCP_CLOSED;
  401. if(ch == 0)
  402. {
  403. tcp_connect_status = TCP_CLOSED; //tcp断线
  404. }
  405. }
  406. static void bc26_recv_event(void)
  407. {
  408. int i;
  409. int minLen = 0;
  410. char *recvdiff[4] = {0};//分割字符串地址
  411. char recvbuf[RECV_SIZE] = {0};
  412. int data_len = 0;//有效数据长度
  413. uint8_t ch = 0;
  414. int valid_data_size = sizeof(valid_data.buf);
  415. uart_recv_wait(150);//有效数据返回有延迟
  416. memcpy((char*)recvbuf,(char*)uart_recv.start_addr,uart_recv.len);
  417. splitCharLimit((char*)recvbuf,',',recvdiff,4);
  418. ch = my_atoi(recvdiff[1]);
  419. data_len = my_atoi(recvdiff[2]);
  420. minLen = data_len < valid_data_size-1? data_len : valid_data_size-1;
  421. if(ch == 0)
  422. {
  423. valid_data.len = minLen;
  424. memcpy(valid_data.buf,(char*)recvdiff[3]+1,minLen); //有效数据前后多了一个双引号
  425. }
  426. module_channel[ch].valid_data.len = minLen;
  427. memcpy(module_channel[ch].valid_data.buf,(char*)recvdiff[3]+1,minLen); //有效数据前后多了一个双引号
  428. UsartPrintf(USART_DEBUG, "bc26_recv_event\r\n");
  429. }
  430. void bc26_error_event(void)
  431. {
  432. UsartPrintf(USART_DEBUG, "error_event:%s\r\n",uart_recv.buf);
  433. if(err_cout == 5) //连续出错5次UsartPrintf,重启模块
  434. {
  435. err_cout= 0;
  436. module_start_status = MODULE_FAIL;
  437. module_init_status = MODULE_FAIL;
  438. }
  439. }
  440. void bc26_sleep_event(void)
  441. {
  442. UsartPrintf(USART_DEBUG, "ENTER DEEPSLEEP\r\n",uart_recv.buf);
  443. }
  444. static void bc26_restart_event(void)
  445. {
  446. int i= sizeof(module_channel)/sizeof(module_channel[0]);
  447. UsartPrintf(USART_DEBUG, "bc26_restart_event:%s\r\n",uart_recv.buf);
  448. module_start_status = MODULE_OK; //bc260启动成功
  449. module_init_status = MODULE_FAIL; //重新配置参数
  450. tcp_connect_status = TCP_CLOSED;//TCP 需要重连
  451. while(i--)
  452. {
  453. module_channel[i].connect_status = TCP_CLOSED;
  454. }
  455. bc26_config();//配置
  456. }
  457. at_event_t bc26_at_event[] = {
  458. { "+IP:", bc26_restart_event}, //tcp连接成功
  459. { "+QIOPEN: ", bc26_online_event}, //tcp连接成功
  460. { "+QIURC: \"closed\"", bc26_close_event}, //tcp断开链接
  461. { "+QIURC: \"recv\"", bc26_recv_event},
  462. { "ERROR", bc26_error_event},
  463. { "+QNBIOTEVENT: \"ENTER DEEPSLEEP\"", bc26_sleep_event}
  464. };
  465. static void bc26_handle_event(void)
  466. {
  467. int i = 0;
  468. char *start_addr;
  469. if(uart_recv.len ==0) return;
  470. for(i=0;i<sizeof(bc26_at_event)/sizeof(bc26_at_event[0]);i++)
  471. {
  472. event = &bc26_at_event[i];
  473. start_addr = strstr(uart_recv.buf, event->event_header);
  474. if(start_addr)
  475. {
  476. if(event->event_callback == bc26_error_event)
  477. {
  478. err_cout++;
  479. }
  480. else
  481. {
  482. err_cout=0;
  483. }
  484. uart_recv.start_addr = start_addr;
  485. event->event_callback();
  486. uart_recv_clear();
  487. break;
  488. }
  489. }
  490. }
  491. at_module_t at_module_bc26 = {
  492. .start = bc26_restart,
  493. .init = bc26_init,
  494. .connect = bc26_tcp_connect,
  495. .reconnect = bc26_tcp_reconnect,
  496. .udpreopen = bc26_udp_reopen,
  497. .channel_open = bc26_channel_open,
  498. .channel_reopen = bc26_channel_reopen,
  499. .tcpstate = bc26_tcp_state,
  500. .channel_state = bc26_channel_state,
  501. .send = bc26_send_data,
  502. .send_rai = bc26_send_data_rai,
  503. .send_ch = bc26_send_data_ch,
  504. .recv_timeout = bc26_get_data,
  505. .close = bc26_tcp_close,
  506. .channel_close = bc26_channel_close,
  507. .handle_event = bc26_handle_event,
  508. .cmd_back = at_send_cmd_back,
  509. .cmd = at_send_simple_cmd,
  510. .low_power = bc26_ue_down,
  511. .udpopen = bc26_udp_open,
  512. };
  513. int bc26_sal_init()
  514. {
  515. if(at_module_register(&at_module_bc26))
  516. {
  517. module_init_status = MODULE_FAIL;
  518. return -1;
  519. }
  520. if(at_module_init())
  521. {
  522. module_init_status = MODULE_FAIL;
  523. return -1;
  524. }
  525. module_init_status = MODULE_OK;
  526. return 0;
  527. }
  528. void bc26_sal_register()
  529. {
  530. at_module_register(&at_module_bc26);
  531. }