1 /*
2  * Copyright (C) 2021 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 "chpp/common/wifi_utils.h"
18 
19 #include <inttypes.h>
20 
21 #include "chpp/log.h"
22 
23 static uint8_t gResultTotal = 0;
24 static uint8_t gResultAcc = 0;
25 static uint8_t gExpectedIndex = 0;
26 
27 void chppCheckWifiScanEventNotificationReset() {
28   gResultTotal = 0;
29   gResultAcc = 0;
30   gExpectedIndex = 0;
31 }
32 
33 bool chppCheckWifiScanEventNotification(const struct chreWifiScanEvent *chre) {
34   bool success = true;
35 
36   if (chre->eventIndex != gExpectedIndex) {
37     CHPP_LOGE("Unexpected scan index %" PRIu8 " exp %" PRIu8, chre->eventIndex,
38               gExpectedIndex);
39     success = false;
40   }
41   if (chre->eventIndex == 0) {
42     if (gResultAcc < gResultTotal) {
43       CHPP_LOGE("Too few prev scan results (%" PRIu8 " missing)",
44                 gResultTotal - gResultAcc);
45       success = false;
46     }
47     gExpectedIndex = 0;
48     gResultAcc = 0;
49     gResultTotal = chre->resultTotal;
50   }
51 
52   if (gResultTotal != chre->resultTotal) {
53     CHPP_LOGE("Inconsistent result total %" PRIu8 " exp %" PRIu8,
54               chre->resultTotal, gResultTotal);
55     success = false;
56   }
57 
58   gResultAcc += chre->resultCount;
59 
60   if (gResultAcc >= gResultTotal) {
61     if (gResultAcc > gResultTotal) {
62       CHPP_LOGE("Too many scan results (%" PRIu8 " extra)",
63                 gResultAcc - gResultTotal);
64       success = false;
65     }
66     gExpectedIndex = 0;
67   } else {
68     gExpectedIndex++;
69   }
70 
71   return success;
72 }
73