1/*
2 * Copyright (C) 2016 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 "wificond/scanning/offload/scan_stats.h"
18
19#include <android-base/logging.h>
20
21#include "wificond/parcelable_utils.h"
22
23using android::status_t;
24
25namespace com {
26namespace android {
27namespace server {
28namespace wifi {
29namespace wificond {
30
31NativeScanStats::NativeScanStats(uint32_t num_scans_requested_by_wifi,
32                                 uint32_t num_scans_serviced_by_wifi,
33                                 uint32_t subscription_duration_ms,
34                                 uint32_t scan_duration_ms,
35                                 uint32_t num_channels_scanned,
36                                 std::vector<uint8_t> histogram_channels)
37    : num_scans_requested_by_wifi_(num_scans_requested_by_wifi),
38      num_scans_serviced_by_wifi_(num_scans_serviced_by_wifi),
39      subscription_duration_ms_(subscription_duration_ms),
40      scan_duration_ms_(scan_duration_ms),
41      num_channels_scanned_(num_channels_scanned),
42      time_stamp_(0),
43      histogram_channels_(histogram_channels) {}
44
45NativeScanStats::NativeScanStats()
46    : num_scans_requested_by_wifi_(0),
47      num_scans_serviced_by_wifi_(0),
48      subscription_duration_ms_(0),
49      scan_duration_ms_(0),
50      num_channels_scanned_(0),
51      time_stamp_(0) {}
52
53bool NativeScanStats::operator==(const NativeScanStats& rhs) const {
54  if ((rhs.num_scans_requested_by_wifi_ != num_scans_requested_by_wifi_) ||
55      (rhs.num_scans_serviced_by_wifi_ != num_scans_serviced_by_wifi_) ||
56      (rhs.scan_duration_ms_ != scan_duration_ms_) ||
57      (rhs.num_channels_scanned_ != num_channels_scanned_)) {
58    return false;
59  }
60  if (rhs.histogram_channels_.size() != histogram_channels_.size()) {
61    return false;
62  }
63  for (size_t i = 0; i < histogram_channels_.size(); i++) {
64    if (rhs.histogram_channels_[i] != histogram_channels_[i]) {
65      return false;
66    }
67  }
68  return true;
69}
70
71status_t NativeScanStats::writeToParcel(::android::Parcel* parcel) const {
72  RETURN_IF_FAILED(parcel->writeUint32(num_scans_requested_by_wifi_));
73  RETURN_IF_FAILED(parcel->writeUint32(num_scans_serviced_by_wifi_));
74  RETURN_IF_FAILED(parcel->writeUint32(subscription_duration_ms_));
75  RETURN_IF_FAILED(parcel->writeUint32(scan_duration_ms_));
76  RETURN_IF_FAILED(parcel->writeUint32(num_channels_scanned_));
77  RETURN_IF_FAILED(parcel->writeByteVector(histogram_channels_));
78  return ::android::OK;
79}
80
81status_t NativeScanStats::readFromParcel(const ::android::Parcel* parcel) {
82  RETURN_IF_FAILED(parcel->readUint32(&num_scans_requested_by_wifi_));
83  RETURN_IF_FAILED(parcel->readUint32(&num_scans_serviced_by_wifi_));
84  RETURN_IF_FAILED(parcel->readUint32(&subscription_duration_ms_));
85  RETURN_IF_FAILED(parcel->readUint32(&scan_duration_ms_));
86  RETURN_IF_FAILED(parcel->readUint32(&num_channels_scanned_));
87  RETURN_IF_FAILED(parcel->readByteVector(&histogram_channels_));
88  return ::android::OK;
89}
90
91void NativeScanStats::DebugLog() {
92  LOG(INFO) << "num_scans_requested_by_wifi=" << num_scans_requested_by_wifi_;
93  LOG(INFO) << "num_scans_serviced_by_wifi=" << num_scans_serviced_by_wifi_;
94  LOG(INFO) << "subscription_duration=" << subscription_duration_ms_;
95  LOG(INFO) << "scan_duration_ms_=" << scan_duration_ms_;
96  LOG(INFO) << "num_channels_scanned=" << num_channels_scanned_;
97  for (size_t i = 0; i < histogram_channels_.size(); i++) {
98    if (histogram_channels_[i] > 0) {
99      LOG(INFO) << "Channel=" << i << " ScannedTimes="
100                << static_cast<uint32_t>(histogram_channels_[i]);
101    }
102  }
103}
104
105}  // namespace wificond
106}  // namespace wifi
107}  // namespace server
108}  // namespace android
109}  // namespace com
110