// This file is part of pluggable-discovery-protocol-handler. // // SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies // SPDX-License-Identifier: GPL-3.0-or-later package discovery import ( "encoding/json" "errors" "fmt" "io" "strings" "sync" "time" "github.com/arduino/go-paths-helper" ) // Client is a tool that detects communication ports to interact // with the boards. type Client struct { id string processArgs []string process *paths.Process outgoingCommandsPipe io.Writer incomingMessagesChan <-chan *discoveryMessage userAgent string logger ClientLogger // All the following fields are guarded by statusMutex statusMutex sync.Mutex incomingMessagesError error eventChan chan<- *Event } // ClientLogger is the interface that must be implemented by a logger // to be used in the discovery client. type ClientLogger interface { Debugf(format string, args ...any) Errorf(format string, args ...any) } type nullClientLogger struct{} func (l *nullClientLogger) Debugf(_ string, _ ...any) {} func (l *nullClientLogger) Errorf(_ string, _ ...any) {} type discoveryMessage struct { EventType string `json:"eventType"` Message string `json:"message"` Error bool `json:"error"` ProtocolVersion int `json:"protocolVersion"` // Used in HELLO command Ports []*Port `json:"ports"` // Used in LIST command Port *Port `json:"port"` // Used in add and remove events } func (msg discoveryMessage) String() string { s := fmt.Sprintf("type: %s", msg.EventType) if msg.Message != "" { s += fmt.Sprintf(", message: %s", msg.Message) } if msg.ProtocolVersion != 0 { s += fmt.Sprintf(", protocol version: %d", msg.ProtocolVersion) } if len(msg.Ports) > 0 { s += fmt.Sprintf(", ports: %s", msg.Ports) } if msg.Port != nil { s += fmt.Sprintf(", port: %s", msg.Port) } return s } // Event is a pluggable discovery event type Event struct { Type string Port *Port DiscoveryID string } // NewClient create a new pluggable discovery client func NewClient(id string, args ...string) *Client { return &Client{ id: id, processArgs: args, userAgent: "pluggable-discovery-protocol-handler", logger: &nullClientLogger{}, } } // SetUserAgent sets the user agent to be used in the discovery func (disc *Client) SetUserAgent(userAgent string) { disc.userAgent = userAgent } // SetLogger sets the logger to be used in the discovery func (disc *Client) SetLogger(logger ClientLogger) { disc.logger = logger } // GetID returns the identifier for this discovery func (disc *Client) GetID() string { return disc.id } func (disc *Client) String() string { return disc.id } func (disc *Client) jsonDecodeLoop(in io.Reader, outChan chan<- *discoveryMessage) { decoder := json.NewDecoder(in) closeAndReportError := func(err error) { disc.statusMutex.Lock() disc.incomingMessagesError = err disc.stopSync() disc.killProcess() disc.statusMutex.Unlock() close(outChan) if err != nil { disc.logger.Errorf("Stopped decode loop: %v", err) } else { disc.logger.Debugf("Stopped decode loop") } } for { var msg discoveryMessage if err := decoder.Decode(&msg); err != nil { closeAndReportError(err) return } disc.logger.Debugf("Received message %s", msg) switch msg.EventType { case "add": if msg.Port == nil { closeAndReportError(errors.New("invalid 'add' message: missing port")) return } disc.statusMutex.Lock() if disc.eventChan != nil { disc.eventChan <- &Event{"add", msg.Port, disc.GetID()} } disc.statusMutex.Unlock() case "remove": if msg.Port == nil { closeAndReportError(errors.New("invalid 'remove' message: missing port")) return } disc.statusMutex.Lock() if disc.eventChan != nil { disc.eventChan <- &Event{"remove", msg.Port, disc.GetID()} } disc.statusMutex.Unlock() default: outChan <- &msg } } } // Alive returns true if the discovery is running and false otherwise. func (disc *Client) Alive() bool { disc.statusMutex.Lock() defer disc.statusMutex.Unlock() return disc.process != nil } func (disc *Client) waitMessage(timeout time.Duration) (*discoveryMessage, error) { select { case msg := <-disc.incomingMessagesChan: if msg == nil { disc.statusMutex.Lock() err := disc.incomingMessagesError disc.statusMutex.Unlock() return nil, err } return msg, nil case <-time.After(timeout): return nil, fmt.Errorf("timeout waiting for message from %s", disc) } } func (disc *Client) sendCommand(command string) error { disc.logger.Debugf("Sending command %s", strings.TrimSpace(command)) data := []byte(command) for { n, err := disc.outgoingCommandsPipe.Write(data) if err != nil { return err } if n == len(data) { return nil } data = data[n:] } } func (disc *Client) runProcess() error { disc.logger.Debugf("Starting discovery process") proc, err := paths.NewProcess(nil, disc.processArgs...) if err != nil { return err } stdout, err := proc.StdoutPipe() if err != nil { return err } stdin, err := proc.StdinPipe() if err != nil { return err } disc.outgoingCommandsPipe = stdin messageChan := make(chan *discoveryMessage) disc.incomingMessagesChan = messageChan go disc.jsonDecodeLoop(stdout, messageChan) if err := proc.Start(); err != nil { return err } disc.statusMutex.Lock() defer disc.statusMutex.Unlock() disc.process = proc disc.logger.Debugf("Discovery process started") return nil } func (disc *Client) killProcess() { disc.logger.Debugf("Killing discovery process") if process := disc.process; process != nil { disc.process = nil if err := process.Kill(); err != nil { disc.logger.Errorf("Killing discovery process: %v", err) } if err := process.Wait(); err != nil { disc.logger.Errorf("Waiting discovery process termination: %v", err) } } disc.logger.Debugf("Discovery process killed") } // Run starts the discovery executable process and sends the HELLO command to the discovery to agree on the // pluggable discovery protocol. This must be the first command to run in the communication with the discovery. // If the process is started but the HELLO command fails the process is killed. func (disc *Client) Run() (err error) { if err = disc.runProcess(); err != nil { return err } defer func() { // If the discovery process is started successfully but the HELLO handshake // fails the discovery is an unusable state, we kill the process to avoid // further issues down the line. if err == nil { return } disc.statusMutex.Lock() disc.killProcess() disc.statusMutex.Unlock() }() if err = disc.sendCommand("HELLO 1 \"arduino-cli " + disc.userAgent + "\"\n"); err != nil { return err } if msg, err := disc.waitMessage(time.Second * 10); err != nil { return fmt.Errorf("calling HELLO: %w", err) } else if msg.EventType != "hello" { return fmt.Errorf("event out of sync, expected 'hello', received '%s'", msg.EventType) } else if msg.Error { return fmt.Errorf("command failed: %s", msg.Message) } else if strings.ToUpper(msg.Message) != "OK" { return fmt.Errorf("communication out of sync, expected 'OK', received '%s'", msg.Message) } else if msg.ProtocolVersion > 1 { return fmt.Errorf("protocol version not supported: requested 1, got %d", msg.ProtocolVersion) } return nil } // Start initializes and start the discovery internal subroutines. This command must be // called before List. func (disc *Client) Start() error { if err := disc.sendCommand("START\n"); err != nil { return err } if msg, err := disc.waitMessage(time.Second * 10); err != nil { return fmt.Errorf("calling START: %w", err) } else if msg.EventType != "start" { return fmt.Errorf("event out of sync, expected 'start', received '%s'", msg.EventType) } else if msg.Error { return fmt.Errorf("command failed: %s", msg.Message) } else if strings.ToUpper(msg.Message) != "OK" { return fmt.Errorf("communication out of sync, expected 'OK', received '%s'", msg.Message) } return nil } // Stop stops the discovery internal subroutines and possibly free the internally // used resources. This command should be called if the client wants to pause the // discovery for a while. func (disc *Client) Stop() error { if err := disc.sendCommand("STOP\n"); err != nil { return err } if msg, err := disc.waitMessage(time.Second * 10); err != nil { return fmt.Errorf("calling STOP: %w", err) } else if msg.EventType != "stop" { return fmt.Errorf("event out of sync, expected 'stop', received '%s'", msg.EventType) } else if msg.Error { return fmt.Errorf("command failed: %s", msg.Message) } else if strings.ToUpper(msg.Message) != "OK" { return fmt.Errorf("communication out of sync, expected 'OK', received '%s'", msg.Message) } disc.statusMutex.Lock() defer disc.statusMutex.Unlock() disc.stopSync() return nil } func (disc *Client) stopSync() { if disc.eventChan != nil { disc.eventChan <- &Event{"stop", nil, disc.GetID()} close(disc.eventChan) disc.eventChan = nil } } // Quit terminates the discovery. No more commands can be accepted by the discovery. func (disc *Client) Quit() { _ = disc.sendCommand("QUIT\n") if _, err := disc.waitMessage(time.Second * 5); err != nil { disc.logger.Errorf("Quitting discovery: %s", err) } disc.statusMutex.Lock() disc.stopSync() disc.killProcess() disc.statusMutex.Unlock() } // List executes an enumeration of the ports and returns a list of the available // ports at the moment of the call. func (disc *Client) List() ([]*Port, error) { if err := disc.sendCommand("LIST\n"); err != nil { return nil, err } msg, err := disc.waitMessage(time.Second * 10) if err != nil { return nil, fmt.Errorf("calling LIST: %w", err) } else if msg.EventType != "list" { return nil, fmt.Errorf("event out of sync, expected 'list', received '%s'", msg.EventType) } else if msg.Error { return nil, fmt.Errorf("command failed: %s", msg.Message) } return msg.Ports, nil } // StartSync puts the discovery in "events" mode: the discovery will send "add" // and "remove" events each time a new port is detected or removed respectively. // After calling StartSync an initial burst of "add" events may be generated to // report all the ports available at the moment of the start. // It also creates a channel used to receive events from the pluggable discovery. // The event channel must be consumed as quickly as possible since it may block the // discovery if it becomes full. The channel size is configurable. func (disc *Client) StartSync(size int) (eventChan <-chan *Event, retErr error) { // Create the event channel *before* sending START_SYNC. // // A discovery may emit its initial "add" events very quickly after receiving the // START_SYNC command, and those events can reach the client's decode loop // before disc.waitMessage returns (the discovery emits events from // a separate goroutine, so the "start_sync OK" and the "add" reply race on the // wire). If the event channel is still nil when such an "add" is decoded, // jsonDecodeLoop silently drops it (see the eventChan nil checks there), and // the port is lost until the next re-sync. Creating the channel up-front // guarantees these initial events are delivered. disc.statusMutex.Lock() disc.stopSync() c := make(chan *Event, size) disc.eventChan = c disc.statusMutex.Unlock() // If START_SYNC does not complete successfully, tear down the event channel we // just created so we don't leave a dangling channel that jsonDecodeLoop would // keep feeding. defer func() { if retErr != nil { disc.statusMutex.Lock() disc.stopSync() disc.statusMutex.Unlock() } }() if err := disc.sendCommand("START_SYNC\n"); err != nil { return nil, err } if msg, err := disc.waitMessage(time.Second * 10); err != nil { return nil, fmt.Errorf("calling START_SYNC: %w", err) } else if msg.EventType != "start_sync" { return nil, fmt.Errorf("evemt out of sync, expected 'start_sync', received '%s'", msg.EventType) } else if msg.Error { return nil, fmt.Errorf("command failed: %s", msg.Message) } else if strings.ToUpper(msg.Message) != "OK" { return nil, fmt.Errorf("communication out of sync, expected 'OK', received '%s'", msg.Message) } return c, nil }