commit 4fcb05617b4f574aa6fc75f05d60497932c7a2c2
parent 653e389680bff89f7881d827ebb8815f75daa49d
Author: Goutham SM <goutham.freaks@gmail.com>
Date: Tue, 22 Jan 2019 19:32:15 +0530
parent 653e389680bff89f7881d827ebb8815f75daa49d
Author: Goutham SM <goutham.freaks@gmail.com>
Date: Tue, 22 Jan 2019 19:32:15 +0530
Added bluetooth server and client, forwarder updated to start left and right network at the same time
3 files changed, 84 insertions(+), 14 deletions(-)
diff --git a/forwarder/forwarder.cpp b/forwarder/forwarder.cpp @@ -1,5 +1,6 @@ #include <iostream> #include <csignal> +#include <thread> #include <Network.hpp> #include <WiFi.hpp> @@ -11,19 +12,32 @@ using namespace std; using namespace wa; Forwarder *f = nullptr; +std::thread *t1 = nullptr; +std::thread *t2 = nullptr; + + Network *w = nullptr; + Network *b = nullptr; void exit_handler(int sig){ cout << "Forwarder ended" << endl; f->stop(); + t1->join(); + t2->join(); + delete w; + delete b; exit(EXIT_SUCCESS); } int main(int argc, char *argv[]) { signal(SIGINT, exit_handler); - Network *w = new WiFi(); - Network *b = new Bluetooth(); Network *c = new Console(); + t1 = new std::thread([](){ + w = new WiFi(); + }); + t2 = new std::thread([](){ + b = new Bluetooth(); + }); try { f = new Forwarder(w, c); } catch(const char *err){
diff --git a/forwarder/include/Bluetooth.hpp b/forwarder/include/Bluetooth.hpp @@ -11,27 +11,59 @@ namespace wa { class Bluetooth : public Network { private: - int dev_id; - int sockfd; + int client; public: Bluetooth(){} 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); + struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 }; + int s; + socklen_t opt = sizeof(rem_addr); + + // allocate socket + s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + if(s < 0){ + return false; + } + + // bind socket to port 1 of the first available + // local bluetooth adapter + loc_addr.rc_family = AF_BLUETOOTH; + loc_addr.rc_bdaddr = *BDADDR_ANY; + 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 + client = accept(s, (struct sockaddr *)&rem_addr, &opt); + if(client < 0){ + return false; + } + return true; } //TODO int send(std::string message) override{ - return -1; + return write(client, message.c_str(), message.size()); } //TODO std::string recv(int bytesToRead) override { - return ""; + 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); + } + + ~Bluetooth(){ + // close connection + close(client); + close(s); } }; }
diff --git a/right_initiator/right_initiator.cpp b/right_initiator/right_initiator.cpp @@ -1,6 +1,30 @@ -#include <iostream> +#include <stdio.h> +#include <unistd.h> +#include <sys/socket.h> +#include <bluetooth/bluetooth.h> +#include <bluetooth/rfcomm.h> int main(int argc, char *argv[]){ - - return 0; + + struct sockaddr_rc addr = { 0 }; + int s, status; + char dest[18] = "B8:27:EB:A5:A1:70"; + // 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_channel = (uint8_t) 1; + str2ba( dest, &addr.rc_bdaddr ); + + // connect to server + status = connect(s, (struct sockaddr *)&addr, sizeof(addr)); + + // send a message + if( status == 0 ) { + char buffer[10]; + status = recv(s, buffer, 6); + } + close(s); + return 0; }
: bharatvaj/forwarder