2017-03-30 22:04:31 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Linaro Limited
|
|
|
|
*
|
|
|
|
* 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 "py/mpconfig.h"
|
|
|
|
#ifdef MICROPY_PY_USOCKET
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
#include <zephyr.h>
|
|
|
|
#include <net/net_context.h>
|
|
|
|
#include <net/nbuf.h>
|
|
|
|
|
|
|
|
typedef struct _socket_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
struct net_context *ctx;
|
|
|
|
} socket_obj_t;
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t socket_type;
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
#define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); }
|
|
|
|
|
2017-03-31 07:10:30 +01:00
|
|
|
STATIC void socket_check_closed(socket_obj_t *socket) {
|
|
|
|
if (socket->ctx == NULL) {
|
|
|
|
// already closed
|
|
|
|
mp_raise_OSError(EBADF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct sockaddr *sockaddr) {
|
|
|
|
// We employ the fact that port and address offsets are the same for IPv4 & IPv6
|
|
|
|
struct sockaddr_in *sockaddr_in = (struct sockaddr_in*)sockaddr;
|
|
|
|
|
|
|
|
mp_obj_t *addr_items;
|
|
|
|
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
|
|
|
|
sockaddr_in->sin_family = net_context_get_family(socket->ctx);
|
|
|
|
RAISE_ERRNO(net_addr_pton(sockaddr_in->sin_family, mp_obj_str_get_str(addr_items[0]), &sockaddr_in->sin_addr));
|
|
|
|
sockaddr_in->sin_port = htons(mp_obj_get_int(addr_items[1]));
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:04:31 +01:00
|
|
|
// Methods
|
|
|
|
|
|
|
|
STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
socket_obj_t *self = self_in;
|
|
|
|
if (self->ctx == NULL) {
|
|
|
|
mp_printf(print, "<socket NULL>");
|
|
|
|
} else {
|
|
|
|
struct net_context *ctx = self->ctx;
|
|
|
|
mp_printf(print, "<socket %p type=%d>", ctx, net_context_get_type(ctx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 4, false);
|
|
|
|
|
|
|
|
socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t);
|
|
|
|
socket->base.type = type;
|
|
|
|
|
|
|
|
int family = AF_INET;
|
|
|
|
int socktype = SOCK_STREAM;
|
|
|
|
int proto = -1;
|
|
|
|
|
|
|
|
if (n_args >= 1) {
|
|
|
|
family = mp_obj_get_int(args[0]);
|
|
|
|
if (n_args >= 2) {
|
|
|
|
socktype = mp_obj_get_int(args[1]);
|
|
|
|
if (n_args >= 3) {
|
|
|
|
proto = mp_obj_get_int(args[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proto == -1) {
|
|
|
|
proto = IPPROTO_TCP;
|
|
|
|
if (socktype != SOCK_STREAM) {
|
|
|
|
proto = IPPROTO_UDP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RAISE_ERRNO(net_context_get(family, socktype, proto, &socket->ctx));
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2017-03-31 07:10:30 +01:00
|
|
|
STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
|
|
|
|
socket_obj_t *socket = self_in;
|
|
|
|
socket_check_closed(socket);
|
|
|
|
|
|
|
|
struct sockaddr sockaddr;
|
|
|
|
parse_inet_addr(socket, addr_in, &sockaddr);
|
|
|
|
|
|
|
|
RAISE_ERRNO(net_context_bind(socket->ctx, &sockaddr, sizeof(sockaddr)));
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
|
|
|
|
|
|
|
|
STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
|
|
|
|
socket_obj_t *socket = self_in;
|
|
|
|
socket_check_closed(socket);
|
|
|
|
|
|
|
|
struct sockaddr sockaddr;
|
|
|
|
parse_inet_addr(socket, addr_in, &sockaddr);
|
|
|
|
|
|
|
|
RAISE_ERRNO(net_context_connect(socket->ctx, &sockaddr, sizeof(sockaddr), NULL, K_FOREVER, NULL));
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
|
|
|
|
|
2017-03-30 22:04:31 +01:00
|
|
|
STATIC mp_obj_t socket_close(mp_obj_t self_in) {
|
|
|
|
socket_obj_t *socket = self_in;
|
|
|
|
if (socket->ctx != NULL) {
|
|
|
|
RAISE_ERRNO(net_context_put(socket->ctx));
|
|
|
|
socket->ctx = NULL;
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
|
|
|
|
|
|
|
|
STATIC const mp_map_elem_t socket_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&socket_close_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj },
|
2017-03-31 07:10:30 +01:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj },
|
2017-03-30 22:04:31 +01:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t socket_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_socket,
|
|
|
|
.print = socket_print,
|
|
|
|
.make_new = socket_make_new,
|
|
|
|
//.protocol = &socket_stream_p,
|
|
|
|
.locals_dict = (mp_obj_t)&socket_locals_dict,
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) },
|
|
|
|
// objects
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&socket_type },
|
|
|
|
// class constants
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(AF_INET) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET6), MP_OBJ_NEW_SMALL_INT(AF_INET6) },
|
|
|
|
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_STREAM), MP_OBJ_NEW_SMALL_INT(SOCK_STREAM) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_DGRAM), MP_OBJ_NEW_SMALL_INT(SOCK_DGRAM) },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_usocket = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_usocket_globals,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_USOCKET
|