From b70264bcf8c542bae9997a7aaf74e1ae14cede39 Mon Sep 17 00:00:00 2001 From: Matias Doyle Date: Tue, 5 Sep 2017 15:56:03 +0200 Subject: [PATCH] Expose more control interface commands --- unixgram.go | 41 +++++++++++++++++++++++++++++++++++++++++ wpasupplicant.go | 18 ++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/unixgram.go b/unixgram.go index e42e23c..e2e9594 100644 --- a/unixgram.go +++ b/unixgram.go @@ -211,6 +211,32 @@ func (uc *unixgramConn) Ping() error { return &ParseError{Line: string(resp)} } +func (uc *unixgramConn) AddNetwork() (int, error) { + resp, err := uc.cmd("ADD_NETWORK") + if err != nil { + return -1, err + } + + b := bytes.NewBuffer(resp) + return strconv.Atoi(strings.Trim(b.String(), "\n")) +} + +func (uc *unixgramConn) EnableNetwork(networkID int) error { + return uc.runCommand(fmt.Sprintf("ENABLE_NETWORK %d", networkID)) +} + +func (uc *unixgramConn) SetNetwork(networkID int, variable string, value string) error { + return uc.runCommand(fmt.Sprintf("SET_NETWORK %d %s \"%s\"", networkID, variable, value)) +} + +func (uc *unixgramConn) SaveConfig() error { + return uc.runCommand("SAVE_CONFIG") +} + +func (uc *unixgramConn) Scan() error { + return uc.runCommand("SCAN") +} + func (uc *unixgramConn) ScanResults() ([]ScanResult, []error) { resp, err := uc.cmd("SCAN_RESULTS") if err != nil { @@ -220,6 +246,21 @@ func (uc *unixgramConn) ScanResults() ([]ScanResult, []error) { return parseScanResults(bytes.NewBuffer(resp)) } +// runCommand is a wrapper around the uc.cmd command which makes sure the +// command returned a successful (OK) response. +func (uc *unixgramConn) runCommand(cmd string) error { + resp, err := uc.cmd(cmd) + if err != nil { + return err + } + + if bytes.Compare(resp, []byte("OK\n")) == 0 { + return nil + } + + return &ParseError{Line: string(resp)} +} + // parseScanResults parses the SCAN_RESULTS output from wpa_supplicant. This // is split out from ScanResults() to make testing easier. func parseScanResults(resp io.Reader) (res []ScanResult, errs []error) { diff --git a/wpasupplicant.go b/wpasupplicant.go index 5037268..83c49aa 100644 --- a/wpasupplicant.go +++ b/wpasupplicant.go @@ -127,6 +127,24 @@ type Conn interface { // responding. Ping() error + // AddNetwork creates an empty network configuration. Returns the network + // ID. + AddNetwork() (int, error) + + // SetNetwork configures a network property. Returns error if the property + // configuration failed. + SetNetwork(int, string, string) error + + // EnableNetwork enables a network. Returns error if the command fails. + EnableNetwork(int) error + + // SaveConfig stores the current network configuration to disk. + SaveConfig() error + + // Scan triggers a new scan. Returns error if the wpa_supplicant does not + // return OK. + Scan() error + // ScanResult returns the latest scanning results. It returns a slice // of scanned BSSs, and/or a slice of errors representing problems // communicating with wpa_supplicant or parsing its output.