sensor_manager.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file sensor manager interface
  8. */
  9. #define SYS_LOG_NO_NEWLINE
  10. #ifdef SYS_LOG_DOMAIN
  11. #undef SYS_LOG_DOMAIN
  12. #endif
  13. #define SYS_LOG_DOMAIN "sensor_manager"
  14. #include <os_common_api.h>
  15. #include <msg_manager.h>
  16. #include <mem_manager.h>
  17. #include <srv_manager.h>
  18. #include <string.h>
  19. #include <sensor_manager.h>
  20. int sensor_send_msg(uint32_t cmd, uint32_t len, void *ptr, uint8_t notify)
  21. {
  22. struct app_msg msg = {0};
  23. os_sem return_notify;
  24. int ret;
  25. if (notify) {
  26. os_sem_init(&return_notify, 0, 1);
  27. }
  28. msg.type = MSG_SENSOR_EVENT;
  29. msg.cmd = cmd;
  30. msg.reserve = len;
  31. msg.ptr = ptr;
  32. if (notify) {
  33. msg.sync_sem = &return_notify;
  34. }
  35. ret = send_async_msg_discardable(SENSOR_SERVICE_NAME, &msg);
  36. if (false == ret) {
  37. return -EBUSY;
  38. }
  39. if (notify) {
  40. if (os_sem_take(&return_notify, OS_FOREVER)) {
  41. return -ETIME;
  42. }
  43. }
  44. return 0;
  45. }
  46. static int _sensor_service_start(void)
  47. {
  48. struct app_msg msg = {0};
  49. if (!srv_manager_check_service_is_actived(SENSOR_SERVICE_NAME)) {
  50. if (srv_manager_active_service(SENSOR_SERVICE_NAME)) {
  51. SYS_LOG_DBG("sensor service start ok");
  52. } else {
  53. SYS_LOG_ERR("sensor service start failed");
  54. return -ESRCH;
  55. }
  56. }
  57. msg.type = MSG_INIT_APP;
  58. return !send_async_msg(SENSOR_SERVICE_NAME, &msg);
  59. }
  60. static int _sensor_service_stop(void)
  61. {
  62. int ret = 0;
  63. if (!srv_manager_check_service_is_actived(SENSOR_SERVICE_NAME)) {
  64. SYS_LOG_ERR("sensor service_stop failed");
  65. ret = -ESRCH;
  66. goto exit;
  67. }
  68. if (!srv_manager_exit_service(SENSOR_SERVICE_NAME)) {
  69. ret = -ETIMEDOUT;
  70. goto exit;
  71. }
  72. SYS_LOG_DBG("sensor service_stop success!");
  73. exit:
  74. return ret;
  75. }
  76. int sensor_manager_enable(uint32_t id, uint32_t func)
  77. {
  78. return sensor_send_msg(MSG_SENSOR_ENABLE, id, (void*)func, 0);
  79. }
  80. int sensor_manager_disable(uint32_t id, uint32_t func)
  81. {
  82. return sensor_send_msg(MSG_SENSOR_DISABLE, id, (void*)func, 0);
  83. }
  84. int sensor_manager_add_callback(sensor_res_cb_t cb)
  85. {
  86. return sensor_send_msg(MSG_SENSOR_ADD_CB, 0, cb, 0);
  87. }
  88. int sensor_manager_remove_callback(sensor_res_cb_t cb)
  89. {
  90. return sensor_send_msg(MSG_SENSOR_REMOVE_CB, 0, cb, 0);
  91. }
  92. int sensor_manager_get_result(sensor_res_t *res)
  93. {
  94. return sensor_send_msg(MSG_SENSOR_GET_RESULT, sizeof(sensor_res_t), res, 1);
  95. }
  96. int sensor_manager_init(void)
  97. {
  98. _sensor_service_start();
  99. return 0;
  100. }
  101. int sensor_manager_exit(void)
  102. {
  103. _sensor_service_stop();
  104. return 0;
  105. }