|
| 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 | +} |
0 commit comments