smartconfig_ack.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2010-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. * After station connects to AP and gets IP address by smartconfig,
  16. * it will use UDP to send 'ACK' to cellphone.
  17. */
  18. #include <string.h>
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "lwip/sockets.h"
  22. #include "tcpip_adapter.h"
  23. #include "esp_log.h"
  24. #include "esp_wifi.h"
  25. #include "esp_smartconfig.h"
  26. #include "smartconfig_ack.h"
  27. static const char *TAG = "smartconfig";
  28. /* Flag to indicate sending smartconfig ACK or not. */
  29. static bool s_sc_ack_send = false;
  30. static int sc_ack_send_get_errno(int fd)
  31. {
  32. int sock_errno = 0;
  33. u32_t optlen = sizeof(sock_errno);
  34. getsockopt(fd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
  35. return sock_errno;
  36. }
  37. static void sc_ack_send_task(void *pvParameters)
  38. {
  39. sc_ack_t *ack = (sc_ack_t *)pvParameters;
  40. tcpip_adapter_ip_info_t local_ip;
  41. uint8_t remote_ip[4];
  42. memcpy(remote_ip, ack->ctx.ip, sizeof(remote_ip));
  43. int remote_port = (ack->type == SC_ACK_TYPE_ESPTOUCH) ? SC_ACK_TOUCH_SERVER_PORT : SC_ACK_AIRKISS_SERVER_PORT;
  44. struct sockaddr_in server_addr;
  45. socklen_t sin_size = sizeof(server_addr);
  46. int send_sock = -1;
  47. int optval = 1;
  48. int sendlen;
  49. int ack_len = (ack->type == SC_ACK_TYPE_ESPTOUCH) ? SC_ACK_TOUCH_LEN : SC_ACK_AIRKISS_LEN;
  50. uint8_t packet_count = 1;
  51. int err;
  52. int ret;
  53. bzero(&server_addr, sizeof(struct sockaddr_in));
  54. server_addr.sin_family = AF_INET;
  55. memcpy(&server_addr.sin_addr.s_addr, remote_ip, sizeof(remote_ip));
  56. server_addr.sin_port = htons(remote_port);
  57. esp_wifi_get_mac(WIFI_IF_STA, ack->ctx.mac);
  58. vTaskDelay(200 / portTICK_RATE_MS);
  59. while (s_sc_ack_send) {
  60. /* Get local IP address of station */
  61. ret = tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &local_ip);
  62. if ((ESP_OK == ret) && (local_ip.ip.addr != INADDR_ANY)) {
  63. /* If ESP touch, smartconfig ACK contains local IP address. */
  64. if (ack->type == SC_ACK_TYPE_ESPTOUCH) {
  65. memcpy(ack->ctx.ip, &local_ip.ip.addr, 4);
  66. }
  67. /* Create UDP socket. */
  68. send_sock = socket(AF_INET, SOCK_DGRAM, 0);
  69. if ((send_sock < LWIP_SOCKET_OFFSET) || (send_sock > (FD_SETSIZE - 1))) {
  70. ESP_LOGE(TAG, "Creat udp socket failed");
  71. goto _end;
  72. }
  73. setsockopt(send_sock, SOL_SOCKET, SO_BROADCAST | SO_REUSEADDR, &optval, sizeof(int));
  74. if (ack->type == SC_ACK_TYPE_AIRKISS) {
  75. char data = 0;
  76. struct sockaddr_in local_addr, from;
  77. socklen_t sockadd_len = sizeof(struct sockaddr);
  78. struct timeval timeout = {
  79. SC_ACK_AIRKISS_TIMEOUT / 1000,
  80. SC_ACK_AIRKISS_TIMEOUT % 1000 * 1000
  81. };
  82. bzero(&local_addr, sizeof(struct sockaddr_in));
  83. bzero(&from, sizeof(struct sockaddr_in));
  84. local_addr.sin_family = AF_INET;
  85. local_addr.sin_addr.s_addr = INADDR_ANY;
  86. local_addr.sin_port = htons(SC_ACK_AIRKISS_DEVICE_PORT);
  87. bind(send_sock, (struct sockaddr *)&local_addr, sockadd_len);
  88. setsockopt(send_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  89. recvfrom(send_sock, &data, 1, 0, (struct sockaddr *)&from, &sockadd_len);
  90. if (from.sin_addr.s_addr != INADDR_ANY) {
  91. memcpy(remote_ip, &from.sin_addr, 4);
  92. server_addr.sin_addr.s_addr = from.sin_addr.s_addr;
  93. } else {
  94. goto _end;
  95. }
  96. }
  97. while (s_sc_ack_send) {
  98. /* Send smartconfig ACK every 100ms. */
  99. vTaskDelay(100 / portTICK_RATE_MS);
  100. sendlen = sendto(send_sock, &ack->ctx, ack_len, 0, (struct sockaddr*) &server_addr, sin_size);
  101. if (sendlen <= 0) {
  102. err = sc_ack_send_get_errno(send_sock);
  103. ESP_LOGD(TAG, "send failed, errno %d", err);
  104. vTaskDelay(100 / portTICK_RATE_MS);
  105. }
  106. /* Send 30 smartconfig ACKs. Then smartconfig is successful. */
  107. if (packet_count++ >= SC_ACK_MAX_COUNT) {
  108. if (ack->link_flag) {
  109. *ack->link_flag = 1;
  110. }
  111. if (ack->cb) {
  112. ack->cb(SC_STATUS_LINK_OVER, remote_ip);
  113. }
  114. goto _end;
  115. }
  116. }
  117. }
  118. else {
  119. vTaskDelay((portTickType)(100 / portTICK_RATE_MS));
  120. }
  121. }
  122. _end:
  123. if ((send_sock >= LWIP_SOCKET_OFFSET) && (send_sock <= (FD_SETSIZE - 1))) {
  124. close(send_sock);
  125. }
  126. free(ack);
  127. vTaskDelete(NULL);
  128. }
  129. void sc_ack_send(sc_ack_t *param)
  130. {
  131. sc_ack_t *ack = NULL;
  132. if (param == NULL) {
  133. ESP_LOGE(TAG, "Smart config ack parameter error");
  134. return;
  135. }
  136. ack = malloc(sizeof(sc_ack_t));
  137. if (ack == NULL) {
  138. ESP_LOGE(TAG, "Smart config ack parameter malloc fail");
  139. return;
  140. }
  141. memcpy(ack, param, sizeof(sc_ack_t));
  142. s_sc_ack_send = true;
  143. if (xTaskCreate(sc_ack_send_task, "sc_ack_send_task", SC_ACK_TASK_STACK_SIZE, ack, SC_ACK_TASK_PRIORITY, NULL) != pdPASS) {
  144. ESP_LOGE(TAG, "Create sending smartconfig ACK task fail");
  145. free(ack);
  146. }
  147. }
  148. void sc_ack_send_stop(void)
  149. {
  150. s_sc_ack_send = false;
  151. }