Import required parts of ameba sdk 4.0b

This commit is contained in:
David Goodlad 2019-04-23 21:10:24 +10:00
parent 2d21e45bba
commit 7319ca1482
737 changed files with 304718 additions and 0 deletions

View file

@ -0,0 +1,110 @@
#ifndef EASYWSCLIENT_H
#define EASYWSCLIENT_H
#include <platform/platform_stdlib.h>
#define WSCLIENT_TLS_POLARSSL 0 /*!< Use PolarSSL for TLS when WSCLIENT */
#define WSCLIENT_TLS_MBEDTLS 1 /*!< Use mbedTLS for TLS when WSCLIENT */
#if CONFIG_USE_POLARSSL
#define WSCLIENT_USE_TLS WSCLIENT_TLS_POLARSSL
#elif CONFIG_USE_MBEDTLS
#define WSCLIENT_USE_TLS WSCLIENT_TLS_MBEDTLS
#endif
/****************Define the debug message level*********************/
#define DEBUG_WSCLIENT 1
#define WSCLIENT_LOG(level, fmt, ...) printf("\n\r[WSCLIENT %s] %s: " fmt "\n", level, __FUNCTION__, ##__VA_ARGS__)
#if DEBUG_WSCLIENT == 2
#define WSCLIENT_DEBUG(fmt, ...) WSCLIENT_LOG("DEBUG", fmt, ##__VA_ARGS__)
#else
#define WSCLIENT_DEBUG(fmt, ...)
#endif
#if DEBUG_WSCLIENT
#define WSCLIENT_ERROR(fmt, ...) WSCLIENT_LOG("ERROR", fmt, ##__VA_ARGS__)
#else
#define WSCLIENT_ERROR(fmt, ...)
#endif
/*******************************************************************/
/****************Define the structures used*************************/
typedef enum{
CLOSING,
CLOSED,
CONNECTING,
OPEN
} readyStateValues;
struct wsheader_type{
unsigned header_size;
int fin;
int mask;
enum opcode_type {
CONTINUATION = 0x0,
TEXT_FRAME = 0x1,
BINARY_FRAME = 0x2,
CLOSE = 8,
PING = 9,
PONG = 0xa,
} opcode;
int N0;
uint64_t N;
uint8_t masking_key[4];
};
struct _wsclient_context;
struct ws_fun_ops{
int (*hostname_connect)(struct _wsclient_context *wsclient);
void (*client_close)(struct _wsclient_context *wsclient);
int (*client_send)(struct _wsclient_context *wsclient, unsigned char *data, size_t data_len);
int (*client_read)(struct _wsclient_context *wsclient, unsigned char *data, size_t data_len);
};
typedef struct _wsclient_context{
char host[128];
char path[128];
char origin[200];
int port;
uint8_t use_ssl;
int sockfd;
readyStateValues readyState;
int tx_len;
void *tls;
uint8_t *txbuf;
uint8_t *rxbuf;
uint8_t *receivedData;
struct ws_fun_ops fun_ops;
}wsclient_context;
/*******************************************************************/
/****************General functions used by wsclient*****************/
void ws_get_random_bytes(void *buf, size_t len);
void* ws_malloc(unsigned int size);
void ws_free(void *buf);
int ws_client_handshake(wsclient_context *wsclient);
int ws_check_handshake(wsclient_context *wsclient);
void ws_sendData(uint8_t type, size_t message_size, uint8_t* message, int useMask, wsclient_context *wsclient);
/*******************************************************************/
/*************Functions used by wsclient without SSL****************/
int ws_hostname_connect(wsclient_context *wsclient);
int ws_client_read(wsclient_context *wsclient, unsigned char *data, size_t data_len);
int ws_client_send(wsclient_context *wsclient, unsigned char *data, size_t data_len);
void ws_client_close(wsclient_context *wsclient);
/*******************************************************************/
/***************Functions used by wsclient with SSL*****************/
int wss_hostname_connect(wsclient_context *wsclient);
int wss_client_read(wsclient_context *wsclient, unsigned char *data, size_t data_len);
int wss_client_send(wsclient_context *wsclient, unsigned char *data, size_t data_len);
void wss_client_close(wsclient_context *wsclient);
void *wss_tls_connect(int *sock , char *host, int port);
int wss_tls_handshake(void *tls_in);
void wss_tls_close(void *tls_in,int *sock);
int wss_tls_write(void *tls_in, char *request, int request_len);
int wss_tls_read(void *tls_in, char *buffer, int buf_len);
/*******************************************************************/
#endif

View file

@ -0,0 +1,103 @@
#ifndef WSCLIENT_H
#define WSCLIENT_H
#include <websocket/libwsclient.h>
/******Define the maximum bytes of data send and receive********/
#define MAX_DATA_LEN 1500
/****************Define if using the polarssl*******************/
#define USING_SSL
/******************Define the function used*********************/
#ifdef USING_SSL
int wss_set_fun_ops(wsclient_context *wsclient);
#define wsclient_set_fun_ops(wsclient) wss_set_fun_ops(wsclient)
#else
int ws_set_fun_ops(wsclient_context *wsclient);
#define wsclient_set_fun_ops(wsclient) ws_set_fun_ops(wsclient)
#endif
/***************************************************************/
/*************************************************************************************************
** Function Name : create_wsclient
** Description : Creating the websocket client context structure
** Input : url:websocket server's url
** port:websocket server's port, if not given, default 80 for "ws", 443 for "wss"
** origin: the address or url of your self
** Return : Created: websocket client context structure
** Failed: NULL
**************************************************************************************************/
wsclient_context *create_wsclient(char *url, int port,char *path, char* origin);
/*************************************************************************************************
** Function Name : ws_connect_url
** Description : Connecting to the websocket server
** Input : wsclient: the websocket client context created by create_wsclientfunction
** Return : Connected: the socket value
** Failed: -1
**************************************************************************************************/
int ws_connect_url(wsclient_context *wsclient);
/*************************************************************************************************
** Function Name : ws_send
** Description : Create the sending string data and copy to tx_buf
** Input : message: the string that send to server(cannot exceeding the MAX_DATA_LEN
** message_len: the length of the string
** use_mask: 0/1; 1 means using mask for bynary
** wsclient: the websocket client context
** Return : None
**************************************************************************************************/
void ws_send(char* message, int message_len, int use_mask, wsclient_context *wsclient);
/*************************************************************************************************
** Function Name : ws_sendBinary
** Description : Create the sending binary data and copy to tx_buf
** Input : message: the binary that send to server(cannot exceeding the MAX_DATA_LEN
** message_len: the length of the binary
** use_mask: 0/1; 1 means using mask for bynary
** wsclient: the websocket client context
** Return : None
**************************************************************************************************/
void ws_sendBinary(uint8_t* message, int message_len, int use_mask, wsclient_context *wsclient);
/*************************************************************************************************
** Function Name : ws_sendPing
** Description : Sending Ping to websocket server
** Input : wsclient: the websocket client context
** Return : None
**************************************************************************************************/
void ws_sendPing(wsclient_context *wsclient);
/*************************************************************************************************
** Function Name : ws_poll
** Description : Receicing data from server and send the data in tx_buf
** Input : timeout(in milliseconds)
wsclient: the websocket client context
** Return : None
**************************************************************************************************/
void ws_poll(int timeout, wsclient_context *wsclient);
/*************************************************************************************************
** Function Name : ws_dispatch
** Description : callback function when getting message from server
** Input : function that resolve the message received and the message length
** Return : None
**************************************************************************************************/
void ws_dispatch(void (*callback)(wsclient_context *, int)) ;
/*************************************************************************************************
** Function Name : ws_getReadyState
** Description : Getting the connection status
** Input : wsclient: the websocket client context
** Return : readyStateValues(4 types:CLOSING, CLOSED, CONNECTING, OPEN)
**************************************************************************************************/
readyStateValues ws_getReadyState(wsclient_context *wsclient);
/*************************************************************************************************
** Function Name : ws_close
** Description : Closing the connection with websocket server
** Input : wsclient: the websocket client context
** Return : None
**************************************************************************************************/
void ws_close(wsclient_context *wsclient);
#endif

View file

@ -0,0 +1,245 @@
#include "platform_opts.h"
#include <websocket/libwsclient.h>
#if (WSCLIENT_USE_TLS == WSCLIENT_TLS_POLARSSL)
#include "polarssl/net.h"
#include "polarssl/ssl.h"
#include <polarssl/memory.h>
struct wss_tls{
ssl_context ctx;
};
#elif (WSCLIENT_USE_TLS == WSCLIENT_TLS_MBEDTLS)
#include "mbedTLS/ssl.h"
#include "mbedtls/net_sockets.h"
struct wss_tls{
mbedtls_ssl_context ctx;
mbedtls_ssl_config conf;
mbedtls_net_context socket;
};
static void* my_calloc(size_t nelements, size_t elementSize){
size_t size;
void *ptr = NULL;
size = nelements * elementSize;
ptr = pvPortMalloc(size);
if(ptr)
memset(ptr, 0, size);
return ptr;
}
static char *ws_itoa(int value){
char *val_str;
int tmp = value, len = 1;
while((tmp /= 10) > 0)
len ++;
val_str = (char *) pvPortMalloc(len + 1);
sprintf(val_str, "%d", value);
return val_str;
}
#endif /* WSCLIENT_USE_TLS */
int ws_random(void *p_rng, unsigned char *output, size_t output_len);
void *wss_tls_connect(int *sock , char *host, int port){
#if (WSCLIENT_USE_TLS == WSCLIENT_TLS_POLARSSL)
int ret;
struct wss_tls *tls =NULL;
memory_set_own(pvPortMalloc, vPortFree);
tls = (struct wss_tls *) malloc(sizeof(struct wss_tls));
if(tls){
ssl_context *ssl = &tls->ctx;
memset(tls, 0, sizeof(struct wss_tls));
if((ret = net_connect(sock, host, port)) != 0){
printf("\n[WSCLIENT] ERROR: net_connect %d\n", ret);
goto exit;
}
if((ret = ssl_init(ssl)) != 0){
printf("\n[WSCLIENT] ERROR: ssl_init %d\n", ret);
goto exit;
}
ssl_set_endpoint(ssl, 0);
ssl_set_authmode(ssl, 0);
ssl_set_rng(ssl, ws_random, NULL);
ssl_set_bio(ssl, net_recv, sock, net_send, sock);
}
else{
printf("\n[WSCLIENT] ERROR: malloc\n");
ret = -1;
goto exit;
}
exit:
if(ret && tls) {
net_close(*sock);
ssl_free(&tls->ctx);
free(tls);
tls = NULL;
}
return (void *) tls;
#elif (WSCLIENT_USE_TLS == WSCLIENT_TLS_MBEDTLS)
int ret;
struct wss_tls *tls =NULL;
mbedtls_platform_set_calloc_free(my_calloc, vPortFree);
tls = (struct wss_tls *) malloc(sizeof(struct wss_tls));
if(tls){
mbedtls_ssl_context *ssl = &tls->ctx;
mbedtls_ssl_config *conf = &tls->conf;
mbedtls_net_context *server_fd = &tls->socket;
memset(tls, 0, sizeof(struct wss_tls));
server_fd->fd = *sock;
char *port_str = ws_itoa (port);
if((ret = mbedtls_net_connect(server_fd, host, port_str, MBEDTLS_NET_PROTO_TCP)) != 0){
printf("\n[WSCLIENT] ERROR: net_connect %d\n", ret);
goto exit;
}
free(port_str);
*sock = server_fd->fd;
mbedtls_ssl_init(ssl);
mbedtls_ssl_config_init(conf);
mbedtls_ssl_set_bio(ssl, server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
if((ret = mbedtls_ssl_config_defaults(conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
printf("\n[WSCLIENT] ERROR: ssl_config %d\n", ret);
goto exit;
}
mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
mbedtls_ssl_conf_rng(conf, ws_random, NULL);
if((ret = mbedtls_ssl_setup(ssl, conf)) != 0) {
printf("\n[WSCLIENT] ERROR: ssl_setup %d\n", ret);
goto exit;
}
}
else{
printf("\n[WSCLIENT] ERROR: malloc\n");
ret = -1;
goto exit;
}
exit:
if(ret && tls){
mbedtls_net_free(&tls->socket);
mbedtls_ssl_free(&tls->ctx);
mbedtls_ssl_config_free(&tls->conf);
free(tls);
tls = NULL;
}
return (void *) tls;
#endif /* WSCLIENT_USE_TLS */
}
int wss_tls_handshake(void *tls_in){
struct wss_tls *tls = (struct wss_tls *) tls_in;
#if (WSCLIENT_USE_TLS == WSCLIENT_TLS_POLARSSL)
int ret;
if((ret = ssl_handshake(&tls->ctx)) != 0) {
printf("\n[WSCLIENT] ERROR: ssl_handshake %d\n", ret);
ret = -1;
}
else {
printf("\n[WSCLIENT] Use ciphersuite %s\n", ssl_get_ciphersuite(&tls->ctx));
}
return ret;
#elif (WSCLIENT_USE_TLS == WSCLIENT_TLS_MBEDTLS)
int ret;
if((ret = mbedtls_ssl_handshake(&tls->ctx)) != 0) {
printf("\n[WSCLIENT] ERROR: ssl_handshake -0x%x\n", -ret);
ret = -1;
}
else {
printf("\n[WSCLIENT] Use ciphersuite %s\n", mbedtls_ssl_get_ciphersuite(&tls->ctx));
}
return ret;
#endif /* WSCLIENT_USE_TLS */
}
void wss_tls_close(void *tls_in,int *sock){
struct wss_tls *tls = (struct wss_tls *) tls_in;
#if (WSCLIENT_USE_TLS == WSCLIENT_TLS_POLARSSL)
if(tls)
ssl_close_notify(&tls->ctx);
if(*sock != -1){
net_close(*sock);
*sock = -1;
}
ssl_free(&tls->ctx);
free(tls);
tls = NULL;
#elif (WSCLIENT_USE_TLS == WSCLIENT_TLS_MBEDTLS)
if(tls)
mbedtls_ssl_close_notify(&tls->ctx);
if(*sock != -1){
mbedtls_net_free(&tls->socket);
*sock = -1;
}
mbedtls_ssl_free(&tls->ctx);
mbedtls_ssl_config_free(&tls->conf);
free(tls);
tls = NULL;
#endif /* WSCLIENT_USE_TLS */
}
int wss_tls_write(void *tls_in, char *request, int request_len){
int ret;
struct wss_tls *tls = (struct wss_tls *) tls_in;
#if (WSCLIENT_USE_TLS == WSCLIENT_TLS_POLARSSL)
ret = ssl_write(&tls->ctx, request, request_len);
if(ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE)
ret = 0;
#elif (WSCLIENT_USE_TLS == WSCLIENT_TLS_MBEDTLS)
ret = mbedtls_ssl_write(&tls->ctx, request, request_len);
if(ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)
ret = 0;
#endif /* WSCLIENT_USE_TLS */
return ret;
}
int wss_tls_read(void *tls_in, char *buffer, int buf_len){
int ret;
struct wss_tls *tls = (struct wss_tls *) tls_in;
#if (WSCLIENT_USE_TLS == WSCLIENT_TLS_POLARSSL)
ret = ssl_read(&tls->ctx, buffer, buf_len);
if(ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE
|| ret == POLARSSL_ERR_NET_RECV_FAILED)
ret =0;
#elif (WSCLIENT_USE_TLS == WSCLIENT_TLS_MBEDTLS)
ret = mbedtls_ssl_read(&tls->ctx, buffer, buf_len);
if(ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE
|| ret == MBEDTLS_ERR_NET_RECV_FAILED)
ret =0;
#endif /* WSCLIENT_USE_TLS */
return ret;
}