• 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 #include "chre/core/ble_request_multiplexer.h"
18 
19 #include "chre/core/event_loop_manager.h"
20 
21 namespace chre {
22 
getMutableRequests()23 DynamicVector<BleRequest> &BleRequestMultiplexer::getMutableRequests() {
24   return mRequests;
25 }
26 
findRequest(uint16_t instanceId,size_t * index)27 const BleRequest *BleRequestMultiplexer::findRequest(uint16_t instanceId,
28                                                      size_t *index) {
29   for (size_t i = 0; i < mRequests.size(); ++i) {
30     if (mRequests[i].getInstanceId() == instanceId) {
31       if (index != nullptr) {
32         *index = i;
33       }
34       return &mRequests[i];
35     }
36   }
37   return nullptr;
38 }
39 
hasRequests(RequestStatus status) const40 bool BleRequestMultiplexer::hasRequests(RequestStatus status) const {
41   for (const BleRequest &request : mRequests) {
42     if (request.getRequestStatus() == status) {
43       return true;
44     }
45   }
46   return false;
47 }
48 
removeRequests(RequestStatus status)49 void BleRequestMultiplexer::removeRequests(RequestStatus status) {
50   bool requestRemoved = false;
51   size_t index = 0;
52   while (index < mRequests.size()) {
53     if (mRequests[index].getRequestStatus() == status) {
54       mRequests.erase(index);
55       requestRemoved = true;
56     } else {
57       // Only increment index if nothing is erased since erasing moves later
58       // elements down a spot so keeping the index the same is safe.
59       index++;
60     }
61   }
62 
63   if (requestRemoved) {
64     // Only update the maximal request after removing all needed requests to
65     // reduce allocations performed.
66     bool maximalRequestChanged = false;
67     updateMaximalRequest(&maximalRequestChanged);
68   }
69 }
70 
removeDisabledRequests()71 void BleRequestMultiplexer::removeDisabledRequests() {
72   size_t index = 0;
73   while (index < mRequests.size()) {
74     BleRequest &request = mRequests[index];
75     if (!request.isEnabled() &&
76         request.getRequestStatus() == RequestStatus::APPLIED) {
77       mRequests.erase(index);
78     } else {
79       // Only increment index if nothing is erased since erasing moves later
80       // elements down a spot so keeping the index the same is safe.
81       index++;
82     }
83   }
84 
85   // No need to update the maximal request after removing since disabled
86   // requests don't affect the maximal request.
87 }
88 
isMaximalRequestEnabled()89 bool BleRequestMultiplexer::isMaximalRequestEnabled() {
90   return getCurrentMaximalRequest().isEnabled();
91 }
92 
93 }  // namespace chre
94