Skip to content

Commit

Permalink
Merge pull request #7746 from FoamyGuy/diskinfo_api
Browse files Browse the repository at this point in the history
Diskinfo api for web workflow
  • Loading branch information
tannewt authored Mar 21, 2023
2 parents 4aa1896 + 0773a2b commit c78502d
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion supervisor/shared/web_workflow/web_workflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -804,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 "\", "
Expand All @@ -828,6 +829,32 @@ 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};

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);
}

uint16_t total_size = fs->n_fatent - 2;

mp_printf(&_socket_print,
"{\"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, "");
}


// 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.
Expand Down Expand Up @@ -1228,6 +1255,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') {
Expand Down

0 comments on commit c78502d

Please sign in to comment.