← Back to ratslair.com
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdvin <[email protected]>2025-04-08 10:32:57 +0200
committerEdvin <[email protected]>2025-04-08 10:32:57 +0200
commit85635af1852b29e33ee56535d71bdf20d700b77e (patch)
tree2cba990ef17b24767a7f6369d690f307d5a7f0d3
parent71520eab9c30b4bc666d79b145a0d11d2501131e (diff)
Added C code for the Raspberry pi due to compatiblity issues between Bluez versions
-rw-r--r--Makefile2
-rw-r--r--control.py5
-rw-r--r--main.py2
-rw-r--r--rpi.c50
4 files changed, 54 insertions, 5 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..880e92f
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,2 @@
+main: rpi.c
+ cc rpi.c -Wall -lbluetooth -o rpi
diff --git a/control.py b/control.py
index 9c10693..aa86c1b 100644
--- a/control.py
+++ b/control.py
@@ -17,15 +17,12 @@ def get_input() -> str:
pi = "B8:27:EB:D1:A5:A9"
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
-# sock.bind(("", bluetooth.PORT_ANY))
-
-# port = sock.getsockname()[1]
print("Attempting to connect to %s..." % pi)
sock.connect((pi, 1))
-print("Connected")
+print("Connected to %s." % pi)
input = "0"
diff --git a/main.py b/main.py
index 4720154..7362778 100644
--- a/main.py
+++ b/main.py
@@ -30,7 +30,7 @@ print("Waiting for connection on RFCOMM channel %d..." % port)
client_sock, client_info = server_sock.accept()
-print("Connected with %d" % client_info)
+print("Connected to %s." % client_info[0])
while client_sock:
read = client_sock.recv(1024)
diff --git a/rpi.c b/rpi.c
new file mode 100644
index 0000000..3f6fa08
--- /dev/null
+++ b/rpi.c
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/rfcomm.h>
+
+int main(int argc, char **argv)
+{
+ struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
+ char buf[1024] = { 0 };
+ int s, client, bytes_read;
+ socklen_t opt = sizeof(rem_addr);
+
+ // allocate socket
+ s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
+
+ // 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));
+
+ // get local address
+ // ba2str( &loc_addr.rc_bdaddr, buf );
+ // fprintf(stdout, "local %s\n", buf);
+
+ // put socket into listening mode
+ fprintf(stdout, "Waiting on connection on RFCOMM channel %i...\n", loc_addr.rc_channel);
+ listen(s, loc_addr.rc_channel);
+
+ // accept one connection
+ client = accept(s, (struct sockaddr *)&rem_addr, &opt);
+ ba2str( &rem_addr.rc_bdaddr, buf );
+ fprintf(stdout, "Accepted connection from %s\n", buf);
+
+ memset(buf, 0, sizeof(buf));
+
+ // read data from the client
+ bytes_read = read(client, buf, sizeof(buf));
+
+ if( bytes_read > 0 ) {
+ printf("received [%s]\n", buf);
+ }
+
+ // close connection
+ close(client);
+ close(s);
+ return 0;
+}