Files
dmxconnect/hardware/FTDIFinder.go

181 lines
4.5 KiB
Go
Raw Normal View History

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"
2025-11-02 10:57:53 +01:00
// FTDIFinder manages all the FTDI peripherals
type FTDIFinder struct {
2025-11-02 10:57:53 +01:00
wg sync.WaitGroup
mu sync.Mutex
2025-11-02 10:57:53 +01:00
detected map[string]*FTDIPeripheral // Detected peripherals
scanEvery time.Duration // Scans peripherals periodically
2025-11-29 17:42:42 +01:00
onArrival func(context.Context, Peripheral) // When a peripheral arrives
onRemoval func(context.Context, Peripheral) // When a peripheral goes away
}
// NewFTDIFinder creates a new FTDI finder
func NewFTDIFinder(scanEvery time.Duration) *FTDIFinder {
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder created")
return &FTDIFinder{
scanEvery: scanEvery,
detected: make(map[string]*FTDIPeripheral),
}
}
// OnArrival is the callback function when a new peripheral arrives
2025-11-29 17:42:42 +01:00
func (f *FTDIFinder) OnArrival(cb func(context.Context, Peripheral)) {
f.onArrival = cb
}
// OnRemoval i the callback when a peripheral goes away
2025-11-29 17:42:42 +01:00
func (f *FTDIFinder) OnRemoval(cb func(context.Context, Peripheral)) {
f.onRemoval = cb
}
// Initialize initializes the FTDI finder
func (f *FTDIFinder) Initialize() error {
// Check platform
if goRuntime.GOOS != "windows" {
log.Error().Str("file", "FTDIFinder").Str("platform", goRuntime.GOOS).Msg("FTDI finder not compatible with your platform")
return fmt.Errorf("the FTDI finder is not compatible with your platform yet (%s)", goRuntime.GOOS)
}
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder initialized")
return nil
}
2025-11-29 17:42:42 +01:00
// Create creates a new peripheral, based on the peripheral information (manually created)
func (f *FTDIFinder) Create(ctx context.Context, peripheralInfo PeripheralInfo) (string, error) {
return "", nil
}
// Remove removes an existing peripheral (manually created)
func (f *FTDIFinder) Remove(ctx context.Context, peripheral Peripheral) error {
return nil
}
// Start starts the finder and search for peripherals
func (f *FTDIFinder) Start(ctx context.Context) error {
2025-11-02 10:57:53 +01:00
f.wg.Add(1)
go func() {
ticker := time.NewTicker(f.scanEvery)
defer ticker.Stop()
2025-11-02 10:57:53 +01:00
defer f.wg.Done()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
// Scan the peripherals
err := f.scanPeripherals(ctx)
if err != nil {
log.Err(err).Str("file", "FTDIFinder").Msg("unable to scan FTDI peripherals")
}
}
}
}()
return nil
}
// GetName returns the name of the driver
func (f *FTDIFinder) GetName() string {
return "FTDI"
}
// scanPeripherals scans the FTDI peripherals
func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
log.Trace().Str("file", "FTDIFinder").Msg("FTDI scan triggered")
count := int(C.get_peripherals_number())
log.Info().Int("number", count).Msg("number of FTDI devices connected")
2025-11-02 10:57:53 +01:00
// Allocating C array
size := C.size_t(count) * C.size_t(unsafe.Sizeof(C.FTDIPeripheralC{}))
devicesPtr := C.malloc(size)
defer C.free(devicesPtr)
2025-11-02 10:57:53 +01:00
devices := (*[1 << 20]C.FTDIPeripheralC)(devicesPtr)[:count:count]
C.get_ftdi_devices((*C.FTDIPeripheralC)(devicesPtr), C.int(count))
currentMap := make(map[string]PeripheralInfo)
2025-01-26 12:01:31 +01:00
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] = PeripheralInfo{
SerialNumber: sn,
Name: desc,
// IsOpen: isOpen,
ProtocolName: "FTDI",
2025-01-26 12:01:31 +01:00
}
2025-11-02 10:57:53 +01:00
// Free C memory
C.free_ftdi_device(&d)
}
log.Info().Any("peripherals", currentMap).Msg("available FTDI peripherals")
// Detect arrivals
for sn, peripheralData := range currentMap {
2025-11-29 17:42:42 +01:00
// If the scanned peripheral isn't in the detected list, create it
if _, known := f.detected[sn]; !known {
2025-11-29 17:42:42 +01:00
peripheral := NewFTDIPeripheral(peripheralData)
if f.onArrival != nil {
2025-11-29 17:42:42 +01:00
f.onArrival(ctx, peripheral)
}
}
}
// Detect removals
2025-11-29 17:42:42 +01:00
for detectedSN, detectedPeripheral := range f.detected {
if _, still := currentMap[detectedSN]; !still {
// Delete it from the detected list
2025-11-29 17:42:42 +01:00
delete(f.detected, detectedSN)
// Execute the removal callback
if f.onRemoval != nil {
2025-11-29 17:42:42 +01:00
f.onRemoval(ctx, detectedPeripheral)
}
}
}
return nil
}
2025-11-02 10:57:53 +01:00
// WaitStop stops the finder
func (f *FTDIFinder) WaitStop() error {
log.Trace().Str("file", "FTDIFinder").Msg("stopping the FTDI finder...")
// Wait for goroutines to stop
f.wg.Wait()
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder stopped")
return nil
}