renaming Finders and Peripherals to Providers and Endpoints

This commit is contained in:
2025-11-30 18:57:20 +01:00
parent 7f60f7b8d7
commit 1c8607800a
25 changed files with 1102 additions and 1102 deletions

180
hardware/FTDIProvider.go Normal file
View File

@@ -0,0 +1,180 @@
package hardware
import (
"context"
"fmt"
goRuntime "runtime"
"sync"
"time"
"unsafe"
"github.com/rs/zerolog/log"
)
/*
#include <stdlib.h>
#cgo LDFLAGS: -L${SRCDIR}/../build/bin -ldetectFTDI
#include "cpp/include/detectFTDIBridge.h"
*/
import "C"
// FTDIProvider manages all the FTDI endpoints
type FTDIProvider struct {
wg sync.WaitGroup
mu sync.Mutex
detected map[string]*FTDIEndpoint // Detected endpoints
scanEvery time.Duration // Scans endpoints periodically
onArrival func(context.Context, Endpoint) // When a endpoint arrives
onRemoval func(context.Context, Endpoint) // When a endpoint goes away
}
// NewFTDIProvider creates a new FTDI provider
func NewFTDIProvider(scanEvery time.Duration) *FTDIProvider {
log.Trace().Str("file", "FTDIProvider").Msg("FTDI provider created")
return &FTDIProvider{
scanEvery: scanEvery,
detected: make(map[string]*FTDIEndpoint),
}
}
// OnArrival is the callback function when a new endpoint arrives
func (f *FTDIProvider) OnArrival(cb func(context.Context, Endpoint)) {
f.onArrival = cb
}
// OnRemoval i the callback when a endpoint goes away
func (f *FTDIProvider) OnRemoval(cb func(context.Context, Endpoint)) {
f.onRemoval = cb
}
// Initialize initializes the FTDI provider
func (f *FTDIProvider) Initialize() error {
// Check platform
if goRuntime.GOOS != "windows" {
log.Error().Str("file", "FTDIProvider").Str("platform", goRuntime.GOOS).Msg("FTDI provider not compatible with your platform")
return fmt.Errorf("the FTDI provider is not compatible with your platform yet (%s)", goRuntime.GOOS)
}
log.Trace().Str("file", "FTDIProvider").Msg("FTDI provider initialized")
return nil
}
// Create creates a new endpoint, based on the endpoint information (manually created)
func (f *FTDIProvider) Create(ctx context.Context, endpointInfo EndpointInfo) (EndpointInfo, error) {
return EndpointInfo{}, nil
}
// Remove removes an existing endpoint (manually created)
func (f *FTDIProvider) Remove(ctx context.Context, endpoint Endpoint) error {
return nil
}
// Start starts the provider and search for endpoints
func (f *FTDIProvider) Start(ctx context.Context) error {
f.wg.Add(1)
go func() {
ticker := time.NewTicker(f.scanEvery)
defer ticker.Stop()
defer f.wg.Done()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
// Scan the endpoints
err := f.scanEndpoints(ctx)
if err != nil {
log.Err(err).Str("file", "FTDIProvider").Msg("unable to scan FTDI endpoints")
}
}
}
}()
return nil
}
// GetName returns the name of the driver
func (f *FTDIProvider) GetName() string {
return "FTDI"
}
// scanEndpoints scans the FTDI endpoints
func (f *FTDIProvider) scanEndpoints(ctx context.Context) error {
log.Trace().Str("file", "FTDIProvider").Msg("FTDI scan triggered")
count := int(C.get_endpoints_number())
log.Info().Int("number", count).Msg("number of FTDI devices connected")
// Allocating C array
size := C.size_t(count) * C.size_t(unsafe.Sizeof(C.FTDIEndpointC{}))
devicesPtr := C.malloc(size)
defer C.free(devicesPtr)
devices := (*[1 << 20]C.FTDIEndpointC)(devicesPtr)[:count:count]
C.get_ftdi_devices((*C.FTDIEndpointC)(devicesPtr), C.int(count))
currentMap := make(map[string]EndpointInfo)
for i := 0; i < count; i++ {
d := devices[i]
sn := C.GoString(d.serialNumber)
desc := C.GoString(d.description)
// isOpen := d.isOpen != 0
currentMap[sn] = EndpointInfo{
SerialNumber: sn,
Name: desc,
// IsOpen: isOpen,
ProtocolName: "FTDI",
}
// Free C memory
C.free_ftdi_device(&d)
}
log.Info().Any("endpoints", currentMap).Msg("available FTDI endpoints")
// Detect arrivals
for sn, endpointData := range currentMap {
// If the scanned endpoint isn't in the detected list, create it
if _, known := f.detected[sn]; !known {
endpoint := NewFTDIEndpoint(endpointData)
if f.onArrival != nil {
f.onArrival(ctx, endpoint)
}
}
}
// Detect removals
for detectedSN, detectedEndpoint := range f.detected {
if _, still := currentMap[detectedSN]; !still {
// Delete it from the detected list
delete(f.detected, detectedSN)
// Execute the removal callback
if f.onRemoval != nil {
f.onRemoval(ctx, detectedEndpoint)
}
}
}
return nil
}
// WaitStop stops the provider
func (f *FTDIProvider) WaitStop() error {
log.Trace().Str("file", "FTDIProvider").Msg("stopping the FTDI provider...")
// Wait for goroutines to stop
f.wg.Wait()
log.Trace().Str("file", "FTDIProvider").Msg("FTDI provider stopped")
return nil
}