commit 8da5cf11516f73cfdabd91b1e5ac537f00b61585
parent e636ad5f3c2e1a11432c74f08317a3e0ba3017cc
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Sun, 24 Mar 2019 19:00:39 +0000
parent e636ad5f3c2e1a11432c74f08317a3e0ba3017cc
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Sun, 24 Mar 2019 19:00:39 +0000
Added network interface for shell
7 files changed, 153 insertions(+), 60 deletions(-)
A
|
77
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -4,6 +4,8 @@ project(forwarder) find_package(comm) find_package(Threads) find_package(fmt) +find_package(crossguid) +find_package(Dot) add_subdirectory(deps/xbee_ansic_library) add_subdirectory(include/network) @@ -15,10 +17,14 @@ target_compile_definitions(forwarder PUBLIC POSIX) target_include_directories(forwarder PRIVATE ${COMM_INCLUDE_LIBS}) target_include_directories(forwarder PRIVATE fmt) target_include_directories(forwarder PRIVATE include) -target_include_directories(forwarder PUBLIC xbee) +target_include_directories(forwarder PRIVATE xbee) +target_include_directories(forwarder PRIVATE crossguid) +target_include_directories(forwarder PRIVATE Dot) target_link_libraries(forwarder ${COMM_LIBS}) target_link_libraries(forwarder ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(forwarder crossguid) +target_link_libraries(forwarder ${DOT_LIBS}) target_link_libraries(forwarder fmt) target_link_libraries(forwarder -lbluetooth) target_link_libraries(forwarder -lmnl)
diff --git a/Shell.hpp b/Shell.hpp @@ -1,17 +1,26 @@ #ifndef _SHELL_H #define _SHELL_H +#include <Dot/Dot.hpp> +#include <cstdarg> #include <fmt/format.h> #include <functional> #include <iostream> #include <map> #include <vector> +#define _SHELL_PORT 50000 +using namespace dot; namespace wa { class Shell { private: + Dot &d = Dot::getDot(_SHELL_PORT); std::map<std::string, std::function<void(std::vector<std::string>)>> operations; - std::function<void(int)> exitHandler = [](int){}; + std::function<void(int)> exitHandler = [](int) {}; std::string shellIcon; + Dot *client = nullptr; + bool isConnected = false; + bool isDaemon = false; + bool endsWith(const std::string &s, const std::string &suffix) { return s.size() >= suffix.size() && s.substr(s.size() - suffix.size()) == suffix; @@ -41,7 +50,21 @@ private: } public: - Shell(std::string shellIcon) : shellIcon(shellIcon) {} + Shell(bool isDaemon, std::string shellIcon) + : isDaemon(isDaemon), shellIcon(shellIcon) { + d.onConnect([=](Dot &c) { + isConnected = true; + client = &c; + print(shellIcon); + client->readFor(".*").onSuccess([=](Dot &, std::string m) { + std::vector<std::string> ins = split(m, " ", true); + execute(ins); + print(shellIcon); + }); + client->onDisconnect([=](Dot &d) { exitHandler(0); }); + }); + d.onDisconnect([=](Dot &client) { isConnected = false; }); + } void addOperation(std::string op, std::function<void(std::vector<std::string>)> opFunc) { operations[op] = opFunc; @@ -55,23 +78,35 @@ public: ins.erase(ins.begin(), ins.begin() + 1); func(ins); } catch (std::bad_function_call bfc) { - fmt::print("{}: operation not found\n", ins[0]); + print(fmt::format("{}: operation not found\n", ins[0])); } } + + void print(std::string message) { + if (isDaemon) { + if (client != nullptr) { + client->write(message); + } + } else { + fmt::print(message); + } + } + void start() { - std::string in; - fmt::print(shellIcon); - while (std::getline(std::cin, in)) { - std::vector<std::string> ins = split(in, " ", true); - execute(ins); + if (isDaemon) { + fmt::print("forwarder server started\n"); + } else { + std::string in; fmt::print(shellIcon); + while (std::getline(std::cin, in)) { + std::vector<std::string> ins = split(in, " ", true); + execute(ins); + fmt::print(shellIcon); + } } - exitHandler(0); } - void setOnShellExit(std::function<void(int)> func) { - exitHandler = func; - } + void setOnShellExit(std::function<void(int)> func) { exitHandler = func; } }; } // namespace wa #endif \ No newline at end of file
diff --git a/forwarder.cpp b/forwarder.cpp @@ -11,12 +11,14 @@ #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; NetworkController *nc; -Shell shell("forwarder> "); +Shell shell(true, "forwarder> "); void exit_handler(int sig) { if (nc != nullptr) { @@ -32,14 +34,14 @@ void exit_handler(int sig) { void add(vector<string> msg) { if (msg.size() == 0) { - fmt::print("Usage: add <network1> [options]\n"); + shell.print("Usage: add <network1> [options]\n"); return; } Network *network = nc->networks[msg[0]]; if (network == nullptr) { network = nc->activeNetworks[msg[0]]; if (network == nullptr) { - fmt::print("Enter a valid network name\n"); + shell.print("Enter a valid network name\n"); return; } } @@ -54,52 +56,39 @@ void clear(vector<string>) { system("clear"); } void config(vector<string> msg) { if (msg.size() == 0) { - fmt::print("Usage: config <network1> [<network2> ...]\n"); + shell.print("Usage: config <network1> [<network2> ...]\n"); return; } } void list(vector<string> msg) { if (nc->activeNetworks.size() > 0) { - fmt::print("\u001b[32m"); + shell.print(fmt::format("\u001b[32m")); for (auto network : nc->activeNetworks) { - fmt::print("{}", network.first); + shell.print(fmt::format("{}", network.first)); if (!network.second->getNetworkParams().empty()) { - fmt::print(" [{}]", network.second->getNetworkParams()); + shell.print(fmt::format(" [{}]", network.second->getNetworkParams())); } - fmt::print("\n"); + shell.print("\n"); } - fmt::print("\u001b[0m"); + shell.print("\u001b[0m"); } if (nc->networks.size() > 0) { for (auto network : nc->networks) { - fmt::print("{}\n", network.first); + shell.print(fmt::format("{}\n", network.first)); } } } void start(vector<string> msg) { for (auto network : nc->activeNetworks) { - network.second->setOnDeviceConnected([](Device &d) { - for (auto network : nc->activeNetworks) { - for (auto device : network.second->getDevices()) { - cout << device->name << endl; - } - } - }); - // FIXME open server vuln - int stat = network.second->listen(); - if (stat < 0) { - cout << "ERROR socket busy" << endl; - return; - } - + network.second->listen(); } } void bind_(vector<string> msg) { if (msg.size() < 2) { - fmt::print("Usage: bind <ip> <mac>\n"); + shell.print("Usage: bind <ip:port> <mac>\n"); return; } } @@ -120,5 +109,6 @@ int main(int argc, char *argv[]) { shell.addOperation("start", start); shell.setOnShellExit(exit_handler); shell.start(); + while(1); return 0; }
diff --git a/include/network/IP.hpp b/include/network/IP.hpp @@ -56,6 +56,10 @@ public: return 0; } + int addDevice(std::string ipaddress){ + return -1; + } + int connect(std::string conParams) override { comm_socket fd = ::comm_connect_server(conParams.c_str(), W_DEFAULT_PORT); if (fd == SOCKET_ERROR) {
diff --git a/include/network/NetworkIndexer.hpp b/include/network/NetworkIndexer.hpp @@ -6,6 +6,7 @@ #include <network/Console.hpp> #include <network/IP.hpp> #include <network/ZigBee.hpp> +#include <network/ZWave.hpp> namespace wa { class NetworkIndexer { @@ -16,6 +17,7 @@ public: networks.push_back(new Console()); networks.push_back(new IP()); networks.push_back(new ZigBee()); + networks.push_back(new ZWave()); return networks; } };
diff --git a/include/network/ZWave.hpp b/include/network/ZWave.hpp @@ -0,0 +1,77 @@ +#ifndef _ZWAVE_H +#define _ZWAVE_H "ZWave" + +#include <cstdlib> +#include <cstring> +#include <iostream> +#include <net/if.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <unistd.h> + +#include <xbee/atcmd.h> +#include <xbee/device.h> +#include <xbee/sxa.h> +#include <xbee/wpan.h> + +#include <network/Network.hpp> +#include <comm.h> +#include <model/NetworkInfo.hpp> + +// TODO update value based on the hardware using cmake +#define W_DEFAULT_PORT 2500 + +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; + +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; + } + + std::string getUserFriendlyName() override { return _ZWAVE_H; } + + int listen() override { + // deviceConnected(*this); + return -1; + } + + int connect(std::string conParams) { return -1; } + + // TODO + std::vector<NetworkInfo> discover() override { + std::vector<NetworkInfo> v; + return v; + } + + ~ZWave() { + // deinit + } +}; +} // namespace wa + +#endif
diff --git a/include/network/ZigBee.hpp b/include/network/ZigBee.hpp @@ -18,25 +18,9 @@ #include <comm.h> #include <model/NetworkInfo.hpp> -// TODO update value based on the hardware using cmake -#define W_DEFAULT_PORT 2500 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 ZigBee : public Network { int fd; @@ -45,12 +29,7 @@ public: ZigBee() {} 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 _ZIGBEE_H; }
: bharatvaj/forwarder