2025-01-18 14:53:29 +00:00
|
|
|
package hardware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
goRuntime "runtime"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
2025-11-01 12:23:22 +01:00
|
|
|
"unsafe"
|
2025-01-18 14:53:29 +00:00
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-01 12:23:22 +01:00
|
|
|
/*
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#cgo LDFLAGS: -L${SRCDIR}/../build/bin -ldetectFTDI
|
|
|
|
|
#include "cpp/include/detectFTDIBridge.h"
|
|
|
|
|
*/
|
|
|
|
|
import "C"
|
2025-01-18 14:53:29 +00:00
|
|
|
|
2025-11-02 10:57:53 +01:00
|
|
|
// FTDIFinder manages all the FTDI peripherals
|
2025-01-18 14:53:29 +00:00
|
|
|
type FTDIFinder struct {
|
2025-11-02 10:57:53 +01:00
|
|
|
wg sync.WaitGroup
|
2025-11-11 19:14:44 +00:00
|
|
|
mu sync.Mutex
|
2025-11-02 10:57:53 +01:00
|
|
|
|
2025-11-11 19:14:44 +00:00
|
|
|
detected map[string]*FTDIPeripheral // Detected peripherals
|
|
|
|
|
|
|
|
|
|
scanEvery time.Duration // Scans peripherals periodically
|
|
|
|
|
|
2025-11-29 20:06:04 +01:00
|
|
|
onArrival func(context.Context, Peripheral, bool) // When a peripheral arrives
|
|
|
|
|
onRemoval func(context.Context, Peripheral) // When a peripheral goes away
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewFTDIFinder creates a new FTDI finder
|
2025-11-11 19:14:44 +00:00
|
|
|
func NewFTDIFinder(scanEvery time.Duration) *FTDIFinder {
|
2025-01-18 14:53:29 +00:00
|
|
|
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder created")
|
|
|
|
|
return &FTDIFinder{
|
2025-11-11 19:14:44 +00:00
|
|
|
scanEvery: scanEvery,
|
|
|
|
|
detected: make(map[string]*FTDIPeripheral),
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-11 19:14:44 +00:00
|
|
|
// OnArrival is the callback function when a new peripheral arrives
|
2025-11-29 20:06:04 +01:00
|
|
|
func (f *FTDIFinder) OnArrival(cb func(context.Context, Peripheral, bool)) {
|
2025-11-11 19:14:44 +00:00
|
|
|
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)) {
|
2025-11-11 19:14:44 +00:00
|
|
|
f.onRemoval = cb
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
// 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)
|
2025-11-29 20:06:04 +01:00
|
|
|
func (f *FTDIFinder) Create(ctx context.Context, peripheralInfo PeripheralInfo) error {
|
|
|
|
|
return nil
|
2025-11-29 17:42:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove removes an existing peripheral (manually created)
|
|
|
|
|
func (f *FTDIFinder) Remove(ctx context.Context, peripheral Peripheral) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
// 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)
|
2025-01-18 14:53:29 +00:00
|
|
|
go func() {
|
2025-11-11 19:14:44 +00:00
|
|
|
ticker := time.NewTicker(f.scanEvery)
|
|
|
|
|
defer ticker.Stop()
|
2025-11-02 10:57:53 +01:00
|
|
|
defer f.wg.Done()
|
2025-11-11 19:14:44 +00:00
|
|
|
|
2025-01-18 14:53:29 +00:00
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
2025-11-11 19:14:44 +00:00
|
|
|
case <-ticker.C:
|
2025-01-18 14:53:29 +00:00
|
|
|
// 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")
|
|
|
|
|
|
2025-11-01 12:23:22 +01:00
|
|
|
count := int(C.get_peripherals_number())
|
2025-01-18 14:53:29 +00:00
|
|
|
|
2025-11-01 12:23:22 +01:00
|
|
|
log.Info().Int("number", count).Msg("number of FTDI devices connected")
|
2025-01-18 14:53:29 +00:00
|
|
|
|
2025-11-02 10:57:53 +01:00
|
|
|
// Allocating C array
|
2025-11-01 12:23:22 +01:00
|
|
|
size := C.size_t(count) * C.size_t(unsafe.Sizeof(C.FTDIPeripheralC{}))
|
|
|
|
|
devicesPtr := C.malloc(size)
|
|
|
|
|
defer C.free(devicesPtr)
|
2025-01-18 14:53:29 +00:00
|
|
|
|
2025-11-02 10:57:53 +01:00
|
|
|
devices := (*[1 << 20]C.FTDIPeripheralC)(devicesPtr)[:count:count]
|
2025-01-18 14:53:29 +00:00
|
|
|
|
2025-11-01 12:23:22 +01:00
|
|
|
C.get_ftdi_devices((*C.FTDIPeripheralC)(devicesPtr), C.int(count))
|
|
|
|
|
|
2025-11-11 19:14:44 +00:00
|
|
|
currentMap := make(map[string]PeripheralInfo)
|
2025-01-26 12:01:31 +01:00
|
|
|
|
2025-11-01 12:23:22 +01:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
|
d := devices[i]
|
2025-01-18 14:53:29 +00:00
|
|
|
|
2025-11-01 12:23:22 +01:00
|
|
|
sn := C.GoString(d.serialNumber)
|
|
|
|
|
desc := C.GoString(d.description)
|
2025-11-11 19:14:44 +00:00
|
|
|
// isOpen := d.isOpen != 0
|
2025-01-18 14:53:29 +00:00
|
|
|
|
2025-11-11 19:14:44 +00:00
|
|
|
currentMap[sn] = PeripheralInfo{
|
2025-11-01 12:23:22 +01:00
|
|
|
SerialNumber: sn,
|
|
|
|
|
Name: desc,
|
2025-11-11 19:14:44 +00:00
|
|
|
// IsOpen: isOpen,
|
2025-11-01 12:23:22 +01:00
|
|
|
ProtocolName: "FTDI",
|
2025-01-26 12:01:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-02 10:57:53 +01:00
|
|
|
// Free C memory
|
2025-11-01 12:23:22 +01:00
|
|
|
C.free_ftdi_device(&d)
|
2025-01-18 14:53:29 +00:00
|
|
|
}
|
2025-11-01 12:23:22 +01:00
|
|
|
|
2025-11-11 19:14:44 +00:00
|
|
|
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
|
2025-11-11 19:14:44 +00:00
|
|
|
if _, known := f.detected[sn]; !known {
|
2025-11-29 17:42:42 +01:00
|
|
|
|
2025-11-11 19:14:44 +00:00
|
|
|
peripheral := NewFTDIPeripheral(peripheralData)
|
|
|
|
|
|
|
|
|
|
if f.onArrival != nil {
|
2025-11-29 20:06:04 +01:00
|
|
|
f.onArrival(ctx, peripheral, false)
|
2025-11-11 19:14:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Detect removals
|
2025-11-29 17:42:42 +01:00
|
|
|
for detectedSN, detectedPeripheral := range f.detected {
|
|
|
|
|
if _, still := currentMap[detectedSN]; !still {
|
2025-11-01 12:23:22 +01:00
|
|
|
|
2025-11-11 19:14:44 +00:00
|
|
|
// Delete it from the detected list
|
2025-11-29 17:42:42 +01:00
|
|
|
delete(f.detected, detectedSN)
|
2025-11-11 19:14:44 +00:00
|
|
|
|
|
|
|
|
// Execute the removal callback
|
|
|
|
|
if f.onRemoval != nil {
|
2025-11-29 17:42:42 +01:00
|
|
|
f.onRemoval(ctx, detectedPeripheral)
|
2025-11-11 19:14:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-18 14:53:29 +00:00
|
|
|
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
|
|
|
|
|
}
|