hal-health.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. */
  6. #include <stdbool.h>
  7. #include <stddef.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include "hal-log.h"
  14. #include "hal.h"
  15. #include "hal-msg.h"
  16. #include "ipc-common.h"
  17. #include "hal-ipc.h"
  18. static const bthl_callbacks_t *cbacks = NULL;
  19. static bool interface_ready(void)
  20. {
  21. return cbacks != NULL;
  22. }
  23. static void handle_app_registration_state(void *buf, uint16_t len, int fd)
  24. {
  25. struct hal_ev_health_app_reg_state *ev = buf;
  26. if (cbacks->app_reg_state_cb)
  27. cbacks->app_reg_state_cb(ev->id, ev->state);
  28. }
  29. static void handle_channel_state(void *buf, uint16_t len, int fd)
  30. {
  31. struct hal_ev_health_channel_state *ev = buf;
  32. int flags;
  33. if (fd < 0)
  34. goto end;
  35. flags = fcntl(fd, F_GETFL, 0);
  36. if (flags < 0) {
  37. error("health: fcntl GETFL error: %s", strerror(errno));
  38. return;
  39. }
  40. /* Clean O_NONBLOCK fd flag as Android Java layer expects */
  41. if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) {
  42. error("health: fcntl SETFL error: %s", strerror(errno));
  43. return;
  44. }
  45. end:
  46. if (cbacks->channel_state_cb)
  47. cbacks->channel_state_cb(ev->app_id, (bt_bdaddr_t *) ev->bdaddr,
  48. ev->mdep_index, ev->channel_id,
  49. ev->channel_state, fd);
  50. }
  51. /*
  52. * handlers will be called from notification thread context,
  53. * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
  54. */
  55. static const struct hal_ipc_handler ev_handlers[] = {
  56. /* HAL_EV_HEALTH_APP_REG_STATE */
  57. { handle_app_registration_state, false,
  58. sizeof(struct hal_ev_health_app_reg_state) },
  59. /* HAL_EV_HEALTH_CHANNEL_STATE */
  60. { handle_channel_state, false,
  61. sizeof(struct hal_ev_health_channel_state) },
  62. };
  63. static bt_status_t register_application(bthl_reg_param_t *reg, int *app_id)
  64. {
  65. uint8_t buf[IPC_MTU];
  66. struct hal_cmd_health_reg_app *cmd = (void *) buf;
  67. struct hal_rsp_health_reg_app rsp;
  68. size_t rsp_len = sizeof(rsp);
  69. bt_status_t status;
  70. uint16_t off, len;
  71. int i;
  72. DBG("");
  73. if (!interface_ready())
  74. return BT_STATUS_NOT_READY;
  75. if (!reg || !app_id || !reg->application_name)
  76. return BT_STATUS_PARM_INVALID;
  77. *app_id = -1;
  78. memset(buf, 0, IPC_MTU);
  79. cmd->num_of_mdep = reg->number_of_mdeps;
  80. off = 0;
  81. cmd->app_name_off = off;
  82. len = strlen(reg->application_name) + 1;
  83. memcpy(cmd->data, reg->application_name, len);
  84. off += len;
  85. cmd->provider_name_off = off;
  86. if (reg->provider_name) {
  87. len = strlen(reg->provider_name) + 1;
  88. memcpy(cmd->data + off, reg->provider_name, len);
  89. off += len;
  90. }
  91. cmd->service_name_off = off;
  92. if (reg->srv_name) {
  93. len = strlen(reg->srv_name) + 1;
  94. memcpy(cmd->data + off, reg->srv_name, len);
  95. off += len;
  96. }
  97. cmd->service_descr_off = off;
  98. if (reg->srv_desp) {
  99. len = strlen(reg->srv_desp) + 1;
  100. memcpy(cmd->data + off, reg->srv_desp, len);
  101. off += len;
  102. }
  103. cmd->len = off;
  104. status = hal_ipc_cmd(HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_REG_APP,
  105. sizeof(*cmd) + cmd->len, buf,
  106. &rsp_len, &rsp, NULL);
  107. if (status != BT_STATUS_SUCCESS)
  108. return status;
  109. for (i = 0; i < reg->number_of_mdeps; i++) {
  110. struct hal_cmd_health_mdep *mdep = (void *) buf;
  111. memset(buf, 0, IPC_MTU);
  112. mdep->app_id = rsp.app_id;
  113. mdep->role = reg->mdep_cfg[i].mdep_role;
  114. mdep->data_type = reg->mdep_cfg[i].data_type;
  115. mdep->channel_type = reg->mdep_cfg[i].channel_type;
  116. if (reg->mdep_cfg[i].mdep_description) {
  117. mdep->descr_len =
  118. strlen(reg->mdep_cfg[i].mdep_description) + 1;
  119. memcpy(mdep->descr, reg->mdep_cfg[i].mdep_description,
  120. mdep->descr_len);
  121. }
  122. status = hal_ipc_cmd(HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
  123. sizeof(*mdep) + mdep->descr_len,
  124. buf, NULL, NULL, NULL);
  125. if (status != BT_STATUS_SUCCESS)
  126. return status;
  127. }
  128. *app_id = rsp.app_id;
  129. return status;
  130. }
  131. static bt_status_t unregister_application(int app_id)
  132. {
  133. struct hal_cmd_health_unreg_app cmd;
  134. DBG("");
  135. if (!interface_ready())
  136. return BT_STATUS_NOT_READY;
  137. cmd.app_id = app_id;
  138. return hal_ipc_cmd(HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_UNREG_APP,
  139. sizeof(cmd), &cmd, NULL, NULL, NULL);
  140. }
  141. static bt_status_t connect_channel(int app_id, bt_bdaddr_t *bd_addr,
  142. int mdep_cfg_index, int *channel_id)
  143. {
  144. struct hal_cmd_health_connect_channel cmd;
  145. struct hal_rsp_health_connect_channel rsp;
  146. size_t len = sizeof(rsp);
  147. bt_status_t status;
  148. DBG("");
  149. if (!interface_ready())
  150. return BT_STATUS_NOT_READY;
  151. if (!bd_addr || !channel_id)
  152. return BT_STATUS_PARM_INVALID;
  153. *channel_id = -1;
  154. cmd.app_id = app_id;
  155. cmd.mdep_index = mdep_cfg_index;
  156. memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
  157. status = hal_ipc_cmd(HAL_SERVICE_ID_HEALTH,
  158. HAL_OP_HEALTH_CONNECT_CHANNEL,
  159. sizeof(cmd), &cmd, &len, &rsp, NULL);
  160. if (status == BT_STATUS_SUCCESS)
  161. *channel_id = rsp.channel_id;
  162. return status;
  163. }
  164. static bt_status_t destroy_channel(int channel_id)
  165. {
  166. struct hal_cmd_health_destroy_channel cmd;
  167. DBG("");
  168. if (!interface_ready())
  169. return BT_STATUS_NOT_READY;
  170. cmd.channel_id = channel_id;
  171. return hal_ipc_cmd(HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_DESTROY_CHANNEL,
  172. sizeof(cmd), &cmd, NULL, NULL, NULL);
  173. }
  174. static bt_status_t init(bthl_callbacks_t *callbacks)
  175. {
  176. struct hal_cmd_register_module cmd;
  177. int ret;
  178. DBG("");
  179. if (interface_ready())
  180. return BT_STATUS_DONE;
  181. /* store reference to user callbacks */
  182. cbacks = callbacks;
  183. hal_ipc_register(HAL_SERVICE_ID_HEALTH, ev_handlers,
  184. sizeof(ev_handlers)/sizeof(ev_handlers[0]));
  185. cmd.service_id = HAL_SERVICE_ID_HEALTH;
  186. cmd.mode = HAL_MODE_DEFAULT;
  187. cmd.max_clients = 1;
  188. ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
  189. sizeof(cmd), &cmd, NULL, NULL, NULL);
  190. if (ret != BT_STATUS_SUCCESS) {
  191. cbacks = NULL;
  192. hal_ipc_unregister(HAL_SERVICE_ID_HEALTH);
  193. }
  194. return ret;
  195. }
  196. static void cleanup(void)
  197. {
  198. struct hal_cmd_unregister_module cmd;
  199. DBG("");
  200. if (!interface_ready())
  201. return;
  202. cmd.service_id = HAL_SERVICE_ID_HEALTH;
  203. hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
  204. sizeof(cmd), &cmd, NULL, NULL, NULL);
  205. hal_ipc_unregister(HAL_SERVICE_ID_HEALTH);
  206. cbacks = NULL;
  207. }
  208. static bthl_interface_t health_if = {
  209. .size = sizeof(health_if),
  210. .init = init,
  211. .register_application = register_application,
  212. .unregister_application = unregister_application,
  213. .connect_channel = connect_channel,
  214. .destroy_channel = destroy_channel,
  215. .cleanup = cleanup
  216. };
  217. bthl_interface_t *bt_get_health_interface(void)
  218. {
  219. return &health_if;
  220. }