File: cmd_raw.go

package info (click to toggle)
golang-github-bougou-go-ipmi 0.7.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,880 kB
  • sloc: makefile: 38
file content (64 lines) | stat: -rw-r--r-- 1,326 bytes parent folder | download
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package ipmi

import (
	"context"
	"fmt"
	"strings"
)

type CommandRawRequest struct {
	NetFn NetFn
	Cmd   uint8
	Data  []byte
	Name  string
}

type CommandRawResponse struct {
	Response []byte
}

func (req *CommandRawRequest) Command() Command {
	return Command{ID: req.Cmd, NetFn: req.NetFn, Name: req.Name}
}

func (req *CommandRawRequest) Pack() []byte {
	out := make([]byte, len(req.Data))

	packBytes(req.Data, out, 0)

	return out
}

func (res *CommandRawResponse) Unpack(msg []byte) error {
	res.Response = msg
	return nil
}

func (res *CommandRawResponse) CompletionCodes() map[uint8]string {
	return map[uint8]string{}
}

func (res *CommandRawResponse) Format() string {
	// convert the byte array to a slice of hex strings
	hexStrings := make([]string, len(res.Response))
	for i, b := range res.Response {
		hexStrings[i] = fmt.Sprintf("0x%02X", b)
	}

	// join the hex strings with commas
	hexString := strings.Join(hexStrings, ", ")

	return fmt.Sprintf("raw.Response = %s", hexString)
}

func (c *Client) RawCommand(ctx context.Context, netFn NetFn, cmd uint8, data []byte, name string) (response *CommandRawResponse, err error) {
	request := &CommandRawRequest{
		NetFn: netFn,
		Cmd:   cmd,
		Data:  data,
		Name:  name,
	}
	response = &CommandRawResponse{}
	err = c.Exchange(ctx, request, response)
	return
}