hal-map-client.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. */
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include <string.h>
  9. #include "hal-log.h"
  10. #include "hal.h"
  11. #include "hal-msg.h"
  12. #include "hal-ipc.h"
  13. static const btmce_callbacks_t *cbs = NULL;
  14. static bool interface_ready(void)
  15. {
  16. return cbs != NULL;
  17. }
  18. /* Event Handlers */
  19. static void remote_mas_instances_to_hal(btmce_mas_instance_t *send_instance,
  20. struct hal_map_client_mas_instance *instance,
  21. int num_instances, uint16_t len)
  22. {
  23. void *buf = instance;
  24. char *name;
  25. int i;
  26. DBG("");
  27. for (i = 0; i < num_instances; i++) {
  28. name = (char *) instance->name;
  29. if (sizeof(*instance) + instance->name_len > len ||
  30. (instance->name_len != 0 &&
  31. name[instance->name_len - 1] != '\0')) {
  32. error("invalid remote mas instance %d, aborting", i);
  33. exit(EXIT_FAILURE);
  34. }
  35. send_instance[i].id = instance->id;
  36. send_instance[i].msg_types = instance->msg_types;
  37. send_instance[i].scn = instance->scn;
  38. send_instance[i].p_name = name;
  39. len -= sizeof(*instance) + instance->name_len;
  40. buf += sizeof(*instance) + instance->name_len;
  41. instance = buf;
  42. }
  43. if (!len)
  44. return;
  45. error("invalid remote mas instances (%u bytes left), aborting", len);
  46. exit(EXIT_FAILURE);
  47. }
  48. static void handle_remote_mas_instances(void *buf, uint16_t len, int fd)
  49. {
  50. struct hal_ev_map_client_remote_mas_instances *ev = buf;
  51. btmce_mas_instance_t instances[ev->num_instances];
  52. DBG("");
  53. len -= sizeof(*ev);
  54. remote_mas_instances_to_hal(instances, ev->instances, ev->num_instances,
  55. len);
  56. if (cbs->remote_mas_instances_cb)
  57. cbs->remote_mas_instances_cb(ev->status,
  58. (bt_bdaddr_t *) ev->bdaddr,
  59. ev->num_instances, instances);
  60. }
  61. /*
  62. * handlers will be called from notification thread context,
  63. * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
  64. */
  65. static const struct hal_ipc_handler ev_handlers[] = {
  66. /* HAL_EV_MCE_REMOTE_MAS_INSTANCES */
  67. { handle_remote_mas_instances, true,
  68. sizeof(struct hal_ev_map_client_remote_mas_instances) }
  69. };
  70. /* API */
  71. static bt_status_t get_remote_mas_instances(bt_bdaddr_t *bd_addr)
  72. {
  73. struct hal_cmd_map_client_get_instances cmd;
  74. if (!interface_ready())
  75. return BT_STATUS_NOT_READY;
  76. memcpy(cmd.bdaddr, bd_addr, sizeof(*bd_addr));
  77. return hal_ipc_cmd(HAL_SERVICE_ID_MAP_CLIENT,
  78. HAL_OP_MAP_CLIENT_GET_INSTANCES, sizeof(cmd),
  79. &cmd, NULL, NULL, NULL);
  80. }
  81. static bt_status_t init(btmce_callbacks_t *callbacks)
  82. {
  83. struct hal_cmd_register_module cmd;
  84. int ret;
  85. DBG("");
  86. /*
  87. * Interface ready check was removed because there is no cleanup
  88. * function to unregister and clear callbacks. MAP client testers may
  89. * restart bluetooth, unregister this profile and try to reuse it.
  90. * This situation make service unregistered but callbacks are still
  91. * set - interface is ready. On android devices there is no need to
  92. * re-init MAP client profile while bluetooth is loaded.
  93. */
  94. cbs = callbacks;
  95. hal_ipc_register(HAL_SERVICE_ID_MAP_CLIENT, ev_handlers,
  96. sizeof(ev_handlers)/sizeof(ev_handlers[0]));
  97. cmd.service_id = HAL_SERVICE_ID_MAP_CLIENT;
  98. cmd.mode = HAL_MODE_DEFAULT;
  99. cmd.max_clients = 1;
  100. ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
  101. sizeof(cmd), &cmd, 0, NULL, NULL);
  102. if (ret != BT_STATUS_SUCCESS) {
  103. cbs = NULL;
  104. hal_ipc_unregister(HAL_SERVICE_ID_MAP_CLIENT);
  105. }
  106. return ret;
  107. }
  108. static btmce_interface_t iface = {
  109. .size = sizeof(iface),
  110. .init = init,
  111. .get_remote_mas_instances = get_remote_mas_instances
  112. };
  113. btmce_interface_t *bt_get_map_client_interface(void)
  114. {
  115. return &iface;
  116. }