8000 RP2: Add networking support. · micropython/micropython@7fd3f11 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7fd3f11

Browse files
committed
RP2: Add networking support.
1 parent b51e7e9 commit 7fd3f11

File tree

6 files changed

+748
-0
lines changed

6 files changed

+748
-0
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,27 @@ set(PICO_SDK_COMPONENTS
161161
tinyusb_device
162162
)
163163

164+
if (MICROPY_PY_NETWORK)
165+
list(APPEND MICROPY_SOURCE_PORT
166+
modusocket.c
167+
modnetwork.c
168+
)
169+
170+
list(APPEND MICROPY_SOURCE_QSTR
171+
${PROJECT_SOURCE_DIR}/modusocket.c
172+
${PROJECT_SOURCE_DIR}/modnetwork.c
173+
)
174+
175+
list(APPEND MICROPY_SOURCE_LIB
176+
${MICROPY_DIR}/shared/netutils/netutils.c
177+
)
178+
179+
target_compile_definitions(${MICROPY_TARGET} PRIVATE
180+
MICROPY_PY_NETWORK=1
181+
MICROPY_PY_USOCKET=1
182+
)
183+
endif()
184+
164185
# Define mpy-cross flags and frozen manifest
165186
set(MICROPY_CROSS_FLAGS -march=armv7m)
166187
if (NOT MICROPY_FROZEN_MANIFEST)

ports/rp2/main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
#include "modrp2.h"
4242
#include "genhdr/mpversion.h"
4343

44+
#if MICROPY_PY_NETWORK
45+
#include "modnetwork.h"
46+
#endif
47+
4448
#include "pico/stdlib.h"
4549
#include "pico/binary_info.h"
4650
#include "hardware/rtc.h"
@@ -107,6 +111,10 @@ int main(int argc, char **argv) {
107111
machine_pin_init();
108112
rp2_pio_init();
109113

114+
#if MICROPY_PY_NETWORK
115+
mod_network_init();
116+
#endif
117+
110118
// Execute _boot.py to set up the filesystem.
111119
pyexec_frozen_module("_boot.py");
112120

@@ -136,6 +144,9 @@ int main(int argc, char **argv) {
136144

137145
soft_reset_exit:
138146
mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n");
147+
#if MICROPY_PY_NETWORK
148+
mod_network_deinit();
149+
#endif
139150
rp2_pio_deinit();
140151
machine_pin_deinit();
141152
#if MICROPY_PY_THREAD

ports/rp2/modnetwork.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 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+
27+
#include <stdio.h>
28+
#include <stdint.h>
29+
#include <string.h>
30+
31+
#include "py/objlist.h"
32+
#include "py/runtime.h"
33+
#include "py/mphal.h"
34+
#include "shared/netutils/netutils.h"
35+
#include "modnetwork.h"
36+
37+
#if MICROPY_PY_NETWORK
38+
/// \module network - network configuration
39+
///
40+
/// This module provides network drivers and routing configuration.
41+
42+
void mod_network_init(void) {
43+
mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
44+
}
45+
46+
void mod_network_deinit(void) {
47+
}
48+
49+
void mod_network_register_nic(mp_obj_t nic) {
50+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
51+
if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {
52+
// nic already registered
53+
return;
54+
}
55+
}
56+
// nic not registered so add to list
57+
mp_obj_list_append(MP_OBJ_FROM_PTR(&MP_STATE_PORT(mod_network_nic_list)), nic);
58+
}
59+
60+
mp_obj_t mod_network_find_nic(const uint8_t *ip) {
61+
// find a NIC that is suited to given IP address
62+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
63+
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
64+
// TODO check IP suitability here
65+
// mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
66+
return nic;
67+
}
68+
69+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("no available NIC"));
70+
}
71+
72+
STATIC mp_obj_t network_route(void) {
73+
return MP_OBJ_FROM_PTR(&MP_STATE_PORT(mod_network_nic_list));
74+
}
75+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_route_obj, network_route);
76+
77+
#if MICROPY_PY_NETWORK_NINAW10
78+
extern const mod_network_nic_type_t mod_network_nic_type_nina;
79+
#endif
80+
81+
STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
82+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },
83+
{ MP_ROM_QSTR(MP_QSTR_route), MP_ROM_PTR(&network_route_obj) },
84+
#if MICROPY_PY_NETWORK_NINAW10
85+
{ MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&mod_network_nic_type_nina) },
86+
#endif
87+
// Station/AP mode.
88+
{ MP_ROM_QSTR(MP_QSTR_STA_IF), MP_ROM_INT(MOD_NETWORK_STA_IF) },
89+
{ MP_ROM_QSTR(MP_QSTR_AP_IF), MP_ROM_INT(MOD_NETWORK_AP_IF) },
90+
};
91+
92+
STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
93+
94+
const mp_obj_module_t mp_module_network = {
95+
.base = { &mp_type_module },
96+
.globals = (mp_obj_dict_t *)&mp_module_network_globals,
97+
};
98+
99+
#endif // MICROPY_PY_NETWORK

ports/rp2/modnetwork.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
#ifndef MICROPY_INCLUDED_RP2_MODNETWORK_H
27+
#define MICROPY_INCLUDED_RP2_MODNETWORK_H
28+
29+
#define MOD_NETWORK_IPADDR_BUF_SIZE (4)
30+
31+
#define MOD_NETWORK_AF_INET (2)
32+
#define MOD_NETWORK_AF_INET6 (10)
33+
34+
#define MOD_NETWORK_SOCK_STREAM (1)
35+
#define MOD_NETWORK_SOCK_DGRAM (2)
36+
#define MOD_NETWORK_SOCK_RAW (3)
37+
38+
#define MOD_NETWORK_STA_IF (0)
39+
#define MOD_NETWORK_AP_IF (1)
40+
41+
struct _mod_network_socket_obj_t;
42+
43+
typedef struct _mod_network_nic_type_t {
44+
mp_obj_type_t base;
45+
46+
// API for non-socket operations
47+
int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out);
48+
49+
// API for socket operations; return -1 on error
50+
int (*socket)(struct _mod_network_socket_obj_t *socket, int *_errno);
51+
void (*close)(struct _mod_network_socket_obj_t *socket);
52+
int (*bind)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
53+
int (*listen)(struct _mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno);
54+
int (*accept)(struct _mod_network_socket_obj_t *socket, struct _mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno);
55+
int (*connect)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
56+
mp_uint_t (*send)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno);
57+
mp_uint_t (*recv)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno);
58+
mp_uint_t (*sendto)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno);
59+
mp_uint_t (*recvfrom)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno);
60+
int (*setsockopt)(struct _mod_network_socket_obj_t *socket, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno);
61+
int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
62+
int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
63+
} mod_network_nic_type_t;
64+
65+
typedef struct _mod_network_socket_obj_t {
66+
mp_obj_base_t base;
67+
mp_obj_t nic;
68+
mod_network_nic_type_t *nic_type;
69+
int16_t fd;
70+
uint16_t timeout;
71+
uint8_t domain;
72+
uint8_t type;
73+
bool bound;
74+
} mod_network_socket_obj_t;
75+
76+
void mod_network_init( 179B void);
77+
void mod_network_deinit(void);
78+
void mod_network_register_nic(mp_obj_t nic);
79+
mp_obj_t mod_network_find_nic(const uint8_t *ip);
80+
81+
#endif // MICROPY_INCLUDED_RP2_MODNETWORK_H

0 commit comments

Comments
 (0)
0