@@ -48,6 +48,7 @@ type IndexBoard struct {
4848 UploadTouch bool `json:"upload.use_1200bps_touch"`
4949 UploadWait bool `json:"upload.wait_for_upload_port"`
5050 UploaderCommand * IndexUploaderCommand `json:"uploader.command,required"`
51+ Latest * IndexFirmware `json:"-"`
5152}
5253
5354type IndexUploaderCommand struct {
@@ -58,11 +59,11 @@ type IndexUploaderCommand struct {
5859
5960// IndexFirmware represents a single Firmware version from module_firmware_index.json file.
6061type 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"`
6667}
6768
6869// IndexLoaderSketch represents the sketch used to upload the new firmware on a board.
@@ -74,12 +75,7 @@ type IndexLoaderSketch struct {
7475
7576// LoadIndex reads a module_firmware_index.json from a file and returns the corresponding Index structure.
7677func 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 )
8379 if err != nil {
8480 return nil , err
8581 }
@@ -93,20 +89,21 @@ func LoadIndex(jsonIndexFile *paths.Path) (*Index, error) {
9389 if err != nil {
9490 return nil , err
9591 }
92+
9693 trusted , _ , err := security .VerifySignature (jsonIndexFile , jsonSignatureFile , key )
9794 if err != nil {
9895 logrus .
9996 WithField ("index" , jsonIndexFile ).
10097 WithField ("signatureFile" , jsonSignatureFile ).
10198 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
108100 }
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
110107}
111108
112109// 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) {
123120
124121 index .IsTrusted = true
125122
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+
126136 return & index , nil
127137}
128138
@@ -133,40 +143,28 @@ func (i *Index) GetLatestFirmwareURL(fqbn string) (string, error) {
133143 if board == nil {
134144 return "" , fmt .Errorf ("invalid FQBN: %s" , fqbn )
135145 }
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- }
140146
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 {
153148 return "" , fmt .Errorf ("cannot find latest version" )
154149 }
150+
151+ return board .Latest .URL , nil
155152}
156153
157154// GetFirmwareURL will take the fqbn of the required board and the version of the firmware as parameters.
158155// 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 ) {
160157 board := i .GetBoard (fqbn )
161158 if board == nil {
162159 return "" , fmt .Errorf ("invalid FQBN: %s" , fqbn )
163160 }
161+ version := semver .ParseRelaxed (v )
164162 for _ , firmware := range board .Firmwares {
165- if firmware .Version == version {
163+ if firmware .Version . Equal ( version ) {
166164 return firmware .URL , nil
167165 }
168166 }
169- return "" , fmt .Errorf ("invalid version: %s" , version )
167+ return "" , fmt .Errorf ("version not found : %s" , version )
170168}
171169
172170// GetLoaderSketchURL will take the board's fqbn and return the url of the loader sketch
0 commit comments