commit e636ad5f3c2e1a11432c74f08317a3e0ba3017cc
parent 4ce5eb18231421026a5360d8e5bb4fcfd4eb6d87
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Fri, 22 Mar 2019 19:42:44 +0000
parent 4ce5eb18231421026a5360d8e5bb4fcfd4eb6d87
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Fri, 22 Mar 2019 19:42:44 +0000
Structured as devices, medium and physical. Added shell for forwarder
32 files changed, 789 insertions(+), 557 deletions(-)
D
|
129
-------------------------------------------------------------------------------
D
|
78
------------------------------------------------------------------------------
A
|
129
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
|
89
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
|
77
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/3rdparty/xbee/CMakeLists.txt b/3rdparty/xbee/CMakeLists.txt @@ -9,5 +9,4 @@ add_library(xbee ${xbee_HDRS} ${xbee_SRCS}) target_compile_definitions(xbee PUBLIC POSIX) target_include_directories(xbee PUBLIC - $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> -$<INSTALL_INTERFACE:include>) + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -3,19 +3,23 @@ project(forwarder) find_package(comm) find_package(Threads) +find_package(fmt) add_subdirectory(deps/xbee_ansic_library) +add_subdirectory(include/network) -add_executable(forwarder forwarder.cpp) +add_executable(forwarder forwarder.cpp Shell.hpp) #FIXME should be obtained from xbee 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_link_libraries(forwarder ${COMM_LIBS}) target_link_libraries(forwarder ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(forwarder fmt) target_link_libraries(forwarder -lbluetooth) target_link_libraries(forwarder -lmnl) target_link_libraries(forwarder /usr/local/lib/libwifi-scan.so)
diff --git a/Shell.hpp b/Shell.hpp @@ -0,0 +1,77 @@ +#ifndef _SHELL_H +#define _SHELL_H +#include <fmt/format.h> +#include <functional> +#include <iostream> +#include <map> +#include <vector> +namespace wa { +class Shell { +private: + std::map<std::string, std::function<void(std::vector<std::string>)>> + operations; + std::function<void(int)> exitHandler = [](int){}; + std::string shellIcon; + bool endsWith(const std::string &s, const std::string &suffix) { + return s.size() >= suffix.size() && + s.substr(s.size() - suffix.size()) == suffix; + } + + std::vector<std::string> split(const std::string &s, + const std::string &delimiter, + const bool &removeEmptyEntries = false) { + std::vector<std::string> tokens; + + for (size_t start = 0, end; start < s.length(); + start = end + delimiter.length()) { + size_t position = s.find(delimiter, start); + end = position != std::string::npos ? position : s.length(); + + std::string token = s.substr(start, end - start); + if (!removeEmptyEntries || !token.empty()) { + tokens.push_back(token); + } + } + + if (!removeEmptyEntries && (s.empty() || endsWith(s, delimiter))) { + tokens.push_back(""); + } + + return tokens; + } + +public: + Shell(std::string shellIcon) : shellIcon(shellIcon) {} + void addOperation(std::string op, + std::function<void(std::vector<std::string>)> opFunc) { + operations[op] = opFunc; + } + 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) { + fmt::print("{}: operation not found\n", ins[0]); + } + } + void start() { + 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; + } +}; +} // namespace wa +#endif+ \ No newline at end of file
diff --git a/depinstall.sh b/depinstall.sh @@ -21,6 +21,10 @@ makeAndInstall() { cd $1 && cmake -DBUILD_SHARED_LIBS=ON . && make && make install && cd .. } +makeBuild() { + cd $1 && cmake -DBUILD_SHARED_LIBS=ON . && make && cd .. +} + configureAndInstall() { cd $1 && ./configure && make && make install && cd .. } @@ -30,7 +34,7 @@ downloadAndInstallXBee() { str=${url##*/} git clone $url $str cp ../3rdparty/xbee/CMakeLists.txt $str/CMakeLists.txt - makeAndInstall $str + makeBuild $str } if [ -f /etc/arch-release ]; then
diff --git a/forwarder.cpp b/forwarder.cpp @@ -3,86 +3,87 @@ #include <map> #include <thread> -#include <Bluetooth.hpp> -#include <Connector.hpp> -#include <Console.hpp> -#include <Forwarder.hpp> -#include <IP.hpp> -#include <Network.hpp> -#include <NetworkIndexer.hpp> -#include <ZigBee.hpp> +#include "Shell.hpp" +#include <Configuration.hpp> +#include <ConfigurationBuilder.hpp> +#include <NetworkController.hpp> +#include <fmt/format.h> #include <model/NetworkInfo.hpp> +#include <network/Network.hpp> +#include <network/NetworkIndexer.hpp> using namespace std; using namespace wa; -Forwarder *f = nullptr; -std::map<std::string, Network *> networks; -std::map<std::string, Network *> activeNetworks; +NetworkController *nc; +Shell shell("forwarder> "); void exit_handler(int sig) { - if (f != nullptr) { - f->stop(); - cout << "Forwarder ended" << endl; + if (nc != nullptr) { + nc->stop(); } // delete left and right initiators* // unregister callbacks - for (auto network : activeNetworks) { + for (auto network : nc->activeNetworks) { delete network.second; } exit(EXIT_SUCCESS); } -Network *getNetwork() { - cout << "Enter the name of the device: "; - std::string str; - cin >> str; - if (str == "end") { - return nullptr; +void add(vector<string> msg) { + if (msg.size() == 0) { + fmt::print("Usage: add <network1> [options]\n"); + return; } - if (str != "" && networks[str] != nullptr) { - return networks[str]; + 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"); + return; + } } - cerr << "No such network available, please select from the list" << endl; - return getNetwork(); -} - -void displaySelectedNetworks() { - for (auto network : activeNetworks) { - cout << network.first << endl; + if (msg.size() > 1) { + network->setNetworkParams(msg[1]); } + nc->activeNetworks[msg[0]] = network; + nc->networks.erase(network->getUserFriendlyName()); } -int main(int argc, char *argv[]) { - signal(SIGINT, exit_handler); +void clear(vector<string>) { system("clear"); } - std::cout << "Initializing please wait..." << std::endl; +void config(vector<string> msg) { + if (msg.size() == 0) { + fmt::print("Usage: config <network1> [<network2> ...]\n"); + return; + } +} - for (Network *n : NetworkIndexer::getNetworks()) { - if (n->init()) { - networks[n->getUserFriendlyName()] = n; +void list(vector<string> msg) { + if (nc->activeNetworks.size() > 0) { + fmt::print("\u001b[32m"); + for (auto network : nc->activeNetworks) { + fmt::print("{}", network.first); + if (!network.second->getNetworkParams().empty()) { + fmt::print(" [{}]", network.second->getNetworkParams()); + } + fmt::print("\n"); } + fmt::print("\u001b[0m"); } - - cout << "Select the networks" << endl; - for (auto n : networks) { - cout << n.first << std::endl; + if (nc->networks.size() > 0) { + for (auto network : nc->networks) { + fmt::print("{}\n", network.first); + } } +} - Network *t = nullptr; - do { - if (t == nullptr) - continue; - activeNetworks[t->getUserFriendlyName()] = t; - } while ((t = getNetwork()) != nullptr); - - displaySelectedNetworks(); - for (auto network : activeNetworks) { +void start(vector<string> msg) { + for (auto network : nc->activeNetworks) { network.second->setOnDeviceConnected([](Device &d) { - for (auto network : activeNetworks) { + for (auto network : nc->activeNetworks) { for (auto device : network.second->getDevices()) { cout << device->name << endl; - network.second->listen(); } } }); @@ -90,14 +91,34 @@ int main(int argc, char *argv[]) { int stat = network.second->listen(); if (stat < 0) { cout << "ERROR socket busy" << endl; - return -1; + return; } + + } +} + +void bind_(vector<string> msg) { + if (msg.size() < 2) { + fmt::print("Usage: bind <ip> <mac>\n"); + return; } - // Forwarder *f = new Forwarder(l, l); - // f->start(); +} + + + +int main(int argc, char *argv[]) { + signal(SIGINT, exit_handler); + std::cout << "Initializing please wait..." << std::endl; + + nc = new NetworkController(); - // c.start(); - while (1) - ; + 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.setOnShellExit(exit_handler); + shell.start(); return 0; }
diff --git a/include/Bluetooth.hpp b/include/Bluetooth.hpp @@ -1,129 +0,0 @@ -#ifndef _BLUETOOTH_H -#define _BLUETOOTH_H "Bluetooth" - -#include <bluetooth/bluetooth.h> -#include <bluetooth/hci.h> -#include <bluetooth/hci_lib.h> -#include <bluetooth/rfcomm.h> -#include <iostream> -#include <stdio.h> -#include <sys/socket.h> -#include <unistd.h> - -#include <BluetoothDevice.hpp> -#include <Network.hpp> -#include <model/NetworkInfo.hpp> - -namespace wa { -class Bluetooth : public Network { -private: - bdaddr_t inc = ((bdaddr_t){{0, 0, 0, 0, 0, 0}}); - -public: - Bluetooth() {} - - bool init() override { - int s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); - close(s); - return !(s < 0); - } - - std::string getUserFriendlyName() override { return _BLUETOOTH_H; } - - int listen() { - struct sockaddr_rc loc_addr = {0}, rem_addr = {0}; - comm_socket s; - socklen_t opt = sizeof(rem_addr); - - // allocate socket - s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); - if (s < 0) { - throw s; - } - - // bind socket to port 1 of the first available - // local bluetooth adapter - loc_addr.rc_family = AF_BLUETOOTH; - loc_addr.rc_bdaddr = inc; - loc_addr.rc_channel = (uint8_t)1; - bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); - - // put socket into listening mode - ::listen(s, 1); - - // accept one connection - comm_socket c = accept(s, (struct sockaddr *)&rem_addr, &opt); - Device *d = (Device *)new BluetoothDevice(c); - devices.push_back(d); - close(s); - if (c == SOCKET_ERROR) { - return -1; - } - deviceConnected(*d); - return 0; - } - - int connect(std::string conParams) { - struct sockaddr_rc addr = {0}; - comm_socket s; - const char *dest = conParams.c_str(); - // allocate a socket - s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); - - // set the connection parameters (who to connect to) - addr.rc_family = AF_BLUETOOTH; - addr.rc_bdaddr = inc; - addr.rc_channel = (uint8_t)1; - str2ba(dest, &addr.rc_bdaddr); - - // connect to server - int status = ::connect(s, (struct sockaddr *)&addr, sizeof(addr)); - devices.push_back((Device *)new BluetoothDevice(s)); - return status; - } - - // TODO - std::vector<NetworkInfo> discover() override { - std::vector<NetworkInfo> values; - inquiry_info *ii = NULL; - int max_rsp, num_rsp; - int dev_id, sock, len, flags; - int i; - char addr[19] = {0}; - char name[248] = {0}; - - dev_id = hci_get_route(NULL); - sock = hci_open_dev(dev_id); - if (dev_id < 0 || sock < 0) { - perror("opening socket"); - exit(1); - } - - len = 8; - max_rsp = 255; - flags = IREQ_CACHE_FLUSH; - ii = (inquiry_info *)malloc(max_rsp * sizeof(inquiry_info)); - - num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); - if (num_rsp < 0) - perror("hci_inquiry"); - - for (i = 0; i < num_rsp; i++) { - ba2str(&(ii + i)->bdaddr, addr); - hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name), name, 0); - NetworkInfo ni; - ni.name = name; - values.push_back(ni); - } - - free(ii); - close(sock); - return values; - } - - ~Bluetooth() { - // deinit - } -}; -} // namespace wa -#endif
diff --git a/include/BluetoothDevice.hpp b/include/BluetoothDevice.hpp @@ -1,42 +0,0 @@ -#ifndef _BLUETOOTH_DEV_H -#define _BLUETOOTH_DEV_H - -#include <Device.hpp> - -#include <bluetooth/bluetooth.h> -#include <bluetooth/hci.h> -#include <bluetooth/hci_lib.h> -#include <bluetooth/rfcomm.h> -#include <iostream> -#include <stdio.h> -#include <sys/socket.h> -#include <unistd.h> - -#include <comm.h> - -namespace wa { -class BluetoothDevice : Device { -private: - comm_socket s; - -public: - BluetoothDevice(comm_socket s) : s(s) {} - - // TODO - int send(std::string message) override { - return write(s, message.c_str(), message.size()); - } - - // TODO - std::string recv(int bytesToRead) override { - char buf[1024] = {0}; - int bytes_read = read(s, buf, sizeof(buf)); - // TODO investigate functionality - // if( bytes_read > 0 ) { - // return std::string(buf); - // } else return ""; - return std::string(buf); - } -}; -} // namespace wa -#endif- \ No newline at end of file
diff --git a/include/Configuration.hpp b/include/Configuration.hpp @@ -0,0 +1,21 @@ +#ifndef _CONFIGURATION_H +#define _CONFIGURATION_H +#include <network/Network.hpp> +#include <medium/Medium.hpp> +namespace wa { + class Configuration { + private: + Network *network; + Medium *medium; + public: + Configuration(Network *network, Medium *medium) : network(network), medium(medium){ + } + Network &getNetwork(){ + return *network; + } + Medium &getMedium(){ + return *medium; + } + }; +} +#endif+ \ No newline at end of file
diff --git a/include/ConfigurationBuilder.hpp b/include/ConfigurationBuilder.hpp @@ -0,0 +1,31 @@ +#ifndef _CONFIGURATION_BUILDER_H +#define _CONFIGURATION_BUILDER_H +#include <Configuration.hpp> +#include <network/Network.hpp> +#include <medium/Medium.hpp> +namespace wa { +class ConfigurationBuilder { + public: + Network *network; + Medium *medium = new Medium(); + Configuration *configuration; + ConfigurationBuilder(){ + } + ConfigurationBuilder &setNetworkParams(std::string networkParams){ + network->setNetworkParams(networkParams); + return *this; + } + ConfigurationBuilder &setNetwork(Network *network){ + this->network = network; + return *this; + } + ConfigurationBuilder &setMedium(Medium *medium){ + this->medium = medium; + return *this; + } + Configuration *build(){ + return new Configuration(network, medium); + } +}; +} +#endif+ \ No newline at end of file
diff --git a/include/Connector.hpp b/include/Connector.hpp @@ -5,7 +5,7 @@ #include <iostream> #include <vector> -#include <Network.hpp> +#include <network/Network.hpp> using namespace std;
diff --git a/include/Console.hpp b/include/Console.hpp @@ -1,58 +0,0 @@ -#ifndef _CONSOLE_H -#define _CONSOLE_H "Console" - -#include <Network.hpp> -#include <iostream> -#include <model/NetworkInfo.hpp> -#include <vector> - -namespace wa { - -class ConsoleDevice : Device { -public: - std::string consoleType; - ConsoleDevice(std::string consoleType) : consoleType(consoleType) {} - - int send(std::string message) override { - cout << message; - return message.size(); - } - - // FIXME should not read until 'bytesToRead', should also check for 'EOF' - std::string recv(int bytesToRead) override { - char buffer[bytesToRead]; - char c = 'a'; - int i = 0; - while (i < bytesToRead) { - buffer[i++] = getc(stdin); - } - return buffer; - } -}; -class Console : public Network { -public: - Console() {} - - bool init() override { return true; } - - std::string getUserFriendlyName() override { return _CONSOLE_H; } - - int listen() override { - Device *d = (Device *)new ConsoleDevice("stdout"); - devices.push_back(d); - deviceConnected(*d); - return 0; - } - - // TODO - int connect(std::string conParam) override { - devices.push_back((Device *)new ConsoleDevice(conParam)); - return 0; - } - - std::vector<NetworkInfo> discover() override { - return std::vector<NetworkInfo>(); - } -}; -} // namespace wa -#endif
diff --git a/include/Forwarder.hpp b/include/Forwarder.hpp @@ -3,7 +3,7 @@ #include <thread> -#include <Device.hpp> +#include <device/Device.hpp> #define BUF_SIZE 20
diff --git a/include/IP.hpp b/include/IP.hpp @@ -1,90 +0,0 @@ -#ifndef _IP_H -#define _IP_H "IP" - -#include <cstdlib> -#include <cstring> -#include <iostream> -#include <net/if.h> -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <unistd.h> -#include <wifi_scan.h> - -#include <IPDevice.hpp> -#include <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; - -namespace wa { -class IP : public Network { - std::string ifName; - -public: - IP(std::string ifName) : ifName(ifName) {} - - bool init() override { - return true; - struct ifreq ifr = {0}; - bool uar = false; // up_and_running - strcpy(ifr.ifr_name, ifName.c_str()); - int fd = socket(AF_INET, SOCK_STREAM, 0); - // evaluate uar - if (ioctl(fd, SIOCGIFFLAGS, &ifr) != -1) - uar = (ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING); - else - uar = false; - return uar; - } - - std::string getUserFriendlyName() override { return _IP_H; } - - int listen() override { - comm_socket fd = comm_start_server(W_DEFAULT_PORT); - Device *d = (Device *)new IPDevice(fd); - // FIXME NAMING ERROR - d->name = "IPDevice " + std::to_string(devices.size()); - devices.push_back(d); - - if (fd == SOCKET_ERROR) { - return -1; - } - deviceConnected(*d); - return 0; - } - - int connect(std::string conParams) override { - comm_socket fd = ::comm_connect_server(conParams.c_str(), W_DEFAULT_PORT); - if (fd == SOCKET_ERROR) { - return -1; - } - devices.push_back((Device *)new IPDevice(fd)); - return 0; - } - - std::vector<NetworkInfo> discover() override { - std::vector<NetworkInfo> v; - int status, i; - struct bss_info bss[10]; - struct wifi_scan *wifi = wifi_scan_init(ifName.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; - } - - ~IP() { - // deinit - } -}; -} // namespace wa - -#endif
diff --git a/include/IPDevice.hpp b/include/IPDevice.hpp @@ -1,41 +0,0 @@ -#ifndef _IP_DEVICE_H -#define _IP_DEVICE_H - -#include <sys/socket.h> -#include <unistd.h> - -#include <net/if.h> -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <unistd.h> - -#include <comm.h> - -#include <Device.hpp> - -#define W_DEFAULT_PORT 2500 - -namespace wa { -class IPDevice : Device { -private: - comm_socket fd; - -public: - IPDevice(comm_socket fd) : fd(fd) {} - - // TODO test - std::string recv(int bytesToRead) override { - char buffer[bytesToRead]; - ::recv(fd, buffer, bytesToRead - 2, 0); - buffer[bytesToRead - 1] = '\0'; - return std::string(buffer); - } - - // TODO test - int send(std::string message) override { - return ::send(fd, message.c_str(), message.size(), 0); - } - ~IPDevice() { comm_close_socket(fd); } -}; -} // namespace wa -#endif- \ No newline at end of file
diff --git a/include/Network.hpp b/include/Network.hpp @@ -1,31 +0,0 @@ -#ifndef _NETWORK_H -#define _NETWORK_H -#include <functional> -#include <vector> - -#include <Device.hpp> -#include <model/NetworkInfo.hpp> - -namespace wa { -class Network { -protected: - std::function<void(Device &)> deviceConnected; - std::vector<Device *> devices; - -public: - Network() {} - ~Network() {} - // FIXME add function for unregistering - virtual void - setOnDeviceConnected(std::function<void(Device &)> deviceConnected) { - this->deviceConnected = deviceConnected; - } - virtual bool init() = 0; - virtual std::string getUserFriendlyName() = 0; - virtual int listen() = 0; - virtual int connect(std::string) = 0; - virtual std::vector<NetworkInfo> discover() = 0; - std::vector<Device *> getDevices() { return devices; } -}; -}; // namespace wa -#endif
diff --git a/include/NetworkController.hpp b/include/NetworkController.hpp @@ -0,0 +1,30 @@ +#ifndef _NETWORK_CONTROLLER_H +#define _NETWORK_CONTROLLER_H +#include <Forwarder.hpp> +#include <iostream> +#include <map> +#include <network/Network.hpp> +#include <network/NetworkIndexer.hpp> +namespace wa { +class NetworkController { +private: + std::vector<Forwarder *> forwarders; + +public: + std::map<std::string, Network *> networks; + std::map<std::string, Network *> activeNetworks; + NetworkController() { + for (Network *n : NetworkIndexer::getNetworks()) { + if (n->init()) { + networks[n->getUserFriendlyName()] = n; + } + } + } + void stop() { + for (auto forwarder : forwarders) { + forwarder->stop(); + } + } +}; +} // namespace wa +#endif+ \ No newline at end of file
diff --git a/include/NetworkIndexer.hpp b/include/NetworkIndexer.hpp @@ -1,23 +0,0 @@ -#ifndef _NET_INDEXER_H -#define _NET_INDEXER_H -#include <Network.hpp> - -#include <Bluetooth.hpp> -#include <Console.hpp> -#include <IP.hpp> -#include <ZigBee.hpp> - -namespace wa { -class NetworkIndexer { -public: - static std::vector<Network *> getNetworks() { - std::vector<Network *> networks; - networks.push_back(new Bluetooth()); - networks.push_back(new Console()); - networks.push_back(new IP("enp3s0")); - networks.push_back(new ZigBee("")); - return networks; - } -}; -} // namespace wa -#endif
diff --git a/include/ZigBee.hpp b/include/ZigBee.hpp @@ -1,78 +0,0 @@ -#ifndef _ZIGBEE_H -#define _ZIGBEE_H "ZigBee" - -#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.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 ZigBee : public Network { - int fd; - std::string ifName; - -public: - ZigBee(std::string ifName) : ifName(ifName) {} - - 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 _ZIGBEE_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; - } - - ~ZigBee() { - // deinit - } -}; -} // namespace wa - -#endif
diff --git a/include/device/BluetoothDevice.hpp b/include/device/BluetoothDevice.hpp @@ -0,0 +1,42 @@ +#ifndef _BLUETOOTH_DEV_H +#define _BLUETOOTH_DEV_H + +#include <device/Device.hpp> + +#include <bluetooth/bluetooth.h> +#include <bluetooth/hci.h> +#include <bluetooth/hci_lib.h> +#include <bluetooth/rfcomm.h> +#include <iostream> +#include <stdio.h> +#include <sys/socket.h> +#include <unistd.h> + +#include <comm.h> + +namespace wa { +class BluetoothDevice : Device { +private: + comm_socket s; + +public: + BluetoothDevice(comm_socket s) : s(s) {} + + // TODO + int send(std::string message) override { + return write(s, message.c_str(), message.size()); + } + + // TODO + std::string recv(int bytesToRead) override { + char buf[1024] = {0}; + int bytes_read = read(s, buf, sizeof(buf)); + // TODO investigate functionality + // if( bytes_read > 0 ) { + // return std::string(buf); + // } else return ""; + return std::string(buf); + } +}; +} // namespace wa +#endif+ \ No newline at end of file
diff --git a/include/device/IPDevice.hpp b/include/device/IPDevice.hpp @@ -0,0 +1,41 @@ +#ifndef _IP_DEVICE_H +#define _IP_DEVICE_H + +#include <sys/socket.h> +#include <unistd.h> + +#include <net/if.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <unistd.h> + +#include <comm.h> + +#include <device/Device.hpp> + +#define W_DEFAULT_PORT 2500 + +namespace wa { +class IPDevice : Device { +private: + comm_socket fd; + +public: + IPDevice(comm_socket fd) : fd(fd) {} + + // TODO test + std::string recv(int bytesToRead) override { + char buffer[bytesToRead]; + ::recv(fd, buffer, bytesToRead - 2, 0); + buffer[bytesToRead - 1] = '\0'; + return std::string(buffer); + } + + // TODO test + int send(std::string message) override { + return ::send(fd, message.c_str(), message.size(), 0); + } + ~IPDevice() { comm_close_socket(fd); } +}; +} // namespace wa +#endif+ \ No newline at end of file
diff --git a/include/medium/Medium.hpp b/include/medium/Medium.hpp @@ -0,0 +1,9 @@ +#ifndef _MEDIUM_H +#define _MEDIUM_H +class Medium { + public: + Medium(){ + + } +}; +#endif+ \ No newline at end of file
diff --git a/include/model/NetworkConfiguration.hpp b/include/model/NetworkConfiguration.hpp @@ -0,0 +1,14 @@ +#ifndef _NETWORK_CONFIGURATION +#define _NETWORK_CONFIGURATION + +#include <network/Network.hpp> +#include <medium/medium.hpp> + +namespace wa { + class NetworkConfiguration { + public: + Network *network; + Medium *medium; + }; +} +#endif+ \ No newline at end of file
diff --git a/include/network/Bluetooth.hpp b/include/network/Bluetooth.hpp @@ -0,0 +1,129 @@ +#ifndef _BLUETOOTH_H +#define _BLUETOOTH_H "Bluetooth" + +#include <bluetooth/bluetooth.h> +#include <bluetooth/hci.h> +#include <bluetooth/hci_lib.h> +#include <bluetooth/rfcomm.h> +#include <iostream> +#include <stdio.h> +#include <sys/socket.h> +#include <unistd.h> + +#include <device/BluetoothDevice.hpp> +#include <network/Network.hpp> +#include <model/NetworkInfo.hpp> + +namespace wa { +class Bluetooth : public Network { +private: + bdaddr_t inc = ((bdaddr_t){{0, 0, 0, 0, 0, 0}}); + +public: + Bluetooth() {} + + bool init() override { + int s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + close(s); + return !(s < 0); + } + + std::string getUserFriendlyName() override { return _BLUETOOTH_H; } + + int listen() { + struct sockaddr_rc loc_addr = {0}, rem_addr = {0}; + comm_socket s; + socklen_t opt = sizeof(rem_addr); + + // allocate socket + s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + if (s < 0) { + throw s; + } + + // bind socket to port 1 of the first available + // local bluetooth adapter + loc_addr.rc_family = AF_BLUETOOTH; + loc_addr.rc_bdaddr = inc; + loc_addr.rc_channel = (uint8_t)1; + bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); + + // put socket into listening mode + ::listen(s, 1); + + // accept one connection + comm_socket c = accept(s, (struct sockaddr *)&rem_addr, &opt); + Device *d = (Device *)new BluetoothDevice(c); + devices.push_back(d); + close(s); + if (c == SOCKET_ERROR) { + return -1; + } + deviceConnected(*d); + return 0; + } + + int connect(std::string conParams) { + struct sockaddr_rc addr = {0}; + comm_socket s; + const char *dest = conParams.c_str(); + // allocate a socket + s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + + // set the connection parameters (who to connect to) + addr.rc_family = AF_BLUETOOTH; + addr.rc_bdaddr = inc; + addr.rc_channel = (uint8_t)1; + str2ba(dest, &addr.rc_bdaddr); + + // connect to server + int status = ::connect(s, (struct sockaddr *)&addr, sizeof(addr)); + devices.push_back((Device *)new BluetoothDevice(s)); + return status; + } + + // TODO + std::vector<NetworkInfo> discover() override { + std::vector<NetworkInfo> values; + inquiry_info *ii = NULL; + int max_rsp, num_rsp; + int dev_id, sock, len, flags; + int i; + char addr[19] = {0}; + char name[248] = {0}; + + dev_id = hci_get_route(NULL); + sock = hci_open_dev(dev_id); + if (dev_id < 0 || sock < 0) { + perror("opening socket"); + exit(1); + } + + len = 8; + max_rsp = 255; + flags = IREQ_CACHE_FLUSH; + ii = (inquiry_info *)malloc(max_rsp * sizeof(inquiry_info)); + + num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); + if (num_rsp < 0) + perror("hci_inquiry"); + + for (i = 0; i < num_rsp; i++) { + ba2str(&(ii + i)->bdaddr, addr); + hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name), name, 0); + NetworkInfo ni; + ni.name = name; + values.push_back(ni); + } + + free(ii); + close(sock); + return values; + } + + ~Bluetooth() { + // deinit + } +}; +} // namespace wa +#endif
diff --git a/include/network/CMakeLists.txt b/include/network/CMakeLists.txt @@ -0,0 +1 @@ +cmake_minimum_required(VERSION 3.0)
diff --git a/include/network/Console.hpp b/include/network/Console.hpp @@ -0,0 +1,58 @@ +#ifndef _CONSOLE_H +#define _CONSOLE_H "Console" + +#include <network/Network.hpp> +#include <iostream> +#include <model/NetworkInfo.hpp> +#include <vector> + +namespace wa { + +class ConsoleDevice : Device { +public: + std::string consoleType; + ConsoleDevice(std::string consoleType) : consoleType(consoleType) {} + + int send(std::string message) override { + std::cout << message; + return message.size(); + } + + // FIXME should not read until 'bytesToRead', should also check for 'EOF' + std::string recv(int bytesToRead) override { + char buffer[bytesToRead]; + char c = 'a'; + int i = 0; + while (i < bytesToRead) { + buffer[i++] = getc(stdin); + } + return buffer; + } +}; +class Console : public Network { +public: + Console() {} + + bool init() override { return true; } + + std::string getUserFriendlyName() override { return _CONSOLE_H; } + + int listen() override { + Device *d = (Device *)new ConsoleDevice("stdout"); + devices.push_back(d); + deviceConnected(*d); + return 0; + } + + // TODO + int connect(std::string conParam) override { + devices.push_back((Device *)new ConsoleDevice(conParam)); + return 0; + } + + std::vector<NetworkInfo> discover() override { + return std::vector<NetworkInfo>(); + } +}; +} // namespace wa +#endif
diff --git a/include/network/IP.hpp b/include/network/IP.hpp @@ -0,0 +1,89 @@ +#ifndef _IP_H +#define _IP_H "IP" + +#include <cstdlib> +#include <cstring> +#include <iostream> +#include <net/if.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <unistd.h> +#include <wifi_scan.h> + +#include <device/IPDevice.hpp> +#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; + +namespace wa { +class IP : public Network { + std::string networkParams; + +public: + IP() {} + + bool init() override { + return true; + struct ifreq ifr; + int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + memset(&ifr, 0, sizeof(ifr)); + strcpy(ifr.ifr_name, networkParams.c_str()); + if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { + perror("SIOCGIFFLAGS"); + } + close(sock); + return !!(ifr.ifr_flags & IFF_UP); + } + + std::string getUserFriendlyName() override { return _IP_H; } + + int listen() override { + comm_socket fd = comm_start_server(W_DEFAULT_PORT); + Device *d = (Device *)new IPDevice(fd); + // FIXME NAMING ERROR + d->name = "IPDevice " + std::to_string(devices.size()); + devices.push_back(d); + + if (fd == SOCKET_ERROR) { + return -1; + } + deviceConnected(*d); + return 0; + } + + int connect(std::string conParams) override { + comm_socket fd = ::comm_connect_server(conParams.c_str(), W_DEFAULT_PORT); + if (fd == SOCKET_ERROR) { + return -1; + } + devices.push_back((Device *)new IPDevice(fd)); + return 0; + } + + 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; + } + + ~IP() { + // deinit + } +}; +} // namespace wa + +#endif
diff --git a/include/network/Network.hpp b/include/network/Network.hpp @@ -0,0 +1,48 @@ +#ifndef _NETWORK_H +#define _NETWORK_H +#include <functional> +#include <vector> + +#include <device/Device.hpp> +#include <model/NetworkInfo.hpp> + +namespace wa { +class Network { +protected: + std::string networkParams; + std::function<void(Device &)> deviceConnected; + std::function<void(Device &)> deviceDisconnected; + std::vector<Device *> devices; + +public: + Network() {} + ~Network() {} + virtual void + setOnDeviceConnected(std::function<void(Device &)> deviceConnected) final { + this->deviceConnected = deviceConnected; + } + virtual void setOnDeviceDisconnected( + std::function<void(Device &)> deviceDisconnected) final { + this->deviceDisconnected = deviceDisconnected; + } + virtual void setNetworkParams(std::string networkParams) final { + this->networkParams = networkParams; + } + virtual std::string getNetworkParams() final { + return this->networkParams; + } + // + virtual bool init() = 0; + // returns a human readable user friendly name + virtual std::string getUserFriendlyName() = 0; + // listen + virtual int listen() = 0; + // connects to the given address + virtual int connect(std::string) = 0; + // discovers different networks + virtual std::vector<NetworkInfo> discover() = 0; + // should be deprecated + std::vector<Device *> getDevices() { return devices; } +}; +}; // namespace wa +#endif
diff --git a/include/network/NetworkIndexer.hpp b/include/network/NetworkIndexer.hpp @@ -0,0 +1,23 @@ +#ifndef _NET_INDEXER_H +#define _NET_INDEXER_H +#include <network/Network.hpp> + +#include <network/Bluetooth.hpp> +#include <network/Console.hpp> +#include <network/IP.hpp> +#include <network/ZigBee.hpp> + +namespace wa { +class NetworkIndexer { +public: + static std::vector<Network *> getNetworks() { + std::vector<Network *> networks; + networks.push_back(new Bluetooth()); + networks.push_back(new Console()); + networks.push_back(new IP()); + networks.push_back(new ZigBee()); + return networks; + } +}; +} // namespace wa +#endif
diff --git a/include/network/ZigBee.hpp b/include/network/ZigBee.hpp @@ -0,0 +1,77 @@ +#ifndef _ZIGBEE_H +#define _ZIGBEE_H "ZigBee" + +#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 ZigBee : public Network { + int fd; + +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; + } + + std::string getUserFriendlyName() override { return _ZIGBEE_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; + } + + ~ZigBee() { + // deinit + } +}; +} // namespace wa + +#endif
: bharatvaj/forwarder