iot_softap.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2021 Huawei Device Co., Ltd.
  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. #include <unistd.h>
  16. #include "lwip/ip_addr.h"
  17. #include "lwip/netifapi.h"
  18. #include "wifi_hotspot.h"
  19. #include "wifi_hotspot_config.h"
  20. #include "iot_softap.h"
  21. #include "iot_demo_def.h"
  22. #define DEFAULT_AP_NAME "TestIotAP"
  23. static struct netif *g_netif = NULL;
  24. static void SetAddr(struct netif *pst_lwip_netif)
  25. {
  26. ip4_addr_t st_gw;
  27. ip4_addr_t st_ipaddr;
  28. ip4_addr_t st_netmask;
  29. IP4_ADDR(&st_ipaddr, 192, 168, 10, 1); // IP ADDR
  30. IP4_ADDR(&st_gw, 192, 168, 10, 1); // GET WAY ADDR
  31. IP4_ADDR(&st_netmask, 255, 255, 255, 0); // NET MASK CODE
  32. netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
  33. }
  34. static void ResetAddr(struct netif *pst_lwip_netif)
  35. {
  36. ip4_addr_t st_gw;
  37. ip4_addr_t st_ipaddr;
  38. ip4_addr_t st_netmask;
  39. IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
  40. IP4_ADDR(&st_gw, 0, 0, 0, 0);
  41. IP4_ADDR(&st_netmask, 0, 0, 0, 0);
  42. netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
  43. }
  44. int BOARD_SoftApStart(const char *ap_name)
  45. {
  46. HotspotConfig config = {0};
  47. ip4_addr_t st_gw;
  48. ip4_addr_t st_ipaddr;
  49. ip4_addr_t st_netmask;
  50. char *apName = ap_name;
  51. char ifname[BUFF_SIZE] = {0};
  52. int retval;
  53. if (IsHotspotActive() == WIFI_HOTSPOT_ACTIVE) {
  54. RaiseLog(LOG_LEVEL_ERR, "WIFI_HOTSPOT_ACTIVE \n");
  55. return -1;
  56. }
  57. if (apName == NULL) {
  58. apName = DEFAULT_AP_NAME;
  59. }
  60. if (strcpy_s(config.ssid, sizeof(config.ssid), apName) != 0) {
  61. RaiseLog(LOG_LEVEL_ERR, "[sample] strcpy ssid fail.\n");
  62. return -1;
  63. }
  64. config.securityType = WIFI_SEC_TYPE_OPEN;
  65. retval = SetHotspotConfig((const HotspotConfig *)(&config));
  66. if (retval != WIFI_SUCCESS) {
  67. RaiseLog(LOG_LEVEL_ERR, "SetHotspotConfig \n");
  68. return -1;
  69. }
  70. retval = EnableHotspot();
  71. if (retval != WIFI_SUCCESS) {
  72. RaiseLog(LOG_LEVEL_ERR, "EnableHotspot failed! \n");
  73. return -1;
  74. }
  75. g_netif = netifapi_netif_find("ap0");
  76. if (g_netif != NULL) {
  77. SetAddr(g_netif);
  78. dhcps_start(g_netif, NULL, 0);
  79. }
  80. return 0;
  81. }
  82. void BOARD_SoftApStop(void)
  83. {
  84. if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE) {
  85. RaiseLog(LOG_LEVEL_ERR, "WIFI_HOTSPOT_NOT_ACTIVE \n");
  86. return;
  87. }
  88. DisableHotspot();
  89. if (g_netif) {
  90. ResetAddr(g_netif);
  91. }
  92. }