我正在编写自己的C库来管理linux中的wlan。我基于wpa_cli接口,但我无法理解,为什么他们使用两个wpa_ctrl结构:
static struct wpa_ctrl *ctrl_conn;
static struct wpa_ctrl *mon_conn;
当我打开并仅使用ctrl_conn附加?
时,它也有效答案 0 :(得分:4)
wpa_cli
有两种方式:互动和非互动
当您收到提示时,您将以交互方式使用wpa_cli
,反之亦然。
以下是互动模式:
$ wpa_cli -i wlan0
wpa_cli v2.1
Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi> and contributors
This software may be distributed under the terms of the BSD license.
See README for more details.
Interactive mode
> status
wpa_state=INACTIVE
address=98:fc:11:d1:89:68
uuid=0cb62eb3-776e-55d2-a4f9-983cdd3e48d2
非交互式模式:
$ wpa_cli -i wlan0 status
wpa_state=INACTIVE
address=98:fc:11:d1:89:68
uuid=0cb62eb3-776e-55d2-a4f9-983cdd3e48d2
当您使用交互式模式时,wpa_cli
似乎同时使用ctrl_conn
和mon_conn
。 ctrl_conn
仅用于发送命令,mon_conn
用于获取事件(即通过wpa_ctrl_attach()
附加的事件)。
当您使用非交互式模式时,wpa_cli
仅使用ctrl_conn
,因为没有返回任何事件。
如果您打算使用wpa_supplicant
事件(我希望您会这样做),我认为最好使用两个不同的连接,如wpa_ctrl_request()
comments concerning the msg_cb argument中所述:< / p>
/**
* wpa_ctrl_request - Send a command to wpa_supplicant/hostapd
* @ctrl: Control interface data from wpa_ctrl_open()
* @cmd: Command; usually, ASCII text, e.g., "PING"
* @cmd_len: Length of the cmd in bytes
* @reply: Buffer for the response
* @reply_len: Reply buffer length
* @msg_cb: Callback function for unsolicited messages or %NULL if not used
* Returns: 0 on success, -1 on error (send or receive failed), -2 on timeout
*
* This function is used to send commands to wpa_supplicant/hostapd. Received
* response will be written to reply and reply_len is set to the actual length
* of the reply. This function will block for up to two seconds while waiting
* for the reply. If unsolicited messages are received, the blocking time may
* be longer.
*
* msg_cb can be used to register a callback function that will be called for
* unsolicited messages received while waiting for the command response. These
* messages may be received if wpa_ctrl_request() is called at the same time as
* wpa_supplicant/hostapd is sending such a message. This can happen only if
* the program has used wpa_ctrl_attach() to register itself as a monitor for
* event messages. Alternatively to msg_cb, programs can register two control
* interface connections and use one of them for commands and the other one for
* receiving event messages, in other words, call wpa_ctrl_attach() only for
* the control interface connection that will be used for event messages.
*/
int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
char *reply, size_t *reply_len,
void (*msg_cb)(char *msg, size_t len));