diff --git a/cmd/repl.go b/cmd/repl.go index 10f19b4..ff453e6 100644 --- a/cmd/repl.go +++ b/cmd/repl.go @@ -12,11 +12,12 @@ func replCmdAction(cmd *cobra.Command, args []string) { PS1: "\rESDI > ", }) - listDevicesREPLCmd := repl.Command{ - Name: "ls", - Usage: "lists the currently available devices", + perClerk := peripheral.NewPeripheralDeviceClerk() + + discoverDevicesREPLCmd := repl.Command{ + Name: "discover", + Usage: "discovers connected devices", Action: func(r *repl.REPL, args []string) error { - perClerk := peripheral.NewPeripheralDeviceClerk() err := perClerk.FindDevices() if err != nil { return err @@ -26,6 +27,17 @@ func replCmdAction(cmd *cobra.Command, args []string) { }, } + listDevicesREPLCmd := repl.Command{ + Name: "list", + Usage: "lists connected devices", + Action: func(r *repl.REPL, args []string) error { + _ = perClerk.ListDevices() + + return nil + }, + } + + r.RegisterCMD(discoverDevicesREPLCmd) r.RegisterCMD(listDevicesREPLCmd) r.Start() diff --git a/peripheral/device.go b/peripheral/device.go index 4798294..3b8c28c 100644 --- a/peripheral/device.go +++ b/peripheral/device.go @@ -36,7 +36,8 @@ func (s PeripheralDeviceState) String() string { type PeripheralDevice struct { State PeripheralDeviceState CommState CommState - Name [32]byte + ID uint8 + Name string WT *WalkieTalkie } @@ -93,7 +94,8 @@ func (p *PeripheralDevice) ToConnectedIdling() { } func (p *PeripheralDevice) Merge(packet *IdentificationPacket) { - p.Name = packet.Name + p.Name = string(packet.Name[:]) + p.ID = packet.DeviceID } func (p *PeripheralDevice) Disconnect() error { diff --git a/peripheral/identificationPacket.go b/peripheral/identificationPacket.go index b1b771c..dabaaa6 100644 --- a/peripheral/identificationPacket.go +++ b/peripheral/identificationPacket.go @@ -7,6 +7,7 @@ import ( type IdentificationPacket struct { StartMarker byte + DeviceID uint8 PktType PacketType Name [32]byte EndMarker byte @@ -33,6 +34,9 @@ func (pkt *IdentificationPacket) Validate() bool { fmt.Println(pkt.EndMarker) fmt.Println("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑") + // Change this to return an error and add a validation against available + // device ids + if pkt.StartMarker != startOfText || pkt.EndMarker != endOfText { return false diff --git a/peripheral/peripheral.go b/peripheral/peripheral.go index fa4e3fe..5fd29ee 100644 --- a/peripheral/peripheral.go +++ b/peripheral/peripheral.go @@ -13,6 +13,7 @@ const ( ) type PeripheralDeviceClerk struct { + // mu sync.RWMutex Devices map[string]*PeripheralDevice } @@ -54,11 +55,27 @@ func (clerk *PeripheralDeviceClerk) FindDevices() error { fmt.Println(p, string(newDevice.Name[:])) } + // go clerk.clerkBackgroundJob() + + return nil +} + +func (clerk *PeripheralDeviceClerk) ListDevices() error { + for _, d := range clerk.Devices { + fmt.Printf("[%d] %s\n", d.ID, d.Name) + } + return nil } // func (c *PeripheralDeviceClerk) clerkBackgroundJob() { // for { -// +// c.mu.RLock() +// for _, d := range c.Devices { +// // Check the state of the device +// if d.CommState == CommIdle { +// } +// } +// c.mu.Unlock() // } // }