diff --git a/arduino/realtek-ambz/cores/arduino/ard_socket.c b/arduino/realtek-ambz/cores/arduino/ard_socket.c deleted file mode 100644 index a6519e2..0000000 --- a/arduino/realtek-ambz/cores/arduino/ard_socket.c +++ /dev/null @@ -1,214 +0,0 @@ -#include -#include -#include -#include -#include "ard_socket.h" - -int start_server(uint16_t port, uint8_t protMode) -{ - int _sock; - int timeout; - - if(protMode == 0) { - timeout = 3000; - _sock = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - lwip_setsockopt(_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - } else { - timeout = 1000; - _sock = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - lwip_setsockopt(_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - } - - if (_sock < 0) { - printf("\r\nERROR opening socket\r\n"); - return -1; - } - - struct sockaddr_in localHost; - memset(&localHost, 0, sizeof(localHost)); - - localHost.sin_family = AF_INET; - localHost.sin_port = htons(port); - localHost.sin_addr.s_addr = INADDR_ANY; - - if (lwip_bind(_sock, (struct sockaddr *)&localHost, sizeof(localHost)) < 0) { - printf("\r\nERROR on binding\r\n"); - return -1; - } - - return _sock; -} - -int sock_listen(int sock, int max) -{ - if(lwip_listen(sock , max) < 0){ - printf("\r\nERROR on listening\r\n"); - return -1; - } - return 0; -} - -int get_available(int sock) -{ - int enable = 1; - int timeout; - int client_fd; - int err; - struct sockaddr_in cli_addr; - - socklen_t client = sizeof(cli_addr); - - do { - client_fd = lwip_accept(sock, (struct sockaddr *) &cli_addr, &client); - if (client_fd < 0) { - err = get_sock_errno(sock); - if (err != EAGAIN) { - break; - } - } - } while (client_fd < 0); - - if(client_fd < 0){ - printf("\r\nERROR on accept\r\n"); - return -1; - } - else { - timeout = 3000; - lwip_setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - timeout = 30000; - lwip_setsockopt(client_fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); - lwip_setsockopt(client_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); - lwip_setsockopt(client_fd, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)); - printf("\r\nA client connected to this server :\r\n[PORT]: %d\r\n[IP]:%s\r\n\r\n", ntohs(cli_addr.sin_port), inet_ntoa(cli_addr.sin_addr.s_addr)); - return client_fd; - } -} - -int get_receive(int sock, uint8_t* data, int length, int flag, uint32_t *peer_addr, uint16_t *peer_port) -{ - int ret = 0; - struct sockaddr from; - socklen_t fromlen; - - uint8_t backup_recvtimeout = 0; - int backup_recv_timeout, recv_timeout, len; - - if (flag & 0x01) { - // for MSG_PEEK, we try to peek packets by changing receiving timeout to 10ms - ret = lwip_getsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &backup_recv_timeout, &len); - if (ret >= 0) { - recv_timeout = 10; - ret = lwip_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout)); - if (ret >= 0) { - backup_recvtimeout = 1; - } - } - } - - ret = lwip_recvfrom(sock, data, length, flag, &from, &fromlen); - if ( ret >= 0 ) { - if (peer_addr != NULL) { - *peer_addr = ((struct sockaddr_in *)&from)->sin_addr.s_addr; - } - if (peer_port != NULL) { - *peer_port = ntohs(((struct sockaddr_in *)&from)->sin_port); - } - } - - if ((flag & 0x01) && (backup_recvtimeout == 1)) { - // restore receiving timeout - lwip_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &backup_recv_timeout, sizeof(recv_timeout)); - } - - return ret; -} - -int get_sock_errno(int sock) -{ - int so_error; - socklen_t len = sizeof(so_error); - getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len); - return so_error; -} - -int set_sock_recv_timeout(int sock, int timeout) -{ - return lwip_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); -} - -void stop_socket(int sock) -{ - lwip_close(sock); -} - -int send_data(int sock, const uint8_t *data, uint16_t len) -{ - int ret; - - ret = lwip_write(sock, data, len); - - return ret; -} - -int sendto_data(int sock, const uint8_t *data, uint16_t len, uint32_t peer_ip, uint16_t peer_port) -{ - int ret; - - struct sockaddr_in peer_addr; - memset(&peer_addr, 0, sizeof(peer_addr)); - peer_addr.sin_family = AF_INET; - peer_addr.sin_addr.s_addr = peer_ip; - peer_addr.sin_port = htons(peer_port); - - ret = lwip_sendto(sock, data, len, 0, (struct sockaddr*)&peer_addr, sizeof(struct sockaddr_in)); - - return ret; -} - -int start_client(uint32_t ipAddress, uint16_t port, uint8_t protMode) -{ - int enable = 1; - int timeout; - int _sock; - - if(protMode == 0)//tcp - _sock = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - else - _sock = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - - if (_sock < 0) { - printf("\r\nERROR opening socket\r\n"); - return -1; - } - - struct sockaddr_in serv_addr; - memset(&serv_addr, 0, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; - serv_addr.sin_addr.s_addr = ipAddress; - serv_addr.sin_port = htons(port); - - if (protMode == 0){//TCP MODE - if(connect(_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == 0){ - printf("\r\nConnect to Server successful!\r\n"); - - timeout = 3000; - lwip_setsockopt(_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - timeout = 30000; - lwip_setsockopt(_sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); - lwip_setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); - lwip_setsockopt(_sock, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)); - - return _sock; - } - else{ - printf("\r\nConnect to Server failed!\r\n"); - stop_socket(_sock); - return -1; - } - } - else { - //printf("\r\nUdp client setup Server's information successful!\r\n"); - } - - return _sock; -} diff --git a/arduino/realtek-ambz/cores/arduino/ard_socket.h b/arduino/realtek-ambz/cores/arduino/ard_socket.h deleted file mode 100644 index 620538a..0000000 --- a/arduino/realtek-ambz/cores/arduino/ard_socket.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef ARD_SOCKET_H -#define ARD_SOCKET_H -#include "main.h" - -int start_server(uint16_t port, uint8_t protMode); - -int sock_listen(int sock, int max); - -int get_available(int sock); - -int get_receive(int sock, uint8_t* data, int length, int flag, uint32_t *peer_addr, uint16_t *peer_port); - -int get_sock_errno(int sock); - -int set_sock_recv_timeout(int sock, int timeout); - -void stop_socket(int sock); - -int send_data(int sock, const uint8_t *data, uint16_t len); - -int sendto_data(int sock, const uint8_t *data, uint16_t len, uint32_t peer_ip, uint16_t peer_port); - -int start_client(uint32_t ipAddress, uint16_t port, uint8_t protMode); - -#endif diff --git a/arduino/realtek-ambz/cores/arduino/ard_ssl.c b/arduino/realtek-ambz/cores/arduino/ard_ssl.c deleted file mode 100644 index e95dfb9..0000000 --- a/arduino/realtek-ambz/cores/arduino/ard_ssl.c +++ /dev/null @@ -1,402 +0,0 @@ -#include "Arduino.h" -#include -#include -#include -#include -#include -#include -#include "ard_ssl.h" - -#define ARDUINO_MBEDTLS_DEBUG_LEVEL 0 // Set to 0 to disable debug messsages, 5 to enable all debug messages - -static unsigned int ard_ssl_arc4random(void) -{ - unsigned int res = xTaskGetTickCount(); - static unsigned int seed = 0xDEADB00B; - - seed = ((seed & 0x007F00FF) << 7) ^ - ((seed & 0x0F80FF00) >> 8) ^ // be sure to stir those low bits - (res << 13) ^ (res >> 9); // using the clock too! - - return seed; -} - -static void get_random_bytes(void *buf, size_t len) -{ - unsigned int ranbuf; - unsigned int *lp; - int i, count; - count = len / sizeof(unsigned int); - lp = (unsigned int *) buf; - - for (i = 0; i < count; i ++) { - lp[i] = ard_ssl_arc4random(); - len -= sizeof(unsigned int); - } - - if (len > 0) { - ranbuf = ard_ssl_arc4random(); - memcpy(&lp[i], &ranbuf, len); - } -} - -static int my_random(void *p_rng, unsigned char *output, size_t output_len) -{ - p_rng = p_rng; - get_random_bytes(output, output_len); - return 0; -} - -static int my_verify(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags) -{ - char buf[1024]; - ((void)data); - - mbedtls_x509_crt_info(buf, (sizeof(buf) - 1), "", crt); - - if(ARDUINO_MBEDTLS_DEBUG_LEVEL < 3) - return(0); - - printf( "\nVerify requested for (Depth %d):\n", depth ); - printf( "%s", buf ); - - if ((*flags) == 0) - printf(" This certificate has no flags\n"); - else - { - mbedtls_x509_crt_verify_info(buf, sizeof( buf ), " ! ", *flags); - printf("%s\n", buf); - } - - return(0); -} - -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 void my_debug(void *ctx, int level, const char *file, int line, const char *str ) -{ - const char *p, *basename; - - ctx = ctx; // Remove unused parameter warning - // Extract basename from file - for( p = basename = file; *p != '\0'; p++ ) - if( *p == '/' || *p == '\\' ) - basename = p + 1; - - printf("%s:%04d: |%d| %s", basename, line, level, str ); -} - -int start_ssl_client(sslclient_context *ssl_client, uint32_t ipAddress, uint32_t port, unsigned char* rootCABuff, unsigned char* cli_cert, unsigned char* cli_key, unsigned char* pskIdent, unsigned char* psKey, char* SNI_hostname) -{ - int ret = 0; - //int timeout; - int enable = 1; - int keep_idle = 30; - mbedtls_x509_crt* cacert = NULL; - mbedtls_x509_crt* _cli_crt = NULL; - mbedtls_pk_context* _clikey_rsa = NULL; - - do { - ssl_client->socket = -1; - ssl_client->socket = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (ssl_client->socket < 0) { - printf("ERROR: opening socket failed! \r\n"); - ret = -1; - break; - } - - struct sockaddr_in serv_addr; - memset(&serv_addr, 0, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; - serv_addr.sin_addr.s_addr = ipAddress; - serv_addr.sin_port = htons(port); - - lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)); - lwip_setsockopt(ssl_client->socket, IPPROTO_TCP, TCP_KEEPIDLE, &keep_idle, sizeof(keep_idle)); - if (lwip_connect(ssl_client->socket, ((struct sockaddr *)&serv_addr), sizeof(serv_addr)) < 0) { - lwip_close(ssl_client->socket); - printf("ERROR: Connect to Server failed! \r\n"); - ret = -1; - break; - } else { - /*/ - if (lwip_connect(ssl_client->socket, ((struct sockaddr *)&serv_addr), sizeof(serv_addr)) == 0) { - timeout = ssl_client->recvTimeout; - lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - timeout = 30000; - lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); - lwip_setsockopt(ssl_client->socket, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); - lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)); - } else { - printf("ERROR: Connect to Server failed!\r\n"); - ret = -1; - break; - }//*/ - - mbedtls_platform_set_calloc_free(my_calloc,vPortFree); - - ssl_client->ssl = (mbedtls_ssl_context *)malloc(sizeof(mbedtls_ssl_context)); - ssl_client->conf = (mbedtls_ssl_config *)malloc(sizeof(mbedtls_ssl_config)); - if (( ssl_client->ssl == NULL )||( ssl_client->conf == NULL )) { - printf("ERROR: malloc ssl failed! \r\n"); - ret = -1; - break; - } - - mbedtls_ssl_init(ssl_client->ssl); - mbedtls_ssl_config_init(ssl_client->conf); - - if (ARDUINO_MBEDTLS_DEBUG_LEVEL > 0) { - mbedtls_ssl_conf_verify(ssl_client->conf, my_verify, NULL); - mbedtls_ssl_conf_dbg(ssl_client->conf, my_debug, NULL); - mbedtls_debug_set_threshold(ARDUINO_MBEDTLS_DEBUG_LEVEL); - } - - if((mbedtls_ssl_config_defaults(ssl_client->conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { - printf("ERROR: mbedtls ssl config defaults failed! \r\n"); - ret = -1; - break; - } - - mbedtls_ssl_conf_rng(ssl_client->conf, my_random, NULL); - - if (rootCABuff != NULL) { - // Configure mbedTLS to use certificate authentication method - cacert = (mbedtls_x509_crt *) mbedtls_calloc( sizeof(mbedtls_x509_crt), 1); - mbedtls_x509_crt_init(cacert); - if (mbedtls_x509_crt_parse(cacert, rootCABuff, (strlen((char*)rootCABuff)) + 1) != 0) { - printf("ERROR: mbedtls x509 crt parse failed! \r\n"); - ret = -1; - break; - } - mbedtls_ssl_conf_ca_chain(ssl_client->conf, cacert, NULL); - mbedtls_ssl_conf_authmode(ssl_client->conf, MBEDTLS_SSL_VERIFY_REQUIRED); - } else if (pskIdent != NULL && psKey != NULL) { - // Configure mbedTLS to use PSK authentication method - // Check for max length and even number of chars - uint16_t pskey_char_len = strlen((char*)psKey); - if ( ((pskey_char_len % 2) != 0) || (pskey_char_len > 2*MBEDTLS_PSK_MAX_LEN) ) { - printf("ERROR: TLS PSK not in valid hex format or too long \n"); - return -1; - } - uint16_t psk_len = pskey_char_len/2; - unsigned char psk[MBEDTLS_PSK_MAX_LEN]; - // Convert PSK from hexadecimal chars to binary - int i; - for (i = 0; i < pskey_char_len; i = i + 2) { - char c = psKey[i]; - // Convert first 4 bits - if (c >= '0' && c <= '9') { - c = c - '0'; - } else if (c >= 'A' && c <= 'F') { - c = c - 'A' + 10; - } else if (c >= 'a' && c <= 'f') { - c = c - 'a' + 10; - } else { - printf("ERROR: TLS PSK not in valid hex format \n"); - return -1; - } - psk[i/2] = c << 4; - c = psKey[i+1]; - // Convert next 4 bits - if (c >= '0' && c <= '9') { - c = c - '0'; - } else if (c >= 'A' && c <= 'F') { - c = c - 'A' + 10; - } else if (c >= 'a' && c <= 'f') { - c = c - 'a' + 10; - } else { - printf("ERROR: TLS PSK not in valid hex format \r\n"); - return -1; - } - psk[i/2] |= c; - } - if (mbedtls_ssl_conf_psk(ssl_client->conf, psk, psk_len, pskIdent, strlen((char*)pskIdent)) != 0) { - printf("ERROR: mbedtls conf psk failed! \r\n"); - } - } else { - mbedtls_ssl_conf_authmode(ssl_client->conf, MBEDTLS_SSL_VERIFY_NONE); - } - - if ((cli_cert != NULL) && (cli_key != NULL)) { - _cli_crt = (mbedtls_x509_crt *) mbedtls_calloc( sizeof(mbedtls_x509_crt), 1); - if (_cli_crt == NULL) { - printf("ERROR: malloc client_crt failed! \r\n"); - ret = -1; - break; - } - mbedtls_x509_crt_init(_cli_crt); - - _clikey_rsa = (mbedtls_pk_context *) mbedtls_calloc( sizeof(mbedtls_pk_context), 1); - if (_clikey_rsa == NULL) { - printf("ERROR: malloc client_rsa failed! \r\n"); - ret = -1; - break; - } - mbedtls_pk_init(_clikey_rsa); - - if (mbedtls_x509_crt_parse(_cli_crt, cli_cert, strlen((char*)cli_cert)+1) != 0) { - printf("ERROR: mbedtls x509 parse client_crt failed! \r\n"); - ret = -1; - break; - } - - if (mbedtls_pk_parse_key(_clikey_rsa, cli_key, strlen((char*)cli_key)+1, NULL, 0) != 0) { - printf("ERROR: mbedtls x509 parse client_rsa failed! \r\n"); - ret = -1; - break; - } - mbedtls_ssl_conf_own_cert(ssl_client->conf, _cli_crt, _clikey_rsa); - } - - if((mbedtls_ssl_setup(ssl_client->ssl, ssl_client->conf)) != 0) { - printf("ERROR: mbedtls ssl setup failed!\r\n"); - ret = -1; - break; - } - mbedtls_ssl_set_bio(ssl_client->ssl, &ssl_client->socket, mbedtls_net_send, mbedtls_net_recv, NULL); - - mbedtls_ssl_set_hostname(ssl_client->ssl, SNI_hostname); - - ret = mbedtls_ssl_handshake(ssl_client->ssl); - if (ret < 0) { - printf("ERROR: mbedtls ssl handshake failed: -0x%04X \r\n", -ret); - ret = -1; - } else { - if (ARDUINO_MBEDTLS_DEBUG_LEVEL > 0) { - printf("mbedTLS SSL handshake success \r\n"); - } - } - //mbedtls_debug_set_threshold(ARDUINO_MBEDTLS_DEBUG_LEVEL); - } - } while (0); - - if (_clikey_rsa) { - mbedtls_pk_free(_clikey_rsa); - mbedtls_free(_clikey_rsa); - _clikey_rsa = NULL; - } - - if (_cli_crt) { - mbedtls_x509_crt_free(_cli_crt); - mbedtls_free(_cli_crt); - _cli_crt = NULL; - } - - if (cacert) { - mbedtls_x509_crt_free(cacert); - mbedtls_free(cacert); - cacert = NULL; - } - - if (ret < 0) { - if (ssl_client->socket >= 0) { - mbedtls_net_free((mbedtls_net_context *)&ssl_client->socket); - ssl_client->socket = -1; - } - - if (ssl_client->ssl != NULL) { - mbedtls_ssl_free(ssl_client->ssl); - free(ssl_client->ssl); - ssl_client->ssl = NULL; - } - if (ssl_client->conf != NULL) { - mbedtls_ssl_config_free(ssl_client->conf); - free(ssl_client->conf); - ssl_client->conf = NULL; - } - } - - return ssl_client->socket; -} - -void stop_ssl_socket(sslclient_context *ssl_client) -{ - lwip_shutdown(ssl_client->socket, SHUT_RDWR); - lwip_close(ssl_client->socket); - //mbedtls_net_free((mbedtls_net_context *)&ssl_client->socket); - ssl_client->socket = -1; - - if (ssl_client->ssl != NULL) { - mbedtls_ssl_free(ssl_client->ssl); - free(ssl_client->ssl); - ssl_client->ssl = NULL; - } - if (ssl_client->conf != NULL) { - mbedtls_ssl_config_free(ssl_client->conf); - free(ssl_client->conf); - ssl_client->conf = NULL; - } -} - -int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, uint16_t len) -{ - int ret = -1; - - if (ssl_client->ssl != NULL) { - ret = mbedtls_ssl_write(ssl_client->ssl, data, len); - } - - return ret; -} - -int get_ssl_receive(sslclient_context *ssl_client, uint8_t* data, int length, int flag) -{ - int ret = 0; - uint8_t has_backup_recvtimeout = 0; - int backup_recv_timeout, recv_timeout; - socklen_t len; - - if (ssl_client->ssl == NULL) { - return 0; - } - - if (flag & 0x01) { - // peek for 10ms - ret = lwip_getsockopt(ssl_client->socket, SOL_SOCKET, SO_RCVTIMEO, &backup_recv_timeout, &len); - if (ret >= 0) { - recv_timeout = 100; - ret = lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout)); - if (ret >= 0) { - has_backup_recvtimeout = 1; - } - } - } - - memset(data, 0, length); - ret = mbedtls_ssl_read(ssl_client->ssl, data, length); - - if ((flag & 0x01) && (has_backup_recvtimeout == 1)) { - // restore receiving timeout - lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_RCVTIMEO, &backup_recv_timeout, sizeof(recv_timeout)); - } - - return ret; -} - -int get_ssl_sock_errno(sslclient_context *ssl_client) { - int so_error; - socklen_t len = sizeof(so_error); - lwip_getsockopt(ssl_client->socket, SOL_SOCKET, SO_ERROR, &so_error, &len); - return so_error; -} - -int get_ssl_bytes_avail(sslclient_context *ssl_client) { - if (ssl_client->ssl != NULL) { - return mbedtls_ssl_get_bytes_avail(ssl_client->ssl); - } else { - return 0; - } -} diff --git a/arduino/realtek-ambz/cores/arduino/ard_ssl.h b/arduino/realtek-ambz/cores/arduino/ard_ssl.h deleted file mode 100644 index 2a64327..0000000 --- a/arduino/realtek-ambz/cores/arduino/ard_ssl.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef ARD_SSL_H -#define ARD_SSL_H - -struct mbedtls_ssl_context; -struct mbedtls_ssl_config; - -typedef struct { - int socket; - int recvTimeout; - mbedtls_ssl_context *ssl; - mbedtls_ssl_config *conf; -} sslclient_context; - -int start_ssl_client(sslclient_context *ssl_client, uint32_t ipAddress, uint32_t port, unsigned char* rootCABuff, unsigned char* cli_cert, unsigned char* cli_key, unsigned char* pskIdent, unsigned char* psKey, char* SNI_hostname); - -void stop_ssl_socket(sslclient_context *ssl_client); - -int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, uint16_t len); - -int get_ssl_receive(sslclient_context *ssl_client, uint8_t* data, int length, int flag); - -int get_ssl_sock_errno(sslclient_context *ssl_client); - -int get_ssl_bytes_avail(sslclient_context *ssl_client); - -#endif diff --git a/arduino/realtek-ambz/cores/arduino/server_drv.cpp b/arduino/realtek-ambz/cores/arduino/server_drv.cpp deleted file mode 100644 index 92b6fb6..0000000 --- a/arduino/realtek-ambz/cores/arduino/server_drv.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "server_drv.h" - -#ifdef __cplusplus -extern "C" { -#include "ard_socket.h" -#include "platform_stdlib.h" -} -#endif - -// Start server TCP on port specified -int ServerDrv::startServer(uint16_t port, uint8_t protMode) -{ - int sock; - - sock = start_server(port, protMode); - if (sock >= 0) { - if(protMode == TCP_MODE) - sock_listen(sock, 1); - } - - return sock; -} - -int ServerDrv::getAvailable(int sock) -{ - return get_available(sock); -} - -int ServerDrv::availData(int sock) -{ - int ret; - uint8_t c; - - if (sock < 0) - return 0; - - if(_available) { - return 1; - } else { - // flag = MSG_PEEK - ret = get_receive(sock, &c, 1, 1, &_peer_addr, &_peer_port); - if ( ret == 1 ) { - _available = true; - return 1; - } - else{ - return ret; - } - } -} - -bool ServerDrv::getData(int sock, uint8_t *data, uint8_t peek) -{ - int ret = 0; - int flag = 0; - - if (peek) { - flag |= 1; - } else { - _available = false; - } - - ret = get_receive(sock, data, 1, flag, &_peer_addr, &_peer_port); - - if (ret == 1) { - return true; - } - - return false; -} - -int ServerDrv::getDataBuf(int sock, uint8_t *_data, uint16_t _dataLen) -{ - int ret; - - _available = false; - - ret = get_receive(sock, _data, _dataLen, 0, &_peer_addr, &_peer_port); - - return ret; -} - -int ServerDrv::getLastErrno(int sock) -{ - return get_sock_errno(sock); -} - -void ServerDrv::stopClient(int sock) -{ - stop_socket(sock); - _available = false; -} - -bool ServerDrv::sendData(int sock, const uint8_t *data, uint16_t len) -{ - int ret; - - if (sock < 0) - return false; - - ret = send_data(sock, data, len); - - if (ret <= 0) { - return false; - } - - return true; -} - -bool ServerDrv::sendtoData(int sock, const uint8_t *data, uint16_t len, uint32_t peer_ip, uint16_t peer_port) -{ - int ret; - - if (sock < 0) - return false; - - ret = sendto_data(sock, data, len, peer_ip, peer_port); - - if (ret == 0) { - return false; - } - - return true; -} - -int ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t protMode) -{ - int sock; - - sock = start_client(ipAddress, port, protMode); - - return sock; -} - -void ServerDrv::getRemoteData(int sock, uint32_t *ip, uint16_t *port) -{ - // TODO: These data may be outdated? - *ip = _peer_addr; - *port = _peer_port; -} - -int ServerDrv::setSockRecvTimeout(int sock, int timeout) -{ - return set_sock_recv_timeout(sock, timeout); -} - diff --git a/arduino/realtek-ambz/cores/arduino/server_drv.h b/arduino/realtek-ambz/cores/arduino/server_drv.h deleted file mode 100644 index 60166b8..0000000 --- a/arduino/realtek-ambz/cores/arduino/server_drv.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - server_drv.h - Library for Arduino Wifi shield. - Copyright (c) 2011-2014 Arduino. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef Server_Drv_h -#define Server_Drv_h - -#include - -typedef enum eProtMode {TCP_MODE, UDP_MODE}tProtMode; -#define DATA_LENTH 128 -class ServerDrv -{ -public: - int startServer(uint16_t port, uint8_t protMode=TCP_MODE); - int getAvailable(int sock); - int startClient(uint32_t ipAddress, uint16_t port, uint8_t protMode=TCP_MODE); - int getLastErrno(int sock); - void stopClient(int sock); - bool getData(int sock, uint8_t *data, uint8_t peek = 0); - int getDataBuf(int sock, uint8_t *_data, uint16_t _dataLen); - - /* Usually used by TCP */ - bool sendData(int sock, const uint8_t *data, uint16_t len); - - /* Usually used by UDP */ - bool sendtoData(int sock, const uint8_t *data, uint16_t len, uint32_t peer_ip, uint16_t peer_port); - - int availData(int sock); - - void getRemoteData(int sock, uint32_t *ip, uint16_t *port); - - int setSockRecvTimeout(int sock, int timeout); - -private: - bool _available; - - uint32_t _peer_addr; - uint16_t _peer_port; -}; - -#endif diff --git a/arduino/realtek-ambz/cores/arduino/ssl_drv.cpp b/arduino/realtek-ambz/cores/arduino/ssl_drv.cpp deleted file mode 100644 index e2a30e8..0000000 --- a/arduino/realtek-ambz/cores/arduino/ssl_drv.cpp +++ /dev/null @@ -1,131 +0,0 @@ -#include "ssl_drv.h" -#ifdef __cplusplus -extern "C" { -#include "platform_stdlib.h" -} -#endif - -uint16_t SSLDrv::availData(sslclient_context *ssl_client) -{ - //int ret; - - if (ssl_client->socket < 0) { - return 0; - } - - if (_available) { - return 1; - } else { - return getData(ssl_client, c, 1); - } -} - -bool SSLDrv::getData(sslclient_context *ssl_client, uint8_t *data, uint8_t peek) -{ - int ret = 0; - int flag = 0; - - if (_available) { - /* we already has data to read */ - data[0] = c[0]; - - //if (peek) { - //} else { - // /* It's not peek and the data has been taken */ - // _available = false; - //} - if (!peek) { - /* It's not peek and the data has been taken */ - _available = false; - } - - return true; - } - - if (peek) { - flag |= 1; - } - - ret = get_ssl_receive(ssl_client, c, 1, flag); - - if (ret == 1) { - data[0] = c[0]; - if (peek) { - _available = true; - } else { - _available = false; - } - return true; - } - - return false; -} - -int SSLDrv::getDataBuf(sslclient_context *ssl_client, uint8_t *_data, uint16_t _dataLen) -{ - int ret; - - if (_available) { - /* there is one byte cached */ - _data[0] = c[0]; - _available = false; - _dataLen--; - if (_dataLen > 0) { - ret = get_ssl_receive(ssl_client, _data, _dataLen, 0); - if (ret > 0) { - ret++; - return ret; - } else { - return 1; - } - } else { - return 1; - } - } else { - ret = get_ssl_receive(ssl_client, _data, _dataLen, 0); - } - - return ret; -} - -void SSLDrv::stopClient(sslclient_context *ssl_client) -{ - stop_ssl_socket(ssl_client); - _available = false; -} - -bool SSLDrv::sendData(sslclient_context *ssl_client, const uint8_t *data, uint16_t len) -{ - int ret; - - if (ssl_client->socket < 0) { - return false; - } - - ret = send_ssl_data(ssl_client, data, len); - - if (ret == 0) { - return false; - } - - return true; -} - -int SSLDrv::startClient(sslclient_context *ssl_client, uint32_t ipAddress, uint32_t port, unsigned char* rootCABuff, unsigned char* cli_cert, unsigned char* cli_key, unsigned char* pskIdent, unsigned char* psKey, char* SNI_hostname) -{ - int ret; - - ret = start_ssl_client(ssl_client, ipAddress, port, rootCABuff, cli_cert, cli_key, pskIdent, psKey, SNI_hostname); - - return ret; -} - -int SSLDrv::getLastErrno(sslclient_context *ssl_client) -{ - return get_ssl_sock_errno(ssl_client); -} - -int SSLDrv::setSockRecvTimeout(int sock, int timeout) -{ - return setSockRecvTimeout(sock, timeout); -} diff --git a/arduino/realtek-ambz/cores/arduino/ssl_drv.h b/arduino/realtek-ambz/cores/arduino/ssl_drv.h deleted file mode 100644 index fce52d6..0000000 --- a/arduino/realtek-ambz/cores/arduino/ssl_drv.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef SSL_DRV_H -#define SSL_DRV_H - -#include "Arduino.h" -#include - -#define DATA_LENTH 128 -#ifdef __cplusplus -extern "C" { -#include "ard_ssl.h" -} -#endif - -class SSLDrv -{ - public: - int startClient(sslclient_context *ssl_client, uint32_t ipAddress, uint32_t port, unsigned char* rootCABuff, unsigned char* cli_cert, unsigned char* cli_key, unsigned char* pskIdent, unsigned char* psKey, char* SNI_hostname); - void stopClient(sslclient_context *ssl_client); - bool getData(sslclient_context *ssl_client, uint8_t *data, uint8_t peek=0); - int getDataBuf(sslclient_context *ssl_client, uint8_t *_data, uint16_t _dataLen); - bool sendData(sslclient_context *ssl_client, const uint8_t *data, uint16_t len); - uint16_t availData(sslclient_context *ssl_client); - sslclient_context *init(void); - int getLastErrno(sslclient_context *ssl_client); - - int setSockRecvTimeout(int sock, int timeout); - - private: - bool _available; - uint8_t c[1]; -}; - -#endif diff --git a/arduino/realtek-ambz/cores/arduino/wifi_drv.cpp b/arduino/realtek-ambz/cores/arduino/wifi_drv.cpp deleted file mode 100644 index 3ce5ca7..0000000 --- a/arduino/realtek-ambz/cores/arduino/wifi_drv.cpp +++ /dev/null @@ -1,492 +0,0 @@ -#include "wifi_drv.h" - -// arduino includes -#include "wl_definitions.h" -#include "wl_types.h" -#include "Arduino.h" - -#ifdef __cplusplus -extern "C" { - -// RTK includes -#include "main.h" -#include "wifi_conf.h" -#include "wifi_constants.h" -#include "wifi_structures.h" -#include "lwip_netconf.h" -#include "lwip/err.h" -#include "lwip/api.h" -#include - -extern struct netif xnetif[NET_IF_NUM]; - -} -#endif - -// Array of data to cache the information related to the networks discovered -uint8_t WiFiDrv::_networkCount = 0; -char WiFiDrv::_networkSsid[][WL_SSID_MAX_LENGTH] = {{"1"},{"2"},{"3"},{"4"},{"5"}}; -int32_t WiFiDrv::_networkRssi[WL_NETWORKS_LIST_MAXNUM] = { 0 }; -uint32_t WiFiDrv::_networkEncr[WL_NETWORKS_LIST_MAXNUM] = { 0 }; - -static bool init_wlan = false; -static int wifi_mode = NULL; - -static rtw_network_info_t wifi = {0}; -static rtw_ap_info_t ap = {0}; -static unsigned char password[65] = {0}; - -rtw_wifi_setting_t WiFiDrv::wifi_setting; - -static void init_wifi_struct(void) -{ - memset(wifi.ssid.val, 0, sizeof(wifi.ssid.val)); - memset(wifi.bssid.octet, 0, ETH_ALEN); - memset(password, 0, sizeof(password)); - wifi.ssid.len = 0; - wifi.password = NULL; - wifi.password_len = 0; - wifi.key_id = -1; - memset(ap.ssid.val, 0, sizeof(ap.ssid.val)); - ap.ssid.len = 0; - ap.password = NULL; - ap.password_len = 0; - ap.channel = 1; -} - -void WiFiDrv::wifiDriverInit() -{ - struct netif * pnetif = &xnetif[0]; - - if (init_wlan == false) { - init_wlan = true; - LwIP_Init(); - wifi_on(RTW_MODE_STA); - wifi_mode = RTW_MODE_STA; - }else if (init_wlan == true) { - if (wifi_mode != RTW_MODE_STA){ - dhcps_deinit(); - wifi_off(); - vTaskDelay(20); - wifi_on(RTW_MODE_STA); - dhcps_init(pnetif); - wifi_mode = RTW_MODE_STA; - } - } -} - -int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) -{ - int ret; - uint8_t dhcp_result; - - memset(wifi.bssid.octet, 0, ETH_ALEN); - memcpy(wifi.ssid.val, ssid, ssid_len); - wifi.ssid.len = ssid_len; - - wifi.security_type = RTW_SECURITY_OPEN; - wifi.password = NULL; - wifi.password_len = 0; - wifi.key_id = 0; - - ret = wifi_connect((char*)wifi.ssid.val, wifi.security_type, (char*)wifi.password, wifi.ssid.len, - wifi.password_len, wifi.key_id, NULL); - - if (ret == RTW_SUCCESS) { - - dhcp_result = LwIP_DHCP(0, DHCP_START); - - init_wifi_struct(); - - if ( dhcp_result == DHCP_ADDRESS_ASSIGNED ) { - return WL_SUCCESS; - } else { - wifi_disconnect(); - return WL_FAILURE; - } - - } else { - - init_wifi_struct(); - - return WL_FAILURE; - } -} - -int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) -{ - int ret; - uint8_t dhcp_result; - - memset(wifi.bssid.octet, 0, ETH_ALEN); - memcpy(wifi.ssid.val, ssid, ssid_len); - wifi.ssid.len = ssid_len; - - wifi.security_type = RTW_SECURITY_WPA2_AES_PSK; - memset(password, 0, sizeof(password)); - memcpy(password, passphrase, len); - wifi.password = password; - wifi.password_len = len; - wifi.key_id = 0; - - ret = wifi_connect((char*)wifi.ssid.val, wifi.security_type, (char*)wifi.password, wifi.ssid.len, - wifi.password_len, wifi.key_id, NULL); - - if (ret == RTW_SUCCESS) { - - dhcp_result = LwIP_DHCP(0, DHCP_START); - - init_wifi_struct(); - - if ( dhcp_result == DHCP_ADDRESS_ASSIGNED ) { - return WL_SUCCESS; - } else { - wifi_disconnect(); - return WL_FAILURE; - } - - } else { - - init_wifi_struct(); - - return WL_FAILURE; - } -} - -int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len) -{ - int ret; - uint8_t dhcp_result; - int i, idx; - const unsigned char* k = (const unsigned char *)key; - - memset(wifi.bssid.octet, 0, ETH_ALEN); - memcpy(wifi.ssid.val, ssid, ssid_len); - wifi.ssid.len = ssid_len; - - wifi.security_type = RTW_SECURITY_WEP_PSK; - memset(password, 0, sizeof(password)); - - // convert hex sring to hex value - for (i=0, idx=0; i= '0' && k[i] <= '9' ) { - password[idx] += (k[i] - '0'); - } else if ( k[i] >= 'a' && k[i] <= 'f' ) { - password[idx] += (k[i] - 'a' + 10); - } else if ( k[i] >= 'A' && k[i] <= 'F' ) { - password[idx] += (k[i] - 'A' + 10); - } - - if (i % 2 == 0) { - password[idx] *= 16; - } else { - idx++; - } - } - - wifi.password = password; - wifi.password_len = len/2; - wifi.key_id = key_idx; - - ret = wifi_connect((char*)wifi.ssid.val, wifi.security_type, (char*)wifi.password, wifi.ssid.len, - wifi.password_len, wifi.key_id, NULL); - - if (ret == RTW_SUCCESS) { - - dhcp_result = LwIP_DHCP(0, DHCP_START); - - init_wifi_struct(); - - if ( dhcp_result == DHCP_ADDRESS_ASSIGNED ) { - return WL_SUCCESS; - } else { - wifi_disconnect(); - return WL_FAILURE; - } - - } else { - - init_wifi_struct(); - - return WL_FAILURE; - } - -} - -int8_t WiFiDrv::apSetNetwork(char* ssid, uint8_t ssid_len) -{ - int ret = WL_SUCCESS; - - ap.ssid.len = ssid_len; - - if(ap.ssid.len > 32){ - printf("Error: SSID length can't exceed 32\n\r"); - ret = WL_FAILURE; - } - strcpy((char *)ap.ssid.val, (char*)ssid); - return ret; -} - -int8_t WiFiDrv::apSetPassphrase(const char *passphrase, uint8_t len) - -{ - int ret = WL_SUCCESS; - strcpy((char *)password, (char*)passphrase); - ap.password = password; - ap.password_len = len; - return ret; -} - -int8_t WiFiDrv::apSetChannel(const char *channel) -{ - int ret = WL_SUCCESS; - ap.channel = (unsigned char) atoi((const char *)channel); - return ret; -} - -int8_t WiFiDrv::apActivate() -{ -#if CONFIG_LWIP_LAYER - struct ip_addr ipaddr; - struct ip_addr netmask; - struct ip_addr gw; - struct netif * pnetif = &xnetif[0]; -#endif - int timeout = 20; - int ret = WL_SUCCESS; - if(ap.ssid.val[0] == 0){ - printf("Error: SSID can't be empty\n\r"); - ret = WL_FAILURE; - goto exit; - } - if(ap.password == NULL){ - ap.security_type = RTW_SECURITY_OPEN; - } - else{ - ap.security_type = RTW_SECURITY_WPA2_AES_PSK; - } - -#if CONFIG_LWIP_LAYER - dhcps_deinit(); - IP4_ADDR(&ipaddr, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); - IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3); - IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); - netif_set_addr(pnetif, &ipaddr, &netmask,&gw); -#endif - wifi_off(); - vTaskDelay(20); - if (wifi_on(RTW_MODE_AP) < 0){ - printf("\n\rERROR: Wifi on failed!"); - ret = WL_FAILURE; - goto exit; - } - printf("\n\rStarting AP ..."); - - if((ret = wifi_start_ap((char*)ap.ssid.val, ap.security_type, (char*)ap.password, ap.ssid.len, ap.password_len, ap.channel) )< 0) { - printf("\n\rERROR: Operation failed!"); - ret = WL_FAILURE; - goto exit; - } - - while(1) { - char essid[33]; - - if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) > 0) { - if(strcmp((const char *) essid, (const char *)ap.ssid.val) == 0) { - printf("\n\r%s started\n", ap.ssid.val); - ret = WL_SUCCESS; - break; - } - } - - if(timeout == 0) { - printf("\n\rERROR: Start AP timeout!"); - ret = WL_FAILURE; - break; - } - - vTaskDelay(1 * configTICK_RATE_HZ); - timeout --; - } -#if CONFIG_LWIP_LAYER - //LwIP_UseStaticIP(pnetif); - dhcps_init(pnetif); -#endif - -exit: - init_wifi_struct( ); - if(ret == WL_SUCCESS){ - wifi_mode = RTW_MODE_AP; - } - return ret; -} -int8_t WiFiDrv::disconnect() -{ - wifi_disconnect(); - - return WL_DISCONNECTED; -} - -uint8_t WiFiDrv::getConnectionStatus() -{ - wifiDriverInit(); - - if (wifi_is_connected_to_ap() == 0) { - return WL_CONNECTED; - } else { - return WL_DISCONNECTED; - } -} - -uint8_t* WiFiDrv::getMacAddress() -{ - return LwIP_GetMAC(&xnetif[0]); -} - -void WiFiDrv::getIpAddress(IPAddress& ip) -{ - ip = LwIP_GetIP(&xnetif[0]); -} - -void WiFiDrv::getSubnetMask(IPAddress& mask) -{ - mask = LwIP_GetMASK(&xnetif[0]); -} - -void WiFiDrv::getGatewayIP(IPAddress& ip) -{ - ip = LwIP_GetGW(&xnetif[0]); -} - -char* WiFiDrv::getCurrentSSID() -{ - wifi_get_setting(WLAN0_NAME, &wifi_setting); - - return (char *)(wifi_setting.ssid); -} - -uint8_t* WiFiDrv::getCurrentBSSID() -{ - uint8_t bssid[ETH_ALEN]; - wext_get_bssid(WLAN0_NAME, bssid); - return bssid; -} - -int32_t WiFiDrv::getCurrentRSSI() -{ - int rssi = 0; - - wifi_get_rssi(&rssi); - - return rssi; -} - -uint8_t WiFiDrv::getCurrentEncryptionType() -{ - wifi_get_setting(WLAN0_NAME, &wifi_setting); - - return (wifi_setting.security_type); -} - -rtw_result_t WiFiDrv::wifidrv_scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result ) -{ - rtw_scan_result_t* record; - - if (malloced_scan_result->scan_complete != RTW_TRUE) { - record = &malloced_scan_result->ap_details; - record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */ - - if ( _networkCount < WL_NETWORKS_LIST_MAXNUM ) { - strcpy( _networkSsid[_networkCount], (char *)record->SSID.val); - _networkRssi[_networkCount] = record->signal_strength; - _networkEncr[_networkCount] = record->security; - _networkCount++; - } - } - - return RTW_SUCCESS; -} - -int8_t WiFiDrv::startScanNetworks() -{ - _networkCount = 0; - if( wifi_scan_networks(wifidrv_scan_result_handler, NULL ) != RTW_SUCCESS ){ - return WL_FAILURE; - } - - return WL_SUCCESS; -} - -uint8_t WiFiDrv::getScanNetworks() -{ - return _networkCount; -} - -char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem) -{ - if (networkItem >= WL_NETWORKS_LIST_MAXNUM) - return NULL; - - return _networkSsid[networkItem]; -} - -uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem) -{ - if (networkItem >= WL_NETWORKS_LIST_MAXNUM) - return NULL; - - uint8_t encType = 0; - - if ( _networkEncr[networkItem] == RTW_SECURITY_OPEN ) { - encType = ENC_TYPE_NONE; - } else if ( (_networkEncr[networkItem] & AES_ENABLED) || (_networkEncr[networkItem] == RTW_SECURITY_WPA_WPA2_MIXED) ) { - encType = ENC_TYPE_CCMP; - } else if ( _networkEncr[networkItem] & TKIP_ENABLED ) { - encType = ENC_TYPE_TKIP; - } else if ( _networkEncr[networkItem] == RTW_SECURITY_WEP_PSK ) { - encType = ENC_TYPE_WEP; - } - - return encType; -} - -uint32_t WiFiDrv::getEncTypeNetowrksEx(uint8_t networkItem) -{ - return (networkItem >= WL_NETWORKS_LIST_MAXNUM) ? NULL : _networkEncr[networkItem]; -} - -int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) -{ - if (networkItem >= WL_NETWORKS_LIST_MAXNUM) - return NULL; - - return _networkRssi[networkItem]; -} - -char* WiFiDrv::getFwVersion() -{ - // The version is for compatible to arduino example code - return "1.1.0"; -} - -int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult) -{ - ip_addr_t ip_addr; - err_t err; - - err = netconn_gethostbyname(aHostname, &ip_addr); - - if (err != ERR_OK) { - return WL_FAILURE; - } - else{ - aResult = ip_addr.addr; - return WL_SUCCESS; - } -} - -int WiFiDrv::disablePowerSave() -{ - return wifi_disable_powersave(); -} diff --git a/arduino/realtek-ambz/cores/arduino/wifi_drv.h b/arduino/realtek-ambz/cores/arduino/wifi_drv.h deleted file mode 100644 index d0b47f6..0000000 --- a/arduino/realtek-ambz/cores/arduino/wifi_drv.h +++ /dev/null @@ -1,272 +0,0 @@ -/* - wifi_drv.h - Library for Arduino Wifi shield. - Copyright (c) 2011-2014 Arduino. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef WiFi_Drv_h -#define WiFi_Drv_h - -#include -#include "api/IPAddress.h" -#include "wl_definitions.h" - -#ifdef __cplusplus -extern "C" { - -// rtk includes -#include "wifi_structures.h" - -} -#endif - -// Key index length -#define KEY_IDX_LEN 1 -// 5 secs of delay to have the connection established -#define WL_DELAY_START_CONNECTION 5000 -// firmware version string length -#define WL_FW_VER_LENGTH 6 - -class WiFiDrv -{ -private: - // settings of requested network - static uint8_t _networkCount; - static char _networkSsid[WL_NETWORKS_LIST_MAXNUM][WL_SSID_MAX_LENGTH]; - static int32_t _networkRssi[WL_NETWORKS_LIST_MAXNUM]; - static uint32_t _networkEncr[WL_NETWORKS_LIST_MAXNUM]; - - // settings of current selected network - static rtw_wifi_setting_t wifi_setting; - - static rtw_result_t wifidrv_scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result ); - -public: - - /* - * Driver initialization - */ - static void wifiDriverInit(); - - /* - * Set the desired network which the connection manager should try to - * connect to. - * - * The ssid of the desired network should be specified. - * - * param ssid: The ssid of the desired network. - * param ssid_len: Lenght of ssid string. - * return: WL_SUCCESS or WL_FAILURE - */ - static int8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); - - /* Start Wifi connection with passphrase - * the most secure supported mode will be automatically selected - * - * param ssid: Pointer to the SSID string. - * param ssid_len: Lenght of ssid string. - * param passphrase: Passphrase. Valid characters in a passphrase - * must be between ASCII 32-126 (decimal). - * param len: Lenght of passphrase string. - * return: WL_SUCCESS or WL_FAILURE - */ - static int8_t wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); - - /* Start Wifi connection with WEP encryption. - * Configure a key into the device. The key type (WEP-40, WEP-104) - * is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104). - * - * param ssid: Pointer to the SSID string. - * param ssid_len: Lenght of ssid string. - * param key_idx: The key index to set. Valid values are 0-3. - * param key: Key input buffer. - * param len: Lenght of key string. - * return: WL_SUCCESS or WL_FAILURE - */ - static int8_t wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len); - - /* Set ip configuration disabling dhcp client - * - * param validParams: set the number of parameters that we want to change - * i.e. validParams = 1 means that we'll change only ip address - * validParams = 3 means that we'll change ip address, gateway and netmask - * param local_ip: Static ip configuration - * param gateway: Static gateway configuration - * param subnet: Static subnet mask configuration - */ - static void config(uint8_t validParams, uint32_t local_ip, uint32_t gateway, uint32_t subnet); - - /* Set DNS ip configuration - * - * param validParams: set the number of parameters that we want to change - * i.e. validParams = 1 means that we'll change only dns_server1 - * validParams = 2 means that we'll change dns_server1 and dns_server2 - * param dns_server1: Static DNS server1 configuration - * param dns_server2: Static DNS server2 configuration - */ - static void setDNS(uint8_t validParams, uint32_t dns_server1, uint32_t dns_server2); - - /* - * Disconnect from the network - * - * return: WL_SUCCESS or WL_FAILURE - */ - static int8_t disconnect(); - - /* - * Disconnect from the network - * - * return: one value of wl_status_t enum - */ - static uint8_t getConnectionStatus(); - - /* - * Get the interface MAC address. - * - * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - */ - static uint8_t* getMacAddress(); - - /* - * Get the interface IP address. - * - * return: copy the ip address value in IPAddress object - */ - static void getIpAddress(IPAddress& ip); - - /* - * Get the interface subnet mask address. - * - * return: copy the subnet mask address value in IPAddress object - */ - static void getSubnetMask(IPAddress& mask); - - /* - * Get the gateway ip address. - * - * return: copy the gateway ip address value in IPAddress object - */ - static void getGatewayIP(IPAddress& ip); - - /* - * Return the current SSID associated with the network - * - * return: ssid string - */ - static char* getCurrentSSID(); - - /* - * Return the current BSSID associated with the network. - * It is the MAC address of the Access Point - * - * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - */ - static uint8_t* getCurrentBSSID(); - - /* - * Return the current RSSI /Received Signal Strength in dBm) - * associated with the network - * - * return: signed value - */ - static int32_t getCurrentRSSI(); - - /* - * Return the Encryption Type associated with the network - * - * return: one value of wl_enc_type enum - */ - static uint8_t getCurrentEncryptionType(); - - /* - * Start scan WiFi networks available - * - * return: Number of discovered networks - */ - static int8_t startScanNetworks(); - - /* - * Get the networks available - * - * return: Number of discovered networks - */ - static uint8_t getScanNetworks(); - - /* - * Return the SSID discovered during the network scan. - * - * param networkItem: specify from which network item want to get the information - * - * return: ssid string of the specified item on the networks scanned list - */ - static char* getSSIDNetoworks(uint8_t networkItem); - - /* - * Return the RSSI of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: signed value of RSSI of the specified item on the networks scanned list - */ - static int32_t getRSSINetoworks(uint8_t networkItem); - - /* - * Return the encryption type of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list - */ - static uint8_t getEncTypeNetowrks(uint8_t networkItem); - - /* - * Return the security type and encryption type of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: security and encryption type of the specified item on the networks scanned list - */ - static uint32_t getEncTypeNetowrksEx(uint8_t networkItem); - - /* - * Resolve the given hostname to an IP address. - * param aHostname: Name to be resolved - * param aResult: IPAddress structure to store the returned IP address - * result: 1 if aIPAddrString was successfully converted to an IP address, - * else error code - */ - static int getHostByName(const char* aHostname, IPAddress& aResult); - - /* - * Get the firmware version - * result: version as string with this format a.b.c - */ - static char* getFwVersion(); - - static int8_t apSetNetwork(char* ssid, uint8_t ssid_len); - - static int8_t apSetPassphrase(const char *passphrase, uint8_t len); - - static int8_t apSetChannel(const char *channel); - - static int8_t apActivate(); - - static int disablePowerSave(); -}; - -extern WiFiDrv wiFiDrv; - -#endif diff --git a/builder/arduino-common.py b/builder/arduino-common.py index 0141945..4ef1f99 100644 --- a/builder/arduino-common.py +++ b/builder/arduino-common.py @@ -1,54 +1,49 @@ +# Copyright (c) Kuba Szczodrzyński 2022-04-23. + from os.path import isdir, join from SCons.Script import DefaultEnvironment env = DefaultEnvironment() platform = env.PioPlatform() -board = env.BoardConfig() API_DIR = platform.get_package_dir("framework-arduino-api") LT_API_DIR = join(platform.get_dir(), "arduino", "libretuya") assert isdir(API_DIR) assert isdir(LT_API_DIR) -# Includes -env.Prepend( - CPPPATH=[ - # fmt: off - join(API_DIR), - join(API_DIR, "api", "deprecated"), - join(LT_API_DIR), - join(LT_API_DIR, "compat"), - join(LT_API_DIR, "libraries", "base64"), - join(LT_API_DIR, "libraries", "HTTPClient"), - join(LT_API_DIR, "libraries", "NetUtils"), - join(LT_API_DIR, "libraries", "WebServer"), - join(LT_API_DIR, "libraries", "WiFiMulti"), - # fmt: on +# Sources - ArduinoCore-API +env.AddLibrary( + name="arduino_api", + base_dir=API_DIR, + srcs=[ + "+", + "+", + "+", + "+", + "+", + "+", ], - CPPDEFINES=[ - ("LT_VERSION", platform.version), - ("LT_BOARD", board.get("build.variant")), + includes=[ + "!<.>", + "!", ], ) -sources_api = [ - # fmt: off - "+<" + API_DIR + "/api/Common.cpp>", - "+<" + API_DIR + "/api/IPAddress.cpp>", - "+<" + API_DIR + "/api/PluggableUSB.cpp>", - "+<" + API_DIR + "/api/Print.cpp>", - "+<" + API_DIR + "/api/Stream.cpp>", - "+<" + API_DIR + "/api/String.cpp>", - "+<" + LT_API_DIR + "/api/*.c*>", - "+<" + LT_API_DIR + "/libraries/**/*.c*>", - # fmt: on -] - -# Arduino API library target -target_api = env.BuildLibrary( - join("$BUILD_DIR", "arduino_api"), - API_DIR, - sources_api, +# Sources - LibreTuya API +env.AddLibrary( + name="libretuya_api", + base_dir=LT_API_DIR, + srcs=[ + "+", + "+", + ], + includes=[ + "!<.>", + "!", + "!", + ], ) -env.Prepend(LIBS=[target_api]) + +# Build all libraries +env.BuildLibraries(safe=False) diff --git a/builder/frameworks/realtek-ambz-arduino.py b/builder/frameworks/realtek-ambz-arduino.py index 65f824b..316bfba 100644 --- a/builder/frameworks/realtek-ambz-arduino.py +++ b/builder/frameworks/realtek-ambz-arduino.py @@ -1,31 +1,14 @@ -from glob import glob -from os.path import isdir, join +# Copyright (c) Kuba Szczodrzyński 2022-04-22. + +from os.path import join from SCons.Script import DefaultEnvironment env = DefaultEnvironment() -platform = env.PioPlatform() -board = env.BoardConfig() env.SConscript("realtek-ambz-sdk.py", exports="env") env.SConscript("../arduino-common.py", exports="env") -mcu = board.get("build.mcu").upper() -family = board.get("build.family").upper() -variant = board.get("build.variant") -ldscript = board.get("build.ldscript_arduino") - -SDK_DIR = platform.get_package_dir("framework-realtek-amb1") -BOARD_DIR = join(platform.get_dir(), "boards", variant) -PLATFORM_DIR = join(platform.get_dir(), "platform", "realtek-ambz") -FIXUPS_DIR = join(PLATFORM_DIR, "fixups") -CORE_DIR = join(platform.get_dir(), "arduino", "realtek-ambz") -assert isdir(SDK_DIR) -assert isdir(env.subst(BOARD_DIR)) -assert isdir(env.subst(PLATFORM_DIR)) -assert isdir(env.subst(FIXUPS_DIR)) -assert isdir(env.subst(CORE_DIR)) - # Flags env.Append( CXXFLAGS=[ @@ -54,7 +37,7 @@ env.Append( "ARDUINO_AMEBA", "ARDUINO_SDK", "ARDUINO_ARCH_AMBZ", - f"BOARD_{family}", + "BOARD_${FAMILY}", # the SDK declares bool if not defined before # which conflicts with C++ built-in bool # so it's either -fpermissive or this: @@ -63,6 +46,7 @@ env.Append( "LT_HAS_LWIP", ("LT_PRINTF_BROKEN", "1"), # printf does not handle %.3f properly ("zalloc", "os_zalloc"), + "MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED", # enable PSK in mbedTLS ], LINKFLAGS=[ "--specs=nosys.specs", @@ -78,110 +62,97 @@ env.Append( # Arduino core uses __libc_init_array env["LINKFLAGS"].remove("-nostartfiles") -# Includes -env.Prepend( - CPPPATH=[ - # fmt: off +# Sources - Arduino Core +env.AddLibrary( + name="ambz_arduino_core", + base_dir="$ARDUINO_DIR", + srcs=[ + # Wiring core + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + ], + includes=[ # prepend these as the Arduino core is incorrectly picking some includes from SDK - join(CORE_DIR, "cores", "arduino"), - join(CORE_DIR, "cores", "arduino", "avr"), - join(CORE_DIR, "cores", "arduino", "rtl8195a"), - join(CORE_DIR, "cores", "arduino", "spiffs"), - # fmt: on - ], -) -env.Append( - CPPPATH=[ - # fmt: off + "!", + "!", + "!", # includes that are missing in the vanilla SDK makefiles - join(SDK_DIR, "component", "common", "drivers", "sdio", "realtek", "sdio_host", "inc"), - join(SDK_DIR, "component", "common", "file_system", "fatfs"), - join(SDK_DIR, "component", "common", "file_system", "fatfs", "r0.10c", "include"), - join(SDK_DIR, "component", "common", "network", "mdns"), - join(SDK_DIR, "component", "common", "network", "libwsclient"), - join(SDK_DIR, "component", "common", "network", "ssl", "mbedtls-2.4.0", "include"), - # fmt: on - ], -) -env["CPPPATH"].remove( - # fmt: off - # remove polarssl - join(SDK_DIR, "component", "common", "network", "ssl", "polarssl-1.3.8", "include"), - # fmt: on -) - -# Sources -sources_core = [ - # fmt: off - "+<" + CORE_DIR + "/cores/arduino/avr/dtostrf.c>", - "+<" + CORE_DIR + "/cores/arduino/b64.cpp>", - "+<" + CORE_DIR + "/cores/arduino/cxxabi-compat.cpp>", - "+<" + CORE_DIR + "/cores/arduino/hooks.c>", - "+<" + CORE_DIR + "/cores/arduino/itoa.c>", - "+<" + CORE_DIR + "/cores/arduino/LOGUARTClass.cpp>", - "+<" + CORE_DIR + "/cores/arduino/lwip_info.c>", - "+<" + CORE_DIR + "/cores/arduino/main.cpp>", - "+<" + CORE_DIR + "/cores/arduino/PowerManagement.cpp>", - "+<" + CORE_DIR + "/cores/arduino/RingBuffer.cpp>", - "+<" + CORE_DIR + "/cores/arduino/rtl_sys.cpp>", - "+<" + CORE_DIR + "/cores/arduino/spiffs/spiffs_cache.c>", - "+<" + CORE_DIR + "/cores/arduino/spiffs/spiffs_check.c>", - "+<" + CORE_DIR + "/cores/arduino/spiffs/spiffs_gc.c>", - "+<" + CORE_DIR + "/cores/arduino/spiffs/spiffs_hydrogen.c>", - "+<" + CORE_DIR + "/cores/arduino/spiffs/spiffs_nucleus.c>", - "+<" + CORE_DIR + "/cores/arduino/Tone.cpp>", - "+<" + CORE_DIR + "/cores/arduino/WebSocketClient.cpp>", - "+<" + CORE_DIR + "/cores/arduino/WInterrupts.c>", - "+<" + CORE_DIR + "/cores/arduino/wiring.c>", - "+<" + CORE_DIR + "/cores/arduino/wiring_analog.c>", - "+<" + CORE_DIR + "/cores/arduino/wiring_digital.c>", - "+<" + CORE_DIR + "/cores/arduino/wiring_os.c>", - "+<" + CORE_DIR + "/cores/arduino/wiring_pulse.cpp>", - "+<" + CORE_DIR + "/cores/arduino/wiring_shift.c>", - "+<" + CORE_DIR + "/cores/arduino/wiring_watchdog.c>", - "+<" + CORE_DIR + "/cores/arduino/WMath.cpp>", - "+<" + BOARD_DIR + "/variant.cpp>", - # fmt: on -] - -# add mbedTLS from SDK -mbedtls_glob = join( - SDK_DIR, "component", "common", "network", "ssl", "mbedtls-2.4.0", "library", "*.c" -) -for file in glob(env.subst(mbedtls_glob)): - if file.endswith("ssl_tls.c"): - continue # skip ssl_tls.c for a fixup - sources_core.append(f"+<{file}>") -env.Append( - CPPDEFINES=[ - "MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED", + "+<$SDK_DIR/component/common/drivers/sdio/realtek/sdio_host/inc>", + "+<$SDK_DIR/component/common/file_system/fatfs>", + "+<$SDK_DIR/component/common/file_system/fatfs/r0.10c/include>", + "+<$SDK_DIR/component/common/network/mdns>", + "+<$SDK_DIR/component/common/network/libwsclient>", ], ) -# Fixups -sources_core += [ - "+<" + FIXUPS_DIR + "/ssl_tls.c>", # rtl sdk defines S1 and S2 which conflicts here -] +# Sources - board variant +env.AddLibrary( + name="ambz_${VARIANT}", + base_dir="$BOARD_DIR", + srcs=[ + "+", + ], +) -# Arduino libraries -sources_libs = [ - # fmt: off - "+<" + CORE_DIR +"/libraries/Flash/Flash.cpp>", - "+<" + CORE_DIR +"/libraries/WiFi/WiFi.cpp>", - "+<" + CORE_DIR +"/libraries/WiFi/WiFiAP.cpp>", - "+<" + CORE_DIR +"/libraries/WiFi/WiFiClient.cpp>", - "+<" + CORE_DIR +"/libraries/WiFi/WiFiGeneric.cpp>", - "+<" + CORE_DIR +"/libraries/WiFi/WiFiScan.cpp>", - "+<" + CORE_DIR +"/libraries/WiFi/WiFiServer.cpp>", - "+<" + CORE_DIR +"/libraries/WiFi/WiFiSTA.cpp>", - # fmt: on -] -env.Append( - CPPPATH=[ - # fmt: off - join(CORE_DIR, "libraries", "Flash"), - join(CORE_DIR, "libraries", "WiFi"), - # fmt: on +# Sources - mbedTLS +env.AddLibrary( + name="ambz_mbedtls", + base_dir="$SDK_DIR", + srcs=[ + # mbedTLS from SDK + "+", + # skip ssl_tls.c for a fixup + "-", + ], + includes=[ + "+", + # remove polarssl + "-", + ], +) + +# Sources - platform fixups +env.AddLibrary( + name="ambz_arduino_fixups", + base_dir="$FIXUPS_DIR", + srcs=[ + "+", # rtl sdk defines S1 and S2 which conflicts here + ], +) + +# Sources - Arduino libraries +env.AddLibrary( + name="ambz_arduino_libs", + base_dir="$ARDUINO_DIR", + srcs=[ + "+", + ], + includes=[ + "+", ], ) @@ -194,22 +165,9 @@ env.Append( ) env.Replace( LDSCRIPT_PATH=[ - join(PLATFORM_DIR, "ld", ldscript), + join("$LD_DIR", "$LDSCRIPT_ARDUINO"), ], ) -# Clone env to ignore options from child projects -envarduino = env.Clone() - -# Arduino Core library target -target_core = envarduino.BuildLibrary( - join("$BUILD_DIR", "ambz_arduino_core"), - CORE_DIR, - sources_core, -) -target_libs = envarduino.BuildLibrary( - join("$BUILD_DIR", "ambz_arduino_libs"), - CORE_DIR, - sources_libs, -) -env.Prepend(LIBS=[target_core, target_libs]) +# Build all libraries +env.BuildLibraries() diff --git a/builder/frameworks/realtek-ambz-sdk.py b/builder/frameworks/realtek-ambz-sdk.py index a78e979..e27a0d7 100644 --- a/builder/frameworks/realtek-ambz-sdk.py +++ b/builder/frameworks/realtek-ambz-sdk.py @@ -1,25 +1,14 @@ -import sys -from os.path import isdir, join +# Copyright (c) Kuba Szczodrzyński 2022-04-20. -from SCons.Script import AlwaysBuild, Builder, DefaultEnvironment +import sys +from os.path import join + +from SCons.Script import Builder, DefaultEnvironment env = DefaultEnvironment() -platform = env.PioPlatform() board = env.BoardConfig() -mcu = board.get("build.mcu").upper() -family = board.get("build.family").upper() -variant = board.get("build.variant") -ldscript = board.get("build.ldscript_sdk") - -SDK_DIR = platform.get_package_dir("framework-realtek-amb1") -BOARD_DIR = join(platform.get_dir(), "boards", variant) -PLATFORM_DIR = join(platform.get_dir(), "platform", "realtek-ambz") -FIXUPS_DIR = join(PLATFORM_DIR, "fixups") -assert isdir(SDK_DIR) -assert isdir(env.subst(BOARD_DIR)) -assert isdir(env.subst(PLATFORM_DIR)) -assert isdir(env.subst(FIXUPS_DIR)) +env.AddDefaults("realtek-ambz", "framework-realtek-amb1") flash_addr = board.get("build.amb_flash_addr") flash_ota1_offset = env.subst("$FLASH_OTA1_OFFSET") @@ -36,9 +25,8 @@ env.Replace( # Tools # fmt: off -TOOL_DIR = join(SDK_DIR, "component", "soc", "realtek", "8711b", "misc", "iar_utility", "common", "tools") +TOOL_DIR = join("$SDK_DIR", "component", "soc", "realtek", "8711b", "misc", "iar_utility", "common", "tools") # fmt: on -assert isdir(SDK_DIR) env.Replace( PICK=join(TOOL_DIR, "pick"), PAD=join(TOOL_DIR, "pad"), @@ -106,291 +94,246 @@ env.Replace( ], ) -# Includes -env.Append( - CPPPATH=[ - # fmt: off - join(BOARD_DIR), - join(FIXUPS_DIR), - join(SDK_DIR, "project", "realtek_amebaz_va0_example", "inc"), - join(SDK_DIR, "component", "os", "freertos"), - join(SDK_DIR, "component", "os", "freertos", "freertos_v8.1.2", "Source", "include"), - join(SDK_DIR, "component", "os", "freertos", "freertos_v8.1.2", "Source", "portable", "GCC", "ARM_CM4F"), - join(SDK_DIR, "component", "os", "os_dep", "include"), - join(SDK_DIR, "component", "common", "api", "network", "include"), - join(SDK_DIR, "component", "common", "api"), - join(SDK_DIR, "component", "common", "api", "at_cmd"), - join(SDK_DIR, "component", "common", "api", "platform"), - join(SDK_DIR, "component", "common", "api", "wifi"), - join(SDK_DIR, "component", "common", "api", "wifi", "rtw_wpa_supplicant", "src"), - join(SDK_DIR, "component", "common", "api", "wifi", "rtw_wowlan"), - join(SDK_DIR, "component", "common", "api", "wifi", "rtw_wpa_supplicant", "wpa_supplicant"), - join(SDK_DIR, "component", "common", "application"), - join(SDK_DIR, "component", "common", "application", "mqtt", "MQTTClient"), - join(SDK_DIR, "component", "common", "application", "mqtt", "MQTTPacket"), - join(SDK_DIR, "component", "common", "example"), - join(SDK_DIR, "component", "common", "example", "wlan_fast_connect"), - join(SDK_DIR, "component", "common", "drivers", "modules"), - join(SDK_DIR, "component", "common", "drivers", "sdio", "realtek", "sdio_host", "inc"), - join(SDK_DIR, "component", "common", "drivers", "inic", "rtl8711b"), - join(SDK_DIR, "component", "common", "drivers", "usb_class", "device"), - join(SDK_DIR, "component", "common", "drivers", "usb_class", "device", "class"), - join(SDK_DIR, "component", "common", "drivers", "wlan", "realtek", "include"), - join(SDK_DIR, "component", "common", "drivers", "wlan", "realtek", "src", "osdep"), - join(SDK_DIR, "component", "common", "drivers", "wlan", "realtek", "src", "hci"), - join(SDK_DIR, "component", "common", "drivers", "wlan", "realtek", "src", "hal"), - join(SDK_DIR, "component", "common", "drivers", "wlan", "realtek", "src", "hal", "rtl8711b"), - join(SDK_DIR, "component", "common", "drivers", "wlan", "realtek", "src", "hal", "OUTSRC"), - join(SDK_DIR, "component", "common", "drivers", "wlan", "realtek", "wlan_ram_map", "rom"), - join(SDK_DIR, "component", "common", "file_system"), - join(SDK_DIR, "component", "common", "network"), - join(SDK_DIR, "component", "common", "network", "lwip", "lwip_v1.4.1", "port", "realtek", "freertos"), - join(SDK_DIR, "component", "common", "network", "lwip", "lwip_v1.4.1", "src", "include"), - join(SDK_DIR, "component", "common", "network", "lwip", "lwip_v1.4.1", "src", "include", "lwip"), - join(SDK_DIR, "component", "common", "network", "lwip", "lwip_v1.4.1", "src", "include", "ipv4"), - join(SDK_DIR, "component", "common", "network", "lwip", "lwip_v1.4.1", "port", "realtek"), - join(SDK_DIR, "component", "common", "network", "ssl", "polarssl-1.3.8", "include"), - join(SDK_DIR, "component", "common", "network", "ssl", "ssl_ram_map", "rom"), - join(SDK_DIR, "component", "common", "test"), - join(SDK_DIR, "component", "common", "utilities"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "app", "monitor", "include"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "cmsis"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "cmsis", "device"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "fwlib"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "fwlib", "include"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "fwlib", "ram_lib", "crypto"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "fwlib", "rom_lib"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "swlib", "os_dep", "include"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "swlib", "std_lib", "include"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "swlib", "std_lib", "libc", "include"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "swlib", "std_lib", "libc", "rom", "string"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "swlib", "std_lib", "libgcc", "rtl8195a", "include"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "swlib", "rtl_lib"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "misc"), - join(SDK_DIR, "component", "soc", "realtek", "8711b", "misc", "os"), - join(SDK_DIR, "component", "common", "mbed", "api"), - join(SDK_DIR, "component", "common", "mbed", "hal"), - join(SDK_DIR, "component", "common", "mbed", "hal_ext"), - join(SDK_DIR, "component", "common", "mbed", "targets", "cmsis"), - join(SDK_DIR, "component", "common", "mbed", "targets", "hal", "rtl8711b"), - join(SDK_DIR, "project", "realtek_8195a_gen_project", "rtl8195a", "sw", "lib", "sw_lib", "mbed", "api"), - join(SDK_DIR, "component", "common", "application", "mqtt", "MQTTClient"), - join(SDK_DIR, "component", "common", "network", "websocket"), - # fmt: on - ], -) - # Sources - from SDK project/realtek_amebaz_va0_example/GCC-RELEASE/application.mk # - "console" is disabled as it introduces build error, and is generally useless # - "utilities example" are also not really needed -sources = [ - # fmt: off - # app uart_adapter - "+<" + SDK_DIR +"/component/common/application/uart_adapter/uart_adapter.c>", - # cmsis - # NOTE: a fixup is used instead, to remove default main() - # "+<" + SDK_DIR +"/component/soc/realtek/8711b/cmsis/device/app_start.c>", - "+<" + SDK_DIR +"/component/soc/realtek/8711b/fwlib/ram_lib/startup.c>", - "+<" + SDK_DIR +"/component/soc/realtek/8711b/cmsis/device/system_8195a.c>", - # console - "+<" + SDK_DIR +"/component/soc/realtek/8711b/app/monitor/ram/rtl_trace.c>", - # network api wifi rtw_wpa_supplicant - "+<" + SDK_DIR +"/component/common/api/wifi/rtw_wpa_supplicant/wpa_supplicant/wifi_eap_config.c>", - "+<" + SDK_DIR +"/component/common/api/wifi/rtw_wpa_supplicant/wpa_supplicant/wifi_p2p_config.c>", - "+<" + SDK_DIR +"/component/common/api/wifi/rtw_wpa_supplicant/wpa_supplicant/wifi_wps_config.c>", - # network api wifi - "+<" + SDK_DIR +"/component/common/api/wifi/wifi_conf.c>", - "+<" + SDK_DIR +"/component/common/api/wifi/wifi_ind.c>", - "+<" + SDK_DIR +"/component/common/api/wifi/wifi_promisc.c>", - "+<" + SDK_DIR +"/component/common/api/wifi/wifi_simple_config.c>", - "+<" + SDK_DIR +"/component/common/api/wifi/wifi_util.c>", - # network api - "+<" + SDK_DIR +"/component/common/api/lwip_netconf.c>", - # network app - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTClient/MQTTClient.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTPacket/MQTTConnectClient.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTPacket/MQTTConnectServer.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTPacket/MQTTDeserializePublish.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTPacket/MQTTFormat.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTClient/MQTTFreertos.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTPacket/MQTTPacket.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTPacket/MQTTSerializePublish.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTPacket/MQTTSubscribeClient.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTPacket/MQTTSubscribeServer.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTPacket/MQTTUnsubscribeClient.c>", - "+<" + SDK_DIR +"/component/common/application/mqtt/MQTTPacket/MQTTUnsubscribeServer.c>", - "+<" + SDK_DIR +"/component/common/api/network/src/ping_test.c>", - "+<" + SDK_DIR +"/component/common/utilities/ssl_client.c>", - "+<" + SDK_DIR +"/component/common/utilities/tcptest.c>", - "+<" + SDK_DIR +"/component/common/api/network/src/wlan_network.c>", - # network lwip api - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/api/api_lib.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/api/api_msg.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/api/err.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/api/netbuf.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/api/netdb.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/api/netifapi.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/api/sockets.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/api/tcpip.c>", - # network lwip core ipv4 - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/autoip.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/icmp.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/igmp.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/inet.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/inet_chksum.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip_addr.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/ipv4/ip_frag.c>", - # network lwip core - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/def.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/dhcp.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/dns.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/init.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/lwip_timers.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/mem.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/memp.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/netif.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/pbuf.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/raw.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/stats.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/sys.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/tcp.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/tcp_in.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/tcp_out.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/core/udp.c>", - # network lwip netif - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/src/netif/etharp.c>", - # network lwip port - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/ethernetif.c>", - "+<" + SDK_DIR +"/component/common/drivers/wlan/realtek/src/osdep/lwip_intf.c>", - "+<" + SDK_DIR +"/component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/sys_arch.c>", - # network - wsclient - "+<" + SDK_DIR +"/component/common/network/websocket/wsclient_tls.c>", - # network lwip - "+<" + SDK_DIR +"/component/common/network/dhcp/dhcps.c>", - "+<" + SDK_DIR +"/component/common/network/sntp/sntp.c>", - # network polarssl polarssl - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/aesni.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/blowfish.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/camellia.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/ccm.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/certs.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/cipher.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/cipher_wrap.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/debug.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/ecp_ram.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/entropy.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/entropy_poll.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/error.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/gcm.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/havege.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/md2.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/md4.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/memory_buffer_alloc.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/net.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/padlock.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/pbkdf2.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/pkcs11.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/pkcs12.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/pkcs5.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/pkparse.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/platform.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/ripemd160.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/ssl_cache.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/ssl_ciphersuites.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/ssl_cli.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/ssl_srv.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/ssl_tls.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/threading.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/timing.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/version.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/version_features.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/x509.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/x509_create.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/x509_crl.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/x509_crt.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/x509_csr.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/x509write_crt.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/x509write_csr.c>", - "+<" + SDK_DIR +"/component/common/network/ssl/polarssl-1.3.8/library/xtea.c>", - # network polarssl ssl_ram_map - "+<" + SDK_DIR +"/component/common/network/ssl/ssl_ram_map/ssl_ram_map.c>", - # os freertos portable - "+<" + SDK_DIR +"/component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_5.c>", - "+<" + SDK_DIR +"/component/os/freertos/freertos_v8.1.2/Source/portable/GCC/ARM_CM4F/port.c>", - # "+<" + SDK_DIR +"/component/os/freertos/freertos_v8.1.2/Source/portable/IAR/ARM_CM4F/portasm.s>", - # os freertos - "+<" + SDK_DIR +"/component/os/freertos/cmsis_os.c>", - "+<" + SDK_DIR +"/component/os/freertos/freertos_v8.1.2/Source/croutine.c>", - "+<" + SDK_DIR +"/component/os/freertos/freertos_v8.1.2/Source/event_groups.c>", - "+<" + SDK_DIR +"/component/os/freertos/freertos_service.c>", - "+<" + SDK_DIR +"/component/os/freertos/freertos_v8.1.2/Source/list.c>", - "+<" + SDK_DIR +"/component/os/freertos/freertos_v8.1.2/Source/queue.c>", - "+<" + SDK_DIR +"/component/os/freertos/freertos_v8.1.2/Source/tasks.c>", - "+<" + SDK_DIR +"/component/os/freertos/freertos_v8.1.2/Source/timers.c>", - # os osdep - "+<" + SDK_DIR +"/component/os/os_dep/device_lock.c>", - "+<" + SDK_DIR +"/component/os/os_dep/osdep_service.c>", - # peripheral api - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/analogin_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/dma_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/efuse_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/flash_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/gpio_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/gpio_irq_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/i2c_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/i2s_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/nfc_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/pinmap.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/pinmap_common.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/port_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/pwmout_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/rtc_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/serial_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/sleep.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/spi_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/sys_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/timer_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/us_ticker.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/us_ticker_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/wait_api.c>", - "+<" + SDK_DIR +"/component/common/mbed/targets/hal/rtl8711b/wdt_api.c>", - # peripheral rtl8710b - "+<" + SDK_DIR +"/component/soc/realtek/8711b/fwlib/ram_lib/rtl8710b_dsleepcfg.c>", - "+<" + SDK_DIR +"/component/soc/realtek/8711b/fwlib/ram_lib/rtl8710b_dstandbycfg.c>", - "+<" + SDK_DIR +"/component/soc/realtek/8711b/fwlib/ram_lib/rtl8710b_intfcfg.c>", - "+<" + SDK_DIR +"/component/soc/realtek/8711b/misc/rtl8710b_ota.c>", - "+<" + SDK_DIR +"/component/soc/realtek/8711b/fwlib/ram_lib/rtl8710b_pinmapcfg.c>", - "+<" + SDK_DIR +"/component/soc/realtek/8711b/fwlib/ram_lib/rtl8710b_sleepcfg.c>", - # network - httpc - "+<" + SDK_DIR +"/component/common/network/httpc/httpc_tls.c>", - # network - httpd - "+<" + SDK_DIR +"/component/common/network/httpd/httpd_tls.c>", - # utilities - "+<" + SDK_DIR +"/component/common/utilities/cJSON.c>", - "+<" + SDK_DIR +"/component/common/utilities/http_client.c>", - "+<" + SDK_DIR +"/component/common/utilities/uart_socket.c>", - "+<" + SDK_DIR +"/component/common/utilities/xml.c>", - # fmt: on -] +env.AddLibrary( + name="ambz_sdk", + base_dir="$SDK_DIR", + srcs=[ + "+", + # NOTE: a fixup is used instead, to remove default main() + # "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + # "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + ], + includes=[ + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + ], +) -# Fixups -env.Append(CPPPATH=[FIXUPS_DIR]) -sources += [ - "+<" + FIXUPS_DIR + "/app_start_patch.c>", - "+<" + FIXUPS_DIR + "/cmsis_ipsr.c>", - "+<" + FIXUPS_DIR + "/log_uart.c>", - "+<" + FIXUPS_DIR + "/wifi_mode.c>", -] +# Sources - platform fixups +env.AddLibrary( + name="ambz_fixups", + base_dir="$FIXUPS_DIR", + srcs=[ + "+", + "+", + "+", + "+", + ], +) # Libs & linker config env.Append( LIBPATH=[ # fmt: off - join(SDK_DIR, "component", "soc", "realtek", "8711b", "misc", "bsp", "lib", "common", "GCC"), + join("$SDK_DIR", "component", "soc", "realtek", "8711b", "misc", "bsp", "lib", "common", "GCC"), # fmt: on - # linker script path - join(PLATFORM_DIR, "ld"), ], LIBS=[ "_platform", @@ -410,7 +353,7 @@ env.Append( ) env.Replace( LDSCRIPT_PATH=[ - join(PLATFORM_DIR, "ld", ldscript), + join("$LD_DIR", "$LDSCRIPT_SDK"), ], ) @@ -552,8 +495,8 @@ actions.append(env.VerboseAction(pick_tool, "Wrapping binary images")) actions.append(env.VerboseAction(concat_xip_ram, "Packaging firmware image - $IMG_FW")) # actions.append(env.VerboseAction(checksum_img, "Generating checksum")) actions.append(env.VerboseAction(package_ota, "Packaging OTA image - $IMG_OTA")) -actions.append(env.VerboseAction("true", f"- OTA1 flash offset: {flash_ota1_offset}")) -actions.append(env.VerboseAction("true", f"- OTA2 flash offset: {flash_ota2_offset}")) +actions.append(env.VerboseAction("true", f"- OTA1 flash offset: $FLASH_OTA1_OFFSET")) +actions.append(env.VerboseAction("true", f"- OTA2 flash offset: $FLASH_OTA2_OFFSET")) # Uploader upload_protocol = env.subst("$UPLOAD_PROTOCOL") @@ -562,7 +505,7 @@ upload_actions = [] # from platform-espressif32/builder/main.py if upload_protocol == "uart": env.Replace( - UPLOADER=join(platform.get_dir(), "tools", "rtltool.py"), + UPLOADER=join("$TOOLS_DIR", "rtltool.py"), UPLOADERFLAGS=[ "--port", "$UPLOAD_PORT", @@ -580,23 +523,18 @@ elif upload_protocol == "custom": else: sys.stderr.write("Warning! Unknown upload protocol %s\n" % upload_protocol) -# Clone env to ignore options from child projects -envsdk = env.Clone() - -# SDK library target -target_sdk = envsdk.BuildLibrary( - join("$BUILD_DIR", "ambz_sdk"), - SDK_DIR, - sources, -) +# Bootloader library target_boot = env.StaticLibrary( join("$BUILD_DIR", "boot_all"), env.BinToObj( join("$BUILD_DIR", "boot_all.o"), - join(PLATFORM_DIR, "bin", boot_all), + join("$BIN_DIR", boot_all), ), ) -env.Prepend(LIBS=[target_sdk, target_boot]) +env.Prepend(LIBS=[target_boot]) + +# Build all libraries +env.BuildLibraries() # Main firmware binary builder env.Append( diff --git a/builder/main.py b/builder/main.py index 8a1c4a6..f07ed14 100644 --- a/builder/main.py +++ b/builder/main.py @@ -1,8 +1,13 @@ +# Copyright (c) Kuba Szczodrzyński 2022-04-20. + from SCons.Script import Default, DefaultEnvironment env = DefaultEnvironment() board = env.BoardConfig() +# Utilities +env.SConscript("utils.py", exports="env") + # Firmware name if env.get("PROGNAME", "program") == "program": env.Replace(PROGNAME="firmware") @@ -36,6 +41,25 @@ if flash_layout: env.Append(CPPDEFINES=defines.items()) env.Replace(**defines) +# Platform builders details: +# - call env.AddDefaults("platform name", "sdk name") to add dir paths +# - call env.AddLibrary("lib name", "base dir", [sources]) to add lib sources +# - output main firmware image binary as $IMG_FW +# - call env.BuildLibraries() to build lib targets with safe envs +# - script code ordering: +# - global vars +# - # Outputs +# - # Tools +# - # Flags (C(XX)FLAGS / CPPDEFINES / LINKFLAGS) +# - sources (env.AddLibrary) +# - # Libs & linker config (LIBPATH / LIBS / LDSCRIPT_PATH) +# - # Misc options +# - # Image conversion (tools, functions, builders, actions, etc.) +# - # Uploader +# - # Library targets +# - env.BuildLibraries() +# - # Main firmware binary builder + target_elf = env.BuildProgram() target_fw = env.DumpFirmwareBinary("$IMG_FW", target_elf) env.AddPlatformTarget("upload", target_fw, env["UPLOAD_ACTIONS"], "Upload") diff --git a/builder/utils.py b/builder/utils.py new file mode 100644 index 0000000..dca3cb9 --- /dev/null +++ b/builder/utils.py @@ -0,0 +1,118 @@ +# Copyright (c) Kuba Szczodrzyński 2022-05-04. + +import fnmatch +from glob import glob +from os.path import isdir, join +from typing import List + +from SCons.Script import DefaultEnvironment + +env = DefaultEnvironment() +platform = env.PioPlatform() +board = env.BoardConfig() + + +def env_add_defaults(env, platform_name: str, sdk_name: str): + vars = dict( + SDK_DIR=platform.get_package_dir(sdk_name), + LT_DIR=platform.get_dir(), + # Root dirs + BOARD_DIR=join("${LT_DIR}", "boards", "${VARIANT}"), + ARDUINO_DIR=join("${LT_DIR}", "arduino", platform_name), + PLATFORM_DIR=join("${LT_DIR}", "platform", platform_name), + TOOLS_DIR=join("${LT_DIR}", "tools"), + # Platform-specific dirs + BIN_DIR=join("${PLATFORM_DIR}", "bin"), + FIXUPS_DIR=join("${PLATFORM_DIR}", "fixups"), + LD_DIR=join("${PLATFORM_DIR}", "ld"), + OPENOCD_DIR=join("${PLATFORM_DIR}", "openocd"), + # Board config variables + MCU=board.get("build.mcu").upper(), + FAMILY=board.get("build.family").upper(), + VARIANT=board.get("build.variant"), + LDSCRIPT_SDK=board.get("build.ldscript_sdk"), + LDSCRIPT_ARDUINO=board.get("build.ldscript_arduino"), + ) + env.Replace(**vars) + for k, v in vars.items(): + if "DIR" in k: + assert isdir(env.subst(v)), f"{env.subst(v)} is not a directory" + env.Prepend( + CPPPATH=[ + "$BOARD_DIR", + "$FIXUPS_DIR", + ], + LIBPATH=[ + "$LD_DIR", + ], + CPPDEFINES=[ + ("LT_VERSION", platform.version), + ("LT_BOARD", board.get("build.variant")), + ], + ) + + +def env_add_library( + env, + name: str, + base_dir: str, + srcs: List[str], + includes: List[str] = [], +): + name = env.subst(name) + # add base dir to all source globs + sources = [] + for src in srcs: + if src[1] != "<" or src[-1] != ">": + raise ValueError(f"Not a source glob: {src}") + + if src[2] == "$": # do not append base path + expr = src[2:-1] + else: + expr = join(base_dir, src[2:-1]) + + sources.append(src[0] + "<" + expr + ">") + # queue library for further env clone and build + env.Prepend(LIBQUEUE=[[join("$BUILD_DIR", name), base_dir, sources]]) + + base_dir = env.subst(base_dir) # expand base_dir for includes + for dir in includes: + if dir[1] != "<" or dir[-1] != ">": + env.Append(CPPPATH=[dir]) + continue + + if dir[2] == "$": # do not append base path + expr = dir[2:-1] + else: + expr = join(base_dir, dir[2:-1]) + expr = env.subst(expr) + + if dir[0] == "-": + for item in fnmatch.filter(env["CPPPATH"], expr): + if item in env["CPPPATH"]: + env["CPPPATH"].remove(item) + else: + for item in glob(expr, recursive=True): + if not isdir(item): + continue + if dir[0] == "!": + env.Prepend(CPPPATH=[item]) + else: + env.Append(CPPPATH=[item]) + + +def env_build_libraries(env, safe: bool = True): + # add lib targets and clone safe envs + if not "LIBQUEUE" in env: + return + queue = env["LIBQUEUE"] + env["LIBQUEUE"] = [] + envsafe = env.Clone() if safe else env + for lib in queue: + target = envsafe.BuildLibrary(*lib) + env.Prepend(LIBS=[target]) + + +env.AddMethod(env_add_defaults, "AddDefaults") +env.AddMethod(env_add_library, "AddLibrary") +env.AddMethod(env_build_libraries, "BuildLibraries") diff --git a/platform.py b/platform.py index ba80554..bbdc366 100644 --- a/platform.py +++ b/platform.py @@ -1,3 +1,5 @@ +# Copyright (c) Kuba Szczodrzyński 2022-04-20. + from os.path import dirname from platformio.debug.config.base import DebugConfigBase