Skip to content

Commit 75f6d01

Browse files
committed
sketchy modsocket ... revisit this once modnetwork is sorted
1 parent 406b468 commit 75f6d01

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

esp32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ SRC_C = \
102102
machine_pin.c \
103103
modmachine.c \
104104
modnetwork.c \
105+
modsocket.c \
105106
modesp.c \
106107
$(SRC_MOD)
107108

esp32/modsocket.c

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Nick Moore
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 <stdlib.h>
30+
#include <string.h>
31+
32+
#include "py/nlr.h"
33+
#include "py/objlist.h"
34+
#include "py/runtime.h"
35+
#include "py/mphal.h"
36+
37+
#include "lwip/sockets.h"
38+
#include "lwip/netdb.h"
39+
#include "esp_log.h"
40+
41+
typedef struct _socket_t {
42+
mp_obj_base_t base;
43+
int fd;
44+
} socket_t;
45+
46+
47+
STATIC mp_obj_t socket_close(const mp_obj_t arg0) {
48+
socket_t *self = MP_OBJ_TO_PTR(arg0);
49+
lwip_close(self->fd);
50+
return mp_const_none;
51+
}
52+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
53+
54+
int _socket_getaddrinfo2(const mp_obj_t host, const mp_obj_t port, struct addrinfo **resp) {
55+
mp_uint_t hostlen, portlen;
56+
const char *shost = mp_obj_str_get_data(host, &hostlen);
57+
const char *sport = mp_obj_str_get_data(port, &portlen);
58+
const struct addrinfo hints = {
59+
.ai_family = AF_INET,
60+
.ai_socktype = SOCK_STREAM,
61+
};
62+
lwip_getaddrinfo(shost, sport, &hints, resp);
63+
return 0;
64+
}
65+
66+
int _socket_getaddrinfo(const mp_obj_t addrtuple, struct addrinfo **resp) {
67+
mp_uint_t len = 0;
68+
mp_obj_t *elem;
69+
mp_obj_get_array(addrtuple, &len, &elem);
70+
if (len == 2) return _socket_getaddrinfo2(elem[0], elem[1], resp);
71+
else return -1;
72+
}
73+
74+
STATIC mp_obj_t socket_bind(const mp_obj_t arg0, const mp_obj_t arg1) {
75+
socket_t *self = MP_OBJ_TO_PTR(arg0);
76+
struct addrinfo *res;
77+
_socket_getaddrinfo(arg1, &res);
78+
lwip_bind(self->fd, res->ai_addr, res->ai_addrlen);
79+
return mp_const_none;
80+
}
81+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
82+
83+
STATIC mp_obj_t socket_listen(const mp_obj_t arg0, const mp_obj_t arg1) {
84+
socket_t *self = MP_OBJ_TO_PTR(arg0);
85+
int backlog = mp_obj_get_int(arg1);
86+
int x = lwip_listen(self->fd, backlog);
87+
return (x == 0) ? mp_const_true : mp_const_false;
88+
}
89+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, socket_listen);
90+
91+
STATIC mp_obj_t socket_accept(const mp_obj_t arg0) {
92+
socket_t *self = MP_OBJ_TO_PTR(arg0);
93+
int x = lwip_accept(self->fd, NULL, NULL);
94+
if (x >= 0) {
95+
socket_t *sock = (socket_t *)calloc(1, sizeof(socket_t));
96+
sock->base.type = self->base.type;
97+
sock->fd = x;
98+
return MP_OBJ_FROM_PTR(sock);
99+
}
100+
return mp_const_none;
101+
}
102+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept);
103+
104+
STATIC mp_obj_t socket_connect(const mp_obj_t arg0, const mp_obj_t arg1) {
105+
socket_t *self = MP_OBJ_TO_PTR(arg0);
106+
struct addrinfo *res;
107+
_socket_getaddrinfo(arg1, &res);
108+
int r = lwip_connect(self->fd, res->ai_addr, res->ai_addrlen);
109+
lwip_freeaddrinfo(res);
110+
return mp_obj_new_int(r);
111+
}
112+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
113+
114+
STATIC mp_obj_t socket_settimeout(const mp_obj_t arg0, const mp_obj_t arg1) {
115+
socket_t *self = MP_OBJ_TO_PTR(arg0);
116+
int timeout = mp_obj_get_int(arg1);
117+
//lwip_settimeout(self->fd, timeout);
118+
return mp_const_none;
119+
}
120+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_settimeout_obj, socket_settimeout);
121+
122+
STATIC mp_obj_t socket_read(const mp_obj_t arg0) {
123+
byte buf[1024];
124+
socket_t *self = MP_OBJ_TO_PTR(arg0);
125+
int x = lwip_recvfrom(self->fd, buf, sizeof(buf), MSG_DONTWAIT, NULL, NULL);
126+
if (x >= 0) return mp_obj_new_bytes(buf, x);
127+
return mp_const_none;
128+
}
129+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_read_obj, socket_read);
130+
131+
STATIC mp_obj_t socket_write(const mp_obj_t arg0, const mp_obj_t arg1) {
132+
socket_t *self = MP_OBJ_TO_PTR(arg0);
133+
mp_uint_t datalen;
134+
const char *data = mp_obj_str_get_data(arg1, &datalen);
135+
int x = lwip_write(self->fd, data, datalen);
136+
return mp_obj_new_int(x);
137+
}
138+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_write_obj, socket_write);
139+
140+
STATIC mp_obj_t socket_fileno(const mp_obj_t arg0) {
141+
socket_t *self = MP_OBJ_TO_PTR(arg0);
142+
return mp_obj_new_int(self->fd);
143+
}
144+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_fileno_obj, socket_fileno);
145+
146+
STATIC const mp_map_elem_t socket_locals_dict_table[] = {
147+
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj },
148+
{ MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj },
149+
{ MP_OBJ_NEW_QSTR(MP_QSTR_listen), (mp_obj_t)&socket_listen_obj },
150+
{ MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj },
151+
{ MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj },
152+
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), mp_const_none },
153+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sendall), mp_const_none },
154+
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), mp_const_none },
155+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sendto), mp_const_none },
156+
{ MP_OBJ_NEW_QSTR(MP_QSTR_recvfrom), mp_const_none },
157+
{ MP_OBJ_NEW_QSTR(MP_QSTR_setsockopt), mp_const_none },
158+
{ MP_OBJ_NEW_QSTR(MP_QSTR_settimeout), mp_const_none },
159+
{ MP_OBJ_NEW_QSTR(MP_QSTR_makefile), mp_const_none },
160+
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&socket_read_obj },
161+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), mp_const_none },
162+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), mp_const_none },
163+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&socket_write_obj },
164+
{ MP_OBJ_NEW_QSTR(MP_QSTR_fileno), (mp_obj_t)&socket_fileno_obj },
165+
// XXX also need to catch destructor so we can free!
166+
};
167+
STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
168+
169+
const mp_obj_type_t socket_type = {
170+
{ &mp_type_type },
171+
.name = MP_QSTR_socket,
172+
.locals_dict = (mp_obj_t)&socket_locals_dict,
173+
};
174+
175+
STATIC mp_obj_t get_socket(mp_uint_t n_args, const mp_obj_t *args) {
176+
socket_t *sock = (socket_t *)calloc(1, sizeof(socket_t));
177+
sock->base.type = &socket_type;
178+
sock->fd = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
179+
return MP_OBJ_FROM_PTR(sock);
180+
}
181+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(get_socket_obj, 0, 3, get_socket);
182+
183+
STATIC mp_obj_t esp_socket_getaddrinfo(const mp_obj_t host, const mp_obj_t port) {
184+
struct addrinfo *res;
185+
_socket_getaddrinfo2(host, port, &res);
186+
return mp_obj_new_bytes((const byte *)res->ai_addr, res->ai_addrlen);
187+
}
188+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_socket_getaddrinfo_obj, esp_socket_getaddrinfo);
189+
190+
STATIC const mp_map_elem_t mp_module_socket_globals_table[] = {
191+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_socket) },
192+
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&get_socket_obj },
193+
{ MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&esp_socket_getaddrinfo_obj },
194+
};
195+
196+
STATIC MP_DEFINE_CONST_DICT(mp_module_socket_globals, mp_module_socket_globals_table);
197+
198+
const mp_obj_module_t mp_module_socket = {
199+
.base = { &mp_type_module },
200+
.globals = (mp_obj_dict_t*)&mp_module_socket_globals,
201+
};

esp32/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,15 @@ extern const struct _mp_obj_module_t utime_module;
143143
extern const struct _mp_obj_module_t uos_module;
144144
extern const struct _mp_obj_module_t mp_module_machine;
145145
extern const struct _mp_obj_module_t mp_module_network;
146+
extern const struct _mp_obj_module_t mp_module_socket;
146147

147148
#define MICROPY_PORT_BUILTIN_MODULES \
148149
{ MP_OBJ_NEW_QSTR(MP_QSTR_esp), (mp_obj_t)&esp_module }, \
149150
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&utime_module }, \
150151
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \
151152
{ MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \
152153
{ MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \
154+
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&mp_module_socket }, \
153155

154156
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
155157
{ MP_OBJ_NEW_QSTR(MP_QSTR_binascii), (mp_obj_t)&mp_module_ubinascii }, \

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy