Skip to content

Commit b400e8a

Browse files
authored
Merge pull request #1 from RT-Thread-packages/master
update
2 parents 7139590 + 80c2ee8 commit b400e8a

File tree

180 files changed

+5940
-3641
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+5940
-3641
lines changed

README.md

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,73 @@
11
# MicroPython
22

3-
## 1、介绍
3+
[中文页](README_ZH.md) | English
44

5-
这是一个在 RT-Thread 上的 `MicroPython` 移植,可以运行在 **RT-Thread 3.0** 版本以上。通过该软件包可以在搭载了 RT-Thread 的嵌入式系统上运行 `MicroPython`
5+
## 1. Introduction
66

7-
如果是第一次接触 RT-Thread MicroPython,推荐你先通过 RT-Thread 官方支持的开发板来快速上手,这些开发板的固件功能完善并提供源代码,适合入门学习,官方支持开发板 [固件下载请点我](https://www.rt-thread.org/qa/forum.php?mod=viewthread&tid=12305&extra=page%3D1%26filter%3Dtypeid%26typeid%3D20)
7+
This is a port of `MicroPython` on RT-Thread, which can run on **RT-Thread 3.0** or higher. This software package can run `MicroPython` on embedded systems equipped with RT-Thread.
88

9-
### 1.1 目录结构
9+
If it is the first time to come into contact with RT-Thread MicroPython, it is recommended that you use RT-Thread officially supported development boards to get started quickly. These development boards have complete firmware functions and provide source code, suitable for introductory learning, and officially support development boards [firmware download Please click on me](https://www.rt-thread.org/qa/forum.php?mod=viewthread&tid=12305&extra=page%3D1%26filter%3Dtypeid%26typeid%3D20).
1010

11-
| 名称 | 说明 |
11+
### 1.1 Directory structure
12+
13+
| Name | Description |
1214
| ---- | ---- |
13-
| docs | 文档目录,包括入门指南和开发手册 |
14-
| drivers | MicroPython 源代码目录 |
15-
| extmod | MicroPython 源代码目录 |
16-
| lib | MicroPython 源代码目录 |
17-
| py | MicroPython 源代码目录 |
18-
| port | 移植代码目录 |
19-
| LICENSE | Micropython MIT 许可证 |
15+
| docs | Document directory, including getting started guide and development manual |
16+
| drivers | MicroPython source code directory |
17+
| extmod | MicroPython Source Code Directory |
18+
| lib | MicroPython source code directory |
19+
| py | MicroPython source code directory |
20+
| port | Porting code directory |
21+
| LICENSE | Micropython MIT License |
2022

21-
### 1.2 许可证
23+
### 1.2 License
2224

23-
RT-Thread MicroPython 遵循 MIT 许可,详见 `LICENSE` 文件。
25+
RT-Thread MicroPython follows the MIT license, see the `LICENSE` file for details.
2426

25-
### 1.3 依赖
27+
### 1.3 Dependency
2628

2729
- RT-Thread 3.0+
2830

29-
## 2、如何打开 RT-Thread MicroPython
31+
## 2. How to open RT-Thread MicroPython
3032

31-
使用 `MicroPython package` 需要在 RT-Thread 的包管理器中选择它,具体路径如下:
33+
To use `MicroPython package`, you need to select it in the RT-Thread package manager. The specific path is as follows:
3234

3335
![elect_micropytho](./docs/assets/select_micropython.png)
3436

35-
然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。
37+
Then let the RT-Thread package manager automatically update, or use the `pkgs --update` command to update the package to the BSP.
3638

37-
## 3、使用 RT-Thread MicroPython
39+
## 3. Use RT-Thread MicroPython
3840

39-
### 3.1 添加软件包到工程
41+
### 3.1 Add software package to project
4042

41-
选中 `MicroPython package` 后,再次进行 `bsp` 编译时,它会被加入到 `bsp` 工程中进行编译。
43+
After selecting `MicroPython package`, when compiling with `bsp` again, it will be added to the `bsp` project for compilation.
4244

43-
* 固件开发可参考 [《MicroPython 固件开发指南](./docs/firmware-develop.md)
45+
* For firmware development, please refer to [《MicroPython Firmware Development Guide](./docs/firmware-develop.md)
4446

45-
* 查阅更多 MicroPython 说明文档请访问 [RT-Thread 文档中心](https://www.rt-thread.org/document/site/submodules/micropython/docs/introduction/)
47+
* For more MicroPython documentation, please visit [RT-Thread Documentation Center](https://www.rt-thread.org/document/site/submodules/micropython/docs/introduction/)
4648

4749

48-
### 3.2 使用 MicroPython IDE
50+
### 3.2 Using MicroPython IDE
4951

50-
[RT-Thread MicroPython IDE](https://marketplace.visualstudio.com/items?itemName=RT-Thread.rt-thread-micropython) MicroPython 提供了强大的开发环境,可以通过 VScode 应用商店直接查询下载,示例如下所示:
52+
[RT-Thread MicroPython IDE](https://marketplace.visualstudio.com/items?itemName=RT-Thread.rt-thread-micropython) provides a powerful development environment for MicroPython, which can be directly searched and downloaded through the VScode application store. Examples are as follows:
5153

5254
![08_direct_run_files](docs/assets/08_direct_run_files.gif)
5355

5456

55-
### 3.3 向 MicroPython 添加 C 扩展
57+
### 3.3 Add C extension to MicroPython
5658

57-
为了方便用户添加自己编写的 C 函数到 MicroPython 中被 Python 脚本调用,RT-Thread 提供了 [MicroPython C 绑定代码自动生成器](https://summerlife.github.io/RT-MicroPython-Generator/) 供大家使用。通过该工具,用户只需要简单几步,即可实现 C 函数扩展,下图展示了自动生成的 C 代码的形式。
59+
In order to facilitate users to add their own C functions to MicroPython to be called by Python scripts, RT-Thread provides [MicroPython C binding code automatic generator](https://summerlife.github.io/RT-MicroPython-Generator/) For everyone to use. With this tool, users only need a few simple steps to achieve C function extension. The following figure shows the form of the automatically generated C code.
5860

5961
![08_direct_run_files](docs/assets/c-gen.png)
6062

61-
## 4、注意事项
63+
## 4. Matters needing attention
6264

63-
- 需要使用 **RT-Thread 3.0** 以上版本
64-
- `menuconfig` 选项中选择 `Micropython` `latest` 版本
65-
- 目前 `System Module` 下的 `ffi` 模块只支持 GCC 工具链,且需要在链接脚本中添加相关段信息
65+
- Need to use **RT-Thread 3.0** or above
66+
- Select the `latest` version of `Micropython` in the `menuconfig` option
67+
- Currently, the `ffi` module under `System Module` only supports GCC toolchain, and relevant information needs to be added to the link script
6668

67-
## 5、开发资源
69+
## 5. Development resources
6870

69-
* [RT-Thread MicroPython 论坛](https://www.rt-thread.org/qa/forum.php)
70-
* [RT-Thread MicroPython 文档中心](https://www.rt-thread.org/document/site/submodules/micropython/docs/introduction/)
71-
* [点击加入 RT-Thread MicroPython 交流群](https://jq.qq.com/?_wv=1027&k=5EhyEjx)
71+
* [RT-Thread MicroPython Forum](https://www.rt-thread.org/qa/forum.php)
72+
* [RT-Thread MicroPython Documentation Center](https://www.rt-thread.org/document/site/submodules/micropython/docs/introduction/)
73+
* [Click to join the RT-Thread MicroPython exchange group](https://jq.qq.com/?_wv=1027&k=5EhyEjx)

README_ZH.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# MicroPython
2+
3+
中文页 | [English](README.md)
4+
5+
## 1、介绍
6+
7+
这是一个在 RT-Thread 上的 `MicroPython` 移植,可以运行在 **RT-Thread 3.0** 版本以上。通过该软件包可以在搭载了 RT-Thread 的嵌入式系统上运行 `MicroPython`
8+
9+
如果是第一次接触 RT-Thread MicroPython,推荐你先通过 RT-Thread 官方支持的开发板来快速上手,这些开发板的固件功能完善并提供源代码,适合入门学习,官方支持开发板 [固件下载请点我](https://www.rt-thread.org/qa/forum.php?mod=viewthread&tid=12305&extra=page%3D1%26filter%3Dtypeid%26typeid%3D20)
10+
11+
### 1.1 目录结构
12+
13+
| 名称 | 说明 |
14+
| ---- | ---- |
15+
| docs | 文档目录,包括入门指南和开发手册 |
16+
| drivers | MicroPython 源代码目录 |
17+
| extmod | MicroPython 源代码目录 |
18+
| lib | MicroPython 源代码目录 |
19+
| py | MicroPython 源代码目录 |
20+
| port | 移植代码目录 |
21+
| LICENSE | Micropython MIT 许可证 |
22+
23+
### 1.2 许可证
24+
25+
RT-Thread MicroPython 遵循 MIT 许可,详见 `LICENSE` 文件。
26+
27+
### 1.3 依赖
28+
29+
- RT-Thread 3.0+
30+
31+
## 2、如何打开 RT-Thread MicroPython
32+
33+
使用 `MicroPython package` 需要在 RT-Thread 的包管理器中选择它,具体路径如下:
34+
35+
![elect_micropytho](./docs/assets/select_micropython.png)
36+
37+
然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。
38+
39+
## 3、使用 RT-Thread MicroPython
40+
41+
### 3.1 添加软件包到工程
42+
43+
选中 `MicroPython package` 后,再次进行 `bsp` 编译时,它会被加入到 `bsp` 工程中进行编译。
44+
45+
* 固件开发可参考 [《MicroPython 固件开发指南》](./docs/firmware-develop.md)
46+
47+
* 查阅更多 MicroPython 说明文档请访问 [RT-Thread 文档中心](https://www.rt-thread.org/document/site/submodules/micropython/docs/introduction/)
48+
49+
50+
### 3.2 使用 MicroPython IDE
51+
52+
[RT-Thread MicroPython IDE](https://marketplace.visualstudio.com/items?itemName=RT-Thread.rt-thread-micropython) 为 MicroPython 提供了强大的开发环境,可以通过 VScode 应用商店直接查询下载,示例如下所示:
53+
54+
![08_direct_run_files](docs/assets/08_direct_run_files.gif)
55+
56+
57+
### 3.3 向 MicroPython 添加 C 扩展
58+
59+
为了方便用户添加自己编写的 C 函数到 MicroPython 中被 Python 脚本调用,RT-Thread 提供了 [MicroPython C 绑定代码自动生成器](https://summerlife.github.io/RT-MicroPython-Generator/) 供大家使用。通过该工具,用户只需要简单几步,即可实现 C 函数扩展,下图展示了自动生成的 C 代码的形式。
60+
61+
![08_direct_run_files](docs/assets/c-gen.png)
62+
63+
## 4、注意事项
64+
65+
- 需要使用 **RT-Thread 3.0** 以上版本
66+
-`menuconfig` 选项中选择 `Micropython``latest` 版本
67+
- 目前 `System Module` 下的 `ffi` 模块只支持 GCC 工具链,且需要在链接脚本中添加相关段信息
68+
69+
## 5、开发资源
70+
71+
* [RT-Thread MicroPython 论坛](https://www.rt-thread.org/qa/forum.php)
72+
* [RT-Thread MicroPython 文档中心](https://www.rt-thread.org/document/site/submodules/micropython/docs/introduction/)
73+
* [点击加入 RT-Thread MicroPython 交流群](https://jq.qq.com/?_wv=1027&k=5EhyEjx)

docs/std-librarys/ussl.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This module provides access to Transport Layer Security (previously and widely k
66

77
## 功能函数
88

9-
- `ussl.wrap_socket`(sock, server_side=False, keyfile=None, certfile=None, cert_reqs=CERT_NONE, ca_certs=None)
9+
- `ussl.wrap_socket`(sock, server_side=False, key=None, cert=None)
1010

1111
Takes a [`stream`](http://docs.micropython.org/en/latest/reference/glossary.html#term-stream) *sock* (usually usocket.socket instance of `SOCK_STREAM` type), and returns an instance of ssl.SSLSocket, which wraps the underlying stream in an SSL context. Returned object has the usual [`stream`](http://docs.micropython.org/en/latest/reference/glossary.html#term-stream) interface methods like `read()`, `write()`, etc. In MicroPython, the returned object does not expose socket interface and methods like `recv()`, `send()`. In particular, a server-side SSL socket should be created from a normal socket returned from[`accept()`](http://docs.micropython.org/en/latest/library/usocket.html#usocket.socket.accept) on a non-SSL listening server socket. Depending on the underlying module implementation in a particular [`MicroPython port`](http://docs.micropython.org/en/latest/reference/glossary.html#term-micropython-port), some or all keyword arguments above may be not supported.
1212

extmod/modussl_axtls.c

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "py/runtime.h"
3131
#include "py/stream.h"
32+
#include "py/objstr.h"
3233

3334
#if MICROPY_PY_USSL && MICROPY_SSL_AXTLS
3435

@@ -54,12 +55,74 @@ struct ssl_args {
5455

5556
STATIC const mp_obj_type_t ussl_socket_type;
5657

58+
// Table of error strings corresponding to SSL_xxx error codes.
59+
STATIC const char *const ssl_error_tab1[] = {
60+
"NOT_OK",
61+
"DEAD",
62+
"CLOSE_NOTIFY",
63+
"EAGAIN",
64+
};
65+
STATIC const char *const ssl_error_tab2[] = {
66+
"CONN_LOST",
67+
"RECORD_OVERFLOW",
68+
"SOCK_SETUP_FAILURE",
69+
NULL,
70+
"INVALID_HANDSHAKE",
71+
"INVALID_PROT_MSG",
72+
"INVALID_HMAC",
73+
"INVALID_VERSION",
74+
"UNSUPPORTED_EXTENSION",
75+
"INVALID_SESSION",
76+
"NO_CIPHER",
77+
"INVALID_CERT_HASH_ALG",
78+
"BAD_CERTIFICATE",
79+
"INVALID_KEY",
80+
NULL,
81+
"FINISHED_INVALID",
82+
"NO_CERT_DEFINED",
83+
"NO_CLIENT_RENOG",
84+
"NOT_SUPPORTED",
85+
};
86+
87+
STATIC NORETURN void ussl_raise_error(int err) {
88+
MP_STATIC_ASSERT(SSL_NOT_OK - 3 == SSL_EAGAIN);
89+
MP_STATIC_ASSERT(SSL_ERROR_CONN_LOST - 18 == SSL_ERROR_NOT_SUPPORTED);
90+
91+
// Check if err corresponds to something in one of the error string tables.
92+
const char *errstr = NULL;
93+
if (SSL_NOT_OK >= err && err >= SSL_EAGAIN) {
94+
errstr = ssl_error_tab1[SSL_NOT_OK - err];
95+
} else if (SSL_ERROR_CONN_LOST >= err && err >= SSL_ERROR_NOT_SUPPORTED) {
96+
errstr = ssl_error_tab2[SSL_ERROR_CONN_LOST - err];
97+
}
98+
99+
// Unknown error, just raise the error code.
100+
if (errstr == NULL) {
101+
mp_raise_OSError(err);
102+
}
103+
104+
// Construct string object.
105+
mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t);
106+
if (o_str == NULL) {
107+
mp_raise_OSError(err);
108+
}
109+
o_str->base.type = &mp_type_str;
110+
o_str->data = (const byte *)errstr;
111+
o_str->len = strlen((char *)o_str->data);
112+
o_str->hash = qstr_compute_hash(o_str->data, o_str->len);
113+
114+
// Raise OSError(err, str).
115+
mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(err), MP_OBJ_FROM_PTR(o_str)};
116+
nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args));
117+
}
118+
119+
57120
STATIC mp_obj_ssl_socket_t *ussl_socket_new(mp_obj_t sock, struct ssl_args *args) {
58-
#if MICROPY_PY_USSL_FINALISER
121+
#if MICROPY_PY_USSL_FINALISER
59122
mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t);
60-
#else
123+
#else
61124
mp_obj_ssl_socket_t *o = m_new_obj(mp_obj_ssl_socket_t);
62-
#endif
125+
#endif
63126
o->base.type = &ussl_socket_type;
64127
o->buf = NULL;
65128
o->bytes_left = 0;
@@ -79,16 +142,16 @@ STATIC mp_obj_ssl_socket_t *ussl_socket_new(mp_obj_t sock, struct ssl_args *args
79142

80143
if (args->key.u_obj != mp_const_none) {
81144
size_t len;
82-
const byte *data = (const byte*)mp_obj_str_get_data(args->key.u_obj, &len);
145+
const byte *data = (const byte *)mp_obj_str_get_data(args->key.u_obj, &len);
83146
int res = ssl_obj_memory_load(o->ssl_ctx, SSL_OBJ_RSA_KEY, data, len, NULL);
84147
if (res != SSL_OK) {
85-
mp_raise_ValueError("invalid key");
148+
mp_raise_ValueError(MP_ERROR_TEXT("invalid key"));
86149
}
87150

88-
data = (const byte*)mp_obj_str_get_data(args->cert.u_obj, &len);
151+
data = (const byte *)mp_obj_str_get_data(args->cert.u_obj, &len);
89152
res = ssl_obj_memory_load(o->ssl_ctx, SSL_OBJ_X509_CERT, data, len, NULL);
90153
if (res != SSL_OK) {
91-
mp_raise_ValueError("invalid cert");
154+
mp_raise_ValueError(MP_ERROR_TEXT("invalid cert"));
92155
}
93156
}
94157

@@ -98,7 +161,7 @@ STATIC mp_obj_ssl_socket_t *ussl_socket_new(mp_obj_t sock, struct ssl_args *args
98161
SSL_EXTENSIONS *ext = ssl_ext_new();
99162

100163
if (args->server_hostname.u_obj != mp_const_none) {
101-
ext->host_name = (char*)mp_obj_str_get_str(args->server_hostname.u_obj);
164+
ext->host_name = (char *)mp_obj_str_get_str(args->server_hostname.u_obj);
102165
}
103166

104167
o->ssl_sock = ssl_client_new(o->ssl_ctx, (long)sock, NULL, 0, ext);
@@ -107,9 +170,7 @@ STATIC mp_obj_ssl_socket_t *ussl_socket_new(mp_obj_t sock, struct ssl_args *args
107170
int res = ssl_handshake_status(o->ssl_sock);
108171

109172
if (res != SSL_OK) {
110-
printf("ssl_handshake_status: %d\n", res);
111-
ssl_display_error(res);
112-
mp_raise_OSError(MP_EIO);
173+
ussl_raise_error(res);
113174
}
114175
}
115176

@@ -155,7 +216,7 @@ STATIC mp_uint_t ussl_socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int
155216
return 0;
156217
}
157218
if (r == SSL_EAGAIN) {
158-
eagain:
219+
eagain:
159220
r = MP_EAGAIN;
160221
}
161222
*errcode = r;
@@ -219,9 +280,9 @@ STATIC const mp_rom_map_elem_t ussl_socket_locals_dict_table[] = {
219280
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
220281
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&ussl_socket_setblocking_obj) },
221282
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
222-
#if MICROPY_PY_USSL_FINALISER
283+
#if MICROPY_PY_USSL_FINALISER
223284
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
224-
#endif
285+
#endif
225286
};
226287

227288
STATIC MP_DEFINE_CONST_DICT(ussl_socket_locals_dict, ussl_socket_locals_dict_table);
@@ -240,16 +301,16 @@ STATIC const mp_obj_type_t ussl_socket_type = {
240301
.getiter = NULL,
241302
.iternext = NULL,
242303
.protocol = &ussl_socket_stream_p,
243-
.locals_dict = (void*)&ussl_socket_locals_dict,
304+
.locals_dict = (void *)&ussl_socket_locals_dict,
244305
};
245306

246307
STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
247308
// TODO: Implement more args
248309
static const mp_arg_t allowed_args[] = {
249-
{ MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
250-
{ MP_QSTR_cert, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
310+
{ MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
311+
{ MP_QSTR_cert, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
251312
{ MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
252-
{ MP_QSTR_server_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
313+
{ MP_QSTR_server_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
253314
{ MP_QSTR_do_handshake, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
254315
};
255316

@@ -258,7 +319,7 @@ STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_
258319

259320
struct ssl_args args;
260321
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
261-
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
322+
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args);
262323

263324
return MP_OBJ_FROM_PTR(ussl_socket_new(sock, &args));
264325
}
@@ -273,7 +334,7 @@ STATIC MP_DEFINE_CONST_DICT(mp_module_ssl_globals, mp_module_ssl_globals_table);
273334

274335
const mp_obj_module_t mp_module_ussl = {
275336
.base = { &mp_type_module },
276-
.globals = (mp_obj_dict_t*)&mp_module_ssl_globals,
337+
.globals = (mp_obj_dict_t *)&mp_module_ssl_globals,
277338
};
278339

279340
#endif // MICROPY_PY_USSL

0 commit comments

Comments
 (0)