commit 346e74170fbc56ce2ac1b180898f7340936f8558
parent 54bccad463bb59156570eacf8d0a6427d669fb45
Author: Goutham SM <goutham.freaks@gmail.com>
Date: Tue, 22 Jan 2019 16:43:51 +0530
parent 54bccad463bb59156570eacf8d0a6427d669fb45
Author: Goutham SM <goutham.freaks@gmail.com>
Date: Tue, 22 Jan 2019 16:43:51 +0530
Classes specified
6 files changed, 161 insertions(+), 81 deletions(-)
M
|
95
++++++++++++-------------------------------------------------------------------
diff --git a/forwarder/CMakeLists.txt b/forwarder/CMakeLists.txt @@ -8,6 +8,7 @@ add_executable(forwarder forwarder.cpp) target_include_directories(forwarder PRIVATE ${DOT_INCLUDE_LIBS}) target_include_directories(forwarder PRIVATE crossguid) +target_include_directories(forwarder PRIVATE include) target_link_libraries(forwarder ${DOT_LIBS}) target_link_libraries(forwarder crossguid)
diff --git a/forwarder/forwarder.cpp b/forwarder/forwarder.cpp @@ -1,13 +1,10 @@ #include <iostream> +#include <csignal> -//for wifi -#include <Dot/Dot.hpp> - -//bluetooth headers -#include <sys/socket.h> -#include <bluetooth/bluetooth.h> -#include <bluetooth/hci.h> -#include <bluetooth/hci_lib.h> +#include <Network.hpp> +#include <WiFi.hpp> +#include <Bluetooth.hpp> +#include <Forwarder.hpp> //TODO update value based on the hardware using cmake #define BUF_SIZE 256 @@ -15,84 +12,20 @@ #define W_PORT 2500 using namespace std; -using namespace dot; - -class Bluetooth -{ - private: - inquiry_info *ii = NULL; - int max_rsp, num_rsp; - int dev_id, len, flags; - int sockfd; - int i; - char addr[19] = {0}; - char name[248] = {0}; - - public: - Bluetooth() - { - //setup socket - dev_id = hci_get_route(NULL); - sockfd = hci_open_dev(dev_id); - if (dev_id < 0 || sockfd < 0) - { - perror("Cannot open socket"); - exit(errno); - } - 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"); - //TODO exit or inquire one more time - } - for(i = 0; i < num_rsp; i++){ - ba2str(&(ii + 1)->bdaddr, addr); - memset(name, 0, sizeof(name)); - if(hci_read_remote_name(sockfd, &(ii+1)->bdaddr, sizeof(name), name, 0) < 0){ - strcpy(name, "[unknown]"); - } - printf("%s &s\n", addr, name); - } - free(ii); - close(sockfd); - } - - int send(std::string message) - { - return 0; - } - - std::string recv(int bytesToRead) - { - return ""; - } -}; - -void forward() -{ - Bluetooth b; - Dot::getDot(0).connect(W_SSID, W_PORT).onConnect([](Dot &d) { - d.readFor("hello").onSuccess([](Dot &e, std::string message) { - //TODO forward message to bluetooth connection - cout << message << endl; - }); - d.onDisconnect([](Dot &d) { - cout << "Connection destroyed" << endl; - }); - }); +void exit_handler(int sig){ + cout << "Forwarder ended" << endl; + exit(EXIT_SUCCESS); } int main(int argc, char *argv[]) { - //FIXME check whether wifi and bluetooth interface are turned on, before starting connections - forward(); - while (1) + signal(SIGINT, exit_handler); + Network &w = *new WiFi(); + Network &b = *new Bluetooth(); + Forwarder &f = *new Forwarder(w, b); + f.start(); + while (1) ; return 0; }
diff --git a/forwarder/include/Bluetooth.hpp b/forwarder/include/Bluetooth.hpp @@ -0,0 +1,39 @@ +#ifndef _BLUETOOTH_H +#define _BLUETOOTH_H + +#include <sys/socket.h> +#include <bluetooth/bluetooth.h> +#include <bluetooth/hci.h> +#include <bluetooth/hci_lib.h> + +#include <Network.hpp> + +namespace wa { + class Bluetooth : public Network { + private: + int dev_id; + int sockfd; + public: + Bluetooth(){} + + //TODO test + bool init() override { + //setup socket + dev_id = hci_get_route(NULL); + sockfd = hci_open_dev(dev_id); + close(sockfd); + return !(dev_id < 0 || sockfd < 0); + } + + //TODO + int send(std::string message) override{ + return -1; + } + + //TODO + std::string recv(int bytesToRead) override { + return ""; + } + }; +} +#endif
diff --git a/forwarder/include/Forwarder.hpp b/forwarder/include/Forwarder.hpp @@ -0,0 +1,39 @@ +#ifndef _FORWARDER_H +#define _FORWARDER_H + +#include <Network.hpp> +#include <WiFi.hpp> +#include <Bluetooth.hpp> + +namespace wa { + class Forwarder { + private: + bool isInit = false; + Network *l, *r; + public: + Forwarder(Network &l, Network &r) : l(*l), r(*r){ + if(!l.init()){ + return; + } + if(!r.init()){ + return; + } + isInit = true; + } + + //TODO + void start(){ + if(!isInit){ + throw "Not initialized"; + } + } + + void stop(){ + if(!isInit){ + throw "Not initialized"; + } + } + + }; +} +#endif
diff --git a/forwarder/include/Network.hpp b/forwarder/include/Network.hpp @@ -0,0 +1,12 @@ +#ifndef _NETWORK_H +#define _NETWORK_H +namespace wa { + class Network { + public: + Netowrk(){} + virtual bool init() = 0; + virtual int send(std::string message) = 0; + virtual std::string recv(int bytesToRead) = 0; + }; +}; +#endif
diff --git a/forwarder/include/WiFi.hpp b/forwarder/include/WiFi.hpp @@ -0,0 +1,56 @@ +#ifndef _WIFI_H +#define _WIFI_H + +#include <cstring> +#include <unistd.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <net/if.h> + +#include <Network.hpp> + +namespace wa { + class WiFi : public Network{ + public: + WiFi(){} + + //TODO test + bool init() override { + struct ifreq ifr = {0}; + bool uar = false; //up_and_running + + int fd = socket( AF_INET, SOCK_DGRAM, 0 ); + strcpy( ifr.ifr_name, "wlan0" ); + + //evavaluate uar + if( ioctl( fd, SIOCGIFFLAGS, &ifr ) != -1 ) + uar = (ifr.ifr_flags & ( IFF_UP | IFF_RUNNING )) == ( IFF_UP | IFF_RUNNING ); + else uar = false; + + close(fd); + return uar; + } + + //TODO + int send(std::string message) override { + // Dot::getDot(0).connect(W_SSID, W_PORT).onConnect([](Dot &d) { + // d.readFor("hello").onSuccess([](Dot &e, std::string message) { + // //TODO forward message to bluetooth connection + // cout << message << endl; + // b.send(message); + // }); + // d.onDisconnect([](Dot &d) { + // cout << "Connection destroyed" << endl; + // }); + // }); + return -1; + } + + //TODO + std::string recv(int bytesToRead) override { + return ""; + } + } +} + +#endif
: bharatvaj/forwarder