]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
esp32 build, cli
authorPatrick <patrick.schoenberger@posteo.de>
Fri, 14 Jul 2023 11:46:27 +0000 (13:46 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Fri, 14 Jul 2023 11:46:27 +0000 (13:46 +0200)
13 files changed:
Makefile
Todo.md [new file with mode: 0644]
esp32/esp_project/components/olm/CMakeLists.txt
esp32/esp_project/main/CMakeLists.txt
esp32/esp_project/main/main.c
esp32/esp_project/main/wifi.c [new file with mode: 0644]
esp32/esp_project/sdkconfig
examples/Cli.c [new file with mode: 0644]
examples/Keys.rdbg [new file with mode: 0644]
examples/ReplyRoomkey.c [new file with mode: 0644]
examples/SendEncrypted.rdbg [new file with mode: 0644]
src/matrix.h
src/matrix_http_mongoose.c

index bfcb1dec63e1705ef42f59c9a3a8842b85fcc77d..e508b2e7310b52260cb8d8fdbdcdadd20fea6bc0 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-CC=clang++\r
+CC=clang\r
 \r
 C_OPTS=-Wall -Wextra -pedantic\r
 C_OPTS+=src/matrix.c\r
@@ -12,9 +12,10 @@ C_OPTS+=-I ext/mongoose/
 C_OPTS+=-l ws2_32\r
 C_OPTS+=-l ssl\r
 C_OPTS+=-l crypto\r
+C_OPTS+=-l stdc++\r
 C_OPTS+=out/olm/libolm.a\r
 C_OPTS+=-D MG_ENABLE_OPENSSL=1\r
-C_OPTS+=-fuse-ld=lld.exe -g -gcodeview -Wl,/debug,/pdb:test.pdb\r
+C_OPTS+=-fuse-ld=lld.exe -g -gcodeview -Wl,/debug,/pdb:out/test.pdb\r
 # C_OPTS+=-I ext/curl/include/\r
 # C_OPTS+=-L ext/curl/build/lib/\r
 # C_OPTS+=-l curl\r
diff --git a/Todo.md b/Todo.md
new file mode 100644 (file)
index 0000000..3e5dc6b
--- /dev/null
+++ b/Todo.md
@@ -0,0 +1,18 @@
+# General\r
++ switch to mongoose\r
++ add esp build\r
+- (write script to build dependencies)\r
+- automate script to generate header with size defines\r
+\r
+# Matrix Lib\r
+- manage keys\r
+- upload keys\r
+- create olm session\r
+  - incoming\r
+  - outgoing\r
+- store keys/sessions\r
+- respond to events\r
+  - room_key_request\r
+- add client saving/loading\r
+- esp compatibility\r
+- http requests in chunks/dynamically allocated
\ No newline at end of file
index 8ce9b41efbf876e915a819665e24563b6625bc30..352ab9cbe32dd7dc25093e9dcdd15d833107a203 100644 (file)
@@ -1,6 +1,6 @@
 idf_component_register(SRCS\r
                             "../../../../ext/olm/src/account.cpp"\r
-                            "../../../../ext/olm/lib/crypto-algorithms/aes.c"\r
+                            #"../../../../ext/olm/lib/crypto-algorithms/aes.c"\r
                             "../../../../ext/olm/src/base64.cpp"\r
                             "../../../../ext/olm/src/cipher.cpp"\r
                             "../../../../ext/olm/src/crypto.cpp"\r
index 8d26c9351a6bbe7ba6dd1fa421fb9288980d8c6a..d0c6abbd14f5c3a0d1fdfe7244ea4cbef2d338e5 100644 (file)
@@ -1,2 +1,2 @@
-idf_component_register(SRCS "main.c"\r
+idf_component_register(SRCS "main.c" "wifi.c"\r
                     INCLUDE_DIRS "")
\ No newline at end of file
index d53fdf78332261d0767bc592d7cefe71c41ae431..f4d3c8c81d373bf39aaf51f393dc7d2ce32f7fa0 100644 (file)
@@ -14,6 +14,8 @@
 #include <olm/olm.h>\r
 #include <matrix.h>\r
 \r
+#include <esp_wifi.h>\r
+\r
 #define SERVER       "https://matrix.org"\r
 #define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO"\r
 #define DEVICE_ID    "MAZNCCZLBR"\r
@@ -26,6 +28,9 @@ app_main(void)
     MatrixClientInit(&client,\r
         SERVER);\r
     \r
+    void wifi_init(const char *ssid, const char *pass);\r
+    wifi_init("Hundehuette", "Affensicherespw55");\r
+\r
     MatrixHttpInit(&client);\r
 \r
     MatrixClientSetAccessToken(&client,\r
diff --git a/esp32/esp_project/main/wifi.c b/esp32/esp_project/main/wifi.c
new file mode 100644 (file)
index 0000000..07d1e41
--- /dev/null
@@ -0,0 +1,104 @@
+// Code taken from the ESP32 IDF WiFi station Example\r
+\r
+#include <string.h>\r
+#include "esp_event.h"\r
+#include "esp_log.h"\r
+#include "esp_system.h"\r
+#include "esp_wifi.h"\r
+#include "freertos/FreeRTOS.h"\r
+#include "freertos/event_groups.h"\r
+#include "freertos/task.h"\r
+#include "nvs_flash.h"\r
+\r
+#include "lwip/err.h"\r
+#include "lwip/sys.h"\r
+\r
+#include "mongoose.h"\r
+\r
+static EventGroupHandle_t s_wifi_event_group;\r
+\r
+/* The event group allows multiple bits for each event, but we only care about\r
+ * two events:\r
+ * - we are connected to the AP with an IP\r
+ * - we failed to connect after the maximum amount of retries */\r
+#define WIFI_CONNECTED_BIT BIT0\r
+#define WIFI_FAIL_BIT BIT1\r
+\r
+static int s_retry_num = 0;\r
+\r
+static void event_handler(void *arg, esp_event_base_t event_base,\r
+                          int32_t event_id, void *event_data) {\r
+  if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {\r
+    esp_wifi_connect();\r
+  } else if (event_base == WIFI_EVENT &&\r
+             event_id == WIFI_EVENT_STA_DISCONNECTED) {\r
+    if (s_retry_num < 3) {\r
+      esp_wifi_connect();\r
+      s_retry_num++;\r
+      MG_INFO(("retry to connect to the AP"));\r
+    } else {\r
+      xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);\r
+    }\r
+    MG_ERROR(("connect to the AP fail"));\r
+  } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {\r
+    ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;\r
+    MG_INFO(("IP ADDRESS:" IPSTR, IP2STR(&event->ip_info.ip)));\r
+    s_retry_num = 0;\r
+    xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);\r
+  }\r
+}\r
+\r
+void wifi_init(const char *ssid, const char *pass) {\r
+  esp_err_t ret = nvs_flash_init();\r
+  if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||\r
+      ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {\r
+    ESP_ERROR_CHECK(nvs_flash_erase());\r
+    ret = nvs_flash_init();\r
+  }\r
+  ESP_ERROR_CHECK(ret);\r
+\r
+  s_wifi_event_group = xEventGroupCreate();\r
+\r
+  ESP_ERROR_CHECK(esp_netif_init());\r
+\r
+  ESP_ERROR_CHECK(esp_event_loop_create_default());\r
+  esp_netif_create_default_wifi_sta();\r
+\r
+  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();\r
+  ESP_ERROR_CHECK(esp_wifi_init(&cfg));\r
+\r
+  esp_event_handler_instance_t instance_any_id;\r
+  esp_event_handler_instance_t instance_got_ip;\r
+  ESP_ERROR_CHECK(esp_event_handler_instance_register(\r
+      WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));\r
+  ESP_ERROR_CHECK(esp_event_handler_instance_register(\r
+      IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));\r
+\r
+  wifi_config_t c = {.sta = {.threshold = {.authmode = WIFI_AUTH_WPA2_PSK},\r
+                             .pmf_cfg = {.capable = true, .required = false}}};\r
+  snprintf((char *) c.sta.ssid, sizeof(c.sta.ssid), "%s", ssid);\r
+  snprintf((char *) c.sta.password, sizeof(c.sta.password), "%s", pass);\r
+  ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));\r
+  ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &c));\r
+  ESP_ERROR_CHECK(esp_wifi_start());\r
+  MG_DEBUG(("wifi_init_sta finished."));\r
+\r
+  EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,\r
+                                         WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,\r
+                                         pdFALSE, pdFALSE, portMAX_DELAY);\r
+\r
+  if (bits & WIFI_CONNECTED_BIT) {\r
+    MG_INFO(("connected to ap SSID:%s password:%s", ssid, pass));\r
+  } else if (bits & WIFI_FAIL_BIT) {\r
+    MG_ERROR(("Failed to connect to SSID:%s, password:%s", ssid, pass));\r
+  } else {\r
+    MG_ERROR(("UNEXPECTED EVENT"));\r
+  }\r
+\r
+  /* The event will not be processed after unregister */\r
+  ESP_ERROR_CHECK(esp_event_handler_instance_unregister(\r
+      IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));\r
+  ESP_ERROR_CHECK(esp_event_handler_instance_unregister(\r
+      WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));\r
+  vEventGroupDelete(s_wifi_event_group);\r
+}
\ No newline at end of file
index f405b2ca301a1035933bc7963eaa6078e82c852b..c55b69175efeeaf0e0b27aa15eb9531b92214989 100644 (file)
@@ -325,12 +325,12 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200
 #\r
 # Partition Table\r
 #\r
-CONFIG_PARTITION_TABLE_SINGLE_APP=y\r
-# CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set\r
+# CONFIG_PARTITION_TABLE_SINGLE_APP is not set\r
+CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y\r
 # CONFIG_PARTITION_TABLE_TWO_OTA is not set\r
 # CONFIG_PARTITION_TABLE_CUSTOM is not set\r
 CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"\r
-CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp.csv"\r
+CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp_large.csv"\r
 CONFIG_PARTITION_TABLE_OFFSET=0x8000\r
 CONFIG_PARTITION_TABLE_MD5=y\r
 # end of Partition Table\r
diff --git a/examples/Cli.c b/examples/Cli.c
new file mode 100644 (file)
index 0000000..af1d6b1
--- /dev/null
@@ -0,0 +1,153 @@
+#include <stdio.h>\r
+#include <stdarg.h>\r
+\r
+#include <mjson.h>\r
+#include <matrix.h>\r
+\r
+#define SERVER       "https://matrix.org"\r
+#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO"\r
+#define DEVICE_ID    "MAZNCCZLBR"\r
+#define USER_ID      "@pscho:matrix.org"\r
+#define ROOM_ID      "!XKFUjAsGrSSrpDFIxB:matrix.org"\r
+\r
+#define BUFFER_SIZE 1024\r
+#define NUMBER_ARGS 10\r
+\r
+void\r
+GetCommand(\r
+    char * cmd,\r
+    int * nargs,\r
+    char ** args\r
+) {\r
+    int index = 0;\r
+    int c;\r
+\r
+    *nargs = 0;\r
+\r
+    printf("> ");\r
+    while ((c = getchar()), c != ' ' && c != '\n')\r
+        cmd[index++] = c;\r
+    cmd[index] = '\0';\r
+\r
+    if (c == '\n')\r
+        return;\r
+    \r
+    *nargs = 1;\r
+    index = 0;\r
+    char * arg = args[0];\r
+    while ((c = getchar()), c != '\n') {\r
+        if (c == ' ') {\r
+            arg[index] = '\0';\r
+            arg = args[(*nargs)++];\r
+            index = 0;\r
+            continue;\r
+        }\r
+        arg[index++] = c;\r
+    }\r
+    arg[index] = '\0';\r
+}\r
+\r
+bool\r
+CheckCommand(\r
+    const char * cmd,\r
+    const char * str\r
+) {\r
+    if (strlen(cmd) != strlen(str))\r
+        return false;\r
+    \r
+    for (size_t i = 0; i < strlen(cmd); i++) {\r
+        if (cmd[i] != str[i])\r
+            return false;\r
+    }\r
+    return true;\r
+}\r
+\r
+void\r
+Usage(\r
+    const char * cmd,\r
+    const char * args\r
+) {\r
+    printf("Usage: %s %s\n", cmd, args);\r
+}\r
+\r
+void\r
+ExecuteCommand(\r
+    MatrixClient * client,\r
+    const char * cmd,\r
+    int nargs, char ** args\r
+) {\r
+    /**/ if (CheckCommand(cmd, "devicekey")) {\r
+        printf("%s\n", client->deviceKey);\r
+    }\r
+    else if (CheckCommand(cmd, "genkeys")) {\r
+        if (nargs != 1) {\r
+            Usage(cmd, "<number of keys>");\r
+            return;\r
+        }\r
+        MatrixClientGenerateOnetimeKeys(client, atoi(args[0]));\r
+    }\r
+    else if (CheckCommand(cmd, "uploadkeys")) {\r
+        MatrixClientUploadOnetimeKeys(client);\r
+    }\r
+    else if (CheckCommand(cmd, "onetimekeys")) {\r
+        static char buffer[1024];\r
+        olm_account_one_time_keys(client->olmAccount.account, buffer, 1024);\r
+        printf("%s\n", buffer);\r
+    }\r
+    else if (CheckCommand(cmd, "getkeys")) {\r
+        MatrixClientRequestDeviceKeys(client);\r
+        for (int i = 0; i < client->numDevices; i++)\r
+            printf("id: %s  key: %s\n",\r
+                client->devices[i].deviceId,\r
+                client->devices[i].deviceKey);\r
+    }\r
+    else if (CheckCommand(cmd, "todevice")) {\r
+        static char buffer[30000];\r
+        MatrixClientSync(client,\r
+            buffer, 30000);\r
+        const char * todevice;\r
+        int todeviceLen;\r
+        mjson_find(buffer, 30000,\r
+            "$.to_device",\r
+            &todevice, &todeviceLen);\r
+        static char prettyBuffer[10000];\r
+        struct mjson_fixedbuf fb = { prettyBuffer, 10000, 0 };\r
+        mjson_pretty(todevice, todeviceLen,\r
+            "  ", mjson_print_fixed_buf, &fb);\r
+        printf("%.*s\n", fb.len, fb.ptr);\r
+    }\r
+}\r
+\r
+int\r
+main(void)\r
+{\r
+    MatrixClient client;\r
+    MatrixClientInit(&client,\r
+        SERVER);\r
+    \r
+    MatrixHttpInit(&client);\r
+\r
+    MatrixClientSetAccessToken(&client,\r
+        ACCESS_TOKEN);\r
+    MatrixClientSetDeviceId(&client,\r
+        DEVICE_ID);\r
+    MatrixClientSetUserId(&client,\r
+        USER_ID);\r
+\r
+    static char cmd[BUFFER_SIZE];\r
+    static char args_[BUFFER_SIZE][NUMBER_ARGS];\r
+    char * args[NUMBER_ARGS];\r
+    for (int i = 0; i < NUMBER_ARGS; i++)\r
+        args[i] = args_[i];\r
+    int nargs;\r
+    do {\r
+        GetCommand(cmd, &nargs, args);\r
+\r
+        ExecuteCommand(&client, cmd, nargs, args);\r
+        \r
+    } while (strcmp(cmd, "exit") != 0);\r
+        \r
+    MatrixHttpDeinit(&client);\r
+\r
+    return 0;\r
+}\r
diff --git a/examples/Keys.rdbg b/examples/Keys.rdbg
new file mode 100644 (file)
index 0000000..7bea6fe
Binary files /dev/null and b/examples/Keys.rdbg differ
diff --git a/examples/ReplyRoomkey.c b/examples/ReplyRoomkey.c
new file mode 100644 (file)
index 0000000..8d61a4c
--- /dev/null
@@ -0,0 +1,40 @@
+#include <matrix.h>\r
+#include <stdio.h>\r
+\r
+#define SERVER       "https://matrix.org"\r
+#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO"\r
+#define DEVICE_ID    "MAZNCCZLBR"\r
+#define USER_ID      "@pscho:matrix.org"\r
+#define ROOM_ID      "!XKFUjAsGrSSrpDFIxB:matrix.org"\r
+\r
+int\r
+main(void)\r
+{\r
+    MatrixClient client;\r
+    MatrixClientInit(&client,\r
+        SERVER);\r
+    \r
+    MatrixHttpInit(&client);\r
+\r
+    MatrixClientSetAccessToken(&client,\r
+        ACCESS_TOKEN);\r
+    MatrixClientSetDeviceId(&client,\r
+        DEVICE_ID);\r
+    MatrixClientSetUserId(&client,\r
+        USER_ID);\r
+\r
+\r
+    MatrixClientSendEventEncrypted(&client,\r
+        ROOM_ID,\r
+        "m.room.message",\r
+        "{\"body\":\"Hello\",\"msgtype\":\"m.text\"}");\r
+\r
+    MatrixClientShareMegolmOutSession(&client,\r
+        USER_ID,\r
+        "ULZZOKJBYN",\r
+        &client.megolmOutSessions[0]);\r
+        \r
+    MatrixHttpDeinit(&client);\r
+\r
+    return 0;\r
+}\r
diff --git a/examples/SendEncrypted.rdbg b/examples/SendEncrypted.rdbg
new file mode 100644 (file)
index 0000000..120f0a0
Binary files /dev/null and b/examples/SendEncrypted.rdbg differ
index 5e5eabdd6e7d5c90acf652fb451dcbb72feb7da0..32b8294c835dd82c90bc0276c6c378d21dbf783f 100644 (file)
@@ -283,6 +283,7 @@ MatrixClientRequestDeviceKeys(
 \r
 \r
 \r
+\r
 bool\r
 MatrixHttpInit(\r
     MatrixClient * client);\r
index 0b6c267cd90e769a483f583337b2453b583aeb0e..a514f72b301eccef9009d466fee310228d82d024 100644 (file)
@@ -56,7 +56,7 @@ MatrixHttpCallback(
         conn->dataLen = hm->body.len;\r
         conn->dataReceived = true;\r
 \r
-        printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data);\r
+        //printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data);\r
     }\r
     if (ev == MG_EV_CLOSE)\r
     {\r
@@ -86,7 +86,7 @@ MatrixHttpConnect(
     MatrixHttpConnection * conn =\r
         (MatrixHttpConnection *)client->httpUserData;\r
     \r
-    struct mg_connection * c =\r
+    //struct mg_connection * c =\r
         mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);\r
 \r
     while (! conn->connected)\r