luat_gnss.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "luat_base.h"
  2. #include "luat_msgbus.h"
  3. #include "luat_malloc.h"
  4. #include "luat_uart.h"
  5. #include "luat_rtc.h"
  6. #include "luat_mcu.h"
  7. #define LUAT_LOG_TAG "gnss"
  8. #include "luat_log.h"
  9. #include "minmea.h"
  10. luat_libgnss_t *libgnss_gnss;
  11. luat_libgnss_tmp_t *libgnss_gnsstmp;
  12. char *libgnss_recvbuff;
  13. int libgnss_route_uart_id = -1;
  14. int gnss_debug = 0;
  15. // static int parse_nmea(const char* line);
  16. // static int parse_data(const char* data, size_t len);
  17. void luat_libgnss_uart_recv_cb(int uart_id, uint32_t data_len) {
  18. (void)data_len;
  19. if (libgnss_recvbuff == NULL)
  20. return;
  21. //LLOGD("uart recv cb");
  22. int len = 0;
  23. while (1) {
  24. len = luat_uart_read(uart_id, libgnss_recvbuff, RECV_BUFF_SIZE - 1);
  25. if (len < 1 || len >= RECV_BUFF_SIZE)
  26. break;
  27. if (libgnss_route_uart_id > 0) {
  28. luat_uart_write(libgnss_route_uart_id, libgnss_recvbuff, len);
  29. }
  30. luat_libgnss_on_rawdata(libgnss_recvbuff, len);
  31. libgnss_recvbuff[len] = 0;
  32. if (libgnss_gnss == NULL)
  33. continue;
  34. // LLOGD("uart recv %d %d", len, gnss_debug);
  35. if (gnss_debug) {
  36. LLOGD(">> %s", libgnss_recvbuff);
  37. }
  38. luat_libgnss_parse_data(libgnss_recvbuff, len);
  39. }
  40. }
  41. static uint32_t msg_counter[MINMEA_SENTENCE_MAX_ID];
  42. int luat_libgnss_init(void) {
  43. if (libgnss_gnss)
  44. return 0;
  45. libgnss_gnss = luat_heap_malloc(sizeof(luat_libgnss_t));
  46. if (libgnss_gnss == NULL) {
  47. LLOGW("out of memory for libgnss data parse");
  48. return -1;
  49. }
  50. libgnss_gnsstmp = luat_heap_malloc(sizeof(luat_libgnss_tmp_t));
  51. if (libgnss_gnsstmp == NULL) {
  52. luat_heap_free(libgnss_gnss);
  53. LLOGW("out of memory for libgnss data parse");
  54. return -1;
  55. }
  56. // gnss->lua_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  57. memset(libgnss_gnss, 0, sizeof(luat_libgnss_t));
  58. memset(libgnss_gnsstmp, 0, sizeof(luat_libgnss_tmp_t));
  59. //lua_pushboolean(L, 1);
  60. return 0;
  61. }
  62. int luat_libgnss_parse_data(const char* data, size_t len) {
  63. size_t prev = 0;
  64. #define MAX_LEN (120)
  65. static char nmea_tmp_buff[MAX_LEN] = {0}; // nmea 最大长度82,含换行符
  66. // if (data[0] == 0xAA && data[1] == 0xF0) {
  67. // LLOGD("EPH data resp?");
  68. // }
  69. for (size_t offset = 0; offset < len; offset++)
  70. {
  71. // \r == 0x0D \n == 0x0A
  72. if (data[offset] == 0x0A) {
  73. // 最短也需要是 OK\r\n
  74. // 应该\r\n的
  75. // 太长了
  76. if (offset - prev < 3 || data[offset - 1] != 0x0D || offset - prev > 82) {
  77. prev = offset + 1;
  78. continue;
  79. }
  80. if (offset - prev - 1 < MAX_LEN) {
  81. memcpy(nmea_tmp_buff, data + prev, offset - prev - 1);
  82. nmea_tmp_buff[offset - prev - 1] = 0x00;
  83. // if (gnss_debug) {
  84. // LLOGD(">> %s", nmea_tmp_buff);
  85. // }
  86. luat_libgnss_parse_nmea((const char*)nmea_tmp_buff);
  87. }
  88. else {
  89. // 超长了, 忽略
  90. }
  91. prev = offset + 1;
  92. }
  93. }
  94. return 0;
  95. }
  96. int luat_libgnss_parse_nmea(const char* line) {
  97. if (libgnss_gnss == NULL && luat_libgnss_init()) {
  98. return 0;
  99. }
  100. struct minmea_sentence_gsv *frame_gsv = &libgnss_gnsstmp->frame_gsv;
  101. enum minmea_sentence_id id = minmea_sentence_id(line, false);
  102. if (id == MINMEA_UNKNOWN || id >= MINMEA_SENTENCE_MAX_ID || id == MINMEA_INVALID)
  103. return -1;
  104. msg_counter[id] ++;
  105. // int ticks = 0;
  106. struct tm tblock = {0};
  107. int ticks = luat_mcu_ticks();
  108. switch (id) {
  109. case MINMEA_SENTENCE_RMC: {
  110. if (minmea_parse_rmc(&(libgnss_gnsstmp->frame_rmc), line)) {
  111. // 清空gsa
  112. memset(libgnss_gnss->frame_gsa, 0, sizeof(struct minmea_sentence_gsa) * FRAME_GSA_MAX);
  113. if (libgnss_gnsstmp->frame_rmc.valid) {
  114. memcpy(&(libgnss_gnss->frame_rmc), &libgnss_gnsstmp->frame_rmc, sizeof(struct minmea_sentence_rmc));
  115. #ifdef LUAT_USE_MCU
  116. if (libgnss_gnss->rtc_auto && (libgnss_gnss->fix_at_ticks == 0 || ((uint32_t)(ticks - libgnss_gnss->fix_at_ticks)) > 600*1000)) {
  117. LLOGI("Auto-Set RTC by GNSS RMC");
  118. minmea_getdatetime(&tblock, &libgnss_gnss->frame_rmc.date, &libgnss_gnss->frame_rmc.time);
  119. tblock.tm_year -= 1900;
  120. luat_rtc_set(&tblock);
  121. // luat_rtc_set_tamp32(mktime(&tblock));
  122. }
  123. if (libgnss_gnss->fix_at_ticks == 0) {
  124. LLOGI("Fixed %d %d", libgnss_gnss->frame_rmc.latitude.value, libgnss_gnss->frame_rmc.longitude.value);
  125. // 发布系统消息
  126. luat_libgnss_state_onchanged(GNSS_STATE_FIXED);
  127. }
  128. libgnss_gnss->fix_at_ticks = luat_mcu_ticks();
  129. #endif
  130. }
  131. else {
  132. if (libgnss_gnss->fix_at_ticks && libgnss_gnss->frame_rmc.valid) {
  133. LLOGI("Lose"); // 发布系统消息
  134. luat_libgnss_state_onchanged(GNSS_STATE_LOSE);
  135. }
  136. libgnss_gnss->fix_at_ticks = 0;
  137. libgnss_gnss->frame_rmc.valid = 0;
  138. if (libgnss_gnsstmp->frame_rmc.date.year > 0) {
  139. memcpy(&(libgnss_gnss->frame_rmc.date), &(libgnss_gnsstmp->frame_rmc.date), sizeof(struct minmea_date));
  140. }
  141. if (libgnss_gnsstmp->frame_rmc.time.hours > 0) {
  142. memcpy(&(libgnss_gnss->frame_rmc.time), &(libgnss_gnsstmp->frame_rmc.time), sizeof(struct minmea_time));
  143. }
  144. }
  145. }
  146. } break;
  147. case MINMEA_SENTENCE_GGA: {
  148. memcpy(libgnss_gnss->gga, line, strlen(line) + 1);
  149. } break;
  150. case MINMEA_SENTENCE_GSA: {
  151. //LLOGD("GSV %s", line);
  152. if (minmea_parse_gsa(&(libgnss_gnsstmp->frame_gsa), line)) {
  153. for (size_t i = 0; i < FRAME_GSA_MAX; i++)
  154. {
  155. // 如果是mode=0,代表空的
  156. if (libgnss_gnss->frame_gsa[i].mode == 0) {
  157. memcpy(&libgnss_gnss->frame_gsa[i], &(libgnss_gnsstmp->frame_gsa), sizeof(struct minmea_sentence_gsa));
  158. break;
  159. }
  160. }
  161. }
  162. } break;
  163. case MINMEA_SENTENCE_GLL: {
  164. memcpy(libgnss_gnss->gll, line, strlen(line) + 1);
  165. } break;
  166. case MINMEA_SENTENCE_GST: {
  167. memcpy(libgnss_gnss->gst, line, strlen(line) + 1);
  168. } break;
  169. case MINMEA_SENTENCE_GSV: {
  170. //LLOGD("Got GSV : %s", line);
  171. if (minmea_parse_gsv(frame_gsv, line)) {
  172. struct minmea_sentence_gsv *gsvs = libgnss_gnss->frame_gsv_gp;
  173. if (0 == memcmp("$GPGSV", line, strlen("$GPGSV"))) {
  174. gsvs = libgnss_gnss->frame_gsv_gp;
  175. }
  176. else if (0 == memcmp("$GBGSV", line, strlen("$GBGSV"))) {
  177. gsvs = libgnss_gnss->frame_gsv_gb;
  178. }
  179. // else if (0 == memcmp("$GLGSV", line, strlen("$GLGSV"))) {
  180. // gsvs = libgnss_gnss->frame_gsv_gl;
  181. // }
  182. // else if (0 == memcmp("$GAGSV", line, strlen("$GAGSV"))) {
  183. // gsvs = libgnss_gnss->frame_gsv_ga;
  184. // }
  185. else {
  186. break;
  187. }
  188. //LLOGD("$GSV: message %d of %d", frame_gsv.msg_nr, frame_gsv.total_msgs);
  189. if (frame_gsv->msg_nr == 1) {
  190. //LLOGD("Clean GSV");
  191. memset(gsvs, 0, sizeof(struct minmea_sentence_gsv) * FRAME_GSV_MAX);
  192. }
  193. if (frame_gsv->msg_nr >= 1 && frame_gsv->msg_nr <= FRAME_GSV_MAX) {
  194. //LLOGD("memcpy GSV %d", frame_gsv.msg_nr);
  195. memcpy(&gsvs[frame_gsv->msg_nr - 1], frame_gsv, sizeof(struct minmea_sentence_gsv));
  196. }
  197. }
  198. else {
  199. //LLOGD("bad GSV %s", line);
  200. }
  201. } break;
  202. case MINMEA_SENTENCE_VTG: {
  203. memcpy(libgnss_gnss->vtg, line, strlen(line) + 1);
  204. } break;
  205. case MINMEA_SENTENCE_ZDA: {
  206. memcpy(libgnss_gnss->zda, line, strlen(line) + 1);
  207. } break;
  208. default:
  209. //LLOGD("why happen");
  210. break;
  211. }
  212. return 0;
  213. }