1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include "base.h"
20 #include "support/adapter.h"
21 #include "support/callbacks.h"
22 #include "btcore/include/property.h"
23 
adapter_enable_disable()24 bool adapter_enable_disable() {
25   int error;
26 
27   CALL_AND_WAIT(error = bt_interface->enable(), adapter_state_changed);
28   TASSERT(error == BT_STATUS_SUCCESS, "Error enabling Bluetooth: %d", error);
29   TASSERT(adapter_get_state() == BT_STATE_ON, "Adapter did not turn on.");
30 
31   CALL_AND_WAIT(error = bt_interface->disable(), adapter_state_changed);
32   TASSERT(error == BT_STATUS_SUCCESS, "Error disabling Bluetooth: %d", error);
33   TASSERT(adapter_get_state() == BT_STATE_OFF, "Adapter did not turn off.");
34 
35   return true;
36 }
37 
adapter_repeated_enable_disable()38 bool adapter_repeated_enable_disable() {
39   for (int i = 0; i < 10; ++i) {
40     if (!adapter_enable_disable()) {
41       return false;
42     }
43   }
44   return true;
45 }
46 
adapter_set_name()47 bool adapter_set_name() {
48   int error;
49   bt_property_t *name = property_new_name("set_name");
50 
51   CALL_AND_WAIT(error = bt_interface->set_adapter_property(name), adapter_properties);
52   TASSERT(error == BT_STATUS_SUCCESS, "Error setting device name.");
53   TASSERT(adapter_get_property_count() == 1, "Expected 1 adapter property change, found %d instead.", adapter_get_property_count());
54   TASSERT(adapter_get_property(BT_PROPERTY_BDNAME), "The Bluetooth name property did not change.");
55   TASSERT(property_equals(adapter_get_property(BT_PROPERTY_BDNAME), name), "Bluetooth name '%s' does not match test value", property_as_name(adapter_get_property(BT_PROPERTY_BDNAME))->name);
56 
57   property_free(name);
58 
59   return true;
60 }
61 
adapter_get_name()62 bool adapter_get_name() {
63   int error;
64   bt_property_t *name = property_new_name("get_name");
65 
66   CALL_AND_WAIT(bt_interface->set_adapter_property(name), adapter_properties);
67   CALL_AND_WAIT(error = bt_interface->get_adapter_property(BT_PROPERTY_BDNAME), adapter_properties);
68   TASSERT(error == BT_STATUS_SUCCESS, "Error getting device name.");
69   TASSERT(adapter_get_property_count() == 1, "Expected 1 adapter property change, found %d instead.", adapter_get_property_count());
70   TASSERT(adapter_get_property(BT_PROPERTY_BDNAME), "The Bluetooth name property did not change.");
71   TASSERT(property_equals(adapter_get_property(BT_PROPERTY_BDNAME), name), "Bluetooth name '%s' does not match test value", property_as_name(adapter_get_property(BT_PROPERTY_BDNAME))->name);
72 
73   property_free(name);
74   return true;
75 }
76 
adapter_start_discovery()77 bool adapter_start_discovery() {
78   int error;
79 
80   CALL_AND_WAIT(error = bt_interface->start_discovery(), discovery_state_changed);
81   TASSERT(error == BT_STATUS_SUCCESS, "Error calling start_discovery: %d", error);
82   TASSERT(adapter_get_discovery_state() == BT_DISCOVERY_STARTED, "Unable to start discovery.");
83 
84   return true;
85 }
86 
adapter_cancel_discovery()87 bool adapter_cancel_discovery() {
88   int error;
89 
90   CALL_AND_WAIT(bt_interface->start_discovery(), discovery_state_changed);
91   CALL_AND_WAIT(error = bt_interface->cancel_discovery(), discovery_state_changed);
92   TASSERT(error == BT_STATUS_SUCCESS, "Error calling cancel_discovery: %d", error);
93   TASSERT(adapter_get_discovery_state() == BT_DISCOVERY_STOPPED, "Unable to stop discovery.");
94 
95   return true;
96 }
97