Home | History | Annotate | Download | only in wifi_relay
      1 /*
      2  * Copyright (C) 2018 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 #pragma once
     18 
     19 #include "common/vsoc/lib/wifi_exchange_view.h"
     20 
     21 #include <errno.h>
     22 #include <functional>
     23 #include <map>
     24 #include <memory>
     25 #include <mutex>
     26 #include <netlink/netlink.h>
     27 
     28 struct Mac80211HwSim {
     29     using MacAddress = vsoc::wifi::WifiExchangeView::MacAddress;
     30 
     31     static constexpr size_t kMessageSizeMax = 128 * 1024;
     32 
     33     explicit Mac80211HwSim(const MacAddress &mac);
     34     Mac80211HwSim(const Mac80211HwSim &) = delete;
     35     Mac80211HwSim &operator=(const Mac80211HwSim &) = delete;
     36 
     37     virtual ~Mac80211HwSim() = default;
     38 
     39     int initCheck() const;
     40 
     41     int socketFd() const;
     42 
     43     void handlePacket();
     44 
     45     int mac80211Family() const { return mMac80211Family; }
     46     int nl80211Family() const { return mNl80211Family; }
     47 
     48     int addRemote(
     49             const MacAddress &mac,
     50             vsoc::wifi::WifiExchangeView *wifiExchange);
     51 
     52     void removeRemote(const MacAddress &mac);
     53 
     54 private:
     55     struct Remote {
     56         explicit Remote(
     57             Mac80211HwSim *parent,
     58             vsoc::wifi::WifiExchangeView *wifiExchange);
     59 
     60         Remote(const Remote &) = delete;
     61         Remote &operator=(const Remote &) = delete;
     62 
     63         virtual ~Remote();
     64 
     65         intptr_t send(const void *data, size_t size);
     66 
     67     private:
     68         Mac80211HwSim *mParent;
     69         vsoc::wifi::WifiExchangeView *mWifiExchange;
     70         std::unique_ptr<vsoc::RegionWorker> mWifiWorker;
     71 
     72         volatile bool mDone = false;
     73         std::thread mThread;
     74     };
     75 
     76     int mInitCheck = -ENODEV;
     77     MacAddress mMAC;
     78     std::unique_ptr<nl_sock, void (*)(nl_sock *)> mSock;
     79     int mMac80211Family = 0;
     80     int mNl80211Family = 0;
     81 
     82     std::mutex mRemotesLock;
     83     std::map<MacAddress, std::unique_ptr<Remote>> mRemotes;
     84 
     85     void injectFrame(const void *data, size_t size);
     86     void ackFrame(nlmsghdr *msg);
     87     int registerOrSubscribe(const MacAddress &mac);
     88 };
     89