• 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);
44 
45   BleRequest(uint16_t instanceId, bool enable, chreBleScanMode mode,
46              uint32_t reportDelayMs, const chreBleScanFilter *filter);
47 
48   BleRequest(BleRequest &&other);
49 
50   BleRequest &operator=(BleRequest &&other);
51 
52   /**
53    * Merges current request with other request. Takes maximum value of mode and
54    * minimum value of reportDelayMs and rssiThreshold. Takes superset of generic
55    * filters from both requests.
56    *
57    * @param request The other request to compare the attributes of.
58    * @return true if any of the attributes of this request changed.
59    */
60   bool mergeWith(const BleRequest &request);
61 
62   /**
63    * Checks whether current request is equivalent to the other request.
64    *
65    * @param request The other request to compare the attributes of.
66    * @return true if the requests are equivalent.
67    */
68   bool isEquivalentTo(const BleRequest &request);
69 
70   /**
71    * @return The instance id of the nanoapp that owns this request
72    */
73   uint16_t getInstanceId() const;
74 
75   /**
76    * @return The scan mode of this request.
77    */
78   chreBleScanMode getMode() const;
79 
80   /**
81    * @return The report delay of this request.
82    */
83   uint32_t getReportDelayMs() const;
84 
85   /**
86    * @return The RSSI threshold of this request.
87    */
88   int8_t getRssiThreshold() const;
89 
90   /**
91    * @return The current status of this request.
92    */
93   RequestStatus getRequestStatus() const;
94 
95   /**
96    * @param status The status this request should be set to.
97    */
98   void setRequestStatus(RequestStatus status);
99 
100   /**
101    * @return Generic filters of this request.
102    */
103   const DynamicVector<chreBleGenericFilter> &getGenericFilters() const;
104 
105   /**
106    * @return chreBleScanFilter that is valid only as long as the internal
107    *    contents of this class are not modified
108    */
109   chreBleScanFilter getScanFilter() const;
110 
111   /**
112    * @return true if nanoapp intends to enable a request.
113    */
114   bool isEnabled() const;
115 
116   /**
117    * Prints state in a string buffer. Must only be called from the context of
118    * the main CHRE thread.
119    *
120    * @param debugDump The debug dump wrapper where a string can be printed
121    * into one of the buffers.
122    * @param isPlatformRequest true if the request to be logged was sent to the
123    * platform.
124    */
125   void logStateToBuffer(DebugDumpWrapper &debugDump,
126                         bool isPlatformRequest = false) const;
127 
128  private:
129   // Maximum requested batching delay in ms.
130   uint32_t mReportDelayMs;
131 
132   // Instance id of nanoapp that sent the request.
133   uint16_t mInstanceId;
134 
135   // Scanning mode selected among enum chreBleScanMode.
136   chreBleScanMode mMode;
137 
138   // Whether a nanoapp intends to enable this request. If set to false, the
139   // following members are invalid: mMode, mReportDelayMs, mFilter.
140   bool mEnabled;
141 
142   // RSSI threshold filter.
143   int8_t mRssiThreshold;
144 
145   // The current status of this request. Note that this value is not considered
146   // when determining equivalence or whe merging to prevent extra churn by the
147   // request multiplexer.
148   RequestStatus mStatus;
149 
150   // Generic scan filters.
151   DynamicVector<chreBleGenericFilter> mFilters;
152 };
153 
154 }  // namespace chre
155 
156 #endif  // CHRE_CORE_BLE_REQUEST_H_