From 408406c84fb51c01fa9b2055d44ddcefad00ab60 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 14 Mar 2023 18:09:18 -0500 Subject: [PATCH 1/3] starting diskinfo api --- supervisor/shared/web_workflow/web_workflow.c | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/supervisor/shared/web_workflow/web_workflow.c b/supervisor/shared/web_workflow/web_workflow.c index 97530b970ba1c..8b3250a0ec8f8 100644 --- a/supervisor/shared/web_workflow/web_workflow.c +++ b/supervisor/shared/web_workflow/web_workflow.c @@ -828,6 +828,46 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request * _send_chunk(socket, ""); } +static void _reply_with_diskinfo_json(socketpool_socket_obj_t *socket, _request *request) { + _send_str(socket, OK_JSON); + _cors_header(socket, request); + _send_str(socket, "\r\n"); + mp_print_t _socket_print = {socket, _print_chunk}; + + const char *hostname = ""; + #if CIRCUITPY_MDNS + if (!common_hal_mdns_server_deinited(&mdns)) { + hostname = common_hal_mdns_server_get_hostname(&mdns); + } + #endif + _update_encoded_ip(); + // Note: this leverages the fact that C concats consecutive string literals together. + mp_printf(&_socket_print, + "{\"web_api_version\": 1, " + "\"version\": \"" MICROPY_GIT_TAG "\", " + "\"build_date\": \"" MICROPY_BUILD_DATE "\", " + "\"board_name\": \"" MICROPY_HW_BOARD_NAME "\", " + "\"mcu_name\": \"" MICROPY_HW_MCU_NAME "\", " + "\"board_id\": \"" CIRCUITPY_BOARD_ID "\", " + "\"creator_id\": %u, " + "\"creation_id\": %u, " + "\"hostname\": \"%s\", " + "\"port\": %d, ", CIRCUITPY_CREATOR_ID, CIRCUITPY_CREATION_ID, hostname, web_api_port, _our_ip_encoded); + #if CIRCUITPY_MICROCONTROLLER && COMMON_HAL_MCU_PROCESSOR_UID_LENGTH > 0 + uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH]; + common_hal_mcu_processor_get_uid(raw_id); + mp_printf(&_socket_print, "\"UID\": \""); + for (uint8_t i = 0; i < COMMON_HAL_MCU_PROCESSOR_UID_LENGTH; i++) { + mp_printf(&_socket_print, "%02X", raw_id[i]); + } + mp_printf(&_socket_print, "\", "); + #endif + mp_printf(&_socket_print, "\"ip\": \"%s\"}", _our_ip_encoded); + // Empty chunk signals the end of the response. + _send_chunk(socket, ""); +} + + // FATFS has a two second timestamp resolution but the BLE API allows for nanosecond resolution. // This function truncates the time the time to a resolution storable by FATFS and fills in the // FATFS encoded version into fattime. @@ -1228,6 +1268,8 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) { _reply_with_devices_json(socket, request); } else if (strcmp(path, "/version.json") == 0) { _reply_with_version_json(socket, request); + } else if (strcmp(path, "/diskinfo.json") == 0) { + _reply_with_diskinfo_json(socket, request); } else if (strcmp(path, "/serial/") == 0) { if (!request->authenticated) { if (_api_password[0] != '\0') { From a1506df805b127d715f275248d1b82457553a388 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 17 Mar 2023 21:36:13 -0500 Subject: [PATCH 2/3] implement real diskinfo functionality --- supervisor/shared/web_workflow/web_workflow.c | 41 +++++++------------ 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/supervisor/shared/web_workflow/web_workflow.c b/supervisor/shared/web_workflow/web_workflow.c index 8b3250a0ec8f8..c006a9e7cb466 100644 --- a/supervisor/shared/web_workflow/web_workflow.c +++ b/supervisor/shared/web_workflow/web_workflow.c @@ -49,6 +49,7 @@ #include "shared-bindings/hashlib/__init__.h" #include "shared-bindings/hashlib/Hash.h" +#include "lib/oofatfs/diskio.h" #if CIRCUITPY_MDNS #include "shared-bindings/mdns/RemoteService.h" @@ -834,35 +835,21 @@ static void _reply_with_diskinfo_json(socketpool_socket_obj_t *socket, _request _send_str(socket, "\r\n"); mp_print_t _socket_print = {socket, _print_chunk}; - const char *hostname = ""; - #if CIRCUITPY_MDNS - if (!common_hal_mdns_server_deinited(&mdns)) { - hostname = common_hal_mdns_server_get_hostname(&mdns); + DWORD free_clusters; + FATFS *fs = filesystem_circuitpy(); + FRESULT blk_result = f_getfree(fs, &free_clusters); + uint16_t block_size; + if (blk_result == FR_OK) { + disk_ioctl(fs, GET_SECTOR_SIZE, &block_size); } - #endif - _update_encoded_ip(); - // Note: this leverages the fact that C concats consecutive string literals together. + + uint16_t total_size = fs->n_fatent - 2; + mp_printf(&_socket_print, - "{\"web_api_version\": 1, " - "\"version\": \"" MICROPY_GIT_TAG "\", " - "\"build_date\": \"" MICROPY_BUILD_DATE "\", " - "\"board_name\": \"" MICROPY_HW_BOARD_NAME "\", " - "\"mcu_name\": \"" MICROPY_HW_MCU_NAME "\", " - "\"board_id\": \"" CIRCUITPY_BOARD_ID "\", " - "\"creator_id\": %u, " - "\"creation_id\": %u, " - "\"hostname\": \"%s\", " - "\"port\": %d, ", CIRCUITPY_CREATOR_ID, CIRCUITPY_CREATION_ID, hostname, web_api_port, _our_ip_encoded); - #if CIRCUITPY_MICROCONTROLLER && COMMON_HAL_MCU_PROCESSOR_UID_LENGTH > 0 - uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH]; - common_hal_mcu_processor_get_uid(raw_id); - mp_printf(&_socket_print, "\"UID\": \""); - for (uint8_t i = 0; i < COMMON_HAL_MCU_PROCESSOR_UID_LENGTH; i++) { - mp_printf(&_socket_print, "%02X", raw_id[i]); - } - mp_printf(&_socket_print, "\", "); - #endif - mp_printf(&_socket_print, "\"ip\": \"%s\"}", _our_ip_encoded); + "{\"free\": %d, " + "\"block_size\": %d, " + "\"total\": %d}", free_clusters * block_size, block_size, total_size * block_size); + // Empty chunk signals the end of the response. _send_chunk(socket, ""); } From 0773a2bd6d741d791734a96a3cf5ca3e7fd3e167 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 20 Mar 2023 17:32:00 -0500 Subject: [PATCH 3/3] increment web_api_version --- supervisor/shared/web_workflow/web_workflow.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervisor/shared/web_workflow/web_workflow.c b/supervisor/shared/web_workflow/web_workflow.c index c006a9e7cb466..2caacbd39b0be 100644 --- a/supervisor/shared/web_workflow/web_workflow.c +++ b/supervisor/shared/web_workflow/web_workflow.c @@ -805,7 +805,7 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request * _update_encoded_ip(); // Note: this leverages the fact that C concats consecutive string literals together. mp_printf(&_socket_print, - "{\"web_api_version\": 1, " + "{\"web_api_version\": 2, " "\"version\": \"" MICROPY_GIT_TAG "\", " "\"build_date\": \"" MICROPY_BUILD_DATE "\", " "\"board_name\": \"" MICROPY_HW_BOARD_NAME "\", "