Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DisableAllNetworks(), Disconnect(), SetCountry() and FAIL-BUSY error handling added. #3

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions unixgram.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package wpasupplicant
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -294,6 +295,10 @@ func (uc *unixgramConn) DisableNetwork(networkID int) error {
return uc.runCommand(fmt.Sprintf("DISABLE_NETWORK %d", networkID))
}

func (uc *unixgramConn) DisableAllNetworks() error {
return uc.runCommand("DISABLE_NETWORK all")
}

func (uc *unixgramConn) RemoveNetwork(networkID int) error {
return uc.runCommand(fmt.Sprintf("REMOVE_NETWORK %d", networkID))
}
Expand All @@ -306,12 +311,19 @@ func (uc *unixgramConn) SetNetwork(networkID int, variable string, value string)
var cmd string

// Since key_mgmt expects the value to not be wrapped in "" we do a little check here.
if variable == "key_mgmt" {
cmd = fmt.Sprintf("SET_NETWORK %d %s %s", networkID, variable, value)
} else {
cmd = fmt.Sprintf("SET_NETWORK %d %s \"%s\"", networkID, variable, value)
switch variable {
case "key_mgmt", "bssid":
{
cmd = fmt.Sprintf("SET_NETWORK %d %s %s", networkID, variable, value)
}
default:
{
cmd = fmt.Sprintf("SET_NETWORK %d %s \"%s\"", networkID, variable, value)
}
}

fmt.Println(cmd)

return uc.runCommand(cmd)
}

Expand All @@ -331,6 +343,10 @@ func (uc *unixgramConn) Reconnect() error {
return uc.runCommand("RECONNECT")
}

func (uc *unixgramConn) Disconnect() error {
return uc.runCommand("DISCONNECT")
}

func (uc *unixgramConn) Scan() error {
return uc.runCommand("SCAN")
}
Expand Down Expand Up @@ -362,6 +378,18 @@ func (uc *unixgramConn) ListNetworks() ([]ConfiguredNetwork, error) {
return parseListNetworksResult(bytes.NewBuffer(resp))
}

func (uc *unixgramConn) SetCountry(country string) error {
var cmd string

if len(country) == 2 {
cmd = fmt.Sprintf("SET country %s", country)
} else {
return errors.New("only ISO 3166-1 alpha-2 (two-letter) country codes are allowed")
}

return uc.runCommand(cmd)
}

// 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 {
Expand All @@ -372,6 +400,10 @@ func (uc *unixgramConn) runCommand(cmd string) error {

if bytes.Compare(resp, []byte("OK\n")) == 0 {
return nil
} else if bytes.Compare(resp, []byte("FAIL\n")) == 0 {
return errors.New("FAIL")
} else if bytes.Compare(resp, []byte("FAIL-BUSY\n")) == 0 {
return errors.New("FAIL-BUSY")
}

return &ParseError{Line: string(resp)}
Expand Down
10 changes: 10 additions & 0 deletions wpasupplicant.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ type Conn interface {
// DisableNetwork disables a network.
DisableNetwork(int) error

// DisableAllNetworks disables all networks.
DisableAllNetworks() error

// RemoveNetwork removes a network from the configuration.
RemoveNetwork(int) error

Expand All @@ -226,6 +229,10 @@ type Conn interface {
// command fails.
Reconnect() error

// Disconnect sends a DISCONNECT command to the wpa_supplicant. Returns error when
// command fails.
Disconnect() error

// ListNetworks returns the currently configured networks.
ListNetworks() ([]ConfiguredNetwork, error)

Expand All @@ -236,6 +243,9 @@ type Conn interface {
// return OK.
Scan() error

// Set country variable:
SetCountry(string) 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.
Expand Down