1 /*
2 * Copyright 2024 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 #include <base/functional/bind.h>
18
19 #include <unordered_map>
20
21 #include "bta/include/bta_gatt_api.h"
22 #include "bta/include/bta_ras_api.h"
23 #include "bta/ras/ras_types.h"
24 #include "os/log.h"
25 #include "stack/include/bt_types.h"
26 #include "stack/include/btm_ble_addr.h"
27 #include "stack/include/gap_api.h"
28 #include "types/bluetooth/uuid.h"
29
30 using namespace bluetooth;
31 using namespace ::ras;
32 using namespace ::ras::uuid;
33
34 namespace ras {
getUuidName(const bluetooth::Uuid & uuid)35 std::string uuid::getUuidName(const bluetooth::Uuid& uuid) {
36 switch (uuid.As16Bit()) {
37 case kRangingService16Bit:
38 return "Ranging Service";
39 case kRasFeaturesCharacteristic16bit:
40 return "RAS Features";
41 case kRasRealTimeRangingDataCharacteristic16bit:
42 return "Real-time Ranging Data";
43 case kRasOnDemandDataCharacteristic16bit:
44 return "On-demand Ranging Data";
45 case kRasControlPointCharacteristic16bit:
46 return "RAS Control Point (RAS-CP)";
47 case kRasRangingDataReadyCharacteristic16bit:
48 return "Ranging Data Ready";
49 case kRasRangingDataOverWrittenCharacteristic16bit:
50 return "Ranging Data Overwritten";
51 case kClientCharacteristicConfiguration16bit:
52 return "Client Characteristic Configuration";
53 default:
54 return "Unknown UUID";
55 }
56 }
57
ParseControlPointCommand(ControlPointCommand * command,const uint8_t * value,uint16_t len)58 bool ParseControlPointCommand(ControlPointCommand* command,
59 const uint8_t* value, uint16_t len) {
60 // Check for minimum expected length
61 switch (value[0]) {
62 case (uint8_t)Opcode::ABORT_OPERATION:
63 break;
64 case (uint8_t)Opcode::PCT_FORMAT: {
65 if (len < 2) {
66 return false;
67 }
68 } break;
69 case (uint8_t)Opcode::GET_RANGING_DATA:
70 case (uint8_t)Opcode::ACK_RANGING_DATA:
71 case (uint8_t)Opcode::FILTER: {
72 if (len < 3) {
73 return false;
74 }
75 } break;
76 case (uint8_t)Opcode::RETRIEVE_LOST_RANGING_DATA_SEGMENTS: {
77 if (len < 5) {
78 return false;
79 }
80 } break;
81 default:
82 log::warn("unknown opcode 0x{:02x}", value[0]);
83 return false;
84 }
85 command->opcode_ = static_cast<Opcode>(value[0]);
86 std::memcpy(command->parameter_, value + 1, len - 1);
87 return true;
88 }
89
GetOpcodeText(Opcode opcode)90 std::string GetOpcodeText(Opcode opcode) {
91 switch (opcode) {
92 case Opcode::GET_RANGING_DATA:
93 return "GET_RANGING_DATA";
94 case Opcode::ACK_RANGING_DATA:
95 return "ACK_RANGING_DATA";
96 case Opcode::RETRIEVE_LOST_RANGING_DATA_SEGMENTS:
97 return "RETRIEVE_LOST_RANGING_DATA_SEGMENTS";
98 case Opcode::ABORT_OPERATION:
99 return "ABORT_OPERATION";
100 case Opcode::FILTER:
101 return "FILTER";
102 case Opcode::PCT_FORMAT:
103 return "PCT_FORMAT";
104 default:
105 return "Unknown Opcode";
106 }
107 }
108
GetResponseOpcodeValueText(ResponseCodeValue response_code_value)109 std::string GetResponseOpcodeValueText(ResponseCodeValue response_code_value) {
110 switch (response_code_value) {
111 case ResponseCodeValue::RESERVED_FOR_FUTURE_USE:
112 return "RESERVED_FOR_FUTURE_USE";
113 case ResponseCodeValue::SUCCESS:
114 return "SUCCESS";
115 case ResponseCodeValue::OP_CODE_NOT_SUPPORTED:
116 return "OP_CODE_NOT_SUPPORTED";
117 case ResponseCodeValue::INVALID_OPERATOR:
118 return "INVALID_OPERATOR";
119 case ResponseCodeValue::OPERATOR_NOT_SUPPORTED:
120 return "OPERATOR_NOT_SUPPORTED";
121 case ResponseCodeValue::INVALID_OPERAND:
122 return "INVALID_OPERAND";
123 case ResponseCodeValue::ABORT_UNSUCCESSFUL:
124 return "ABORT_UNSUCCESSFUL";
125 case ResponseCodeValue::PROCEDURE_NOT_COMPLETED:
126 return "PROCEDURE_NOT_COMPLETED";
127 case ResponseCodeValue::OPERAND_NOT_SUPPORTED:
128 return "OPERAND_NOT_SUPPORTED";
129 case ResponseCodeValue::NO_RECORDS_FOUND:
130 return "NO_RECORDS_FOUND";
131 default:
132 return "Unknown Opcode";
133 }
134 }
135
IsRangingServiceCharacteristic(const bluetooth::Uuid & uuid)136 bool IsRangingServiceCharacteristic(const bluetooth::Uuid& uuid) {
137 if (!uuid.Is16Bit()) {
138 return false;
139 }
140 switch (uuid.As16Bit()) {
141 case kRangingService16Bit:
142 case kRasFeaturesCharacteristic16bit:
143 case kRasRealTimeRangingDataCharacteristic16bit:
144 case kRasOnDemandDataCharacteristic16bit:
145 case kRasControlPointCharacteristic16bit:
146 case kRasRangingDataReadyCharacteristic16bit:
147 case kRasRangingDataOverWrittenCharacteristic16bit:
148 return true;
149 default:
150 return false;
151 }
152 }
153
154 } // namespace ras
155