aboutsummaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-05-25 18:30:13 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-05-25 18:30:13 +0200
commit4525f60f29359b7ba88e47880d79fb9869913656 (patch)
treed6fa4be85ddfcee9861304eee53660bff7487d26 /main
parent4fc192eb9ba949276c47c1bbd86164d955d3548c (diff)
parent5d5b186a5a82b7e2415eddd77ef93af851034a5b (diff)
Merge branch 'wip/main-controller' into wip/i2c-communication
Diffstat (limited to 'main')
-rw-r--r--main/CMakeLists.txt1
-rw-r--r--main/sock.c52
-rw-r--r--main/sock.h4
3 files changed, 51 insertions, 6 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index 88abf60..30685a4 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -33,6 +33,7 @@ target_link_libraries(main
hardware_i2c
FreeRTOS-Kernel
FreeRTOS-Kernel-Heap4
+ puzbus
mpack
)
diff --git a/main/sock.c b/main/sock.c
index dac62af..4f50981 100644
--- a/main/sock.c
+++ b/main/sock.c
@@ -3,19 +3,63 @@
#include <lwip/opt.h>
#include <lwip/sys.h>
#include <lwip/api.h>
+#include <string.h>
#include "init.h"
#include "config.h"
+#include "puzbusv1.h"
+#include "sock.h"
struct netconn* current_connection = NULL;
+struct pb_msg recv_msg;
+
+void i2c_send(uint16_t addr, const char * data, size_t data_size) {
+ if (current_connection == NULL) return;
+
+ struct pb_msg send_msg = {
+ .addr = addr,
+ .data = (char *) data,
+ .length = data_size,
+ };
+
+ char * buf;
+ size_t buf_sz;
+
+ if (!pb_write(&send_msg, &buf, &buf_sz)) return;
+
+ // NOTE: netconn does return an error code, but the data needs to be freed
+ // whether netconn throws an error or not, so it remains unused
+ netconn_write(current_connection, buf, buf_sz, NETCONN_COPY);
+
+ free(buf);
+}
+
+void i2c_recv(uint16_t addr, const char * data, size_t data_size) {
+ printf("address: 0x%02x\n", addr);
+ printf("data: \"%.*s\"\n", data_size, data);
+
+ // send message back
+ char reply[] = "Test message back!";
+ i2c_send(0x69, reply, strlen(reply));
+
+ // TODO: this function should forward the recieved message onto the puzzle
+ // bus instead of printing/replying
+}
void recv_handler(struct netconn* conn, struct netbuf* buf) {
- void *data;
- uint16_t len;
+ pb_read_reset(&recv_msg);
do {
- netbuf_data(buf, &data, &len);
- printf("got %d bytes!\n", len);
+ char* data;
+ uint16_t len;
+ netbuf_data(buf, (void**)&data, &len);
+
+ // continue early if more data is needed to complete message
+ if (!pb_read(&recv_msg, data, len)) continue;
+
+ // forward received message to puzzle bus
+ i2c_recv(recv_msg.addr, recv_msg.data, recv_msg.length);
+ free(recv_msg.data);
} while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
diff --git a/main/sock.h b/main/sock.h
index 2a73418..f2db35d 100644
--- a/main/sock.h
+++ b/main/sock.h
@@ -6,6 +6,6 @@
/** \brief start listening for TCP socket requests */
void serve_task();
-void i2c_send(uint16_t addr, char* data, size_t data_size);
-void i2c_recv(uint16_t addr, char* data, size_t data_size);
+void i2c_send(uint16_t addr, const char * data, size_t data_size);
+void i2c_recv(uint16_t addr, const char * data, size_t data_size);