1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "bt_headless_sdp"
18
19 #include <future>
20
21 #include "base/logging.h" // LOG() stdout and android log
22 #include "osi/include/log.h" // android log only
23 #include "stack/include/sdp_api.h"
24 #include "test/headless/get_options.h"
25 #include "test/headless/headless.h"
26 #include "test/headless/sdp/sdp.h"
27 #include "test/headless/sdp/sdp_db.h"
28 #include "types/raw_address.h"
29
30 using namespace bluetooth::test::headless;
31
bta_jv_start_discovery_callback(tSDP_STATUS result,void * user_data)32 static void bta_jv_start_discovery_callback(tSDP_STATUS result,
33 void* user_data) {
34 auto promise = static_cast<std::promise<uint16_t>*>(user_data);
35 promise->set_value(result);
36 }
37
38 namespace {
39
40 struct sdp_error_code_s {
41 const char* name;
42 uint16_t error_code;
43 } sdp_error_code[] = {
44 {"KsdpSuccess", 0},
45 {"KsdpInvalidVersion", 0x0001},
46 {"KsdpInvalidServRecHdl", 0x0002},
47 {"KsdpInvalidReqSyntax", 0x0003},
48 {"KsdpInvalidPduSize", 0x0004},
49 {"KsdpInvalidContState", 0x0005},
50 {"KsdpNoResources", 0x0006},
51 {"KsdpDiRegFailed", 0x0007},
52 {"KsdpDiDiscFailed", 0x0008},
53 {"KsdpNoDiRecordFound", 0x0009},
54 {"KsdpErrAttrNotPresent", 0x000a},
55 {"KsdpIllegalParameter", 0x000b},
56 {"KsdpNoRecsMatch", 0xFFF0},
57 {"KsdpConnFailed", 0xFFF1},
58 {"KsdpCfgFailed", 0xFFF2},
59 {"KsdpGenericError", 0xFFF3},
60 {"KsdpDbFull", 0xFFF4},
61 {"KsdpInvalidPdu", 0xFFF5},
62 {"KsdpSecurityErr", 0xFFF6},
63 {"KsdpConnRejected", 0xFFF7},
64 {"KsdpCancel", 0xFFF8},
65 };
66
67 const char* kUnknownText = "Unknown";
68
SdpErrorCodeToString(uint16_t code)69 const char* SdpErrorCodeToString(uint16_t code) {
70 for (size_t i = 0; i < sizeof(sdp_error_code) / sizeof(sdp_error_code_s);
71 ++i) {
72 if (sdp_error_code[i].error_code == code) {
73 return sdp_error_code[i].name;
74 }
75 }
76 return kUnknownText;
77 }
78
79 constexpr size_t kMaxDiscoveryRecords = 64;
80
sdp_query_uuid(unsigned int num_loops,const RawAddress & raw_address,const bluetooth::Uuid & uuid)81 int sdp_query_uuid(unsigned int num_loops, const RawAddress& raw_address,
82 const bluetooth::Uuid& uuid) {
83 SdpDb sdp_discovery_db(kMaxDiscoveryRecords);
84
85 if (!SDP_InitDiscoveryDb(sdp_discovery_db.RawPointer(),
86 sdp_discovery_db.Length(),
87 1, // num_uuid,
88 &uuid, 0, nullptr)) {
89 fprintf(stdout, "%s Unable to initialize sdp discovery\n", __func__);
90 return -1;
91 }
92
93 std::promise<uint16_t> promise;
94 auto future = promise.get_future();
95
96 sdp_discovery_db.Print(stdout);
97
98 if (!SDP_ServiceSearchAttributeRequest2(
99 raw_address, sdp_discovery_db.RawPointer(),
100 bta_jv_start_discovery_callback, (void*)&promise)) {
101 fprintf(stdout, "%s Failed to start search attribute request\n", __func__);
102 return -2;
103 }
104
105 uint16_t result = future.get();
106 if (result != 0) {
107 fprintf(stdout, "Failed search discovery result:%s\n",
108 SdpErrorCodeToString(result));
109 return result;
110 }
111
112 tSDP_DISC_REC* rec = SDP_FindServiceInDb(sdp_discovery_db.RawPointer(),
113 uuid.As16Bit(), nullptr);
114 if (rec == nullptr) {
115 fprintf(stdout, "discovery record is null from:%s uuid:%s\n",
116 raw_address.ToString().c_str(), uuid.ToString().c_str());
117 } else {
118 fprintf(stdout, "result:%d attr_id:%x from:%s uuid:%s\n", result,
119 rec->p_first_attr->attr_id, rec->remote_bd_addr.ToString().c_str(),
120 uuid.ToString().c_str());
121 }
122 return 0;
123 }
124
125 } // namespace
126
Run()127 int bluetooth::test::headless::Sdp::Run() {
128 if (options_.loop_ < 1) {
129 printf("This test requires at least a single loop\n");
130 options_.Usage();
131 return -1;
132 }
133 if (options_.device_.size() != 1) {
134 printf("This test requires a single device specified\n");
135 options_.Usage();
136 return -1;
137 }
138 if (options_.uuid_.size() != 1) {
139 printf("This test requires a single uuid specified\n");
140 options_.Usage();
141 return -1;
142 }
143
144 return RunOnHeadlessStack<int>([this]() {
145 return sdp_query_uuid(options_.loop_, options_.device_.front(),
146 options_.uuid_.front());
147 });
148 }
149