commit 7186dd1f1476644be1dd26ba5e03d0b2a5979be6
parent 787fd08203d8663344ef132eba18d2913366627f
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Sat, 16 Mar 2019 21:15:16 +0000
parent 787fd08203d8663344ef132eba18d2913366627f
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Sat, 16 Mar 2019 21:15:16 +0000
Forwarder algo adapter to Devices, Added ConsoleDevice for Console, Added BluetoothDevice and ZigBeeDevice. Removed send and recv from Network"
10 files changed, 168 insertions(+), 78 deletions(-)
diff --git a/forwarder.cpp b/forwarder.cpp @@ -40,7 +40,7 @@ Network *getNetwork(){ if(str == "end"){ return nullptr; } - if(networks[str] != nullptr){ + if(str != "" && networks[str] != nullptr){ return networks[str]; } cerr << "No such network available, please select from the list" << endl; @@ -77,8 +77,24 @@ int main(int argc, char *argv[]) } while((t = getNetwork()) != nullptr); displaySelectedNetworks(); - - Connector c = Connector(activeNetworks); + for(auto network : activeNetworks){ + network.second->setOnDeviceConnected([](Device &d){ + for(auto network : activeNetworks){ + for(auto device : network.second->getDevices()){ + cout << device->name << endl; + network.second->listen(); + } + } + }); + //FIXME open server vuln + int stat = network.second->listen(); + if(stat < 0){ + cout << "ERROR socket busy" << endl; + return -1; + } + } + // Forwarder *f = new Forwarder(l, l); + // f->start(); // c.start(); while (1);
diff --git a/include/Bluetooth.hpp b/include/Bluetooth.hpp @@ -12,11 +12,11 @@ #include <Network.hpp> #include <model/NetworkInfo.hpp> +#include <BluetoothDevice.hpp> namespace wa { class Bluetooth : public Network { private: - int client; bdaddr_t inc = ((bdaddr_t) {{0, 0, 0, 0, 0, 0}}); public: Bluetooth() { @@ -32,9 +32,9 @@ public: return _BLUETOOTH_H; } - void listen() { + int listen() { struct sockaddr_rc loc_addr = {0}, rem_addr = {0}; - int s; + comm_socket s; socklen_t opt = sizeof(rem_addr); // allocate socket @@ -54,13 +54,20 @@ public: ::listen(s, 1); // accept one connection - client = accept(s, (struct sockaddr *)&rem_addr, &opt); + 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 }; - int s, status=0; + comm_socket s; const char *dest = conParams.c_str(); // allocate a socket s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); @@ -72,26 +79,11 @@ public: str2ba( dest, &addr.rc_bdaddr ); // connect to server - status = ::connect(s, (struct sockaddr *)&addr, sizeof(addr)); + int status = ::connect(s, (struct sockaddr *)&addr, sizeof(addr)); + devices.push_back((Device *)new BluetoothDevice(s)); return status; } - // TODO - int send(std::string message) override { - return write(client, message.c_str(), message.size()); - } - - // TODO - std::string recv(int bytesToRead) override { - char buf[1024] = {0}; - int bytes_read = read(client, buf, sizeof(buf)); - // TODO investigate functionality - // if( bytes_read > 0 ) { - // return std::string(buf); - // } else return ""; - return std::string(buf); - } - //TODO std::vector<NetworkInfo> discover() override{ std::vector<NetworkInfo> values; @@ -131,8 +123,7 @@ public: } ~Bluetooth() { - // close connection - close(client); + //deinit } }; } // namespace wa
diff --git a/include/BluetoothDevice.hpp b/include/BluetoothDevice.hpp @@ -0,0 +1,48 @@ +#ifndef _BLUETOOTH_DEV_H +#define _BLUETOOTH_DEV_H + +#include <Device.hpp> + +#include <bluetooth/bluetooth.h> +#include <bluetooth/rfcomm.h> +#include <bluetooth/hci.h> +#include <bluetooth/hci_lib.h> +#include <stdio.h> +#include <iostream> +#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/Console.hpp b/include/Console.hpp @@ -7,17 +7,12 @@ #include <model/NetworkInfo.hpp> namespace wa { - class Console : public Network { - public: - Console(){ - } - bool init() override { - return true; - } + class ConsoleDevice : Device { + public: + std::string consoleType; + ConsoleDevice(std::string consoleType) : consoleType(consoleType){ - std::string getUserFriendlyName() override{ - return _CONSOLE_H; } int send(std::string message) override{ @@ -25,15 +20,6 @@ namespace wa { return message.size(); } - void listen(){ - listener(*this); - } - - //TODO - int connect(std::string conParam) override { - return 0; - } - //FIXME should not read until 'bytesToRead', should also check for 'EOF' std::string recv(int bytesToRead) override { char buffer[bytesToRead]; @@ -44,6 +30,31 @@ namespace wa { } 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>();
diff --git a/include/Device.hpp b/include/Device.hpp @@ -1,9 +1,10 @@ #ifndef _DEVICE_H #define _DEVICE_H #include <iostream> +#include <model/NetworkInfo.hpp> namespace wa { -class Device +class Device : public NetworkInfo { public: Device(){}
diff --git a/include/Forwarder.hpp b/include/Forwarder.hpp @@ -3,20 +3,18 @@ #include <thread> -#include <Network.hpp> -#include <IP.hpp> -#include <Bluetooth.hpp> +#include <Device.hpp> #define BUF_SIZE 20 namespace wa { class Forwarder { private: - Network *lp, *rp; + Device *lp, *rp; std::thread *t1, *t2; bool shouldStop = false; public: - Forwarder(Network &l, Network &r) : lp(&l), rp(&r){ + Forwarder(Device *l, Device *r) : lp(l), rp(r){ } //FIXME last 'while' blocked, input expected from the network before closing
diff --git a/include/IP.hpp b/include/IP.hpp @@ -44,14 +44,22 @@ namespace wa { return _IP_H; } - void listen() + int listen() override { comm_socket fd = comm_start_server(W_DEFAULT_PORT); - devices.push_back((Device *)new IPDevice(fd)); - listener(*this); + 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){ + int connect(std::string conParams) override { comm_socket fd = ::comm_connect_server(conParams.c_str(), W_DEFAULT_PORT); if(fd == SOCKET_ERROR){ return -1;
diff --git a/include/Network.hpp b/include/Network.hpp @@ -9,7 +9,7 @@ namespace wa { class Network { protected: - std::function<void(Network &)> listener; + std::function<void(Device &)> deviceConnected; std::vector<Device *> devices; public: Network(){ @@ -17,19 +17,13 @@ namespace wa { ~Network(){ } //FIXME add function for unregistering - virtual void setOnNetworkConnected(std::function<void(Network &)> listener){ - this->listener = listener; + virtual void setOnDeviceConnected(std::function<void(Device &)> deviceConnected){ + this->deviceConnected = deviceConnected; } virtual bool init() = 0; virtual std::string getUserFriendlyName() = 0; - virtual void listen() = 0; + virtual int listen() = 0; virtual int connect(std::string) = 0; - virtual int send(std::string message){ - return -1; - } - virtual std::string recv(int bytesToRead) { - return ""; - } virtual std::vector<NetworkInfo> discover() = 0; std::vector<Device *> getDevices(){ return devices;
diff --git a/include/ZigBee.hpp b/include/ZigBee.hpp @@ -62,32 +62,23 @@ namespace wa { return _ZIGBEE_H; } - void listen(){ - listener(*this); + int listen() override { + // deviceConnected(*this); + return -1; } int connect(std::string conParams){ return -1; } - //TODO test - int send(std::string message) override { - return ::send(fd, message.c_str(), message.size(), 0); - } - //TODO std::vector<NetworkInfo> discover() override{ std::vector<NetworkInfo> v; return v; } - //TODO test - std::string recv(int bytesToRead) override { - //zigbee recv - return std::string("h"); - } - ~ZigBee(){ + //deinit } }; }
diff --git a/include/ZigBeeDevice.hpp b/include/ZigBeeDevice.hpp @@ -0,0 +1,30 @@ +#ifndef _ZIGBEE_DEV_H +#define _ZIGBEE_DEV_H + +#include <Device.hpp> + +namespace wa +{ +class ZigBeeDevice : Device +{ + + public: + ZigBeeDevice() + { + } + + //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
: bharatvaj/forwarder