Skip to content

Commit 0283517

Browse files
committed
Refactor GetSystemInfoParamsFor to use a new calculateSetsCount function for determining sets count based on block data length, improving code clarity and reducing redundancy. (fix #60)
1 parent ae0a53f commit 0283517

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

cmd_get_system_info_params.go

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,39 @@ func (c *Client) GetSystemInfoParamsFor(ctx context.Context, params *SystemInfoP
120120
0x80, // parameter not supported
121121
)
122122

123+
calculateSetsCount := func(blockData []byte) uint8 {
124+
// For the first block of string data (set selector = 0),
125+
// the first two bytes indicate the encoding of the string and its overall length as follows.
126+
// So, if the length is less than 2, it means there is no string data.
127+
if len(blockData) < 2 {
128+
return 0
129+
}
130+
131+
// Sets count is based on first two bytes + string length.
132+
stringLength := uint8(blockData[1])
133+
totalLength := 2 + stringLength
134+
135+
// 1 set per 16 bytes. Subtract 1 before dividing by 16, else multiples
136+
// of 16 would get an extra set.
137+
return (totalLength-1)/16 + 1
138+
}
139+
123140
if err := c.GetSystemInfoParamFor(ctx, params.SetInProgress); canIgnore(err) != nil {
124141
return err
125142
}
126143

127144
if params.SystemFirmwareVersions != nil {
128145
if len(params.SystemFirmwareVersions) == 0 {
129-
var setsCount uint8
130-
131146
p := &SystemInfoParam_SystemFirmwareVersion{
132147
SetSelector: 0,
133148
}
134149
if err := c.GetSystemInfoParamFor(ctx, p); canIgnore(err) != nil {
135150
return err
136151
}
137-
if len(p.BlockData) < 2 {
138-
// For the first block of string data (set selector = 0),
139-
// the first two bytes indicate the encoding of the string and its overall length as follows.
140-
// So, if the length is less than 2, it means there is no string data.
152+
setsCount := calculateSetsCount(p.BlockData)
153+
if setsCount == 0 {
141154
return nil
142155
}
143-
stringLength := uint8(p.BlockData[1])
144-
setsCount = stringLength/16 + 1
145156

146157
params.SystemFirmwareVersions = make([]*SystemInfoParam_SystemFirmwareVersion, setsCount)
147158
for i := uint8(0); i < setsCount; i++ {
@@ -161,19 +172,16 @@ func (c *Client) GetSystemInfoParamsFor(ctx context.Context, params *SystemInfoP
161172

162173
if params.SystemNames != nil {
163174
if len(params.SystemNames) == 0 {
164-
var setsCount uint8
165-
166175
p := &SystemInfoParam_SystemName{
167176
SetSelector: 0,
168177
}
169178
if err := c.GetSystemInfoParamFor(ctx, p); canIgnore(err) != nil {
170179
return err
171180
}
172-
if len(p.BlockData) < 2 {
181+
setsCount := calculateSetsCount(p.BlockData)
182+
if setsCount == 0 {
173183
return nil
174184
}
175-
stringLength := uint8(p.BlockData[1])
176-
setsCount = stringLength/16 + 1
177185

178186
params.SystemNames = make([]*SystemInfoParam_SystemName, setsCount)
179187
for i := uint8(0); i < setsCount; i++ {
@@ -193,19 +201,16 @@ func (c *Client) GetSystemInfoParamsFor(ctx context.Context, params *SystemInfoP
193201

194202
if params.PrimaryOSNames != nil {
195203
if len(params.PrimaryOSNames) == 0 {
196-
var setsCount uint8
197-
198204
p := &SystemInfoParam_PrimaryOSName{
199205
SetSelector: 0,
200206
}
201207
if err := c.GetSystemInfoParamFor(ctx, p); canIgnore(err) != nil {
202208
return err
203209
}
204-
if len(p.BlockData) < 2 {
210+
setsCount := calculateSetsCount(p.BlockData)
211+
if setsCount == 0 {
205212
return nil
206213
}
207-
stringLength := uint8(p.BlockData[1])
208-
setsCount = stringLength/16 + 1
209214

210215
params.PrimaryOSNames = make([]*SystemInfoParam_PrimaryOSName, setsCount)
211216
for i := uint8(0); i < setsCount; i++ {
@@ -225,19 +230,16 @@ func (c *Client) GetSystemInfoParamsFor(ctx context.Context, params *SystemInfoP
225230

226231
if params.OSNames != nil {
227232
if len(params.OSNames) == 0 {
228-
var setsCount uint8
229-
230233
p := &SystemInfoParam_OSName{
231234
SetSelector: 0,
232235
}
233236
if err := c.GetSystemInfoParamFor(ctx, p); canIgnore(err) != nil {
234237
return err
235238
}
236-
if len(p.BlockData) < 2 {
239+
setsCount := calculateSetsCount(p.BlockData)
240+
if setsCount == 0 {
237241
return nil
238242
}
239-
stringLength := uint8(p.BlockData[1])
240-
setsCount = stringLength/16 + 1
241243

242244
params.OSNames = make([]*SystemInfoParam_OSName, setsCount)
243245
for i := uint8(0); i < setsCount; i++ {
@@ -257,19 +259,16 @@ func (c *Client) GetSystemInfoParamsFor(ctx context.Context, params *SystemInfoP
257259

258260
if params.OSVersions != nil {
259261
if len(params.OSVersions) == 0 {
260-
var setsCount uint8
261-
262262
p := &SystemInfoParam_OSVersion{
263263
SetSelector: 0,
264264
}
265265
if err := c.GetSystemInfoParamFor(ctx, p); canIgnore(err) != nil {
266266
return err
267267
}
268-
if len(p.BlockData) < 2 {
268+
setsCount := calculateSetsCount(p.BlockData)
269+
if setsCount == 0 {
269270
return nil
270271
}
271-
stringLength := uint8(p.BlockData[1])
272-
setsCount = stringLength/16 + 1
273272

274273
params.OSVersions = make([]*SystemInfoParam_OSVersion, setsCount)
275274
for i := uint8(0); i < setsCount; i++ {
@@ -295,11 +294,10 @@ func (c *Client) GetSystemInfoParamsFor(ctx context.Context, params *SystemInfoP
295294
if err := c.GetSystemInfoParamFor(ctx, p); canIgnore(err) != nil {
296295
return err
297296
}
298-
if len(p.BlockData) < 2 {
297+
setsCount := calculateSetsCount(p.BlockData)
298+
if setsCount == 0 {
299299
return nil
300300
}
301-
stringLength := uint8(p.BlockData[1]) // string length 1-based
302-
setsCount := stringLength/16 + 1
303301

304302
params.BMCURLs = make([]*SystemInfoParam_BMCURL, setsCount)
305303
for i := uint8(0); i < setsCount; i++ {
@@ -325,11 +323,10 @@ func (c *Client) GetSystemInfoParamsFor(ctx context.Context, params *SystemInfoP
325323
if err := c.GetSystemInfoParamFor(ctx, p); canIgnore(err) != nil {
326324
return err
327325
}
328-
if len(p.BlockData) < 2 {
326+
setsCount := calculateSetsCount(p.BlockData)
327+
if setsCount == 0 {
329328
return nil
330329
}
331-
stringLength := uint8(p.BlockData[1]) // string length 1-based
332-
setsCount := stringLength/16 + 1
333330

334331
params.ManagementURLs = make([]*SystemInfoParam_ManagementURL, setsCount)
335332
for i := uint8(0); i < setsCount; i++ {

0 commit comments

Comments
 (0)