user_devicefind.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: user_devicefind.c
  5. *
  6. * Description: Find your hardware's information while working any mode.
  7. *
  8. * Modification history:
  9. * 2014/3/12, v1.0 create this file.
  10. *******************************************************************************/
  11. #include "ets_sys.h"
  12. #include "os_type.h"
  13. #include "osapi.h"
  14. #include "mem.h"
  15. #include "user_interface.h"
  16. #include "espconn.h"
  17. #include "user_json.h"
  18. #include "user_devicefind.h"
  19. const char *device_find_request = "Are You Espressif IOT Smart Device?";
  20. #if PLUG_DEVICE
  21. const char *device_find_response_ok = "I'm Plug.";
  22. #elif LIGHT_DEVICE
  23. const char *device_find_response_ok = "I'm Light.";
  24. #elif SENSOR_DEVICE
  25. #if HUMITURE_SUB_DEVICE
  26. const char *device_find_response_ok = "I'm Humiture.";
  27. #elif FLAMMABLE_GAS_SUB_DEVICE
  28. const char *device_find_response_ok = "I'm Flammable Gas.";
  29. #endif
  30. #endif
  31. /*---------------------------------------------------------------------------*/
  32. LOCAL struct espconn ptrespconn;
  33. /******************************************************************************
  34. * FunctionName : user_devicefind_recv
  35. * Description : Processing the received data from the host
  36. * Parameters : arg -- Additional argument to pass to the callback function
  37. * pusrdata -- The received data (or NULL when the connection has been closed!)
  38. * length -- The length of received data
  39. * Returns : none
  40. *******************************************************************************/
  41. LOCAL void ICACHE_FLASH_ATTR
  42. user_devicefind_recv(void *arg, char *pusrdata, unsigned short length)
  43. {
  44. char DeviceBuffer[40] = {0};
  45. char Device_mac_buffer[60] = {0};
  46. char hwaddr[6];
  47. remot_info *premot = NULL;
  48. struct ip_info ipconfig;
  49. if (wifi_get_opmode() != STATION_MODE) {
  50. wifi_get_ip_info(SOFTAP_IF, &ipconfig);
  51. wifi_get_macaddr(SOFTAP_IF, hwaddr);
  52. if (!ip_addr_netcmp((struct ip_addr *)ptrespconn.proto.udp->remote_ip, &ipconfig.ip, &ipconfig.netmask)) {
  53. wifi_get_ip_info(STATION_IF, &ipconfig);
  54. wifi_get_macaddr(STATION_IF, hwaddr);
  55. }
  56. } else {
  57. wifi_get_ip_info(STATION_IF, &ipconfig);
  58. wifi_get_macaddr(STATION_IF, hwaddr);
  59. }
  60. if (pusrdata == NULL) {
  61. return;
  62. }
  63. if (length == os_strlen(device_find_request) &&
  64. os_strncmp(pusrdata, device_find_request, os_strlen(device_find_request)) == 0) {
  65. os_sprintf(DeviceBuffer, "%s" MACSTR " " IPSTR, device_find_response_ok,
  66. MAC2STR(hwaddr), IP2STR(&ipconfig.ip));
  67. os_printf("%s\n", DeviceBuffer);
  68. length = os_strlen(DeviceBuffer);
  69. if (espconn_get_connection_info(&ptrespconn, &premot, 0) != ESPCONN_OK)
  70. return;
  71. os_memcpy(ptrespconn.proto.udp->remote_ip, premot->remote_ip, 4);
  72. ptrespconn.proto.udp->remote_port = premot->remote_port;
  73. espconn_sent(&ptrespconn, DeviceBuffer, length);
  74. } else if (length == (os_strlen(device_find_request) + 18)) {
  75. os_sprintf(Device_mac_buffer, "%s " MACSTR , device_find_request, MAC2STR(hwaddr));
  76. os_printf("%s", Device_mac_buffer);
  77. if (os_strncmp(Device_mac_buffer, pusrdata, os_strlen(device_find_request) + 18) == 0) {
  78. //os_printf("%s\n", Device_mac_buffer);
  79. length = os_strlen(DeviceBuffer);
  80. os_sprintf(DeviceBuffer, "%s" MACSTR " " IPSTR, device_find_response_ok,
  81. MAC2STR(hwaddr), IP2STR(&ipconfig.ip));
  82. os_printf("%s\n", DeviceBuffer);
  83. length = os_strlen(DeviceBuffer);
  84. if (espconn_get_connection_info(&ptrespconn, &premot, 0) != ESPCONN_OK)
  85. return;
  86. os_memcpy(ptrespconn.proto.udp->remote_ip, premot->remote_ip, 4);
  87. ptrespconn.proto.udp->remote_port = premot->remote_port;
  88. espconn_sent(&ptrespconn, DeviceBuffer, length);
  89. } else {
  90. return;
  91. }
  92. }
  93. }
  94. /******************************************************************************
  95. * FunctionName : user_devicefind_init
  96. * Description : the espconn struct parame init
  97. * Parameters : none
  98. * Returns : none
  99. *******************************************************************************/
  100. void ICACHE_FLASH_ATTR
  101. user_devicefind_init(void)
  102. {
  103. ptrespconn.type = ESPCONN_UDP;
  104. ptrespconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
  105. ptrespconn.proto.udp->local_port = 1025;
  106. espconn_regist_recvcb(&ptrespconn, user_devicefind_recv);
  107. espconn_create(&ptrespconn);
  108. }