2025-01-18 14:53:29 +00:00
|
|
|
package hardware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-11-02 10:57:53 +01:00
|
|
|
"errors"
|
2025-01-18 14:53:29 +00:00
|
|
|
"fmt"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// PeripheralEvent is trigger by the finders when the scan is complete
|
|
|
|
|
type PeripheralEvent string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// PeripheralArrival is triggerd when a peripheral has been connected to the system
|
|
|
|
|
PeripheralArrival PeripheralEvent = "PERIPHERAL_ARRIVAL"
|
|
|
|
|
// PeripheralRemoval is triggered when a peripheral has been disconnected from the system
|
|
|
|
|
PeripheralRemoval PeripheralEvent = "PERIPHERAL_REMOVAL"
|
2025-08-31 11:15:38 +02:00
|
|
|
// PeripheralStatus is triggered when a peripheral status has been updated (disconnected - connecting - connected)
|
|
|
|
|
PeripheralStatus PeripheralEvent = "PERIPHERAL_STATUS"
|
2025-01-18 14:53:29 +00:00
|
|
|
// debounceDuration = 500 * time.Millisecond
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// HardwareManager is the class who manages the hardware
|
|
|
|
|
type HardwareManager struct {
|
2025-11-02 10:57:53 +01:00
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
finders map[string]PeripheralFinder // The map of peripherals finders
|
|
|
|
|
peripherals []Peripheral // The current list of peripherals
|
|
|
|
|
peripheralsScanTrigger chan struct{} // Trigger the peripherals scans
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewHardwareManager creates a new HardwareManager
|
|
|
|
|
func NewHardwareManager() *HardwareManager {
|
|
|
|
|
log.Trace().Str("package", "hardware").Msg("Hardware instance created")
|
|
|
|
|
return &HardwareManager{
|
|
|
|
|
finders: make(map[string]PeripheralFinder),
|
|
|
|
|
peripherals: make([]Peripheral, 0),
|
|
|
|
|
peripheralsScanTrigger: make(chan struct{}),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start starts to find new peripheral events
|
|
|
|
|
func (h *HardwareManager) Start(ctx context.Context) error {
|
2025-11-02 10:57:53 +01:00
|
|
|
// Initialize all the finders
|
2025-01-18 14:53:29 +00:00
|
|
|
for finderName, finder := range h.finders {
|
|
|
|
|
err := finder.Initialize()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Err(err).Str("file", "hardware").Str("finderName", finderName).Msg("unable to initialize finder")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = finder.Start(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Err(err).Str("file", "hardware").Str("finderName", finderName).Msg("unable to start finder")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-02 10:57:53 +01:00
|
|
|
|
|
|
|
|
// Periodically scan all the finders
|
|
|
|
|
h.wg.Add(1)
|
2025-01-18 14:53:29 +00:00
|
|
|
go func() {
|
2025-11-02 10:57:53 +01:00
|
|
|
defer h.wg.Done()
|
2025-01-18 14:53:29 +00:00
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
case <-h.peripheralsScanTrigger:
|
|
|
|
|
for finderName, finder := range h.finders {
|
|
|
|
|
log.Trace().Str("file", "hardware").Str("finderName", finderName).Msg("force a finder to scan peripherals")
|
|
|
|
|
finder.ForceScan()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetFinder returns a register finder
|
|
|
|
|
func (h *HardwareManager) GetFinder(finderName string) (PeripheralFinder, error) {
|
|
|
|
|
finder, exists := h.finders[finderName]
|
|
|
|
|
if !exists {
|
|
|
|
|
log.Error().Str("file", "hardware").Str("finderName", finderName).Msg("unable to get the finder")
|
|
|
|
|
return nil, fmt.Errorf("unable to locate the '%s' finder", finderName)
|
|
|
|
|
}
|
|
|
|
|
log.Debug().Str("file", "hardware").Str("finderName", finderName).Msg("got finder")
|
|
|
|
|
return finder, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterFinder registers a new peripherals finder
|
|
|
|
|
func (h *HardwareManager) RegisterFinder(finder PeripheralFinder) {
|
|
|
|
|
h.finders[finder.GetName()] = finder
|
|
|
|
|
log.Info().Str("file", "hardware").Str("finderName", finder.GetName()).Msg("finder registered")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scan scans all the peripherals for the registered finders
|
|
|
|
|
func (h *HardwareManager) Scan() error {
|
2025-11-02 10:57:53 +01:00
|
|
|
select {
|
|
|
|
|
case h.peripheralsScanTrigger <- struct{}{}:
|
|
|
|
|
return nil
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("scan trigger not available (manager stopped?)")
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// emitPeripheralsChanges compares the old and new peripherals to determine which ones have been added or removed.
|
2025-01-26 12:01:31 +01:00
|
|
|
func emitPeripheralsChanges(ctx context.Context, oldPeripherals map[string]PeripheralInfo, newPeripherals map[string]PeripheralInfo) {
|
2025-01-18 14:53:29 +00:00
|
|
|
log.Trace().Any("oldList", oldPeripherals).Any("newList", newPeripherals).Msg("emitting peripherals changes to the front")
|
|
|
|
|
|
|
|
|
|
// Identify removed peripherals: present in the old list but not in the new list
|
|
|
|
|
for oldPeriphName := range oldPeripherals {
|
|
|
|
|
if _, exists := newPeripherals[oldPeriphName]; !exists {
|
2025-01-26 12:01:31 +01:00
|
|
|
runtime.EventsEmit(ctx, string(PeripheralRemoval), oldPeripherals[oldPeriphName])
|
2025-01-18 14:53:29 +00:00
|
|
|
log.Trace().Str("file", "hardware").Str("event", string(PeripheralRemoval)).Msg("emit peripheral removal event")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Identify added peripherals: present in the new list but not in the old list
|
|
|
|
|
for newPeriphName := range newPeripherals {
|
|
|
|
|
if _, exists := oldPeripherals[newPeriphName]; !exists {
|
2025-01-26 12:01:31 +01:00
|
|
|
runtime.EventsEmit(ctx, string(PeripheralArrival), newPeripherals[newPeriphName])
|
2025-01-18 14:53:29 +00:00
|
|
|
log.Trace().Str("file", "hardware").Str("event", string(PeripheralArrival)).Msg("emit peripheral arrival event")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-02 10:57:53 +01:00
|
|
|
|
|
|
|
|
// WaitStop stops the hardware manager
|
|
|
|
|
func (h *HardwareManager) WaitStop() error {
|
|
|
|
|
log.Trace().Str("file", "hardware").Msg("closing the hardware manager")
|
|
|
|
|
|
|
|
|
|
// Closing trigger channel
|
|
|
|
|
close(h.peripheralsScanTrigger)
|
|
|
|
|
|
|
|
|
|
// Stop each finder
|
|
|
|
|
var errs []error
|
|
|
|
|
for name, f := range h.finders {
|
|
|
|
|
if err := f.WaitStop(); err != nil {
|
|
|
|
|
errs = append(errs, fmt.Errorf("%s: %w", name, err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait for goroutines to finish
|
|
|
|
|
h.wg.Wait()
|
|
|
|
|
|
|
|
|
|
// Returning errors
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
|
return errors.Join(errs...)
|
|
|
|
|
}
|
|
|
|
|
log.Info().Str("file", "hardware").Msg("hardware manager stopped")
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|