From 59610c400480891c3c6cd29814fdf67a80ac3b69 Mon Sep 17 00:00:00 2001 From: Josef Gajdusek Date: Mon, 18 May 2015 18:35:25 +0200 Subject: [PATCH] esp8266: Add uos module Currently implements only .uname() --- esp8266/Makefile | 1 + esp8266/moduos.c | 79 ++++++++++++++++++++++++++++++++++++++++++ esp8266/mpconfigport.h | 4 +++ esp8266/qstrdefsport.h | 10 ++++++ 4 files changed, 94 insertions(+) create mode 100644 esp8266/moduos.c diff --git a/esp8266/Makefile b/esp8266/Makefile index 8d6cd167e6..d7f840b82b 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -59,6 +59,7 @@ SRC_C = \ modpybrtc.c \ modesp.c \ modutime.c \ + moduos.c \ utils.c \ $(BUILD)/frozen.c \ diff --git a/esp8266/moduos.c b/esp8266/moduos.c new file mode 100644 index 0000000000..56827d12f4 --- /dev/null +++ b/esp8266/moduos.c @@ -0,0 +1,79 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2015 Josef Gajdusek + * + * 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 + +#include "py/mpconfig.h" +#include "py/nlr.h" +#include "py/obj.h" +#include "py/objtuple.h" +#include "py/objstr.h" +#include "genhdr/mpversion.h" +#include "user_interface.h" + +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, MICROPY_PY_SYS_PLATFORM); +STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, MICROPY_PY_SYS_PLATFORM); +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_obj_tuple_t os_uname_info_obj = { + .base = {&mp_type_attrtuple}, + .len = 5, + .items = { + (mp_obj_t)&os_uname_info_sysname_obj, + (mp_obj_t)&os_uname_info_nodename_obj, + NULL, + (mp_obj_t)&os_uname_info_version_obj, + (mp_obj_t)&os_uname_info_machine_obj, + (void *)os_uname_info_fields, + } +}; + +STATIC mp_obj_t os_uname(void) { + if (os_uname_info_obj.items[2] == NULL) { + const char *ver = system_get_sdk_version(); + os_uname_info_obj.items[2] = mp_obj_new_str(ver, strlen(ver), false); + } + return (mp_obj_t)&os_uname_info_obj; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname); + +STATIC const mp_map_elem_t os_module_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_uname), (mp_obj_t)&os_uname_obj }, +}; + +STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table); + +const mp_obj_module_t uos_module = { + .base = { &mp_type_module }, + .name = MP_QSTR_uos, + .globals = (mp_obj_dict_t*)&os_module_globals, +}; diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index d76710b7ad..9460eae609 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -66,14 +66,17 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj; extern const struct _mp_obj_module_t pyb_module; extern const struct _mp_obj_module_t esp_module; extern const struct _mp_obj_module_t utime_module; +extern const struct _mp_obj_module_t uos_module; #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_esp), (mp_obj_t)&esp_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&utime_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \ #define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&utime_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&uos_module }, \ #define MP_STATE_PORT MP_STATE_VM @@ -92,3 +95,4 @@ extern const struct _mp_obj_module_t utime_module; #define MICROPY_HAL_H "esp_mphal.h" #define MICROPY_HW_BOARD_NAME "ESP module" #define MICROPY_HW_MCU_NAME "ESP8266" +#define MICROPY_PY_SYS_PLATFORM "ESP8266" diff --git a/esp8266/qstrdefsport.h b/esp8266/qstrdefsport.h index 439afdea3b..68508fee03 100644 --- a/esp8266/qstrdefsport.h +++ b/esp8266/qstrdefsport.h @@ -41,6 +41,16 @@ Q(udelay) Q(sync) Q(hard_reset) +// uos module +Q(uos) +Q(os) +Q(uname) +Q(sysname) +Q(nodename) +Q(release) +Q(version) +Q(machine) + Q(esp) Q(socket) Q(connect)