about summary refs log tree commit diff
path: root/src/libwpactrl/wpactrl_command.c
blob: d10e346b0068ed5ad589b03be7739551e1b8ebb1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* ISC license. */

#include <errno.h>
#include <bcnm/wpactrl.h>

#define WPARESPONSE_MAXLEN 28

struct wparesponse_map_s
{
  char const *s ;
  wparesponse_t r ;
} ;

wparesponse_t wpactrl_command (wpactrl_t *a, char const *s, tain_t *stamp)
{
  static struct wparesponse_map_s const wparesponses[] =
  {
    { "OK\n", WPA_OK },
    { "PONG\n", WPA_PONG },
    { "UNKNOWN COMMAND\n", WPA_UNKNOWNCOMMAND },
    { "FAIL\n", WPA_FAIL },
    { "FAIL-BUSY\n", WPA_FAILBUSY },
    { "FAIL-CHECKSUM\n", WPA_FAILCHECKSUM },
    { "FAIL-INVALID-PIN\n", WPA_FAILINVALIDPIN },
    { "FAIL-CHANNEL-UNAVAILABLE\n", WPA_FAILCHANNELUNAVAILABLE },
    { "FAIL-CHANNEL-UNSUPPORTED\n", WPA_FAILCHANNELUNSUPPORTED },
    { "FAIL-Invalid range\n", WPA_FAILINVALIDRANGE },
    { "FAIL-Too long response\n", WPA_FAILTOOLONGRESPONSE },
    { "FAIL-PBC-OVERLAP\n", WPA_FAILPBCOVERLAP },
    { "FAIL-UNKNOWN-UUID\n", WPA_FAILUNKNOWNUUID },
    { "FAIL-NO-AP-SETTINGS\n", WPA_FAILNOAPSETTINGS },
    { "FAIL-NO-IFNAME-MATCH\n", WPA_FAILNOIFNAMEMATCH },
    { 0, WPA_UNKNOWNRESPONSE }
  } ;
  char buf[WPARESPONSE_MAXLEN] ;
  ssize_t r = wpactrl_query(a, s, buf, WPARESPONSE_MAXLEN, stamp) ;
  if (r < 0) return WPA_ERROR ;
  if (!r) return (errno = EPIPE, WPA_ERROR) ;
  {
    unsigned int i = 0 ;
    for (; wparesponses[i].s ; i++)
      if (!strncmp(buf, wparesponses[i].s, r))
        break ;
    return wparesponses[i].r ;
  }
}