ntrip_rtk_demo.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "ql_api_osi.h"
  16. #include "ql_api_nw.h"
  17. #include "ql_api_dev.h"
  18. #include "ql_api_datacall.h"
  19. #include "quec_pin_index.h"
  20. #include "ql_api_ntrip_rtk.h"
  21. #include "ql_uart.h"
  22. #include "ql_gpio.h"
  23. #include "sockets.h"
  24. #include "lwip/ip_addr.h"
  25. #include "lwip/ip6_addr.h"
  26. #include "lwip/netdb.h"
  27. #include "lwip/netif.h"
  28. #include "lwip/inet.h"
  29. #include "lwip/tcp.h"
  30. #include "ql_log.h"
  31. #include "ntrip_rtk_demo.h"
  32. #define QL_NTRIP_RTK_DEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  33. #define QL_NTRIP_RTK_DEMO_LOG(msg, ...) QL_LOG(QL_NTRIP_RTK_DEMO_LOG_LEVEL, "ql_SPI_DEMO", msg, ##__VA_ARGS__)
  34. #define QL_NTRIP_RTK_DEMO_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_SPI_DEMO", msg, ##__VA_ARGS__)
  35. #define QL_GNSS_UART_RX_BUFF_SIZE 2048
  36. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  37. #define GNSS_NMEA_END_CHAR_1 '\r'
  38. #define GNSS_NMEA_END_CHAR_2 '\n'
  39. #define QL_NTRIP_RTK_APP_START_SOCKET (1001 | (QL_COMPONENT_APP_START<<16))
  40. #define QL_NTRIP_RTK_APP_REAC_DATA (1002 | (QL_COMPONENT_APP_START<<16))
  41. #define QL_NTRIP_RTK_APP_STATE_CHANGE (1003 | (QL_COMPONENT_APP_START<<16))
  42. #define QL_NTRIP_RTK_SERVER_IP "120.24.149.249"
  43. #define QL_NTRIP_RTK_SERVER_PORT 7001
  44. #define QL_NTRIP_RTK_SERVER_USER "HDcv6x5mc9t1nr"
  45. #define QL_NTRIP_RTK_SERVER_PWD "ce10b1c4";
  46. ql_task_t ntrip_rtk_demo_task = NULL;
  47. static ql_ntrip_rtk_state_e ql_ntrip_demo_state = QL_NTRIP_RTK_IDLE;
  48. void ql_gnss_uart_notify_cb(unsigned int ind_type, ql_uart_port_number_e port, unsigned int size)
  49. {
  50. QL_NTRIP_RTK_DEMO_LOG("UART port %d receive ind type:0x%x, receive data size:%d", port, ind_type, size);
  51. switch(ind_type)
  52. {
  53. case QUEC_UART_RX_OVERFLOW_IND: //rx buffer overflow
  54. case QUEC_UART_RX_RECV_DATA_IND:
  55. {
  56. ql_event_t ql_event = {0};
  57. ql_event.id = ind_type;
  58. ql_event.param1 = port;
  59. ql_event.param2 = size;
  60. ql_rtos_event_send(ntrip_rtk_demo_task,&ql_event);
  61. break;
  62. }
  63. }
  64. }
  65. void ql_ntrip_rtk_handle_cb(ql_ntrip_rtk_state_e state, char *data, int len)
  66. {
  67. QL_NTRIP_RTK_DEMO_LOG("state=%d", state);
  68. ql_event_t ql_event = {0};
  69. if (QL_NTRIP_RTK_RECV_DATA == state)
  70. {
  71. //只有为QL_NTRIP_RTK_RECV_DATA时,需要处理数据
  72. if (data && len)
  73. {
  74. if (ntrip_rtk_demo_task)
  75. {
  76. char *rtk_data = (char *)malloc(len);
  77. if (rtk_data)
  78. {
  79. ql_event.id = QL_NTRIP_RTK_APP_REAC_DATA;
  80. ql_event.param1 = (int)rtk_data;
  81. ql_event.param2 = len;
  82. ql_rtos_event_send(ntrip_rtk_demo_task, &ql_event);
  83. }
  84. }
  85. }
  86. }
  87. else
  88. {
  89. ql_event.id = QL_NTRIP_RTK_APP_STATE_CHANGE;
  90. ql_event.param1 = state;
  91. ql_rtos_event_send(ntrip_rtk_demo_task, &ql_event);
  92. }
  93. }
  94. char *ql_gnss_get_lastest_char(char *str, int len, char ch)
  95. {
  96. char *get_char = NULL;
  97. if (NULL == str)
  98. {
  99. return NULL;
  100. }
  101. //QL_NTRIP_RTK_DEMO_LOG("len = %d", strlen(str));
  102. while (len--)
  103. {
  104. if (*str == ch)
  105. {
  106. get_char = str;
  107. }
  108. str++;
  109. }
  110. return get_char;
  111. }
  112. char *ql_gnss_get_typical_char(char *str, int len, char ch, int num)
  113. {
  114. char *get_char = NULL;
  115. int get_num = 0;
  116. if (NULL == str)
  117. {
  118. return NULL;
  119. }
  120. //QL_NTRIP_RTK_DEMO_LOG("len = %d", strlen(str));
  121. while (len--)
  122. {
  123. if (*str == ch)
  124. {
  125. get_num++;
  126. if (get_num == num)
  127. {
  128. get_char = str;
  129. break;
  130. }
  131. }
  132. str++;
  133. }
  134. return get_char;
  135. }
  136. static void ql_ntrip_rtk_demo_pthread(void *arg)
  137. {
  138. ql_errcode_ntrip_rtk_e ret = QL_NTRIP_RTK_SUCCESS;
  139. int profile_idx = 1;
  140. int i = 0;
  141. uint8_t nSim = 0;
  142. char ip4_addr_str[16] = {0};
  143. ql_data_call_info_s info;
  144. ql_uart_errcode_e uart_ret = 0;
  145. ql_errcode_gpio gpio_ret = 0;
  146. ql_uart_config_s uart_cfg = {0};
  147. ql_event_t ql_event = {0};
  148. unsigned char *recv_buff = calloc(1, QL_GNSS_UART_RX_BUFF_SIZE+1);
  149. unsigned int real_size = 0;
  150. int read_len = 0;
  151. unsigned int size = 0;
  152. unsigned char nmea_buff[256];
  153. char *start = NULL, *end = NULL;
  154. static int total_bytes = 0;
  155. char *gnss_nmea_buff = calloc(1, QL_GNSS_UART_RX_BUFF_SIZE+1);
  156. int len = 0;
  157. int ntrip_rtk_id = 0;
  158. ql_ntrip_rtk_config_s config = {0};
  159. ql_rtos_task_sleep_s(10);
  160. QL_NTRIP_RTK_DEMO_LOG("========== ntrip_rtk demo start ==========");
  161. QL_NTRIP_RTK_DEMO_LOG("wait for network register done");
  162. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  163. {
  164. i++;
  165. ql_rtos_task_sleep_s(1);
  166. }
  167. if(ret == 0)
  168. {
  169. i = 0;
  170. QL_NTRIP_RTK_DEMO_LOG("====network registered!!!!====");
  171. }
  172. else
  173. {
  174. QL_NTRIP_RTK_DEMO_LOG("====network register failure!!!!!====");
  175. goto exit;
  176. }
  177. ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  178. QL_NTRIP_RTK_DEMO_LOG("===start data call====");
  179. ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, NULL, NULL, NULL, 0);
  180. QL_NTRIP_RTK_DEMO_LOG("===data call result:%d", ret);
  181. if(ret != 0)
  182. {
  183. QL_NTRIP_RTK_DEMO_LOG("====data call failure!!!!=====");
  184. }
  185. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  186. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  187. if(ret != 0)
  188. {
  189. QL_NTRIP_RTK_DEMO_LOG("ql_get_data_call_info ret: %d", ret);
  190. ql_stop_data_call(nSim, profile_idx);
  191. goto exit;
  192. }
  193. QL_NTRIP_RTK_DEMO_LOG("info->profile_idx: %d", info.profile_idx);
  194. QL_NTRIP_RTK_DEMO_LOG("info->ip_version: %d", info.ip_version);
  195. QL_NTRIP_RTK_DEMO_LOG("info->v4.state: %d", info.v4.state);
  196. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  197. QL_NTRIP_RTK_DEMO_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  198. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  199. QL_NTRIP_RTK_DEMO_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  200. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  201. QL_NTRIP_RTK_DEMO_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  202. /************************************************************/
  203. uart_cfg.baudrate = QL_UART_BAUD_115200;
  204. uart_cfg.flow_ctrl = QL_FC_NONE;
  205. uart_cfg.data_bit = QL_UART_DATABIT_8;
  206. uart_cfg.stop_bit = QL_UART_STOP_1;
  207. uart_cfg.parity_bit = QL_UART_PARITY_NONE;
  208. uart_ret = ql_uart_set_dcbconfig(QL_UART_PORT_2, &uart_cfg);
  209. QL_NTRIP_RTK_DEMO_LOG("ret: 0x%x", uart_ret);
  210. if(QL_UART_SUCCESS != uart_ret)
  211. {
  212. goto exit;
  213. }
  214. /***********************************************************
  215. Note start:
  216. 1. If QL_UART_PORT_1 is selected for use, there is no need to set TX and RX pin and function
  217. 2. According to the QuecOpen GPIO table, user should select the correct PIN to set function
  218. 3. CTS and RTS pins (UART2 and UART3) also need to be initialized if hardware flow control function is required
  219. ************************************************************/
  220. gpio_ret = ql_pin_set_func(QUEC_PIN_UART2_TXD, 0X01);
  221. if(QL_GPIO_SUCCESS != gpio_ret)
  222. {
  223. goto exit;
  224. }
  225. gpio_ret = ql_pin_set_func(QUEC_PIN_UART2_RXD, 0X01);
  226. if(QL_GPIO_SUCCESS != gpio_ret)
  227. {
  228. goto exit;
  229. }
  230. uart_ret = ql_uart_register_cb(QL_UART_PORT_2, ql_gnss_uart_notify_cb);
  231. uart_ret = ql_uart_open(QL_UART_PORT_2);
  232. QL_NTRIP_RTK_DEMO_LOG("ret: 0x%x", uart_ret);
  233. while(1)
  234. {
  235. if (ql_event_try_wait(&ql_event) == 0)
  236. {
  237. if(ql_event.id == 0)
  238. {
  239. continue;
  240. }
  241. switch(ql_event.id)
  242. {
  243. case QUEC_UART_RX_OVERFLOW_IND:
  244. case QUEC_UART_RX_RECV_DATA_IND:
  245. {
  246. size = ql_event.param2;
  247. while(size > 0)
  248. {
  249. memset(recv_buff, 0, QL_GNSS_UART_RX_BUFF_SIZE+1);
  250. real_size= MIN(size, QL_GNSS_UART_RX_BUFF_SIZE);
  251. read_len = ql_uart_read(ql_event.param1, recv_buff, real_size);
  252. //QL_NTRIP_RTK_DEMO_LOG("read_len=%d, recv_data=%s", read_len, recv_buff);
  253. if((read_len > 0) && (size >= read_len))
  254. {
  255. size -= read_len;
  256. }
  257. else
  258. {
  259. break;
  260. }
  261. }
  262. total_bytes += ql_event.param2;
  263. if (total_bytes > QL_GNSS_UART_RX_BUFF_SIZE-150)
  264. {
  265. QL_NTRIP_RTK_DEMO_LOG("nmea data Overflow");
  266. total_bytes = 0;
  267. memset(gnss_nmea_buff, 0x00, QL_GNSS_UART_RX_BUFF_SIZE+1);
  268. continue;
  269. }
  270. QL_NTRIP_RTK_DEMO_LOG("total_bytes=%d", total_bytes);
  271. memcpy(gnss_nmea_buff + total_bytes - ql_event.param2, recv_buff, ql_event.param2);
  272. //QL_NTRIP_RTK_DEMO_LOG("gnss_nmea_buff=%s", gnss_nmea_buff);
  273. if(ql_event.param2 > 0)
  274. {
  275. //找NMEA语句
  276. start = strstr(gnss_nmea_buff, "$GNGGA");
  277. if (NULL == start)
  278. {
  279. start = ql_gnss_get_lastest_char(gnss_nmea_buff, total_bytes, '$');
  280. if (start)
  281. {
  282. len = start - gnss_nmea_buff;
  283. //QL_NTRIP_RTK_DEMO_LOG("len = %d", len);
  284. memmove(gnss_nmea_buff, start, total_bytes - len);
  285. total_bytes = total_bytes - len;
  286. //QL_NTRIP_RTK_DEMO_LOG("total_bytes=%d", total_bytes);
  287. memset(gnss_nmea_buff+total_bytes, 0x00, QL_GNSS_UART_RX_BUFF_SIZE + 1 - total_bytes);
  288. }
  289. else
  290. {
  291. //不存在$字符
  292. total_bytes = 0;
  293. memset(gnss_nmea_buff, 0x00, QL_GNSS_UART_RX_BUFF_SIZE+1);
  294. }
  295. continue;
  296. }
  297. //找\r,\n
  298. end = memchr(start, GNSS_NMEA_END_CHAR_1, total_bytes - (start - gnss_nmea_buff ));
  299. if (end == NULL)
  300. {
  301. //有$号,但是没有\r
  302. continue;
  303. }
  304. if (GNSS_NMEA_END_CHAR_2 != *(++end))
  305. {
  306. //有$号,有\r,但是没有\n
  307. continue;
  308. }
  309. //找到对应的nmea语句
  310. memset(nmea_buff, 0x00, sizeof(nmea_buff));
  311. len = end - start - 1;
  312. memcpy(nmea_buff, start, len );
  313. QL_NTRIP_RTK_DEMO_LOG("real_nmea_buff=%s", nmea_buff);
  314. //判断是否已定位
  315. start = ql_gnss_get_typical_char((char *)nmea_buff, len, ',', 6);
  316. QL_NTRIP_RTK_DEMO_LOG("locate=%c", start[1]);
  317. if ('1' == start[1] && QL_NTRIP_RTK_IDLE == ql_ntrip_demo_state)
  318. {
  319. ql_ntrip_demo_state = QL_NTRIP_RTK_CONNECTING;
  320. ql_event.id = QL_NTRIP_RTK_APP_START_SOCKET;
  321. ql_rtos_event_send(ntrip_rtk_demo_task, &ql_event);
  322. }
  323. if (QL_NTRIP_RTK_POINTED == ql_ntrip_demo_state || QL_NTRIP_RTK_RECV_DATA == ql_ntrip_demo_state)
  324. {
  325. ql_ntrip_rtk_send(ntrip_rtk_id, (char *)nmea_buff, len);
  326. }
  327. total_bytes = 0;
  328. memset(gnss_nmea_buff, 0x00, QL_GNSS_UART_RX_BUFF_SIZE+1);
  329. }
  330. QL_NTRIP_RTK_DEMO_LOG("total_bytes=%d", total_bytes);
  331. }
  332. break;
  333. case QL_NTRIP_RTK_APP_START_SOCKET:
  334. {
  335. config.sim_id = 0;
  336. config.pdp_cid = 1;
  337. config.ip = QL_NTRIP_RTK_SERVER_IP;
  338. config.port = QL_NTRIP_RTK_SERVER_PORT;
  339. config.user = QL_NTRIP_RTK_SERVER_USER;
  340. config.pwd = QL_NTRIP_RTK_SERVER_PWD;
  341. config.point = "RTCM32";
  342. config.gnss_type = "GPGGA";
  343. config.func = ql_ntrip_rtk_handle_cb;
  344. ret = ql_ntrip_rtk_init(ntrip_rtk_id, &config);
  345. if (ret != QL_NTRIP_RTK_SUCCESS)
  346. {
  347. QL_NTRIP_RTK_DEMO_LOG("init err=%x", ret);
  348. goto exit;
  349. }
  350. }
  351. break;
  352. case QL_NTRIP_RTK_APP_REAC_DATA:
  353. {
  354. ql_ntrip_demo_state = QL_NTRIP_RTK_RECV_DATA;
  355. char *rtk_data = (char *)ql_event.param1;
  356. if (rtk_data)
  357. {
  358. ql_uart_write(QL_UART_PORT_2, (unsigned char *)rtk_data, ql_event.param2);
  359. free(rtk_data);
  360. }
  361. #if 0
  362. //断开差分服务器连接,只是为了测试ql_ntrip_rtk_close接口功能,实际应用需要屏蔽掉
  363. static int count = 0;
  364. if (count++ > 20)
  365. {
  366. count = 0;
  367. ql_ntrip_rtk_close(ntrip_rtk_id);
  368. }
  369. QL_NTRIP_RTK_DEMO_LOG("test_count=%d", count);
  370. #endif
  371. }
  372. break;
  373. case QL_NTRIP_RTK_APP_STATE_CHANGE:
  374. {
  375. ql_ntrip_demo_state = ql_event.param1;
  376. if (QL_NTRIP_RTK_IDLE == ql_ntrip_demo_state)
  377. {
  378. ql_ntrip_demo_state = QL_NTRIP_RTK_CONNECTING;
  379. ql_event.id = QL_NTRIP_RTK_APP_START_SOCKET;
  380. ql_rtos_event_send(ntrip_rtk_demo_task, &ql_event);
  381. }
  382. }
  383. break;
  384. default:
  385. break;
  386. }
  387. }
  388. }
  389. exit:
  390. ql_uart_close(QL_UART_PORT_2);
  391. if (recv_buff)
  392. {
  393. free(recv_buff);
  394. }
  395. if (gnss_nmea_buff)
  396. {
  397. free(gnss_nmea_buff);
  398. }
  399. ql_rtos_task_delete(NULL);
  400. }
  401. QlOSStatus ql_ntrip_rtk_demo_init(void)
  402. {
  403. QlOSStatus err = QL_OSI_SUCCESS;
  404. err = ql_rtos_task_create(&ntrip_rtk_demo_task, NTRIP_RTK_DEMO_TASK_STACK_SIZE,NTRIP_RTK_DEMO_TASK_PRIO, "ql_ntrip_rtk_demo", ql_ntrip_rtk_demo_pthread, NULL, NTRIP_RTK_DEMO_TASK_EVENT_CNT);
  405. if(err != QL_OSI_SUCCESS)
  406. {
  407. QL_NTRIP_RTK_DEMO_LOG("demo_task created failed");
  408. return err;
  409. }
  410. return err;
  411. }