2022-03-03 01:59:30 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2022 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/objstr.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS
|
2022-03-03 01:59:30 -05:00
|
|
|
|
|
|
|
#include "extmod/misc.h"
|
|
|
|
#include "extmod/vfs.h"
|
|
|
|
|
|
|
|
#if MICROPY_VFS_FAT
|
|
|
|
#include "extmod/vfs_fat.h"
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_SYNC
|
2023-05-30 15:44:36 -04:00
|
|
|
#include "lib/oofatfs/ff.h"
|
|
|
|
#include "lib/oofatfs/diskio.h"
|
|
|
|
#endif
|
2022-03-03 01:59:30 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2
|
|
|
|
#include "extmod/vfs_lfs.h"
|
|
|
|
#endif
|
|
|
|
|
2022-03-08 20:32:44 -05:00
|
|
|
#if MICROPY_VFS_POSIX
|
|
|
|
#include "extmod/vfs_posix.h"
|
|
|
|
#endif
|
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_UNAME
|
2022-03-03 01:59:30 -05:00
|
|
|
#include "genhdr/mpversion.h"
|
|
|
|
#endif
|
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#ifdef MICROPY_PY_OS_INCLUDEFILE
|
|
|
|
#include MICROPY_PY_OS_INCLUDEFILE
|
2022-03-03 01:59:30 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MICROPY_BUILD_TYPE
|
|
|
|
#define MICROPY_BUILD_TYPE_PAREN " (" MICROPY_BUILD_TYPE ")"
|
|
|
|
#else
|
|
|
|
#define MICROPY_BUILD_TYPE_PAREN
|
|
|
|
#endif
|
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_SYNC
|
2023-05-30 15:44:36 -04:00
|
|
|
// sync()
|
|
|
|
// Sync all filesystems.
|
2022-08-18 00:47:56 -04:00
|
|
|
STATIC mp_obj_t mp_os_sync(void) {
|
2023-05-30 15:44:36 -04:00
|
|
|
#if MICROPY_VFS_FAT
|
|
|
|
for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
|
|
|
|
// this assumes that vfs->obj is fs_user_mount_t with block device functions
|
|
|
|
disk_ioctl(MP_OBJ_TO_PTR(vfs->obj), CTRL_SYNC, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-08-18 00:47:56 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_os_sync_obj, mp_os_sync);
|
2023-05-30 15:44:36 -04:00
|
|
|
#endif
|
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_UNAME
|
2022-03-03 07:46:16 -05:00
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_UNAME_RELEASE_DYNAMIC
|
2022-03-03 07:46:16 -05:00
|
|
|
#define CONST_RELEASE
|
|
|
|
#else
|
|
|
|
#define CONST_RELEASE const
|
|
|
|
#endif
|
|
|
|
|
2022-08-18 00:47:56 -04:00
|
|
|
STATIC const qstr mp_os_uname_info_fields[] = {
|
2022-03-03 01:59:30 -05:00
|
|
|
MP_QSTR_sysname,
|
|
|
|
MP_QSTR_nodename,
|
|
|
|
MP_QSTR_release,
|
|
|
|
MP_QSTR_version,
|
|
|
|
MP_QSTR_machine
|
|
|
|
};
|
2022-08-18 00:47:56 -04:00
|
|
|
STATIC const MP_DEFINE_STR_OBJ(mp_os_uname_info_sysname_obj, MICROPY_PY_SYS_PLATFORM);
|
|
|
|
STATIC const MP_DEFINE_STR_OBJ(mp_os_uname_info_nodename_obj, MICROPY_PY_SYS_PLATFORM);
|
|
|
|
STATIC CONST_RELEASE MP_DEFINE_STR_OBJ(mp_os_uname_info_release_obj, MICROPY_VERSION_STRING);
|
|
|
|
STATIC const MP_DEFINE_STR_OBJ(mp_os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE MICROPY_BUILD_TYPE_PAREN);
|
|
|
|
STATIC const MP_DEFINE_STR_OBJ(mp_os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME);
|
2022-03-03 01:59:30 -05:00
|
|
|
|
|
|
|
STATIC MP_DEFINE_ATTRTUPLE(
|
2022-08-18 00:47:56 -04:00
|
|
|
mp_os_uname_info_obj,
|
|
|
|
mp_os_uname_info_fields,
|
2022-03-03 01:59:30 -05:00
|
|
|
5,
|
2022-08-18 00:47:56 -04:00
|
|
|
MP_ROM_PTR(&mp_os_uname_info_sysname_obj),
|
|
|
|
MP_ROM_PTR(&mp_os_uname_info_nodename_obj),
|
|
|
|
MP_ROM_PTR(&mp_os_uname_info_release_obj),
|
|
|
|
MP_ROM_PTR(&mp_os_uname_info_version_obj),
|
|
|
|
MP_ROM_PTR(&mp_os_uname_info_machine_obj)
|
2022-03-03 01:59:30 -05:00
|
|
|
);
|
|
|
|
|
2022-08-18 00:47:56 -04:00
|
|
|
STATIC mp_obj_t mp_os_uname(void) {
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_UNAME_RELEASE_DYNAMIC
|
2022-08-18 00:47:56 -04:00
|
|
|
const char *release = mp_os_uname_release();
|
|
|
|
mp_os_uname_info_release_obj.len = strlen(release);
|
|
|
|
mp_os_uname_info_release_obj.data = (const byte *)release;
|
2022-03-03 07:46:16 -05:00
|
|
|
#endif
|
2022-08-18 00:47:56 -04:00
|
|
|
return MP_OBJ_FROM_PTR(&mp_os_uname_info_obj);
|
2022-03-03 01:59:30 -05:00
|
|
|
}
|
2022-08-18 00:47:56 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_os_uname_obj, mp_os_uname);
|
2022-03-03 07:46:16 -05:00
|
|
|
|
2022-03-03 03:17:12 -05:00
|
|
|
#endif
|
2022-03-03 01:59:30 -05:00
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
|
2022-08-17 02:18:31 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_os) },
|
2022-03-03 01:59:30 -05:00
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_GETENV_PUTENV_UNSETENV
|
2022-08-18 00:47:56 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getenv), MP_ROM_PTR(&mp_os_getenv_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_putenv), MP_ROM_PTR(&mp_os_putenv_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_unsetenv), MP_ROM_PTR(&mp_os_unsetenv_obj) },
|
2022-03-08 20:32:44 -05:00
|
|
|
#endif
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_SEP
|
2022-03-03 07:15:59 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sep), MP_ROM_QSTR(MP_QSTR__slash_) },
|
|
|
|
#endif
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_SYNC
|
2022-08-18 00:47:56 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sync), MP_ROM_PTR(&mp_os_sync_obj) },
|
2022-03-03 07:15:59 -05:00
|
|
|
#endif
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_SYSTEM
|
2022-08-18 00:47:56 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&mp_os_system_obj) },
|
2022-03-08 20:32:44 -05:00
|
|
|
#endif
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_UNAME
|
2022-08-18 00:47:56 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_uname), MP_ROM_PTR(&mp_os_uname_obj) },
|
2022-03-03 03:17:12 -05:00
|
|
|
#endif
|
2022-08-17 02:18:31 -04:00
|
|
|
#if MICROPY_PY_OS_URANDOM
|
2022-08-18 00:47:56 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&mp_os_urandom_obj) },
|
2022-03-03 01:59:30 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_VFS
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&mp_vfs_chdir_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&mp_vfs_getcwd_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&mp_vfs_listdir_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mp_vfs_mkdir_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&mp_vfs_remove_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&mp_vfs_rename_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&mp_vfs_rmdir_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&mp_vfs_stat_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&mp_vfs_statvfs_obj) },
|
2022-03-03 03:17:12 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&mp_vfs_remove_obj) }, // unlink aliases to remove
|
2022-03-03 01:59:30 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// The following are MicroPython extensions.
|
|
|
|
|
|
|
|
#if MICROPY_PY_OS_DUPTERM
|
2022-08-18 00:47:56 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mp_os_dupterm_obj) },
|
2022-03-03 01:59:30 -05:00
|
|
|
#endif
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_DUPTERM_NOTIFY
|
2022-08-18 00:47:56 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_dupterm_notify), MP_ROM_PTR(&mp_os_dupterm_notify_obj) },
|
2022-03-03 03:09:01 -05:00
|
|
|
#endif
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_OS_ERRNO
|
2022-08-18 00:47:56 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mp_os_errno_obj) },
|
2022-03-08 20:32:44 -05:00
|
|
|
#endif
|
2022-03-03 01:59:30 -05:00
|
|
|
|
|
|
|
#if MICROPY_VFS
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&mp_vfs_ilistdir_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_vfs_umount_obj) },
|
|
|
|
#if MICROPY_VFS_FAT
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
|
|
|
|
#endif
|
|
|
|
#if MICROPY_VFS_LFS1
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_VfsLfs1), MP_ROM_PTR(&mp_type_vfs_lfs1) },
|
|
|
|
#endif
|
|
|
|
#if MICROPY_VFS_LFS2
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_VfsLfs2), MP_ROM_PTR(&mp_type_vfs_lfs2) },
|
|
|
|
#endif
|
2022-03-08 20:32:44 -05:00
|
|
|
#if MICROPY_VFS_POSIX
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_VfsPosix), MP_ROM_PTR(&mp_type_vfs_posix) },
|
|
|
|
#endif
|
2022-03-03 01:59:30 -05:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);
|
|
|
|
|
2022-08-18 00:47:56 -04:00
|
|
|
const mp_obj_module_t mp_module_os = {
|
2022-03-03 01:59:30 -05:00
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&os_module_globals,
|
|
|
|
};
|
|
|
|
|
2022-08-18 00:47:56 -04:00
|
|
|
MP_REGISTER_MODULE(MP_QSTR_os, mp_module_os);
|
2022-04-20 02:06:22 -04:00
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#endif // MICROPY_PY_OS
|