1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include <cmath> 17#include <cstdio> 18#include <unistd.h> 19#include <gtest/gtest.h> 20#include <securec.h> 21#include "hdf_base.h" 22#include "osal_time.h" 23#include "v1_0/ihci_interface.h" 24#include "bluetooth_hci_callback_impl.h" 25#include "hci_interface_impl.h" 26#include "vendor_interface.h" 27 28using namespace OHOS::HDI::Bluetooth::Hci::V1_0; 29using namespace testing::ext; 30#define COMMAND_HCI_SHOULD_BE_UNKNOWN \ 31 { 0xff, 0x3B, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 } 32#define COMMAND_HCI_READ_LOCAL_VERSION_INFORMATION \ 33 { 0x01, 0x10, 0x00 } 34#define COMMAND_HCI_READ_BUFFER_SIZE \ 35 { 0x05, 0x10, 0x00 } 36#define COMMAND_HCI_RESET \ 37 { 0x03, 0x0c, 0x00 } 38#define COMMAND_HCI_WRITE_LOCAL_NAME \ 39 { 0x13, 0x0c, 0xf8 } 40#define ACL_BROADCAST_POINT_TO_POINT \ 41 { 0x13, 0x0c, 0xf8 } 42#define ACL_PACKET_BOUNDARY_FIRST_AUTO_FLUSHABLE \ 43 { 0x13, 0x0c, 0xf8 } 44namespace { 45 sptr<IHciInterface> g_iBtHci = nullptr; 46 sptr<HciCallbackImpl> g_hciCallbacks = nullptr; 47 constexpr int32_t SENSOR_WAIT_TIME = 1000; 48 49} 50class HdfBluetoothHdiTest : public testing::Test { 51public: 52 static void SetUpTestCase(); 53 static void TearDownTestCase(); 54 void SetUp(); 55 void TearDown(); 56}; 57void HdfBluetoothHdiTest::SetUpTestCase() 58{ 59 g_iBtHci = IHciInterface::Get(); 60 std::cout << "g_iBtHci = " << g_iBtHci << std::endl; 61} 62void HdfBluetoothHdiTest::TearDownTestCase() 63{ 64} 65void HdfBluetoothHdiTest::SetUp() 66{ 67} 68void HdfBluetoothHdiTest::TearDown() 69{ 70} 71 72/** 73 * @tc.name: HdiBluetoothInit_0100 74 * @tc.desc: Returns 0 if the callback is successfully Init. 75 * @tc.type: FUNC 76 */ 77HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothInit_0100, TestSize.Level1) 78{ 79 g_hciCallbacks = new (std::nothrow) HciCallbackImpl(); 80 ASSERT_NE(nullptr, g_hciCallbacks); 81 std::cout << "g_hciCallbacks = " << g_hciCallbacks << std::endl; 82 ASSERT_NE(nullptr, g_iBtHci); 83 std::cout << "g_iBtHci0100 = " << g_iBtHci << std::endl; 84 int32_t ret = g_iBtHci->Init(g_hciCallbacks); 85 EXPECT_EQ(HDF_SUCCESS, ret); 86 OsalMSleep(SENSOR_WAIT_TIME); 87} 88 89/** 90 * @tc.name: HdiBluetoothSend_0100 91 * @tc.desc: the HCI ACL_DATA packets is sent success. 92 * @tc.type: FUNC 93 */ 94HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_0100, TestSize.Level2) 95{ 96 ASSERT_NE(nullptr, g_iBtHci); 97 std::cout << "g_iBtHci002 = " << g_iBtHci << std::endl; 98 int n = 0; 99 std::vector<uint8_t> acl_vector; 100 acl_vector.push_back(static_cast<uint8_t>(0xff)); 101 acl_vector.push_back(static_cast<uint8_t>((0x0f00) >> 8)); 102 acl_vector.push_back(static_cast<uint8_t>(0xff)); 103 acl_vector.push_back(static_cast<uint8_t>((0xff00) >> 8)); 104 for (size_t i = 0; i < 10; i++) { 105 acl_vector.push_back(static_cast<uint8_t>(i + n)); 106 } 107 BtType btType = BtType::ACL_DATA; 108 int32_t ret = g_iBtHci->SendHciPacket(btType, acl_vector); 109 std::cout << "HdiBluetoothSend_0100_ret = " << ret << std::endl; 110 EXPECT_EQ(HDF_SUCCESS, ret); 111} 112 113/** 114 * @tc.name: HdiBluetoothSend_0200 115 * @tc.desc: the HCI null ACL_DATA packets is sent. 116 * @tc.type: FUNC 117 */ 118HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_0200, TestSize.Level2) 119{ 120 ASSERT_NE(nullptr, g_iBtHci); 121 std::cout << "g_iBtHci003 = " << g_iBtHci << std::endl; 122 BtType btType = BtType::ACL_DATA; 123 std::vector<uint8_t> data; 124 int32_t ret = g_iBtHci->SendHciPacket(btType, data); 125 std::cout << "HdiBluetoothSend_0200_ret = " << ret << std::endl; 126 EXPECT_EQ(HDF_FAILURE, ret); 127} 128 129/** 130 * @tc.name: HdiBluetoothSend_0300 131 * @tc.desc: the reset HCI_CMD packets is sent successfully. 132 * @tc.type: FUNC 133 */ 134HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_0300, TestSize.Level2) 135{ 136 ASSERT_NE(nullptr, g_iBtHci); 137 std::cout << "g_iBtHci004 = " << g_iBtHci << std::endl; 138 std::vector<uint8_t> cmd = COMMAND_HCI_RESET; 139 BtType btType = BtType::HCI_CMD; 140 int32_t ret = g_iBtHci->SendHciPacket(btType, cmd); 141 std::cout << "HdiBluetoothSend_0300_ret = " << ret << std::endl; 142 EXPECT_EQ(HDF_SUCCESS, ret); 143} 144 145/** 146 * @tc.name: HdiBluetoothSend_0400 147 * @tc.desc: the version HCI_CMD packets is sent successfully. 148 * @tc.type: FUNC 149 */ 150HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_0400, TestSize.Level2) 151{ 152 ASSERT_NE(nullptr, g_iBtHci); 153 std::cout << "g_iBtHci005 = " << g_iBtHci << std::endl; 154 std::vector<uint8_t> cmd = COMMAND_HCI_READ_LOCAL_VERSION_INFORMATION; 155 BtType btType = BtType::HCI_CMD; 156 int32_t ret = g_iBtHci->SendHciPacket(btType, cmd); 157 std::cout << "HdiBluetoothSend_0400_ret = " << ret << std::endl; 158 EXPECT_EQ(HDF_SUCCESS, ret); 159} 160 161/** 162 * @tc.name: HdiBluetoothSend_0500 163 * @tc.desc: the unkonwn HCI_CMD packets is sent successfully. 164 * @tc.type: FUNC 165 */ 166HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_0500, TestSize.Level2) 167{ 168 ASSERT_NE(nullptr, g_iBtHci); 169 std::cout << "g_iBtHci006 = " << g_iBtHci << std::endl; 170 std::vector<uint8_t> cmd = COMMAND_HCI_SHOULD_BE_UNKNOWN; 171 BtType btType = BtType::HCI_CMD; 172 int32_t ret = g_iBtHci->SendHciPacket(btType, cmd); 173 std::cout << "HdiBluetoothSend_0500_ret = " << ret << std::endl; 174 EXPECT_EQ(HDF_SUCCESS, ret); 175} 176 177/** 178 * @tc.name: HdiBluetoothSend_0600 179 * @tc.desc: the buffer HCI_CMD packets is sent successfully. 180 * @tc.type: FUNC 181 */ 182HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_0600, TestSize.Level2) 183{ 184 ASSERT_NE(nullptr, g_iBtHci); 185 std::cout << "g_iBtHci007 = " << g_iBtHci << std::endl; 186 std::vector<uint8_t> cmd = COMMAND_HCI_READ_BUFFER_SIZE; 187 BtType btType = BtType::HCI_CMD; 188 int32_t ret = g_iBtHci->SendHciPacket(btType, cmd); 189 std::cout << "HdiBluetoothSend_0600_ret = " << ret << std::endl; 190 EXPECT_EQ(HDF_SUCCESS, ret); 191} 192 193/** 194 * @tc.name: HdiBluetoothSend_0700 195 * @tc.desc: the name HCI_CMD packets is sent successfully. 196 * @tc.type: FUNC 197 */ 198HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_0700, TestSize.Level2) 199{ 200 ASSERT_NE(nullptr, g_iBtHci); 201 std::cout << "g_iBtHci008 = " << g_iBtHci << std::endl; 202 // Send an HCI packet 203 std::vector<uint8_t> writeName = COMMAND_HCI_WRITE_LOCAL_NAME; 204 // With a name 205 char newName[] = "John Jacob Jingleheimer Schmidt ___________________0"; 206 size_t newNameLength = strlen(newName); 207 for (size_t i = 0; i < newNameLength; i++) 208 writeName.push_back(static_cast<uint8_t>(newName[i])); 209 // And the packet number 210 size_t i = newNameLength - 1; 211 size_t n = newNameLength; 212 for (int digits = n; digits > 0; digits = digits / 10, i--) 213 writeName[i] = static_cast<uint8_t>('0' + digits % 10); 214 // And padding 215 for (size_t i = 0; i < 248 - newNameLength; i++) 216 writeName.push_back(static_cast<uint8_t>(0)); 217 std::vector<uint8_t> cmd = writeName; 218 BtType btType = BtType::HCI_CMD; 219 int32_t ret = g_iBtHci->SendHciPacket(btType, cmd); 220 std::cout << "HdiBluetoothSend_0700_ret = " << ret << std::endl; 221 EXPECT_EQ(HDF_SUCCESS, ret); 222} 223 224/** 225 * @tc.name: HdiBluetoothSend_0800 226 * uint16_t handle; 227 * size_t size;@tc.desc: the HCI null HCI_CMD packets is sent successfully. 228 * @tc.type: FUNC 229 */ 230HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_0800, TestSize.Level2) 231{ 232 ASSERT_NE(nullptr, g_iBtHci); 233 std::cout << "g_iBtHci009 = " << g_iBtHci << std::endl; 234 BtType btType = BtType::HCI_CMD; 235 std::vector<uint8_t> data; 236 int32_t ret = g_iBtHci->SendHciPacket(btType, data); 237 std::cout << "HdiBluetoothSend_0800_ret = " << ret << std::endl; 238 EXPECT_EQ(HDF_FAILURE, ret); 239} 240 241/** 242 * @tc.name: HdiBluetoothSend_0900 243 * @tc.desc: the HCI SCO_DATA packets is sent successfully. 244 * @tc.type: FUNC 245 */ 246HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_0900, TestSize.Level2) 247{ 248 ASSERT_NE(nullptr, g_iBtHci); 249 std::cout << "g_iBtHci100 = " << g_iBtHci << std::endl; 250 int n = 0; 251 std::vector<uint8_t> sco_vector; 252 sco_vector.push_back(static_cast<uint8_t>(0xff)); 253 sco_vector.push_back(static_cast<uint8_t>((0x0f00) >> 8)); 254 sco_vector.push_back(static_cast<uint8_t>(0xff)); 255 sco_vector.push_back(static_cast<uint8_t>((0xff00) >> 8)); 256 for (size_t i = 0; i < 10; i++) { 257 sco_vector.push_back(static_cast<uint8_t>(i + n)); 258 } 259 BtType btType = BtType::SCO_DATA; 260 int32_t ret = g_iBtHci->SendHciPacket(btType, sco_vector); 261 std::cout << "HdiBluetoothSend_0900_ret = " << ret << std::endl; 262 EXPECT_EQ(HDF_SUCCESS, ret); 263} 264 265/** 266 * @tc.name: HdiBluetoothSend_1000 267 * @tc.desc: the HCI SCO_DATA packets is sent successfully. 268 * @tc.type: FUNC 269 */ 270HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_1000, TestSize.Level2) 271{ 272 ASSERT_NE(nullptr, g_iBtHci); 273 std::cout << "g_iBtHci110 = " << g_iBtHci << std::endl; 274 BtType btType = BtType::SCO_DATA; 275 std::vector<uint8_t> data; 276 int32_t ret = g_iBtHci->SendHciPacket(btType, data); 277 std::cout << "HdiBluetoothSend_1000_ret = " << ret << std::endl; 278 EXPECT_EQ(HDF_FAILURE, ret); 279} 280 281/** 282 * @tc.name: HdiBluetoothSend_1100 283 * @tc.desc: the HCI HCI_EVENT packets is sent successfully. 284 * @tc.type: FUNC 285 */ 286HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_1100, TestSize.Level2) 287{ 288 ASSERT_NE(nullptr, g_iBtHci); 289 std::cout << "g_iBtHci120 = " << g_iBtHci << std::endl; 290 BtType btType = BtType::HCI_EVENT; 291 std::vector<uint8_t> data; 292 int32_t ret = g_iBtHci->SendHciPacket(btType, data); 293 std::cout << "HdiBluetoothSend_1100_ret = " << ret << std::endl; 294 EXPECT_EQ(HDF_FAILURE, ret); 295} 296 297/** 298 * @tc.name: HdiBluetoothSend_1200 299 * @tc.desc: the HCI ISO_DATA packets is sent successfully. 300 * @tc.type: FUNC 301 */ 302HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothSend_1200, TestSize.Level2) 303{ 304 ASSERT_NE(nullptr, g_iBtHci); 305 std::cout << "g_iBtHci130 = " << g_iBtHci << std::endl; 306 BtType btType = BtType::ISO_DATA; 307 std::vector<uint8_t> data; 308 int32_t ret = g_iBtHci->SendHciPacket(btType, data); 309 std::cout << "HdiBluetoothSend_1200_ret = " << ret << std::endl; 310 EXPECT_EQ(HDF_FAILURE, ret); 311} 312 313/** 314 * @tc.name: HdiBluetoothClose_0800 315 * @tc.desc: Disable the HCI interface. 316 * @tc.type: FUNC 317 */ 318HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothClose_0100, TestSize.Level3) 319{ 320 ASSERT_NE(nullptr, g_iBtHci); 321 std::cout << "g_iBtHci140 = " << g_iBtHci << std::endl; 322 int32_t ret = g_iBtHci->Close(); 323 std::cout << "diBluetoothClose_0100_ret = " << ret << std::endl; 324 EXPECT_EQ(HDF_SUCCESS, ret); 325} 326 327/** 328 * @tc.name: HdiBluetoothHciType_0100 329 * @tc.desc: the Hdibluetooth hci enum types test 330 * @tc.type: FUNC 331 */ 332 333HWTEST_F(HdfBluetoothHdiTest, SUB_DriverSystem_HdiBluetoothHciType_0100, TestSize.Level2) 334{ 335 BtStatus btstatus_success = BtStatus::SUCCESS; 336 std::cout << "BtStatus SUCCESS = " << btstatus_success << std::endl; 337 EXPECT_EQ(btstatus_success, 0); 338 339 BtStatus btstatus_initialError = BtStatus::INITIAL_ERROR; 340 std::cout << "BtStatus INITIAL_ERROR = " << btstatus_initialError << std::endl; 341 EXPECT_EQ(btstatus_initialError, 1); 342 343 BtStatus btstatus_unknown = BtStatus::UNKNOWN; 344 std::cout << "BtStatus UNKNOWN = " << btstatus_unknown << std::endl; 345 EXPECT_EQ(btstatus_unknown, 2); 346} 347