diff options
author | Uros Majstorovic <majstor@majstor.org> | 2019-12-04 06:12:15 +0100 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2019-12-04 06:12:15 +0100 |
commit | 4c28fb525edc64fa16c6c3bf10929dca65d9c77c (patch) | |
tree | 2531b3bcc2d06c655f6eb9a6233696f5eadfa089 /code/esp32/components/eos/transport.c | |
parent | 31578e285a21a749a49e3ac146feb8b02fcc7b52 (diff) |
esp32 refactoring
Diffstat (limited to 'code/esp32/components/eos/transport.c')
-rwxr-xr-x | code/esp32/components/eos/transport.c | 196 |
1 files changed, 0 insertions, 196 deletions
diff --git a/code/esp32/components/eos/transport.c b/code/esp32/components/eos/transport.c deleted file mode 100755 index 4c056a8..0000000 --- a/code/esp32/components/eos/transport.c +++ /dev/null @@ -1,196 +0,0 @@ -/* -MIT License - -Copyright (c) 2017 Olof Astrand (Ebiroll) - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#include <string.h> -#include <stdint.h> - -#include <freertos/FreeRTOS.h> -#include <freertos/task.h> - -#include <esp_system.h> -#include <esp_event.h> -#include <esp_event_loop.h> -#include <esp_log.h> -#include <esp_err.h> -#include <esp_wifi.h> -#include <nvs_flash.h> - -#include <lwip/sockets.h> -#include <lwip/err.h> -#include <lwip/sockets.h> -#include <lwip/sys.h> -#include <lwip/netdb.h> -#include <lwip/dns.h> - -#include "eos.h" -#include "fe310.h" -#include "transport.h" -#include "modem.h" -#include "bq25895.h" -#include "drv2605l.h" - -static const char *TAG = "EOS"; -static int udp_sock = -1; -static TaskHandle_t receiver_task; - -static int t_open(void) { - struct sockaddr_in _myaddr; - - udp_sock = socket(PF_INET, SOCK_DGRAM, 0); - if (udp_sock < 0) return udp_sock; - - memset((char *)&_myaddr, 0, sizeof(_myaddr)); - _myaddr.sin_family = AF_INET; - _myaddr.sin_addr.s_addr = htonl(INADDR_ANY); - _myaddr.sin_port = htons(3000); - - int rv = bind(udp_sock, (struct sockaddr *)&_myaddr, sizeof(_myaddr)); - if (rv < 0) { - close(udp_sock); - return rv; - } - return EOS_OK; -} - -static void t_close(void) { - close(udp_sock); -} - -static ssize_t t_send(void *msg, size_t msg_size, EOSNetAddr *addr) { - struct sockaddr_in servaddr; - - memset((void *)&servaddr, 0, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - servaddr.sin_port = addr->port; - memcpy((void *)&servaddr.sin_addr, addr->host, sizeof(addr->host)); - return sendto(udp_sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); -} - -static ssize_t t_recv(void *msg, size_t msg_size, EOSNetAddr *addr) { - struct sockaddr_in servaddr; - socklen_t addrlen = sizeof(servaddr); - memset((void *)&servaddr, 0, sizeof(servaddr)); - - ssize_t recvlen = recvfrom(udp_sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, &addrlen); - if (recvlen < 0) return recvlen; - - if (addr) { - addr->port = servaddr.sin_port; - memcpy(addr->host, (void *)&servaddr.sin_addr, sizeof(addr->host)); - } - return recvlen; -} - -static void receiver(void *pvParameters) { - EOSNetAddr addr; - size_t addr_len = sizeof(addr.host) + sizeof(addr.port); - unsigned char buffer[2048]; - - for (;;) { - ssize_t rv = t_recv(buffer+addr_len, sizeof(buffer)-addr_len, &addr); - if (rv < 0) { - ESP_LOGI(TAG, "UDP RECV ERR:%d", rv); - continue; - } - memcpy(buffer, addr.host, sizeof(addr.host)); - memcpy(buffer+sizeof(addr.host), &addr.port, sizeof(addr.port)); - eos_fe310_send(EOS_FE310_CMD_WIFI_PKT, buffer, rv+addr_len); - } -} - -static esp_err_t esp32_wifi_event_handler(void *ctx, system_event_t *event) { - switch(event->event_id) { - case SYSTEM_EVENT_WIFI_READY: - break; - - case SYSTEM_EVENT_AP_STACONNECTED: - break; - - case SYSTEM_EVENT_AP_START: - break; - - case SYSTEM_EVENT_SCAN_DONE: - break; - - case SYSTEM_EVENT_STA_CONNECTED: - break; - - case SYSTEM_EVENT_STA_DISCONNECTED: - ESP_LOGI(TAG, "DISCONNECT"); - vTaskDelete(receiver_task); - t_close(); - break; - - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "********************************************"); - ESP_LOGI(TAG, "* We are now connected to AP"); - ESP_LOGI(TAG, "* - Our IP address is: " IPSTR, IP2STR(&event->event_info.got_ip.ip_info.ip)); - ESP_LOGI(TAG, "********************************************"); - t_open(); - xTaskCreate(&receiver, "wifi_receiver", 4096, NULL, EOS_PRIORITY_WIFI, &receiver_task); - // xTaskCreatePinnedToCore(&receiver, "wifi_receiver", 4096, NULL, EOS_PRIORITY_WIFI, &receiver_task, 1); - eos_fe310_send(EOS_FE310_CMD_WIFI_CONNECT, NULL, 0); - // eos_drv2605l_test(); - break; - - default: // Ignore the other event types - break; - } - - return ESP_OK; -} - -void eos_wifi_init(void) { - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - wifi_config_t wifi_config; - - memset(&wifi_config, 0, sizeof(wifi_config)); - tcpip_adapter_init(); - // ESP_ERROR_CHECK( nvs_flash_init() ); - ESP_ERROR_CHECK( esp_event_loop_init(esp32_wifi_event_handler, NULL) ); - ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); - ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); - ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - -void eos_wifi_connect(char *ssid, char *password) { - wifi_config_t wifi_config; - - ESP_LOGI(TAG, "CONNECTING...."); - memset(&wifi_config, 0, sizeof(wifi_config)); - strncpy((char *)wifi_config.sta.ssid, ssid, 31); - strncpy((char *)wifi_config.sta.password, password, 63); - ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK( esp_wifi_connect() ); -} - -void eos_wifi_disconnect(void) { - ESP_ERROR_CHECK( esp_wifi_disconnect() ); -} - -ssize_t eos_wifi_send(void *msg, size_t msg_size, EOSNetAddr *addr) { - return t_send(msg, msg_size, addr); -} |