List all supported storage devices

This code snippet lists all available storages supported on the device and prints their type, root path and status.
#include <dlog.h> //for logging purposes
#include <storage.h>

//Callback to be executed on each supported storage device available.
bool storage_device_callback (int storage_id, storage_type_e type, storage_state_e state, const char *path, void *user_data)
{
	// In this snippet we only print to logs the properties we received along with the callback.
	// However, when needed, using storage_id received here, you an retrieve more storage properties.

	dlog_print(DLOG_INFO, LOG_TAG, "Storage device found!");
	dlog_print(DLOG_INFO, LOG_TAG, "\tStorage ID: %d", storage_id);
	dlog_print(DLOG_INFO, LOG_TAG, "\tRoot path: %s", path);

	switch(type)
	{
	case STORAGE_TYPE_INTERNAL:
		dlog_print(DLOG_INFO, LOG_TAG, "\tType: internal");
		break;
	case STORAGE_TYPE_EXTERNAL:
		dlog_print(DLOG_INFO, LOG_TAG, "\tType: external");
		break;
	default:
		dlog_print(DLOG_INFO, LOG_TAG, "\tType: invalid type! %d", type);
	}

	switch(state)
	{
	case STORAGE_STATE_UNMOUNTABLE:
		dlog_print(DLOG_INFO, LOG_TAG, "\tState: unmountable (file system corrupt or other problem)");
		break;
	case STORAGE_STATE_REMOVED:
		dlog_print(DLOG_INFO, LOG_TAG, "\tState: removed (currently no storage present)");
		break;
	case STORAGE_STATE_MOUNTED:
		dlog_print(DLOG_INFO, LOG_TAG, "\tState: mounted with read/write access");
		break;
	case STORAGE_STATE_MOUNTED_READ_ONLY:
		dlog_print(DLOG_INFO, LOG_TAG, "\tState: mounted with read only access");
		break;
	default:
		dlog_print(DLOG_INFO, LOG_TAG, "\tState: Invalid state! %d", state);
	}
	return true;
}

// Function listing all supported storage devices
void list_supported_storage_devices()
{
	if (storage_foreach_device_supported(storage_device_callback, NULL) != STORAGE_ERROR_NONE)
	{
		dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when trying to list supported storage devices!");
	}
}

Responses

0 Replies