Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6362852
  • 博文数量: 579
  • 博客积分: 1548
  • 博客等级: 上尉
  • 技术积分: 16634
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-12 15:29
个人简介

http://www.csdn.net/ http://www.arm.com/zh/ https://www.kernel.org/ http://www.linuxpk.com/ http://www.51develop.net/ http://linux.chinaitlab.com/ http://www.embeddedlinux.org.cn http://bbs.pediy.com/

文章分类

全部博文(579)

文章存档

2018年(18)

2015年(91)

2014年(159)

2013年(231)

2012年(80)

分类: 嵌入式

2015-05-29 16:35:05

前言:
    在实际使用中,很多设备都需要用到wifi模块,但是如何使设备智能的连接到热点上。
    其中一种方法是:先把设备的wifi模块设置为ap模式,客户端先搜索到这个设备的热点,
然后连接它,再把设备真正需要连接的热点发送给设备,从而使设备可以连接到指定的热点上。

一,设置为ap模式后,如何获取其它热点
    设置为ap模式,默认是无法查找到别的wifi热点的,这个需要以下操作
    iwpriv ra0 set SiteSurvey

    然后在通过以下命令来获取热点信息
    iwpriv ra0 get_site_survey

二,编程获取wifi热点信息

点击(此处)折叠或打开

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

  4. typedef unsigned char HI_U8;
  5. typedef unsigned short HI_U16;
  6. typedef unsigned int HI_U32;

  7. typedef signed char HI_S8;
  8. typedef char *         HI_PS8;
  9. typedef short HI_S16;
  10. typedef int HI_S32;
  11. typedef int * HI_PS32;

  12. #define MK_PROC_MAX_WIFI_AP_SIZE        (10)

  13. typedef struct _HI_DEV_WIFI_SCANNING_S
  14. {
  15.     HI_U32 u32Size; //结构体的大小
  16.     HI_U32 u32ApNum; //* 第几个ap */
  17.     HI_U32 u32Frequency; //频率
  18.     HI_U32 u32CurrentUsed; //* 是否正在使用 1:正在使用 0:未使用 */
  19.     HI_U32 u32SignalQuality; //* 信号质量 */
  20.     HI_U32 u32BitRate; //* 传输速度(比特率) */
  21.     char szEssid[32]; //* essid */
  22.     char szProtocol[16]; //ap support protocol
  23.     char szMacAddr[32]; //* mac addr */
  24.     HI_U8 u8Mode; //mode 0:Auto 1:Managed 2:Ad-Hoc ...
  25.     HI_U8 u8IsEncrypt; //*是否加密1:加密 0:不加密*/
  26.     HI_U8 u8EncrypType; //*网络加密方式0:NONE 1:WAP 2:WAP2 3:WEP */
  27.     HI_U8 u8AuthMode; //* 认证模式 0:NONE 1:EAP 2:PSK 3:OPEN 4:SHARED*/
  28.     HI_U8 u8SecretKeyType; //密钥管理方式 0:none 1:AES 2:TKIP
  29.     
  30.     HI_U8 u8Res[7];
  31. }HI_DEV_WIFI_SCANNING_S,*LPHI_DEV_WIFI_SCANNING_S;

  32. static void enable_softap_scanning(void)
  33. {
  34.     system("iwpriv ra0 set SiteSurvey"); //开启ap扫描功能
  35.     sleep(2);
  36. }

  37. //*网络加密方式0:NONE 1:WAP 2:WAP2 3:WEP */
  38. // 4:other
  39. static int get_ap_security_type(char *buf)
  40. {
  41.     int    type = 0;
  42.     if (buf == NULL)     return -1;
  43.     
  44.     if (strstr(buf, "WPA2") != NULL)
  45.         type = 2;
  46.     else if (strstr(buf, "WPA") != NULL)
  47.         type = 1;
  48.     else if (strstr(buf, "WEP") != NULL)
  49.         type = 3;
  50.     else if (strstr(buf, "NONE") != NULL)
  51.         type = 0;
  52.     else
  53.         type = 4;
  54.     return type;
  55. }

  56. int _softap_get_scan_res(HI_DEV_WIFI_SCANNING_S *stScanInfo)
  57. {
  58.     FILE     *fp;
  59.     char     buf[256] = {0};
  60.     int        findFlag = 0;
  61.     int     index = 0;
  62.     char     chBuf[4] = {0},     ssidBuf[32] = {0},    macBuf[32]     = {0},
  63.             athBuf[32]     = {0},    sigBuf[16]     = {0},     wBuf[8]     = {0},
  64.             extBuf[8]     = {0},    ntBuf[4]     = {0};
  65.     
  66.     enable_softap_scanning();
  67.     
  68.     if((fp = popen("iwpriv ra0 get_site_survey", "r")) == NULL) //获取ap扫描结果
  69.     {
  70.         printf("open cmd: iwpriv ra0 get_site_survey fail!\n");
  71.         return -1;
  72.     }

  73.     while(fgets(buf, sizeof(buf), fp) != NULL)
  74.     {
  75.         if(!findFlag && strstr(buf, "Ch") != NULL)
  76.         {
  77.             findFlag = 1;
  78.             continue;
  79.         }
  80.         
  81.         if (findFlag)
  82.         {
  83.             if(sscanf(buf, "%s %s %s %s %s %s %s %s\n",chBuf,ssidBuf,macBuf,athBuf,\
  84.                     sigBuf,wBuf,extBuf,ntBuf) != 8)
  85.             {
  86.                 continue;
  87.             }
  88.             
  89.             if ((strcmp(ssidBuf, "") == 0) ||
  90.                 (strcmp(ssidBuf, "CMCC-AUTO") == 0) ||
  91.                 (strcmp(ssidBuf, "CMCC-WEB") == 0) ||
  92.                 (strcmp(ssidBuf, "CMCC-FREE") == 0) ||
  93.                 (strcmp(ssidBuf, "CMCC") == 0) ||
  94.                 (strcmp(macBuf, "") == 0) ||
  95.                 (strcmp(athBuf, "") == 0) ||
  96.                 (atoi(sigBuf) <= 0))
  97.                 
  98.             {
  99.                 continue;
  100.             }
  101.             else
  102.             {
  103.                 int     security_type = 0;
  104.                 if ((security_type = get_ap_security_type(athBuf)) < 0)
  105.                     continue;
  106.                     
  107.                 strncpy(stScanInfo[index].szEssid,ssidBuf,32);
  108.                 strncpy(stScanInfo[index].szMacAddr,macBuf,32);
  109.                 stScanInfo[index].u32SignalQuality = atoi(sigBuf);
  110.                 stScanInfo[index].u32CurrentUsed = 0;
  111.                 stScanInfo[index].u8EncrypType     = security_type;
  112.                 ++index;
  113.                 
  114.                 if (index >= MK_PROC_MAX_WIFI_AP_SIZE)
  115.                     break;
  116.             }
  117.         }
  118.     }
  119.     
  120.     pclose(fp);
  121.     
  122.     return index;
  123. }


  124. static     void show_ap(HI_DEV_WIFI_SCANNING_S *stScanInfo,int count)
  125. {
  126.     int index = 0;
  127.     
  128.     printf("%-4s%-32s%-32s%-8s%-8s\n","id","ssid","essid","sig","sec");
  129.     for (index = 0; index < count; index++)
  130.     {
  131. #if 0
  132.         printf("%*d,ssid[%*s],essid[%*s],signal[%*d],type[%*d]\n",4,index,
  133.                 32,stScanInfo[index].szEssid,
  134.                 32,stScanInfo[index].szMacAddr,
  135.                 4,stScanInfo[index].u32SignalQuality,
  136.                 4,stScanInfo[index].u8EncrypType);
  137. #else
  138.         
  139.         printf("%-4d%-32s%-32s%-8d%-8d\n",index,
  140.                 stScanInfo[index].szEssid,
  141.                 stScanInfo[index].szMacAddr,
  142.                 stScanInfo[index].u32SignalQuality,
  143.                 stScanInfo[index].u8EncrypType);
  144. #endif
  145.     }
  146. }


  147. int main(int argc,char *argv[])
  148. {
  149.     HI_DEV_WIFI_SCANNING_S stScanInfo[MK_PROC_MAX_WIFI_AP_SIZE];
  150.     int        ap_count = 0;
  151.     
  152.     ap_count = _softap_get_scan_res(stScanInfo);
  153.     show_ap(stScanInfo,ap_count);
  154.     
  155.     return 0;
  156. }
对 fgets 函数有疑惑的请看:fgets函数的理解
测试结果:


阅读(4590) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~