From a8f5cb567c08c7952e3a1f585df995c3459ff1bf Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Sun, 4 Apr 2021 15:29:52 +0200
Subject: [PATCH] feat(pycardium): Expose epic_fs_is_attached() in the `os`
 module

Add an `os.fs_is_attached()` function to allow apps to check whether the
filesystem is available.
---
 Documentation/pycardium/os.rst | 14 ++++++++++++++
 pycardium/modules/os.c         | 12 ++++++++++++
 pycardium/modules/qstrdefs.h   |  1 +
 3 files changed, 27 insertions(+)

diff --git a/Documentation/pycardium/os.rst b/Documentation/pycardium/os.rst
index d2fd7cf2..7915fccd 100644
--- a/Documentation/pycardium/os.rst
+++ b/Documentation/pycardium/os.rst
@@ -115,3 +115,17 @@ Card10-Specific
 .. py:data:: USB_FLASH
 
    Mass-Storage device active.
+
+.. py:function:: fs_is_attached()
+
+   Check whether the filesystem is currently attached to card10 (or whether a connected
+   USB host is currently holding control over it and possibly writing to it).
+
+   :returns:
+
+      - ``True`` if the filesystem is attached to card10 and an app can read and
+        write files.
+      - ``False`` if the filesystem is not available to card10 because a USB
+        host is currently controlling it.
+
+   .. versionadded: 1.18
diff --git a/pycardium/modules/os.c b/pycardium/modules/os.c
index ea8b4d0f..ad704f10 100644
--- a/pycardium/modules/os.c
+++ b/pycardium/modules/os.c
@@ -220,6 +220,16 @@ static mp_obj_t mp_os_usbconfig(mp_obj_t dev)
 }
 static MP_DEFINE_CONST_FUN_OBJ_1(usbconfig_obj, mp_os_usbconfig);
 
+static mp_obj_t mp_os_fs_is_attached(void)
+{
+	if (epic_fs_is_attached()) {
+		return mp_const_true;
+	} else {
+		return mp_const_false;
+	}
+}
+static MP_DEFINE_CONST_FUN_OBJ_0(fs_is_attached_obj, mp_os_fs_is_attached);
+
 static const mp_rom_map_elem_t os_module_globals_table[] = {
 	{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_os) },
 	{ MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&exit_obj) },
@@ -232,6 +242,8 @@ static const mp_rom_map_elem_t os_module_globals_table[] = {
 	{ MP_ROM_QSTR(MP_QSTR_read_battery), MP_ROM_PTR(&read_battery_obj) },
 	{ MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&urandom_obj) },
 	{ MP_ROM_QSTR(MP_QSTR_usbconfig), MP_ROM_PTR(&usbconfig_obj) },
+	{ MP_ROM_QSTR(MP_QSTR_fs_is_attached),
+	  MP_ROM_PTR(&fs_is_attached_obj) },
 
 	{ MP_ROM_QSTR(MP_QSTR_USB_SERIAL), MP_ROM_INT(USB_DEVICE_SERIAL) },
 	{ MP_ROM_QSTR(MP_QSTR_USB_FLASH), MP_ROM_INT(USB_DEVICE_FLASH) },
diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h
index ad366580..01f32c67 100644
--- a/pycardium/modules/qstrdefs.h
+++ b/pycardium/modules/qstrdefs.h
@@ -154,6 +154,7 @@ Q(usbconfig)
 Q(USB_FLASH)
 Q(USB_SERIAL)
 Q(USB_NONE)
+Q(fs_is_attached)
 
 /* gpio */
 Q(gpio)
-- 
GitLab