|
| 1 | +package main |
| 2 | + |
| 3 | +// roleswap.go — flipctl role-swap <port> [--to host|device | --flip] |
| 4 | +// |
| 5 | +// Sends a "role-swap" Request to the flipd daemon over a Unix |
| 6 | +// Domain Socket and prints the prev → next transition on success. |
| 7 | +// The CLI never writes to /sys/class/typec directly — all sysfs |
| 8 | +// mutations go through flipd so the audit trail (daemon log) and |
| 9 | +// authorisation (SO_PEERCRED against the flipctl group) live in one |
| 10 | +// place. |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "encoding/json" |
| 15 | + "errors" |
| 16 | + "fmt" |
| 17 | + |
| 18 | + "github.com/spf13/cobra" |
| 19 | + |
| 20 | + "flipd/pkg/ipc" |
| 21 | +) |
| 22 | + |
| 23 | +var roleSwapCmd = &cobra.Command{ |
| 24 | + Use: "role-swap <port>", |
| 25 | + Short: "Swap (or set) the data role of a Type-C port via the flipd daemon", |
| 26 | + Long: `Send a role-swap request to the flipd daemon. |
| 27 | +
|
| 28 | +Examples: |
| 29 | + flipctl role-swap port0 --flip # host → device, or device → host |
| 30 | + flipctl role-swap port0 --to host # force into host mode |
| 31 | + flipctl role-swap port0 --to device # force into device mode |
| 32 | +
|
| 33 | +Exactly one of --flip and --to is required. The daemon validates |
| 34 | +the request against the kernel's current state and returns the |
| 35 | +previous and new role.`, |
| 36 | + Args: cobra.ExactArgs(1), |
| 37 | + RunE: runRoleSwap, |
| 38 | +} |
| 39 | + |
| 40 | +var roleSwapFlags struct { |
| 41 | + To string |
| 42 | + Flip bool |
| 43 | +} |
| 44 | + |
| 45 | +func init() { |
| 46 | + roleSwapCmd.Flags().StringVar(&roleSwapFlags.To, "to", "", |
| 47 | + "set data_role to this value (host or device)") |
| 48 | + roleSwapCmd.Flags().BoolVar(&roleSwapFlags.Flip, "flip", false, |
| 49 | + "swap to the opposite of the current role") |
| 50 | + roleSwapCmd.MarkFlagsOneRequired("to", "flip") |
| 51 | + roleSwapCmd.MarkFlagsMutuallyExclusive("to", "flip") |
| 52 | +} |
| 53 | + |
| 54 | +func runRoleSwap(cmd *cobra.Command, args []string) error { |
| 55 | + port := args[0] |
| 56 | + if roleSwapFlags.To != "" && roleSwapFlags.To != "host" && roleSwapFlags.To != "device" { |
| 57 | + return fmt.Errorf("--to must be 'host' or 'device' (got %q)", roleSwapFlags.To) |
| 58 | + } |
| 59 | + |
| 60 | + ctx, cancel := signalContext(cmd.Context()) |
| 61 | + defer cancel() |
| 62 | + |
| 63 | + socketPath, _ := cmd.Flags().GetString("socket") |
| 64 | + timeout, _ := cmd.Flags().GetDuration("timeout") |
| 65 | + |
| 66 | + dctx, dcancel := context.WithTimeout(ctx, timeout) |
| 67 | + defer dcancel() |
| 68 | + |
| 69 | + client, err := ipc.Dial(dctx, socketPath) |
| 70 | + if err != nil { |
| 71 | + // Surface "flipd is not running" before any other error so |
| 72 | + // the user understands their environment, not the daemon. |
| 73 | + if errors.Is(err, ipc.ErrNotRunning) { |
| 74 | + return fmt.Errorf("flipd daemon not reachable at %s "+ |
| 75 | + "(is the systemd unit enabled?): %w", socketPath, err) |
| 76 | + } |
| 77 | + return fmt.Errorf("connect %s: %w", socketPath, err) |
| 78 | + } |
| 79 | + defer client.Close() |
| 80 | + |
| 81 | + params := map[string]any{"port": port} |
| 82 | + if roleSwapFlags.Flip { |
| 83 | + params["flip"] = true |
| 84 | + } else { |
| 85 | + params["to"] = roleSwapFlags.To |
| 86 | + } |
| 87 | + |
| 88 | + req := &ipc.Request{ |
| 89 | + Method: "role-swap", |
| 90 | + Params: marshalOrDie(params), |
| 91 | + } |
| 92 | + resp, callErr := client.Call(dctx, req) |
| 93 | + if callErr != nil { |
| 94 | + return friendlyRoleSwapErr(callErr) |
| 95 | + } |
| 96 | + |
| 97 | + var result struct { |
| 98 | + Port string `json:"port"` |
| 99 | + Prev string `json:"prev"` |
| 100 | + Next string `json:"next"` |
| 101 | + Method string `json:"method"` |
| 102 | + } |
| 103 | + if err := json.Unmarshal(resp.Result, &result); err != nil { |
| 104 | + return fmt.Errorf("decode role-swap result: %w (raw: %s)", |
| 105 | + err, string(resp.Result)) |
| 106 | + } |
| 107 | + |
| 108 | + fmt.Fprintf(cmd.OutOrStdout(), |
| 109 | + "port %s: data %s → %s (method=%s)\n", |
| 110 | + result.Port, result.Prev, result.Next, result.Method) |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +// friendlyRoleSwapErr maps typed sentinels to operator-facing |
| 115 | +// guidance. Untyped errors bubble through unchanged so genuine bugs |
| 116 | +// aren't masked. |
| 117 | +func friendlyRoleSwapErr(err error) error { |
| 118 | + switch { |
| 119 | + case errors.Is(err, ipc.ErrPortNotFound): |
| 120 | + return fmt.Errorf("port not found by flipd: %w", err) |
| 121 | + case errors.Is(err, ipc.ErrUnauthorized): |
| 122 | + return fmt.Errorf("flipd rejected the call "+ |
| 123 | + "(are you in the 'flipctl' group?): %w", err) |
| 124 | + case errors.Is(err, ipc.ErrInvalidParams): |
| 125 | + return fmt.Errorf("invalid arguments sent to flipd: %w", err) |
| 126 | + case errors.Is(err, ipc.ErrUnsupportedRole): |
| 127 | + return fmt.Errorf("kernel returned an unrecognised role: %w", err) |
| 128 | + case errors.Is(err, ipc.ErrInternal): |
| 129 | + return fmt.Errorf("flipd internal error: %w", err) |
| 130 | + } |
| 131 | + return fmt.Errorf("role-swap: %w", err) |
| 132 | +} |
| 133 | + |
| 134 | +// marshalOrDie marshals v and panics on failure. For our use |
| 135 | +// (plain map[string]any with primitive values) Marshal never |
| 136 | +// actually fails; panicking here is loud, fast, and unmistakable. |
| 137 | +func marshalOrDie(v any) json.RawMessage { |
| 138 | + b, err := json.Marshal(v) |
| 139 | + if err != nil { |
| 140 | + panic("flipctl: marshal request params: " + err.Error()) |
| 141 | + } |
| 142 | + return b |
| 143 | +} |
0 commit comments