2014-11-29 13:48:45 +00:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-11-29 13:48:45 +00:00
|
|
|
*
|
|
|
|
* 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 <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
// CC3000 defines its own ENOBUFS (different to standard one!)
|
|
|
|
#undef ENOBUFS
|
|
|
|
|
2015-01-01 21:06:20 +00:00
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/objlist.h"
|
|
|
|
#include "py/stream.h"
|
|
|
|
#include "py/runtime.h"
|
2016-05-10 23:45:33 +01:00
|
|
|
#include "py/mperrno.h"
|
2017-03-02 05:17:04 +00:00
|
|
|
#include "py/mphal.h"
|
2021-07-09 05:19:15 +01:00
|
|
|
#include "shared/netutils/netutils.h"
|
2014-11-29 13:48:45 +00:00
|
|
|
#include "modnetwork.h"
|
|
|
|
#include "pin.h"
|
|
|
|
#include "spi.h"
|
|
|
|
|
|
|
|
#include "hci.h"
|
|
|
|
#include "socket.h"
|
|
|
|
#include "inet_ntop.h"
|
|
|
|
#include "inet_pton.h"
|
|
|
|
#include "ccspi.h"
|
|
|
|
#include "wlan.h"
|
|
|
|
#include "nvmem.h"
|
|
|
|
#include "netapp.h"
|
|
|
|
#include "patch_prog.h"
|
|
|
|
|
|
|
|
#define MAX_ADDRSTRLEN (128)
|
|
|
|
#define MAX_RX_PACKET (CC3000_RX_BUFFER_SIZE - CC3000_MINIMAL_RX_SIZE - 1)
|
|
|
|
#define MAX_TX_PACKET (CC3000_TX_BUFFER_SIZE - CC3000_MINIMAL_TX_SIZE - 1)
|
|
|
|
|
|
|
|
#define MAKE_SOCKADDR(addr, ip, port) \
|
|
|
|
sockaddr addr; \
|
|
|
|
addr.sa_family = AF_INET; \
|
|
|
|
addr.sa_data[0] = port >> 8; \
|
|
|
|
addr.sa_data[1] = port; \
|
|
|
|
addr.sa_data[2] = ip[0]; \
|
|
|
|
addr.sa_data[3] = ip[1]; \
|
|
|
|
addr.sa_data[4] = ip[2]; \
|
|
|
|
addr.sa_data[5] = ip[3];
|
|
|
|
|
|
|
|
#define UNPACK_SOCKADDR(addr, ip, port) \
|
|
|
|
port = (addr.sa_data[0] << 8) | addr.sa_data[1]; \
|
|
|
|
ip[0] = addr.sa_data[2]; \
|
|
|
|
ip[1] = addr.sa_data[3]; \
|
|
|
|
ip[2] = addr.sa_data[4]; \
|
|
|
|
ip[3] = addr.sa_data[5];
|
|
|
|
|
|
|
|
STATIC int cc3k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
|
|
|
|
|
|
|
|
int CC3000_EXPORT(errno); // for cc3000 driver
|
|
|
|
|
|
|
|
STATIC volatile uint32_t fd_closed_state = 0;
|
|
|
|
STATIC volatile bool wlan_connected = false;
|
|
|
|
STATIC volatile bool ip_obtained = false;
|
|
|
|
|
|
|
|
STATIC int cc3k_get_fd_closed_state(int fd) {
|
|
|
|
return fd_closed_state & (1 << fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void cc3k_set_fd_closed_state(int fd) {
|
|
|
|
fd_closed_state |= 1 << fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void cc3k_reset_fd_closed_state(int fd) {
|
|
|
|
fd_closed_state &= ~(1 << fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void cc3k_callback(long event_type, char *data, unsigned char length) {
|
|
|
|
switch (event_type) {
|
|
|
|
case HCI_EVNT_WLAN_UNSOL_CONNECT:
|
|
|
|
wlan_connected = true;
|
|
|
|
break;
|
|
|
|
case HCI_EVNT_WLAN_UNSOL_DISCONNECT:
|
|
|
|
// link down
|
|
|
|
wlan_connected = false;
|
|
|
|
ip_obtained = false;
|
|
|
|
break;
|
|
|
|
case HCI_EVNT_WLAN_UNSOL_DHCP:
|
|
|
|
ip_obtained = true;
|
|
|
|
break;
|
|
|
|
case HCI_EVNT_BSD_TCP_CLOSE_WAIT:
|
|
|
|
// mark socket for closure
|
|
|
|
cc3k_set_fd_closed_state(data[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int cc3k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip) {
|
|
|
|
uint32_t ip;
|
|
|
|
// CC3000 gethostbyname is unreliable and usually returns -95 on first call
|
|
|
|
for (int retry = 5; CC3000_EXPORT(gethostbyname)((char *)name, len, &ip) < 0; retry--) {
|
|
|
|
if (retry == 0 || CC3000_EXPORT(errno) != -95) {
|
|
|
|
return CC3000_EXPORT(errno);
|
|
|
|
}
|
2017-03-02 04:02:57 +00:00
|
|
|
mp_hal_delay_ms(50);
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ip == 0) {
|
|
|
|
// unknown host
|
2017-10-16 03:19:11 +01:00
|
|
|
return -2;
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out_ip[0] = ip >> 24;
|
|
|
|
out_ip[1] = ip >> 16;
|
|
|
|
out_ip[2] = ip >> 8;
|
|
|
|
out_ip[3] = ip;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int cc3k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) {
|
|
|
|
if (socket->u_param.domain != MOD_NETWORK_AF_INET) {
|
2016-05-10 23:45:33 +01:00
|
|
|
*_errno = MP_EAFNOSUPPORT;
|
2014-11-29 13:48:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_uint_t type;
|
|
|
|
switch (socket->u_param.type) {
|
|
|
|
case MOD_NETWORK_SOCK_STREAM:
|
|
|
|
type = SOCK_STREAM;
|
|
|
|
break;
|
|
|
|
case MOD_NETWORK_SOCK_DGRAM:
|
|
|
|
type = SOCK_DGRAM;
|
|
|
|
break;
|
|
|
|
case MOD_NETWORK_SOCK_RAW:
|
|
|
|
type = SOCK_RAW;
|
|
|
|
break;
|
2016-05-10 23:45:33 +01:00
|
|
|
default:
|
|
|
|
*_errno = MP_EINVAL;
|
|
|
|
return -1;
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open socket
|
|
|
|
int fd = CC3000_EXPORT(socket)(AF_INET, type, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
*_errno = CC3000_EXPORT(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear socket state
|
|
|
|
cc3k_reset_fd_closed_state(fd);
|
|
|
|
|
|
|
|
// store state of this socket
|
|
|
|
socket->u_state = fd;
|
|
|
|
|
|
|
|
// make accept blocking by default
|
|
|
|
int optval = SOCK_OFF;
|
|
|
|
socklen_t optlen = sizeof(optval);
|
|
|
|
CC3000_EXPORT(setsockopt)(socket->u_state, SOL_SOCKET, SOCKOPT_ACCEPT_NONBLOCK, &optval, optlen);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void cc3k_socket_close(mod_network_socket_obj_t *socket) {
|
|
|
|
CC3000_EXPORT(closesocket)(socket->u_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int cc3k_socket_bind(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) {
|
|
|
|
MAKE_SOCKADDR(addr, ip, port)
|
|
|
|
int ret = CC3000_EXPORT(bind)(socket->u_state, &addr, sizeof(addr));
|
|
|
|
if (ret != 0) {
|
|
|
|
*_errno = ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int cc3k_socket_listen(mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno) {
|
|
|
|
int ret = CC3000_EXPORT(listen)(socket->u_state, backlog);
|
|
|
|
if (ret != 0) {
|
|
|
|
*_errno = ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int cc3k_socket_accept(mod_network_socket_obj_t *socket, mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno) {
|
|
|
|
// accept incoming connection
|
|
|
|
int fd;
|
|
|
|
sockaddr addr;
|
|
|
|
socklen_t addr_len = sizeof(addr);
|
|
|
|
if ((fd = CC3000_EXPORT(accept)(socket->u_state, &addr, &addr_len)) < 0) {
|
|
|
|
if (fd == SOC_IN_PROGRESS) {
|
2016-05-10 23:45:33 +01:00
|
|
|
*_errno = MP_EAGAIN;
|
2014-11-29 13:48:45 +00:00
|
|
|
} else {
|
|
|
|
*_errno = -fd;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear socket state
|
|
|
|
cc3k_reset_fd_closed_state(fd);
|
|
|
|
|
|
|
|
// store state in new socket object
|
|
|
|
socket2->u_state = fd;
|
|
|
|
|
|
|
|
// return ip and port
|
|
|
|
// it seems CC3000 returns little endian for accept??
|
|
|
|
// UNPACK_SOCKADDR(addr, ip, *port);
|
|
|
|
*port = (addr.sa_data[1] << 8) | addr.sa_data[0];
|
|
|
|
ip[3] = addr.sa_data[2];
|
|
|
|
ip[2] = addr.sa_data[3];
|
|
|
|
ip[1] = addr.sa_data[4];
|
|
|
|
ip[0] = addr.sa_data[5];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int cc3k_socket_connect(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) {
|
|
|
|
MAKE_SOCKADDR(addr, ip, port)
|
|
|
|
int ret = CC3000_EXPORT(connect)(socket->u_state, &addr, sizeof(addr));
|
|
|
|
if (ret != 0) {
|
|
|
|
*_errno = CC3000_EXPORT(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t cc3k_socket_send(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) {
|
|
|
|
if (cc3k_get_fd_closed_state(socket->u_state)) {
|
|
|
|
CC3000_EXPORT(closesocket)(socket->u_state);
|
2016-05-10 23:45:33 +01:00
|
|
|
*_errno = MP_EPIPE;
|
2014-11-29 13:48:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CC3K does not handle fragmentation, and will overflow,
|
|
|
|
// split the packet into smaller ones and send them out.
|
|
|
|
mp_int_t bytes = 0;
|
|
|
|
while (bytes < len) {
|
|
|
|
int n = MIN((len - bytes), MAX_TX_PACKET);
|
|
|
|
n = CC3000_EXPORT(send)(socket->u_state, (uint8_t *)buf + bytes, n, 0);
|
|
|
|
if (n <= 0) {
|
|
|
|
*_errno = CC3000_EXPORT(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bytes += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t cc3k_socket_recv(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno) {
|
|
|
|
// check the socket is open
|
|
|
|
if (cc3k_get_fd_closed_state(socket->u_state)) {
|
|
|
|
// socket is closed, but CC3000 may have some data remaining in buffer, so check
|
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(socket->u_state, &rfds);
|
2016-05-10 23:45:33 +01:00
|
|
|
cc3000_timeval tv;
|
2014-11-29 13:48:45 +00:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 1;
|
|
|
|
int nfds = CC3000_EXPORT(select)(socket->u_state + 1, &rfds, NULL, NULL, &tv);
|
|
|
|
if (nfds == -1 || !FD_ISSET(socket->u_state, &rfds)) {
|
|
|
|
// no data waiting, so close socket and return 0 data
|
|
|
|
CC3000_EXPORT(closesocket)(socket->u_state);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cap length at MAX_RX_PACKET
|
|
|
|
len = MIN(len, MAX_RX_PACKET);
|
|
|
|
|
|
|
|
// do the recv
|
|
|
|
int ret = CC3000_EXPORT(recv)(socket->u_state, buf, len, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
*_errno = CC3000_EXPORT(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t cc3k_socket_sendto(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno) {
|
|
|
|
MAKE_SOCKADDR(addr, ip, port)
|
|
|
|
int ret = CC3000_EXPORT(sendto)(socket->u_state, (byte *)buf, len, 0, (sockaddr *)&addr, sizeof(addr));
|
|
|
|
if (ret < 0) {
|
|
|
|
*_errno = CC3000_EXPORT(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t cc3k_socket_recvfrom(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) {
|
|
|
|
sockaddr addr;
|
|
|
|
socklen_t addr_len = sizeof(addr);
|
|
|
|
mp_int_t ret = CC3000_EXPORT(recvfrom)(socket->u_state, buf, len, 0, &addr, &addr_len);
|
|
|
|
if (ret < 0) {
|
|
|
|
*_errno = CC3000_EXPORT(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
UNPACK_SOCKADDR(addr, ip, *port);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int cc3k_socket_setsockopt(mod_network_socket_obj_t *socket, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno) {
|
|
|
|
int ret = CC3000_EXPORT(setsockopt)(socket->u_state, level, opt, optval, optlen);
|
|
|
|
if (ret < 0) {
|
|
|
|
*_errno = CC3000_EXPORT(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int cc3k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno) {
|
|
|
|
int ret;
|
|
|
|
if (timeout_ms == 0 || timeout_ms == -1) {
|
|
|
|
int optval;
|
|
|
|
socklen_t optlen = sizeof(optval);
|
|
|
|
if (timeout_ms == 0) {
|
|
|
|
// set non-blocking mode
|
|
|
|
optval = SOCK_ON;
|
|
|
|
} else {
|
|
|
|
// set blocking mode
|
|
|
|
optval = SOCK_OFF;
|
|
|
|
}
|
|
|
|
ret = CC3000_EXPORT(setsockopt)(socket->u_state, SOL_SOCKET, SOCKOPT_RECV_NONBLOCK, &optval, optlen);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = CC3000_EXPORT(setsockopt)(socket->u_state, SOL_SOCKET, SOCKOPT_ACCEPT_NONBLOCK, &optval, optlen);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// set timeout
|
|
|
|
socklen_t optlen = sizeof(timeout_ms);
|
|
|
|
ret = CC3000_EXPORT(setsockopt)(socket->u_state, SOL_SOCKET, SOCKOPT_RECV_TIMEOUT, &timeout_ms, optlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
*_errno = CC3000_EXPORT(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int cc3k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno) {
|
|
|
|
mp_uint_t ret;
|
2016-12-02 05:37:29 +00:00
|
|
|
if (request == MP_STREAM_POLL) {
|
2014-11-29 13:48:45 +00:00
|
|
|
mp_uint_t flags = arg;
|
|
|
|
ret = 0;
|
|
|
|
int fd = socket->u_state;
|
|
|
|
|
|
|
|
// init fds
|
|
|
|
fd_set rfds, wfds, xfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_ZERO(&xfds);
|
|
|
|
|
|
|
|
// set fds if needed
|
2016-12-02 05:37:29 +00:00
|
|
|
if (flags & MP_STREAM_POLL_RD) {
|
2014-11-29 13:48:45 +00:00
|
|
|
FD_SET(fd, &rfds);
|
|
|
|
|
|
|
|
// A socked that just closed is available for reading. A call to
|
|
|
|
// recv() returns 0 which is consistent with BSD.
|
|
|
|
if (cc3k_get_fd_closed_state(fd)) {
|
2016-12-02 05:37:29 +00:00
|
|
|
ret |= MP_STREAM_POLL_RD;
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-02 05:37:29 +00:00
|
|
|
if (flags & MP_STREAM_POLL_WR) {
|
2014-11-29 13:48:45 +00:00
|
|
|
FD_SET(fd, &wfds);
|
|
|
|
}
|
2016-12-02 05:37:29 +00:00
|
|
|
if (flags & MP_STREAM_POLL_HUP) {
|
2014-11-29 13:48:45 +00:00
|
|
|
FD_SET(fd, &xfds);
|
|
|
|
}
|
|
|
|
|
|
|
|
// call cc3000 select with minimum timeout
|
2016-05-10 23:45:33 +01:00
|
|
|
cc3000_timeval tv;
|
2014-11-29 13:48:45 +00:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 1;
|
|
|
|
int nfds = CC3000_EXPORT(select)(fd + 1, &rfds, &wfds, &xfds, &tv);
|
|
|
|
|
|
|
|
// check for error
|
|
|
|
if (nfds == -1) {
|
|
|
|
*_errno = CC3000_EXPORT(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check return of select
|
|
|
|
if (FD_ISSET(fd, &rfds)) {
|
2016-12-02 05:37:29 +00:00
|
|
|
ret |= MP_STREAM_POLL_RD;
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
if (FD_ISSET(fd, &wfds)) {
|
2016-12-02 05:37:29 +00:00
|
|
|
ret |= MP_STREAM_POLL_WR;
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
if (FD_ISSET(fd, &xfds)) {
|
2016-12-02 05:37:29 +00:00
|
|
|
ret |= MP_STREAM_POLL_HUP;
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-10 23:45:33 +01:00
|
|
|
*_errno = MP_EINVAL;
|
2014-11-29 13:48:45 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-30 08:22:17 +01:00
|
|
|
// MicroPython bindings; CC3K class
|
2014-11-29 13:48:45 +00:00
|
|
|
|
|
|
|
typedef struct _cc3k_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
} cc3k_obj_t;
|
|
|
|
|
2015-01-22 00:17:40 +00:00
|
|
|
STATIC const cc3k_obj_t cc3k_obj = {{(mp_obj_type_t *)&mod_network_nic_type_cc3k}};
|
|
|
|
|
2014-11-29 13:48:45 +00:00
|
|
|
// \classmethod \constructor(spi, pin_cs, pin_en, pin_irq)
|
|
|
|
// Initialise the CC3000 using the given SPI bus and pins and return a CC3K object.
|
|
|
|
//
|
|
|
|
// Note: pins were originally hard-coded to:
|
|
|
|
// PYBv1.0: init(pyb.SPI(2), pyb.Pin.board.Y5, pyb.Pin.board.Y4, pyb.Pin.board.Y3)
|
|
|
|
// [SPI on Y position; Y6=B13=SCK, Y7=B14=MISO, Y8=B15=MOSI]
|
|
|
|
//
|
|
|
|
// STM32F4DISC: init(pyb.SPI(2), pyb.Pin.cpu.A15, pyb.Pin.cpu.B10, pyb.Pin.cpu.B11)
|
2017-01-04 13:10:42 +00:00
|
|
|
STATIC mp_obj_t cc3k_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-11-29 13:48:45 +00:00
|
|
|
// check arguments
|
|
|
|
mp_arg_check_num(n_args, n_kw, 4, 4, false);
|
|
|
|
|
|
|
|
// set the pins to use
|
|
|
|
SpiInit(
|
2018-02-05 02:45:08 +00:00
|
|
|
spi_from_mp_obj(args[0])->spi,
|
2014-11-29 13:48:45 +00:00
|
|
|
pin_find(args[1]),
|
|
|
|
pin_find(args[2]),
|
|
|
|
pin_find(args[3])
|
|
|
|
);
|
|
|
|
|
|
|
|
// initialize and start the module
|
|
|
|
wlan_init(cc3k_callback, NULL, NULL, NULL,
|
|
|
|
ReadWlanInterruptPin, SpiResumeSpi, SpiPauseSpi, WriteWlanPin);
|
|
|
|
|
|
|
|
if (wlan_start(0) != 0) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("failed to init CC3000 module"));
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set connection policy. this should be called explicitly by the user
|
|
|
|
// wlan_ioctl_set_connection_policy(0, 0, 0);
|
|
|
|
|
|
|
|
// Mask out all non-required events from the CC3000
|
|
|
|
wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE |
|
|
|
|
HCI_EVNT_WLAN_UNSOL_INIT |
|
|
|
|
HCI_EVNT_WLAN_ASYNC_PING_REPORT |
|
|
|
|
HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE);
|
|
|
|
|
|
|
|
// register with network module
|
2015-01-22 00:17:40 +00:00
|
|
|
mod_network_register_nic((mp_obj_t)&cc3k_obj);
|
2014-11-29 13:48:45 +00:00
|
|
|
|
2015-01-22 00:17:40 +00:00
|
|
|
return (mp_obj_t)&cc3k_obj;
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// method connect(ssid, key=None, *, security=WPA2, bssid=None)
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t cc3k_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2014-11-29 13:48:45 +00:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_key, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_security, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = WLAN_SEC_WPA2} },
|
|
|
|
{ MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
};
|
|
|
|
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
// get ssid
|
2017-03-26 07:35:12 +01:00
|
|
|
size_t ssid_len;
|
2014-11-29 13:48:45 +00:00
|
|
|
const char *ssid = mp_obj_str_get_data(args[0].u_obj, &ssid_len);
|
|
|
|
|
|
|
|
// get key and sec
|
2017-03-26 07:35:12 +01:00
|
|
|
size_t key_len = 0;
|
2014-11-29 13:48:45 +00:00
|
|
|
const char *key = NULL;
|
|
|
|
mp_uint_t sec = WLAN_SEC_UNSEC;
|
|
|
|
if (args[1].u_obj != mp_const_none) {
|
|
|
|
key = mp_obj_str_get_data(args[1].u_obj, &key_len);
|
|
|
|
sec = args[2].u_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get bssid
|
|
|
|
const char *bssid = NULL;
|
|
|
|
if (args[3].u_obj != mp_const_none) {
|
|
|
|
bssid = mp_obj_str_get_str(args[3].u_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect to AP
|
|
|
|
if (wlan_connect(sec, (char *)ssid, ssid_len, (uint8_t *)bssid, (uint8_t *)key, key_len) != 0) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg_varg(&mp_type_OSError, MP_ERROR_TEXT("could not connect to ssid=%s, sec=%d, key=%s\n"), ssid, sec, key);
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(cc3k_connect_obj, 1, cc3k_connect);
|
|
|
|
|
|
|
|
STATIC mp_obj_t cc3k_disconnect(mp_obj_t self_in) {
|
|
|
|
// should we check return value?
|
|
|
|
wlan_disconnect();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(cc3k_disconnect_obj, cc3k_disconnect);
|
|
|
|
|
|
|
|
STATIC mp_obj_t cc3k_isconnected(mp_obj_t self_in) {
|
2015-10-11 10:09:43 +01:00
|
|
|
return mp_obj_new_bool(wlan_connected && ip_obtained);
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(cc3k_isconnected_obj, cc3k_isconnected);
|
|
|
|
|
|
|
|
STATIC mp_obj_t cc3k_ifconfig(mp_obj_t self_in) {
|
|
|
|
tNetappIpconfigRetArgs ipconfig;
|
|
|
|
netapp_ipconfig(&ipconfig);
|
|
|
|
|
|
|
|
// render MAC address
|
2015-04-18 14:28:39 +01:00
|
|
|
VSTR_FIXED(mac_vstr, 18);
|
2014-11-29 13:48:45 +00:00
|
|
|
const uint8_t *mac = ipconfig.uaMacAddr;
|
2015-04-18 14:28:39 +01:00
|
|
|
vstr_printf(&mac_vstr, "%02x:%02x:%02x:%02x:%02x:%02x", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
|
2014-11-29 13:48:45 +00:00
|
|
|
|
|
|
|
// create and return tuple with ifconfig info
|
|
|
|
mp_obj_t tuple[7] = {
|
2015-05-03 19:26:08 +01:00
|
|
|
netutils_format_ipv4_addr(ipconfig.aucIP, NETUTILS_LITTLE),
|
|
|
|
netutils_format_ipv4_addr(ipconfig.aucSubnetMask, NETUTILS_LITTLE),
|
|
|
|
netutils_format_ipv4_addr(ipconfig.aucDefaultGateway, NETUTILS_LITTLE),
|
|
|
|
netutils_format_ipv4_addr(ipconfig.aucDNSServer, NETUTILS_LITTLE),
|
|
|
|
netutils_format_ipv4_addr(ipconfig.aucDHCPServer, NETUTILS_LITTLE),
|
2017-11-16 02:17:51 +00:00
|
|
|
mp_obj_new_str(mac_vstr.buf, mac_vstr.len),
|
|
|
|
mp_obj_new_str((const char *)ipconfig.uaSSID, strlen((const char *)ipconfig.uaSSID)),
|
2014-11-29 13:48:45 +00:00
|
|
|
};
|
|
|
|
return mp_obj_new_tuple(MP_ARRAY_SIZE(tuple), tuple);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(cc3k_ifconfig_obj, cc3k_ifconfig);
|
|
|
|
|
|
|
|
STATIC mp_obj_t cc3k_patch_version(mp_obj_t self_in) {
|
|
|
|
uint8_t pver[2];
|
|
|
|
mp_obj_tuple_t *t_pver;
|
|
|
|
|
|
|
|
nvmem_read_sp_version(pver);
|
|
|
|
t_pver = mp_obj_new_tuple(2, NULL);
|
|
|
|
t_pver->items[0] = mp_obj_new_int(pver[0]);
|
|
|
|
t_pver->items[1] = mp_obj_new_int(pver[1]);
|
|
|
|
return t_pver;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(cc3k_patch_version_obj, cc3k_patch_version);
|
|
|
|
|
|
|
|
STATIC mp_obj_t cc3k_patch_program(mp_obj_t self_in, mp_obj_t key_in) {
|
|
|
|
const char *key = mp_obj_str_get_str(key_in);
|
|
|
|
if (key[0] == 'p' && key[1] == 'g' && key[2] == 'm' && key[3] == '\0') {
|
|
|
|
patch_prog_start();
|
|
|
|
} else {
|
2015-04-18 14:28:39 +01:00
|
|
|
mp_print_str(&mp_plat_print, "pass 'pgm' as argument in order to program\n");
|
2014-11-29 13:48:45 +00:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(cc3k_patch_program_obj, cc3k_patch_program);
|
|
|
|
|
2017-05-06 08:03:40 +01:00
|
|
|
STATIC const mp_rom_map_elem_t cc3k_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&cc3k_connect_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&cc3k_disconnect_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&cc3k_isconnected_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&cc3k_ifconfig_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_patch_version), MP_ROM_PTR(&cc3k_patch_version_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_patch_program), MP_ROM_PTR(&cc3k_patch_program_obj) },
|
2014-11-29 13:48:45 +00:00
|
|
|
|
|
|
|
// class constants
|
2017-05-06 08:03:40 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_WEP), MP_ROM_INT(WLAN_SEC_WEP) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_WPA), MP_ROM_INT(WLAN_SEC_WPA) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_WPA2), MP_ROM_INT(WLAN_SEC_WPA2) },
|
2014-11-29 13:48:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(cc3k_locals_dict, cc3k_locals_dict_table);
|
|
|
|
|
|
|
|
const mod_network_nic_type_t mod_network_nic_type_cc3k = {
|
|
|
|
.base = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_CC3K,
|
|
|
|
.make_new = cc3k_make_new,
|
2017-05-06 08:03:40 +01:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&cc3k_locals_dict,
|
2014-11-29 13:48:45 +00:00
|
|
|
},
|
|
|
|
.gethostbyname = cc3k_gethostbyname,
|
|
|
|
.socket = cc3k_socket_socket,
|
|
|
|
.close = cc3k_socket_close,
|
|
|
|
.bind = cc3k_socket_bind,
|
|
|
|
.listen = cc3k_socket_listen,
|
|
|
|
.accept = cc3k_socket_accept,
|
|
|
|
.connect = cc3k_socket_connect,
|
|
|
|
.send = cc3k_socket_send,
|
|
|
|
.recv = cc3k_socket_recv,
|
|
|
|
.sendto = cc3k_socket_sendto,
|
|
|
|
.recvfrom = cc3k_socket_recvfrom,
|
|
|
|
.setsockopt = cc3k_socket_setsockopt,
|
|
|
|
.settimeout = cc3k_socket_settimeout,
|
|
|
|
.ioctl = cc3k_socket_ioctl,
|
|
|
|
};
|