2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 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.
|
|
|
|
*/
|
|
|
|
|
2014-05-03 08:24:21 -04:00
|
|
|
#include <stdint.h>
|
2014-03-22 10:56:32 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-07-27 18:52:56 -04:00
|
|
|
#include "py/mpstate.h"
|
2016-10-06 22:58:25 -04:00
|
|
|
#include "py/runtime.h"
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/objtuple.h"
|
2015-04-21 10:51:49 -04:00
|
|
|
#include "py/objstr.h"
|
2015-04-22 12:38:05 -04:00
|
|
|
#include "genhdr/mpversion.h"
|
2015-03-04 15:35:41 -05:00
|
|
|
#include "lib/fatfs/ff.h"
|
|
|
|
#include "lib/fatfs/diskio.h"
|
2015-05-10 20:00:19 -04:00
|
|
|
#include "timeutils.h"
|
2014-03-22 10:56:32 -04:00
|
|
|
#include "rng.h"
|
2015-10-19 16:57:41 -04:00
|
|
|
#include "uart.h"
|
2017-01-26 23:04:17 -05:00
|
|
|
#include "extmod/vfs_fat.h"
|
2014-07-31 18:44:04 -04:00
|
|
|
#include "sdcard.h"
|
2015-11-24 17:43:11 -05:00
|
|
|
#include "extmod/fsusermount.h"
|
2014-05-03 18:27:38 -04:00
|
|
|
#include "portmodules.h"
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \module os - basic "operating system" services
|
|
|
|
///
|
|
|
|
/// The `os` module contains functions for filesystem access and `urandom`.
|
|
|
|
///
|
|
|
|
/// The filesystem has `/` as the root directory, and the available physical
|
|
|
|
/// drives are accessible from here. They are currently:
|
|
|
|
///
|
|
|
|
/// /flash -- the internal flash filesystem
|
|
|
|
/// /sd -- the SD card (if it exists)
|
|
|
|
///
|
|
|
|
/// On boot up, the current directory is `/flash` if no SD card is inserted,
|
|
|
|
/// otherwise it is `/sd`.
|
|
|
|
|
2015-04-21 10:51:49 -04:00
|
|
|
STATIC const qstr os_uname_info_fields[] = {
|
|
|
|
MP_QSTR_sysname, MP_QSTR_nodename,
|
|
|
|
MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
|
|
|
|
};
|
|
|
|
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, "pyboard");
|
|
|
|
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, "pyboard");
|
|
|
|
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, MICROPY_VERSION_STRING);
|
|
|
|
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE);
|
|
|
|
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME);
|
|
|
|
STATIC MP_DEFINE_ATTRTUPLE(
|
|
|
|
os_uname_info_obj,
|
|
|
|
os_uname_info_fields,
|
|
|
|
5,
|
|
|
|
(mp_obj_t)&os_uname_info_sysname_obj,
|
|
|
|
(mp_obj_t)&os_uname_info_nodename_obj,
|
|
|
|
(mp_obj_t)&os_uname_info_release_obj,
|
|
|
|
(mp_obj_t)&os_uname_info_version_obj,
|
|
|
|
(mp_obj_t)&os_uname_info_machine_obj
|
|
|
|
);
|
|
|
|
|
|
|
|
STATIC mp_obj_t os_uname(void) {
|
|
|
|
return (mp_obj_t)&os_uname_info_obj;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname);
|
|
|
|
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \function chdir(path)
|
|
|
|
/// Change current directory.
|
2014-07-31 18:44:04 -04:00
|
|
|
STATIC mp_obj_t os_chdir(mp_obj_t path_in) {
|
|
|
|
const char *path;
|
|
|
|
path = mp_obj_str_get_str(path_in);
|
|
|
|
|
|
|
|
FRESULT res = f_chdrive(path);
|
|
|
|
|
|
|
|
if (res == FR_OK) {
|
|
|
|
res = f_chdir(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res != FR_OK) {
|
|
|
|
// TODO should be mp_type_FileNotFoundError
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path));
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_chdir_obj, os_chdir);
|
|
|
|
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \function getcwd()
|
|
|
|
/// Get the current directory.
|
2014-07-31 18:44:04 -04:00
|
|
|
STATIC mp_obj_t os_getcwd(void) {
|
|
|
|
char buf[MICROPY_ALLOC_PATH_MAX + 1];
|
|
|
|
FRESULT res = f_getcwd(buf, sizeof buf);
|
|
|
|
|
|
|
|
if (res != FR_OK) {
|
2016-10-06 22:58:25 -04:00
|
|
|
mp_raise_OSError(fresult_to_errno_table[res]);
|
2014-07-31 18:44:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mp_obj_new_str(buf, strlen(buf), false);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_getcwd_obj, os_getcwd);
|
|
|
|
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \function listdir([dir])
|
|
|
|
/// With no argument, list the current directory. Otherwise list the given directory.
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-07-31 18:44:04 -04:00
|
|
|
bool is_str_type = true;
|
2014-03-22 10:56:32 -04:00
|
|
|
const char *path;
|
|
|
|
if (n_args == 1) {
|
2014-03-29 09:43:38 -04:00
|
|
|
if (mp_obj_get_type(args[0]) == &mp_type_bytes) {
|
2014-07-31 18:44:04 -04:00
|
|
|
is_str_type = false;
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
path = mp_obj_str_get_str(args[0]);
|
|
|
|
} else {
|
2014-07-31 18:44:04 -04:00
|
|
|
path = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// "hack" to list root directory
|
|
|
|
if (path[0] == '/' && path[1] == '\0') {
|
|
|
|
mp_obj_t dir_list = mp_obj_new_list(0, NULL);
|
2016-02-10 11:32:57 -05:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) {
|
|
|
|
fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i];
|
|
|
|
if (vfs != NULL) {
|
|
|
|
mp_obj_list_append(dir_list, mp_obj_new_str(vfs->str + 1, vfs->len - 1, false));
|
|
|
|
}
|
2014-12-27 15:20:51 -05:00
|
|
|
}
|
2014-07-31 18:44:04 -04:00
|
|
|
return dir_list;
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
|
2016-02-28 10:17:24 -05:00
|
|
|
return fat_vfs_listdir(path, is_str_type);
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \function mkdir(path)
|
|
|
|
/// Create a new directory.
|
2014-03-22 10:56:32 -04:00
|
|
|
STATIC mp_obj_t os_mkdir(mp_obj_t path_o) {
|
|
|
|
const char *path = mp_obj_str_get_str(path_o);
|
|
|
|
FRESULT res = f_mkdir(path);
|
|
|
|
switch (res) {
|
|
|
|
case FR_OK:
|
|
|
|
return mp_const_none;
|
|
|
|
case FR_EXIST:
|
|
|
|
// TODO should be FileExistsError
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "File exists: '%s'", path));
|
2014-03-22 10:56:32 -04:00
|
|
|
default:
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error creating directory '%s'", path));
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \function remove(path)
|
|
|
|
/// Remove a file.
|
2014-03-22 10:56:32 -04:00
|
|
|
STATIC mp_obj_t os_remove(mp_obj_t path_o) {
|
|
|
|
const char *path = mp_obj_str_get_str(path_o);
|
|
|
|
// TODO check that path is actually a file before trying to unlink it
|
|
|
|
FRESULT res = f_unlink(path);
|
|
|
|
switch (res) {
|
|
|
|
case FR_OK:
|
|
|
|
return mp_const_none;
|
|
|
|
default:
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing file '%s'", path));
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2015-05-10 20:30:56 -04:00
|
|
|
/// \function rename(old_path, new_path)
|
|
|
|
/// Rename a file
|
|
|
|
STATIC mp_obj_t os_rename(mp_obj_t path_in, mp_obj_t path_out) {
|
|
|
|
const char *old_path = mp_obj_str_get_str(path_in);
|
|
|
|
const char *new_path = mp_obj_str_get_str(path_out);
|
|
|
|
FRESULT res = f_rename(old_path, new_path);
|
2016-12-01 23:13:29 -05:00
|
|
|
if (res == FR_EXIST) {
|
|
|
|
// if new_path exists then try removing it
|
|
|
|
res = f_unlink(new_path);
|
|
|
|
if (res == FR_OK) {
|
|
|
|
// try to rename again
|
|
|
|
res = f_rename(old_path, new_path);
|
|
|
|
}
|
|
|
|
}
|
2015-05-10 20:30:56 -04:00
|
|
|
switch (res) {
|
|
|
|
case FR_OK:
|
|
|
|
return mp_const_none;
|
|
|
|
default:
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error renaming file '%s' to '%s'", old_path, new_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(os_rename_obj, os_rename);
|
|
|
|
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \function rmdir(path)
|
|
|
|
/// Remove a directory.
|
2014-03-22 10:56:32 -04:00
|
|
|
STATIC mp_obj_t os_rmdir(mp_obj_t path_o) {
|
|
|
|
const char *path = mp_obj_str_get_str(path_o);
|
|
|
|
// TODO check that path is actually a directory before trying to unlink it
|
|
|
|
FRESULT res = f_unlink(path);
|
|
|
|
switch (res) {
|
|
|
|
case FR_OK:
|
|
|
|
return mp_const_none;
|
|
|
|
default:
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing directory '%s'", path));
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);
|
|
|
|
|
2014-08-06 14:02:34 -04:00
|
|
|
// Checks for path equality, ignoring trailing slashes:
|
|
|
|
// path_equal(/, /) -> true
|
|
|
|
// path_equal(/flash//, /flash) -> true
|
|
|
|
// second argument must be in canonical form (meaning no trailing slash, unless it's just /)
|
|
|
|
STATIC bool path_equal(const char *path, const char *path_canonical) {
|
|
|
|
for (; *path_canonical != '\0' && *path == *path_canonical; ++path, ++path_canonical) {
|
|
|
|
}
|
|
|
|
if (*path_canonical != '\0') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (; *path == '/'; ++path) {
|
|
|
|
}
|
|
|
|
return *path == '\0';
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \function stat(path)
|
|
|
|
/// Get the status of a file or directory.
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC mp_obj_t os_stat(mp_obj_t path_in) {
|
|
|
|
const char *path = mp_obj_str_get_str(path_in);
|
|
|
|
|
|
|
|
FILINFO fno;
|
|
|
|
#if _USE_LFN
|
|
|
|
fno.lfname = NULL;
|
|
|
|
fno.lfsize = 0;
|
|
|
|
#endif
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2014-08-06 14:02:34 -04:00
|
|
|
FRESULT res;
|
2016-02-10 11:32:57 -05:00
|
|
|
if (path_equal(path, "/")) {
|
|
|
|
// stat root directory
|
2014-07-31 18:44:04 -04:00
|
|
|
fno.fsize = 0;
|
|
|
|
fno.fdate = 0;
|
|
|
|
fno.ftime = 0;
|
|
|
|
fno.fattrib = AM_DIR;
|
|
|
|
} else {
|
2016-02-10 11:32:57 -05:00
|
|
|
res = FR_NO_PATH;
|
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) {
|
|
|
|
fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i];
|
|
|
|
if (vfs != NULL && path_equal(path, vfs->str)) {
|
|
|
|
// stat mounted device directory
|
|
|
|
fno.fsize = 0;
|
|
|
|
fno.fdate = 0;
|
|
|
|
fno.ftime = 0;
|
|
|
|
fno.fattrib = AM_DIR;
|
|
|
|
res = FR_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (res == FR_NO_PATH) {
|
|
|
|
// stat normal file
|
|
|
|
res = f_stat(path, &fno);
|
|
|
|
}
|
2014-07-31 18:44:04 -04:00
|
|
|
if (res != FR_OK) {
|
2016-10-06 22:58:25 -04:00
|
|
|
mp_raise_OSError(fresult_to_errno_table[res]);
|
2014-07-31 18:44:04 -04:00
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL);
|
|
|
|
mp_int_t mode = 0;
|
|
|
|
if (fno.fattrib & AM_DIR) {
|
|
|
|
mode |= 0x4000; // stat.S_IFDIR
|
|
|
|
} else {
|
|
|
|
mode |= 0x8000; // stat.S_IFREG
|
|
|
|
}
|
2015-05-10 20:00:19 -04:00
|
|
|
mp_int_t seconds = timeutils_seconds_since_2000(
|
2014-07-19 11:39:13 -04:00
|
|
|
1980 + ((fno.fdate >> 9) & 0x7f),
|
|
|
|
(fno.fdate >> 5) & 0x0f,
|
|
|
|
fno.fdate & 0x1f,
|
|
|
|
(fno.ftime >> 11) & 0x1f,
|
|
|
|
(fno.ftime >> 5) & 0x3f,
|
|
|
|
2 * (fno.ftime & 0x1f)
|
|
|
|
);
|
|
|
|
t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
|
|
|
|
t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
|
|
|
|
t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
|
|
|
|
t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
|
|
|
|
t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
|
|
|
|
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
|
|
|
|
t->items[6] = MP_OBJ_NEW_SMALL_INT(fno.fsize); // st_size
|
|
|
|
t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime
|
|
|
|
t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime
|
|
|
|
t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2016-01-16 00:56:08 -05:00
|
|
|
STATIC mp_obj_t os_statvfs(mp_obj_t path_in) {
|
|
|
|
const char *path = mp_obj_str_get_str(path_in);
|
|
|
|
|
|
|
|
DWORD nclst;
|
|
|
|
FATFS *fatfs;
|
|
|
|
FRESULT res = f_getfree(path, &nclst, &fatfs);
|
|
|
|
if (res != FR_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL);
|
|
|
|
|
|
|
|
t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * 512); // f_bsize - block size
|
|
|
|
t->items[1] = t->items[0]; // f_frsize - fragment size
|
2016-10-05 00:52:36 -04:00
|
|
|
t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2) * fatfs->csize); // f_blocks - total number of blocks
|
2016-01-16 00:56:08 -05:00
|
|
|
t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree - number of free blocks
|
|
|
|
t->items[4] = t->items[3]; // f_bavail - free blocks avail to unpriviledged users
|
|
|
|
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files - # inodes
|
|
|
|
t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree - # free inodes
|
|
|
|
t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail - # free inodes avail to unpriviledges users
|
|
|
|
t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
|
|
|
|
t->items[9] = MP_OBJ_NEW_SMALL_INT(_MAX_LFN); // f_namemax
|
|
|
|
|
|
|
|
return t;
|
|
|
|
|
|
|
|
error:
|
2016-10-06 22:58:25 -04:00
|
|
|
mp_raise_OSError(fresult_to_errno_table[res]);
|
2016-01-16 00:56:08 -05:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_statvfs_obj, os_statvfs);
|
|
|
|
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \function sync()
|
|
|
|
/// Sync all filesystems.
|
2014-03-22 10:56:32 -04:00
|
|
|
STATIC mp_obj_t os_sync(void) {
|
2015-03-04 15:38:28 -05:00
|
|
|
disk_ioctl(0, CTRL_SYNC, NULL);
|
|
|
|
disk_ioctl(1, CTRL_SYNC, NULL);
|
2014-12-27 15:20:51 -05:00
|
|
|
disk_ioctl(2, CTRL_SYNC, NULL);
|
2014-03-22 10:56:32 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-03-04 15:38:28 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mod_os_sync_obj, os_sync);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2014-08-05 18:35:21 -04:00
|
|
|
#if MICROPY_HW_ENABLE_RNG
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \function urandom(n)
|
|
|
|
/// Return a bytes object with n random bytes, generated by the hardware
|
|
|
|
/// random number generator.
|
2014-03-22 10:56:32 -04:00
|
|
|
STATIC mp_obj_t os_urandom(mp_obj_t num) {
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t n = mp_obj_get_int(num);
|
2015-01-21 17:48:37 -05:00
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init_len(&vstr, n);
|
2014-03-22 10:56:32 -04:00
|
|
|
for (int i = 0; i < n; i++) {
|
2015-01-21 17:48:37 -05:00
|
|
|
vstr.buf[i] = rng_get();
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
2015-01-21 17:48:37 -05:00
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
2014-03-22 10:56:32 -04:00
|
|
|
}
|
2014-07-19 11:39:13 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
|
2014-08-05 18:35:21 -04:00
|
|
|
#endif
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2015-10-19 16:57:41 -04:00
|
|
|
// Get or set the UART object that the REPL is repeated on.
|
|
|
|
// TODO should accept any object with read/write methods.
|
|
|
|
STATIC mp_obj_t os_dupterm(mp_uint_t n_args, const mp_obj_t *args) {
|
|
|
|
if (n_args == 0) {
|
|
|
|
if (MP_STATE_PORT(pyb_stdio_uart) == NULL) {
|
|
|
|
return mp_const_none;
|
|
|
|
} else {
|
|
|
|
return MP_STATE_PORT(pyb_stdio_uart);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (args[0] == mp_const_none) {
|
|
|
|
MP_STATE_PORT(pyb_stdio_uart) = NULL;
|
|
|
|
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
|
|
|
|
MP_STATE_PORT(pyb_stdio_uart) = args[0];
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object"));
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_os_dupterm_obj, 0, 1, os_dupterm);
|
|
|
|
|
2014-03-22 10:56:32 -04:00
|
|
|
STATIC const mp_map_elem_t os_module_globals_table[] = {
|
2014-10-12 15:23:47 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) },
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2015-04-21 10:51:49 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_uname), (mp_obj_t)&os_uname_obj },
|
|
|
|
|
2014-07-31 18:44:04 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_chdir), (mp_obj_t)&os_chdir_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_getcwd), (mp_obj_t)&os_getcwd_obj },
|
2014-03-22 10:56:32 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_listdir), (mp_obj_t)&os_listdir_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), (mp_obj_t)&os_mkdir_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&os_remove_obj },
|
2015-05-10 20:30:56 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_rename),(mp_obj_t)&os_rename_obj},
|
2014-03-22 10:56:32 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&os_rmdir_obj },
|
2014-07-19 11:39:13 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj },
|
2016-01-16 00:56:08 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_statvfs), (mp_obj_t)&os_statvfs_obj },
|
2014-03-22 10:56:32 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove
|
|
|
|
|
2015-03-04 15:38:28 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&mod_os_sync_obj },
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2014-08-08 18:30:01 -04:00
|
|
|
/// \constant sep - separation character used in paths
|
2014-03-22 10:56:32 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_OBJ_NEW_QSTR(MP_QSTR__slash_) },
|
|
|
|
|
2014-08-05 18:35:21 -04:00
|
|
|
#if MICROPY_HW_ENABLE_RNG
|
2014-03-22 10:56:32 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_urandom), (mp_obj_t)&os_urandom_obj },
|
2014-08-05 18:35:21 -04:00
|
|
|
#endif
|
2015-10-19 16:57:41 -04:00
|
|
|
|
|
|
|
// these are MicroPython extensions
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_dupterm), (mp_obj_t)&mod_os_dupterm_obj },
|
2016-02-10 11:28:26 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&fsuser_mount_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_umount), (mp_obj_t)&fsuser_umount_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkfs), (mp_obj_t)&fsuser_mkfs_obj },
|
2014-03-22 10:56:32 -04:00
|
|
|
};
|
|
|
|
|
2014-11-29 09:39:27 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);
|
2014-03-22 10:56:32 -04:00
|
|
|
|
2014-10-12 15:23:47 -04:00
|
|
|
const mp_obj_module_t mp_module_uos = {
|
2014-03-22 10:56:32 -04:00
|
|
|
.base = { &mp_type_module },
|
2014-04-05 16:53:54 -04:00
|
|
|
.globals = (mp_obj_dict_t*)&os_module_globals,
|
2014-03-22 10:56:32 -04:00
|
|
|
};
|