• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ssid.h"
18 #include "chre/apps/wifi_offload/utility.h"
19 
20 namespace wifi_offload {
21 
Ssid(const Ssid & other)22 Ssid::Ssid(const Ssid &other) {
23   SetData(other.ssid_vec_.data(), other.ssid_vec_.size());
24 }
25 
SetData(const uint8_t * buff,size_t len)26 void Ssid::SetData(const uint8_t *buff, size_t len) {
27   if (len > kMaxSsidLen) {
28     LOGE("Ssid buffer len %zu larger than max ssid len %zu. Truncating.", len,
29          kMaxSsidLen);
30     len = kMaxSsidLen;
31   }
32 
33   ssid_vec_.clear();
34   ssid_vec_.reserve(len);
35   for (size_t i = 0; i < len; i++) {
36     ssid_vec_.push_back(buff[i]);
37   }
38 }
39 
operator ==(const Ssid & other) const40 bool Ssid::operator==(const Ssid &other) const {
41   return ssid_vec_ == other.ssid_vec_;
42 }
43 
Serialize(flatbuffers::FlatBufferBuilder * builder) const44 flatbuffers::Offset<Ssid::FbsType> Ssid::Serialize(
45     flatbuffers::FlatBufferBuilder *builder) const {
46   return fbs::CreateSsid(*builder, builder->CreateVector(ssid_vec_));
47 }
48 
Deserialize(const Ssid::FbsType & fbs_ssid)49 bool Ssid::Deserialize(const Ssid::FbsType &fbs_ssid) {
50   const auto &ssid_vec = fbs_ssid.ssid();
51   if (ssid_vec == nullptr) {
52     LOGE("Failed to deserialize Ssid. Null or incomplete members.");
53     return false;
54   }
55 
56   if (ssid_vec->size() > kMaxSsidLen) {
57     LOGE("Failed to deserialize Ssid. Ssid size is larger than max len.");
58     return false;
59   }
60 
61   SetData(ssid_vec->data(), ssid_vec->size());
62   return true;
63 }
64 
Log() const65 void Ssid::Log() const {
66   utility::LogSsid(ssid_vec_.data(), static_cast<uint8_t>(ssid_vec_.size()));
67 }
68 
ToChreWifiSsidListItem(chreWifiSsidListItem * chre_ssid) const69 void Ssid::ToChreWifiSsidListItem(chreWifiSsidListItem *chre_ssid) const {
70   if (chre_ssid == nullptr) {
71     LOGW("Failed to convert to chreWifiSsidListItem. Output pointer is null");
72     return;
73   }
74 
75   std::memcpy(chre_ssid->ssid, ssid_vec_.data(), ssid_vec_.size());
76   chre_ssid->ssidLen = static_cast<uint8_t>(ssid_vec_.size());
77 }
78 
79 }  // namespace wifi_offload
80