@@ -48,6 +48,7 @@ type IndexBoard struct {
48
48
UploadTouch bool `json:"upload.use_1200bps_touch"`
49
49
UploadWait bool `json:"upload.wait_for_upload_port"`
50
50
UploaderCommand * IndexUploaderCommand `json:"uploader.command,required"`
51
+ Latest * IndexFirmware `json:"-"`
51
52
}
52
53
53
54
type IndexUploaderCommand struct {
@@ -58,11 +59,11 @@ type IndexUploaderCommand struct {
58
59
59
60
// IndexFirmware represents a single Firmware version from module_firmware_index.json file.
60
61
type IndexFirmware struct {
61
- Version string `json:"version,required"` // `*semver.Version` but with SARA version is giving problems
62
- URL string `json:"url,required"`
63
- Checksum string `json:"checksum,required"`
64
- Size json.Number `json:"size,required"`
65
- Module string `json:"module,required"`
62
+ Version * semver. RelaxedVersion `json:"version,required"`
63
+ URL string `json:"url,required"`
64
+ Checksum string `json:"checksum,required"`
65
+ Size json.Number `json:"size,required"`
66
+ Module string `json:"module,required"`
66
67
}
67
68
68
69
// IndexLoaderSketch represents the sketch used to upload the new firmware on a board.
@@ -74,12 +75,7 @@ type IndexLoaderSketch struct {
74
75
75
76
// LoadIndex reads a module_firmware_index.json from a file and returns the corresponding Index structure.
76
77
func LoadIndex (jsonIndexFile * paths.Path ) (* Index , error ) {
77
- buff , err := jsonIndexFile .ReadFile ()
78
- if err != nil {
79
- return nil , err
80
- }
81
- var index Index
82
- err = json .Unmarshal (buff , & index .Boards )
78
+ index , err := LoadIndexNoSign (jsonIndexFile )
83
79
if err != nil {
84
80
return nil , err
85
81
}
@@ -93,20 +89,21 @@ func LoadIndex(jsonIndexFile *paths.Path) (*Index, error) {
93
89
if err != nil {
94
90
return nil , err
95
91
}
92
+
96
93
trusted , _ , err := security .VerifySignature (jsonIndexFile , jsonSignatureFile , key )
97
94
if err != nil {
98
95
logrus .
99
96
WithField ("index" , jsonIndexFile ).
100
97
WithField ("signatureFile" , jsonSignatureFile ).
101
98
WithError (err ).Infof ("Checking signature" )
102
- } else {
103
- logrus .
104
- WithField ("index" , jsonIndexFile ).
105
- WithField ("signatureFile" , jsonSignatureFile ).
106
- WithField ("trusted" , trusted ).Infof ("Checking signature" )
107
- index .IsTrusted = trusted
99
+ return nil , err
108
100
}
109
- return & index , nil
101
+ logrus .
102
+ WithField ("index" , jsonIndexFile ).
103
+ WithField ("signatureFile" , jsonSignatureFile ).
104
+ WithField ("trusted" , trusted ).Infof ("Checking signature" )
105
+ index .IsTrusted = trusted
106
+ return index , nil
110
107
}
111
108
112
109
// LoadIndexNoSign reads a module_firmware_index.json from a file and returns the corresponding Index structure.
@@ -123,6 +120,19 @@ func LoadIndexNoSign(jsonIndexFile *paths.Path) (*Index, error) {
123
120
124
121
index .IsTrusted = true
125
122
123
+ // Determine latest firmware for each board
124
+ for _ , board := range index .Boards {
125
+ if board .Module == "SARA" {
126
+ // TODO implement?? by defualt you have to specify the version
127
+ continue
128
+ }
129
+ for _ , firmware := range board .Firmwares {
130
+ if board .Latest == nil || firmware .Version .GreaterThan (board .Latest .Version ) {
131
+ board .Latest = firmware
132
+ }
133
+ }
134
+ }
135
+
126
136
return & index , nil
127
137
}
128
138
@@ -133,40 +143,28 @@ func (i *Index) GetLatestFirmwareURL(fqbn string) (string, error) {
133
143
if board == nil {
134
144
return "" , fmt .Errorf ("invalid FQBN: %s" , fqbn )
135
145
}
136
- if board .Module == "SARA" { // TODO togliere sara, lo assumo giá nel comando
137
- // TODO implement?? by defualt you have to specify the version
138
- return "" , fmt .Errorf ("not implemented for SARA module" )
139
- }
140
146
141
- var latestVersion * semver.RelaxedVersion
142
- var latestFirmwareURL string
143
- for _ , firmware := range board .Firmwares {
144
- version := semver .ParseRelaxed (firmware .Version )
145
- if latestVersion == nil || version .GreaterThan (latestVersion ) { // TODO check the condition
146
- latestVersion = version
147
- latestFirmwareURL = firmware .URL
148
- }
149
- }
150
- if latestVersion != nil {
151
- return latestFirmwareURL , nil
152
- } else {
147
+ if board .Latest == nil {
153
148
return "" , fmt .Errorf ("cannot find latest version" )
154
149
}
150
+
151
+ return board .Latest .URL , nil
155
152
}
156
153
157
154
// GetFirmwareURL will take the fqbn of the required board and the version of the firmware as parameters.
158
155
// It will return the URL of the required firmware
159
- func (i * Index ) GetFirmwareURL (fqbn , version string ) (string , error ) {
156
+ func (i * Index ) GetFirmwareURL (fqbn , v string ) (string , error ) {
160
157
board := i .GetBoard (fqbn )
161
158
if board == nil {
162
159
return "" , fmt .Errorf ("invalid FQBN: %s" , fqbn )
163
160
}
161
+ version := semver .ParseRelaxed (v )
164
162
for _ , firmware := range board .Firmwares {
165
- if firmware .Version == version {
163
+ if firmware .Version . Equal ( version ) {
166
164
return firmware .URL , nil
167
165
}
168
166
}
169
- return "" , fmt .Errorf ("invalid version: %s" , version )
167
+ return "" , fmt .Errorf ("version not found : %s" , version )
170
168
}
171
169
172
170
// GetLoaderSketchURL will take the board's fqbn and return the url of the loader sketch
0 commit comments