|
18 | 18 | package main
|
19 | 19 |
|
20 | 20 | import (
|
| 21 | + "errors" |
21 | 22 | "fmt"
|
| 23 | + "io" |
22 | 24 | "os"
|
| 25 | + "strconv" |
23 | 26 |
|
24 |
| - discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2" |
| 27 | + monitor "github.com/arduino/pluggable-monitor-protocol-handler" |
25 | 28 | "github.com/arduino/serial-monitor/args"
|
26 |
| - "github.com/arduino/serial-monitor/sync" |
27 | 29 | "github.com/arduino/serial-monitor/version"
|
| 30 | + "go.bug.st/serial" |
28 | 31 | )
|
29 | 32 |
|
| 33 | +var serialSettings = &monitor.PortDescriptor{ |
| 34 | + Protocol: "serial", |
| 35 | + ConfigurationParameter: map[string]*monitor.PortParameterDescriptor{ |
| 36 | + "baudrate": { |
| 37 | + Label: "Baudrate", |
| 38 | + Type: "enum", |
| 39 | + Values: []string{"300", "600", "750", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400", "460800", "500000", "921600", "1000000", "2000000"}, |
| 40 | + Selected: "9600", |
| 41 | + }, |
| 42 | + "parity": { |
| 43 | + Label: "Parity", |
| 44 | + Type: "enum", |
| 45 | + Values: []string{"N", "E", "O", "M", "S"}, |
| 46 | + Selected: "N", |
| 47 | + }, |
| 48 | + "bits": { |
| 49 | + Label: "Data bits", |
| 50 | + Type: "enum", |
| 51 | + Values: []string{"5", "6", "7", "8", "9"}, |
| 52 | + Selected: "8", |
| 53 | + }, |
| 54 | + "stop_bits": { |
| 55 | + Label: "Stop bits", |
| 56 | + Type: "enum", |
| 57 | + Values: []string{"1", "1.5", "2"}, |
| 58 | + Selected: "1", |
| 59 | + }, |
| 60 | + }, |
| 61 | +} |
| 62 | + |
| 63 | +var openedPort serial.Port |
| 64 | + |
30 | 65 | func main() {
|
31 | 66 | args.Parse()
|
32 | 67 | if args.ShowVersion {
|
33 | 68 | fmt.Printf("%s\n", version.VersionInfo)
|
34 | 69 | return
|
35 | 70 | }
|
36 | 71 |
|
37 |
| - serialDisc := &SerialDiscovery{} |
38 |
| - disc := discovery.NewServer(serialDisc) |
39 |
| - if err := disc.Run(os.Stdin, os.Stdout); err != nil { |
| 72 | + serialMonitor := &SerialMonitor{} |
| 73 | + monitorServer := monitor.NewServer(serialMonitor) |
| 74 | + if err := monitorServer.Run(os.Stdin, os.Stdout); err != nil { |
40 | 75 | fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
|
41 | 76 | os.Exit(1)
|
42 | 77 | }
|
43 | 78 | }
|
44 | 79 |
|
45 |
| -// SerialDiscovery is the implementation of the serial ports pluggable-discovery |
46 |
| -type SerialDiscovery struct { |
47 |
| - closeChan chan<- bool |
| 80 | +// SerialMonitor is the implementation of the serial ports pluggable-monitor |
| 81 | +type SerialMonitor struct { |
| 82 | + closeChan chan<- bool //TODO maybe useless |
48 | 83 | }
|
49 | 84 |
|
50 |
| -// Hello is the handler for the pluggable-discovery HELLO command |
51 |
| -func (d *SerialDiscovery) Hello(userAgent string, protocolVersion int) error { |
| 85 | +// Hello is the handler for the pluggable-monitor HELLO command |
| 86 | +func (d *SerialMonitor) Hello(userAgent string, protocol int) error { |
52 | 87 | return nil
|
53 | 88 | }
|
54 | 89 |
|
55 |
| -// Quit is the handler for the pluggable-discovery QUIT command |
56 |
| -func (d *SerialDiscovery) Quit() { |
| 90 | +// Describe is the handler for the pluggable-monitor DESCRIBE command |
| 91 | +func (d *SerialMonitor) Describe() (*monitor.PortDescriptor, error) { |
| 92 | + return serialSettings, nil |
57 | 93 | }
|
58 | 94 |
|
59 |
| -// Stop is the handler for the pluggable-discovery STOP command |
60 |
| -func (d *SerialDiscovery) Stop() error { |
61 |
| - if d.closeChan != nil { |
62 |
| - d.closeChan <- true |
63 |
| - close(d.closeChan) |
64 |
| - d.closeChan = nil |
| 95 | +// Configure is the handler for the pluggable-monitor CONFIGURE command |
| 96 | +func (d *SerialMonitor) Configure(parameterName string, value string) error { |
| 97 | + if serialSettings.ConfigurationParameter[parameterName] == nil { |
| 98 | + return fmt.Errorf("could not find parameter named %s", parameterName) |
65 | 99 | }
|
66 |
| - return nil |
| 100 | + values := serialSettings.ConfigurationParameter[parameterName].Values |
| 101 | + for _, i := range values { |
| 102 | + if i == value { |
| 103 | + serialSettings.ConfigurationParameter[parameterName].Selected = value |
| 104 | + if openedPort != nil { |
| 105 | + err := openedPort.SetMode(getMode()) |
| 106 | + if err != nil { |
| 107 | + return errors.New(err.Error()) |
| 108 | + |
| 109 | + } |
| 110 | + } |
| 111 | + return nil |
| 112 | + } |
| 113 | + } |
| 114 | + return fmt.Errorf("invalid value for parameter %s: %s", parameterName, value) |
67 | 115 | }
|
68 | 116 |
|
69 |
| -// StartSync is the handler for the pluggable-discovery START_SYNC command |
70 |
| -func (d *SerialDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error { |
71 |
| - close, err := sync.Start(eventCB, errorCB) |
| 117 | +// Open is the handler for the pluggable-monitor OPEN command |
| 118 | +func (d *SerialMonitor) Open(boardPort string) (io.ReadWriter, error) { |
| 119 | + if openedPort != nil { |
| 120 | + return nil, fmt.Errorf("port already opened: %s", boardPort) |
| 121 | + } |
| 122 | + openedPort, err := serial.Open(boardPort, getMode()) |
72 | 123 | if err != nil {
|
73 |
| - return err |
| 124 | + fmt.Print(boardPort) |
| 125 | + openedPort = nil |
| 126 | + return nil, errors.New(err.Error()) |
| 127 | + |
74 | 128 | }
|
75 |
| - d.closeChan = close |
| 129 | + return openedPort, nil |
| 130 | +} |
| 131 | + |
| 132 | +// Close is the handler for the pluggable-monitor CLOSE command |
| 133 | +func (d *SerialMonitor) Close() error { |
| 134 | + if openedPort == nil { |
| 135 | + return errors.New("port already closed") |
| 136 | + } |
| 137 | + openedPort.Close() |
| 138 | + openedPort = nil |
76 | 139 | return nil
|
77 | 140 | }
|
| 141 | + |
| 142 | +// Quit is the handler for the pluggable-monitor QUIT command |
| 143 | +func (d *SerialMonitor) Quit() {} |
| 144 | + |
| 145 | +func getMode() *serial.Mode { |
| 146 | + baud, _ := strconv.Atoi(serialSettings.ConfigurationParameter["baudrate"].Selected) |
| 147 | + parity, _ := strconv.Atoi(serialSettings.ConfigurationParameter["parity"].Selected) |
| 148 | + dataBits, _ := strconv.Atoi(serialSettings.ConfigurationParameter["bits"].Selected) |
| 149 | + stopBits, _ := strconv.Atoi(serialSettings.ConfigurationParameter["stop_bits"].Selected) |
| 150 | + |
| 151 | + mode := &serial.Mode{ |
| 152 | + BaudRate: baud, |
| 153 | + Parity: serial.Parity(parity), |
| 154 | + DataBits: dataBits, |
| 155 | + StopBits: serial.StopBits(stopBits), |
| 156 | + } |
| 157 | + return mode |
| 158 | +} |
0 commit comments