xref: /aosp_15_r20/system/chre/apps/nearby/location/lbs/contexthub/nanoapps/nearby/presence_service_data.h (revision 84e339476a462649f82315436d70fd732297a399)
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_SERVICE_DATA_H_
18 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_PRESENCE_SERVICE_DATA_H_
19 
20 #include <stdint.h>
21 
22 #include "third_party/contexthub/chre/util/include/chre/util/non_copyable.h"
23 #include "third_party/contexthub/chre/util/include/chre/util/optional.h"
24 
25 namespace nearby {
26 static constexpr uint8_t kFpAccountKeyLength = 16;
27 static constexpr uint8_t kFpAccountKeySaltLength = 2;
28 static constexpr uint8_t kFpAccountKeyFilterLength = 9;
29 static constexpr uint8_t kFpAccountKeyDataLength =
30     kFpAccountKeySaltLength + kFpAccountKeyFilterLength;
31 static constexpr uint8_t kFpModelIdLength = 3;
32 static constexpr uint8_t kFpBatteryStatusLength = 3;
33 
34 // Represents a Nearby service data in BLE advertisement.
35 struct PresenceServiceData {
36  public:
37   static constexpr uint16_t kUuid = 0xFCF1;
38 
39   // Returns Presence service data by parsing data, which is an encoded byte
40   // following the spec (go/nearby-presence-spec).
41   // Returns no value if parse fails. Callee keeps the ownership of data.
42   static chre::Optional<PresenceServiceData> Parse(const uint8_t data[],
43                                                    const uint16_t size);
44   chre::Optional<uint8_t> first_intent;
45   chre::Optional<uint8_t> second_intent;
46   bool has_fp_model_id = false;
47   uint8_t fp_model_id[kFpModelIdLength];
48   bool has_fp_account_key_data = false;
49   uint8_t fp_account_key_salt[kFpAccountKeySaltLength];
50   uint8_t fp_account_key_filter[kFpAccountKeyFilterLength];
51   bool has_battery_status = false;
52   uint8_t fp_battery_status[kFpBatteryStatusLength];
53 };
54 
55 // Represents a field header inside a Nearby service data.
56 struct PresenceFieldHeader {
57  public:
58   // Constructs a Field Header for Presence service data from a header and an
59   // optional extension byte.
PresenceFieldHeaderPresenceFieldHeader60   PresenceFieldHeader(const uint8_t header, const uint8_t extension) {
61     type = header & 0x0F;
62     if (type == kExtensionType) {
63       type = extension & 0x0F;
64       length = ((header & 0xF0) >> 2) + ((extension & 0xC0) >> 6);
65     } else {
66       length = (header & 0xF0) >> 4;
67     }
68   }
69 
70   // Constants defining the Presence data element type, sorted by their value.
71   static constexpr uint8_t kIntentType = 0b0101;
72   // Fast Pair model ID.
73   static constexpr uint8_t kFpModelIdType = 0b0111;
74   // Fast Pair Account Key data includes both SALT and Bloom filter.
75   static constexpr uint8_t kFpAccountKeyDataType = 0b1001;
76   static constexpr uint8_t kBatteryStatusType = 0b1011;
77   static constexpr uint8_t kExtensionType = 0b1111;
78 
79   // length of data element value.
80   uint8_t length;
81   // type of data element.
82   uint8_t type;
83 };
84 
85 }  // namespace nearby
86 
87 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_PRESENCE_SERVICE_DATA_H_
88