Skip to content

Commit f4c3213

Browse files
committed
Copy modnetwork and modusocket across from MicroPython
1 parent 27fc84a commit f4c3213

File tree

3 files changed

+644
-0
lines changed

3 files changed

+644
-0
lines changed

shared-bindings/network/__init__.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 "modnetwork.h"
34+
35+
#if MICROPY_PY_NETWORK
36+
37+
/// \module network - network configuration
38+
///
39+
/// This module provides network drivers and routing configuration.
40+
41+
void mod_network_init(void) {
42+
mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
43+
}
44+
45+
void mod_network_register_nic(mp_obj_t nic) {
46+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
47+
if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {
48+
// nic already registered
49+
return;
50+
}
51+
}
52+
// nic not registered so add to list
53+
mp_obj_list_append(&MP_STATE_PORT(mod_network_nic_list), nic);
54+
}
55+
56+
mp_obj_t mod_network_find_nic(const uint8_t *ip) {
57+
// find a NIC that is suited to given IP address
58+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
59+
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
60+
// TODO check IP suitability here
61+
//mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
62+
return nic;
63+
}
64+
65+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no available NIC"));
66+
}
67+
68+
STATIC mp_obj_t network_route(void) {
69+
return &MP_STATE_PORT(mod_network_nic_list);
70+
}
71+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_route_obj, network_route);
72+
73+
STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
74+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },
75+
76+
#if MICROPY_PY_WIZNET5K
77+
{ MP_ROM_QSTR(MP_QSTR_WIZNET5K), MP_ROM_PTR(&mod_network_nic_type_wiznet5k) },
78+
#endif
79+
#if MICROPY_PY_CC3K
80+
{ MP_ROM_QSTR(MP_QSTR_CC3K), MP_ROM_PTR(&mod_network_nic_type_cc3k) },
81+
#endif
82+
83+
{ MP_ROM_QSTR(MP_QSTR_route), MP_ROM_PTR(&network_route_obj) },
84+
};
85+
86+
STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
87+
88+
const mp_obj_module_t mp_module_network = {
89+
.base = { &mp_type_module },
90+
.globals = (mp_obj_dict_t*)&mp_module_network_globals,
91+
};
92+
93+
#endif // MICROPY_PY_NETWORK

shared-bindings/network/__init__.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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_STM32_MODNETWORK_H
27+
#define MICROPY_INCLUDED_STM32_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+
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 mod_network_init(void);
80+
void mod_network_register_nic(mp_obj_t nic);
81+
mp_obj_t mod_network_find_nic(const uint8_t *ip);
82+
83+
#endif // MICROPY_INCLUDED_STM32_MODNETWORK_H

0 commit comments

Comments
 (0)