Skip to content

commands: use the new endpoint to detect boards #2974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions commands/service_board_identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"net/http"
"regexp"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -130,14 +131,14 @@ func identifyViaCloudAPI(ctx context.Context, props *properties.Map, settings *c
}

var (
vidPidURL = "https://builder.arduino.cc/v3/boards/byVidPid"
vidPidURL = "https://api2.arduino.cc/boards/v1/boards"
validVidPid = regexp.MustCompile(`0[xX][a-fA-F\d]{4}`)
)

func cachedAPIByVidPid(ctx context.Context, vid, pid string, settings *configuration.Settings) ([]*rpc.BoardListItem, error) {
var resp []*rpc.BoardListItem

cacheKey := fmt.Sprintf("cache.builder-api.v3/boards/byvid/pid/%s/%s", vid, pid)
cacheKey := fmt.Sprintf("cache.api2.arduino.cc/boards/v1/boards?vid-pid=%s-%s", vid, pid)
if cachedResp := inventory.Store.GetString(cacheKey + ".data"); cachedResp != "" {
ts := inventory.Store.GetTime(cacheKey + ".ts")
if time.Since(ts) < time.Hour*24 {
Expand Down Expand Up @@ -169,7 +170,7 @@ func apiByVidPid(ctx context.Context, vid, pid string, settings *configuration.S
return nil, errors.New(i18n.Tr("Invalid pid value: '%s'", pid))
}

url := fmt.Sprintf("%s/%s/%s", vidPidURL, vid, pid)
url := fmt.Sprintf("%s?vid-pid=%s-%s", vidPidURL, vid, pid)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Content-Type", "application/json")

Expand Down Expand Up @@ -198,20 +199,32 @@ func apiByVidPid(ctx context.Context, vid, pid string, settings *configuration.S
return nil, err
}

var dat map[string]interface{}
type boardsResponse struct {
Items []struct {
Name string `json:"name"`
FQBN string `json:"fqbn"`
} `json:"items"`
}
var dat boardsResponse
if err := json.Unmarshal(resp, &dat); err != nil {
return nil, fmt.Errorf("%s: %w", i18n.Tr("error processing response from server"), err)
}
name, nameFound := dat["name"].(string)
fqbn, fbqnFound := dat["fqbn"].(string)
if !nameFound || !fbqnFound {
return nil, errors.New(i18n.Tr("wrong format in server response"))

response := make([]*rpc.BoardListItem, len(dat.Items))
for i, v := range dat.Items {
if v.Name == "" || v.FQBN == "" {
return nil, errors.New(i18n.Tr("wrong format in server response"))
}
response[i] = &rpc.BoardListItem{Name: v.Name, Fqbn: v.FQBN}
}

return []*rpc.BoardListItem{
{
Name: name,
Fqbn: fqbn,
},
}, nil
// In case multiple platform matches the same vid-pid we put on top
// the arduino ones
slices.SortFunc(response, func(a, b *rpc.BoardListItem) int {
if strings.HasPrefix(a.Fqbn, "arduino") {
return -1
}
return 0
})
return response, nil
}
59 changes: 48 additions & 11 deletions commands/service_board_identify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,69 @@ func TestGetByVidPid(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `
{
"architecture": "samd",
"fqbn": "arduino:samd:mkr1000",
"href": "/v3/boards/arduino:samd:mkr1000",
"id": "mkr1000",
"name": "Arduino/Genuino MKR1000",
"package": "arduino",
"plan": "create-free"
"items": [
{
"fqbn": "arduino:avr:uno",
"vendor": "arduino",
"architecture": "avr",
"board_id": "uno",
"name": "Arduino Uno"
}
]
}
`)
}))
defer ts.Close()

vidPidURL = ts.URL
settings := configuration.NewSettings()
res, err := apiByVidPid(context.Background(), "0xf420", "0XF069", settings)
res, err := apiByVidPid(context.Background(), "0x2341", "0x0043", settings)
require.Nil(t, err)
require.Len(t, res, 1)
require.Equal(t, "Arduino/Genuino MKR1000", res[0].GetName())
require.Equal(t, "arduino:samd:mkr1000", res[0].GetFqbn())
require.Equal(t, "Arduino Uno", res[0].GetName())
require.Equal(t, "arduino:avr:uno", res[0].GetFqbn())

// wrong vid (too long), wrong pid (not an hex value)

_, err = apiByVidPid(context.Background(), "0xfffff", "0xDEFG", settings)
require.NotNil(t, err)
}

func TestGetByVidPidPutArduinoOnTop(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `
{
"items": [
{
"fqbn": "esp32:esp32:nano_nora",
"vendor": "esp32",
"architecture": "esp32",
"board_id": "nano_nora",
"name": "Arduino Nano ESP32"
},
{
"fqbn": "arduino:esp32:nano_nora",
"vendor": "arduino",
"architecture": "esp32",
"board_id": "nano_nora",
"name": "Arduino Nano ESP32"
}
]
}
`)
}))
defer ts.Close()

vidPidURL = ts.URL
settings := configuration.NewSettings()
res, err := apiByVidPid(context.Background(), "0x2341", "0x0070", settings)
require.Nil(t, err)
require.Len(t, res, 2)
require.Equal(t, "Arduino Nano ESP32", res[0].GetName())
require.Equal(t, "arduino:esp32:nano_nora", res[0].GetFqbn())
require.Equal(t, "esp32:esp32:nano_nora", res[1].GetFqbn())
}

func TestGetByVidPidNotFound(t *testing.T) {
settings := configuration.NewSettings()

Expand Down Expand Up @@ -95,7 +132,7 @@ func TestGetByVidPidMalformedResponse(t *testing.T) {
settings := configuration.NewSettings()

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{}")
fmt.Fprintln(w, `{"items":[{}]}`)
}))
defer ts.Close()

Expand Down
Loading