Skip to content

Commit

Permalink
mutex to protect calls
Browse files Browse the repository at this point in the history
  • Loading branch information
tvanriper committed Jun 7, 2022
1 parent 47dd472 commit 33231d2
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"strconv"
"strings"
"sync"
"time"

"github.com/pkg/errors"
Expand All @@ -22,6 +23,7 @@ type WPAConn interface {
// Client represents a wireless client
type Client struct {
conn WPAConn
mu sync.Mutex
}

// NewClient will create a new client by connecting to the
Expand Down Expand Up @@ -59,6 +61,8 @@ func (cl *Client) Subscribe(topics ...string) *Subscription {

// Status will return the current state of the WPA
func (cl *Client) Status() (State, error) {
cl.mu.Lock()
defer cl.mu.Unlock()
data, err := cl.conn.SendCommand(CmdStatus)
if err != nil {
return State{}, err
Expand All @@ -69,6 +73,8 @@ func (cl *Client) Status() (State, error) {

// Scan will scan for networks and return the APs it finds
func (cl *Client) Scan() (nets APs, err error) {
cl.mu.Lock()
defer cl.mu.Unlock()
err = cl.conn.SendCommandBool(CmdScan)
if err != nil {
return nets, err
Expand Down Expand Up @@ -104,6 +110,8 @@ func (cl *Client) Scan() (nets APs, err error) {

// Networks lists the known networks
func (cl *Client) Networks() (nets Networks, err error) {
cl.mu.Lock()
defer cl.mu.Unlock()
data, err := cl.conn.SendCommand(CmdListNetworks)
if err != nil {
return nil, err
Expand Down

0 comments on commit 33231d2

Please sign in to comment.