moduos.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #include "py/objtuple.h"
  30. #include "py/objstr.h"
  31. #include "lib/timeutils/timeutils.h"
  32. #include "lib/oofatfs/ff.h"
  33. #include "lib/oofatfs/diskio.h"
  34. #include "extmod/misc.h"
  35. #include "extmod/vfs.h"
  36. #include "extmod/vfs_fat.h"
  37. #include "genhdr/mpversion.h"
  38. #include "rng.h"
  39. #include "uart.h"
  40. #include "portmodules.h"
  41. /// \module os - basic "operating system" services
  42. ///
  43. /// The `os` module contains functions for filesystem access and `urandom`.
  44. ///
  45. /// The filesystem has `/` as the root directory, and the available physical
  46. /// drives are accessible from here. They are currently:
  47. ///
  48. /// /flash -- the internal flash filesystem
  49. /// /sd -- the SD card (if it exists)
  50. ///
  51. /// On boot up, the current directory is `/flash` if no SD card is inserted,
  52. /// otherwise it is `/sd`.
  53. STATIC const qstr os_uname_info_fields[] = {
  54. MP_QSTR_sysname, MP_QSTR_nodename,
  55. MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
  56. };
  57. STATIC const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, "pyboard");
  58. STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, "pyboard");
  59. STATIC const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, MICROPY_VERSION_STRING);
  60. STATIC const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE);
  61. STATIC const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME);
  62. STATIC MP_DEFINE_ATTRTUPLE(
  63. os_uname_info_obj,
  64. os_uname_info_fields,
  65. 5,
  66. MP_ROM_PTR(&os_uname_info_sysname_obj),
  67. MP_ROM_PTR(&os_uname_info_nodename_obj),
  68. MP_ROM_PTR(&os_uname_info_release_obj),
  69. MP_ROM_PTR(&os_uname_info_version_obj),
  70. MP_ROM_PTR(&os_uname_info_machine_obj)
  71. );
  72. STATIC mp_obj_t os_uname(void) {
  73. return MP_OBJ_FROM_PTR(&os_uname_info_obj);
  74. }
  75. STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname);
  76. /// \function sync()
  77. /// Sync all filesystems.
  78. STATIC mp_obj_t os_sync(void) {
  79. #if MICROPY_VFS_FAT
  80. for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
  81. // this assumes that vfs->obj is fs_user_mount_t with block device functions
  82. disk_ioctl(MP_OBJ_TO_PTR(vfs->obj), CTRL_SYNC, NULL);
  83. }
  84. #endif
  85. return mp_const_none;
  86. }
  87. MP_DEFINE_CONST_FUN_OBJ_0(mod_os_sync_obj, os_sync);
  88. #if MICROPY_HW_ENABLE_RNG
  89. /// \function urandom(n)
  90. /// Return a bytes object with n random bytes, generated by the hardware
  91. /// random number generator.
  92. STATIC mp_obj_t os_urandom(mp_obj_t num) {
  93. mp_int_t n = mp_obj_get_int(num);
  94. vstr_t vstr;
  95. vstr_init_len(&vstr, n);
  96. for (int i = 0; i < n; i++) {
  97. vstr.buf[i] = rng_get();
  98. }
  99. return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
  100. }
  101. STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
  102. #endif
  103. STATIC mp_obj_t uos_dupterm(size_t n_args, const mp_obj_t *args) {
  104. mp_obj_t prev_obj = mp_uos_dupterm_obj.fun.var(n_args, args);
  105. if (mp_obj_get_type(prev_obj) == &pyb_uart_type) {
  106. uart_attach_to_repl(MP_OBJ_TO_PTR(prev_obj), false);
  107. }
  108. if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
  109. uart_attach_to_repl(MP_OBJ_TO_PTR(args[0]), true);
  110. }
  111. return prev_obj;
  112. }
  113. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(uos_dupterm_obj, 1, 2, uos_dupterm);
  114. STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
  115. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) },
  116. { MP_ROM_QSTR(MP_QSTR_uname), MP_ROM_PTR(&os_uname_obj) },
  117. { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&mp_vfs_chdir_obj) },
  118. { MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&mp_vfs_getcwd_obj) },
  119. { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&mp_vfs_ilistdir_obj) },
  120. { MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&mp_vfs_listdir_obj) },
  121. { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mp_vfs_mkdir_obj) },
  122. { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&mp_vfs_remove_obj) },
  123. { MP_ROM_QSTR(MP_QSTR_rename),MP_ROM_PTR(&mp_vfs_rename_obj)},
  124. { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&mp_vfs_rmdir_obj) },
  125. { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&mp_vfs_stat_obj) },
  126. { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&mp_vfs_statvfs_obj) },
  127. { MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&mp_vfs_remove_obj) }, // unlink aliases to remove
  128. { MP_ROM_QSTR(MP_QSTR_sync), MP_ROM_PTR(&mod_os_sync_obj) },
  129. /// \constant sep - separation character used in paths
  130. { MP_ROM_QSTR(MP_QSTR_sep), MP_ROM_QSTR(MP_QSTR__slash_) },
  131. #if MICROPY_HW_ENABLE_RNG
  132. { MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&os_urandom_obj) },
  133. #endif
  134. // these are MicroPython extensions
  135. { MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&uos_dupterm_obj) },
  136. { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) },
  137. { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_vfs_umount_obj) },
  138. #if MICROPY_VFS_FAT
  139. { MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
  140. #endif
  141. };
  142. STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);
  143. const mp_obj_module_t mp_module_uos = {
  144. .base = { &mp_type_module },
  145. .globals = (mp_obj_dict_t*)&os_module_globals,
  146. };