~craftyguy/jankboot

f661048086a9303ba9e2b9b324173b390ae8d9db — Clayton Craft 2 years ago 4c585e2
pkg/getVar: add method for fetching variables from client device
1 files changed, 27 insertions(+), 0 deletions(-)

M pkg/jankboot/jankboot.go
M pkg/jankboot/jankboot.go => pkg/jankboot/jankboot.go +27 -0
@@ 316,6 316,33 @@ func (d *FastbootDevice) openIOEndpoints(intf *gousb.Interface) (epIn *gousb.InE
	return
}

// GetVar retrieves a fastboot variable, or err if the variable doesn't exist
func (d *FastbootDevice) GetVar(variable string) (value string, err error) {
	ctx := gousb.NewContext()
	defer ctx.Close()

	resp, err := d.getVar(ctx, variable)

	value = resp.Response
	return
}

// getVar retrieves a fastboot variable, or err if the variable doesn't exist.
// This is the non-exported version that receives an open gousb.Context
func (d *FastbootDevice) getVar(ctx *gousb.Context, variable string) (resp fastbootResponse, err error) {
	resp, err = d.sendCommand(ctx, fmt.Sprintf("getvar:%s", variable))
	if err != nil {
		err = fmt.Errorf("jankboot.getVar(): %w", err)
		return
	}

	if !checkResponse(resp, "OKAY", "") {
		err = fmt.Errorf("jankboot.getVar(): invalid response recieved when requesting variable %q: %s", variable, resp)
	}

	return
}

// getResponse parses the response from the client into a fastbootResponse type
func getResponse(ctx *gousb.Context, epIn *gousb.InEndpoint) (resp fastbootResponse, err error) {