4 * Copyright (c) 2015 -2016 Samsung Electronics Co., Ltd All Rights Reserved.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
29 #include "bt-hal-log.h"
30 #include "bt-hal-msg.h"
31 #include "bt-hal-utils.h"
33 #include <bt-hal-adapter-dbus-handler.h>
35 #define enum_prop_to_hal(prop, hal_prop, type) do { \
38 prop.len = sizeof(e); \
39 e = *((uint8_t *) (hal_prop->val)); \
42 static const bt_callbacks_t *bt_hal_cbacks = NULL;
45 /* Forward declarations */
46 static void __bt_adapter_props_to_hal(bt_property_t *send_props, struct hal_property *prop, uint8_t num_props, uint16_t len);
47 static void __bt_device_props_to_hal(bt_property_t *send_props,
48 struct hal_property *prop, uint8_t num_props,
50 static void __bt_hal_handle_adapter_state_changed(void *buf, uint16_t len);
51 static void __bt_hal_handle_adapter_property_changed(void *buf, uint16_t len);
52 static void __bt_hal_handle_stack_messages(int message, void *buf, uint16_t len);
53 static void __bt_hal_handle_adapter_discovery_state_changed(void *buf, uint16_t len);
54 static void __bt_hal_handle_device_found_event(void *buf, uint16_t len);
56 static bool interface_ready(void)
58 return bt_hal_cbacks != NULL;
61 static int init(bt_callbacks_t *callbacks)
64 DBG("HAL library Initialization..");
66 if (interface_ready())
67 return BT_STATUS_DONE;
69 bt_hal_cbacks = callbacks;
70 DBG("Store HAL stack msg handler callback");
71 _bt_hal_dbus_store_stack_msg_cb(__bt_hal_handle_stack_messages);
72 ret = _bt_hal_initialize_event_receiver(__bt_hal_handle_stack_messages);
74 if (ret == BT_STATUS_SUCCESS)
75 return BT_STATUS_SUCCESS;
77 return BT_STATUS_FAIL;
80 return BT_STATUS_SUCCESS;
84 static int enable(void)
86 return _bt_hal_dbus_enable_adapter();
90 static int disable(void)
92 return _bt_hal_dbus_disable_adapter();
95 static void cleanup(void)
100 static int get_adapter_properties(void)
102 return _bt_hal_dbus_get_adapter_properties();
105 static int get_adapter_property(bt_property_type_t type)
107 return _bt_hal_dbus_get_adapter_property(type);
110 static int set_adapter_property(const bt_property_t *property)
113 ERR("Invalid param");
114 return BT_STATUS_PARM_INVALID;
117 return _bt_hal_dbus_set_adapter_property(property);
120 static int get_remote_device_properties(bt_bdaddr_t *remote_addr)
122 return BT_STATUS_UNSUPPORTED;
125 static int get_remote_device_property(bt_bdaddr_t *remote_addr,
126 bt_property_type_t type)
128 return BT_STATUS_UNSUPPORTED;
131 static int set_remote_device_property(bt_bdaddr_t *remote_addr,
132 const bt_property_t *property)
134 return BT_STATUS_UNSUPPORTED;
137 static int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
139 return BT_STATUS_UNSUPPORTED;
143 static int get_remote_services(bt_bdaddr_t *remote_addr)
145 return BT_STATUS_UNSUPPORTED;
148 static int start_discovery(void)
150 return _bt_hal_dbus_start_discovery();
153 static int cancel_discovery(void)
155 return _bt_hal_dbus_stop_discovery();
158 static int create_bond(const bt_bdaddr_t *bd_addr, int transport)
160 return BT_STATUS_UNSUPPORTED;
163 static int cancel_bond(const bt_bdaddr_t *bd_addr)
165 return BT_STATUS_UNSUPPORTED;
168 static int remove_bond(const bt_bdaddr_t *bd_addr)
170 return BT_STATUS_UNSUPPORTED;
173 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
174 uint8_t pin_len, bt_pin_code_t *pin_code)
176 return BT_STATUS_UNSUPPORTED;
179 static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
180 uint8_t accept, uint32_t passkey)
182 return BT_STATUS_UNSUPPORTED;
185 static const void *get_profile_interface(const char *profile_id)
187 /*TODO: Profile interfaces to be included later*/
188 DBG("%s", profile_id);
190 if (!interface_ready())
193 if (!strcmp(profile_id, BT_PROFILE_PAN_ID))
196 if (!strcmp(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
199 if (!strcmp(profile_id, BT_PROFILE_AV_RC_ID))
202 if (!strcmp(profile_id, BT_PROFILE_HANDSFREE_ID))
205 if (!strcmp(profile_id, BT_PROFILE_GATT_ID))
208 if (!strcmp(profile_id, BT_PROFILE_HEALTH_ID))
211 if (!strcmp(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
214 if (!strcmp(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
217 if (!strcmp(profile_id, BT_PROFILE_MAP_CLIENT_ID))
220 if (!strcmp(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
226 static int dut_mode_configure(uint8_t enable)
228 return BT_STATUS_UNSUPPORTED;
231 static int dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t buf_len)
233 return BT_STATUS_UNSUPPORTED;
236 static int le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t buf_len)
238 return BT_STATUS_UNSUPPORTED;
241 static int config_hci_snoop_log(uint8_t enable)
243 return BT_STATUS_UNSUPPORTED;
246 static int get_connection_state(const bt_bdaddr_t *bd_addr)
248 return BT_STATUS_UNSUPPORTED;
251 static int set_os_callouts(bt_os_callouts_t *callouts)
253 DBG("callouts: %p", callouts);
255 /* TODO: implement */
257 return BT_STATUS_UNSUPPORTED;
260 static int read_energy_info(void)
262 return BT_STATUS_UNSUPPORTED;
265 static const bt_interface_t bluetooth_if = {
266 .size = sizeof(bt_interface_t),
271 .get_adapter_properties = get_adapter_properties,
272 .get_adapter_property = get_adapter_property,
273 .set_adapter_property = set_adapter_property,
274 .get_remote_device_properties = get_remote_device_properties,
275 .get_remote_device_property = get_remote_device_property,
276 .set_remote_device_property = set_remote_device_property,
277 .get_remote_service_record = get_remote_service_record,
278 .get_remote_services = get_remote_services,
279 .start_discovery = start_discovery,
280 .cancel_discovery = cancel_discovery,
281 .create_bond = create_bond,
282 .remove_bond = remove_bond,
283 .cancel_bond = cancel_bond,
284 .pin_reply = pin_reply,
285 .ssp_reply = ssp_reply,
286 .get_profile_interface = get_profile_interface,
287 .dut_mode_configure = dut_mode_configure,
288 .dut_mode_send = dut_mode_send,
289 .le_test_mode = le_test_mode,
290 .config_hci_snoop_log = config_hci_snoop_log,
291 .get_connection_state = get_connection_state,
292 .set_os_callouts = set_os_callouts,
293 .read_energy_info = read_energy_info,
296 static const bt_interface_t *get_bluetooth_interface(void)
299 return &bluetooth_if;
302 static int close_bluetooth(struct hw_device_t *device)
310 static int open_bluetooth(const struct hw_module_t *module, char const *name,
311 struct hw_device_t **device)
313 bluetooth_device_t *dev = malloc(sizeof(bluetooth_device_t));
317 memset(dev, 0, sizeof(bluetooth_device_t));
318 dev->common.tag = HARDWARE_DEVICE_TAG;
319 dev->common.version = 0;
320 dev->common.module = (struct hw_module_t *) module;
321 dev->common.close = close_bluetooth;
322 dev->get_bluetooth_interface = get_bluetooth_interface;
324 *device = (struct hw_device_t *) dev;
329 static struct hw_module_methods_t bluetooth_module_methods = {
330 .open = open_bluetooth,
333 struct hw_module_t HAL_MODULE_INFO_SYM = {
334 .tag = HARDWARE_MODULE_TAG,
337 .id = BT_HARDWARE_MODULE_ID,
338 .name = "Bluetooth stack",
339 .author = "Intel Corporation",
340 .methods = &bluetooth_module_methods
343 static void __bt_hal_handle_adapter_state_changed(void *buf, uint16_t len)
345 struct hal_ev_adapter_state_changed *ev = buf;
347 DBG("Adapter State: %d", ev->state);
349 if (bt_hal_cbacks->adapter_state_changed_cb)
350 bt_hal_cbacks->adapter_state_changed_cb(ev->state);
353 static void __bt_adapter_props_to_hal(bt_property_t *send_props, struct hal_property *prop,
354 uint8_t num_props, uint16_t len)
359 for (i = 0; i < num_props; i++) {
360 if (sizeof(*prop) + prop->len > len) {
361 ERR("invalid adapter properties(%zu > %u), cant process further properties!!!",
362 sizeof(*prop) + prop->len, len);
366 send_props[i].type = prop->type;
368 switch (prop->type) {
369 /* TODO: Add Adapter Properties */
371 send_props[i].len = prop->len;
372 send_props[i].val = prop->val;
376 DBG("prop[%d]: %s", i, btproperty2str(&send_props[i]));
378 len -= sizeof(*prop) + prop->len;
379 buf += sizeof(*prop) + prop->len;
387 static void __bt_device_props_to_hal(bt_property_t *send_props,
388 struct hal_property *prop, uint8_t num_props,
396 for (i = 0; i < num_props; i++) {
398 if (sizeof(*prop) + prop->len > len) {
399 ERR("invalid device properties (%zu > %u), cant process further properties!!!",
400 sizeof(*prop) + prop->len, len);
404 send_props[i].type = prop->type;
406 DBG("HAL prop Type [%d]", prop->type);
408 switch (prop->type) {
409 case HAL_PROP_DEVICE_TYPE:
411 DBG("Device property:HAL_PROP_DEVICE_TYPE:");
412 enum_prop_to_hal(send_props[i], prop,
416 case HAL_PROP_DEVICE_VERSION_INFO:
418 DBG("Device property: HAL_PROP_DEVICE_VERSION_INFO");
419 static bt_remote_version_t e;
420 const struct hal_prop_device_info *p;
421 send_props[i].val = &e;
422 send_props[i].len = sizeof(e);
423 p = (struct hal_prop_device_info *) prop->val;
424 e.manufacturer = p->manufacturer;
425 e.sub_ver = p->sub_version;
426 e.version = p->version;
429 case HAL_PROP_DEVICE_SERVICE_REC:
431 DBG("Device property: HAL_PROP_DEVICE_SERVICE_REC");
432 static bt_service_record_t e;
433 const struct hal_prop_device_service_rec *p;
434 send_props[i].val = &e;
435 send_props[i].len = sizeof(e);
436 p = (struct hal_prop_device_service_rec *) prop->val;
437 memset(&e, 0, sizeof(e));
438 memcpy(&e.channel, &p->channel, sizeof(e.channel));
439 memcpy(e.uuid.uu, p->uuid, sizeof(e.uuid.uu));
440 memcpy(e.name, p->name, p->name_len);
444 send_props[i].len = prop->len;
445 send_props[i].val = prop->val;
449 DBG("prop[%d]: %s", i, btproperty2str(&send_props[i]));
450 len -= sizeof(*prop) + prop->len;
451 buf += sizeof(*prop) + prop->len;
461 ERR("invalid device properties (%u bytes left), ", len);
464 static void __bt_hal_handle_adapter_property_changed(void *buf, uint16_t len)
466 struct hal_ev_adapter_props_changed *ev = (struct hal_ev_adapter_props_changed *)buf;
467 bt_property_t props[ev->num_props];
470 if (!bt_hal_cbacks->adapter_properties_cb)
474 __bt_adapter_props_to_hal(props, ev->props, ev->num_props, len);
476 if (bt_hal_cbacks->adapter_properties_cb)
477 bt_hal_cbacks->adapter_properties_cb(ev->status, ev->num_props, props);
480 static void __bt_hal_handle_adapter_discovery_state_changed(void *buf, uint16_t len)
482 struct hal_ev_discovery_state_changed *ev = (struct hal_ev_discovery_state_changed *)buf;
486 if (bt_hal_cbacks->discovery_state_changed_cb)
487 bt_hal_cbacks->discovery_state_changed_cb(ev->state);
490 static void __bt_hal_handle_device_found_event(void *buf, uint16_t len)
492 struct hal_ev_device_found *ev = (struct hal_ev_device_found *) buf;
493 bt_property_t props[ev->num_props];
496 if (!bt_hal_cbacks->device_found_cb)
500 __bt_device_props_to_hal(props, ev->props, ev->num_props, len);
502 bt_hal_cbacks->device_found_cb(ev->num_props, props);
505 static void __bt_hal_handle_stack_messages(int message, void *buf, uint16_t len)
509 case HAL_EV_ADAPTER_STATE_CHANGED:
510 DBG("Event: HAL_EV_ADAPTER_STATE_CHANGED");
511 __bt_hal_handle_adapter_state_changed(buf, len);
513 case HAL_EV_ADAPTER_PROPS_CHANGED:
514 DBG("Event: HAL_EV_ADAPTER_PROPS_CHANGED");
515 __bt_hal_handle_adapter_property_changed(buf, len);
517 case HAL_EV_DISCOVERY_STATE_CHANGED:
518 DBG("Event: HAL_EV_DISCOVERY_STATE_CHANGED");
519 __bt_hal_handle_adapter_discovery_state_changed(buf, len);
521 case HAL_EV_DEVICE_FOUND:
522 DBG("Event: HAL_EV_DEVICE_FOUND");
523 __bt_hal_handle_device_found_event(buf, len);
525 DBG("Event Currently not handled!!");