gps_manager.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2022 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file gps manager interface
  8. */
  9. #include <os_common_api.h>
  10. #include <msg_manager.h>
  11. #include <mem_manager.h>
  12. #include <srv_manager.h>
  13. #include <string.h>
  14. #include <gps_manager.h>
  15. gps_res_t gps_res;
  16. gps_res_t *gps_manager_get_res_info(void)
  17. {
  18. return &gps_res;
  19. }
  20. struct minmea_sentence_gga *
  21. gps_manager_get_gga_info(void)
  22. {
  23. #ifdef CONFIG_GPS_PARSE_GGA_ENABLE
  24. return &gps_res.gga_data;
  25. #else
  26. return NULL;
  27. #endif
  28. }
  29. bool gps_manager_get_located(void)
  30. {
  31. return gps_res.gga_data.satellites_tracked;
  32. }
  33. uint8_t gps_manager_get_signal(void)
  34. {
  35. return gps_res.gga_data.fix_quality;
  36. }
  37. int gps_send_msg(uint32_t cmd, uint32_t len, void *ptr, uint8_t notify)
  38. {
  39. struct app_msg msg = {0};
  40. os_sem return_notify;
  41. int ret;
  42. if (notify)
  43. {
  44. os_sem_init(&return_notify, 0, 1);
  45. }
  46. msg.type = MSG_SENSOR_EVENT;
  47. msg.cmd = cmd;
  48. msg.reserve = len;
  49. msg.ptr = ptr;
  50. if (notify)
  51. {
  52. msg.sync_sem = &return_notify;
  53. }
  54. ret = send_async_msg(GPS_SERVICE_NAME, &msg);
  55. if (false == ret)
  56. {
  57. return -EBUSY;
  58. }
  59. if (notify)
  60. {
  61. if (os_sem_take(&return_notify, OS_FOREVER))
  62. {
  63. return -ETIME;
  64. }
  65. }
  66. return 0;
  67. }
  68. static int _gps_service_start(void)
  69. {
  70. struct app_msg msg = {0};
  71. if (!srv_manager_check_service_is_actived(GPS_SERVICE_NAME))
  72. {
  73. SYS_LOG_INF("gps service not actived\n");
  74. if (srv_manager_active_service(GPS_SERVICE_NAME))
  75. {
  76. SYS_LOG_INF("gps service start ok");
  77. }
  78. else
  79. {
  80. SYS_LOG_ERR("gps service start failed");
  81. return -ESRCH;
  82. }
  83. }
  84. else
  85. {
  86. SYS_LOG_ERR("gps service is actived\n");
  87. }
  88. msg.type = MSG_INIT_APP;
  89. int ret = send_async_msg(GPS_SERVICE_NAME, &msg);
  90. SYS_LOG_INF("_gps_service_start, send_async_msg MSG_INIT_APP:%d\n", ret);
  91. return !ret;
  92. }
  93. int gps_manager_init(void)
  94. {
  95. SYS_LOG_INF("gps_manager_init");
  96. _gps_service_start();
  97. return 0;
  98. }
  99. int gps_manager_enable(uint32_t id, uint32_t func)
  100. {
  101. SYS_LOG_INF("gps_manager_enable");
  102. /*
  103. SYS_LOG_INF("gps_manager_enable MSG_INIT_APP");
  104. struct app_msg msg = {0};
  105. msg.type = MSG_INIT_APP;
  106. send_async_msg(GPS_SERVICE_NAME, &msg);
  107. */
  108. return gps_send_msg(MSG_GPS_ENABLE, id, (void *)func, 0);
  109. }
  110. int gps_manager_disable(uint32_t id, uint32_t func)
  111. {
  112. SYS_LOG_INF("gps_manager_disable");
  113. return gps_send_msg(MSG_GPS_DISABLE, id, (void *)func, 0);
  114. }
  115. int gps_manager_add_callback(gps_res_cb_t cb)
  116. {
  117. SYS_LOG_INF("gps_manager_add_callback");
  118. return gps_send_msg(MSG_GPS_ADD_CB, 0, cb, 0);
  119. }
  120. int gps_manager_remove_callback(gps_res_cb_t cb)
  121. {
  122. SYS_LOG_INF("gps_manager_remove_callback");
  123. return gps_send_msg(MSG_GPS_REMOVE_CB, 0, cb, 0);
  124. }