[文章]Neptune WiFi连接——STA模式

阅读量0
0
4
WiFi连接的代码示例是 wm_sta_demo.c
1.测试步骤
手机设置热点,或者将路由器的热点名称改成 hihope
mm改成12345678
在UserMain函数最后面调用w800_sta_demo函数
  1. void UserMain(void)
  2. {
  3.     printf("n user task n");
  4. #if DEMO_CONSOLE
  5.     CreateDemoTask();
  6. #endif

  7. #if defined(LOSCFG_KERNEL_TEST_FULL) || defined(LOSCFG_KERNEL_TEST)
  8.         LosAppInit();
  9. #else
  10.     if (DeviceManagerStart()) {
  11.         printf("[%s] No drivers need load by hdf manager!",__func__);
  12.     }
  13. #endif       
  14.     w800_sta_demo();
  15. }
复制代码
编译烧录后查看打印信息:

说明WiFi连接成功
2.代码部分

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>

  4. #include "lwip/ip4_addr.h"
  5. #include "lwip/netif.h"
  6. #include "lwip/netifapi.h"

  7. #include "cmsis_os2.h"
  8. #include "ohos_init.h"
  9. #include "wifi_device.h"
  10. #include "wifi_error_code.h"


  11. #define DEF_TIMEOUT 15
  12. #define ONE_SECOND 1
  13. #define DHCP_DELAY 100

  14. static int WiFiInit(void);
  15. static void WaitScanResult(void);
  16. static int WaitConnectResult(void);
  17. static void OnWifiScanStateChangedHandler(int state, int size);
  18. static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info);
  19. static void OnHotspotStaJoinHandler(StationInfo *info);
  20. static void OnHotspotStateChangedHandler(int state);
  21. static void OnHotspotStaLeaveHandler(StationInfo *info);
  22. static void PrintLinkedInfo(WifiLinkedInfo* info);

  23. static int g_staScanSuccess = 0;
  24. static int g_connectSuccess = 0;
  25. static int g_ssid_count = 0;
  26. static struct netif *g_lwip_netif = NULL;
  27. static WifiEvent g_wifiEventHandler = { 0 };
  28. static WifiErrorCode error;

  29. #define SELECT_WLAN_PORT "wlan0"

  30. #define SELECT_WIFI_SSID "hihope"
  31. #define SELECT_WIFI_PASSWORD "12345678"
  32. #define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK



  33. static int WifiConnectAp(const char *ssid, const char *psk, WifiScanInfo *info, int i)
  34. {
  35.     if (strcmp(ssid, info[i].ssid) == 0) {
  36.         int result;
  37.         printf("Select:%3d wireless, Waiting...rn", i + 1);


  38.         WifiDeviceConfig select_ap_config = { 0 };
  39.         strcpy_s(select_ap_config.ssid, sizeof(select_ap_config.ssid), info[i].ssid);
  40.         strcpy_s(select_ap_config.preSharedKey, sizeof(select_ap_config.preSharedKey), psk);
  41.         select_ap_config.securityType = WIFI_SEC_TYPE_PSK;

  42.         if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS) {
  43.             if (ConnectTo(result) == WIFI_SUCCESS && WaitConnectResult() == 1) {
  44.                 g_lwip_netif = netifapi_netif_find_by_name(SELECT_WLAN_PORT);
  45.                 return 0;
  46.             }
  47.         }
  48.     }
  49.     return -1;
  50. }


  51. static void wm_sta_task(void)
  52. {
  53.     unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;


  54.     if (WiFiInit() != WIFI_SUCCESS) {
  55.         printf("WiFiInit failed, error = %drn", error);
  56.         return -1;
  57.     }

  58.     WifiScanInfo *info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
  59.     memset(info, 0, (sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT));
  60.     if (info == NULL) {
  61.         return -1;
  62.     }

  63.     do {
  64.         Scan();
  65.         WaitScanResult();
  66.         error = GetScanInfoList(info, &size);
  67.     } while (g_staScanSuccess != 1);

  68.     printf("********************rn");
  69.     for (uint8_t i = 0; i < g_ssid_count; i++) {
  70.         printf("no:%03d, ssid:%-30s, rssi:%5drn", i + 1, info[i].ssid, info[i].rssi);
  71.     }
  72.     printf("********************rn");

  73.     for (uint8_t i = 0; i < g_ssid_count; i++) {
  74.         if (WifiConnectAp(SELECT_WIFI_SSID, SELECT_WIFI_PASSWORD, info, i) == WIFI_SUCCESS) {
  75.             printf("WiFi connect succeed!rn");
  76.             break;
  77.         }

  78.         if (i == g_ssid_count - 1) {
  79.             printf("ERROR: No wifi as expectedrn");
  80.             while (1)
  81.                 osDelay(DHCP_DELAY);
  82.         }
  83.     }


  84.     if (g_lwip_netif) {
  85.         dhcp_start(g_lwip_netif);
  86.         printf("begain to dhcprn");
  87.     }

  88.     for (;;) {
  89.         if (dhcp_is_bound(g_lwip_netif) == ERR_OK) {
  90.             printf("<-- DHCP state:OK -->rn");

  91.             break;
  92.         }
  93.         osDelay(DHCP_DELAY);
  94.     }


  95.     for (;;) {
  96.         osDelay(DHCP_DELAY);
  97.     }
  98. }

  99. int WiFiInit(void)
  100. {
  101.     g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
  102.     g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
  103.     g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
  104.     g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
  105.     g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
  106.     error = RegisterWifiEvent(&g_wifiEventHandler);
  107.     if (error != WIFI_SUCCESS) {
  108.         printf("register wifi event fail!rn");
  109.         return -1;
  110.     }

  111.     if (EnableWifi() != WIFI_SUCCESS) {
  112.         printf("EnableWifi failed, error = %drn", error);
  113.         return -1;
  114.     }

  115.     if (IsWifiActive() == 0) {
  116.         printf("Wifi station is not active.rn");
  117.         return -1;
  118.     }
  119.     return 0;
  120. }

  121. static void OnWifiScanStateChangedHandler(int state, int size)
  122. {
  123.     (void)state;
  124.     if (size > 0) {
  125.         g_ssid_count = size;
  126.         g_staScanSuccess = 1;
  127.     }
  128.     return;
  129. }

  130. static void PrintLinkedInfo(WifiLinkedInfo* info)
  131. {
  132.     if (!info) return;

  133.     static char macAddress[32] = {0};
  134.     unsigned char* mac = info->bssid;
  135.     snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
  136.         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  137.     printf("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %srn",
  138.         macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
  139. }

  140. static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
  141. {
  142.     (void)info;

  143.     if (state > 0) {
  144.         g_connectSuccess = 1;

  145.         //PrintLinkedInfo(info);

  146.         printf("callback function for wifi connectrn");
  147.     } else {
  148.         printf("connect error,please check passwordrn");
  149.     }
  150.     return;
  151. }

  152. static void OnHotspotStaJoinHandler(StationInfo *info)
  153. {
  154.     (void)info;
  155.     printf("STA join APn");
  156.     return;
  157. }

  158. static void OnHotspotStaLeaveHandler(StationInfo *info)
  159. {
  160.     (void)info;
  161.     printf("HotspotStaLeave:info is null.n");
  162.     return;
  163. }

  164. static void OnHotspotStateChangedHandler(int state)
  165. {
  166.     printf("HotspotStateChanged:state is %d.n", state);
  167.     return;
  168. }

  169. static void WaitScanResult(void)
  170. {
  171.     int scanTimeout = DEF_TIMEOUT;
  172.     while (scanTimeout > 0) {
  173.         sleep(ONE_SECOND);
  174.         scanTimeout--;
  175.         if (g_staScanSuccess == 1) {
  176.             printf("WaitScanResult:wait success[%d]sn", (DEF_TIMEOUT - scanTimeout));
  177.             break;
  178.         }
  179.     }
  180.     if (scanTimeout <= 0) {
  181.         printf("WaitScanResult:timeout!n");
  182.     }
  183. }

  184. static int WaitConnectResult(void)
  185. {
  186.     int ConnectTimeout = DEF_TIMEOUT;
  187.     while (ConnectTimeout > 0) {
  188.         sleep(1);
  189.         ConnectTimeout--;
  190.         if (g_connectSuccess == 1) {
  191.             printf("WaitConnectResult:wait success[%d]sn", (DEF_TIMEOUT - ConnectTimeout));
  192.             break;
  193.         }
  194.     }
  195.     if (ConnectTimeout <= 0) {
  196.         printf("WaitConnectResult:timeout!n");
  197.         return 0;
  198.     }

  199.     return 1;
  200. }


  201. void w800_sta_demo(void)
  202. {
  203.     osThreadAttr_t attr;

  204.     attr.name = "wm_sta_task";
  205.     attr.attr_bits = 0U;
  206.     attr.cb_mem = NULL;
  207.     attr.cb_size = 0U;
  208.     attr.stack_mem = NULL;
  209.     attr.stack_size = 10240;
  210.     attr.priority = 26;

  211.     if (osThreadNew((osThreadFunc_t)wm_sta_task, NULL, &attr) == NULL) {
  212.         printf("Falied to create wm_sta_task!rn");
  213.     }
  214. }
复制代码

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友