1、select函数
定义函数 int select(int n,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout);
函数说明 select()用来等待文件描述词状态的改变。参数n代表最大的文件描述词加1,参数readfds、writefds 和exceptfds 称为描述词组,是用来回传该描述词的读,写或例外的状况。底下的宏提供了处理这三种描述词组的方式:
- FD_CLR(inr fd,fd_set* set);用来清除描述词组set中相关fd 的位
- FD_ISSET(int fd,fd_set *set);用来测试描述词组set中相关fd 的位是否为真
- FD_SET(int fd,fd_set*set);用来设置描述词组set中相关fd的位
- FD_ZERO(fd_set *set); 用来清除描述词组set的全部位
参数 timeout为结构timeval,用来设置select()的等待时间,其结构定义如下
struct timeval
{
time_t tv_sec;
time_t tv_usec;
};
```c
返回值 如果参数timeout设为NULL则表示select()没有timeout
通过模拟手机wifi模式,写一个Linux下wifi的驱动程序
摘取重要部分函数:
```c
/*
* WPA Supplicant - command line interface for wpa_supplicant daemon
* Copyright (c) 2004-2012, Jouni Malinen <[email protected]>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
static int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
{
return _wpa_ctrl_command(ctrl, cmd, 0);
}
static int wpa_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd, int min_args,
int argc, char *argv[])
{
char buf[256];
if (argc < min_args) {
printf("Invalid %s command - at least %d argument%s "
"required.\n", cmd, min_args,
min_args > 1 ? "s are" : " is");
return -1;
}
if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
return -1;
return wpa_ctrl_command(ctrl, buf);
}
/*WPA命令请求函数*/
static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
{
struct wpa_cli_cmd *cmd, *match = NULL;
int count;
int ret = 0;
count = 0;
cmd = wpa_cli_commands;
while (cmd->cmd) {
if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
{
match = cmd;
if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
/* we have an exact match */
count = 1;
break;
}
count++;
}
cmd++;
}
if (count > 1) {
printf("Ambiguous command '%s'; possible commands:", argv[0]);
cmd = wpa_cli_commands;
while (cmd->cmd) {
if (os_strncasecmp(cmd->cmd, argv[0],
os_strlen(argv[0])) == 0) {
printf(" %s", cmd->cmd);
}
cmd++;
}
printf("\n");
ret = 1;
} else if (count == 0) {
printf("Unknown command '%s'\n", argv[0]);
ret = 1;
} else {
ret = match->handler(ctrl, argc - 1, &argv[1]);
}
return ret;
}
#define CFG_MAXARGS 10
/*行解析函数,从U-boot中提取的这个函数,返回的是该行命令有的字段的个数,以空格和tab为分界,进行提取*/
static int parse_line (char *line,