blob: cd332e627d056adf550e42b545c0e20fc98296e9 [file] [log] [blame]
Sampa Misra0db1dfa2019-03-19 00:15:31 -05001#include <endian.h>
2#include <string.h>
3
4#include "platform.h"
5
6int encode_set_state_effecter_states_resp(uint8_t instance_id,
7 uint8_t completion_code,
8 struct pldm_msg *msg)
9{
10 struct pldm_header_info header = {0};
11 int rc = PLDM_SUCCESS;
12
vkaverapa6575b82019-04-03 05:33:52 -050013 msg->payload[0] = completion_code;
Sampa Misra0db1dfa2019-03-19 00:15:31 -050014
15 header.msg_type = PLDM_RESPONSE;
16 header.instance = instance_id;
17 header.pldm_type = PLDM_PLATFORM;
18 header.command = PLDM_SET_STATE_EFFECTER_STATES;
19
20 rc = pack_pldm_header(&header, &(msg->hdr));
21
22 return rc;
23}
24
vkaverap98a2c192019-04-03 05:33:52 -050025int encode_set_state_effecter_states_req(uint8_t instance_id,
26 uint16_t effecter_id,
27 uint8_t comp_effecter_count,
28 set_effecter_state_field *field,
29 struct pldm_msg *msg)
30{
31 struct pldm_header_info header = {0};
32 int rc = PLDM_SUCCESS;
33
34 header.msg_type = PLDM_REQUEST;
35 header.instance = instance_id;
36 header.pldm_type = PLDM_PLATFORM;
37 header.command = PLDM_SET_STATE_EFFECTER_STATES;
38
39 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
40 return rc;
41 }
42
43 if (comp_effecter_count < 0x1 || comp_effecter_count > 0x8) {
44 return PLDM_ERROR_INVALID_DATA;
45 }
46
Priyanga7257fdf2019-06-10 01:59:45 -050047 struct pldm_set_state_effecter_states_req *request =
48 (struct pldm_set_state_effecter_states_req *)msg->payload;
vkaverap98a2c192019-04-03 05:33:52 -050049 effecter_id = htole16(effecter_id);
Priyanga7257fdf2019-06-10 01:59:45 -050050 request->effecter_id = effecter_id;
51 request->comp_effecter_count = comp_effecter_count;
52 memcpy(request->field, field,
vkaverap98a2c192019-04-03 05:33:52 -050053 (sizeof(set_effecter_state_field) * comp_effecter_count));
54
55 return PLDM_SUCCESS;
56}
57
Zahed Hossain223a73d2019-07-04 12:46:18 -050058int decode_set_state_effecter_states_resp(const struct pldm_msg *msg,
vkaverapa6575b82019-04-03 05:33:52 -050059 size_t payload_length,
vkaverap98a2c192019-04-03 05:33:52 -050060 uint8_t *completion_code)
61{
62 if (msg == NULL || completion_code == NULL) {
63 return PLDM_ERROR_INVALID_DATA;
64 }
65
George Liu684a7162019-12-06 15:10:52 +080066 *completion_code = msg->payload[0];
67 if (PLDM_SUCCESS != *completion_code) {
68 return PLDM_SUCCESS;
69 }
70
vkaverapa6575b82019-04-03 05:33:52 -050071 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES) {
72 return PLDM_ERROR_INVALID_LENGTH;
73 }
74
vkaverap98a2c192019-04-03 05:33:52 -050075 return PLDM_SUCCESS;
76}
77
Zahed Hossain223a73d2019-07-04 12:46:18 -050078int decode_set_state_effecter_states_req(const struct pldm_msg *msg,
vkaverapa6575b82019-04-03 05:33:52 -050079 size_t payload_length,
Sampa Misra0db1dfa2019-03-19 00:15:31 -050080 uint16_t *effecter_id,
81 uint8_t *comp_effecter_count,
82 set_effecter_state_field *field)
83{
84 if (msg == NULL || effecter_id == NULL || comp_effecter_count == NULL ||
85 field == NULL) {
86 return PLDM_ERROR_INVALID_DATA;
87 }
vkaverapa6575b82019-04-03 05:33:52 -050088
89 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) {
90 return PLDM_ERROR_INVALID_LENGTH;
91 }
92
Priyanga7257fdf2019-06-10 01:59:45 -050093 struct pldm_set_state_effecter_states_req *request =
Zahed Hossain223a73d2019-07-04 12:46:18 -050094 (struct pldm_set_state_effecter_states_req *)msg->payload;
Priyanga7257fdf2019-06-10 01:59:45 -050095
96 *effecter_id = le16toh(request->effecter_id);
97 *comp_effecter_count = request->comp_effecter_count;
98 memcpy(field, request->field,
Sampa Misra0db1dfa2019-03-19 00:15:31 -050099 (sizeof(set_effecter_state_field) * (*comp_effecter_count)));
100
101 return PLDM_SUCCESS;
102}
Sampa Misra7fcfb662019-05-08 13:13:53 -0500103
104int decode_get_pdr_req(const struct pldm_msg *msg, size_t payload_length,
105 uint32_t *record_hndl, uint32_t *data_transfer_hndl,
106 uint8_t *transfer_op_flag, uint16_t *request_cnt,
107 uint16_t *record_chg_num)
108{
109 if (msg == NULL || record_hndl == NULL || data_transfer_hndl == NULL ||
110 transfer_op_flag == NULL || request_cnt == NULL ||
111 record_chg_num == NULL) {
112 return PLDM_ERROR_INVALID_DATA;
113 }
114 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
115 return PLDM_ERROR_INVALID_LENGTH;
116 }
117
118 struct pldm_get_pdr_req *request =
119 (struct pldm_get_pdr_req *)msg->payload;
120 *record_hndl = le32toh(request->record_handle);
121 *data_transfer_hndl = le32toh(request->data_transfer_handle);
122 *transfer_op_flag = request->transfer_op_flag;
123 *request_cnt = le16toh(request->request_count);
124 *record_chg_num = le16toh(request->record_change_number);
125
126 return PLDM_SUCCESS;
127}
128
129int encode_get_pdr_resp(uint8_t instance_id, uint8_t completion_code,
130 uint32_t next_record_hndl,
131 uint32_t next_data_transfer_hndl, uint8_t transfer_flag,
132 uint16_t resp_cnt, const uint8_t *record_data,
133 uint8_t transfer_crc, struct pldm_msg *msg)
134{
135 struct pldm_header_info header = {0};
136 int rc = PLDM_SUCCESS;
137
138 if (msg == NULL) {
139 return PLDM_ERROR_INVALID_DATA;
140 }
141 struct pldm_get_pdr_resp *response =
142 (struct pldm_get_pdr_resp *)msg->payload;
143
144 response->completion_code = completion_code;
145
146 header.msg_type = PLDM_RESPONSE;
147 header.instance = instance_id;
148 header.pldm_type = PLDM_PLATFORM;
149 header.command = PLDM_GET_PDR;
150 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
151 return rc;
152 }
153
154 if (response->completion_code == PLDM_SUCCESS) {
155 response->next_record_handle = htole32(next_record_hndl);
156 response->next_data_transfer_handle =
157 htole32(next_data_transfer_hndl);
158 response->transfer_flag = transfer_flag;
159 response->response_count = htole16(resp_cnt);
160 if (record_data != NULL && resp_cnt > 0) {
161 memcpy(response->record_data, record_data, resp_cnt);
162 }
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500163 if (transfer_flag == PLDM_END) {
164 uint8_t *dst = msg->payload;
165 dst +=
166 (sizeof(struct pldm_get_pdr_resp) - 1) + resp_cnt;
167 *dst = transfer_crc;
168 }
Sampa Misra7fcfb662019-05-08 13:13:53 -0500169 }
170
171 return PLDM_SUCCESS;
172}
George Liu820a9a52019-11-26 14:43:59 +0800173
174int encode_get_pdr_req(uint8_t instance_id, uint32_t record_hndl,
175 uint32_t data_transfer_hndl, uint8_t transfer_op_flag,
176 uint16_t request_cnt, uint16_t record_chg_num,
177 struct pldm_msg *msg, size_t payload_length)
178{
179 struct pldm_header_info header = {0};
180 int rc = PLDM_SUCCESS;
181
182 if (msg == NULL) {
183 return PLDM_ERROR_INVALID_DATA;
184 }
185 struct pldm_get_pdr_req *request =
186 (struct pldm_get_pdr_req *)msg->payload;
187
188 header.msg_type = PLDM_REQUEST;
189 header.instance = instance_id;
190 header.pldm_type = PLDM_PLATFORM;
191 header.command = PLDM_GET_PDR;
192
193 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
194 return rc;
195 }
196
197 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
198 return PLDM_ERROR_INVALID_LENGTH;
199 }
200
201 request->record_handle = htole32(record_hndl);
202 request->data_transfer_handle = htole32(data_transfer_hndl);
203 request->transfer_op_flag = transfer_op_flag;
204 request->request_count = htole16(request_cnt);
205 request->record_change_number = htole16(record_chg_num);
206
207 return PLDM_SUCCESS;
208}
209
210int decode_get_pdr_resp(const struct pldm_msg *msg, size_t payload_length,
211 uint8_t *completion_code, uint32_t *next_record_hndl,
212 uint32_t *next_data_transfer_hndl,
213 uint8_t *transfer_flag, uint16_t *resp_cnt,
214 uint8_t *record_data, size_t record_data_length,
215 uint8_t *transfer_crc)
216{
217 if (msg == NULL || completion_code == NULL ||
218 next_record_hndl == NULL || next_data_transfer_hndl == NULL ||
219 transfer_flag == NULL || resp_cnt == NULL || record_data == NULL ||
220 transfer_crc == NULL) {
221 return PLDM_ERROR_INVALID_DATA;
222 }
223
224 *completion_code = msg->payload[0];
225 if (PLDM_SUCCESS != *completion_code) {
George Liu684a7162019-12-06 15:10:52 +0800226 return PLDM_SUCCESS;
George Liu820a9a52019-11-26 14:43:59 +0800227 }
228
229 if (payload_length < PLDM_GET_PDR_MIN_RESP_BYTES) {
230 return PLDM_ERROR_INVALID_LENGTH;
231 }
232
233 struct pldm_get_pdr_resp *response =
234 (struct pldm_get_pdr_resp *)msg->payload;
235
236 *next_record_hndl = le32toh(response->next_record_handle);
237 *next_data_transfer_hndl = le32toh(response->next_data_transfer_handle);
238 *transfer_flag = response->transfer_flag;
239 *resp_cnt = le16toh(response->response_count);
240
241 if (*transfer_flag != PLDM_END &&
242 (int)payload_length != PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt) {
243 return PLDM_ERROR_INVALID_LENGTH;
244 }
245
246 if (*transfer_flag == PLDM_END &&
247 (int)payload_length !=
248 PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt + 1) {
249 return PLDM_ERROR_INVALID_LENGTH;
250 }
251
252 if (*resp_cnt > 0) {
253 if (record_data_length < *resp_cnt) {
254 return PLDM_ERROR_INVALID_LENGTH;
255 }
256 memcpy(record_data, response->record_data, *resp_cnt);
257 }
258
259 if (*transfer_flag == PLDM_END) {
260 *transfer_crc =
261 msg->payload[PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt];
262 }
263
264 return PLDM_SUCCESS;
265}
George Liu30b859f2020-01-07 15:03:22 +0800266
267int decode_set_numeric_effecter_value_req(const struct pldm_msg *msg,
268 size_t payload_length,
269 uint16_t *effecter_id,
270 uint8_t *effecter_data_size,
271 uint8_t *effecter_value)
272{
273 if (msg == NULL || effecter_id == NULL || effecter_data_size == NULL ||
274 effecter_value == NULL) {
275 return PLDM_ERROR_INVALID_DATA;
276 }
277
278 if (payload_length < PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
279 return PLDM_ERROR_INVALID_LENGTH;
280 }
281
282 struct pldm_set_numeric_effecter_value_req *request =
283 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
284 *effecter_id = le16toh(request->effecter_id);
285 *effecter_data_size = request->effecter_data_size;
286
287 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
288 return PLDM_ERROR_INVALID_DATA;
289 }
290
291 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
292 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
293
294 if (payload_length !=
295 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
296 return PLDM_ERROR_INVALID_LENGTH;
297 }
298
299 *effecter_value = request->effecter_value[0];
300 }
301
302 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
303 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
304
305 if (payload_length !=
306 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
307 return PLDM_ERROR_INVALID_LENGTH;
308 }
309
310 memcpy(effecter_value, request->effecter_value, 2);
311 *effecter_value = le16toh(*effecter_value);
312 }
313
314 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
315 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
316
317 if (payload_length !=
318 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
319 return PLDM_ERROR_INVALID_LENGTH;
320 }
321
322 memcpy(effecter_value, request->effecter_value, 4);
George Liu30b859f2020-01-07 15:03:22 +0800323 }
324
325 return PLDM_SUCCESS;
326}
327
328int encode_set_numeric_effecter_value_resp(uint8_t instance_id,
329 uint8_t completion_code,
330 struct pldm_msg *msg,
331 size_t payload_length)
332{
333 struct pldm_header_info header = {0};
334 int rc = PLDM_SUCCESS;
335
336 if (msg == NULL) {
337 return PLDM_ERROR_INVALID_DATA;
338 }
339
340 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
341 return PLDM_ERROR_INVALID_LENGTH;
342 }
343
344 msg->payload[0] = completion_code;
345
346 header.msg_type = PLDM_RESPONSE;
347 header.instance = instance_id;
348 header.pldm_type = PLDM_PLATFORM;
349 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
350
351 rc = pack_pldm_header(&header, &(msg->hdr));
352
353 return rc;
354}
355
356int encode_set_numeric_effecter_value_req(
357 uint8_t instance_id, uint16_t effecter_id, uint8_t effecter_data_size,
358 uint8_t *effecter_value, struct pldm_msg *msg, size_t payload_length)
359{
360 struct pldm_header_info header = {0};
361 int rc = PLDM_SUCCESS;
362
363 if (msg == NULL || effecter_value == NULL) {
364 return PLDM_ERROR_INVALID_DATA;
365 }
366 struct pldm_set_numeric_effecter_value_req *request =
367 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
368
369 header.msg_type = PLDM_REQUEST;
370 header.instance = instance_id;
371 header.pldm_type = PLDM_PLATFORM;
372 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
373
374 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
375 return rc;
376 }
377
378 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
379 return PLDM_ERROR_INVALID_DATA;
380 }
381
382 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
383 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
384 if (payload_length !=
385 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
386 return PLDM_ERROR_INVALID_LENGTH;
387 }
388 request->effecter_value[0] = *effecter_value;
389 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
390 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
391 if (payload_length !=
392 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
393 return PLDM_ERROR_INVALID_LENGTH;
394 }
395
396 memcpy(request->effecter_value, effecter_value, 2);
397 *request->effecter_value = htole16(*request->effecter_value);
398 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
399 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
400 if (payload_length !=
401 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
402 return PLDM_ERROR_INVALID_LENGTH;
403 }
404
405 memcpy(request->effecter_value, effecter_value, 4);
406 *request->effecter_value = htole32(*request->effecter_value);
407 }
408
409 request->effecter_id = htole16(effecter_id);
410 request->effecter_data_size = effecter_data_size;
411
412 return PLDM_SUCCESS;
413}
414
415int decode_set_numeric_effecter_value_resp(const struct pldm_msg *msg,
416 size_t payload_length,
417 uint8_t *completion_code)
418{
419 if (msg == NULL || completion_code == NULL) {
420 return PLDM_ERROR_INVALID_DATA;
421 }
422
423 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
424 return PLDM_ERROR_INVALID_LENGTH;
425 }
426
427 *completion_code = msg->payload[0];
428
429 return PLDM_SUCCESS;
430}
Jolie Ku3557bad2020-03-02 16:22:57 +0800431
432int encode_get_state_sensor_readings_resp(uint8_t instance_id,
433 uint8_t completion_code,
434 uint8_t comp_sensor_count,
435 get_sensor_state_field *field,
436 struct pldm_msg *msg)
437{
438 struct pldm_header_info header = {0};
439 int rc = PLDM_SUCCESS;
440
441 header.msg_type = PLDM_RESPONSE;
442 header.instance = instance_id;
443 header.pldm_type = PLDM_PLATFORM;
444 header.command = PLDM_GET_STATE_SENSOR_READINGS;
445
446 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
447 return rc;
448 }
449
450 if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
451 return PLDM_ERROR_INVALID_DATA;
452 }
453
454 struct pldm_get_state_sensor_readings_resp *response =
455 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
456
457 response->completion_code = completion_code;
458 response->comp_sensor_count = comp_sensor_count;
459 memcpy(response->field, field,
460 (sizeof(get_sensor_state_field) * comp_sensor_count));
461
462 return rc;
463}
464
465int encode_get_state_sensor_readings_req(uint8_t instance_id,
466 uint16_t sensor_id,
467 bitfield8_t sensor_rearm,
468 uint8_t reserved, struct pldm_msg *msg)
469{
470 struct pldm_header_info header = {0};
471 int rc = PLDM_SUCCESS;
472
473 header.msg_type = PLDM_REQUEST;
474 header.instance = instance_id;
475 header.pldm_type = PLDM_PLATFORM;
476 header.command = PLDM_GET_STATE_SENSOR_READINGS;
477
478 if (msg == NULL) {
479 return PLDM_ERROR_INVALID_DATA;
480 }
481
482 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
483 return rc;
484 }
485
486 struct pldm_get_state_sensor_readings_req *request =
487 (struct pldm_get_state_sensor_readings_req *)msg->payload;
488
489 request->sensor_id = htole16(sensor_id);
490 request->reserved = reserved;
491 request->sensor_rearm = sensor_rearm;
492
493 return PLDM_SUCCESS;
494}
495
496int decode_get_state_sensor_readings_resp(const struct pldm_msg *msg,
497 size_t payload_length,
498 uint8_t *completion_code,
499 uint8_t *comp_sensor_count,
500 get_sensor_state_field *field)
501{
502 if (msg == NULL || completion_code == NULL ||
503 comp_sensor_count == NULL || field == NULL) {
504 return PLDM_ERROR_INVALID_DATA;
505 }
506
507 *completion_code = msg->payload[0];
508 if (PLDM_SUCCESS != *completion_code) {
509 return PLDM_SUCCESS;
510 }
511
512 if (payload_length > PLDM_GET_STATE_SENSOR_READINGS_RESP_BYTES) {
513 return PLDM_ERROR_INVALID_LENGTH;
514 }
515
516 struct pldm_get_state_sensor_readings_resp *response =
517 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
518
519 if (response->comp_sensor_count < 0x1 ||
520 response->comp_sensor_count > 0x8) {
521 return PLDM_ERROR_INVALID_DATA;
522 }
523 if (response->comp_sensor_count > *comp_sensor_count) {
524 return PLDM_ERROR_INVALID_LENGTH;
525 }
526 *comp_sensor_count = response->comp_sensor_count;
527
528 memcpy(field, response->field,
529 (sizeof(get_sensor_state_field) * (*comp_sensor_count)));
530
531 return PLDM_SUCCESS;
532}
533
534int decode_get_state_sensor_readings_req(const struct pldm_msg *msg,
535 size_t payload_length,
536 uint16_t *sensor_id,
537 bitfield8_t *sensor_rearm,
538 uint8_t *reserved)
539{
540 if (msg == NULL || sensor_id == NULL || sensor_rearm == NULL) {
541 return PLDM_ERROR_INVALID_DATA;
542 }
543
544 if (payload_length != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES) {
545 return PLDM_ERROR_INVALID_LENGTH;
546 }
547
548 struct pldm_get_state_sensor_readings_req *request =
549 (struct pldm_get_state_sensor_readings_req *)msg->payload;
550
551 *sensor_id = le16toh(request->sensor_id);
552 *reserved = request->reserved;
553 memcpy(&(sensor_rearm->byte), &(request->sensor_rearm.byte),
554 sizeof(request->sensor_rearm.byte));
555
556 return PLDM_SUCCESS;
557}