1 /*
2  * Copyright 2018 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 #pragma once
18 
19 #include <array>
20 #include <variant>
21 
22 #include "bluetooth/uuid.h"
23 #include "packet.h"
24 #include "sdp_common.h"
25 #include "stack/include/bt_types.h"
26 
27 namespace bluetooth {
28 namespace sdp {
29 
30 // A helper class that helps extract data element objects from SDP packets.
31 class DataElementReader {
32  public:
33   // If the DataElement contains monostate, that means parsing has failed.
34   using DataElement =
35       std::variant<std::monostate, bool, int8_t, int16_t, int32_t, int64_t,
36                    uint8_t, uint16_t, uint32_t, uint64_t, Octet16, Uuid,
37                    std::string, DataElementReader>;
38 
DataElementReader(Iterator begin,Iterator end)39   DataElementReader(Iterator begin, Iterator end) : it_(begin), end_(end){};
40 
41   // Get the next Data Element in the data. If reading fails for any reason,
42   // the DataElementReader becomes invalid and will continuously fail to read
43   // from that point onward.
44   DataElement ReadNext();
45 
46  private:
47   // Extraction Helpers
48   DataElement ParseFail();
49   template <class IntegerType>
50   DataElement ReadInteger();
51   DataElement ReadLargeInt();
52 
53   // Extraction Functions
54   DataElement ReadSignedInt(DataElementSize size);
55   DataElement ReadUnsignedInt(DataElementSize size);
56   DataElement ReadUuid(DataElementSize size);
57   DataElement ReadString(DataElementSize size);
58   DataElement ReadSequence(DataElementSize size);
59 
60   Iterator it_;
61   Iterator end_;
62 };
63 
64 }  // namespace sdp
65 }  // namespace bluetooth
66