Home | History | Annotate | Download | only in wifi_offload
      1 /*
      2  * Copyright (C) 2017 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/apps/wifi_offload/scan_stats.h"
     18 #include "chre/apps/wifi_offload/vector_serialization.h"
     19 
     20 namespace wifi_offload {
     21 
     22 ScanStats::ScanStats()
     23     : num_scans_requested_by_nanoapp_(0),
     24       num_scans_serviced_by_hardware_(0),
     25       num_scans_serviced_by_cache_(0),
     26       updated_at_chre_ms_(0),
     27       sent_at_chre_ms_(0),
     28       last_subscription_duration_ms_(0) {}
     29 
     30 bool ScanStats::operator==(const ScanStats &other) const {
     31   if (this == &other) {
     32     return true;
     33   }
     34   return num_scans_requested_by_nanoapp_ ==
     35              other.num_scans_requested_by_nanoapp_ &&
     36          num_scans_serviced_by_hardware_ ==
     37              other.num_scans_serviced_by_hardware_ &&
     38          num_scans_serviced_by_cache_ == other.num_scans_serviced_by_cache_ &&
     39          updated_at_chre_ms_ == other.updated_at_chre_ms_ &&
     40          sent_at_chre_ms_ == other.sent_at_chre_ms_ &&
     41          last_subscription_duration_ms_ ==
     42              other.last_subscription_duration_ms_ &&
     43          channel_histogram_ == other.channel_histogram_ &&
     44          scan_records_ == other.scan_records_ &&
     45          rpc_log_records_ == other.rpc_log_records_;
     46 }
     47 
     48 flatbuffers::Offset<ScanStats::FbsType> ScanStats::Serialize(
     49     flatbuffers::FlatBufferBuilder *builder) const {
     50   auto histo = channel_histogram_.Serialize(builder);
     51   auto scan_recs = SerializeVector(scan_records_, builder);
     52   auto log_recs = SerializeVector(rpc_log_records_, builder);
     53   return fbs::CreateScanStats(*builder, num_scans_requested_by_nanoapp_,
     54                               num_scans_serviced_by_hardware_,
     55                               num_scans_serviced_by_cache_, updated_at_chre_ms_,
     56                               sent_at_chre_ms_, last_subscription_duration_ms_,
     57                               histo, scan_recs, log_recs);
     58 }
     59 
     60 bool ScanStats::Deserialize(const ScanStats::FbsType &fbs_stats) {
     61   const auto &histo = fbs_stats.channel_scan_count();
     62   if (histo == nullptr || !channel_histogram_.Deserialize(*histo)) {
     63     LOGE("Failed to deserialize ScanStats. Null or incomplete members.");
     64     return false;
     65   }
     66 
     67   const auto &scan_recs = fbs_stats.scan_records();
     68   if (scan_recs == nullptr ||
     69       !DeserializeVector<ScanRecord>(*scan_recs, &scan_records_)) {
     70     LOGE("Failed to deserialize ScanStats. Null or incomplete members.");
     71     return false;
     72   }
     73 
     74   const auto &log_recs = fbs_stats.rpc_log_records();
     75   if (log_recs == nullptr ||
     76       !DeserializeVector<RpcLogRecord>(*log_recs, &rpc_log_records_)) {
     77     LOGE("Failed to deserialize ScanStats. Null or incomplete members.");
     78     return false;
     79   }
     80 
     81   num_scans_requested_by_nanoapp_ = fbs_stats.num_scans_requested_by_nanoapp();
     82   num_scans_serviced_by_hardware_ = fbs_stats.num_scans_serviced_by_hardware();
     83   num_scans_serviced_by_cache_ = fbs_stats.num_scans_serviced_by_cache();
     84   updated_at_chre_ms_ = fbs_stats.updated_at_chre_ms();
     85   sent_at_chre_ms_ = fbs_stats.sent_at_chre_ms();
     86   last_subscription_duration_ms_ = fbs_stats.last_subscription_duration_ms();
     87   return true;
     88 }
     89 
     90 }  // namespace wifi_offload
     91