Home | History | Annotate | Download | only in lib
      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 #include "common/vsoc/lib/wifi_exchange_view.h"
     17 
     18 #include <algorithm>
     19 #include <string>
     20 
     21 #include <linux/if_ether.h>
     22 #include "common/vsoc/lib/circqueue_impl.h"
     23 
     24 namespace vsoc {
     25 namespace wifi {
     26 
     27 intptr_t WifiExchangeView::Send(const void* buffer, intptr_t length) {
     28 #ifdef CUTTLEFISH_HOST
     29   return data()->guest_ingress.Write(this, static_cast<const char*>(buffer),
     30                                      length);
     31 #else
     32   return data()->guest_egress.Write(this, static_cast<const char*>(buffer),
     33                                     length);
     34 #endif
     35 }
     36 
     37 intptr_t WifiExchangeView::Recv(void* buffer, intptr_t max_length) {
     38 #ifdef CUTTLEFISH_HOST
     39   return data()->guest_egress.Read(this, static_cast<char*>(buffer),
     40                                    max_length);
     41 #else
     42   return data()->guest_ingress.Read(this, static_cast<char*>(buffer),
     43                                     max_length);
     44 #endif
     45 }
     46 
     47 void WifiExchangeView::SetGuestMACAddress(
     48     const WifiExchangeView::MacAddress& mac_address) {
     49   std::copy(std::begin(mac_address),
     50             std::end(mac_address),
     51             std::begin(data()->guest_mac_address));
     52 }
     53 
     54 WifiExchangeView::MacAddress WifiExchangeView::GetGuestMACAddress() {
     55   WifiExchangeView::MacAddress ret;
     56   std::copy(std::begin(data()->guest_mac_address),
     57             std::end(data()->guest_mac_address),
     58             std::begin(ret));
     59   return ret;
     60 }
     61 
     62 void WifiExchangeView::SetHostMACAddress(
     63     const WifiExchangeView::MacAddress& mac_address) {
     64   std::copy(std::begin(mac_address),
     65             std::end(mac_address),
     66             std::begin(data()->host_mac_address));
     67 }
     68 
     69 WifiExchangeView::MacAddress WifiExchangeView::GetHostMACAddress() {
     70   WifiExchangeView::MacAddress ret;
     71   std::copy(std::begin(data()->host_mac_address),
     72             std::end(data()->host_mac_address),
     73             std::begin(ret));
     74   return ret;
     75 }
     76 
     77 // static
     78 bool WifiExchangeView::ParseMACAddress(const std::string& s,
     79                                        WifiExchangeView::MacAddress* mac) {
     80   char dummy;
     81   // This is likely to always be true, but better safe than sorry
     82   static_assert(std::tuple_size<WifiExchangeView::MacAddress>::value == 6,
     83                 "Mac address size has changed");
     84   if (sscanf(s.c_str(),
     85              "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx%c",
     86              &(*mac)[0],
     87              &(*mac)[1],
     88              &(*mac)[2],
     89              &(*mac)[3],
     90              &(*mac)[4],
     91              &(*mac)[5],
     92              &dummy) != 6) {
     93     return false;
     94   }
     95   return true;
     96 }
     97 
     98 // static
     99 std::string WifiExchangeView::MacAddressToString(
    100     const WifiExchangeView::MacAddress& mac) {
    101   char buffer[3 * mac.size()];
    102   // This is likely to always be true, but better safe than sorry
    103   static_assert(std::tuple_size<WifiExchangeView::MacAddress>::value == 6,
    104                 "Mac address size has changed");
    105   snprintf(buffer,
    106            sizeof(buffer),
    107            "%02x:%02x:%02x:%02x:%02x:%02x",
    108            mac[0],
    109            mac[1],
    110            mac[2],
    111            mac[3],
    112            mac[4],
    113            mac[5]);
    114   return std::string(buffer);
    115 }
    116 
    117 }  // namespace wifi
    118 }  // namespace vsoc
    119