• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "wifi/wifi_facade.h"
16 
17 #include "util/log.h"
18 
19 namespace netsim::wifi {
20 namespace {
21 // To detect bugs of misuse of chip_id more efficiently.
22 const int kGlobalChipStartIndex = 2000;
23 
24 class ChipInfo {
25  public:
26   uint32_t simulation_device;
27   std::shared_ptr<model::Chip::Radio> model;
28 
ChipInfo(uint32_t simulation_device,std::shared_ptr<model::Chip::Radio> model)29   ChipInfo(uint32_t simulation_device,
30            std::shared_ptr<model::Chip::Radio> model)
31       : simulation_device(simulation_device), model(std::move(model)) {}
32 };
33 
34 std::unordered_map<uint32_t, std::shared_ptr<ChipInfo>> id_to_chip_info_;
35 
ChangedState(model::State a,model::State b)36 bool ChangedState(model::State a, model::State b) {
37   return (b != model::State::UNKNOWN && a != b);
38 }
39 
IncrTx(uint32_t id)40 void IncrTx(uint32_t id) {
41   if (auto it = id_to_chip_info_.find(id); it != id_to_chip_info_.end()) {
42     auto &model = it->second->model;
43     model->set_tx_count(model->tx_count() + 1);
44   }
45 }
46 
IncrRx(uint32_t id)47 void IncrRx(uint32_t id) {
48   if (auto it = id_to_chip_info_.find(id); it != id_to_chip_info_.end()) {
49     auto &model = it->second->model;
50     model->set_rx_count(model->rx_count() + 1);
51   }
52 }
53 
54 }  // namespace
55 
56 namespace facade {
57 
Reset(uint32_t id)58 void Reset(uint32_t id) {
59   BtsLog("wifi::facade::Reset(%d)", id);
60   if (auto it = id_to_chip_info_.find(id); it != id_to_chip_info_.end()) {
61     auto chip_info = it->second;
62     chip_info->model->set_state(model::State::ON);
63     chip_info->model->set_tx_count(0);
64     chip_info->model->set_rx_count(0);
65   }
66 }
Remove(uint32_t id)67 void Remove(uint32_t id) {
68   BtsLog("wifi::facade::Remove(%d)", id);
69   id_to_chip_info_.erase(id);
70 }
71 
Patch(uint32_t id,const model::Chip::Radio & request)72 void Patch(uint32_t id, const model::Chip::Radio &request) {
73   BtsLog("wifi::facade::Patch(%d)", id);
74   auto it = id_to_chip_info_.find(id);
75   if (it == id_to_chip_info_.end()) {
76     BtsLog("Patch an unknown id %d", id);
77     return;
78   }
79   auto &model = it->second->model;
80   if (ChangedState(model->state(), request.state())) {
81     model->set_state(request.state());
82   }
83 }
84 
Get(uint32_t id)85 model::Chip::Radio Get(uint32_t id) {
86   BtsLog("wifi::facade::Get(%d)", id);
87   model::Chip::Radio radio;
88   if (auto it = id_to_chip_info_.find(id); it != id_to_chip_info_.end()) {
89     radio.CopyFrom(*it->second->model);
90   }
91   return radio;
92 }
93 
Add(uint32_t simulation_device)94 uint32_t Add(uint32_t simulation_device) {
95   BtsLog("wifi::facade::Add(%d)", simulation_device);
96   static uint32_t global_chip_id = kGlobalChipStartIndex;
97 
98   auto model = std::make_shared<model::Chip::Radio>();
99   model->set_state(model::State::ON);
100   id_to_chip_info_.emplace(
101       global_chip_id, std::make_shared<ChipInfo>(simulation_device, model));
102 
103   return global_chip_id++;
104 }
105 
Start()106 void Start() { BtsLog("wifi::facade::Start()"); }
Stop()107 void Stop() { BtsLog("wifi::facade::Stop()"); }
108 
109 }  // namespace facade
110 
HandleWifiRequest(uint32_t facade_id,const std::shared_ptr<std::vector<uint8_t>> & packet)111 void HandleWifiRequest(uint32_t facade_id,
112                        const std::shared_ptr<std::vector<uint8_t>> &packet) {
113   BtsLog("netsim::wifi::HandleWifiRequest()");
114   netsim::wifi::IncrTx(facade_id);
115 
116   // TODO: Broadcast the packet to other emulators.
117   // TODO: Send the packet to the WiFi service.
118 }
119 }  // namespace netsim::wifi
120