1 /* 2 * Copyright (C) 2021 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 <cstdint> 20 #include <memory> 21 #include <optional> 22 #include <string> 23 24 #include <wmediumd/api.h> 25 26 #include "common/libs/fs/shared_fd.h" 27 28 namespace cuttlefish { 29 30 enum class WmediumdMessageType : uint32_t { 31 kInvalid = 0, 32 kAck = 1, 33 kRegister = 2, 34 kUnregister = 3, 35 kNetlink = 4, 36 kSetControl = 5, 37 kTxStart = 6, 38 kGetStations = 7, 39 kSetSnr = 8, 40 kReloadConfig = 9, 41 kReloadCurrentConfig = 10, 42 kStartPcap = 11, 43 kStopPcap = 12, 44 kStationsList = 13, 45 }; 46 47 class WmediumdMessage { 48 public: ~WmediumdMessage()49 virtual ~WmediumdMessage() {} 50 51 std::string Serialize(void) const; 52 53 virtual WmediumdMessageType Type() const = 0; 54 55 private: SerializeBody(std::string &)56 virtual void SerializeBody(std::string&) const {}; 57 }; 58 59 class WmediumdMessageSetControl : public WmediumdMessage { 60 public: WmediumdMessageSetControl(uint32_t flags)61 WmediumdMessageSetControl(uint32_t flags) : flags_(flags) {} 62 Type()63 WmediumdMessageType Type() const override { 64 return WmediumdMessageType::kSetControl; 65 } 66 67 private: 68 void SerializeBody(std::string& out) const override; 69 uint32_t flags_; 70 }; 71 72 class WmediumdMessageSetSnr : public WmediumdMessage { 73 public: 74 WmediumdMessageSetSnr(const std::string& node1, const std::string& node2, 75 uint8_t snr); 76 Type()77 WmediumdMessageType Type() const override { 78 return WmediumdMessageType::kSetSnr; 79 } 80 81 private: 82 void SerializeBody(std::string& out) const override; 83 84 uint8_t node1_mac_[6]; 85 uint8_t node2_mac_[6]; 86 uint8_t snr_; 87 }; 88 89 class WmediumdMessageReloadConfig : public WmediumdMessage { 90 public: WmediumdMessageReloadConfig(const std::string & configPath)91 WmediumdMessageReloadConfig(const std::string& configPath) 92 : config_path_(configPath) {} 93 Type()94 WmediumdMessageType Type() const override { 95 return WmediumdMessageType::kReloadConfig; 96 } 97 98 private: 99 void SerializeBody(std::string& out) const override; 100 101 std::string config_path_; 102 }; 103 104 class WmediumdMessageReloadCurrentConfig : public WmediumdMessage { 105 public: 106 WmediumdMessageReloadCurrentConfig() = default; 107 Type()108 WmediumdMessageType Type() const override { 109 return WmediumdMessageType::kReloadCurrentConfig; 110 } 111 }; 112 113 class WmediumdMessageStartPcap : public WmediumdMessage { 114 public: WmediumdMessageStartPcap(const std::string & pcapPath)115 WmediumdMessageStartPcap(const std::string& pcapPath) 116 : pcap_path_(pcapPath) {} 117 Type()118 WmediumdMessageType Type() const override { 119 return WmediumdMessageType::kStartPcap; 120 } 121 122 private: 123 void SerializeBody(std::string& out) const override; 124 125 std::string pcap_path_; 126 }; 127 128 class WmediumdMessageStopPcap : public WmediumdMessage { 129 public: 130 WmediumdMessageStopPcap() = default; 131 Type()132 WmediumdMessageType Type() const override { 133 return WmediumdMessageType::kStopPcap; 134 } 135 }; 136 137 class WmediumdMessageGetStations : public WmediumdMessage { 138 public: 139 WmediumdMessageGetStations() = default; 140 Type()141 WmediumdMessageType Type() const override { 142 return WmediumdMessageType::kGetStations; 143 } 144 }; 145 146 class WmediumdMessageReply : public WmediumdMessage { 147 public: 148 WmediumdMessageReply() = default; WmediumdMessageReply(WmediumdMessageType type,const std::string & data)149 WmediumdMessageReply(WmediumdMessageType type, const std::string& data) 150 : type_(type), data_(data) {} 151 Type()152 WmediumdMessageType Type() const override { return type_; } 153 Size()154 size_t Size() const { return data_.size(); } Data()155 const char* Data() const { return data_.data(); } 156 157 private: 158 WmediumdMessageType type_; 159 std::string data_; 160 }; 161 162 class WmediumdMessageStationsList : public WmediumdMessage { 163 public: 164 WmediumdMessageStationsList() = default; 165 static std::optional<WmediumdMessageStationsList> Parse( 166 const WmediumdMessageReply& reply); 167 Type()168 WmediumdMessageType Type() const override { 169 return WmediumdMessageType::kStationsList; 170 } 171 GetStations()172 const std::vector<wmediumd_station_info>& GetStations() const { 173 return station_list_; 174 } 175 176 private: 177 std::vector<wmediumd_station_info> station_list_; 178 }; 179 180 } // namespace cuttlefish 181