github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/command/device/board.go (about) 1 // This file is part of arduino-cloud-cli. 2 // 3 // Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published 7 // by the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 package device 19 20 import ( 21 "strings" 22 23 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" 24 ) 25 26 var ( 27 cryptoFQBN = []string{ 28 "arduino:samd:nano_33_iot", 29 "arduino:samd:mkrwifi1010", 30 "arduino:mbed_nano:nanorp2040connect", 31 "arduino:mbed_portenta:envie_m7", 32 "arduino:mbed_nicla:nicla_vision", 33 "arduino:samd:mkr1000", 34 "arduino:samd:mkrgsm1400", 35 "arduino:samd:mkrnb1500", 36 "arduino:mbed_opta:opta", 37 "arduino:mbed_giga:giga", 38 } 39 loraFQBN = []string{ 40 "arduino:samd:mkrwan1310", 41 "arduino:samd:mkrwan1300", 42 } 43 ) 44 45 // board contains details of a physical arduino board. 46 type board struct { 47 fqbn string 48 serial string 49 dType string 50 address string 51 protocol string 52 } 53 54 // isCrypto checks if the board is a valid arduino board with a 55 // supported crypto-chip. 56 func (b *board) isCrypto() bool { 57 for _, f := range cryptoFQBN { 58 if b.fqbn == f { 59 return true 60 } 61 } 62 return false 63 } 64 65 // isLora checks if the board is a valid LoRa arduino board. 66 func (b *board) isLora() bool { 67 for _, f := range loraFQBN { 68 if b.fqbn == f { 69 return true 70 } 71 } 72 return false 73 } 74 75 // boardFromPorts returns a board that matches all the criteria 76 // passed in. If no criteria are passed, it returns the first board found. 77 func boardFromPorts(ports []*rpc.DetectedPort, params *CreateParams) *board { 78 for _, port := range ports { 79 if portFilter(port, params) { 80 continue 81 } 82 boardFound := boardFilter(port.MatchingBoards, params) 83 if boardFound != nil { 84 b := &board{ 85 fqbn: boardFound.Fqbn, 86 serial: port.Port.Properties["serialNumber"], 87 dType: strings.Split(boardFound.Fqbn, ":")[2], 88 address: port.Port.Address, 89 protocol: port.Port.Protocol, 90 } 91 return b 92 } 93 } 94 95 return nil 96 } 97 98 // portFilter filters out the given port in the following cases: 99 // - if the port parameter does not match the actual port address. 100 // - if the the detected port does not contain any board. 101 // It returns: 102 // true -> to skip the port. 103 // false -> to keep the port. 104 func portFilter(port *rpc.DetectedPort, params *CreateParams) bool { 105 if len(port.MatchingBoards) == 0 { 106 return true 107 } 108 if params.Port != nil && *params.Port != port.Port.Address { 109 return true 110 } 111 return false 112 } 113 114 // boardFilter looks for a board which has the same fqbn passed as parameter. 115 // If fqbn parameter is nil, then the first board found is returned. 116 // It returns: 117 // - a board if it is found. 118 // - nil if no board matching the fqbn parameter is found. 119 func boardFilter(boards []*rpc.BoardListItem, params *CreateParams) (board *rpc.BoardListItem) { 120 if params.FQBN == nil { 121 return boards[0] 122 } 123 for _, b := range boards { 124 if b.Fqbn == *params.FQBN { 125 return b 126 } 127 } 128 return 129 }