Home | History | Annotate | Download | only in rfcomm
      1 /******************************************************************************
      2  *
      3  *  Copyright 2016 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 "rfcomm/rfcomm_test.h"
     20 #include "adapter/bluetooth_test.h"
     21 
     22 using bluetooth::Uuid;
     23 
     24 namespace bttest {
     25 
     26 const Uuid RFCommTest::HFP_UUID = Uuid::From16Bit(0x111E);
     27 
     28 void RFCommTest::SetUp() {
     29   BluetoothTest::SetUp();
     30 
     31   ASSERT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
     32   semaphore_wait(adapter_state_changed_callback_sem_);
     33   ASSERT_TRUE(GetState() == BT_STATE_ON);
     34   socket_interface_ =
     35       (const btsock_interface_t*)bt_interface()->get_profile_interface(
     36           BT_PROFILE_SOCKETS_ID);
     37   ASSERT_NE(socket_interface_, nullptr);
     38 
     39   // Find a bonded device that supports HFP
     40   bt_remote_bdaddr_ = RawAddress::kEmpty;
     41 
     42   bt_property_t* bonded_devices_prop =
     43       GetProperty(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
     44   RawAddress* devices = (RawAddress*)bonded_devices_prop->val;
     45   int num_bonded_devices = bonded_devices_prop->len / sizeof(RawAddress);
     46 
     47   for (int i = 0; i < num_bonded_devices && bt_remote_bdaddr_.IsEmpty(); i++) {
     48     ClearSemaphore(remote_device_properties_callback_sem_);
     49     bt_interface()->get_remote_device_property(&devices[i], BT_PROPERTY_UUIDS);
     50     semaphore_wait(remote_device_properties_callback_sem_);
     51 
     52     bt_property_t* uuid_prop =
     53         GetRemoteDeviceProperty(&devices[i], BT_PROPERTY_UUIDS);
     54     if (uuid_prop == nullptr) continue;
     55     Uuid* uuids = reinterpret_cast<Uuid*>(uuid_prop->val);
     56     int num_uuids = uuid_prop->len / sizeof(Uuid);
     57 
     58     for (int j = 0; j < num_uuids; j++) {
     59       if (!memcmp(uuids + j, &HFP_UUID, sizeof(Uuid))) {
     60         bt_remote_bdaddr_ = *(devices + i);
     61         break;
     62       }
     63     }
     64   }
     65 
     66   ASSERT_FALSE(bt_remote_bdaddr_.IsEmpty())
     67       << "Could not find paired device that supports HFP";
     68 }
     69 
     70 void RFCommTest::TearDown() {
     71   socket_interface_ = NULL;
     72 
     73   ASSERT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
     74   semaphore_wait(adapter_state_changed_callback_sem_);
     75 
     76   BluetoothTest::TearDown();
     77 }
     78 
     79 }  // bttest
     80