: bharatvaj/forwarder-initiators

commit ddd20dcd7654e34065142434d7fd665463fa2e78
parent 2e59a58fa678be18fb8617af42634b113eb5247a
Author: Bharatvaj H <bharatvaj@yahoo.com>
Date: Thu, 28 Mar 2019 20:25:16 +0000

updated right initiator to match listen connect mode v2
3 files changed, 38 insertions(+), 14 deletions(-)
M
Bluetooth.hpp
|
20
++++++++++++--------
M
GPIO.hpp
|
12
+++++++-----
M
right_initiator.cpp
|
20
+++++++++++++++++++-
diff --git a/Bluetooth.hpp b/Bluetooth.hpp
@@ -1,5 +1,6 @@
 #ifndef _BLUETOOTH_H
 #define _BLUETOOTH_H
+#include <thread>
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/rfcomm.h>
 #include <crosssocket.h>

@@ -43,19 +44,23 @@ public:
     }
 
     // accept one connection
-    xs_SOCKET c = accept(s, (struct sockaddr *)&rem_addr, &opt);
+new std::thread([&](int so){
+    xs_SOCKET c = accept(so, (struct sockaddr *)&rem_addr, &opt);
+
+    close(s);
+      try {
     if (c < 0) {
-      return false;
+	perror("socket\n");
+      onDeviceAdded(-1);
+	return;
     }
-    if (onDeviceAdded != nullptr) {
-      try {
         onDeviceAdded(c);
       } catch (std::bad_function_call bfc) {
       }
-    }
 
-    close(s);
+}, s);
+
     return true;
   }
 };
-#endif-
\ No newline at end of file
+#endif
diff --git a/GPIO.hpp b/GPIO.hpp
@@ -16,6 +16,7 @@ class GPIO {
 
   template <typename T> void write(std::string path, T value) {
     std::ofstream f(path);
+	std::cout << value << std::endl;
     f << value;
     f.close();
   }

@@ -23,11 +24,13 @@ class GPIO {
 public:
   int pin = 0;
   Direction direction = Direction::IN;
-  GPIO(int pin) { this->pin = offset + pin; }
+  GPIO(int pin) : pin(pin) { 
+	write(GPIO_EXPORT_PATH, pin);
+}
 
   static GPIO *getGPIO(int pinNo) {
     int pin = offset + pinNo;
-    return new GPIO(pinNo);
+    return new GPIO(pin);
   }
 
   void setDirection(Direction direction) {

@@ -42,8 +45,8 @@ public:
 
   void setLow() {
     std::string path = fmt::format(GPIO_VALUE_PATH, pin);
-    write(path, 1);
+    write(path, 0);
   }
 };
 } // namespace gpio
-#endif-
\ No newline at end of file
+#endif
diff --git a/right_initiator.cpp b/right_initiator.cpp
@@ -4,14 +4,23 @@
 #include <iostream>
 #include <thread>
 #include <unistd.h>
+#include <signal.h>
 
 #define BUF_SIZE 256
 
 using namespace std;
 using namespace gpio;
 
+xs_SOCKET x;
+
 void deviceConnected(xs_SOCKET s)
 {
+	x = s;
+	if(s == -1){
+		fmt::print("Error creation of socket\n");
+		return;
+	}
+	fmt::print("Device connected\n");
 	int pin = 17;
 	GPIO *g = GPIO::getGPIO(pin);
 	if (g == nullptr)

@@ -19,13 +28,15 @@ void deviceConnected(xs_SOCKET s)
 		fmt::print("Cannot open GPIO {}\n", pin);
 		return;
 	}
+	fmt::print("led going to create\n");
 	LED led(g);
 	int bytesRead = 0;
 	char *msg = (char *)calloc(sizeof(char), BUF_SIZE);
+	fmt::print("going to read\n");
 	while ((bytesRead = read(s, msg, BUF_SIZE - 1)) > 0)
 	{
 		msg[bytesRead] = '\0';
-		if (strncmp("hello", msg, 5) == 0)
+		if (strncmp("rmk on", msg, 6) == 0)
 		{
 			led.on();
 		}

@@ -35,10 +46,17 @@ void deviceConnected(xs_SOCKET s)
 		}
 		printf("%s", msg);
 	}
+ 	fmt::print("EOF\n");
+}
+
+void exit_handler(int sig){
+	close(x);
+	exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char *argv[])
 {
+	signal(SIGINT, exit_handler);
 	Bluetooth b;
 	b.setOnDeviceConnected(deviceConnected);
 	if (b.startSlave())