Skip to content

Commit c78502d

Browse files
authored
Merge pull request #7746 from FoamyGuy/diskinfo_api
Diskinfo api for web workflow
2 parents 4aa1896 + 0773a2b commit c78502d

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
#include "shared-bindings/hashlib/__init__.h"
5151
#include "shared-bindings/hashlib/Hash.h"
52+
#include "lib/oofatfs/diskio.h"
5253

5354
#if CIRCUITPY_MDNS
5455
#include "shared-bindings/mdns/RemoteService.h"
@@ -804,7 +805,7 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request *
804805
_update_encoded_ip();
805806
// Note: this leverages the fact that C concats consecutive string literals together.
806807
mp_printf(&_socket_print,
807-
"{\"web_api_version\": 1, "
808+
"{\"web_api_version\": 2, "
808809
"\"version\": \"" MICROPY_GIT_TAG "\", "
809810
"\"build_date\": \"" MICROPY_BUILD_DATE "\", "
810811
"\"board_name\": \"" MICROPY_HW_BOARD_NAME "\", "
@@ -828,6 +829,32 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request *
828829
_send_chunk(socket, "");
829830
}
830831

832+
static void _reply_with_diskinfo_json(socketpool_socket_obj_t *socket, _request *request) {
833+
_send_str(socket, OK_JSON);
834+
_cors_header(socket, request);
835+
_send_str(socket, "\r\n");
836+
mp_print_t _socket_print = {socket, _print_chunk};
837+
838+
DWORD free_clusters;
839+
FATFS *fs = filesystem_circuitpy();
840+
FRESULT blk_result = f_getfree(fs, &free_clusters);
841+
uint16_t block_size;
842+
if (blk_result == FR_OK) {
843+
disk_ioctl(fs, GET_SECTOR_SIZE, &block_size);
844+
}
845+
846+
uint16_t total_size = fs->n_fatent - 2;
847+
848+
mp_printf(&_socket_print,
849+
"{\"free\": %d, "
850+
"\"block_size\": %d, "
851+
"\"total\": %d}", free_clusters * block_size, block_size, total_size * block_size);
852+
853+
// Empty chunk signals the end of the response.
854+
_send_chunk(socket, "");
855+
}
856+
857+
831858
// FATFS has a two second timestamp resolution but the BLE API allows for nanosecond resolution.
832859
// This function truncates the time the time to a resolution storable by FATFS and fills in the
833860
// FATFS encoded version into fattime.
@@ -1228,6 +1255,8 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
12281255
_reply_with_devices_json(socket, request);
12291256
} else if (strcmp(path, "/version.json") == 0) {
12301257
_reply_with_version_json(socket, request);
1258+
} else if (strcmp(path, "/diskinfo.json") == 0) {
1259+
_reply_with_diskinfo_json(socket, request);
12311260
} else if (strcmp(path, "/serial/") == 0) {
12321261
if (!request->authenticated) {
12331262
if (_api_password[0] != '\0') {

0 commit comments

Comments
 (0)