zaphyra's git: airpodsctl

Control and monitor your AirPods

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
package main

import (
	"encoding/hex"
	"fmt"
	"os"

	"git.zaphyra.eu/airpodsctl/types"
)

func (state State) toggleFeatureCommand(feature string, value types.Bool) {
	if feature == "" {
		fmt.Fprint(os.Stderr, "[toggleFeatureCommand] Error: No feature supplied!\n\nUse `--help` for a list of available features.\n", feature)
		os.Exit(1)
	}

	if _, ok := AACPControlCommand[feature]; !ok {
		fmt.Fprintf(os.Stderr, "[toggleFeatureCommand] Error: Unknown feature: %s!\nUse `--help` for a list of available features.\n", feature)
		os.Exit(1)
	}

	device, err := state.Devices.Get(state.SelectedDevice)
	if err != nil {
		fmt.Fprintf(os.Stderr, "[toggleFeatureCommand] Error: Device '%s' is not connected!\n", state.SelectedDevice)
		os.Exit(1)
	}

	cmd := AACPControlCommandPacket
	cmd[6] = AACPControlCommand[feature]
	cmd[7] = AACPControlCommandBool[bool(value)]

	if err := state.SendRawCommand(device, hex.EncodeToString(cmd)); err != nil {
		fmt.Fprintln(os.Stderr, "[toggleFeatureCommand] Error:", err)
		os.Exit(1)
	}

	os.Exit(0)
}