代码拉取完成,页面将自动刷新
/*
* Copyright (C) 2023 HiHope Open Source Organization .
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
* limitations under the License.
*/
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
//#include "hi_wifi_api.h"
//#include "lwip/sockets.h"
#include "lwip/ip_addr.h"
#include "lwip/netifapi.h"
#include "lwip/api_shell.h"
#include "wifi_device.h"
#include "net_config.h"
#include "pzcar_wheel_ctrl.h"
#ifdef SUPPORT_KV_STORE
#include "kv_store.h"
#endif
extern PzCarInfo g_carInfo;
static int g_connected = 0;
char ssid[64] = {0};
char pswd[64] = {0};
char flag = 0; //init state
int netId = -1;
#if 1
#define IP_LEN (16)
char ip[IP_LEN] = {0};
char gw[IP_LEN] = {0};
char* WifiGetSSID(void)
{
return ssid;
}
char* WifiGetIP(void)
{
unsigned char n = ip[0]-'0';
if ((0 < n) && (n <= 9)) { // current ip is valid
// printf("WifiGetIP: ip[%s]\n", ip);
return ip;
}
// current ip is invalid, get ip from wlan0
struct netif* p_netif = netifapi_netif_find("wlan0");
if(NULL == p_netif) {
printf("WifiGetIP: netifapi_netif_find fail\n");
return ip;
}
ip4_addr_t gwaddr = {0};
ip4_addr_t ipaddr = {0};
ip4_addr_t netmask = {0};
if (0 != netifapi_netif_get_addr(p_netif, &ipaddr, &netmask, &gwaddr)) {
printf("WifiGetIP: netifapi_netif_get_addr fail\n");
return ip;
}
inet_ntop(AF_INET, &ipaddr, ip, IP_LEN);
inet_ntop(AF_INET, &gwaddr, gw, IP_LEN);
//printf("WifiGetIP: IP[%s]/GW[%s]\n", ip, gw);
return ip;
}
#endif
static void PrintWifiLinkedInfo(WifiLinkedInfo* info)
{
if (!info) return;
static char macAddress[32] = {0};
unsigned char* mac = info->bssid;
snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("[STA] %s:\n", __func__);
printf(" bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %s\n",
macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
}
//static
void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
{
if (!info) {
return;
}
printf("[STA] %s: state[%d], info:\n", __func__, state);
PrintWifiLinkedInfo(info);
if (state == 1) {
g_connected = 1;
g_carInfo.e_NetST = Net_ST_Conned;
g_carInfo.e_NetMode = Net_MODE_NET_SERVER;
#ifdef SUPPORT_KV_STORE
int ret = -1;
ret = UtilsSetValue("ssid", ssid);
//printf("%s: [%d] UtilsSetValue(ssid[%s])\n", __func__, ret, ssid);
ret = UtilsSetValue("pswd", pswd);
//printf("%s: [%d] UtilsSetValue(pswd[%s])\n", __func__, ret, pswd);
#endif
} else {
memset(ip, 0, sizeof(ip));
g_connected = 0;
g_carInfo.e_NetST = Net_ST_DisCon;
g_carInfo.e_NetMode = Net_MODE_NET_SERVER;
}
}
//static
void OnWifiScanStateChanged(int state, int size)
{
printf("[STA] %s %d, state = %X, size = %d\n", __FUNCTION__, __LINE__, state, size);
}
/*
//监听事件回调函数
static WifiEvent g_eventListener = {
//链接状态发生变化
.OnWifiConnectionChanged = OnWifiConnectionChanged,
//扫描结果发生变化
.OnWifiScanStateChanged = OnWifiScanStateChanged
};
*/
WifiErrorCode StartStation(void)
{
WifiErrorCode retCode = WIFI_SUCCESS;
printf("-------------------------------------------\n");
printf("%s: Begin:\n", __func__);
//usleep(100*1000);
//retCode = RegisterWifiEvent(&g_eventListener);
//printf("%s: RegisterWifiEvent: ret = %d\n", __func__, retCode);
WifiDeviceConfig apConfig = {};
strcpy(apConfig.ssid, ssid);
strcpy(apConfig.preSharedKey, pswd);
apConfig.securityType = WIFI_SEC_TYPE_PSK;
printf("%s: apConfig: ssid[%s] pswd[%s]\n", __func__, apConfig.ssid, apConfig.preSharedKey);
retCode = EnableWifi();
printf("%s: EnableWifi: ret = %d\n", __func__, retCode);
usleep(100*1000);
retCode = AddDeviceConfig(&apConfig, &netId);
printf("%s: AddDeviceConfig: ret[%d], netId[%d]\n", __func__, retCode, netId);
g_connected = 0;
retCode = ConnectTo(netId);
printf("%s: ConnectTo(netId[%d]): ret[%d]\n", __func__, netId, retCode);
unsigned char cnt = 50; // 10s内连不上指定的热点,返回失败
while (!g_connected && cnt > 0) {
cnt--;
usleep(200*1000);
}
printf("%s: g_connected: %d\n", __func__, g_connected);
if (g_connected == 0) {
StopStation();
printf("%s: End.\n", __func__);
printf("-------------------------------------------\n");
return ERROR_WIFI_UNKNOWN;
}
usleep(500*1000);
struct netif* iface = netifapi_netif_find("wlan0");
if (iface) {
err_t ret = netifapi_dhcp_start(iface);
printf("%s: netifapi_dhcp_start: ret = %d\n", __func__, ret);
usleep(1000*1000); // wait DHCP server assign IP
ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL);
printf("%s: netifapi_netif_common: ret = %d\n", __func__, ret);
}
printf("%s: End.\n", __func__);
printf("-------------------------------------------\n");
return WIFI_SUCCESS;
}
WifiErrorCode StopStation(void)
{
WifiErrorCode retCode = WIFI_SUCCESS;
//UnRegisterWifiEvent(&g_eventListener);
retCode = Disconnect();
retCode = RemoveDevice(netId);
retCode = DisableWifi();
return retCode;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。