: bharatvaj/forwarder-initiators

commit eacddda36cc901ea36548b52c6e608b6c80e833c
parent 612adb4c37976a780947df1b3aeabd5553ade87d
Author: Bharatvaj H <bharatvaj@yahoo.com>
Date: Thu, 28 Mar 2019 13:00:42 +0000

Added light turn on/off feature
3 files changed, 28 insertions(+), 4 deletions(-)
A
.gitignore
|
1
+
M
CMakeLists.txt
|
3
+++
M
right_initiator.cpp
|
28
++++++++++++++++++++++++----
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+build/
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -1,7 +1,10 @@
 cmake_minimum_required(VERSION 3.0)
 project(right_initiator)
 
+find_package(Threads)
+
 add_executable(right_initiator right_initiator.cpp)
 
 #TODO use find_package, instead of hard linking
 target_link_libraries(right_initiator -lbluetooth)
+target_link_libraries(right_initiator ${CMAKE_THREAD_LIBS_INIT})
diff --git a/right_initiator.cpp b/right_initiator.cpp
@@ -3,6 +3,9 @@
 #include <sys/socket.h>
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/rfcomm.h>
+#include <thread>
+
+#define BUF_SIZE 256
 
 using namespace std;
 

@@ -12,7 +15,8 @@ int main(int argc, char *argv[]){
 
 	    struct sockaddr_rc addr = { 0 };
 	    int s, status=0;
-	    char dest[18] = "B8:27:EB:A5:A1:70";
+	    char *msg = (char *)calloc(sizeof(char), BUF_SIZE);
+	    char dest[18] = "18:5E:0F:75:DB:8F";
 	    // allocate a socket
 	    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
 	    cout << s << endl;

@@ -28,9 +32,25 @@ int main(int argc, char *argv[]){
 
 	    // send a message
 	    if( status == 0 ) {
-		cout << "Receing message" << endl;
-		char buffer[10];
-	        status = recv(s, buffer, 6, 0);
+		int bytesRead = 0;
+		new std::thread([&](){
+			string str;
+			std::getline(std::cin, str);
+			write(s, str.c_str(), str.size());
+		});
+		while((bytesRead = read(s, msg, BUF_SIZE-1)) > 0){
+			msg[bytesRead] = '\0';
+			if(strncmp("hello", msg, 5) == 0){
+		system("echo \"475\" > /sys/class/gpio/export");
+		system("echo \"out\" > /sys/class/gpio/gpio475/direction");
+		system(" echo \"1\" > /sys/class/gpio/gpio475/value");
+		printf("light on\n");
+			} else { 
+		system(" echo \"0\" > /sys/class/gpio/gpio475/value");
+		printf("light off\n");
+		}
+			printf("%s", msg);
+		} 
 	    } else {
 		    int se=errno;
 		cout << "Connection failed" <<status<<se<< endl;