Skip to content

Commit 823ff77

Browse files
committed
network module c api into shared-module
1 parent 8d75c3d commit 823ff77

File tree

8 files changed

+102
-90
lines changed

8 files changed

+102
-90
lines changed

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#include "supervisor/serial.h"
5656

5757
#ifdef MICROPY_PY_NETWORK
58-
#include "shared-bindings/network/__init__.h"
58+
#include "shared-module/network/__init__.h"
5959
#endif
6060

6161
void do_str(const char *src, mp_parse_input_kind_t input_kind) {

shared-bindings/network/__init__.c

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,7 @@
4747

4848
/// \module network - network configuration
4949
///
50-
/// This module provides network drivers and routing configuration.
51-
52-
void network_module_init(void) {
53-
mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
54-
}
55-
56-
void network_module_deinit(void) {
57-
}
58-
59-
void network_module_register_nic(mp_obj_t nic) {
60-
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
61-
if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {
62-
// nic already registered
63-
return;
64-
}
65-
}
66-
// nic not registered so add to list
67-
mp_obj_list_append(MP_OBJ_FROM_PTR(&MP_STATE_PORT(mod_network_nic_list)), nic);
68-
}
69-
70-
mp_obj_t network_module_find_nic(const uint8_t *ip) {
71-
// find a NIC that is suited to given IP address
72-
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
73-
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
74-
// TODO check IP suitability here
75-
//mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
76-
return nic;
77-
}
78-
79-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, translate("no available NIC")));
80-
}
50+
/// This module provides a registry of configured NICs.
8151

8252
STATIC mp_obj_t network_route(void) {
8353
return MP_OBJ_FROM_PTR(&MP_STATE_PORT(mod_network_nic_list));

shared-bindings/network/__init__.h

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,59 +26,6 @@
2626
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_NETWORK___INIT___H
2727
#define MICROPY_INCLUDED_SHARED_BINDINGS_NETWORK___INIT___H
2828

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-
struct _mod_network_socket_obj_t;
39-
40-
typedef struct _mod_network_nic_type_t {
41-
mp_obj_type_t base;
42-
43-
// API for non-socket operations
44-
int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out);
45-
46-
// API for socket operations; return -1 on error
47-
int (*socket)(struct _mod_network_socket_obj_t *socket, int *_errno);
48-
void (*close)(struct _mod_network_socket_obj_t *socket);
49-
int (*bind)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
50-
int (*listen)(struct _mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno);
51-
int (*accept)(struct _mod_network_socket_obj_t *socket, struct _mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno);
52-
int (*connect)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
53-
mp_uint_t (*send)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno);
54-
mp_uint_t (*recv)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno);
55-
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);
56-
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);
57-
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);
58-
int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
59-
int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
60-
} mod_network_nic_type_t;
61-
62-
typedef struct _mod_network_socket_obj_t {
63-
mp_obj_base_t base;
64-
mp_obj_t nic;
65-
mod_network_nic_type_t *nic_type;
66-
union {
67-
struct {
68-
uint8_t domain;
69-
uint8_t type;
70-
int8_t fileno;
71-
} u_param;
72-
mp_uint_t u_state;
73-
};
74-
} mod_network_socket_obj_t;
75-
76-
extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
77-
extern const mod_network_nic_type_t mod_network_nic_type_cc3k;
78-
79-
void network_module_init(void);
80-
void network_module_deinit(void);
81-
void network_module_register_nic(mp_obj_t nic);
82-
mp_obj_t network_module_find_nic(const uint8_t *ip);
29+
// nothing
8330

8431
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_NETWORK___INIT___H

shared-bindings/socket/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "py/mperrno.h"
3636
#include "lib/netutils/netutils.h"
3737

38-
#include "shared-bindings/network/__init__.h"
38+
#include "shared-module/network/__init__.h"
3939

4040
//| :mod:`socket` --- TCP, UDP and RAW socket support
4141
//| =================================================

shared-bindings/wiznet/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "py/runtime.h"
3434
#include "py/mphal.h"
3535

36-
#include "shared-bindings/network/__init__.h"
36+
#include "shared-module/network/__init__.h"
3737

3838
//| :mod:`wiznet` --- Support for WizNet hardware
3939
//| =============================================

shared-bindings/wiznet/wiznet5k.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "py/mphal.h"
3737
#include "lib/netutils/netutils.h"
3838

39-
#include "shared-bindings/network/__init__.h"
39+
#include "shared-module/network/__init__.h"
4040
#include "shared-bindings/digitalio/DigitalInOut.h"
4141
#include "shared-bindings/digitalio/DriveMode.h"
4242
#include "shared-bindings/busio/SPI.h"

shared-module/network/__init__.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,46 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/objlist.h"
28+
#include "py/runtime.h"
2729
#include "py/mphal.h"
30+
#include "py/mperrno.h"
2831

2932
#include "shared-bindings/random/__init__.h"
3033

34+
// mod_network_nic_list needs to be declared in mpconfigport.h
35+
36+
37+
void network_module_init(void) {
38+
mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
39+
}
40+
41+
void network_module_deinit(void) {
42+
}
43+
44+
void network_module_register_nic(mp_obj_t nic) {
45+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
46+
if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {
47+
// nic already registered
48+
return;
49+
}
50+
}
51+
// nic not registered so add to list
52+
mp_obj_list_append(MP_OBJ_FROM_PTR(&MP_STATE_PORT(mod_network_nic_list)), nic);
53+
}
54+
55+
mp_obj_t network_module_find_nic(const uint8_t *ip) {
56+
// find a NIC that is suited to given IP address
57+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
58+
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
59+
// TODO check IP suitability here
60+
//mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
61+
return nic;
62+
}
63+
64+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, translate("no available NIC")));
65+
}
66+
3167
void network_module_create_random_mac_address(uint8_t *mac) {
3268
uint32_t rb1 = shared_modules_random_getrandbits(24);
3369
uint32_t rb2 = shared_modules_random_getrandbits(24);
@@ -40,4 +76,3 @@ void network_module_create_random_mac_address(uint8_t *mac) {
4076
mac[4] = (uint8_t)(rb2 >> 8);
4177
mac[5] = (uint8_t)(rb2);
4278
}
43-

shared-module/network/__init__.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6+
* Copyright (c) 2013, 2014 Damien P. George
67
* Copyright (c) 2018 Nick Moore
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -26,3 +27,62 @@
2627

2728
void network_module_create_random_mac_address(uint8_t *mac);
2829

30+
#ifndef MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H
31+
#define MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H
32+
33+
#define MOD_NETWORK_IPADDR_BUF_SIZE (4)
34+
35+
#define MOD_NETWORK_AF_INET (2)
36+
#define MOD_NETWORK_AF_INET6 (10)
37+
38+
#define MOD_NETWORK_SOCK_STREAM (1)
39+
#define MOD_NETWORK_SOCK_DGRAM (2)
40+
#define MOD_NETWORK_SOCK_RAW (3)
41+
42+
struct _mod_network_socket_obj_t;
43+
44+
typedef struct _mod_network_nic_type_t {
45+
mp_obj_type_t base;
46+
47+
// API for non-socket operations
48+
int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out);
49+
50+
// API for socket operations; return -1 on error
51+
int (*socket)(struct _mod_network_socket_obj_t *socket, int *_errno);
52+
void (*close)(struct _mod_network_socket_obj_t *socket);
53+
int (*bind)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
54+
int (*listen)(struct _mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno);
55+
int (*accept)(struct _mod_network_socket_obj_t *socket, struct _mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno);
56+
int (*connect)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
57+
mp_uint_t (*send)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno);
58+
mp_uint_t (*recv)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno);
59+
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);
60+
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);
61+
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);
62+
int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
63+
int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
64+
} mod_network_nic_type_t;
65+
66+
typedef struct _mod_network_socket_obj_t {
67+
mp_obj_base_t base;
68+
mp_obj_t nic;
69+
mod_network_nic_type_t *nic_type;
70+
union {
71+
struct {
72+
uint8_t domain;
73+
uint8_t type;
74+
int8_t fileno;
75+
} u_param;
76+
mp_uint_t u_state;
77+
};
78+
} mod_network_socket_obj_t;
79+
80+
extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
81+
extern const mod_network_nic_type_t mod_network_nic_type_cc3k;
82+
83+
void network_module_init(void);
84+
void network_module_deinit(void);
85+
void network_module_register_nic(mp_obj_t nic);
86+
mp_obj_t network_module_find_nic(const uint8_t *ip);
87+
88+
#endif // MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H

0 commit comments

Comments
 (0)