moduos.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/mpstate.h"
  29. #include "py/runtime.h"
  30. #include "py/objtuple.h"
  31. #include "py/objstr.h"
  32. #include "lib/oofatfs/ff.h"
  33. #include "lib/oofatfs/diskio.h"
  34. #include "modules/uos/microbitfs.h"
  35. #include "extmod/vfs.h"
  36. #include "extmod/vfs_fat.h"
  37. #include "genhdr/mpversion.h"
  38. //#include "timeutils.h"
  39. #include "uart.h"
  40. //#include "portmodules.h"
  41. #if MICROPY_HW_ENABLE_RNG
  42. #include "modrandom.h"
  43. #endif // MICROPY_HW_ENABLE_RNG
  44. /// \module os - basic "operating system" services
  45. ///
  46. /// The `os` module contains functions for filesystem access and `urandom`.
  47. ///
  48. /// The filesystem has `/` as the root directory, and the available physical
  49. /// drives are accessible from here. They are currently:
  50. ///
  51. /// /flash -- the internal flash filesystem
  52. /// /sd -- the SD card (if it exists)
  53. ///
  54. /// On boot up, the current directory is `/flash` if no SD card is inserted,
  55. /// otherwise it is `/sd`.
  56. STATIC const qstr os_uname_info_fields[] = {
  57. MP_QSTR_sysname, MP_QSTR_nodename,
  58. MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
  59. };
  60. STATIC const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, "pyboard");
  61. STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, "pyboard");
  62. STATIC const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, MICROPY_VERSION_STRING);
  63. STATIC const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE);
  64. STATIC const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME);
  65. STATIC MP_DEFINE_ATTRTUPLE(
  66. os_uname_info_obj,
  67. os_uname_info_fields,
  68. 5,
  69. (mp_obj_t)&os_uname_info_sysname_obj,
  70. (mp_obj_t)&os_uname_info_nodename_obj,
  71. (mp_obj_t)&os_uname_info_release_obj,
  72. (mp_obj_t)&os_uname_info_version_obj,
  73. (mp_obj_t)&os_uname_info_machine_obj
  74. );
  75. STATIC mp_obj_t os_uname(void) {
  76. return (mp_obj_t)&os_uname_info_obj;
  77. }
  78. STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname);
  79. #if MICROPY_VFS
  80. /// \function sync()
  81. /// Sync all filesystems.
  82. STATIC mp_obj_t os_sync(void) {
  83. for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
  84. // this assumes that vfs->obj is fs_user_mount_t with block device functions
  85. disk_ioctl(MP_OBJ_TO_PTR(vfs->obj), CTRL_SYNC, NULL);
  86. }
  87. return mp_const_none;
  88. }
  89. MP_DEFINE_CONST_FUN_OBJ_0(mod_os_sync_obj, os_sync);
  90. #endif
  91. #if MICROPY_HW_ENABLE_RNG
  92. /// \function urandom(n)
  93. /// Return a bytes object with n random bytes, generated by the hardware
  94. /// random number generator.
  95. STATIC mp_obj_t os_urandom(mp_obj_t num) {
  96. mp_int_t n = mp_obj_get_int(num);
  97. vstr_t vstr;
  98. vstr_init_len(&vstr, n);
  99. for (int i = 0; i < n; i++) {
  100. vstr.buf[i] = (uint8_t)(machine_rng_generate_random_word() & 0xFF);
  101. }
  102. return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
  103. }
  104. STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
  105. #endif
  106. #if MICROPY_PY_MACHINE_UART
  107. // Get or set the UART object that the REPL is repeated on.
  108. // TODO should accept any object with read/write methods.
  109. STATIC mp_obj_t os_dupterm(mp_uint_t n_args, const mp_obj_t *args) {
  110. if (n_args == 0) {
  111. if (MP_STATE_PORT(board_stdio_uart) == NULL) {
  112. return mp_const_none;
  113. } else {
  114. return MP_STATE_PORT(board_stdio_uart);
  115. }
  116. } else {
  117. if (args[0] == mp_const_none) {
  118. MP_STATE_PORT(board_stdio_uart) = NULL;
  119. } else if (mp_obj_get_type(args[0]) == &machine_hard_uart_type) {
  120. MP_STATE_PORT(board_stdio_uart) = args[0];
  121. } else {
  122. mp_raise_ValueError("need a UART object");
  123. }
  124. return mp_const_none;
  125. }
  126. }
  127. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_os_dupterm_obj, 0, 1, os_dupterm);
  128. #endif // MICROPY_PY_MACHINE_UART
  129. STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
  130. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) },
  131. { MP_ROM_QSTR(MP_QSTR_uname), MP_ROM_PTR(&os_uname_obj) },
  132. #if MICROPY_VFS
  133. { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&mp_vfs_chdir_obj) },
  134. { MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&mp_vfs_getcwd_obj) },
  135. { MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&mp_vfs_listdir_obj) },
  136. { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mp_vfs_mkdir_obj) },
  137. { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&mp_vfs_remove_obj) },
  138. { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&mp_vfs_rename_obj) },
  139. { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&mp_vfs_rmdir_obj) },
  140. { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&mp_vfs_stat_obj) },
  141. { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&mp_vfs_statvfs_obj) },
  142. { MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&mp_vfs_remove_obj) }, // unlink aliases to remove
  143. { MP_ROM_QSTR(MP_QSTR_sync), MP_ROM_PTR(&mod_os_sync_obj) },
  144. #elif MICROPY_MBFS
  145. { MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&uos_mbfs_listdir_obj) },
  146. { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&uos_mbfs_ilistdir_obj) }, // uses ~136 bytes
  147. { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&uos_mbfs_stat_obj) }, // uses ~228 bytes
  148. { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&uos_mbfs_remove_obj) },
  149. #endif
  150. /// \constant sep - separation character used in paths
  151. { MP_ROM_QSTR(MP_QSTR_sep), MP_ROM_QSTR(MP_QSTR__slash_) },
  152. #if MICROPY_HW_ENABLE_RNG
  153. { MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&os_urandom_obj) },
  154. #endif
  155. // these are MicroPython extensions
  156. #if MICROPY_PY_MACHINE_UART
  157. { MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mod_os_dupterm_obj) },
  158. #endif
  159. #if MICROPY_VFS
  160. { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) },
  161. { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_vfs_umount_obj) },
  162. { MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
  163. #endif
  164. };
  165. STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);
  166. const mp_obj_module_t mp_module_uos = {
  167. .base = { &mp_type_module },
  168. .globals = (mp_obj_dict_t*)&os_module_globals,
  169. };