|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "time" |
| 8 | + |
| 9 | + helper "github.com/arduino/fwuploader-plugin-helper" |
| 10 | + "github.com/arduino/go-paths-helper" |
| 11 | + "github.com/sstallion/go-hid" |
| 12 | + semver "go.bug.st/relaxed-semver" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + pluginName = "uno-r4-wifi-fwuploader" |
| 17 | + pluginVersion = "1.0.0" |
| 18 | +) |
| 19 | + |
| 20 | +func main() { |
| 21 | + helper.RunPlugin(&unoR4WifiPlugin{}) |
| 22 | +} |
| 23 | + |
| 24 | +type unoR4WifiPlugin struct{} |
| 25 | + |
| 26 | +var _ helper.Plugin = (*unoR4WifiPlugin)(nil) |
| 27 | + |
| 28 | +// GetPluginInfo returns information about the plugin |
| 29 | +func (p *unoR4WifiPlugin) GetPluginInfo() *helper.PluginInfo { |
| 30 | + return &helper.PluginInfo{ |
| 31 | + Name: pluginName, |
| 32 | + Version: semver.MustParse(pluginVersion), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// UploadFirmware performs a firmware upload on the board |
| 37 | +func (p *unoR4WifiPlugin) UploadFirmware(portAddress string, firmwarePath *paths.Path, feedback *helper.PluginFeedback) error { |
| 38 | + if portAddress == "" { |
| 39 | + fmt.Fprintln(feedback.Err(), "Port address not specified") |
| 40 | + return fmt.Errorf("invalid port address") |
| 41 | + } |
| 42 | + if firmwarePath == nil || firmwarePath.IsDir() { |
| 43 | + fmt.Fprintln(feedback.Err(), "Invalid firmware path") |
| 44 | + return fmt.Errorf("invalid firmware path") |
| 45 | + } |
| 46 | + |
| 47 | + d, err := openFirstHID() |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + if err := reboot(d); err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + if err := hid.Exit(); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + // Wait a bit before flashing the firmware to allow the board to become available again. |
| 61 | + time.Sleep(3 * time.Second) |
| 62 | + |
| 63 | + cmd := exec.Command("espflash", "flash", firmwarePath.String(), "-p", portAddress) |
| 64 | + cmd.Stdout = feedback.Out() |
| 65 | + cmd.Stderr = feedback.Err() |
| 66 | + cmd.Env = append(cmd.Env, os.Environ()...) |
| 67 | + if err := cmd.Run(); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + fmt.Fprintf(feedback.Out(), "\nUpload completed! You can now detach the board.\n") |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// UploadCertificate performs a certificate upload on the board. The certificate must be in crt format |
| 76 | +// and be multiple of 4, otherwise `espflash` won't work! |
| 77 | +func (p *unoR4WifiPlugin) UploadCertificate(portAddress string, certificatePath *paths.Path, feedback *helper.PluginFeedback) error { |
| 78 | + if portAddress == "" { |
| 79 | + fmt.Fprintln(feedback.Err(), "Port address not specified") |
| 80 | + return fmt.Errorf("invalid port address") |
| 81 | + } |
| 82 | + if certificatePath == nil || certificatePath.IsDir() { |
| 83 | + fmt.Fprintln(feedback.Err(), "Invalid certificate path") |
| 84 | + return fmt.Errorf("invalid certificate path") |
| 85 | + } |
| 86 | + fmt.Fprintf(feedback.Out(), "Uploading certificates to %s...\n", portAddress) |
| 87 | + |
| 88 | + d, err := openFirstHID() |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + if err := reboot(d); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + if err := hid.Exit(); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + // Wait a bit before flashing the certificate to allow the board to become available again. |
| 102 | + time.Sleep(3 * time.Second) |
| 103 | + |
| 104 | + cmd := exec.Command("espflash", "write-bin", "-p", portAddress, "-b", "921600", "0x3C0000", certificatePath.String()) |
| 105 | + cmd.Stdout = feedback.Out() |
| 106 | + cmd.Stderr = feedback.Err() |
| 107 | + cmd.Env = append(cmd.Env, os.Environ()...) |
| 108 | + if err := cmd.Run(); err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + fmt.Fprintf(feedback.Out(), "\nUpload completed! You can now detach the board.\n") |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +// GetFirmwareVersion retrieve the firmware version installed on the board |
| 117 | +func (p *unoR4WifiPlugin) GetFirmwareVersion(portAddress string, feedback *helper.PluginFeedback) (*semver.RelaxedVersion, error) { |
| 118 | + d, err := openFirstHID() |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + defer hid.Exit() |
| 123 | + |
| 124 | + buf := make([]byte, 65) |
| 125 | + if _, err := d.GetFeatureReport(buf); err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + return semver.ParseRelaxed(fmt.Sprintf("%d.%d.%d", buf[1], buf[2], buf[3])), nil |
| 129 | +} |
0 commit comments