2021-11-10 01:18:00 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-09-13 04:42:45 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* After station connects to AP and gets IP address by smartconfig,
|
|
|
|
* it will use UDP to send 'ACK' to cellphone.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2019-09-01 12:25:23 -04:00
|
|
|
#include "esp_netif.h"
|
2017-09-13 04:42:45 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_wifi.h"
|
2019-04-16 23:44:51 -04:00
|
|
|
#include "esp_event.h"
|
2020-09-11 18:37:28 -04:00
|
|
|
|
|
|
|
#if CONFIG_ESP_NETIF_TCPIP_LWIP
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "lwip/sockets.h"
|
2017-09-13 04:42:45 -04:00
|
|
|
#include "esp_smartconfig.h"
|
2018-04-09 02:29:53 -04:00
|
|
|
#include "smartconfig_ack.h"
|
2017-09-13 04:42:45 -04:00
|
|
|
|
2019-04-16 23:44:51 -04:00
|
|
|
#define SC_ACK_TASK_PRIORITY 2 /*!< Priority of sending smartconfig ACK task */
|
|
|
|
#define SC_ACK_TASK_STACK_SIZE 2048 /*!< Stack size of sending smartconfig ACK task */
|
|
|
|
|
2020-08-17 22:34:29 -04:00
|
|
|
#define SC_ACK_TOUCH_DEVICE_PORT 7001 /*!< ESPTouch UDP port of server on device */
|
|
|
|
#define SC_ACK_TOUCH_SERVER_PORT 18266 /*!< ESPTouch UDP port of server on cellphone */
|
|
|
|
#define SC_ACK_TOUCH_V2_SERVER_PORT(i) (18266+i*10000) /*!< ESPTouch v2 UDP port of server on cellphone */
|
2019-04-16 23:44:51 -04:00
|
|
|
#define SC_ACK_AIRKISS_SERVER_PORT 10000 /*!< Airkiss UDP port of server on cellphone */
|
2020-01-15 22:54:02 -05:00
|
|
|
#define SC_ACK_AIRKISS_DEVICE_PORT 10001 /*!< Airkiss UDP port of server on device */
|
|
|
|
#define SC_ACK_AIRKISS_TIMEOUT 1500 /*!< Airkiss read data timout millisecond */
|
2019-04-16 23:44:51 -04:00
|
|
|
|
2020-08-17 22:34:29 -04:00
|
|
|
#define SC_ACK_TOUCH_LEN 11 /*!< Length of ESPTouch ACK context */
|
2019-04-16 23:44:51 -04:00
|
|
|
#define SC_ACK_AIRKISS_LEN 7 /*!< Length of Airkiss ACK context */
|
|
|
|
|
|
|
|
#define SC_ACK_MAX_COUNT 30 /*!< Maximum count of sending smartconfig ACK */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Smartconfig parameters passed to sc_ack_send call.
|
|
|
|
*/
|
|
|
|
typedef struct sc_ack {
|
|
|
|
smartconfig_type_t type; /*!< Smartconfig type(ESPTouch or AirKiss) */
|
|
|
|
struct {
|
|
|
|
uint8_t token; /*!< Smartconfig token from the cellphone */
|
|
|
|
uint8_t mac[6]; /*!< MAC address of station */
|
|
|
|
uint8_t ip[4]; /*!< IP address of cellphone */
|
|
|
|
} ctx;
|
|
|
|
} sc_ack_t;
|
|
|
|
|
2017-09-13 04:42:45 -04:00
|
|
|
static const char *TAG = "smartconfig";
|
|
|
|
|
|
|
|
/* Flag to indicate sending smartconfig ACK or not. */
|
|
|
|
static bool s_sc_ack_send = false;
|
|
|
|
|
|
|
|
static int sc_ack_send_get_errno(int fd)
|
|
|
|
{
|
|
|
|
int sock_errno = 0;
|
|
|
|
u32_t optlen = sizeof(sock_errno);
|
|
|
|
|
2021-11-10 01:18:00 -05:00
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen) < 0) {
|
|
|
|
return sock_errno;
|
|
|
|
}
|
2017-09-13 04:42:45 -04:00
|
|
|
|
2021-11-10 01:18:00 -05:00
|
|
|
return 0;
|
2017-09-13 04:42:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sc_ack_send_task(void *pvParameters)
|
|
|
|
{
|
|
|
|
sc_ack_t *ack = (sc_ack_t *)pvParameters;
|
2019-09-01 12:25:23 -04:00
|
|
|
esp_netif_ip_info_t local_ip;
|
2018-07-03 06:39:49 -04:00
|
|
|
uint8_t remote_ip[4];
|
|
|
|
memcpy(remote_ip, ack->ctx.ip, sizeof(remote_ip));
|
2017-09-13 04:42:45 -04:00
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
socklen_t sin_size = sizeof(server_addr);
|
2018-04-09 02:29:53 -04:00
|
|
|
int send_sock = -1;
|
2017-09-13 04:42:45 -04:00
|
|
|
int optval = 1;
|
|
|
|
int sendlen;
|
2019-04-16 23:44:51 -04:00
|
|
|
int ack_len = (ack->type == SC_TYPE_ESPTOUCH) ? SC_ACK_TOUCH_LEN : SC_ACK_AIRKISS_LEN;
|
2017-09-13 04:42:45 -04:00
|
|
|
uint8_t packet_count = 1;
|
|
|
|
int err;
|
|
|
|
int ret;
|
|
|
|
|
2020-08-17 22:34:29 -04:00
|
|
|
int remote_port = 0;
|
|
|
|
if (ack->type == SC_TYPE_ESPTOUCH) {
|
|
|
|
remote_port = SC_ACK_TOUCH_SERVER_PORT;
|
|
|
|
} else if (ack->type == SC_TYPE_ESPTOUCH_V2) {
|
|
|
|
uint8_t port_bit = ack->ctx.token;
|
|
|
|
if(port_bit > 3) {
|
|
|
|
port_bit = 0;
|
|
|
|
}
|
|
|
|
remote_port = SC_ACK_TOUCH_V2_SERVER_PORT(port_bit);
|
|
|
|
} else {
|
|
|
|
remote_port = SC_ACK_AIRKISS_SERVER_PORT;
|
|
|
|
}
|
|
|
|
|
2017-09-13 04:42:45 -04:00
|
|
|
bzero(&server_addr, sizeof(struct sockaddr_in));
|
|
|
|
server_addr.sin_family = AF_INET;
|
2019-09-10 22:42:16 -04:00
|
|
|
memcpy(&server_addr.sin_addr.s_addr, remote_ip, sizeof(remote_ip));
|
2017-09-13 04:42:45 -04:00
|
|
|
server_addr.sin_port = htons(remote_port);
|
|
|
|
|
|
|
|
esp_wifi_get_mac(WIFI_IF_STA, ack->ctx.mac);
|
|
|
|
|
|
|
|
vTaskDelay(200 / portTICK_RATE_MS);
|
|
|
|
|
|
|
|
while (s_sc_ack_send) {
|
|
|
|
/* Get local IP address of station */
|
2019-09-01 12:25:23 -04:00
|
|
|
ret = esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &local_ip);
|
2017-09-13 04:42:45 -04:00
|
|
|
if ((ESP_OK == ret) && (local_ip.ip.addr != INADDR_ANY)) {
|
2020-08-17 22:34:29 -04:00
|
|
|
/* If ESPTouch, smartconfig ACK contains local IP address. */
|
2019-04-16 23:44:51 -04:00
|
|
|
if (ack->type == SC_TYPE_ESPTOUCH) {
|
2017-09-13 04:42:45 -04:00
|
|
|
memcpy(ack->ctx.ip, &local_ip.ip.addr, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create UDP socket. */
|
|
|
|
send_sock = socket(AF_INET, SOCK_DGRAM, 0);
|
2018-04-09 02:29:53 -04:00
|
|
|
if ((send_sock < LWIP_SOCKET_OFFSET) || (send_sock > (FD_SETSIZE - 1))) {
|
2017-09-13 04:42:45 -04:00
|
|
|
ESP_LOGE(TAG, "Creat udp socket failed");
|
2020-11-10 02:40:01 -05:00
|
|
|
goto _end;
|
2017-09-13 04:42:45 -04:00
|
|
|
}
|
|
|
|
|
2021-11-10 01:18:00 -05:00
|
|
|
if (setsockopt(send_sock, SOL_SOCKET, SO_BROADCAST | SO_REUSEADDR, &optval, sizeof(int)) < 0) {
|
|
|
|
ESP_LOGE(TAG, "setsockopt failed");
|
|
|
|
goto _end;
|
|
|
|
}
|
2017-09-13 04:42:45 -04:00
|
|
|
|
2020-01-15 22:54:02 -05:00
|
|
|
if (ack->type == SC_TYPE_AIRKISS) {
|
|
|
|
char data = 0;
|
|
|
|
struct sockaddr_in local_addr, from;
|
|
|
|
socklen_t sockadd_len = sizeof(struct sockaddr);
|
|
|
|
struct timeval timeout = {
|
|
|
|
SC_ACK_AIRKISS_TIMEOUT / 1000,
|
|
|
|
SC_ACK_AIRKISS_TIMEOUT % 1000 * 1000
|
|
|
|
};
|
|
|
|
|
|
|
|
bzero(&local_addr, sizeof(struct sockaddr_in));
|
|
|
|
bzero(&from, sizeof(struct sockaddr_in));
|
|
|
|
local_addr.sin_family = AF_INET;
|
|
|
|
local_addr.sin_addr.s_addr = INADDR_ANY;
|
2020-08-17 22:34:29 -04:00
|
|
|
if (ack->type == SC_TYPE_AIRKISS) {
|
|
|
|
local_addr.sin_port = htons(SC_ACK_AIRKISS_DEVICE_PORT);
|
|
|
|
} else {
|
|
|
|
local_addr.sin_port = htons(SC_ACK_TOUCH_DEVICE_PORT);
|
|
|
|
}
|
2020-01-15 22:54:02 -05:00
|
|
|
|
2021-11-10 01:18:00 -05:00
|
|
|
if (bind(send_sock, (struct sockaddr *)&local_addr, sockadd_len) < 0) {
|
|
|
|
ESP_LOGE(TAG, "socket bind failed");
|
|
|
|
goto _end;
|
|
|
|
}
|
|
|
|
if (setsockopt(send_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
|
|
|
|
goto _end;
|
|
|
|
}
|
2020-01-15 22:54:02 -05:00
|
|
|
|
2021-11-10 01:18:00 -05:00
|
|
|
if (recvfrom(send_sock, &data, 1, 0, (struct sockaddr *)&from, &sockadd_len) < 0) {
|
|
|
|
goto _end;
|
|
|
|
}
|
2020-01-15 22:54:02 -05:00
|
|
|
if (from.sin_addr.s_addr != INADDR_ANY) {
|
|
|
|
memcpy(remote_ip, &from.sin_addr, 4);
|
|
|
|
server_addr.sin_addr.s_addr = from.sin_addr.s_addr;
|
|
|
|
} else {
|
|
|
|
goto _end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 04:42:45 -04:00
|
|
|
while (s_sc_ack_send) {
|
|
|
|
/* Send smartconfig ACK every 100ms. */
|
|
|
|
vTaskDelay(100 / portTICK_RATE_MS);
|
|
|
|
|
|
|
|
sendlen = sendto(send_sock, &ack->ctx, ack_len, 0, (struct sockaddr*) &server_addr, sin_size);
|
2021-01-25 03:50:36 -05:00
|
|
|
if (sendlen <= 0) {
|
2017-09-13 04:42:45 -04:00
|
|
|
err = sc_ack_send_get_errno(send_sock);
|
2021-01-25 03:50:36 -05:00
|
|
|
ESP_LOGD(TAG, "send failed, errno %d", err);
|
|
|
|
vTaskDelay(100 / portTICK_RATE_MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send 30 smartconfig ACKs. Then smartconfig is successful. */
|
|
|
|
if (packet_count++ >= SC_ACK_MAX_COUNT) {
|
|
|
|
esp_event_post(SC_EVENT, SC_EVENT_SEND_ACK_DONE, NULL, 0, portMAX_DELAY);
|
2018-04-09 02:29:53 -04:00
|
|
|
goto _end;
|
2017-09-13 04:42:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vTaskDelay((portTickType)(100 / portTICK_RATE_MS));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 02:29:53 -04:00
|
|
|
_end:
|
2021-11-10 01:18:00 -05:00
|
|
|
close(send_sock);
|
2017-09-13 04:42:45 -04:00
|
|
|
free(ack);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:44:51 -04:00
|
|
|
esp_err_t sc_send_ack_start(smartconfig_type_t type, uint8_t token, uint8_t *cellphone_ip)
|
2017-09-13 04:42:45 -04:00
|
|
|
{
|
|
|
|
sc_ack_t *ack = NULL;
|
|
|
|
|
2019-04-16 23:44:51 -04:00
|
|
|
if (cellphone_ip == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Cellphone IP address is NULL");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
2017-09-13 04:42:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ack = malloc(sizeof(sc_ack_t));
|
|
|
|
if (ack == NULL) {
|
2019-04-16 23:44:51 -04:00
|
|
|
ESP_LOGE(TAG, "ACK parameter malloc fail");
|
|
|
|
return ESP_ERR_NO_MEM;
|
2017-09-13 04:42:45 -04:00
|
|
|
}
|
2019-04-16 23:44:51 -04:00
|
|
|
ack->type = type;
|
|
|
|
ack->ctx.token = token;
|
|
|
|
memcpy(ack->ctx.ip, cellphone_ip, 4);
|
2017-09-13 04:42:45 -04:00
|
|
|
|
|
|
|
s_sc_ack_send = true;
|
|
|
|
|
2018-04-09 02:29:53 -04:00
|
|
|
if (xTaskCreate(sc_ack_send_task, "sc_ack_send_task", SC_ACK_TASK_STACK_SIZE, ack, SC_ACK_TASK_PRIORITY, NULL) != pdPASS) {
|
|
|
|
ESP_LOGE(TAG, "Create sending smartconfig ACK task fail");
|
|
|
|
free(ack);
|
2019-04-16 23:44:51 -04:00
|
|
|
return ESP_ERR_NO_MEM;
|
2018-04-09 02:29:53 -04:00
|
|
|
}
|
2019-04-16 23:44:51 -04:00
|
|
|
|
|
|
|
return ESP_OK;
|
2017-09-13 04:42:45 -04:00
|
|
|
}
|
|
|
|
|
2019-04-16 23:44:51 -04:00
|
|
|
void sc_send_ack_stop(void)
|
2017-09-13 04:42:45 -04:00
|
|
|
{
|
|
|
|
s_sc_ack_send = false;
|
|
|
|
}
|
2020-09-11 18:37:28 -04:00
|
|
|
|
|
|
|
#endif
|