• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_PRESENCE_DECODER_V1_H_
18 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_PRESENCE_DECODER_V1_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 
23 #include "location/lbs/contexthub/nanoapps/nearby/byte_array.h"
24 #include "location/lbs/contexthub/nanoapps/nearby/crypto.h"
25 #include "location/lbs/contexthub/nanoapps/nearby/proto/ble_filter.nanopb.h"
26 #include "third_party/contexthub/chre/util/include/chre/util/dynamic_vector.h"
27 #include "third_party/contexthub/chre/util/include/chre/util/optional.h"
28 
29 namespace nearby {
30 
31 // Represents a Data Element header.
32 struct DataElementHeaderV1 {
33   // Decodes data and returns the first Data Element header.
34   // Returns no value if decoding fails.
35   static chre::Optional<DataElementHeaderV1> Decode(const uint8_t data[],
36                                                     size_t data_size);
37 
38   static constexpr uint64_t kSaltType = 0;
39   static constexpr uint64_t kPrivateIdentityType = 1;
40   static constexpr uint64_t kProvisionIdentityType = 4;
41   static constexpr uint64_t kTxPowerType = 5;
42   static constexpr uint64_t kActionType = 6;
43   static constexpr uint64_t kModelIdType = 7;
44   static constexpr uint64_t kConnectionStatusType = 10;
45   static constexpr uint64_t kBatteryStatusType = 11;
46 
47   static constexpr size_t kSaltLength = 2;
48   static constexpr size_t kIdentityLength = 16;
49   static constexpr size_t kTxPowerLength = 1;
50   static constexpr size_t kActionLength = 1;
51   static constexpr size_t kModelIdLength = 3;
52   static constexpr size_t kConnectionStatusLength = 3;
53   static constexpr size_t kBatteryStatusLength = 3;
54 
55   // length of data element value.
56   uint8_t length;
57   // length of header itself.
58   uint8_t header_length;
59   // type of data element.
60   uint64_t type;
61 };
62 
63 struct DataElement {
DataElementDataElement64   DataElement(nearby_DataElement_ElementType key, ByteArray value)
65       : key(key), value(value) {}
66 
67   nearby_DataElement_ElementType key;
68   ByteArray value;
69 };
70 
71 // PresenceDecoderV1 contains data fields specified by Presence V1.
72 struct PresenceDecoderV1 {
73   static constexpr size_t kMaxNumActions = 5;
74   static constexpr size_t kDecryptionOutputBufSize = 16 * 20;
75 
76   PresenceDecoderV1() = default;
77   // Decodes encoded_data which is a byte array encoded by following the
78   // Presence V1 specification. Returns true when decoding succeeds.
79   bool Decode(const ByteArray &encoded_data, const Crypto &crypto,
80               const ByteArray &key,
81               const ByteArray &metadata_encryption_key_tag);
82   // Helper function to decode Presence data elements from data.
83   // Returns true if decoding succeeds.
84   bool DecodeDataElements(uint8_t data[], size_t data_size);
85   // Returns true if valid Extended DE type.
86   bool IsValidExtDataElementsType(uint64_t type);
87 
88   // required fields.
89   uint8_t salt[DataElementHeaderV1::kSaltLength];
90   uint8_t identity[DataElementHeaderV1::kIdentityLength];
91 
92   // repeated fields.
93   uint8_t actions[kMaxNumActions];
94   size_t num_actions = 0;
95 
96   // optional fields. An empty field is defined as Zero length of ByteArray.
97   ByteArray tx_power;
98   ByteArray model_id;
99   ByteArray connection_status;
100   ByteArray battery_status;
101 
102   // Extended DE list.
103   chre::DynamicVector<DataElement> extended_des;
104 
105   // Sets to true after successfully decoding.
106   bool decoded = false;
107 
108  private:
109   // Decrypted buffer provide the underlying storage for optional fields.
110   uint8_t decryption_output_buffer[kDecryptionOutputBufSize];
111 };
112 
113 }  // namespace nearby
114 
115 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_PRESENCE_DECODER_V1_H_
116