Skip to content

Commit

Permalink
more solidfying of the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
tvanriper committed Mar 24, 2022
1 parent 23a1a50 commit 47dd472
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
39 changes: 16 additions & 23 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package wireless

import (
"errors"
"fmt"
"io"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
)

// WPAConn is an interface to the connection
Expand Down Expand Up @@ -131,10 +132,12 @@ func (cl *Client) Connect(net Network) (Network, error) {
sub := cl.conn.Subscribe(EventNetworkNotFound, EventAuthReject, EventConnected, EventDisconnected, EventAssocReject)
defer sub.Unsubscribe()
if net.IsDisabled() {
fmt.Printf("Enabling network %d\n", net.ID)
if err := cl.EnableNetwork(net.ID); err != nil {
return net, err
}
} else {
fmt.Printf("Selecting network %d\n", net.ID)
if err := cl.SelectNetwork(net.ID); err != nil {
return net, err
}
Expand All @@ -158,32 +161,22 @@ func (cl *Client) Connect(net Network) (Network, error) {
return net, errors.New("failed to catch event " + ev.Name)
}

func (cl *Client) Disconnect(net Network) (result Network, err error) {
nets, err := cl.Networks()
if err != nil {
return result, err
}
result, ok := nets.FindBySSID(net.SSID)
if !ok {
err = fmt.Errorf("unable to find %s", net.SSID)
return result, err
}
func (cl *Client) Disconnect() (err error) {
_, err = cl.conn.SendCommand(CmdDisconnect)
return result, err
return err
}

// AddOrUpdateNetwork will add or, if the network has IDStr set, update it
func (cl *Client) AddOrUpdateNetwork(net Network) (Network, error) {
if net.IDStr != "" {
if len(net.IDStr) == 0 {
nets, err := cl.Networks()
if err != nil {
return net, err
}

for _, n := range nets {
if n.IDStr == net.IDStr {
return cl.UpdateNetwork(net)
}
_, ok := nets.FindByIDStr(net.IDStr)
if ok {
return cl.UpdateNetwork(net)
}
}

Expand All @@ -195,11 +188,11 @@ func (cl *Client) AddOrUpdateNetwork(net Network) (Network, error) {
return net, err
}

for _, n := range nets {
if n.SSID == net.SSID {
net.IDStr = n.IDStr
return cl.UpdateNetwork(net)
}
found, ok := nets.FindBySSID(net.SSID)
if ok {
net.ID = found.ID
net.IDStr = found.IDStr
return cl.UpdateNetwork(net)
}
}

Expand All @@ -215,7 +208,7 @@ func (cl *Client) UpdateNetwork(net Network) (Network, error) {

for _, cmd := range setCmds(net) {
if err := cl.conn.SendCommandBool(cmd); err != nil {
return net, err
return net, errors.Wrap(err, cmd)
}
}

Expand Down
25 changes: 10 additions & 15 deletions cmd/connectap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"os"
"time"

"github.com/tvanriper/go-wireless"
)
Expand Down Expand Up @@ -38,26 +37,22 @@ func main() {
}
defer wc.Close()

state, err := wc.Status()
status, err := wc.Status()
if err != nil {
fmt.Printf("problems getting the current status: %s\n", err)
fmt.Printf("failed to get status: %s\n", err)
os.Exit(1)
}

net := wireless.NewNetwork(ssid, pks)

if len(state.IPAddress) > 0 {
// Must disconnect first.
fmt.Printf("Disconnecting from %s.\n", state.SSID)
_, err = wc.Disconnect(wireless.NewNetwork(state.SSID, ""))
if status.WpaState != wireless.EventDisconnected {
fmt.Printf("Disconnecting.\n")
err = wc.Disconnect()
if err != nil {
fmt.Printf("failed to disconnect network: %s\n", err)
os.Exit(1)
fmt.Printf("failed to disconnect: %s\n", err)
fmt.Println("continuing...")
}
// Pause for a bit to let messages run.
time.Sleep(time.Second)
}

net := wireless.NewNetwork(ssid, pks)

fmt.Printf("Attempting to connect to %s...\n", ssid)
_, err = wc.Connect(net)
if err != nil {
Expand All @@ -72,7 +67,7 @@ func main() {
case wireless.ErrAssocRejected:
fmt.Println("Assoc rejected")
default:
fmt.Println("failed to save configuration")
fmt.Printf("failed to save configuration: %s", err)
}
fmt.Println("Use wifistate to monitor for changes.")
os.Exit(1)
Expand Down

0 comments on commit 47dd472

Please sign in to comment.