2 * Copyright (C) 2014 Intel Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
27 static const btmce_callbacks_t *cbs = NULL;
29 static bool interface_ready(void)
36 static void remote_mas_instances_to_hal(btmce_mas_instance_t *send_instance,
37 struct hal_map_client_mas_instance *instance,
38 int num_instances, uint16_t len)
46 for (i = 0; i < num_instances; i++) {
47 name = (char *) instance->name;
48 if (sizeof(*instance) + instance->name_len > len ||
49 (instance->name_len != 0 &&
50 name[instance->name_len - 1] != '\0')) {
51 error("invalid remote mas instance %d, aborting", i);
55 send_instance[i].id = instance->id;
56 send_instance[i].msg_types = instance->msg_types;
57 send_instance[i].scn = instance->scn;
58 send_instance[i].p_name = name;
60 len -= sizeof(*instance) + instance->name_len;
61 buf += sizeof(*instance) + instance->name_len;
68 error("invalid remote mas instances (%u bytes left), aborting", len);
72 static void handle_remote_mas_instances(void *buf, uint16_t len, int fd)
74 struct hal_ev_map_client_remote_mas_instances *ev = buf;
75 btmce_mas_instance_t instances[ev->num_instances];
80 remote_mas_instances_to_hal(instances, ev->instances, ev->num_instances,
83 if (cbs->remote_mas_instances_cb)
84 cbs->remote_mas_instances_cb(ev->status,
85 (bt_bdaddr_t *) ev->bdaddr,
86 ev->num_instances, instances);
90 * handlers will be called from notification thread context,
91 * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
93 static const struct hal_ipc_handler ev_handlers[] = {
94 /* HAL_EV_MCE_REMOTE_MAS_INSTANCES */
95 { handle_remote_mas_instances, true,
96 sizeof(struct hal_ev_map_client_remote_mas_instances) }
101 static bt_status_t get_remote_mas_instances(bt_bdaddr_t *bd_addr)
103 struct hal_cmd_map_client_get_instances cmd;
105 if (!interface_ready())
106 return BT_STATUS_NOT_READY;
108 memcpy(cmd.bdaddr, bd_addr, sizeof(*bd_addr));
110 return hal_ipc_cmd(HAL_SERVICE_ID_MAP_CLIENT,
111 HAL_OP_MAP_CLIENT_GET_INSTANCES, sizeof(cmd),
112 &cmd, NULL, NULL, NULL);
115 static bt_status_t init(btmce_callbacks_t *callbacks)
117 struct hal_cmd_register_module cmd;
123 * Interface ready check was removed because there is no cleanup
124 * function to unregister and clear callbacks. MAP client testers may
125 * restart bluetooth, unregister this profile and try to reuse it.
126 * This situation make service unregistered but callbacks are still
127 * set - interface is ready. On android devices there is no need to
128 * re-init MAP client profile while bluetooth is loaded.
133 hal_ipc_register(HAL_SERVICE_ID_MAP_CLIENT, ev_handlers,
134 sizeof(ev_handlers)/sizeof(ev_handlers[0]));
136 cmd.service_id = HAL_SERVICE_ID_MAP_CLIENT;
137 cmd.mode = HAL_MODE_DEFAULT;
140 ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
141 sizeof(cmd), &cmd, 0, NULL, NULL);
143 if (ret != BT_STATUS_SUCCESS) {
145 hal_ipc_unregister(HAL_SERVICE_ID_MAP_CLIENT);
151 static btmce_interface_t iface = {
152 .size = sizeof(iface),
154 .get_remote_mas_instances = get_remote_mas_instances
157 btmce_interface_t *bt_get_map_client_interface(void)