Expose more control interface commands

This commit is contained in:
Matias Doyle 2017-09-05 15:56:03 +02:00
parent 3d367e7a27
commit b70264bcf8
No known key found for this signature in database
GPG Key ID: D8D001818E21F4D1
2 changed files with 59 additions and 0 deletions

View File

@ -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) {

View File

@ -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.