hal-avrcp-ctrl.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. */
  6. #define _GNU_SOURCE
  7. #include <stdbool.h>
  8. #include <stddef.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "hal-utils.h"
  12. #include "hal-log.h"
  13. #include "hal.h"
  14. #include "hal-msg.h"
  15. #include "ipc-common.h"
  16. #include "hal-ipc.h"
  17. static const btrc_ctrl_callbacks_t *cbs = NULL;
  18. static bool interface_ready(void)
  19. {
  20. return cbs != NULL;
  21. }
  22. static void handle_connection_state(void *buf, uint16_t len, int fd)
  23. {
  24. struct hal_ev_avrcp_ctrl_conn_state *ev = buf;
  25. if (cbs->connection_state_cb)
  26. cbs->connection_state_cb(ev->state,
  27. (bt_bdaddr_t *) (ev->bdaddr));
  28. }
  29. static void handle_passthrough_rsp(void *buf, uint16_t len, int fd)
  30. {
  31. struct hal_ev_avrcp_ctrl_passthrough_rsp *ev = buf;
  32. if (cbs->passthrough_rsp_cb)
  33. cbs->passthrough_rsp_cb(ev->id, ev->key_state);
  34. }
  35. /*
  36. * handlers will be called from notification thread context,
  37. * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
  38. */
  39. static const struct hal_ipc_handler ev_handlers[] = {
  40. /* HAL_EV_AVRCP_CTRL_CONN_STATE */
  41. { handle_connection_state, false,
  42. sizeof(struct hal_ev_avrcp_ctrl_conn_state) },
  43. /* HAL_EV_AVRCP_CTRL_PASSTHROUGH_RSP */
  44. { handle_passthrough_rsp, false,
  45. sizeof(struct hal_ev_avrcp_ctrl_passthrough_rsp) },
  46. };
  47. static bt_status_t init(btrc_ctrl_callbacks_t *callbacks)
  48. {
  49. struct hal_cmd_register_module cmd;
  50. int ret;
  51. DBG("");
  52. if (interface_ready())
  53. return BT_STATUS_DONE;
  54. cbs = callbacks;
  55. hal_ipc_register(HAL_SERVICE_ID_AVRCP_CTRL, ev_handlers,
  56. sizeof(ev_handlers) / sizeof(ev_handlers[0]));
  57. cmd.service_id = HAL_SERVICE_ID_AVRCP_CTRL;
  58. cmd.mode = HAL_MODE_DEFAULT;
  59. cmd.max_clients = 1;
  60. ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
  61. sizeof(cmd), &cmd, NULL, NULL, NULL);
  62. if (ret != BT_STATUS_SUCCESS) {
  63. cbs = NULL;
  64. hal_ipc_unregister(HAL_SERVICE_ID_AVRCP_CTRL);
  65. }
  66. return ret;
  67. }
  68. static bt_status_t send_pass_through_cmd(bt_bdaddr_t *bd_addr, uint8_t key_code,
  69. uint8_t key_state)
  70. {
  71. struct hal_cmd_avrcp_ctrl_send_passthrough cmd;
  72. DBG("");
  73. if (!interface_ready())
  74. return BT_STATUS_NOT_READY;
  75. memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
  76. cmd.key_code = key_code;
  77. cmd.key_state = key_state;
  78. return hal_ipc_cmd(HAL_SERVICE_ID_AVRCP_CTRL,
  79. HAL_OP_AVRCP_CTRL_SEND_PASSTHROUGH,
  80. sizeof(cmd), &cmd, NULL, NULL, NULL);
  81. }
  82. static void cleanup(void)
  83. {
  84. struct hal_cmd_unregister_module cmd;
  85. DBG("");
  86. if (!interface_ready())
  87. return;
  88. cmd.service_id = HAL_SERVICE_ID_AVRCP_CTRL;
  89. hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
  90. sizeof(cmd), &cmd, NULL, NULL, NULL);
  91. hal_ipc_unregister(HAL_SERVICE_ID_AVRCP_CTRL);
  92. cbs = NULL;
  93. }
  94. static btrc_ctrl_interface_t iface = {
  95. .size = sizeof(iface),
  96. .init = init,
  97. .send_pass_through_cmd = send_pass_through_cmd,
  98. .cleanup = cleanup
  99. };
  100. btrc_ctrl_interface_t *bt_get_avrcp_ctrl_interface(void)
  101. {
  102. return &iface;
  103. }