github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/command/device/listfqbn.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  	"context"
    22  	"encoding/json"
    23  	"fmt"
    24  	"io/ioutil"
    25  	"net/http"
    26  	"time"
    27  )
    28  
    29  const (
    30  	arduinoPackage = "arduino"
    31  	esp32Package   = "esp32"
    32  	esp8266Package = "esp8266"
    33  )
    34  
    35  // FQBNInfo contains the details of a FQBN.
    36  type FQBNInfo struct {
    37  	Value   string `json:"fqbn"`
    38  	Name    string `json:"name"`
    39  	Package string `json:"package"`
    40  }
    41  
    42  // ListFQBN command returns a list of the supported FQBN.
    43  func ListFQBN(ctx context.Context) ([]FQBNInfo, error) {
    44  	url := "https://builder.arduino.cc/v3/boards/"
    45  	req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
    46  	if err != nil {
    47  		return nil, fmt.Errorf("cannot retrieve boards: %w", err)
    48  	}
    49  
    50  	h := &http.Client{Timeout: time.Second * 5}
    51  	resp, err := h.Do(req)
    52  	if err != nil {
    53  		return nil, fmt.Errorf("cannot retrieve boards: %w", err)
    54  	}
    55  	defer resp.Body.Close()
    56  
    57  	body, err := ioutil.ReadAll(resp.Body)
    58  	if err != nil {
    59  		return nil, fmt.Errorf("reading boards from builder.arduino.cc: cannot read response's body: %w", err)
    60  	}
    61  
    62  	var fqbnList struct {
    63  		Items []FQBNInfo `json:"items"`
    64  	}
    65  	if err = json.Unmarshal(body, &fqbnList); err != nil {
    66  		return nil, fmt.Errorf("cannot parse boards retrieved from builder.arduino.cc: %w", err)
    67  	}
    68  
    69  	return filterFQBN(fqbnList.Items), nil
    70  }
    71  
    72  // filterFQBN takes a list of fqbn and returns only the
    73  // ones supported by iot cloud.
    74  func filterFQBN(ls []FQBNInfo) []FQBNInfo {
    75  	filtered := make([]FQBNInfo, 0, len(ls))
    76  	for _, fqbn := range ls {
    77  		switch fqbn.Package {
    78  
    79  		case esp32Package, esp8266Package:
    80  			filtered = append(filtered, fqbn)
    81  
    82  		case arduinoPackage:
    83  			compatible := append(cryptoFQBN, loraFQBN...)
    84  			for _, b := range compatible {
    85  				if fqbn.Value == b {
    86  					filtered = append(filtered, fqbn)
    87  					break
    88  				}
    89  			}
    90  		}
    91  	}
    92  	return filtered
    93  }