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"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// EndpointEvent is trigger by the providers when the scan is complete
|
|
|
|
|
type EndpointEvent string
|
2025-01-18 14:53:29 +00:00
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// EndpointStatus is the endpoint status (DISCONNECTED => CONNECTING => DEACTIVATED => ACTIVATED)
|
|
|
|
|
type EndpointStatus string
|
2025-11-11 19:14:44 +00:00
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
const (
|
2025-11-30 18:57:20 +01:00
|
|
|
// EndpointArrival is triggerd when a endpoint has been connected to the system
|
|
|
|
|
EndpointArrival EndpointEvent = "PERIPHERAL_ARRIVAL"
|
|
|
|
|
// EndpointRemoval is triggered when a endpoint has been disconnected from the system
|
|
|
|
|
EndpointRemoval EndpointEvent = "PERIPHERAL_REMOVAL"
|
|
|
|
|
// EndpointLoad is triggered when a endpoint is added to the project
|
|
|
|
|
EndpointLoad EndpointEvent = "PERIPHERAL_LOAD"
|
|
|
|
|
// EndpointUnload is triggered when a endpoint is removed from the project
|
|
|
|
|
EndpointUnload EndpointEvent = "PERIPHERAL_UNLOAD"
|
|
|
|
|
// EndpointStatusUpdated is triggered when a endpoint status has been updated (disconnected - connecting - deactivated - activated)
|
|
|
|
|
EndpointStatusUpdated EndpointEvent = "PERIPHERAL_STATUS"
|
|
|
|
|
// EndpointEventEmitted is triggered when a endpoint event is emitted
|
|
|
|
|
EndpointEventEmitted EndpointEvent = "PERIPHERAL_EVENT_EMITTED"
|
|
|
|
|
// EndpointStatusDisconnected : endpoint is now disconnected
|
|
|
|
|
EndpointStatusDisconnected EndpointStatus = "PERIPHERAL_DISCONNECTED"
|
|
|
|
|
// EndpointStatusConnecting : endpoint is now connecting
|
|
|
|
|
EndpointStatusConnecting EndpointStatus = "PERIPHERAL_CONNECTING"
|
|
|
|
|
// EndpointStatusDeactivated : endpoint is now deactivated
|
|
|
|
|
EndpointStatusDeactivated EndpointStatus = "PERIPHERAL_DEACTIVATED"
|
|
|
|
|
// EndpointStatusActivated : endpoint is now activated
|
|
|
|
|
EndpointStatusActivated EndpointStatus = "PERIPHERAL_ACTIVATED"
|
2025-01-18 14:53:29 +00:00
|
|
|
)
|
|
|
|
|
|
2025-11-24 19:23:50 +01:00
|
|
|
// Manager is the class who manages the hardware
|
|
|
|
|
type Manager struct {
|
2025-11-29 17:42:42 +01:00
|
|
|
mu sync.Mutex
|
2025-11-02 10:57:53 +01:00
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
providers map[string]EndpointProvider // The map of endpoints providers
|
|
|
|
|
DetectedEndpoints map[string]Endpoint // The current list of endpoints
|
|
|
|
|
SavedEndpoints map[string]EndpointInfo // The list of stored endpoints
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-24 19:23:50 +01:00
|
|
|
// NewManager creates a new hardware manager
|
|
|
|
|
func NewManager() *Manager {
|
2025-01-18 14:53:29 +00:00
|
|
|
log.Trace().Str("package", "hardware").Msg("Hardware instance created")
|
2025-11-24 19:23:50 +01:00
|
|
|
return &Manager{
|
2025-11-30 18:57:20 +01:00
|
|
|
providers: make(map[string]EndpointProvider),
|
|
|
|
|
DetectedEndpoints: make(map[string]Endpoint, 0),
|
|
|
|
|
SavedEndpoints: make(map[string]EndpointInfo, 0),
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// RegisterEndpoint registers a new endpoint
|
|
|
|
|
func (h *Manager) RegisterEndpoint(ctx context.Context, endpointInfo EndpointInfo) (string, error) {
|
2025-11-29 17:42:42 +01:00
|
|
|
h.mu.Lock()
|
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// Create the endpoint from its provider (if needed)
|
|
|
|
|
if provider, found := h.providers[endpointInfo.ProtocolName]; found {
|
2025-11-30 18:33:00 +01:00
|
|
|
var err error
|
2025-11-30 18:57:20 +01:00
|
|
|
endpointInfo, err = provider.Create(ctx, endpointInfo)
|
2025-11-30 18:33:00 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// Do not save if the endpoint doesn't have a S/N
|
|
|
|
|
if endpointInfo.SerialNumber == "" {
|
|
|
|
|
return "", fmt.Errorf("serial number is empty for this endpoint")
|
2025-11-29 20:06:04 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
h.SavedEndpoints[endpointInfo.SerialNumber] = endpointInfo
|
2025-11-29 17:42:42 +01:00
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
runtime.EventsEmit(ctx, string(EndpointStatusUpdated), endpointInfo, EndpointStatusDisconnected)
|
2025-11-29 17:42:42 +01:00
|
|
|
|
|
|
|
|
// If already detected, connect it
|
2025-11-30 18:57:20 +01:00
|
|
|
if endpoint, ok := h.DetectedEndpoints[endpointInfo.SerialNumber]; ok {
|
2025-11-29 17:42:42 +01:00
|
|
|
h.wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer h.wg.Done()
|
2025-11-30 18:57:20 +01:00
|
|
|
err := endpoint.Connect(ctx)
|
2025-11-29 17:42:42 +01:00
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Err(err).Str("file", "FTDIProvider").Str("endpointSN", endpointInfo.SerialNumber).Msg("unable to connect the endpoint")
|
2025-11-29 17:42:42 +01:00
|
|
|
return
|
|
|
|
|
}
|
2025-11-30 18:57:20 +01:00
|
|
|
// Endpoint connected, activate it
|
|
|
|
|
err = endpoint.Activate(ctx)
|
2025-11-29 17:42:42 +01:00
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Err(err).Str("file", "FTDIProvider").Str("endpointSN", endpointInfo.SerialNumber).Msg("unable to activate the FTDI endpoint")
|
2025-11-29 17:42:42 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Emits the event in the hardware
|
2025-11-30 18:57:20 +01:00
|
|
|
runtime.EventsEmit(ctx, string(EndpointLoad), endpointInfo)
|
2025-11-29 17:42:42 +01:00
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
return endpointInfo.SerialNumber, nil
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// UnregisterEndpoint unregisters an existing endpoint
|
|
|
|
|
func (h *Manager) UnregisterEndpoint(ctx context.Context, endpointInfo EndpointInfo) error {
|
2025-11-29 17:42:42 +01:00
|
|
|
h.mu.Lock()
|
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
if endpoint, detected := h.DetectedEndpoints[endpointInfo.SerialNumber]; detected {
|
|
|
|
|
// Deactivating endpoint
|
|
|
|
|
err := endpoint.Deactivate(ctx)
|
2025-11-29 17:42:42 +01:00
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Err(err).Str("sn", endpointInfo.SerialNumber).Msg("unable to deactivate the endpoint")
|
2025-11-29 17:42:42 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
2025-11-30 18:57:20 +01:00
|
|
|
// Disconnecting endpoint
|
|
|
|
|
err = endpoint.Disconnect(ctx)
|
2025-11-29 17:42:42 +01:00
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Err(err).Str("sn", endpointInfo.SerialNumber).Msg("unable to disconnect the endpoint")
|
2025-11-29 17:42:42 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
2025-11-30 18:57:20 +01:00
|
|
|
// Remove the endpoint from its provider (if needed)
|
|
|
|
|
if provider, found := h.providers[endpointInfo.ProtocolName]; found {
|
|
|
|
|
err = provider.Remove(ctx, endpoint)
|
2025-11-30 18:33:00 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
2025-11-30 18:57:20 +01:00
|
|
|
delete(h.SavedEndpoints, endpointInfo.SerialNumber)
|
|
|
|
|
runtime.EventsEmit(ctx, string(EndpointUnload), endpointInfo)
|
2025-11-29 17:42:42 +01:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// GetEndpointSettings gets the endpoint settings
|
|
|
|
|
func (h *Manager) GetEndpointSettings(endpointSN string) (map[string]any, error) {
|
|
|
|
|
// Return the specified endpoint
|
|
|
|
|
endpoint, found := h.DetectedEndpoints[endpointSN]
|
2025-11-29 17:42:42 +01:00
|
|
|
if !found {
|
2025-11-30 18:57:20 +01:00
|
|
|
// Endpoint not detected, return the last settings saved
|
|
|
|
|
if savedEndpoint, isFound := h.SavedEndpoints[endpointSN]; isFound {
|
|
|
|
|
return savedEndpoint.Settings, nil
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
2025-11-30 18:57:20 +01:00
|
|
|
return nil, fmt.Errorf("unable to found the endpoint")
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
2025-11-30 18:57:20 +01:00
|
|
|
return endpoint.GetSettings(), nil
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// SetEndpointSettings sets the endpoint settings
|
|
|
|
|
func (h *Manager) SetEndpointSettings(ctx context.Context, endpointSN string, settings map[string]any) error {
|
|
|
|
|
endpoint, found := h.DetectedEndpoints[endpointSN]
|
2025-11-29 17:42:42 +01:00
|
|
|
if !found {
|
2025-11-30 18:57:20 +01:00
|
|
|
return fmt.Errorf("unable to found the FTDI endpoint")
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
2025-11-30 18:57:20 +01:00
|
|
|
return endpoint.SetSettings(ctx, settings)
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// Start starts to find new endpoint events
|
2025-11-24 19:23:50 +01:00
|
|
|
func (h *Manager) Start(ctx context.Context) error {
|
2025-11-30 18:57:20 +01:00
|
|
|
for providerName, provider := range h.providers {
|
2025-11-29 17:42:42 +01:00
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// Initialize the provider
|
|
|
|
|
err := provider.Initialize()
|
2025-01-18 14:53:29 +00:00
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Err(err).Str("file", "hardware").Str("providerName", providerName).Msg("unable to initialize provider")
|
2025-01-18 14:53:29 +00:00
|
|
|
return err
|
|
|
|
|
}
|
2025-11-29 17:42:42 +01:00
|
|
|
|
|
|
|
|
// Set callback functions
|
2025-11-30 18:57:20 +01:00
|
|
|
provider.OnArrival(h.OnEndpointArrival)
|
|
|
|
|
provider.OnRemoval(h.OnEndpointRemoval)
|
2025-11-29 17:42:42 +01:00
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// Start the provider
|
|
|
|
|
err = provider.Start(ctx)
|
2025-01-18 14:53:29 +00:00
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Err(err).Str("file", "hardware").Str("providerName", providerName).Msg("unable to start provider")
|
2025-01-18 14:53:29 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-29 17:42:42 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
2025-11-02 10:57:53 +01:00
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// OnEndpointArrival is called when a endpoint arrives in the system
|
|
|
|
|
func (h *Manager) OnEndpointArrival(ctx context.Context, endpoint Endpoint) {
|
|
|
|
|
// Add the endpoint to the detected hardware
|
|
|
|
|
h.DetectedEndpoints[endpoint.GetInfo().SerialNumber] = endpoint
|
2025-11-29 17:42:42 +01:00
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// If the endpoint is saved in the project, connect it
|
|
|
|
|
if _, saved := h.SavedEndpoints[endpoint.GetInfo().SerialNumber]; saved {
|
2025-11-29 17:42:42 +01:00
|
|
|
h.wg.Add(1)
|
2025-11-30 18:57:20 +01:00
|
|
|
go func(p Endpoint) {
|
2025-11-29 17:42:42 +01:00
|
|
|
defer h.wg.Done()
|
|
|
|
|
err := p.Connect(ctx)
|
|
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Err(err).Str("sn", p.GetInfo().SerialNumber).Msg("unable to connect the FTDI endpoint")
|
2025-01-18 14:53:29 +00:00
|
|
|
return
|
|
|
|
|
}
|
2025-11-29 17:42:42 +01:00
|
|
|
err = p.Activate(ctx)
|
|
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Err(err).Str("sn", p.GetInfo().SerialNumber).Msg("unable to activate the FTDI endpoint")
|
2025-11-29 17:42:42 +01:00
|
|
|
return
|
|
|
|
|
}
|
2025-11-30 18:57:20 +01:00
|
|
|
}(endpoint)
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// TODO: Update the Endpoint reference in the corresponding devices
|
2025-11-29 17:42:42 +01:00
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
runtime.EventsEmit(ctx, string(EndpointArrival), endpoint.GetInfo())
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// OnEndpointRemoval is called when a endpoint exits the system
|
|
|
|
|
func (h *Manager) OnEndpointRemoval(ctx context.Context, endpoint Endpoint) {
|
|
|
|
|
// Properly deactivating and disconnecting the endpoint
|
2025-11-29 17:42:42 +01:00
|
|
|
h.wg.Add(1)
|
2025-11-30 18:57:20 +01:00
|
|
|
go func(p Endpoint) {
|
2025-11-29 17:42:42 +01:00
|
|
|
defer h.wg.Done()
|
|
|
|
|
err := p.Deactivate(ctx)
|
|
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Err(err).Str("sn", p.GetInfo().SerialNumber).Msg("unable to deactivate endpoint after disconnection")
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
2025-11-29 17:42:42 +01:00
|
|
|
err = p.Disconnect(ctx)
|
|
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Err(err).Str("sn", p.GetInfo().SerialNumber).Msg("unable to disconnect the endpoint after disconnection")
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
2025-11-30 18:57:20 +01:00
|
|
|
}(endpoint)
|
2025-11-29 17:42:42 +01:00
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// Remove the endpoint from the hardware
|
|
|
|
|
delete(h.DetectedEndpoints, endpoint.GetInfo().SerialNumber)
|
2025-11-29 17:42:42 +01:00
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// TODO: Update the Endpoint reference in the corresponding devices
|
|
|
|
|
runtime.EventsEmit(ctx, string(EndpointRemoval), endpoint.GetInfo())
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// GetProvider returns a register provider
|
|
|
|
|
func (h *Manager) GetProvider(providerName string) (EndpointProvider, error) {
|
|
|
|
|
provider, exists := h.providers[providerName]
|
2025-01-18 14:53:29 +00:00
|
|
|
if !exists {
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Error().Str("file", "hardware").Str("providerName", providerName).Msg("unable to get the provider")
|
|
|
|
|
return nil, fmt.Errorf("unable to locate the '%s' provider", providerName)
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
2025-11-30 18:57:20 +01:00
|
|
|
log.Debug().Str("file", "hardware").Str("providerName", providerName).Msg("got provider")
|
|
|
|
|
return provider, nil
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// RegisterProvider registers a new endpoints provider
|
|
|
|
|
func (h *Manager) RegisterProvider(provider EndpointProvider) {
|
|
|
|
|
h.providers[provider.GetName()] = provider
|
|
|
|
|
log.Info().Str("file", "hardware").Str("providerName", provider.GetName()).Msg("provider registered")
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-02 10:57:53 +01:00
|
|
|
// WaitStop stops the hardware manager
|
2025-11-24 19:23:50 +01:00
|
|
|
func (h *Manager) WaitStop() error {
|
2025-11-02 10:57:53 +01:00
|
|
|
log.Trace().Str("file", "hardware").Msg("closing the hardware manager")
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// Stop each provider
|
2025-11-02 10:57:53 +01:00
|
|
|
var errs []error
|
2025-11-30 18:57:20 +01:00
|
|
|
for name, f := range h.providers {
|
2025-11-02 10:57:53 +01:00
|
|
|
if err := f.WaitStop(); err != nil {
|
|
|
|
|
errs = append(errs, fmt.Errorf("%s: %w", name, err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-30 18:57:20 +01:00
|
|
|
// Wait for all the endpoints to close
|
|
|
|
|
log.Trace().Str("file", "MIDIProvider").Msg("closing all MIDI endpoints")
|
|
|
|
|
for registeredEndpointSN, registeredEndpoint := range h.DetectedEndpoints {
|
|
|
|
|
err := registeredEndpoint.WaitStop()
|
2025-11-29 17:42:42 +01:00
|
|
|
if err != nil {
|
2025-11-30 18:57:20 +01:00
|
|
|
errs = append(errs, fmt.Errorf("%s: %w", registeredEndpointSN, err))
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-02 10:57:53 +01:00
|
|
|
// 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
|
|
|
|
|
}
|