• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 CHRE_CORE_BLE_REQUEST_H_
18 #define CHRE_CORE_BLE_REQUEST_H_
19 
20 #include "chre/util/dynamic_vector.h"
21 #include "chre/util/non_copyable.h"
22 #include "chre/util/system/debug_dump.h"
23 #include "chre_api/chre/ble.h"
24 
25 namespace chre {
26 
27 // Indicates what the current status of this request is w.r.t. its usage by
28 // the PAL.
29 enum class RequestStatus : uint8_t {
30   // Indicates the request is waiting to be sent to the PAL
31   PENDING_REQ,
32   // Indicates the request has been issued to the PAL, but hasn't received
33   // a response yet
34   PENDING_RESP,
35   // Indicates this request has been successfully applied by the PAL.
36   APPLIED,
37 };
38 
39 class BleRequest : public NonCopyable {
40  public:
41   BleRequest();
42 
43   BleRequest(uint16_t instanceId, bool enable, const void *cookie);
44 
45   BleRequest(uint16_t instanceId, bool enable, chreBleScanMode mode,
46              uint32_t reportDelayMs, const chreBleScanFilterV1_9 *filter,
47              const void *cookie);
48 
49   BleRequest(BleRequest &&other);
50 
51   BleRequest &operator=(BleRequest &&other);
52 
53   /**
54    * Merges current request with other request. Takes maximum value of mode and
55    * minimum value of reportDelayMs and rssiThreshold. Takes superset of generic
56    * filters from both requests.
57    *
58    * @param request The other request to compare the attributes of.
59    * @return true if any of the attributes of this request changed.
60    */
61   bool mergeWith(const BleRequest &request);
62 
63   /**
64    * Checks whether current request is equivalent to the other request.
65    *
66    * @param request The other request to compare the attributes of.
67    * @return true if the requests are equivalent.
68    */
69   bool isEquivalentTo(const BleRequest &request);
70 
71   /**
72    * @return The instance id of the nanoapp that owns this request
73    */
74   uint16_t getInstanceId() const;
75 
76   /**
77    * @return The scan mode of this request.
78    */
79   chreBleScanMode getMode() const;
80 
81   /**
82    * @return The report delay of this request.
83    */
84   uint32_t getReportDelayMs() const;
85 
86   /**
87    * @return The RSSI threshold of this request.
88    */
89   int8_t getRssiThreshold() const;
90 
91   /**
92    * @return The current status of this request.
93    */
94   RequestStatus getRequestStatus() const;
95 
96   /**
97    * @param status The status this request should be set to.
98    */
99   void setRequestStatus(RequestStatus status);
100 
101   /**
102    * @return Generic filters of this request.
103    */
104   const DynamicVector<chreBleGenericFilter> &getGenericFilters() const;
105 
106   /**
107    * @return Broadcaster address filters of this request.
108    */
109   const DynamicVector<chreBleBroadcasterAddressFilter> &getBroadcasterFilters()
110       const;
111 
112   /**
113    * @return The cookie this request.
114    */
115   const void *getCookie() const;
116 
117   /**
118    * @return chreBleScanFilterV1_9 that is valid only as long as the internal
119    *    contents of this class are not modified
120    */
121   chreBleScanFilterV1_9 getScanFilter() const;
122 
123   /**
124    * @return true if nanoapp intends to enable a request.
125    */
126   bool isEnabled() const;
127 
128   /**
129    * Prints state in a string buffer. Must only be called from the context of
130    * the main CHRE thread.
131    *
132    * @param debugDump The debug dump wrapper where a string can be printed
133    * into one of the buffers.
134    * @param isPlatformRequest true if the request to be logged was sent to the
135    * platform.
136    */
137   void logStateToBuffer(DebugDumpWrapper &debugDump,
138                         bool isPlatformRequest = false) const;
139 
140  private:
141   // Maximum requested batching delay in ms.
142   uint32_t mReportDelayMs;
143 
144   // Instance id of nanoapp that sent the request.
145   uint16_t mInstanceId;
146 
147   // Scanning mode selected among enum chreBleScanMode.
148   chreBleScanMode mMode;
149 
150   // Whether a nanoapp intends to enable this request. If set to false, the
151   // following members are invalid: mMode, mReportDelayMs, mGenericFilters.
152   bool mEnabled;
153 
154   // RSSI threshold filter.
155   int8_t mRssiThreshold;
156 
157   // The current status of this request. Note that this value is not considered
158   // when determining equivalence or whe merging to prevent extra churn by the
159   // request multiplexer.
160   RequestStatus mStatus;
161 
162   // Generic scan filters.
163   DynamicVector<chreBleGenericFilter> mGenericFilters;
164 
165   // Broadcaster address filters.
166   DynamicVector<chreBleBroadcasterAddressFilter> mBroadcasterFilters;
167 
168   // Cookie value included in this request, supplied by the nanoapp.
169   const void *mCookie;
170 };
171 
172 }  // namespace chre
173 
174 #endif  // CHRE_CORE_BLE_REQUEST_H_