8000 extmod: Move modnetwork, modusocket to extmod. by iabdalkader · Pull Request #7698 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

extmod: Move modnetwork, modusocket to extmod. #7698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 5 additions & 56 deletions ports/stm32/modnetwork.c → extmod/modnetwork.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,11 @@
#if MICROPY_PY_NETWORK

#if MICROPY_PY_LWIP

#include "lwip/netif.h"
#include "lwip/timeouts.h"
#include "lwip/dns.h"
#include "lwip/dhcp.h"
#include "lwip/apps/mdns.h"
#include "extmod/network_cyw43.h"
#include "drivers/cyw43/cyw43.h"

// Poll lwIP every 128ms
#define LWIP_TICK(tick) (((tick) & ~(SYSTICK_DISPATCH_NUM_SLOTS - 1) & 0x7f) == 0)

u32_t sys_now(void) {
return mp_hal_ticks_ms();
}

STATIC void pyb_lwip_poll(void) {
#if MICROPY_PY_WIZNET5K
// Poll the NIC for incoming data
wiznet5k_poll();
#endif

// Run the lwIP internal updates
sys_check_timeouts();
}

void mod_network_lwip_poll_wrapper(uint32_t ticks_ms) {
if (LWIP_TICK(ticks_ms)) {
pendsv_schedule_dispatch(PENDSV_DISPATCH_LWIP, pyb_lwip_poll);
}

#if MICROPY_PY_NETWORK_CYW43
if (cyw43_poll) {
if (cyw43_sleep != 0) {
if (--cyw43_sleep == 0) {
pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, cyw43_poll);
}
}
}
#endif
}

#endif

/// \module network - network configuration
Expand Down Expand Up @@ -124,28 +87,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_route_obj, network_route);

STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },

#if defined(MICROPY_HW_ETH_MDC)
{ MP_ROM_QSTR(MP_QSTR_LAN), MP_ROM_PTR(&network_lan_type) },
#endif
#if MICROPY_PY_NETWORK_CYW43
{ MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&mp_network_cyw43_type) },
#endif

#if MICROPY_PY_WIZNET5K
{ MP_ROM_QSTR(MP_QSTR_WIZNET5K), MP_ROM_PTR(&mod_network_nic_type_wiznet5k) },
#endif
#if MICROPY_PY_CC3K
{ MP_ROM_QSTR(MP_QSTR_CC3K), MP_ROM_PTR(&mod_network_nic_type_cc3k) },
#endif

{ MP_ROM_QSTR(MP_QSTR_route), MP_ROM_PTR(&network_route_obj) },

// Defined per port in mpconfigport.h
MICROPY_PORT_NETWORK_INTERFACES

// Constants
#if MICROPY_PY_NETWORK_CYW43
{ MP_ROM_QSTR(MP_QSTR_STA_IF), MP_ROM_INT(CYW43_ITF_STA)},
{ MP_ROM_QSTR(MP_QSTR_AP_IF), MP_ROM_INT(CYW43_ITF_AP)},
#endif
{ MP_ROM_QSTR(MP_QSTR_STA_IF), MP_ROM_INT(MOD_NETWORK_STA_IF) },
{ MP_ROM_QSTR(MP_QSTR_AP_IF), MP_ROM_INT(MOD_NETWORK_AP_IF) },
};

STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
Expand Down
22 changes: 10 additions & 12 deletions ports/stm32/modnetwork.h → extmod/modnetwork.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,13 @@
#define MOD_NETWORK_SOCK_DGRAM (2)
#define MOD_NETWORK_SOCK_RAW (3)

#if MICROPY_PY_LWIP
#define MOD_NETWORK_STA_IF (0)
#define MOD_NETWORK_AP_IF (1)

#if MICROPY_PY_LWIP
struct netif;

extern const mp_obj_type_t network_lan_type;
extern const mp_obj_type_t mod_network_nic_type_wiznet5k;

void mod_network_lwip_poll_wrapper(uint32_t ticks_ms);
mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_obj_t *args);

void wiznet5k_poll(void);

#else

struct _mod_network_socket_obj_t;
Expand Down Expand Up @@ -82,15 +77,18 @@ typedef struct _mod_network_socket_obj_t {
uint8_t domain;
uint8_t type;
int8_t fileno;
uint8_t bound;
} u_param;
mp_uint_t u_state;
};
#if MICROPY_PY_USOCKET_EXTENDED_STATE
// Extended socket state for NICs/ports that need it.
int32_t timeout;
void *state;
#endif
} mod_network_socket_obj_t;

extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
extern const mod_network_nic_type_t mod_network_nic_type_cc3k;

#endif
#endif // MICROPY_PY_LWIP

void mod_network_init(void);
void mod_network_deinit(void);
Expand Down
39 changes: 38 additions & 1 deletion ports/stm32/modusocket.c → extmod/modusocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t
s->u_param.domain = MOD_NETWORK_AF_INET;
s->u_param.type = MOD_NETWORK_SOCK_STREAM;
s->u_param.fileno = -1;
s->u_param.bound = false;
if (n_args >= 1) {
s->u_param.domain = mp_obj_get_int(args[0]);
if (n_args >= 2) {
Expand All @@ -63,7 +64,10 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t
}
}
}

#if MICROPY_PY_USOCKET_EXTENDED_STATE
s->timeout = 0;
s->state = NULL;
#endif
return MP_OBJ_FROM_PTR(s);
}

Expand Down Expand Up @@ -356,6 +360,11 @@ STATIC const mp_rom_map_elem_t socket_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_setsockopt), MP_ROM_PTR(&socket_setsockopt_obj) },
{ MP_ROM_QSTR(MP_QSTR_settimeout), MP_ROM_PTR(&socket_settimeout_obj) },
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },

{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
};

STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
Expand All @@ -379,7 +388,35 @@ mp_uint_t socket_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *
return self->nic_type->ioctl(self, request, arg, errcode);
}

mp_uint_t socket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (self->nic == MP_OBJ_NULL) {
return MP_STREAM_ERROR;
}
mp_int_t ret = self->nic_type->recv(self, (byte *)buf, size, errcode);
if (ret < 0) {
ret = MP_STREAM_ERROR;
*errcode = -(*errcode); // expects a positive error code
}
return ret;
}

mp_uint_t socket_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (self->nic == MP_OBJ_NULL) {
return MP_STREAM_ERROR;
}
mp_int_t ret = self->nic_type->send(self, buf, size, errcode);
if (ret < 0) {
ret = MP_STREAM_ERROR;
*errcode = -(*errcode); // expects a positive error code
}
return ret;
}

STATIC const mp_stream_p_t socket_stream_p = {
.read = socket_read,
.write = socket_write,
.ioctl = socket_ioctl,
.is_text = false,
};
Expand Down
7 changes: 4 additions & 3 deletions ports/stm32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ endif

EXTMOD_SRC_C += $(addprefix extmod/,\
modonewire.c \
)
modnetwork.c \
modusocket.c \
)

DRIVERS_SRC_C += $(addprefix drivers/,\
bus/softspi.c \
Expand All @@ -287,6 +289,7 @@ SRC_C += \
usbd_msc_interface.c \
mphalport.c \
mpthreadport.c \
mpnetworkport.c \
irq.c \
pendsv.c \
systick.c \
Expand Down Expand Up @@ -329,9 +332,7 @@ SRC_C += \
modstm.c \
moduos.c \
modutime.c \
modusocket.c \
network_lan.c \
modnetwork.c \
extint.c \
usrsw.c \
rng.c \
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/eth.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "py/mperrno.h"
#include "shared/netutils/netutils.h"
#include "pin_static_af.h"
#include "modnetwork.h"
#include "extmod/modnetwork.h"
#include "mpu.h"
#include "eth.h"

Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
#include "servo.h"
#include "dac.h"
#include "can.h"
#include "modnetwork.h"
#include "extmod/modnetwork.h"

#if MICROPY_PY_THREAD
STATIC pyb_thread_t pyb_thread_main;
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/modnwcc3k.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "py/mperrno.h"
#include "py/mphal.h"
#include "shared/netutils/netutils.h"
#include "modnetwork.h"
#include "extmod/modnetwork.h"
#include "pin.h"
#include "spi.h"

Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/modnwwiznet5k.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "py/mperrno.h"
#include "py/mphal.h"
#include "shared/netutils/netutils.h"
#include "modnetwork.h"
#include "extmod/modnetwork.h"
#include "pin.h"
#include "spi.h"

Expand Down
34 changes: 34 additions & 0 deletions ports/stm32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,34 @@ extern const struct _mp_obj_module_t mp_module_onewire;
#define ONEWIRE_BUILTIN_MODULE
#endif

#if defined(MICROPY_HW_ETH_MDC)
extern const struct _mp_obj_type_t network_lan_type;
#define MICROPY_HW_NIC_ETH { MP_ROM_QSTR(MP_QSTR_LAN), MP_ROM_PTR(&network_lan_type) },
#else
#define MICROPY_HW_NIC_ETH
#endif

#if MICROPY_PY_NETWORK_CYW43
extern const struct _mp_obj_type_t mp_network_cyw43_type;
#define MICROPY_HW_NIC_CYW43 { MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&mp_network_cyw43_type) },
#else
#define MICROPY_HW_NIC_CYW43
#endif

#if MICROPY_PY_WIZNET5K
extern const struct _mod_network_nic_type_t mod_network_nic_type_wiznet5k;
#define MICROPY_HW_NIC_WIZNET5K { MP_ROM_QSTR(MP_QSTR_WIZNET5K), MP_ROM_PTR(&mod_network_nic_type_wiznet5k) },
#else
#define MICROPY_HW_NIC_WIZNET5K
#endif

#if MICROPY_PY_CC3K
extern const struct _mod_network_nic_type_t mod_network_nic_type_cc3k;
#define MICROPY_HW_NIC_CC3K { MP_ROM_QSTR(MP_QSTR_CC3K), MP_ROM_PTR(&mod_network_nic_type_cc3k) },
#else
#define MICROPY_HW_NIC_CC3K
#endif

#define MICROPY_PORT_BUILTIN_MODULES \
MACHINE_BUILTIN_MODULE \
PYB_BUILTIN_MODULE \
Expand All @@ -338,6 +366,12 @@ extern const struct _mp_obj_module_t mp_module_onewire;
PYB_BUILTIN_MODULE \
STM_BUILTIN_MODULE \

#define MICROPY_PORT_NETWORK_INTERFACES \
MICROPY_HW_NIC_ETH \
MICROPY_HW_NIC_CYW43 \
MICROPY_HW_NIC_WIZNET5K \
MICROPY_HW_NIC_CC3K \

#define MP_STATE_PORT MP_STATE_VM

#if MICROPY_SSL_MBEDTLS
Expand Down
81 changes: 81 additions & 0 deletions ports/stm32/mpnetworkport.c
Original file line number Diff line number F42D Diff line change
@@ -0,0 +1,81 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2014 Damien P. George
*
* 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 <stdio.h>
#include <stdint.h>
#include <string.h>

#include "py/objlist.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared/netutils/netutils.h"
#include "systick.h"
#include "pendsv.h"
#include "extmod/modnetwork.h"

#if MICROPY_PY_LWIP
#include "lwip/netif.h"
#include "lwip/timeouts.h"
#include "lwip/dns.h"
#include "lwip/dhcp.h"
#include "lwip/apps/mdns.h"
#include "extmod/network_cyw43.h"
#include "drivers/cyw43/cyw43.h"

// Poll lwIP every 128ms
#define LWIP_TICK(tick) (((tick) & ~(SYSTICK_DISPATCH_NUM_SLOTS - 1) & 0x7f) == 0)

u32_t sys_now(void) {
return mp_hal_ticks_ms();
}

STATIC void pyb_lwip_poll(void) {
#if MICROPY_PY_WIZNET5K
// Poll the NIC for incoming data
wiznet5k_poll();
#endif

// Run the lwIP internal updates
sys_check_timeouts();
}

void mod_network_lwip_poll_wrapper(uint32_t ticks_ms) {
if (LWIP_TICK(ticks_ms)) {
pendsv_schedule_dispatch(PENDSV_DISPATCH_LWIP, pyb_lwip_poll);
}

#if MICROPY_PY_NETWORK_CYW43
if (cyw43_poll) {
if (cyw43_sleep != 0) {
if (--cyw43_sleep == 0) {
pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, cyw43_poll);
}
}
}
#endif
}

#endif // MICROPY_PY_LWIP
2 changes: 1 addition & 1 deletion ports/stm32/network_lan.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "py/runtime.h"
#include "py/mphal.h"
#include "modnetwork.h"
#include "extmod/modnetwork.h"
#include "eth.h"

#if defined(MICROPY_HW_ETH_MDC)
Expand Down
Loading
0