• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test/headless/sdp/sdp.h"
20 
21 #include <future>
22 
23 #include "base/logging.h"     // LOG() stdout and android log
24 #include "osi/include/log.h"  // android log only
25 #include "stack/include/sdp_api.h"
26 #include "test/headless/get_options.h"
27 #include "test/headless/headless.h"
28 #include "test/headless/sdp/sdp_db.h"
29 #include "types/bluetooth/uuid.h"
30 #include "types/raw_address.h"
31 
32 using namespace bluetooth::test::headless;
33 
bta_jv_start_discovery_callback(tSDP_STATUS result,const void * user_data)34 static void bta_jv_start_discovery_callback(tSDP_STATUS result,
35                                             const void* user_data) {
36   auto promise =
37       static_cast<std::promise<uint16_t>*>(const_cast<void*>(user_data));
38   promise->set_value(result);
39 }
40 
41 namespace {
42 
43 struct sdp_error_code_s {
44   const char* name;
45   uint16_t error_code;
46 } sdp_error_code[] = {
47     {"KsdpSuccess", 0},
48     {"KsdpInvalidVersion", 0x0001},
49     {"KsdpInvalidServRecHdl", 0x0002},
50     {"KsdpInvalidReqSyntax", 0x0003},
51     {"KsdpInvalidPduSize", 0x0004},
52     {"KsdpInvalidContState", 0x0005},
53     {"KsdpNoResources", 0x0006},
54     {"KsdpDiRegFailed", 0x0007},
55     {"KsdpDiDiscFailed", 0x0008},
56     {"KsdpNoDiRecordFound", 0x0009},
57     {"KsdpErrAttrNotPresent", 0x000a},
58     {"KsdpIllegalParameter", 0x000b},
59     {"KsdpNoRecsMatch", 0xFFF0},
60     {"KsdpConnFailed", 0xFFF1},
61     {"KsdpCfgFailed", 0xFFF2},
62     {"KsdpGenericError", 0xFFF3},
63     {"KsdpDbFull", 0xFFF4},
64     {"KsdpInvalidPdu", 0xFFF5},
65     {"KsdpSecurityErr", 0xFFF6},
66     {"KsdpConnRejected", 0xFFF7},
67     {"KsdpCancel", 0xFFF8},
68 };
69 
70 const char* kUnknownText = "Unknown";
71 
SdpErrorCodeToString(uint16_t code)72 const char* SdpErrorCodeToString(uint16_t code) {
73   for (size_t i = 0; i < sizeof(sdp_error_code) / sizeof(sdp_error_code_s);
74        ++i) {
75     if (sdp_error_code[i].error_code == code) {
76       return sdp_error_code[i].name;
77     }
78   }
79   return kUnknownText;
80 }
81 
82 constexpr size_t kMaxDiscoveryRecords = 64;
83 
sdp_query_uuid(unsigned int num_loops,const RawAddress & raw_address,const bluetooth::Uuid & uuid)84 int sdp_query_uuid([[maybe_unused]] unsigned int num_loops,
85                    const RawAddress& raw_address, const bluetooth::Uuid& uuid) {
86   SdpDb sdp_discovery_db(kMaxDiscoveryRecords);
87 
88   if (!SDP_InitDiscoveryDb(sdp_discovery_db.RawPointer(),
89                            sdp_discovery_db.Length(),
90                            1,  // num_uuid,
91                            &uuid, 0, nullptr)) {
92     fprintf(stdout, "%s Unable to initialize sdp discovery\n", __func__);
93     return -1;
94   }
95 
96   std::promise<uint16_t> promise;
97   auto future = promise.get_future();
98 
99   sdp_discovery_db.Print(stdout);
100 
101   if (!SDP_ServiceSearchAttributeRequest2(
102           raw_address, sdp_discovery_db.RawPointer(),
103           bta_jv_start_discovery_callback, (void*)&promise)) {
104     fprintf(stdout, "%s Failed to start search attribute request\n", __func__);
105     return -2;
106   }
107 
108   uint16_t result = future.get();
109   if (result != 0) {
110     fprintf(stdout, "Failed search discovery result:%s\n",
111             SdpErrorCodeToString(result));
112     return result;
113   }
114 
115   tSDP_DISC_REC* rec = SDP_FindServiceInDb(sdp_discovery_db.RawPointer(),
116                                            uuid.As16Bit(), nullptr);
117   if (rec == nullptr) {
118     fprintf(stdout, "discovery record is null from:%s uuid:%s\n",
119             raw_address.ToString().c_str(), uuid.ToString().c_str());
120   } else {
121     fprintf(stdout, "result:%d attr_id:%x from:%s uuid:%s\n", result,
122             rec->p_first_attr->attr_id, rec->remote_bd_addr.ToString().c_str(),
123             uuid.ToString().c_str());
124   }
125   return 0;
126 }
127 
128 }  // namespace
129 
Run()130 int bluetooth::test::headless::Sdp::Run() {
131   if (options_.loop_ < 1) {
132     printf("This test requires at least a single loop\n");
133     options_.Usage();
134     return -1;
135   }
136   if (options_.device_.size() != 1) {
137     printf("This test requires a single device specified\n");
138     options_.Usage();
139     return -1;
140   }
141   if (options_.uuid_.size() != 1) {
142     printf("This test requires a single uuid specified\n");
143     options_.Usage();
144     return -1;
145   }
146 
147   return RunOnHeadlessStack<int>([this]() {
148     return sdp_query_uuid(options_.loop_, options_.device_.front(),
149                           options_.uuid_.front());
150   });
151 }
152