1/****************************************************************************** 2 * 3 * Copyright (C) 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 "adapter/bluetooth_test.h" 20#include "rfcomm/rfcomm_test.h" 21 22#include "btcore/include/bdaddr.h" 23 24#include <sys/socket.h> 25#include <unistd.h> 26 27namespace { 28static const char HANDSHAKE_COMMAND[] = "AT+BRSF=29\r"; 29} // namespace 30 31namespace bttest { 32 33TEST_F(RFCommTest, RfcommConnectPairedDevice) { 34 int fd = -1; 35 int error = 0; 36 size_t len = 0; 37 38 error = socket_interface()->connect(&bt_remote_bdaddr_, BTSOCK_RFCOMM, 39 (const uint8_t*)&HFP_UUID, 0, &fd, 0, 40 getuid()); 41 EXPECT_TRUE(error == BT_STATUS_SUCCESS) << "Error creating RFCOMM socket: " 42 << error; 43 EXPECT_TRUE(fd != -1) << "Error creating RFCOMM socket: invalid fd"; 44 45 int channel; 46 sock_connect_signal_t signal; 47 len = read(fd, &channel, sizeof(channel)); 48 EXPECT_TRUE(len == sizeof(channel)) 49 << "Channel not read from RFCOMM socket. Bytes read: " << len; 50 len = read(fd, &signal, sizeof(signal)); 51 EXPECT_TRUE(len == sizeof(signal)) 52 << "Connection signal not read from RFCOMM socket. Bytes read: " << len; 53 54 EXPECT_TRUE(!memcmp(&signal.bd_addr, &bt_remote_bdaddr_, sizeof(bt_bdaddr_t))) 55 << "Connected to a different bdaddr than expected."; 56 EXPECT_TRUE(channel == signal.channel) 57 << "Inconsistent channels returned: " << channel << " and " 58 << signal.channel; 59 60 len = write(fd, HANDSHAKE_COMMAND, sizeof(HANDSHAKE_COMMAND)); 61 EXPECT_TRUE(len == sizeof(HANDSHAKE_COMMAND)) 62 << "Unable to send HFP handshake. Bytes written: " << len; 63 64 char response[1024]; 65 len = read(fd, response, sizeof(response)); 66 EXPECT_TRUE(len > 0) << "Read " << len << " bytes"; 67 68 close(fd); 69} 70 71TEST_F(RFCommTest, RfcommRepeatedConnectPairedDevice) { 72 static const int max_iterations = 128; 73 int channel_fail = 0, signal_fail = 0, handshake_fail = 0, read_fail = 0; 74 75 for (int i = 0; i < max_iterations; ++i) { 76 int fd = -1; 77 int error = 0; 78 size_t len = 0; 79 80 error = socket_interface()->connect(&bt_remote_bdaddr_, BTSOCK_RFCOMM, 81 (const uint8_t*)&HFP_UUID, 0, &fd, 0, 82 getuid()); 83 ASSERT_TRUE(error == BT_STATUS_SUCCESS) << "Error creating RFCOMM socket: " 84 << error; 85 ASSERT_TRUE(fd != -1) << "Error creating RFCOMM socket: invalid fd"; 86 87 int channel; 88 sock_connect_signal_t signal; 89 len = read(fd, &channel, sizeof(channel)); 90 if (len != sizeof(channel)) { 91 ADD_FAILURE() << "Channel not read from RFCOMM socket. Bytes read: " 92 << len << ", Sizeof channel: " << sizeof(channel); 93 channel_fail++; 94 } 95 96 len = read(fd, &signal, sizeof(signal)); 97 if (len != sizeof(signal)) { 98 ADD_FAILURE() 99 << "Connection signal not read from RFCOMM socket. Bytes read: " 100 << len; 101 signal_fail++; 102 } 103 104 EXPECT_TRUE( 105 !memcmp(&signal.bd_addr, &bt_remote_bdaddr_, sizeof(bt_bdaddr_t))) 106 << "Connected to a different bdaddr than expected."; 107 EXPECT_TRUE(channel == signal.channel) 108 << "Inconsistent channels returned: " << channel << " and " 109 << signal.channel; 110 len = write(fd, HANDSHAKE_COMMAND, sizeof(HANDSHAKE_COMMAND)); 111 if (len != sizeof(HANDSHAKE_COMMAND)) { 112 ADD_FAILURE() << "Unable to send HFP handshake. Bytes written: " << len; 113 handshake_fail++; 114 } 115 116 char response[1024]; 117 len = read(fd, response, sizeof(response)); 118 if (len <= 0) { 119 ADD_FAILURE() << "Read " << len << " bytes"; 120 read_fail++; 121 } 122 123 close(fd); 124 } 125 126 if (channel_fail > 0 || signal_fail > 0 || handshake_fail > 0 || 127 read_fail > 0) { 128 ADD_FAILURE() << "Number of channel read fails: " << channel_fail << "\n" 129 << "Number of signal read fails: " << signal_fail << "\n" 130 << "Number of handshake send fails: " << handshake_fail 131 << "\n" 132 << "Number of read response fails: " << read_fail; 133 } 134} 135 136} // bttest 137