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