|
| 1 | +/* |
| 2 | + * This file is part of arduino-cli. |
| 3 | + * |
| 4 | + * arduino-cli is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + * As a special exception, you may use this file as part of a free software |
| 19 | + * library without restriction. Specifically, if other files instantiate |
| 20 | + * templates or use macros or inline functions from this file, or you compile |
| 21 | + * this file and link it with other files to produce an executable, this |
| 22 | + * file does not by itself cause the resulting executable to be covered by |
| 23 | + * the GNU General Public License. This exception does not however |
| 24 | + * invalidate any other reasons why the executable file might be covered by |
| 25 | + * the GNU General Public License. |
| 26 | + * |
| 27 | + * Copyright 2017 ARDUINO AG (http://www.arduino.cc/) |
| 28 | + */ |
| 29 | + |
| 30 | +package output |
| 31 | + |
| 32 | +import ( |
| 33 | + "fmt" |
| 34 | + |
| 35 | + discovery "github.com/arduino/board-discovery" |
| 36 | + "github.com/bcmi-labs/arduino-modules/boards" |
| 37 | +) |
| 38 | + |
| 39 | +// SerialBoardListItem represents a board connected using serial port. |
| 40 | +type SerialBoardListItem struct { |
| 41 | + Name string `json:"name,required"` |
| 42 | + Fqbn string `json:"fqbn,required"` |
| 43 | + Port string `json:"port,required"` |
| 44 | + UsbID string `json:"usbID,reqiured"` |
| 45 | +} |
| 46 | + |
| 47 | +// NetworkBoardListItem represents a board connected via network. |
| 48 | +type NetworkBoardListItem struct { |
| 49 | + Name string `json:"name,required"` |
| 50 | + Fqbn string `json:"fqbn,required"` |
| 51 | + Location string `json:"location,required"` |
| 52 | +} |
| 53 | + |
| 54 | +// BoardList is a list of attached boards. |
| 55 | +type BoardList struct { |
| 56 | + SerialBoards []SerialBoardListItem `json:"serialBoards,required"` |
| 57 | + NetworkBoards []NetworkBoardListItem `json:"networkBoards,required"` |
| 58 | +} |
| 59 | + |
| 60 | +func (bl *BoardList) String() string { |
| 61 | + ret := fmt.Sprintln("DEVICES:") + |
| 62 | + fmt.Sprintln("SERIAL:") |
| 63 | + if len(bl.SerialBoards) == 0 { |
| 64 | + ret += fmt.Sprintln(" <none>") |
| 65 | + } else { |
| 66 | + for _, item := range bl.SerialBoards { |
| 67 | + ret += fmt.Sprintln(" - BOARD NAME:", item.Name) + |
| 68 | + fmt.Sprintln(" FQBN:", item.Fqbn) + |
| 69 | + fmt.Sprintln(" PORT:", item.Port) + |
| 70 | + fmt.Sprintln(" USB ID:", item.UsbID) |
| 71 | + } |
| 72 | + } |
| 73 | + ret += fmt.Sprintln("NETWORK:") |
| 74 | + if len(bl.NetworkBoards) == 0 { |
| 75 | + ret += fmt.Sprintln(" <none>") |
| 76 | + } else { |
| 77 | + for _, item := range bl.NetworkBoards { |
| 78 | + ret += fmt.Sprintln(" - BOARD NAME:", item.Name) + |
| 79 | + fmt.Sprintln(" FQBN:", item.Fqbn) + |
| 80 | + fmt.Sprintln(" LOCATION:", item.Location) |
| 81 | + } |
| 82 | + } |
| 83 | + return ret |
| 84 | +} |
| 85 | + |
| 86 | +// NewBoardList returns a new board list by adding discovered boards from the board list and a monitor. |
| 87 | +func NewBoardList(boards boards.Boards, monitor *discovery.Monitor) *BoardList { |
| 88 | + if monitor == nil || boards == nil { |
| 89 | + return nil |
| 90 | + } |
| 91 | + |
| 92 | + serialDevices := monitor.Serial() |
| 93 | + networkDevices := monitor.Network() |
| 94 | + ret := &BoardList{ |
| 95 | + SerialBoards: make([]SerialBoardListItem, 0, len(serialDevices)), |
| 96 | + NetworkBoards: make([]NetworkBoardListItem, 0, len(networkDevices)), |
| 97 | + } |
| 98 | + |
| 99 | + for _, item := range serialDevices { |
| 100 | + board := boards.ByVidPid(item.VendorID, item.ProductID) |
| 101 | + if board == nil { // skip it if not recognized |
| 102 | + continue |
| 103 | + } |
| 104 | + |
| 105 | + ret.SerialBoards = append(ret.SerialBoards, SerialBoardListItem{ |
| 106 | + Name: board.Name, |
| 107 | + Fqbn: board.Fqbn, |
| 108 | + Port: item.Port, |
| 109 | + UsbID: fmt.Sprintf("%s:%s - %s", item.ProductID[2:len(item.ProductID)-1], item.VendorID[2:len(item.VendorID)-1], item.SerialNumber), |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + for _, item := range networkDevices { |
| 114 | + board := boards.ByID(item.Name) |
| 115 | + if board == nil { // skip it if not recognized |
| 116 | + continue |
| 117 | + } |
| 118 | + |
| 119 | + ret.NetworkBoards = append(ret.NetworkBoards, NetworkBoardListItem{ |
| 120 | + Name: board.Name, |
| 121 | + Fqbn: board.Fqbn, |
| 122 | + Location: fmt.Sprintf("%s:%d", item.Address, item.Port), |
| 123 | + }) |
| 124 | + } |
| 125 | + return ret |
| 126 | +} |
0 commit comments