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