概述
Linux应用程序获取hostapd热点状态
在hostapd的官网有一些关于应用程序访问hostapd简单的介绍。主要说的就是通过wpa_ctrl.c和wpa_ctrl.h中的接口进行访问。把这两个文件放入到c程序中就可以了,但是这两个文件很难放入到我们的工程源码中,因为这两个文件引用了hostapd源码中其他很多文件,不能单独使用。
关于官网对hostapd如何使用,我把它贴在这里,详细的可以直接访问官网:
http://w1.fi/wpa_supplicant/devel/hostapd_ctrl_iface_page.html
hostapd control interface
hostapd implements a control interface that can be used by external programs to control the operations of the hostapd daemon and to get status information and event notifications. There is a small C library, in a form of a single C file, wpa_ctrl.c, that provides helper functions to facilitate the use of the control interface. External programs can link this file into them and then use the library functions documented in wpa_ctrl.h to interact with wpa_supplicant. This library can also be used with C++. hostapd_cli.c is an example program using this library.
There are multiple mechanisms for inter-process communication. For example, Linux version of hostapd is using UNIX domain sockets for the control interface. The use of the functions defined in wpa_ctrl.h can be used to hide the details of the used IPC from external programs.
Using the control interface
External programs, e.g., a GUI or a configuration utility, that need to communicate with hostapd should link in wpa_ctrl.c. This allows them to use helper functions to open connection to the control interface with wpa_ctrl_open() and to send commands with wpa_ctrl_request().
hostapd uses the control interface for two types of communication: commands and unsolicited event messages. Commands are a pair of messages, a request from the external program and a response from hostapd. These can be executed using wpa_ctrl_request(). Unsolicited event messages are sent by hostapd to the control interface connection without specific request from the external program for receiving each message. However, the external program needs to attach to the control interface with wpa_ctrl_attach() to receive these unsolicited messages.
If the control interface connection is used both for commands and unsolicited event messages, there is potential for receiving an unsolicited message between the command request and response. wpa_ctrl_request() caller will need to supply a callback, msg_cb, for processing these messages. Often it is easier to open two control interface connections by calling wpa_ctrl_open() twice and then use one of the connections for commands and the other one for unsolicited messages. This way command request/response pairs will not be broken by unsolicited messages. wpa_cli.c is an example of how to use only one connection for both purposes and wpa_gui demonstrates how to use two separate connections.
Once the control interface connection is not needed anymore, it should be closed by calling wpa_ctrl_close(). If the connection was used for unsolicited event messages, it should be first detached by calling wpa_ctrl_detach().
关于wpa_ctrl中的接口正常的思维应该是工程中产生一个库文件,供用户调用,但是hostapd却没有。不要着急,虽然hostapd没有提供,但是wpa_supplicant工程却提供了.so动态库,这个.so动态库主要的源码就是wpa_ctrl.c中的源码。hostapd和wpa_supplicant是一个开源工程,他们的wpa_ctrl.c的源码也是完全一样的。
在wpa_supplicant工程中可以配置生成wpa_client.so动态库,这样在编译我们自己应用程序的时候,就可以通过这个动态库。这个动态库实现了wpa_ctrl.h中所有的接口。
如果你在使用的是buildroot,那么可以使能如下选项:
BR2_PACKAGE_WPA_SUPPLICANT_WPA_CLIENT_SO: │
│ │
│ Install libwpa_client.so. │
│ │
│ Symbol: BR2_PACKAGE_WPA_SUPPLICANT_WPA_CLIENT_SO [=y] │
│ Type : bool │
│ Prompt: Install wpa_client shared library │
│ Location: │
│ -> Target packages │
│ -> Networking applications │
│ -> wpa_supplicant (BR2_PACKAGE_WPA_SUPPLICANT [=y]) │
│ Defined at package/wpa_supplicant/Config.in:96 │
│ Depends on: BR2_PACKAGE_WPA_SUPPLICANT [=y] && !BR2_STATIC_LIBS [=n]
下面是我写的一个小例子,供大家参考:
#include <stddef.h>
#include <wpa_ctrl.h>
#include <string.h>
#include <stdio.h>
/**
*
*/
int strcpyline(char *line, int line_index, char *src)
{
int i = 0;
char *p = src;
char *q = line;
while (*p && i != line_index ) {
if (*p == 'n') {
i++;
}
p++;
}
if (*p) {
while (*p && *p != 'n') {
*q = *p;
p++;
q++;
}
*q = '