|
| 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 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "io/ioutil" |
| 24 | + "net/http" |
| 25 | + "time" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + arduinoPackage = "arduino" |
| 30 | + esp32Package = "esp32" |
| 31 | + esp8266Package = "esp8266" |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + // this is temporary... it will be removed when |
| 36 | + // https://github.com/arduino/arduino-cloud-cli/pull/74/files#diff-d891696d5c17ea0eecc6b1c23802cbaf553379e701c5e0e1ff23ee0d26d2877cR27-R39 |
| 37 | + // will be merged |
| 38 | + compatibleArduinoFQBN = []string{ |
| 39 | + "arduino:samd:nano_33_iot", |
| 40 | + "arduino:samd:mkrwifi1010", |
| 41 | + "arduino:mbed_nano:nanorp2040connect", |
| 42 | + "arduino:mbed_portenta:envie_m7", |
| 43 | + "arduino:samd:mkr1000", |
| 44 | + "arduino:samd:mkrgsm1400", |
| 45 | + "arduino:samd:mkrnb1500", |
| 46 | + "arduino:samd:mkrwan1310", |
| 47 | + "arduino:samd:mkrwan1300", |
| 48 | + } |
| 49 | +) |
| 50 | + |
| 51 | +// FQBNInfo contains the details of a FQBN. |
| 52 | +type FQBNInfo struct { |
| 53 | + Value string `json:"fqbn"` |
| 54 | + Name string `json:"name"` |
| 55 | + Package string `json:"package"` |
| 56 | +} |
| 57 | + |
| 58 | +// ListFQBN command returns a list of the supported FQBN. |
| 59 | +func ListFQBN() ([]FQBNInfo, error) { |
| 60 | + h := &http.Client{Timeout: time.Second * 5} |
| 61 | + resp, err := h.Get("https://builder.arduino.cc/v3/boards/") |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("cannot retrieve boards from builder.arduino.cc: %w", err) |
| 64 | + } |
| 65 | + defer resp.Body.Close() |
| 66 | + |
| 67 | + body, err := ioutil.ReadAll(resp.Body) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("reading boards from builder.arduino.cc: cannot read response's body: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + var fqbnList struct { |
| 73 | + Items []FQBNInfo `json:"items"` |
| 74 | + } |
| 75 | + if err = json.Unmarshal(body, &fqbnList); err != nil { |
| 76 | + return nil, fmt.Errorf("cannot parse boards retrieved from builder.arduino.cc: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + return filterFQBN(fqbnList.Items), nil |
| 80 | +} |
| 81 | + |
| 82 | +// filterFQBN takes a list of fqbn and returns only the |
| 83 | +// ones supported by iot cloud. |
| 84 | +func filterFQBN(ls []FQBNInfo) []FQBNInfo { |
| 85 | + filtered := make([]FQBNInfo, 0, len(ls)) |
| 86 | + for _, fqbn := range ls { |
| 87 | + switch fqbn.Package { |
| 88 | + |
| 89 | + case esp32Package, esp8266Package: |
| 90 | + filtered = append(filtered, fqbn) |
| 91 | + |
| 92 | + case arduinoPackage: |
| 93 | + for _, b := range compatibleArduinoFQBN { |
| 94 | + if fqbn.Value == b { |
| 95 | + filtered = append(filtered, fqbn) |
| 96 | + break |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return filtered |
| 102 | +} |
0 commit comments