Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / android / hal-map-client.c
1 /*
2  * Copyright (C) 2014 Intel Corporation
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <stdbool.h>
20 #include <string.h>
21
22 #include "hal-log.h"
23 #include "hal.h"
24 #include "hal-msg.h"
25 #include "hal-ipc.h"
26
27 static const btmce_callbacks_t *cbs = NULL;
28
29 static bool interface_ready(void)
30 {
31         return cbs != NULL;
32 }
33
34 /* Event Handlers */
35
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)
39 {
40         void *buf = instance;
41         char *name;
42         int i;
43
44         DBG("");
45
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);
52                         exit(EXIT_FAILURE);
53                 }
54
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;
59
60                 len -= sizeof(*instance) + instance->name_len;
61                 buf += sizeof(*instance) + instance->name_len;
62                 instance = buf;
63         }
64
65         if (!len)
66                 return;
67
68         error("invalid remote mas instances (%u bytes left), aborting", len);
69         exit(EXIT_FAILURE);
70 }
71
72 static void handle_remote_mas_instances(void *buf, uint16_t len, int fd)
73 {
74         struct hal_ev_map_client_remote_mas_instances *ev = buf;
75         btmce_mas_instance_t instances[ev->num_instances];
76
77         DBG("");
78
79         len -= sizeof(*ev);
80         remote_mas_instances_to_hal(instances, ev->instances, ev->num_instances,
81                                                                         len);
82
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);
87 }
88
89 /*
90  * handlers will be called from notification thread context,
91  * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
92  */
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) }
97 };
98
99 /* API */
100
101 static bt_status_t get_remote_mas_instances(bt_bdaddr_t *bd_addr)
102 {
103         struct hal_cmd_map_client_get_instances cmd;
104
105         if (!interface_ready())
106                 return BT_STATUS_NOT_READY;
107
108         memcpy(cmd.bdaddr, bd_addr, sizeof(*bd_addr));
109
110         return hal_ipc_cmd(HAL_SERVICE_ID_MAP_CLIENT,
111                                 HAL_OP_MAP_CLIENT_GET_INSTANCES, sizeof(cmd),
112                                 &cmd, NULL, NULL, NULL);
113 }
114
115 static bt_status_t init(btmce_callbacks_t *callbacks)
116 {
117         struct hal_cmd_register_module cmd;
118         int ret;
119
120         DBG("");
121
122         /*
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.
129          */
130
131         cbs = callbacks;
132
133         hal_ipc_register(HAL_SERVICE_ID_MAP_CLIENT, ev_handlers,
134                                 sizeof(ev_handlers)/sizeof(ev_handlers[0]));
135
136         cmd.service_id = HAL_SERVICE_ID_MAP_CLIENT;
137         cmd.mode = HAL_MODE_DEFAULT;
138         cmd.max_clients = 1;
139
140         ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
141                                         sizeof(cmd), &cmd, 0, NULL, NULL);
142
143         if (ret != BT_STATUS_SUCCESS) {
144                 cbs = NULL;
145                 hal_ipc_unregister(HAL_SERVICE_ID_MAP_CLIENT);
146         }
147
148         return ret;
149 }
150
151 static btmce_interface_t iface = {
152         .size = sizeof(iface),
153         .init = init,
154         .get_remote_mas_instances = get_remote_mas_instances
155 };
156
157 btmce_interface_t *bt_get_map_client_interface(void)
158 {
159         return &iface;
160 }