I am writing my own C library to manage wlan in linux. I base on wpa_cli interface, but I can not understand, why do they use two wpa_ctrl structures:
static struct wpa_ctrl *ctrl_conn;
static struct wpa_ctrl *mon_conn;
It works also when I open and attach only with ctrl_conn ?
wpa_cli
works two ways: interactive and non-interactive
When you have a prompt you are using wpa_cli
interactively and vice versa.
Here is the interactive mode:
$ 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
And is the non-interactive mode:
$ wpa_cli -i wlan0 status
wpa_state=INACTIVE
address=98:fc:11:d1:89:68
uuid=0cb62eb3-776e-55d2-a4f9-983cdd3e48d2
It seems that when you are using the interactive mode, wpa_cli
use both ctrl_conn
and mon_conn
. ctrl_conn
is used to send commands only, and mon_conn
is used to get events (i.e it is the one to be attached via wpa_ctrl_attach()
).
And when you are using the non-interactive mode, wpa_cli
use only ctrl_conn
because there is no events returned.
If you plan to use the wpa_supplicant
events (and I hope you will do), I think it is better to use two different connections as explained in the wpa_ctrl_request()
comments concerning the msg_cb argument:
/**
* 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));