commit 02178ec96da90470a13f3a6468a0a78053a319e7
parent 8da5cf11516f73cfdabd91b1e5ac537f00b61585
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Mon, 25 Mar 2019 00:36:34 +0000
parent 8da5cf11516f73cfdabd91b1e5ac537f00b61585
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Mon, 25 Mar 2019 00:36:34 +0000
Added new method for shell4c
10 files changed, 180 insertions(+), 73 deletions(-)
diff --git a/Operation.hpp b/Operation.hpp @@ -0,0 +1,20 @@ +#ifndef _OPERATION_H +#define _OPERATION_H +#include <functional> +#include <iostream> +#include <vector> +namespace shell4c { +class Operation { +public: + std::string name; + std::string description; + std::function<void(Operation, std::vector<std::string>)> fp; + std::string usage; + Operation(std::string name, std::string description, + std::function<void(Operation, std::vector<std::string>)> fp, + std::string usage) + : name(name), description(description), fp(fp), + usage(usage) {} +}; +} +#endif+ \ No newline at end of file
diff --git a/Shell.hpp b/Shell.hpp @@ -9,12 +9,11 @@ #include <vector> #define _SHELL_PORT 50000 using namespace dot; -namespace wa { +namespace shell4c { class Shell { private: Dot &d = Dot::getDot(_SHELL_PORT); - std::map<std::string, std::function<void(std::vector<std::string>)>> - operations; + std::map<std::string, Operation *> operations; std::function<void(int)> exitHandler = [](int) {}; std::string shellIcon; Dot *client = nullptr; @@ -67,19 +66,31 @@ public: } void addOperation(std::string op, std::function<void(std::vector<std::string>)> opFunc) { - operations[op] = opFunc; + // operations[op] = opFunc; } + void addOperation(Operation *op){ + operations[op->name] = op; + } + + std::map<std::string, Operation *> getOperations(){ + return operations; + } + //TODO + // void addOperation(Operation op){ + // operations[op.name] = op; + // } void execute(std::vector<std::string> ins) { if (ins.size() == 0) { return; } - std::function<void(std::vector<std::string>)> func = operations[ins[0]]; - try { - ins.erase(ins.begin(), ins.begin() + 1); - func(ins); - } catch (std::bad_function_call bfc) { + Operation *op = operations[ins[0]]; + if(op == nullptr){ print(fmt::format("{}: operation not found\n", ins[0])); + return; } + std::function<void(Operation, std::vector<std::string>)> func = op->fp; + ins.erase(ins.begin(), ins.begin() + 1); + func(*op, ins); } void print(std::string message) { @@ -92,6 +103,10 @@ public: } } + void println(std::string message){ + print(message + "\n"); + } + void start() { if (isDaemon) { fmt::print("forwarder server started\n");
diff --git a/forwarder.cpp b/forwarder.cpp @@ -3,6 +3,7 @@ #include <map> #include <thread> +#include "Operation.hpp" #include "Shell.hpp" #include <Configuration.hpp> #include <ConfigurationBuilder.hpp> @@ -11,11 +12,10 @@ #include <model/NetworkInfo.hpp> #include <network/Network.hpp> #include <network/NetworkIndexer.hpp> -#include <Dot/Dot.hpp> using namespace std; -using namespace dot; using namespace wa; +using namespace shell4c; NetworkController *nc; Shell shell(true, "forwarder> "); @@ -25,16 +25,16 @@ void exit_handler(int sig) { nc->stop(); } // delete left and right initiators* - // unregister callbacks + // TODO unregister callbacks for (auto network : nc->activeNetworks) { delete network.second; } exit(EXIT_SUCCESS); } -void add(vector<string> msg) { +void add(shell4c::Operation op, vector<string> msg) { if (msg.size() == 0) { - shell.print("Usage: add <network1> [options]\n"); + shell.print(op.usage + "\n"); return; } Network *network = nc->networks[msg[0]]; @@ -52,16 +52,16 @@ void add(vector<string> msg) { nc->networks.erase(network->getUserFriendlyName()); } -void clear(vector<string>) { system("clear"); } +void clear(shell4c::Operation op, vector<string>) { system("clear"); } -void config(vector<string> msg) { +void config(shell4c::Operation op, vector<string> msg) { if (msg.size() == 0) { - shell.print("Usage: config <network1> [<network2> ...]\n"); + shell.print(op.usage); return; } } -void list(vector<string> msg) { +void list(shell4c::Operation op, vector<string> msg) { if (nc->activeNetworks.size() > 0) { shell.print(fmt::format("\u001b[32m")); for (auto network : nc->activeNetworks) { @@ -80,35 +80,78 @@ void list(vector<string> msg) { } } -void start(vector<string> msg) { +void start(shell4c::Operation op, vector<string> msg) { for (auto network : nc->activeNetworks) { network.second->listen(); } } -void bind_(vector<string> msg) { +void bind_(shell4c::Operation op, vector<string> msg) { if (msg.size() < 2) { - shell.print("Usage: bind <ip:port> <mac>\n"); + shell.println(op.usage); return; } + if (nc->activeNetworks["IP"] == nullptr) { + shell.println("Add IP first"); + return; + } else { + IP *i = (IP *)nc->activeNetworks["IP"]; + int stat = system( + fmt::format("ip address add {} dev {}", msg[0], i->getNetworkParams()) + .c_str()); + if (stat < 0) { + shell.println("Operation failed"); + } + } } +// FIXME +// void bind_(shell4c::Operation op, vector<string> msg) { +// if (msg.size() < 2) { +// shell.print(op.usageMessage); +// return; +// } +// } +void help(shell4c::Operation op, vector<string> messages) { + auto ops = shell.getOperations(); + if (messages.size() == 0) { + for (auto op : ops) { + shell.println(fmt::format("{} - {}", op.first, op.second->description)); + } + return; + } + for (auto msg : messages) { + if (ops.find(msg) != ops.end()) { + shell4c::Operation op = *ops[msg]; + shell.println(op.usage); + } + } +} int main(int argc, char *argv[]) { signal(SIGINT, exit_handler); std::cout << "Initializing please wait..." << std::endl; nc = new NetworkController(); - - shell.addOperation("add", add); - shell.addOperation("bind", bind_); - shell.addOperation("config", config); - shell.addOperation("clear", clear); - shell.addOperation("ls", list); - shell.addOperation("start", start); + shell.addOperation(new shell4c::Operation( + "add", "adds network to ready state", add, "add <network1> [options]")); + shell.addOperation(new shell4c::Operation("bind", "binds ip address to mac", + bind_, "bind <ip> <port> <mac>")); + shell.addOperation( + new shell4c::Operation("config", "configures medium to network", config, + "config <network1> [<network2> ...]")); + shell.addOperation( + new shell4c::Operation("clear", "clears the screen", clear, "clear")); + shell.addOperation(new shell4c::Operation("ls", "list all available network", + list, "ls [network]")); + shell.addOperation( + new shell4c::Operation("start", "start forwarder", start, "start")); + shell.addOperation(new shell4c::Operation("help", "prints this menu", help, + "help [operation1 ...]")); shell.setOnShellExit(exit_handler); shell.start(); - while(1); + while (1) + ; return 0; }
diff --git a/include/Configuration.hpp b/include/Configuration.hpp @@ -10,11 +10,11 @@ namespace wa { public: Configuration(Network *network, Medium *medium) : network(network), medium(medium){ } - Network &getNetwork(){ - return *network; + Network *getNetwork(){ + return network; } - Medium &getMedium(){ - return *medium; + Medium *getMedium(){ + return medium; } }; }
diff --git a/include/ConfigurationBuilder.hpp b/include/ConfigurationBuilder.hpp @@ -7,7 +7,7 @@ namespace wa { class ConfigurationBuilder { public: Network *network; - Medium *medium = new Medium(); + Medium *medium; Configuration *configuration; ConfigurationBuilder(){ }
diff --git a/include/device/ZWaveDevice.hpp b/include/device/ZWaveDevice.hpp @@ -0,0 +1,22 @@ +#ifndef _ZWAVE_DEV_H +#define _ZWAVE_DEV_H + +#include <Device.hpp> + +namespace wa { +class ZWaveDevice : Device { + +public: + ZWaveDevice() {} + + // TODO test + int send(std::string message) override { return -1; } + + // TODO test + std::string recv(int bytesToRead) override { + // zigbee recv + return ""; + } +}; +} // namespace wa +#endif+ \ No newline at end of file
diff --git a/include/medium/Medium.hpp b/include/medium/Medium.hpp @@ -1,9 +1,12 @@ #ifndef _MEDIUM_H #define _MEDIUM_H +#include <model/NetworkInfo.hpp> +namespace wa { class Medium { - public: - Medium(){ - - } +public: + Medium() {} + virtual std::vector<NetworkInfo> discover() = 0; + virtual std::string getUserFriendlyName() = 0; }; +} // namespace wa #endif \ No newline at end of file
diff --git a/include/medium/WiFi.hpp b/include/medium/WiFi.hpp @@ -0,0 +1,30 @@ +#ifndef _WIFI_H +#define _WIFI_H "WiFi" +#include <Medium.hpp> +#include <iostream> +#include <vector> +#include <wifi_scan.h> +#include <model/NetworkInfo.hpp> +namespace wa { +class WiFi : Medium { +public: + std::vector<NetworkInfo> discover() override { + std::vector<NetworkInfo> v; + int status, i; + struct bss_info bss[10]; + //FIXME add dependency to network + struct wifi_scan *wifi = wifi_scan_init("wlan0"); + status = wifi_scan_all(wifi, bss, 10); + for (i = 0; i < status && i < 10; ++i) { + NetworkInfo ni; + ni.address = bss[i].ssid; + v.push_back(ni); + } + wifi_scan_close(wifi); + return v; + } + + std::string getUserFriendlyName() override { return _WIFI_H; } +}; +} // namespace wa +#endif+ \ No newline at end of file
diff --git a/include/network/IP.hpp b/include/network/IP.hpp @@ -8,7 +8,6 @@ #include <sys/ioctl.h> #include <sys/socket.h> #include <unistd.h> -#include <wifi_scan.h> #include <device/IPDevice.hpp> #include <network/Network.hpp> @@ -22,6 +21,9 @@ using namespace std; namespace wa { class IP : public Network { + + + std::string networkParams; public: @@ -70,18 +72,7 @@ public: } std::vector<NetworkInfo> discover() override { - std::vector<NetworkInfo> v; - int status, i; - struct bss_info bss[10]; - struct wifi_scan *wifi = wifi_scan_init(networkParams.c_str()); - status = wifi_scan_all(wifi, bss, 10); - for (i = 0; i < status && i < 10; ++i) { - NetworkInfo ni; - ni.address = bss[i].ssid; - v.push_back(ni); - } - wifi_scan_close(wifi); - return v; + return std::vector<NetworkInfo>(); } ~IP() {
diff --git a/include/network/ZWave.hpp b/include/network/ZWave.hpp @@ -23,20 +23,6 @@ using namespace std; -const xbee_dispatch_table_entry_t xbee_frame_handlers[] = { - // Standard handlers (required) for AT command processing - XBEE_FRAME_HANDLE_LOCAL_AT, - XBEE_FRAME_HANDLE_REMOTE_AT, - - // Standard handlers (required) for SXA to work properly - XBEE_FRAME_HANDLE_ND_RESPONSE, - XBEE_FRAME_HANDLE_RX_EXPLICIT, -#ifdef SHOW_STATUS - {XBEE_FRAME_MODEM_STATUS, 0, my_modem_status, NULL}, - {XBEE_FRAME_TRANSMIT_STATUS, 0, my_handle_transmit_status, NULL}, -#endif - XBEE_FRAME_TABLE_END}; - namespace wa { class ZWave : public Network { int fd; @@ -45,18 +31,12 @@ public: ZWave() {} bool init() override { - xbee_serial_t XBEE_SERPORT; - xbee_dev_t my_xbee; - if (xbee_dev_init(&my_xbee, &XBEE_SERPORT, NULL, NULL)) { - return false; - } - return true; + return false; } std::string getUserFriendlyName() override { return _ZWAVE_H; } - int listen() override { - // deviceConnected(*this); + int listen() override { return -1; }
: bharatvaj/forwarder