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>
34 #include <bt-hal-device-dbus-handler.h>
36 #define enum_prop_to_hal(prop, hal_prop, type) do { \
39 prop.len = sizeof(e); \
40 e = *((uint8_t *) (hal_prop->val)); \
43 static const bt_callbacks_t *bt_hal_cbacks = NULL;
46 /* Forward declarations */
47 static void __bt_adapter_props_to_hal(bt_property_t *send_props, struct hal_property *prop, uint8_t num_props, uint16_t len);
48 static void __bt_device_props_to_hal(bt_property_t *send_props,
49 struct hal_property *prop, uint8_t num_props,
51 static void __bt_hal_handle_adapter_state_changed(void *buf, uint16_t len);
52 static void __bt_hal_handle_adapter_property_changed(void *buf, uint16_t len);
53 static void __bt_hal_handle_stack_messages(int message, void *buf, uint16_t len);
54 static void __bt_hal_handle_adapter_discovery_state_changed(void *buf, uint16_t len);
55 static void __bt_hal_handle_device_found_event(void *buf, uint16_t len);
57 static bool interface_ready(void)
59 return bt_hal_cbacks != NULL;
62 static int init(bt_callbacks_t *callbacks)
65 DBG("HAL library Initialization..");
67 if (interface_ready())
68 return BT_STATUS_DONE;
70 bt_hal_cbacks = callbacks;
71 DBG("Store HAL stack msg handler callback");
72 _bt_hal_dbus_store_stack_msg_cb(__bt_hal_handle_stack_messages);
73 ret = _bt_hal_initialize_event_receiver(__bt_hal_handle_stack_messages);
75 if (ret == BT_STATUS_SUCCESS)
76 return BT_STATUS_SUCCESS;
78 return BT_STATUS_FAIL;
81 return BT_STATUS_SUCCESS;
85 static int enable(void)
87 return _bt_hal_dbus_enable_adapter();
91 static int disable(void)
93 return _bt_hal_dbus_disable_adapter();
96 static void cleanup(void)
101 static int get_adapter_properties(void)
103 return _bt_hal_dbus_get_adapter_properties();
106 static int get_adapter_property(bt_property_type_t type)
108 return _bt_hal_dbus_get_adapter_property(type);
111 static int set_adapter_property(const bt_property_t *property)
114 ERR("Invalid param");
115 return BT_STATUS_PARM_INVALID;
118 return _bt_hal_dbus_set_adapter_property(property);
121 static int get_remote_device_properties(bt_bdaddr_t *remote_addr)
124 ERR("Invalid param");
125 return BT_STATUS_PARM_INVALID;
128 return _bt_hal_dbus_get_remote_device_properties(remote_addr);
131 static int get_remote_device_property(bt_bdaddr_t *remote_addr,
132 bt_property_type_t type)
134 return BT_STATUS_UNSUPPORTED;
137 static int set_remote_device_property(bt_bdaddr_t *remote_addr,
138 const bt_property_t *property)
140 return _bt_hal_dbus_set_remote_device_property(remote_addr, property);
143 static int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
145 return BT_STATUS_UNSUPPORTED;
149 static int get_remote_services(bt_bdaddr_t *remote_addr)
151 return BT_STATUS_UNSUPPORTED;
154 static int start_discovery(void)
156 return _bt_hal_dbus_start_discovery();
159 static int cancel_discovery(void)
161 return _bt_hal_dbus_stop_discovery();
164 static int create_bond(const bt_bdaddr_t *bd_addr, int transport)
167 return _bt_hal_device_create_bond(bd_addr, transport);
170 static int cancel_bond(const bt_bdaddr_t *bd_addr)
172 return BT_STATUS_UNSUPPORTED;
175 static int remove_bond(const bt_bdaddr_t *bd_addr)
178 return _bt_hal_device_remove_bond(bd_addr);
181 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
182 uint8_t pin_len, bt_pin_code_t *pin_code)
184 return BT_STATUS_UNSUPPORTED;
187 static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
188 uint8_t accept, uint32_t passkey)
190 return BT_STATUS_UNSUPPORTED;
193 static const void *get_profile_interface(const char *profile_id)
195 /*TODO: Profile interfaces to be included later*/
196 DBG("%s", profile_id);
198 if (!interface_ready())
201 if (!strcmp(profile_id, BT_PROFILE_PAN_ID))
204 if (!strcmp(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
207 if (!strcmp(profile_id, BT_PROFILE_AV_RC_ID))
210 if (!strcmp(profile_id, BT_PROFILE_HANDSFREE_ID))
213 if (!strcmp(profile_id, BT_PROFILE_GATT_ID))
216 if (!strcmp(profile_id, BT_PROFILE_HEALTH_ID))
219 if (!strcmp(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
222 if (!strcmp(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
225 if (!strcmp(profile_id, BT_PROFILE_MAP_CLIENT_ID))
228 if (!strcmp(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
234 static int dut_mode_configure(uint8_t enable)
236 return BT_STATUS_UNSUPPORTED;
239 static int dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t buf_len)
241 return BT_STATUS_UNSUPPORTED;
244 static int le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t buf_len)
246 return BT_STATUS_UNSUPPORTED;
249 static int config_hci_snoop_log(uint8_t enable)
251 return BT_STATUS_UNSUPPORTED;
254 static int get_connection_state(const bt_bdaddr_t *bd_addr)
256 return BT_STATUS_UNSUPPORTED;
259 static int set_os_callouts(bt_os_callouts_t *callouts)
261 DBG("callouts: %p", callouts);
263 /* TODO: implement */
265 return BT_STATUS_UNSUPPORTED;
268 static int read_energy_info(void)
270 return BT_STATUS_UNSUPPORTED;
273 static const bt_interface_t bluetooth_if = {
274 .size = sizeof(bt_interface_t),
279 .get_adapter_properties = get_adapter_properties,
280 .get_adapter_property = get_adapter_property,
281 .set_adapter_property = set_adapter_property,
282 .get_remote_device_properties = get_remote_device_properties,
283 .get_remote_device_property = get_remote_device_property,
284 .set_remote_device_property = set_remote_device_property,
285 .get_remote_service_record = get_remote_service_record,
286 .get_remote_services = get_remote_services,
287 .start_discovery = start_discovery,
288 .cancel_discovery = cancel_discovery,
289 .create_bond = create_bond,
290 .remove_bond = remove_bond,
291 .cancel_bond = cancel_bond,
292 .pin_reply = pin_reply,
293 .ssp_reply = ssp_reply,
294 .get_profile_interface = get_profile_interface,
295 .dut_mode_configure = dut_mode_configure,
296 .dut_mode_send = dut_mode_send,
297 .le_test_mode = le_test_mode,
298 .config_hci_snoop_log = config_hci_snoop_log,
299 .get_connection_state = get_connection_state,
300 .set_os_callouts = set_os_callouts,
301 .read_energy_info = read_energy_info,
304 static const bt_interface_t *get_bluetooth_interface(void)
307 return &bluetooth_if;
310 static int close_bluetooth(struct hw_device_t *device)
318 static int open_bluetooth(const struct hw_module_t *module, char const *name,
319 struct hw_device_t **device)
321 bluetooth_device_t *dev = malloc(sizeof(bluetooth_device_t));
325 memset(dev, 0, sizeof(bluetooth_device_t));
326 dev->common.tag = HARDWARE_DEVICE_TAG;
327 dev->common.version = 0;
328 dev->common.module = (struct hw_module_t *) module;
329 dev->common.close = close_bluetooth;
330 dev->get_bluetooth_interface = get_bluetooth_interface;
332 *device = (struct hw_device_t *) dev;
337 static struct hw_module_methods_t bluetooth_module_methods = {
338 .open = open_bluetooth,
341 struct hw_module_t HAL_MODULE_INFO_SYM = {
342 .tag = HARDWARE_MODULE_TAG,
345 .id = BT_HARDWARE_MODULE_ID,
346 .name = "Bluetooth stack",
347 .author = "Intel Corporation",
348 .methods = &bluetooth_module_methods
351 static void __bt_hal_handle_adapter_state_changed(void *buf, uint16_t len)
353 struct hal_ev_adapter_state_changed *ev = buf;
355 DBG("Adapter State: %d", ev->state);
357 if (bt_hal_cbacks->adapter_state_changed_cb)
358 bt_hal_cbacks->adapter_state_changed_cb(ev->state);
361 static void __bt_adapter_props_to_hal(bt_property_t *send_props, struct hal_property *prop,
362 uint8_t num_props, uint16_t len)
367 for (i = 0; i < num_props; i++) {
368 if (sizeof(*prop) + prop->len > len) {
369 ERR("invalid adapter properties(%zu > %u), cant process further properties!!!",
370 sizeof(*prop) + prop->len, len);
374 send_props[i].type = prop->type;
376 switch (prop->type) {
377 /* TODO: Add Adapter Properties */
379 send_props[i].len = prop->len;
380 send_props[i].val = prop->val;
384 DBG("prop[%d]: %s", i, btproperty2str(&send_props[i]));
386 len -= sizeof(*prop) + prop->len;
387 buf += sizeof(*prop) + prop->len;
395 static void __bt_device_props_to_hal(bt_property_t *send_props,
396 struct hal_property *prop, uint8_t num_props,
404 for (i = 0; i < num_props; i++) {
406 if (sizeof(*prop) + prop->len > len) {
407 ERR("invalid device properties (%zu > %u), cant process further properties!!!",
408 sizeof(*prop) + prop->len, len);
412 send_props[i].type = prop->type;
414 DBG("HAL prop Type [%d]", prop->type);
416 switch (prop->type) {
417 case HAL_PROP_DEVICE_TYPE:
419 DBG("Device property:HAL_PROP_DEVICE_TYPE:");
420 enum_prop_to_hal(send_props[i], prop,
424 case HAL_PROP_DEVICE_VERSION_INFO:
426 DBG("Device property: HAL_PROP_DEVICE_VERSION_INFO");
427 static bt_remote_version_t e;
428 const struct hal_prop_device_info *p;
429 send_props[i].val = &e;
430 send_props[i].len = sizeof(e);
431 p = (struct hal_prop_device_info *) prop->val;
432 e.manufacturer = p->manufacturer;
433 e.sub_ver = p->sub_version;
434 e.version = p->version;
437 case HAL_PROP_DEVICE_SERVICE_REC:
439 DBG("Device property: HAL_PROP_DEVICE_SERVICE_REC");
440 static bt_service_record_t e;
441 const struct hal_prop_device_service_rec *p;
442 send_props[i].val = &e;
443 send_props[i].len = sizeof(e);
444 p = (struct hal_prop_device_service_rec *) prop->val;
445 memset(&e, 0, sizeof(e));
446 memcpy(&e.channel, &p->channel, sizeof(e.channel));
447 memcpy(e.uuid.uu, p->uuid, sizeof(e.uuid.uu));
448 memcpy(e.name, p->name, p->name_len);
452 send_props[i].len = prop->len;
453 send_props[i].val = prop->val;
457 DBG("prop[%d]: %s", i, btproperty2str(&send_props[i]));
458 len -= sizeof(*prop) + prop->len;
459 buf += sizeof(*prop) + prop->len;
469 ERR("invalid device properties (%u bytes left), ", len);
472 static void __bt_hal_handle_adapter_property_changed(void *buf, uint16_t len)
474 struct hal_ev_adapter_props_changed *ev = (struct hal_ev_adapter_props_changed *)buf;
475 bt_property_t props[ev->num_props];
478 if (!bt_hal_cbacks->adapter_properties_cb)
482 __bt_adapter_props_to_hal(props, ev->props, ev->num_props, len);
484 if (bt_hal_cbacks->adapter_properties_cb)
485 bt_hal_cbacks->adapter_properties_cb(ev->status, ev->num_props, props);
488 static void __bt_hal_handle_adapter_discovery_state_changed(void *buf, uint16_t len)
490 struct hal_ev_discovery_state_changed *ev = (struct hal_ev_discovery_state_changed *)buf;
494 if (bt_hal_cbacks->discovery_state_changed_cb)
495 bt_hal_cbacks->discovery_state_changed_cb(ev->state);
498 static void __bt_hal_handle_device_found_event(void *buf, uint16_t len)
500 struct hal_ev_device_found *ev = (struct hal_ev_device_found *) buf;
501 bt_property_t props[ev->num_props];
504 if (!bt_hal_cbacks->device_found_cb)
508 __bt_device_props_to_hal(props, ev->props, ev->num_props, len);
510 bt_hal_cbacks->device_found_cb(ev->num_props, props);
513 static void __bt_hal_handle_remote_device_properties_event(void *buf, uint16_t len)
515 struct hal_ev_remote_device_props *ev = (struct hal_ev_remote_device_props *) buf;
517 bt_property_t props[ev->num_props];
521 if (!bt_hal_cbacks->remote_device_properties_cb)
525 __bt_device_props_to_hal(props, ev->props, ev->num_props, len);
526 memcpy(bd_addr.address, ev->bdaddr, 6);
527 bt_hal_cbacks->remote_device_properties_cb(
528 ev->status, &bd_addr, ev->num_props, props);
532 static void __bt_hal_handle_stack_messages(int message, void *buf, uint16_t len)
536 case HAL_EV_ADAPTER_STATE_CHANGED:
537 DBG("Event: HAL_EV_ADAPTER_STATE_CHANGED");
538 __bt_hal_handle_adapter_state_changed(buf, len);
540 case HAL_EV_ADAPTER_PROPS_CHANGED:
541 DBG("Event: HAL_EV_ADAPTER_PROPS_CHANGED");
542 __bt_hal_handle_adapter_property_changed(buf, len);
544 case HAL_EV_DISCOVERY_STATE_CHANGED:
545 DBG("Event: HAL_EV_DISCOVERY_STATE_CHANGED");
546 __bt_hal_handle_adapter_discovery_state_changed(buf, len);
548 case HAL_EV_DEVICE_FOUND:
549 DBG("Event: HAL_EV_DEVICE_FOUND");
550 __bt_hal_handle_device_found_event(buf, len);
552 case HAL_EV_REMOTE_DEVICE_PROPS:
553 DBG("Event: HAL_EV_REMOTE_DEVICE_PROPS");
554 __bt_hal_handle_remote_device_properties_event(buf, len);
557 DBG("Event Currently not handled!!");