Skip to content

Commit f481f24

Browse files
committed
Add WithXXX methods to CommandContext
1 parent 0fa8ad7 commit f481f24

File tree

3 files changed

+66
-31
lines changed

3 files changed

+66
-31
lines changed

client.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -279,30 +279,3 @@ func (c *Client) lock() {
279279
func (c *Client) unlock() {
280280
c.l.Unlock()
281281
}
282-
283-
// CommandContext represents the specific context used for Exchange of the specific request.
284-
//
285-
// Some sensor-related commands (e.g. GetSensorReading) need to use the responder and requester addresses and LUNs which are specific to the sensor.
286-
//
287-
// This context is used to store the responder and requester addresses and LUNs for such commands.
288-
type CommandContext struct {
289-
responderAddr *uint8
290-
responderLUN *uint8
291-
requesterAddr *uint8
292-
requesterLUN *uint8
293-
}
294-
295-
type commandContextKeyType string
296-
297-
const commandContextKey commandContextKeyType = "CommandContext"
298-
299-
func WithCommandContext(ctx context.Context, commandContext *CommandContext) context.Context {
300-
return context.WithValue(ctx, commandContextKey, commandContext)
301-
}
302-
303-
func GetCommandContext(ctx context.Context) *CommandContext {
304-
if ctx.Value(commandContextKey) == nil {
305-
return nil
306-
}
307-
return ctx.Value(commandContextKey).(*CommandContext)
308-
}

client_command_context.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package ipmi
2+
3+
import "context"
4+
5+
// CommandContext represents the specific context used for IPMI command exchanges.
6+
// It stores addressing information needed for certain IPMI commands, particularly
7+
// sensor-related operations.
8+
//
9+
// The context contains:
10+
// - responderAddr: The address of the responding device (e.g., BMC)
11+
// - responderLUN: The Logical Unit Number of the responding device
12+
// - requesterAddr: The address of the requesting device
13+
// - requesterLUN: The Logical Unit Number of the requesting device
14+
//
15+
// This context is essential for commands that require specific addressing information,
16+
// such as GetSensorReading and other sensor-related operations.
17+
type CommandContext struct {
18+
responderAddr *uint8
19+
responderLUN *uint8
20+
requesterAddr *uint8
21+
requesterLUN *uint8
22+
}
23+
24+
func (cmdCtx *CommandContext) WithResponderAddr(responderAddr uint8) *CommandContext {
25+
cmdCtx.responderAddr = &responderAddr
26+
return cmdCtx
27+
}
28+
29+
func (cmdCtx *CommandContext) WithResponderLUN(responderLUN uint8) *CommandContext {
30+
cmdCtx.responderLUN = &responderLUN
31+
return cmdCtx
32+
}
33+
34+
func (cmdCtx *CommandContext) WithRequesterAddr(requesterAddr uint8) *CommandContext {
35+
cmdCtx.requesterAddr = &requesterAddr
36+
return cmdCtx
37+
}
38+
39+
func (cmdCtx *CommandContext) WithRequesterLUN(requesterLUN uint8) *CommandContext {
40+
cmdCtx.requesterLUN = &requesterLUN
41+
return cmdCtx
42+
}
43+
44+
// commandContextKeyType is a custom type for the context key to avoid collisions
45+
type commandContextKeyType string
46+
47+
// commandContextKey is the key used to store CommandContext in the context.Context
48+
const commandContextKey commandContextKeyType = "CommandContext"
49+
50+
// WithCommandContext adds a CommandContext to the provided context.Context.
51+
func WithCommandContext(ctx context.Context, commandContext *CommandContext) context.Context {
52+
return context.WithValue(ctx, commandContextKey, commandContext)
53+
}
54+
55+
// GetCommandContext retrieves the CommandContext from the provided context.Context.
56+
func GetCommandContext(ctx context.Context) *CommandContext {
57+
if ctx.Value(commandContextKey) == nil {
58+
return nil
59+
}
60+
return ctx.Value(commandContextKey).(*CommandContext)
61+
}

cmd_get_sensors.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,11 @@ func (c *Client) sdrToSensor(ctx context.Context, sdr *SDR) (*Sensor, error) {
206206

207207
sensorOwner := uint8(sensor.GeneratorID.OwnerID())
208208
sensorLUN := uint8(sensor.GeneratorID.LUN())
209-
commandContext := &CommandContext{
210-
responderAddr: &sensorOwner,
211-
responderLUN: &sensorLUN,
212-
}
209+
commandContext := &CommandContext{}
210+
commandContext.
211+
WithResponderAddr(sensorOwner).
212+
WithResponderLUN(sensorLUN)
213+
213214
ctx = WithCommandContext(ctx, commandContext)
214215
c.Debug("Set CommandContext:", commandContext)
215216

0 commit comments

Comments
 (0)