--- /dev/null
+#
+# Copyright (C) 2014 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE := bdtool
+
+LOCAL_SRC_FILES := \
+ adapter.c \
+ bdtool.c \
+ ../../test/suite/support/callbacks.c \
+ ../../test/suite/support/hal.c
+
+LOCAL_STATIC_LIBRARIES := \
+ libbtcore \
+ libosi
+
+LOCAL_CFLAGS := -std=c99 $(bdroid_CFLAGS) -Wno-unused-parameter -Wno-missing-field-initializers
+
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/../../test/suite \
+ $(LOCAL_PATH)/../..
+
+LOCAL_SHARED_LIBRARIES += \
+ libhardware
+
+include $(BUILD_EXECUTABLE)
--- /dev/null
+/******************************************************************************
+ *
+ * Copyright (C) 2014 Google, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+#include "base.h"
+#include "btcore/include/bdaddr.h"
+#include "btcore/include/property.h"
+#include "support/adapter.h"
+#include "support/callbacks.h"
+
+static bt_state_t state;
+static int property_count = 0;
+static bt_property_t *properties = NULL;
+static bt_discovery_state_t discovery_state;
+static bt_acl_state_t acl_state;
+static bt_bond_state_t bond_state;
+
+static void parse_properties(int num_properties, bt_property_t *property);
+
+// Returns the current adapter state.
+bt_state_t adapter_get_state() {
+ return state;
+}
+
+// Returns the number of adapter properties.
+int adapter_get_property_count() {
+ return property_count;
+}
+
+// Returns the specified property.
+bt_property_t *adapter_get_property(bt_property_type_t type) {
+ for (int i = 0; i < property_count; ++i) {
+ if (properties[i].type == type) {
+ return &properties[i];
+ }
+ }
+
+ return NULL;
+}
+
+// Returns the device discovery state.
+bt_discovery_state_t adapter_get_discovery_state() {
+ return discovery_state;
+}
+
+// Returns the device acl state.
+bt_acl_state_t adapter_get_acl_state() {
+ return acl_state;
+}
+
+// Returns the device bond state.
+bt_bond_state_t adapter_get_bond_state() {
+ return bond_state;
+}
+
+// callback
+void acl_state_changed(bt_status_t status, bt_bdaddr_t *remote_bd_addr, bt_acl_state_t state) {
+ acl_state = state;
+ CALLBACK_RET();
+}
+
+// callback
+void adapter_properties(bt_status_t status,
+ int num_properties,
+ bt_property_t *new_properties) {
+ property_free_array(properties, property_count);
+ properties = property_copy_array(new_properties, num_properties);
+ property_count = num_properties;
+
+ CALLBACK_RET();
+}
+
+// callback
+void adapter_state_changed(bt_state_t new_state) {
+ state = new_state;
+ CALLBACK_RET();
+}
+
+// callback
+void bond_state_changed(bt_status_t status,
+ bt_bdaddr_t *bdaddr,
+ bt_bond_state_t state) {
+ char buf[18];
+ bond_state = state;
+
+ const char *state_name = "Bond state unknown";
+ switch (bond_state) {
+ case BT_BOND_STATE_NONE:
+ state_name = "Bond state none";
+ break;
+
+ case BT_BOND_STATE_BONDING:
+ state_name = "Bond state bonding";
+ break;
+
+ case BT_BOND_STATE_BONDED:
+ state_name = "Bond state bonded";
+ break;
+
+ // default none
+ }
+ fprintf(stdout, "Bond state changed callback addr:%s state:%s\n", bdaddr_to_string(bdaddr, buf, sizeof(buf)), state_name);
+
+ CALLBACK_RET();
+}
+
+// callback
+void device_found(int num_properties, bt_property_t *property) {
+ fprintf(stdout, "Device found num_properties:%d\n", num_properties);
+ parse_properties(num_properties, property);
+
+ CALLBACK_RET();
+}
+
+// callback
+void discovery_state_changed(bt_discovery_state_t state) {
+ const char *state_name = "Unknown";
+ discovery_state = state;
+
+ switch (discovery_state) {
+ case BT_DISCOVERY_STOPPED:
+ state_name = "Discovery stopped";
+ break;
+
+ case BT_DISCOVERY_STARTED:
+ state_name = "Discovery started";
+ break;
+
+ // default omitted
+ }
+ fprintf(stdout, "Discover state %s\n", state_name);
+
+ CALLBACK_RET();
+}
+
+// callback
+void remote_device_properties(bt_status_t status,
+ bt_bdaddr_t *bdaddr,
+ int num_properties,
+ bt_property_t *properties) {
+ char buf[18];
+ fprintf(stdout, "Device found bdaddr:%s num_properties:%d\n",
+ bdaddr_to_string(bdaddr, buf, sizeof(buf)), num_properties);
+
+ parse_properties(num_properties, properties);
+
+ CALLBACK_RET();
+}
+
+// callback
+void ssp_request(
+ bt_bdaddr_t *remote_bd_addr,
+ bt_bdname_t *bd_name,
+ uint32_t cod,
+ bt_ssp_variant_t pairing_variant,
+ uint32_t pass_key) {
+ char *pairing_variant_name = "Unknown";
+
+ switch (pairing_variant) {
+ case BT_SSP_VARIANT_PASSKEY_CONFIRMATION:
+ pairing_variant_name = "Passkey confirmation";
+ break;
+ case BT_SSP_VARIANT_PASSKEY_ENTRY:
+ pairing_variant_name = "Passkey entry";
+ break;
+
+ case BT_SSP_VARIANT_CONSENT:
+ pairing_variant_name = "Passkey consent";
+ break;
+
+ case BT_SSP_VARIANT_PASSKEY_NOTIFICATION:
+ pairing_variant_name = "Passkey notification";
+ break;
+ }
+
+ fprintf(stdout, "Got ssp request device_class:%u passkey:%x pairing_variant:%s\n", cod, pass_key, pairing_variant_name);
+ char buf[18];
+ fprintf(stdout, "Device found:%s %s\n", bdaddr_to_string(remote_bd_addr, buf, sizeof(buf)), bd_name->name);
+
+
+ fprintf(stdout, "auto-accepting bond\n");
+ bool accept = true;
+ int rc = bt_interface->ssp_reply(remote_bd_addr, pairing_variant,
+ (uint8_t)accept, pass_key);
+ CALLBACK_RET();
+}
+
+// callback
+void thread_evt(bt_cb_thread_evt evt) {
+ CALLBACK_RET();
+}
+
+static void parse_properties(int num_properties, bt_property_t *property) {
+ while (num_properties-- > 0) {
+ switch(property->type) {
+ case BT_PROPERTY_BDNAME:
+ {
+ const bt_bdname_t *bdname = property_extract_bdname(property);
+ if (bdname)
+ fprintf(stdout, " name:%s\n", bdname->name);
+ }
+ break;
+
+ case BT_PROPERTY_BDADDR:
+ {
+ char buf[18];
+ const bt_bdaddr_t *bdaddr = property_extract_bdaddr(property);
+ if (bdaddr)
+ fprintf(stdout, " addr:%s\n", bdaddr_to_string(bdaddr, buf, sizeof(buf)));
+ }
+ break;
+
+ case BT_PROPERTY_UUIDS:
+ {
+ const bt_uuid_t *uuid = property_extract_uuid(property);
+ if (uuid) {
+ fprintf(stdout, " uuid:");
+ for (size_t i = 0; i < sizeof(uuid); i++) {
+ fprintf(stdout, "%02x", uuid->uu[i]);
+ }
+ fprintf(stdout, "\n");
+ }
+ }
+ break;
+
+ case BT_PROPERTY_TYPE_OF_DEVICE:
+ {
+ const bt_device_type_t *device_type = property_extract_device_type(property);
+ if (device_type) {
+ const struct {
+ const char * device_type;
+ } device_type_lookup[] = {
+ { "Unknown" },
+ { "Classic Only" },
+ { "BLE Only" },
+ { "Both Classic and BLE" },
+ };
+ bt_device_type_t idx = *device_type;
+ if (idx > BT_DEVICE_DEVTYPE_DUAL)
+ idx = 0;
+ fprintf(stdout, " device_type:%s\n", device_type_lookup[idx].device_type);
+ }
+ }
+ break;
+
+ case BT_PROPERTY_CLASS_OF_DEVICE:
+ {
+ const uint32_t device_class = property_extract_device_class(property);
+ fprintf(stdout, " device_class:%u\n", device_class);
+ }
+ break;
+
+ case BT_PROPERTY_REMOTE_RSSI:
+ {
+ const int32_t rssi = property_extract_remote_rssi(property);
+ fprintf(stdout, " rssi:%d\n", rssi);
+ }
+ break;
+
+ case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
+ {
+ const bt_bdname_t *bdname = property_extract_bdname(property);
+ if (bdname)
+ fprintf(stdout, " remote_name:%s\n", bdname->name);
+ }
+ break;
+
+ case BT_PROPERTY_SERVICE_RECORD:
+ case BT_PROPERTY_ADAPTER_SCAN_MODE:
+ case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
+ case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
+ case BT_PROPERTY_REMOTE_VERSION_INFO:
+ case BT_PROPERTY_LOCAL_LE_FEATURES:
+ case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
+ default:
+ {
+ fprintf(stderr, "Unhandled property type:%d len:%d\n", property->type, property->len);
+ uint8_t *p = (uint8_t *)property->val;
+ for (int i = 0; i < property->len; ++i, p++) {
+ fprintf(stderr, " %02x", *p);
+ }
+ if (property->len != 0)
+ fprintf(stderr, "\n");
+ }
+ }
+ property++;
+ }
+}
--- /dev/null
+/******************************************************************************
+ *
+ * Copyright (C) 2014 Google, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+#include <getopt.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "btcore/include/bdaddr.h"
+#include "btcore/include/property.h"
+#include "osi/include/osi.h"
+#include "test/suite/support/callbacks.h"
+#include "test/suite/support/hal.h"
+
+const bt_interface_t *bt_interface;
+
+bt_bdaddr_t bt_remote_bdaddr;
+
+static int f_verbose;
+static bool discover = false;
+static bool discoverable = false;
+static bool bond = false;
+static bool up = false;
+static bool get_name = false;
+static bool set_name = false;
+
+static int timeout_in_sec = 30;
+static char *bd_name;
+
+static struct option long_options[] = {
+ {"bdaddr", required_argument, 0, 0 },
+ {"discover", no_argument, 0, 0 },
+ {"discoverable", no_argument, 0, 0 },
+ {"time", required_argument, 0, 0 },
+ {"bond", no_argument, 0, 0 },
+ {"up", no_argument, 0, 0 },
+ {"verbose", no_argument, 0, 0 },
+ {"get_name", no_argument, 0, 0 },
+ {"set_name", required_argument, 0, 0 },
+ {0, 0, 0, 0 }
+};
+
+static void usage(const char *name);
+static bool parse_args(int argc, char **argv);
+static void sig_handler(int signo);
+
+bt_property_t *adapter_get_property(bt_property_type_t type);
+
+int main(int argc, char **argv) {
+ if (!parse_args(argc, argv)) {
+ usage(argv[0]);
+ }
+
+ if (bond && discoverable) {
+ fprintf(stderr, "Can only select either bond or discoverable, not both\n");
+ usage(argv[0]);
+ }
+
+ if (!bond && !discover && !discoverable && !up && !get_name && !set_name) {
+ fprintf(stderr, "Must specify one command\n");
+ usage(argv[0]);
+ }
+
+ if (signal(SIGINT, sig_handler) == SIG_ERR) {
+ fprintf(stderr, "Will be unable to catch signals\n");
+ }
+
+ fprintf(stdout, "Bringing up bluetooth adapter\n");
+ if (!hal_open(callbacks_get_adapter_struct())) {
+ fprintf(stderr, "Unable to open Bluetooth HAL.\n");
+ return 1;
+ }
+
+ if (discover) {
+ CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
+ fprintf(stdout, "BT adapter is up\n");
+
+ fprintf(stdout, "Starting to start discovery\n");
+ CALL_AND_WAIT(bt_interface->start_discovery(), discovery_state_changed);
+ fprintf(stdout, "Started discovery for %d seconds\n", timeout_in_sec);
+
+ sleep(timeout_in_sec);
+
+ fprintf(stdout, "Starting to cancel discovery\n");
+ CALL_AND_WAIT(bt_interface->cancel_discovery(), discovery_state_changed);
+ fprintf(stdout, "Cancelled discovery after %d seconds\n", timeout_in_sec);
+ }
+
+ if (discoverable) {
+ CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
+ fprintf(stdout, "BT adapter is up\n");
+
+ bt_property_t *property = property_new_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
+
+ int rc = bt_interface->set_adapter_property(property);
+ fprintf(stdout, "Set rc:%d device as discoverable for %d seconds\n", rc, timeout_in_sec);
+
+ sleep(timeout_in_sec);
+
+ property_free(property);
+ }
+
+ if (bond) {
+ if (bdaddr_is_empty(&bt_remote_bdaddr)) {
+ fprintf(stderr, "Must specify a remote device address [ --bdaddr=xx:yy:zz:aa:bb:cc ]\n");
+ exit(1);
+ }
+
+ CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
+ fprintf(stdout, "BT adapter is up\n");
+
+ int rc = bt_interface->create_bond(&bt_remote_bdaddr);
+ fprintf(stdout, "Started bonding:%d for %d seconds\n", rc, timeout_in_sec);
+
+ sleep(timeout_in_sec);
+ }
+
+ if (up) {
+ CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
+ fprintf(stdout, "BT adapter is up\n");
+
+ fprintf(stdout, "Waiting for %d seconds\n", timeout_in_sec);
+ sleep(timeout_in_sec);
+ }
+
+ if (get_name) {
+ CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
+ fprintf(stdout, "BT adapter is up\n");
+ int error;
+ CALL_AND_WAIT(error = bt_interface->get_adapter_property(BT_PROPERTY_BDNAME), adapter_properties);
+ bt_property_t *property = adapter_get_property(BT_PROPERTY_BDNAME);
+ const bt_bdname_t *bdname = property_extract_bdname(property);
+ if (bdname)
+ printf("Queried bluetooth device name:%s\n", bdname->name);
+ else
+ printf("No name\n");
+ }
+
+ if (set_name) {
+ CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
+ fprintf(stdout, "BT adapter is up\n");
+
+ int error;
+ bt_property_t *property = property_new_name(bd_name);
+ printf("Setting bluetooth device name to:%s\n", bd_name);
+ CALL_AND_WAIT(error = bt_interface->set_adapter_property(property), adapter_properties);
+ CALL_AND_WAIT(error = bt_interface->get_adapter_property(BT_PROPERTY_BDNAME), adapter_properties);
+ property_free(property);
+ sleep(timeout_in_sec);
+ }
+
+ CALL_AND_WAIT(bt_interface->disable(), adapter_state_changed);
+ fprintf(stdout, "BT adapter is down\n");
+}
+
+static void sig_handler(int signo) {
+ if (signo == SIGINT) {
+ fprintf(stderr, "Received SIGINT\n");
+ CALL_AND_WAIT(bt_interface->disable(), adapter_state_changed);
+ fprintf(stderr, "BT adapter is down\n");
+ exit(1);
+ }
+}
+
+static void usage(const char *name) {
+ fprintf(stderr, "Usage: %s [--bond|--discover|--discoverable-|-up] [--bdaddr=<bdaddr>] [--time=<time_in_sec>] --verbose\n", name);
+ fprintf(stderr, " bond: Discover actively advertising devices\n");
+ fprintf(stderr, " discover: Discover actively advertising devices\n");
+ fprintf(stderr, " discoverable: Set into a connectable and discoverable mode\n");
+ fprintf(stderr, " up: Only bring up stack\n");
+ fprintf(stderr, " time: Time to hold in the specified mode\n");
+ exit(1);
+}
+
+static bool parse_args(int argc, char **argv) {
+ while (1) {
+ int option_index = 0;
+ int c = getopt_long_only(argc, argv, "", long_options, &option_index);
+ if (c != 0)
+ break;
+
+ switch (c) {
+ case 0:
+ if (option_index == 0) {
+ if (!string_to_bdaddr(optarg, &bt_remote_bdaddr)) {
+ return false;
+ }
+ }
+ if (option_index == 1) {
+ discover = true;
+ }
+ if (option_index == 2) {
+ discoverable = true;
+ }
+ if (option_index == 3) {
+ timeout_in_sec = atoi(optarg);
+ }
+ if (option_index == 4) {
+ bond = true;
+ }
+ if (option_index == 5) {
+ up = true;
+ }
+ if (option_index == 6) {
+ f_verbose++;
+ }
+ if (option_index == 7) {
+ get_name = true;
+ }
+ if (option_index == 8) {
+ bd_name = (char *)optarg;
+ set_name = true;
+ }
+ break;
+
+ default:
+ fprintf(stderr, "?? getopt returned character code 0%o ??\n", c);
+ }
+ }
+
+ if (optind < argc) {
+ fprintf(stderr, "non-option ARGV-elements: ");
+ while (optind < argc)
+ fprintf(stderr, "%s ", argv[optind++]);
+ fprintf(stderr, "\n");
+ return false;
+ }
+ return true;
+}