Skip to content

borgeuz/goswu

Repository files navigation

goswu

Go Reference

A Go client library for SWUpdate, the software update framework for embedded Linux.

It speaks the SWUpdate IPC protocol over Unix domain sockets, letting you trigger updates and monitor progress from Go code running on the same device.

Install

go get github.com/borgeuz/goswu

Usage

Local update from a file path

sock := goswu.NewSocket(goswu.WithImagePath("/tmp/update.swu"))
client := goswu.NewClient(sock, goswu.ParseSelection("stable,main"))

if err := client.Install(); err != nil {
    log.Fatal(err)
}

Local update from an io.Reader

f, err := os.Open("/tmp/update.swu")
if err != nil {
    log.Fatal(err)
}
defer f.Close()

sock := goswu.NewSocket(goswu.WithImageReader(f))
client := goswu.NewClient(sock, nil)

if err := client.Install(); err != nil {
    log.Fatal(err)
}

Dry-run mode

client.SetDryRun(true)
err := client.Install() // simulates the update, no changes written

Reading progress

progress, err := client.Progress()
if err != nil {
    log.Fatal(err)
}
fmt.Printf("status=%d step=%d/%d percent=%d%%\n",
    progress.Status, progress.CurStep, progress.NSteps, progress.CurPercent)

Streaming progress

sock := goswu.NewSocket()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

ch, err := client.StreamProgress(ctx)
if err != nil {
    log.Fatal(err)
}
for p := range ch {
    fmt.Printf("status=%d step=%d/%d percent=%d%%\n",
        p.Status, p.CurStep, p.NSteps, p.CurPercent)
}

The channel closes when the update reaches StatusSuccess/StatusFailure, the connection drops, or the context is cancelled.

Custom socket paths

sock := goswu.NewSocket(
    goswu.WithControlPath("/run/swupdate/ctrl"),
    goswu.WithProgressPath("/run/swupdate/progress"),
    goswu.WithImagePath("/tmp/update.swu"),
)

How it works

SWUpdate exposes two Unix sockets:

  • Control socket (/tmp/sockinstctrl) -- send install requests, receive ACK/NACK
  • Progress socket (/tmp/swupdateprog) -- read progress_msg structs with status, percentage, current step, etc.

The client serializes requests and messages matching the C structs from SWUpdate's network_ipc.h, so it can talk directly to the daemon without CGo.

Project structure

types.go      -- public enums (Status, RunType, SourceType)
errors.go     -- sentinel errors
ipc.go        -- internal IPC protocol (magic, msgType, ipcMsg)
progress.go   -- ProgressMsg and its deserialization
request.go    -- Request struct, binary serialization, Selection
transport.go  -- Transport interface
socket.go     -- Unix socket Transport implementation
client.go     -- high-level Client API

License

MIT

About

Go client library for SWUpdate. Trigger firmware updates and monitor progress directly from Go, no CGo required.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages