: bharatvaj/forwarder

commit e4acbbf333044db09d0921b3184e07d5ec135b0e
parent 0537cbff807d36bffbe28418061cb01d7615db2b
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Sat, 9 Mar 2019 20:48:19 +0000

Changed return type of discover from vector<string> to vector<NetworkInfo>
6 files changed, 66 insertions(+), 16 deletions(-)
M
forwarder.cpp
|
7
++++---
M
include/Bluetooth.hpp
|
30
++++++++++++++++++++++++++----
M
include/Console.hpp
|
10
++++++++--
M
include/Network.hpp
|
4
+++-
M
include/WiFi.hpp
|
19
+++++++++++++------
A
include/model/NetworkInfo.hpp
|
12
++++++++++++
diff --git a/forwarder.cpp b/forwarder.cpp
@@ -8,6 +8,7 @@
 #include <Console.hpp>
 #include <Connector.hpp>
 #include <Forwarder.hpp>
+#include <model/NetworkInfo.hpp>
 
 using namespace std;
 using namespace wa;

@@ -32,7 +33,7 @@ int main(int argc, char *argv[])
 {
   signal(SIGINT, exit_handler);
   //TODO get all wireless
-    l = new Bluetooth();
+   l = new WiFi(argv[1]);
     r = new Console();
   //Wait for the initiators to connect
 

@@ -48,8 +49,8 @@ int main(int argc, char *argv[])
 
   auto v = l->discover();
   cout << "Discovered" << endl;
-  for (std::string s : v){
-     cout << s << endl;
+  for (NetworkInfo s : v){
+     cout << s.name << " : " << s.address << endl;
   }
 
   Connector c = Connector(*l, *r);
diff --git a/include/Bluetooth.hpp b/include/Bluetooth.hpp
@@ -6,10 +6,12 @@
 #include <bluetooth/hci.h>
 #include <bluetooth/hci_lib.h>
 #include <stdio.h>
+#include <iostream>
 #include <sys/socket.h>
 #include <unistd.h>
 
 #include <Network.hpp>
+#include <model/NetworkInfo.hpp>
 
 namespace wa {
 class Bluetooth : public Network {

@@ -51,6 +53,24 @@ public:
     close(s);
   }
 
+  int connect(std::string conParams){
+    struct sockaddr_rc addr = { 0 };
+	    int s, status=0;
+	    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
+	    status = ::connect(s, (struct sockaddr *)&addr, sizeof(addr));
+      return status;
+  }
+
   // TODO
   int send(std::string message) override {
     return write(client, message.c_str(), message.size());

@@ -68,8 +88,8 @@ public:
   }
 
    //TODO
-  std::vector<std::string> discover() override{
-    std::vector<std::string> values;
+  std::vector<NetworkInfo> discover() override{
+    std::vector<NetworkInfo> values;
     inquiry_info *ii = NULL;
     int max_rsp, num_rsp;
     int dev_id, sock, len, flags;

@@ -88,14 +108,16 @@ public:
     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);
-        values.push_back(name);
+        NetworkInfo ni;
+        ni.name = name;
+        values.push_back(ni);
     }
 
     free( ii );
diff --git a/include/Console.hpp b/include/Console.hpp
@@ -4,6 +4,7 @@
 #include <iostream>
 #include <vector>
 #include <Network.hpp>
+#include <model/NetworkInfo.hpp>
 
 namespace wa {
   class Console : public Network {

@@ -23,6 +24,11 @@ namespace wa {
       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];

@@ -34,8 +40,8 @@ namespace wa {
       return buffer;
     }
 
-    std::vector<std::string> discover(){
-      return std::vector<std::string>();
+    std::vector<NetworkInfo> discover() override{
+      return std::vector<NetworkInfo>();
     }
   };
 }
diff --git a/include/Network.hpp b/include/Network.hpp
@@ -2,6 +2,7 @@
 #define _NETWORK_H
 #include <functional>
 #include <vector>
+#include <model/NetworkInfo.hpp>
 
 namespace wa {
   class Network {

@@ -15,9 +16,10 @@ namespace wa {
     }
     virtual bool init() = 0;
     virtual void listen() = 0;
+    virtual int connect(std::string) = 0;
     virtual int send(std::string message) = 0;
     virtual std::string recv(int bytesToRead) = 0;
-    virtual std::vector<std::string> discover() = 0;
+    virtual std::vector<NetworkInfo> discover() = 0;
   };
 };
 #endif
diff --git a/include/WiFi.hpp b/include/WiFi.hpp
@@ -10,8 +10,8 @@
 #include <net/if.h>
 #include <wifi_scan.h>
 
-
 #include <Network.hpp>
+#include <model/NetworkInfo.hpp>
 #include <comm.h>
 
 //TODO update value based on the hardware using cmake

@@ -45,20 +45,27 @@ namespace wa {
       listener(*this);
     }
 
+    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<std::string> discover() override{
-        std::vector<std::string> v;
+      std::vector<NetworkInfo> discover() override{
+        std::vector<NetworkInfo> v;
         	int status, i;
-	struct bss_info bss[10]; 
+	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)
-                v.push_back(bss[i].ssid);
+	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;
       }
diff --git a/include/model/NetworkInfo.hpp b/include/model/NetworkInfo.hpp
@@ -0,0 +1,12 @@
+#ifndef NET_INFO_H
+#define NET_INFO_H
+
+#include <iostream>
+
+class NetworkInfo {
+public:
+  std::string address;
+  std::string name;
+};
+
+#endif