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