10a7ce71fSopenharmony_ci/*
20a7ce71fSopenharmony_ci * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
30a7ce71fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40a7ce71fSopenharmony_ci * you may not use this file except in compliance with the License.
50a7ce71fSopenharmony_ci * You may obtain a copy of the License at
60a7ce71fSopenharmony_ci *
70a7ce71fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80a7ce71fSopenharmony_ci *
90a7ce71fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100a7ce71fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110a7ce71fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120a7ce71fSopenharmony_ci * See the License for the specific language governing permissions and
130a7ce71fSopenharmony_ci * limitations under the License.
140a7ce71fSopenharmony_ci */
150a7ce71fSopenharmony_ci
160a7ce71fSopenharmony_ci#include <hi_types_base.h>
170a7ce71fSopenharmony_ci#include <app_demo_get_mac_addr.h>
180a7ce71fSopenharmony_ci#include "hi_wifi_api.h"
190a7ce71fSopenharmony_ci#include "lwip/ip_addr.h"
200a7ce71fSopenharmony_ci#include "lwip/netifapi.h"
210a7ce71fSopenharmony_ci#include "wifi_softap.h"
220a7ce71fSopenharmony_ci
230a7ce71fSopenharmony_ci#define APP_INIT_VAP_NUM    2
240a7ce71fSopenharmony_ci#define APP_INIT_USR_NUM    2
250a7ce71fSopenharmony_ci
260a7ce71fSopenharmony_cistatic struct netif *g_lwip_netif = NULL;
270a7ce71fSopenharmony_ci
280a7ce71fSopenharmony_ci/* clear netif's ip, gateway and netmask */
290a7ce71fSopenharmony_civoid hi_softap_reset_addr(const struct netif *pst_lwip_netif)
300a7ce71fSopenharmony_ci{
310a7ce71fSopenharmony_ci    ip4_addr_t st_gw;
320a7ce71fSopenharmony_ci    ip4_addr_t st_ipaddr;
330a7ce71fSopenharmony_ci    ip4_addr_t st_netmask;
340a7ce71fSopenharmony_ci
350a7ce71fSopenharmony_ci    if (pst_lwip_netif == NULL) {
360a7ce71fSopenharmony_ci        printf("hisi_reset_addr::Null param of netdev\r\n");
370a7ce71fSopenharmony_ci        return;
380a7ce71fSopenharmony_ci    }
390a7ce71fSopenharmony_ci
400a7ce71fSopenharmony_ci    IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
410a7ce71fSopenharmony_ci    IP4_ADDR(&st_gw, 0, 0, 0, 0);
420a7ce71fSopenharmony_ci    IP4_ADDR(&st_netmask, 0, 0, 0, 0);
430a7ce71fSopenharmony_ci
440a7ce71fSopenharmony_ci    netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
450a7ce71fSopenharmony_ci}
460a7ce71fSopenharmony_ci
470a7ce71fSopenharmony_ciint hi_wifi_start_softap(void)
480a7ce71fSopenharmony_ci{
490a7ce71fSopenharmony_ci    int ret = 0;
500a7ce71fSopenharmony_ci    errno_t rc = 0;
510a7ce71fSopenharmony_ci    unsigned char *ssid = NULL;
520a7ce71fSopenharmony_ci
530a7ce71fSopenharmony_ci    char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
540a7ce71fSopenharmony_ci    int len = sizeof(ifname);
550a7ce71fSopenharmony_ci    hi_wifi_softap_config hapd_conf = {0};
560a7ce71fSopenharmony_ci    const unsigned char wifi_vap_res_num = APP_INIT_VAP_NUM;
570a7ce71fSopenharmony_ci    const unsigned char wifi_user_res_num = APP_INIT_USR_NUM;
580a7ce71fSopenharmony_ci    ip4_addr_t st_gw;
590a7ce71fSopenharmony_ci    ip4_addr_t st_ipaddr;
600a7ce71fSopenharmony_ci    ip4_addr_t st_netmask;
610a7ce71fSopenharmony_ci
620a7ce71fSopenharmony_ci    ret = hi_wifi_init(wifi_vap_res_num, wifi_user_res_num);
630a7ce71fSopenharmony_ci    if (ret != HISI_OK) {
640a7ce71fSopenharmony_ci        printf("hi_wifi_init\n");
650a7ce71fSopenharmony_ci        return -1;
660a7ce71fSopenharmony_ci    }
670a7ce71fSopenharmony_ci    /* get Hi3861 mac addr */
680a7ce71fSopenharmony_ci    hi3816_get_mac_addr();
690a7ce71fSopenharmony_ci    ssid = GetSsid();
700a7ce71fSopenharmony_ci    rc = memcpy_s(hapd_conf.ssid, HI_WIFI_MAX_SSID_LEN + 1, ssid, 20); /* 20:ssid length */
710a7ce71fSopenharmony_ci    if (rc != EOK) {
720a7ce71fSopenharmony_ci        return -1;
730a7ce71fSopenharmony_ci    }
740a7ce71fSopenharmony_ci
750a7ce71fSopenharmony_ci    hapd_conf.authmode = HI_WIFI_SECURITY_OPEN;
760a7ce71fSopenharmony_ci    hapd_conf.channel_num = 1;
770a7ce71fSopenharmony_ci
780a7ce71fSopenharmony_ci    ret = hi_wifi_softap_start(&hapd_conf, ifname, &len);
790a7ce71fSopenharmony_ci    if (ret != HISI_OK) {
800a7ce71fSopenharmony_ci        printf("hi_wifi_softap_start\n");
810a7ce71fSopenharmony_ci        return -1;
820a7ce71fSopenharmony_ci    }
830a7ce71fSopenharmony_ci
840a7ce71fSopenharmony_ci    /* acquire netif for IP operation */
850a7ce71fSopenharmony_ci    g_lwip_netif = netifapi_netif_find(ifname);
860a7ce71fSopenharmony_ci    if (g_lwip_netif == NULL) {
870a7ce71fSopenharmony_ci        printf("%s: get netif failed\n", __FUNCTION__);
880a7ce71fSopenharmony_ci        return -1;
890a7ce71fSopenharmony_ci    }
900a7ce71fSopenharmony_ci
910a7ce71fSopenharmony_ci    IP4_ADDR(&st_gw, 192, 168, 1, 1);          /* input your IP for example: 192.168.1.1 */
920a7ce71fSopenharmony_ci    IP4_ADDR(&st_ipaddr, 192, 168, 1, 1);      /* input your netmask for example: 192.168.1.1 */
930a7ce71fSopenharmony_ci    IP4_ADDR(&st_netmask, 255, 255, 255, 0);     /* input your gateway for example: 255.255.255.0 */
940a7ce71fSopenharmony_ci    netifapi_netif_set_addr(g_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
950a7ce71fSopenharmony_ci
960a7ce71fSopenharmony_ci    netifapi_dhcps_start(g_lwip_netif, 0, 0);
970a7ce71fSopenharmony_ci
980a7ce71fSopenharmony_ci    return 0;
990a7ce71fSopenharmony_ci}
1000a7ce71fSopenharmony_ci
1010a7ce71fSopenharmony_civoid hi_wifi_stop_softap(void)
1020a7ce71fSopenharmony_ci{
1030a7ce71fSopenharmony_ci    int ret;
1040a7ce71fSopenharmony_ci
1050a7ce71fSopenharmony_ci    netifapi_dhcps_stop(g_lwip_netif);
1060a7ce71fSopenharmony_ci    hi_softap_reset_addr(g_lwip_netif);
1070a7ce71fSopenharmony_ci
1080a7ce71fSopenharmony_ci    ret = hi_wifi_softap_stop();
1090a7ce71fSopenharmony_ci    if (ret != HISI_OK) {
1100a7ce71fSopenharmony_ci        printf("failed to stop softap\n");
1110a7ce71fSopenharmony_ci    }
1120a7ce71fSopenharmony_ci
1130a7ce71fSopenharmony_ci    ret = hi_wifi_deinit();
1140a7ce71fSopenharmony_ci    if (ret != HISI_OK) {
1150a7ce71fSopenharmony_ci        printf("failed to deinit wifi\n");
1160a7ce71fSopenharmony_ci    }
1170a7ce71fSopenharmony_ci
1180a7ce71fSopenharmony_ci    g_lwip_netif = NULL;
1190a7ce71fSopenharmony_ci}
1200a7ce71fSopenharmony_ci
121