blob: 8b83747866f418ca3a61345cc1c020c404705ca6 [file] [log] [blame]
Andrew Jeffery9c766792022-08-10 23:12:49 +09301#include <endian.h>
2#include <string.h>
3
4#include "platform.h"
5
6int encode_state_effecter_pdr(
7 struct pldm_state_effecter_pdr *const effecter,
8 const size_t allocation_size,
9 const struct state_effecter_possible_states *const possible_states,
10 const size_t possible_states_size, size_t *const actual_size)
11{
12 // Encode possible states
13
14 size_t calculated_possible_states_size = 0;
15
16 {
17 char *states_ptr = (char *)possible_states;
18 char *const begin_states_ptr = states_ptr;
19
20 for (int i = 0; i < effecter->composite_effecter_count; ++i) {
21 struct state_effecter_possible_states *states =
22 (struct state_effecter_possible_states *)states_ptr;
23
24 HTOLE16(states->state_set_id);
25
26 states_ptr +=
27 (sizeof(*states) - sizeof(states->states) +
28 states->possible_states_size);
29 }
30
31 calculated_possible_states_size = states_ptr - begin_states_ptr;
32 }
33
34 // Check lengths
35
36 if (possible_states_size != calculated_possible_states_size) {
37 *actual_size = 0;
38 return PLDM_ERROR;
39 }
40
41 *actual_size =
42 (sizeof(struct pldm_state_effecter_pdr) + possible_states_size -
43 sizeof(effecter->possible_states));
44
45 if (allocation_size < *actual_size) {
46 *actual_size = 0;
47 return PLDM_ERROR_INVALID_LENGTH;
48 }
49
50 // Encode rest of PDR
51
52 effecter->hdr.version = 1;
53 effecter->hdr.type = PLDM_STATE_EFFECTER_PDR;
54 effecter->hdr.length = *actual_size - sizeof(struct pldm_pdr_hdr);
55
56 memcpy(effecter->possible_states, possible_states,
57 possible_states_size);
58
59 // Convert effecter PDR body
60 HTOLE16(effecter->terminus_handle);
61 HTOLE16(effecter->effecter_id);
62 HTOLE16(effecter->entity_type);
63 HTOLE16(effecter->entity_instance);
64 HTOLE16(effecter->container_id);
65 HTOLE16(effecter->effecter_semantic_id);
66
67 // Convert header
68 HTOLE32(effecter->hdr.record_handle);
69 HTOLE16(effecter->hdr.record_change_num);
70 HTOLE16(effecter->hdr.length);
71
72 return PLDM_SUCCESS;
73}
74
75int encode_state_sensor_pdr(
76 struct pldm_state_sensor_pdr *const sensor, const size_t allocation_size,
77 const struct state_sensor_possible_states *const possible_states,
78 const size_t possible_states_size, size_t *const actual_size)
79{
80 // Encode possible states
81
82 size_t calculated_possible_states_size = 0;
83
84 {
85 char *states_ptr = (char *)possible_states,
86 *const begin_states_ptr = states_ptr;
87
88 for (int i = 0; i < sensor->composite_sensor_count; ++i) {
89 struct state_sensor_possible_states *states =
90 (struct state_sensor_possible_states *)states_ptr;
91
92 HTOLE16(states->state_set_id);
93
94 states_ptr +=
95 (sizeof(*states) - sizeof(states->states) +
96 states->possible_states_size);
97 }
98
99 calculated_possible_states_size = states_ptr - begin_states_ptr;
100 }
101
102 // Check lengths
103
104 if (possible_states_size != calculated_possible_states_size) {
105 *actual_size = 0;
106 return PLDM_ERROR;
107 }
108
109 *actual_size = (sizeof(struct pldm_state_sensor_pdr) +
110 possible_states_size - sizeof(sensor->possible_states));
111
112 if (allocation_size < *actual_size) {
113 *actual_size = 0;
114 return PLDM_ERROR_INVALID_LENGTH;
115 }
116
117 // Encode rest of PDR
118
119 sensor->hdr.version = 1;
120 sensor->hdr.type = PLDM_STATE_SENSOR_PDR;
121 sensor->hdr.length = *actual_size - sizeof(struct pldm_pdr_hdr);
122
123 memcpy(sensor->possible_states, possible_states, possible_states_size);
124
125 // Convert sensor PDR body
126 HTOLE16(sensor->terminus_handle);
127 HTOLE16(sensor->sensor_id);
128 HTOLE16(sensor->entity_type);
129 HTOLE16(sensor->entity_instance);
130 HTOLE16(sensor->container_id);
131
132 // Convert header
133 HTOLE32(sensor->hdr.record_handle);
134 HTOLE16(sensor->hdr.record_change_num);
135 HTOLE16(sensor->hdr.length);
136
137 return PLDM_SUCCESS;
138}
139
140int encode_set_state_effecter_states_resp(uint8_t instance_id,
141 uint8_t completion_code,
142 struct pldm_msg *msg)
143{
144 if (msg == NULL) {
145 return PLDM_ERROR_INVALID_DATA;
146 }
147
148 struct pldm_header_info header = {0};
149 header.msg_type = PLDM_RESPONSE;
150 header.instance = instance_id;
151 header.pldm_type = PLDM_PLATFORM;
152 header.command = PLDM_SET_STATE_EFFECTER_STATES;
153
154 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
155 if (rc != PLDM_SUCCESS) {
156 return rc;
157 }
158
159 msg->payload[0] = completion_code;
160
161 return PLDM_SUCCESS;
162}
163
164int encode_set_state_effecter_states_req(uint8_t instance_id,
165 uint16_t effecter_id,
166 uint8_t comp_effecter_count,
167 set_effecter_state_field *field,
168 struct pldm_msg *msg)
169{
170 if (msg == NULL) {
171 return PLDM_ERROR_INVALID_DATA;
172 }
173
174 if (comp_effecter_count < 0x1 || comp_effecter_count > 0x8 ||
175 field == NULL) {
176 return PLDM_ERROR_INVALID_DATA;
177 }
178
179 struct pldm_header_info header = {0};
180 header.msg_type = PLDM_REQUEST;
181 header.instance = instance_id;
182 header.pldm_type = PLDM_PLATFORM;
183 header.command = PLDM_SET_STATE_EFFECTER_STATES;
184
185 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
186 if (rc != PLDM_SUCCESS) {
187 return rc;
188 }
189
190 struct pldm_set_state_effecter_states_req *request =
191 (struct pldm_set_state_effecter_states_req *)msg->payload;
192 effecter_id = htole16(effecter_id);
193 request->effecter_id = effecter_id;
194 request->comp_effecter_count = comp_effecter_count;
195 memcpy(request->field, field,
196 (sizeof(set_effecter_state_field) * comp_effecter_count));
197
198 return PLDM_SUCCESS;
199}
200
201int decode_set_state_effecter_states_resp(const struct pldm_msg *msg,
202 size_t payload_length,
203 uint8_t *completion_code)
204{
205 if (msg == NULL || completion_code == NULL) {
206 return PLDM_ERROR_INVALID_DATA;
207 }
208
209 *completion_code = msg->payload[0];
210 if (PLDM_SUCCESS != *completion_code) {
211 return PLDM_SUCCESS;
212 }
213
214 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES) {
215 return PLDM_ERROR_INVALID_LENGTH;
216 }
217
218 return PLDM_SUCCESS;
219}
220
221int decode_set_state_effecter_states_req(const struct pldm_msg *msg,
222 size_t payload_length,
223 uint16_t *effecter_id,
224 uint8_t *comp_effecter_count,
225 set_effecter_state_field *field)
226{
227 if (msg == NULL || effecter_id == NULL || comp_effecter_count == NULL ||
228 field == NULL) {
229 return PLDM_ERROR_INVALID_DATA;
230 }
231
232 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) {
233 return PLDM_ERROR_INVALID_LENGTH;
234 }
235
236 struct pldm_set_state_effecter_states_req *request =
237 (struct pldm_set_state_effecter_states_req *)msg->payload;
238
239 *effecter_id = le16toh(request->effecter_id);
240 *comp_effecter_count = request->comp_effecter_count;
241 memcpy(field, request->field,
242 (sizeof(set_effecter_state_field) * (*comp_effecter_count)));
243
244 return PLDM_SUCCESS;
245}
246
247int decode_get_pdr_req(const struct pldm_msg *msg, size_t payload_length,
248 uint32_t *record_hndl, uint32_t *data_transfer_hndl,
249 uint8_t *transfer_op_flag, uint16_t *request_cnt,
250 uint16_t *record_chg_num)
251{
252 if (msg == NULL || record_hndl == NULL || data_transfer_hndl == NULL ||
253 transfer_op_flag == NULL || request_cnt == NULL ||
254 record_chg_num == NULL) {
255 return PLDM_ERROR_INVALID_DATA;
256 }
257 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
258 return PLDM_ERROR_INVALID_LENGTH;
259 }
260
261 struct pldm_get_pdr_req *request =
262 (struct pldm_get_pdr_req *)msg->payload;
263 *record_hndl = le32toh(request->record_handle);
264 *data_transfer_hndl = le32toh(request->data_transfer_handle);
265 *transfer_op_flag = request->transfer_op_flag;
266 *request_cnt = le16toh(request->request_count);
267 *record_chg_num = le16toh(request->record_change_number);
268
269 return PLDM_SUCCESS;
270}
271
272int encode_get_pdr_resp(uint8_t instance_id, uint8_t completion_code,
273 uint32_t next_record_hndl,
274 uint32_t next_data_transfer_hndl, uint8_t transfer_flag,
275 uint16_t resp_cnt, const uint8_t *record_data,
276 uint8_t transfer_crc, struct pldm_msg *msg)
277{
278 if (msg == NULL) {
279 return PLDM_ERROR_INVALID_DATA;
280 }
281
282 struct pldm_header_info header = {0};
283 header.msg_type = PLDM_RESPONSE;
284 header.instance = instance_id;
285 header.pldm_type = PLDM_PLATFORM;
286 header.command = PLDM_GET_PDR;
287
288 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
289 if (rc != PLDM_SUCCESS) {
290 return rc;
291 }
292
293 struct pldm_get_pdr_resp *response =
294 (struct pldm_get_pdr_resp *)msg->payload;
295 response->completion_code = completion_code;
296
297 if (response->completion_code == PLDM_SUCCESS) {
298 response->next_record_handle = htole32(next_record_hndl);
299 response->next_data_transfer_handle =
300 htole32(next_data_transfer_hndl);
301 response->transfer_flag = transfer_flag;
302 response->response_count = htole16(resp_cnt);
303 if (record_data != NULL && resp_cnt > 0) {
304 memcpy(response->record_data, record_data, resp_cnt);
305 }
306 if (transfer_flag == PLDM_END) {
307 uint8_t *dst = msg->payload;
308 dst +=
309 (sizeof(struct pldm_get_pdr_resp) - 1) + resp_cnt;
310 *dst = transfer_crc;
311 }
312 }
313
314 return PLDM_SUCCESS;
315}
316
317int encode_get_pdr_repository_info_resp(
318 uint8_t instance_id, uint8_t completion_code, uint8_t repository_state,
319 const uint8_t *update_time, const uint8_t *oem_update_time,
320 uint32_t record_count, uint32_t repository_size,
321 uint32_t largest_record_size, uint8_t data_transfer_handle_timeout,
322 struct pldm_msg *msg)
323{
324 if (msg == NULL) {
325 return PLDM_ERROR_INVALID_DATA;
326 }
327
328 struct pldm_header_info header = {0};
329 header.msg_type = PLDM_RESPONSE;
330 header.instance = instance_id;
331 header.pldm_type = PLDM_PLATFORM;
332 header.command = PLDM_GET_PDR_REPOSITORY_INFO;
333
334 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
335 if (rc != PLDM_SUCCESS) {
336 return rc;
337 }
338
339 struct pldm_pdr_repository_info_resp *response =
340 (struct pldm_pdr_repository_info_resp *)msg->payload;
341 response->completion_code = completion_code;
342
343 if (response->completion_code == PLDM_SUCCESS) {
344 response->repository_state = repository_state;
345 if (update_time != NULL) {
346 memcpy(response->update_time, update_time,
347 PLDM_TIMESTAMP104_SIZE);
348 }
349 if (oem_update_time != NULL) {
350 memcpy(response->oem_update_time, oem_update_time,
351 PLDM_TIMESTAMP104_SIZE);
352 }
353 response->record_count = htole32(record_count);
354 response->repository_size = htole32(repository_size);
355 response->largest_record_size = htole32(largest_record_size);
356 response->data_transfer_handle_timeout =
357 data_transfer_handle_timeout;
358 }
359
360 return PLDM_SUCCESS;
361}
362
363int encode_get_pdr_req(uint8_t instance_id, uint32_t record_hndl,
364 uint32_t data_transfer_hndl, uint8_t transfer_op_flag,
365 uint16_t request_cnt, uint16_t record_chg_num,
366 struct pldm_msg *msg, size_t payload_length)
367{
368 if (msg == NULL) {
369 return PLDM_ERROR_INVALID_DATA;
370 }
371
372 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
373 return PLDM_ERROR_INVALID_LENGTH;
374 }
375
376 struct pldm_header_info header = {0};
377 header.msg_type = PLDM_REQUEST;
378 header.instance = instance_id;
379 header.pldm_type = PLDM_PLATFORM;
380 header.command = PLDM_GET_PDR;
381
382 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
383 if (rc != PLDM_SUCCESS) {
384 return rc;
385 }
386
387 struct pldm_get_pdr_req *request =
388 (struct pldm_get_pdr_req *)msg->payload;
389 request->record_handle = htole32(record_hndl);
390 request->data_transfer_handle = htole32(data_transfer_hndl);
391 request->transfer_op_flag = transfer_op_flag;
392 request->request_count = htole16(request_cnt);
393 request->record_change_number = htole16(record_chg_num);
394
395 return PLDM_SUCCESS;
396}
397
398int decode_get_pdr_resp(const struct pldm_msg *msg, size_t payload_length,
399 uint8_t *completion_code, uint32_t *next_record_hndl,
400 uint32_t *next_data_transfer_hndl,
401 uint8_t *transfer_flag, uint16_t *resp_cnt,
402 uint8_t *record_data, size_t record_data_length,
403 uint8_t *transfer_crc)
404{
405 if (msg == NULL || completion_code == NULL ||
406 next_record_hndl == NULL || next_data_transfer_hndl == NULL ||
407 transfer_flag == NULL || resp_cnt == NULL || transfer_crc == NULL) {
408 return PLDM_ERROR_INVALID_DATA;
409 }
410
411 *completion_code = msg->payload[0];
412 if (PLDM_SUCCESS != *completion_code) {
413 return PLDM_SUCCESS;
414 }
415
416 if (payload_length < PLDM_GET_PDR_MIN_RESP_BYTES) {
417 return PLDM_ERROR_INVALID_LENGTH;
418 }
419
420 struct pldm_get_pdr_resp *response =
421 (struct pldm_get_pdr_resp *)msg->payload;
422
423 *next_record_hndl = le32toh(response->next_record_handle);
424 *next_data_transfer_hndl = le32toh(response->next_data_transfer_handle);
425 *transfer_flag = response->transfer_flag;
426 *resp_cnt = le16toh(response->response_count);
427
428 if (*transfer_flag != PLDM_END &&
429 (int)payload_length != PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt) {
430 return PLDM_ERROR_INVALID_LENGTH;
431 }
432
433 if (*transfer_flag == PLDM_END &&
434 (int)payload_length !=
435 PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt + 1) {
436 return PLDM_ERROR_INVALID_LENGTH;
437 }
438
439 if (*resp_cnt > 0 && record_data != NULL) {
440 if (record_data_length < *resp_cnt) {
441 return PLDM_ERROR_INVALID_LENGTH;
442 }
443 memcpy(record_data, response->record_data, *resp_cnt);
444 }
445
446 if (*transfer_flag == PLDM_END) {
447 *transfer_crc =
448 msg->payload[PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt];
449 }
450
451 return PLDM_SUCCESS;
452}
453
454int decode_set_numeric_effecter_value_req(const struct pldm_msg *msg,
455 size_t payload_length,
456 uint16_t *effecter_id,
457 uint8_t *effecter_data_size,
458 uint8_t *effecter_value)
459{
460 if (msg == NULL || effecter_id == NULL || effecter_data_size == NULL ||
461 effecter_value == NULL) {
462 return PLDM_ERROR_INVALID_DATA;
463 }
464
465 if (payload_length < PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
466 return PLDM_ERROR_INVALID_LENGTH;
467 }
468
469 struct pldm_set_numeric_effecter_value_req *request =
470 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
471 *effecter_id = le16toh(request->effecter_id);
472 *effecter_data_size = request->effecter_data_size;
473
474 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
475 return PLDM_ERROR_INVALID_DATA;
476 }
477
478 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
479 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
480
481 if (payload_length !=
482 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
483 return PLDM_ERROR_INVALID_LENGTH;
484 }
485
486 *effecter_value = request->effecter_value[0];
487 }
488
489 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
490 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
491
492 if (payload_length !=
493 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
494 return PLDM_ERROR_INVALID_LENGTH;
495 }
496
497 memcpy(effecter_value, request->effecter_value, 2);
498 uint16_t *val = (uint16_t *)(effecter_value);
499 *val = le16toh(*val);
500 }
501
502 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
503 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
504
505 if (payload_length !=
506 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
507 return PLDM_ERROR_INVALID_LENGTH;
508 }
509
510 memcpy(effecter_value, request->effecter_value, 4);
511 uint32_t *val = (uint32_t *)(effecter_value);
512 *val = le32toh(*val);
513 }
514
515 return PLDM_SUCCESS;
516}
517
518int encode_set_numeric_effecter_value_resp(uint8_t instance_id,
519 uint8_t completion_code,
520 struct pldm_msg *msg,
521 size_t payload_length)
522{
523 if (msg == NULL) {
524 return PLDM_ERROR_INVALID_DATA;
525 }
526
527 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
528 return PLDM_ERROR_INVALID_LENGTH;
529 }
530
531 struct pldm_header_info header = {0};
532 header.msg_type = PLDM_RESPONSE;
533 header.instance = instance_id;
534 header.pldm_type = PLDM_PLATFORM;
535 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
536
537 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
538 if (rc != PLDM_SUCCESS) {
539 return rc;
540 }
541
542 msg->payload[0] = completion_code;
543
544 return rc;
545}
546
547int encode_set_numeric_effecter_value_req(
548 uint8_t instance_id, uint16_t effecter_id, uint8_t effecter_data_size,
549 uint8_t *effecter_value, struct pldm_msg *msg, size_t payload_length)
550{
551 if (msg == NULL || effecter_value == NULL) {
552 return PLDM_ERROR_INVALID_DATA;
553 }
554
555 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
556 return PLDM_ERROR_INVALID_DATA;
557 }
558
559 struct pldm_header_info header = {0};
560 header.msg_type = PLDM_REQUEST;
561 header.instance = instance_id;
562 header.pldm_type = PLDM_PLATFORM;
563 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
564
565 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
566 if (rc != PLDM_SUCCESS) {
567 return rc;
568 }
569
570 struct pldm_set_numeric_effecter_value_req *request =
571 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
572 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
573 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
574 if (payload_length !=
575 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
576 return PLDM_ERROR_INVALID_LENGTH;
577 }
578 request->effecter_value[0] = *effecter_value;
579 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
580 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
581 if (payload_length !=
582 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
583 return PLDM_ERROR_INVALID_LENGTH;
584 }
585
586 uint16_t val = *(uint16_t *)(effecter_value);
587 val = htole16(val);
588 memcpy(request->effecter_value, &val, sizeof(uint16_t));
589
590 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
591 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
592 if (payload_length !=
593 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
594 return PLDM_ERROR_INVALID_LENGTH;
595 }
596
597 uint32_t val = *(uint32_t *)(effecter_value);
598 val = htole32(val);
599 memcpy(request->effecter_value, &val, sizeof(uint32_t));
600 }
601
602 request->effecter_id = htole16(effecter_id);
603 request->effecter_data_size = effecter_data_size;
604
605 return PLDM_SUCCESS;
606}
607
608int decode_set_numeric_effecter_value_resp(const struct pldm_msg *msg,
609 size_t payload_length,
610 uint8_t *completion_code)
611{
612 if (msg == NULL || completion_code == NULL) {
613 return PLDM_ERROR_INVALID_DATA;
614 }
615
616 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
617 return PLDM_ERROR_INVALID_LENGTH;
618 }
619
620 *completion_code = msg->payload[0];
621
622 return PLDM_SUCCESS;
623}
624
625int encode_get_state_sensor_readings_resp(uint8_t instance_id,
626 uint8_t completion_code,
627 uint8_t comp_sensor_count,
628 get_sensor_state_field *field,
629 struct pldm_msg *msg)
630{
631 if (msg == NULL) {
632 return PLDM_ERROR_INVALID_DATA;
633 }
634
635 if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
636 return PLDM_ERROR_INVALID_DATA;
637 }
638
639 struct pldm_header_info header = {0};
640 header.msg_type = PLDM_RESPONSE;
641 header.instance = instance_id;
642 header.pldm_type = PLDM_PLATFORM;
643 header.command = PLDM_GET_STATE_SENSOR_READINGS;
644
645 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
646 if (rc != PLDM_SUCCESS) {
647 return rc;
648 }
649
650 struct pldm_get_state_sensor_readings_resp *response =
651 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
652
653 response->completion_code = completion_code;
654 response->comp_sensor_count = comp_sensor_count;
655 memcpy(response->field, field,
656 (sizeof(get_sensor_state_field) * comp_sensor_count));
657
658 return PLDM_SUCCESS;
659}
660
661int encode_get_state_sensor_readings_req(uint8_t instance_id,
662 uint16_t sensor_id,
663 bitfield8_t sensor_rearm,
664 uint8_t reserved, struct pldm_msg *msg)
665{
666 if (msg == NULL) {
667 return PLDM_ERROR_INVALID_DATA;
668 }
669
670 struct pldm_header_info header = {0};
671 header.msg_type = PLDM_REQUEST;
672 header.instance = instance_id;
673 header.pldm_type = PLDM_PLATFORM;
674 header.command = PLDM_GET_STATE_SENSOR_READINGS;
675
676 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
677 if (rc != PLDM_SUCCESS) {
678 return rc;
679 }
680
681 struct pldm_get_state_sensor_readings_req *request =
682 (struct pldm_get_state_sensor_readings_req *)msg->payload;
683
684 request->sensor_id = htole16(sensor_id);
685 request->reserved = reserved;
686 request->sensor_rearm = sensor_rearm;
687
688 return PLDM_SUCCESS;
689}
690
691int decode_get_state_sensor_readings_resp(const struct pldm_msg *msg,
692 size_t payload_length,
693 uint8_t *completion_code,
694 uint8_t *comp_sensor_count,
695 get_sensor_state_field *field)
696{
697 if (msg == NULL || completion_code == NULL ||
698 comp_sensor_count == NULL || field == NULL) {
699 return PLDM_ERROR_INVALID_DATA;
700 }
701
702 *completion_code = msg->payload[0];
703 if (PLDM_SUCCESS != *completion_code) {
704 return PLDM_SUCCESS;
705 }
706
707 struct pldm_get_state_sensor_readings_resp *response =
708 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
709
710 if (response->comp_sensor_count < 0x1 ||
711 response->comp_sensor_count > 0x8) {
712 return PLDM_ERROR_INVALID_DATA;
713 }
714
715 if (payload_length >
716 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
717 sizeof(get_sensor_state_field) * response->comp_sensor_count) {
718 return PLDM_ERROR_INVALID_LENGTH;
719 }
720
721 *comp_sensor_count = response->comp_sensor_count;
722
723 memcpy(field, response->field,
724 (sizeof(get_sensor_state_field) * (*comp_sensor_count)));
725
726 return PLDM_SUCCESS;
727}
728
729int decode_get_state_sensor_readings_req(const struct pldm_msg *msg,
730 size_t payload_length,
731 uint16_t *sensor_id,
732 bitfield8_t *sensor_rearm,
733 uint8_t *reserved)
734{
735 if (msg == NULL || sensor_id == NULL || sensor_rearm == NULL) {
736 return PLDM_ERROR_INVALID_DATA;
737 }
738
739 if (payload_length != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES) {
740 return PLDM_ERROR_INVALID_LENGTH;
741 }
742
743 struct pldm_get_state_sensor_readings_req *request =
744 (struct pldm_get_state_sensor_readings_req *)msg->payload;
745
746 *sensor_id = le16toh(request->sensor_id);
747 *reserved = request->reserved;
748 memcpy(&(sensor_rearm->byte), &(request->sensor_rearm.byte),
749 sizeof(request->sensor_rearm.byte));
750
751 return PLDM_SUCCESS;
752}
753
754int encode_sensor_event_data(
755 struct pldm_sensor_event_data *const event_data,
756 const size_t event_data_size, const uint16_t sensor_id,
757 const enum sensor_event_class_states sensor_event_class,
758 const uint8_t sensor_offset, const uint8_t event_state,
759 const uint8_t previous_event_state, size_t *const actual_event_data_size)
760{
761 *actual_event_data_size =
762 (sizeof(*event_data) - sizeof(event_data->event_class) +
763 sizeof(struct pldm_sensor_event_state_sensor_state));
764
765 if (!event_data) {
766 return PLDM_SUCCESS;
767 }
768
769 if (event_data_size < *actual_event_data_size) {
770 *actual_event_data_size = 0;
771 return PLDM_ERROR_INVALID_LENGTH;
772 }
773
774 event_data->sensor_id = htole16(sensor_id);
775 event_data->sensor_event_class_type = sensor_event_class;
776
777 struct pldm_sensor_event_state_sensor_state *const state_data =
778 (struct pldm_sensor_event_state_sensor_state *)
779 event_data->event_class;
780
781 state_data->sensor_offset = sensor_offset;
782 state_data->event_state = event_state;
783 state_data->previous_event_state = previous_event_state;
784
785 return PLDM_SUCCESS;
786}
787
788int decode_platform_event_message_req(const struct pldm_msg *msg,
789 size_t payload_length,
790 uint8_t *format_version, uint8_t *tid,
791 uint8_t *event_class,
792 size_t *event_data_offset)
793{
794
795 if (msg == NULL || format_version == NULL || tid == NULL ||
796 event_class == NULL || event_data_offset == NULL) {
797 return PLDM_ERROR_INVALID_DATA;
798 }
799
800 if (payload_length <= PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES) {
801 return PLDM_ERROR_INVALID_LENGTH;
802 }
803 struct pldm_platform_event_message_req *response =
804 (struct pldm_platform_event_message_req *)msg->payload;
805
806 *format_version = response->format_version;
807 *tid = response->tid;
808 *event_class = response->event_class;
809 *event_data_offset =
810 sizeof(*format_version) + sizeof(*tid) + sizeof(*event_class);
811
812 return PLDM_SUCCESS;
813}
814
815int encode_platform_event_message_resp(uint8_t instance_id,
816 uint8_t completion_code,
817 uint8_t platform_event_status,
818 struct pldm_msg *msg)
819{
820 if (msg == NULL) {
821 return PLDM_ERROR_INVALID_DATA;
822 }
823
824 if (platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
825 return PLDM_ERROR_INVALID_DATA;
826 }
827
828 struct pldm_header_info header = {0};
829 header.msg_type = PLDM_RESPONSE;
830 header.instance = instance_id;
831 header.pldm_type = PLDM_PLATFORM;
832 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
833
834 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
835 if (rc != PLDM_SUCCESS) {
836 return rc;
837 }
838
839 struct pldm_platform_event_message_resp *response =
840 (struct pldm_platform_event_message_resp *)msg->payload;
841 response->completion_code = completion_code;
842 response->platform_event_status = platform_event_status;
843
844 return PLDM_SUCCESS;
845}
846
847int encode_platform_event_message_req(
848 uint8_t instance_id, uint8_t format_version, uint8_t tid,
849 uint8_t event_class, const uint8_t *event_data, size_t event_data_length,
850 struct pldm_msg *msg, size_t payload_length)
851
852{
853 if (format_version != 1) {
854 return PLDM_ERROR_INVALID_DATA;
855 }
856
857 if (msg == NULL || event_data == NULL) {
858 return PLDM_ERROR_INVALID_DATA;
859 }
860
861 if (event_data_length == 0) {
862 return PLDM_ERROR_INVALID_DATA;
863 }
864
865 if (payload_length !=
866 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + event_data_length) {
867 return PLDM_ERROR_INVALID_LENGTH;
868 }
869
870 if (event_class > PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT &&
871 !(event_class >= 0xF0 && event_class <= 0xFE)) {
872 return PLDM_ERROR_INVALID_DATA;
873 }
874
875 struct pldm_header_info header = {0};
876 header.msg_type = PLDM_REQUEST;
877 header.instance = instance_id;
878 header.pldm_type = PLDM_PLATFORM;
879 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
880
881 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
882 if (rc != PLDM_SUCCESS) {
883 return rc;
884 }
885
886 struct pldm_platform_event_message_req *request =
887 (struct pldm_platform_event_message_req *)msg->payload;
888 request->format_version = format_version;
889 request->tid = tid;
890 request->event_class = event_class;
891 memcpy(request->event_data, event_data, event_data_length);
892
893 return PLDM_SUCCESS;
894}
895
896int decode_platform_event_message_resp(const struct pldm_msg *msg,
897 size_t payload_length,
898 uint8_t *completion_code,
899 uint8_t *platform_event_status)
900{
901 if (msg == NULL || completion_code == NULL ||
902 platform_event_status == NULL) {
903 return PLDM_ERROR_INVALID_DATA;
904 }
905
906 *completion_code = msg->payload[0];
907 if (PLDM_SUCCESS != *completion_code) {
908 return PLDM_SUCCESS;
909 }
910 if (payload_length != PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES) {
911 return PLDM_ERROR_INVALID_LENGTH;
912 }
913
914 struct pldm_platform_event_message_resp *response =
915 (struct pldm_platform_event_message_resp *)msg->payload;
916 *platform_event_status = response->platform_event_status;
917
918 if (*platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
919 return PLDM_ERROR_INVALID_DATA;
920 }
921
922 return PLDM_SUCCESS;
923}
924
925int decode_sensor_event_data(const uint8_t *event_data,
926 size_t event_data_length, uint16_t *sensor_id,
927 uint8_t *sensor_event_class_type,
928 size_t *event_class_data_offset)
929{
930 if (event_data == NULL) {
931 return PLDM_ERROR_INVALID_DATA;
932 }
933 if (event_data_length < PLDM_SENSOR_EVENT_DATA_MIN_LENGTH) {
934 return PLDM_ERROR_INVALID_LENGTH;
935 }
936
937 size_t event_class_data_length =
938 event_data_length - PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
939
940 struct pldm_sensor_event_data *sensor_event_data =
941 (struct pldm_sensor_event_data *)event_data;
942 *sensor_id = sensor_event_data->sensor_id;
943 *sensor_event_class_type = sensor_event_data->sensor_event_class_type;
944 if (sensor_event_data->sensor_event_class_type ==
945 PLDM_SENSOR_OP_STATE) {
946 if (event_class_data_length !=
947 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
948 return PLDM_ERROR_INVALID_LENGTH;
949 }
950 } else if (sensor_event_data->sensor_event_class_type ==
951 PLDM_STATE_SENSOR_STATE) {
952 if (event_class_data_length !=
953 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
954 return PLDM_ERROR_INVALID_LENGTH;
955 }
956 } else if (sensor_event_data->sensor_event_class_type ==
957 PLDM_NUMERIC_SENSOR_STATE) {
958 if (event_class_data_length <
959 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
960 event_class_data_length >
961 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
962 return PLDM_ERROR_INVALID_LENGTH;
963 }
964 } else {
965 return PLDM_ERROR_INVALID_DATA;
966 }
967 *event_class_data_offset =
968 sizeof(*sensor_id) + sizeof(*sensor_event_class_type);
969 return PLDM_SUCCESS;
970}
971
972int decode_sensor_op_data(const uint8_t *sensor_data, size_t sensor_data_length,
973 uint8_t *present_op_state, uint8_t *previous_op_state)
974{
975 if (sensor_data == NULL || present_op_state == NULL ||
976 previous_op_state == NULL) {
977 return PLDM_ERROR_INVALID_DATA;
978 }
979 if (sensor_data_length !=
980 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
981 return PLDM_ERROR_INVALID_LENGTH;
982 }
983
984 struct pldm_sensor_event_sensor_op_state *sensor_op_data =
985 (struct pldm_sensor_event_sensor_op_state *)sensor_data;
986 *present_op_state = sensor_op_data->present_op_state;
987 *previous_op_state = sensor_op_data->previous_op_state;
988 return PLDM_SUCCESS;
989}
990
991int decode_state_sensor_data(const uint8_t *sensor_data,
992 size_t sensor_data_length, uint8_t *sensor_offset,
993 uint8_t *event_state,
994 uint8_t *previous_event_state)
995{
996 if (sensor_data == NULL || sensor_offset == NULL ||
997 event_state == NULL || previous_event_state == NULL) {
998 return PLDM_ERROR_INVALID_DATA;
999 }
1000 if (sensor_data_length !=
1001 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
1002 return PLDM_ERROR_INVALID_LENGTH;
1003 }
1004
1005 struct pldm_sensor_event_state_sensor_state *sensor_state_data =
1006 (struct pldm_sensor_event_state_sensor_state *)sensor_data;
1007 *sensor_offset = sensor_state_data->sensor_offset;
1008 *event_state = sensor_state_data->event_state;
1009 *previous_event_state = sensor_state_data->previous_event_state;
1010 return PLDM_SUCCESS;
1011}
1012
1013int decode_numeric_sensor_data(const uint8_t *sensor_data,
1014 size_t sensor_data_length, uint8_t *event_state,
1015 uint8_t *previous_event_state,
1016 uint8_t *sensor_data_size,
1017 uint32_t *present_reading)
1018{
1019 if (sensor_data == NULL || sensor_data_size == NULL ||
1020 event_state == NULL || previous_event_state == NULL ||
1021 present_reading == NULL) {
1022 return PLDM_ERROR_INVALID_DATA;
1023 }
1024 if (sensor_data_length <
1025 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
1026 sensor_data_length >
1027 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
1028 return PLDM_ERROR_INVALID_LENGTH;
1029 }
1030 struct pldm_sensor_event_numeric_sensor_state *numeric_sensor_data =
1031 (struct pldm_sensor_event_numeric_sensor_state *)sensor_data;
1032 *event_state = numeric_sensor_data->event_state;
1033 *previous_event_state = numeric_sensor_data->previous_event_state;
1034 *sensor_data_size = numeric_sensor_data->sensor_data_size;
1035 uint8_t *present_reading_ptr = numeric_sensor_data->present_reading;
1036
1037 switch (*sensor_data_size) {
1038 case PLDM_SENSOR_DATA_SIZE_UINT8:
1039 case PLDM_SENSOR_DATA_SIZE_SINT8:
1040 if (sensor_data_length !=
1041 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_8BIT_DATA_LENGTH) {
1042 return PLDM_ERROR_INVALID_LENGTH;
1043 }
1044 *present_reading = present_reading_ptr[0];
1045 break;
1046 case PLDM_SENSOR_DATA_SIZE_UINT16:
1047 case PLDM_SENSOR_DATA_SIZE_SINT16:
1048 if (sensor_data_length !=
1049 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH) {
1050 return PLDM_ERROR_INVALID_LENGTH;
1051 }
1052 *present_reading = le16toh(present_reading_ptr[1] |
1053 (present_reading_ptr[0] << 8));
1054 break;
1055 case PLDM_SENSOR_DATA_SIZE_UINT32:
1056 case PLDM_SENSOR_DATA_SIZE_SINT32:
1057 if (sensor_data_length !=
1058 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH) {
1059 return PLDM_ERROR_INVALID_LENGTH;
1060 }
1061 *present_reading = le32toh(present_reading_ptr[3] |
1062 (present_reading_ptr[2] << 8) |
1063 (present_reading_ptr[1] << 16) |
1064 (present_reading_ptr[0] << 24));
1065 break;
1066 default:
1067 return PLDM_ERROR_INVALID_DATA;
1068 }
1069 return PLDM_SUCCESS;
1070}
1071
1072int encode_get_numeric_effecter_value_req(uint8_t instance_id,
1073 uint16_t effecter_id,
1074 struct pldm_msg *msg)
1075{
1076 if (msg == NULL) {
1077 return PLDM_ERROR_INVALID_DATA;
1078 }
1079
1080 struct pldm_header_info header = {0};
1081 header.msg_type = PLDM_REQUEST;
1082 header.instance = instance_id;
1083 header.pldm_type = PLDM_PLATFORM;
1084 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1085
1086 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1087 if (rc != PLDM_SUCCESS) {
1088 return rc;
1089 }
1090
1091 struct pldm_get_numeric_effecter_value_req *request =
1092 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
1093 request->effecter_id = htole16(effecter_id);
1094
1095 return PLDM_SUCCESS;
1096}
1097
1098int encode_get_numeric_effecter_value_resp(
1099 uint8_t instance_id, uint8_t completion_code, uint8_t effecter_data_size,
1100 uint8_t effecter_oper_state, uint8_t *pending_value, uint8_t *present_value,
1101 struct pldm_msg *msg, size_t payload_length)
1102{
1103 if (msg == NULL || pending_value == NULL || present_value == NULL) {
1104 return PLDM_ERROR_INVALID_DATA;
1105 }
1106
1107 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1108 return PLDM_ERROR_INVALID_DATA;
1109 }
1110
1111 if (effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1112 return PLDM_ERROR_INVALID_DATA;
1113 }
1114
1115 struct pldm_header_info header = {0};
1116 header.msg_type = PLDM_RESPONSE;
1117 header.instance = instance_id;
1118 header.pldm_type = PLDM_PLATFORM;
1119 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1120
1121 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1122 if (rc != PLDM_SUCCESS) {
1123 return rc;
1124 }
1125
1126 struct pldm_get_numeric_effecter_value_resp *response =
1127 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
1128
1129 response->completion_code = completion_code;
1130 response->effecter_data_size = effecter_data_size;
1131 response->effecter_oper_state = effecter_oper_state;
1132
1133 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1134 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1135 if (payload_length !=
1136 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1137 return PLDM_ERROR_INVALID_LENGTH;
1138 }
1139 response->pending_and_present_values[0] = *pending_value;
1140 response->pending_and_present_values[1] = *present_value;
1141
1142 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1143 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1144 if (payload_length !=
1145 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1146 return PLDM_ERROR_INVALID_LENGTH;
1147 }
1148 uint16_t val_pending = *(uint16_t *)pending_value;
1149 val_pending = htole16(val_pending);
1150 memcpy(response->pending_and_present_values, &val_pending,
1151 sizeof(uint16_t));
1152 uint16_t val_present = *(uint16_t *)present_value;
1153 val_present = htole16(val_present);
1154 memcpy(
1155 (response->pending_and_present_values + sizeof(uint16_t)),
1156 &val_present, sizeof(uint16_t));
1157
1158 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1159 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1160 if (payload_length !=
1161 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1162 return PLDM_ERROR_INVALID_LENGTH;
1163 }
1164 uint32_t val_pending = *(uint32_t *)pending_value;
1165 val_pending = htole32(val_pending);
1166 memcpy(response->pending_and_present_values, &val_pending,
1167 sizeof(uint32_t));
1168 uint32_t val_present = *(uint32_t *)present_value;
1169 val_present = htole32(val_present);
1170 memcpy(
1171 (response->pending_and_present_values + sizeof(uint32_t)),
1172 &val_present, sizeof(uint32_t));
1173 }
1174 return PLDM_SUCCESS;
1175}
1176
1177int decode_get_numeric_effecter_value_req(const struct pldm_msg *msg,
1178 size_t payload_length,
1179 uint16_t *effecter_id)
1180{
1181 if (msg == NULL || effecter_id == NULL) {
1182 return PLDM_ERROR_INVALID_DATA;
1183 }
1184
1185 if (payload_length != PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES) {
1186 return PLDM_ERROR_INVALID_LENGTH;
1187 }
1188
1189 struct pldm_get_numeric_effecter_value_req *request =
1190 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
1191
1192 *effecter_id = le16toh(request->effecter_id);
1193
1194 return PLDM_SUCCESS;
1195}
1196
1197int decode_get_numeric_effecter_value_resp(
1198 const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
1199 uint8_t *effecter_data_size, uint8_t *effecter_oper_state,
1200 uint8_t *pending_value, uint8_t *present_value)
1201{
1202 if (msg == NULL || effecter_data_size == NULL ||
1203 effecter_oper_state == NULL || pending_value == NULL ||
1204 present_value == NULL) {
1205 return PLDM_ERROR_INVALID_DATA;
1206 }
1207
1208 *completion_code = msg->payload[0];
1209 if (PLDM_SUCCESS != *completion_code) {
1210 return PLDM_SUCCESS;
1211 }
1212
1213 if (payload_length < PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1214 return PLDM_ERROR_INVALID_LENGTH;
1215 }
1216
1217 struct pldm_get_numeric_effecter_value_resp *response =
1218 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
1219
1220 *effecter_data_size = response->effecter_data_size;
1221 *effecter_oper_state = response->effecter_oper_state;
1222
1223 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1224 return PLDM_ERROR_INVALID_DATA;
1225 }
1226
1227 if (*effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1228 return PLDM_ERROR_INVALID_DATA;
1229 }
1230
1231 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1232 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1233 if (payload_length !=
1234 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1235 return PLDM_ERROR_INVALID_LENGTH;
1236 }
1237 memcpy(pending_value, response->pending_and_present_values, 1);
1238 memcpy(present_value, &response->pending_and_present_values[1],
1239 1);
1240
1241 } else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1242 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1243 if (payload_length !=
1244 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1245 return PLDM_ERROR_INVALID_LENGTH;
1246 }
1247 memcpy(pending_value, response->pending_and_present_values,
1248 sizeof(uint16_t));
1249 uint16_t *val_pending = (uint16_t *)pending_value;
1250 *val_pending = le16toh(*val_pending);
1251 memcpy(
1252 present_value,
1253 (response->pending_and_present_values + sizeof(uint16_t)),
1254 sizeof(uint16_t));
1255 uint16_t *val_present = (uint16_t *)present_value;
1256 *val_present = le16toh(*val_present);
1257
1258 } else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1259 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1260 if (payload_length !=
1261 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1262 return PLDM_ERROR_INVALID_LENGTH;
1263 }
1264 memcpy(pending_value, response->pending_and_present_values,
1265 sizeof(uint32_t));
1266 uint32_t *val_pending = (uint32_t *)pending_value;
1267 *val_pending = le32toh(*val_pending);
1268 memcpy(
1269 present_value,
1270 (response->pending_and_present_values + sizeof(uint32_t)),
1271 sizeof(uint32_t));
1272 uint32_t *val_present = (uint32_t *)present_value;
1273 *val_present = le32toh(*val_present);
1274 }
1275 return PLDM_SUCCESS;
1276}
1277
1278int encode_pldm_pdr_repository_chg_event_data(
1279 uint8_t event_data_format, uint8_t number_of_change_records,
1280 const uint8_t *event_data_operations,
1281 const uint8_t *numbers_of_change_entries,
1282 const uint32_t *const *change_entries,
1283 struct pldm_pdr_repository_chg_event_data *event_data,
1284 size_t *actual_change_records_size, size_t max_change_records_size)
1285{
1286 if (event_data_operations == NULL ||
1287 numbers_of_change_entries == NULL || change_entries == NULL) {
1288 return PLDM_ERROR_INVALID_DATA;
1289 }
1290
1291 size_t expected_size =
1292 sizeof(event_data_format) + sizeof(number_of_change_records);
1293
1294 expected_size +=
1295 sizeof(*event_data_operations) * number_of_change_records;
1296 expected_size +=
1297 sizeof(*numbers_of_change_entries) * number_of_change_records;
1298
1299 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1300 expected_size +=
1301 sizeof(*change_entries[0]) * numbers_of_change_entries[i];
1302 }
1303
1304 *actual_change_records_size = expected_size;
1305
1306 if (event_data == NULL) {
1307 return PLDM_SUCCESS;
1308 }
1309
1310 if (max_change_records_size < expected_size) {
1311 return PLDM_ERROR_INVALID_LENGTH;
1312 }
1313
1314 event_data->event_data_format = event_data_format;
1315 event_data->number_of_change_records = number_of_change_records;
1316
1317 struct pldm_pdr_repository_change_record_data *record_data =
1318 (struct pldm_pdr_repository_change_record_data *)
1319 event_data->change_records;
1320
1321 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1322 record_data->event_data_operation = event_data_operations[i];
1323 record_data->number_of_change_entries =
1324 numbers_of_change_entries[i];
1325
1326 for (uint8_t j = 0; j < record_data->number_of_change_entries;
1327 ++j) {
1328 record_data->change_entry[j] =
1329 htole32(change_entries[i][j]);
1330 }
1331
1332 record_data = (struct pldm_pdr_repository_change_record_data
1333 *)(record_data->change_entry +
1334 record_data->number_of_change_entries);
1335 }
1336
1337 return PLDM_SUCCESS;
1338}
1339
1340int decode_pldm_pdr_repository_chg_event_data(const uint8_t *event_data,
1341 size_t event_data_size,
1342 uint8_t *event_data_format,
1343 uint8_t *number_of_change_records,
1344 size_t *change_record_data_offset)
1345{
1346 if (event_data == NULL || event_data_format == NULL ||
1347 number_of_change_records == NULL ||
1348 change_record_data_offset == NULL) {
1349 return PLDM_ERROR_INVALID_DATA;
1350 }
1351 if (event_data_size < PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH) {
1352 return PLDM_ERROR_INVALID_LENGTH;
1353 }
1354
1355 struct pldm_pdr_repository_chg_event_data
1356 *pdr_repository_chg_event_data =
1357 (struct pldm_pdr_repository_chg_event_data *)event_data;
1358
1359 *event_data_format = pdr_repository_chg_event_data->event_data_format;
1360 *number_of_change_records =
1361 pdr_repository_chg_event_data->number_of_change_records;
1362 *change_record_data_offset =
1363 sizeof(*event_data_format) + sizeof(*number_of_change_records);
1364
1365 return PLDM_SUCCESS;
1366}
1367
1368int decode_pldm_pdr_repository_change_record_data(
1369 const uint8_t *change_record_data, size_t change_record_data_size,
1370 uint8_t *event_data_operation, uint8_t *number_of_change_entries,
1371 size_t *change_entry_data_offset)
1372{
1373 if (change_record_data == NULL || event_data_operation == NULL ||
1374 number_of_change_entries == NULL ||
1375 change_entry_data_offset == NULL) {
1376 return PLDM_ERROR_INVALID_DATA;
1377 }
1378 if (change_record_data_size <
1379 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH) {
1380 return PLDM_ERROR_INVALID_LENGTH;
1381 }
1382
1383 struct pldm_pdr_repository_change_record_data
1384 *pdr_repository_change_record_data =
1385 (struct pldm_pdr_repository_change_record_data *)
1386 change_record_data;
1387
1388 *event_data_operation =
1389 pdr_repository_change_record_data->event_data_operation;
1390 *number_of_change_entries =
1391 pdr_repository_change_record_data->number_of_change_entries;
1392 *change_entry_data_offset =
1393 sizeof(*event_data_operation) + sizeof(*number_of_change_entries);
1394
1395 return PLDM_SUCCESS;
1396}
1397
1398int encode_get_sensor_reading_req(uint8_t instance_id, uint16_t sensor_id,
1399 uint8_t rearm_event_state,
1400 struct pldm_msg *msg)
1401{
1402 if (msg == NULL) {
1403 return PLDM_ERROR_INVALID_DATA;
1404 }
1405
1406 struct pldm_header_info header = {0};
1407 header.msg_type = PLDM_REQUEST;
1408 header.instance = instance_id;
1409 header.pldm_type = PLDM_PLATFORM;
1410 header.command = PLDM_GET_SENSOR_READING;
1411
1412 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1413 if (rc != PLDM_SUCCESS) {
1414 return rc;
1415 }
1416
1417 struct pldm_get_sensor_reading_req *request =
1418 (struct pldm_get_sensor_reading_req *)msg->payload;
1419
1420 request->sensor_id = htole16(sensor_id);
1421 request->rearm_event_state = rearm_event_state;
1422
1423 return PLDM_SUCCESS;
1424}
1425
1426int decode_get_sensor_reading_resp(
1427 const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
1428 uint8_t *sensor_data_size, uint8_t *sensor_operational_state,
1429 uint8_t *sensor_event_message_enable, uint8_t *present_state,
1430 uint8_t *previous_state, uint8_t *event_state, uint8_t *present_reading)
1431{
1432 if (msg == NULL || completion_code == NULL ||
1433 sensor_data_size == NULL || sensor_operational_state == NULL ||
1434 sensor_event_message_enable == NULL || present_state == NULL ||
1435 previous_state == NULL || event_state == NULL ||
1436 present_reading == NULL) {
1437 return PLDM_ERROR_INVALID_DATA;
1438 }
1439
1440 *completion_code = msg->payload[0];
1441 if (PLDM_SUCCESS != *completion_code) {
1442 return PLDM_SUCCESS;
1443 }
1444
1445 if (payload_length < PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1446 return PLDM_ERROR_INVALID_LENGTH;
1447 }
1448
1449 struct pldm_get_sensor_reading_resp *response =
1450 (struct pldm_get_sensor_reading_resp *)msg->payload;
1451
1452 if (response->sensor_data_size > PLDM_SENSOR_DATA_SIZE_SINT32) {
1453 return PLDM_ERROR_INVALID_DATA;
1454 }
1455
1456 *sensor_data_size = response->sensor_data_size;
1457 *sensor_operational_state = response->sensor_operational_state;
1458 *sensor_event_message_enable = response->sensor_event_message_enable;
1459 *present_state = response->present_state;
1460 *previous_state = response->previous_state;
1461 *event_state = response->event_state;
1462
1463 if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1464 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1465 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1466 return PLDM_ERROR_INVALID_LENGTH;
1467 }
1468 *present_reading = response->present_reading[0];
1469
1470 } else if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1471 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1472 if (payload_length !=
1473 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
1474 return PLDM_ERROR_INVALID_LENGTH;
1475 }
1476 memcpy(present_reading, response->present_reading, 2);
1477 uint16_t *val = (uint16_t *)(present_reading);
1478 *val = le16toh(*val);
1479
1480 } else if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1481 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1482 if (payload_length !=
1483 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
1484 return PLDM_ERROR_INVALID_LENGTH;
1485 }
1486 memcpy(present_reading, response->present_reading, 4);
1487 uint32_t *val = (uint32_t *)(present_reading);
1488 *val = le32toh(*val);
1489 }
1490
1491 return PLDM_SUCCESS;
1492}
1493
1494int encode_get_sensor_reading_resp(
1495 uint8_t instance_id, uint8_t completion_code, uint8_t sensor_data_size,
1496 uint8_t sensor_operational_state, uint8_t sensor_event_message_enable,
1497 uint8_t present_state, uint8_t previous_state, uint8_t event_state,
1498 uint8_t *present_reading, struct pldm_msg *msg, size_t payload_length)
1499{
1500 if (msg == NULL || present_reading == NULL) {
1501 return PLDM_ERROR_INVALID_DATA;
1502 }
1503
1504 if (sensor_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1505 return PLDM_ERROR_INVALID_DATA;
1506 }
1507
1508 struct pldm_header_info header = {0};
1509 header.msg_type = PLDM_RESPONSE;
1510 header.instance = instance_id;
1511 header.pldm_type = PLDM_PLATFORM;
1512 header.command = PLDM_GET_SENSOR_READING;
1513
1514 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1515 if (rc != PLDM_SUCCESS) {
1516 return rc;
1517 }
1518
1519 struct pldm_get_sensor_reading_resp *response =
1520 (struct pldm_get_sensor_reading_resp *)msg->payload;
1521
1522 response->completion_code = completion_code;
1523 response->sensor_data_size = sensor_data_size;
1524 response->sensor_operational_state = sensor_operational_state;
1525 response->sensor_event_message_enable = sensor_event_message_enable;
1526 response->present_state = present_state;
1527 response->previous_state = previous_state;
1528 response->event_state = event_state;
1529
1530 if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1531 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1532 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1533 return PLDM_ERROR_INVALID_LENGTH;
1534 }
1535 response->present_reading[0] = *present_reading;
1536
1537 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1538 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1539 if (payload_length !=
1540 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
1541 return PLDM_ERROR_INVALID_LENGTH;
1542 }
1543 uint16_t val = *(uint16_t *)present_reading;
1544 val = htole16(val);
1545 memcpy(response->present_reading, &val, 2);
1546
1547 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1548 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1549 if (payload_length !=
1550 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
1551 return PLDM_ERROR_INVALID_LENGTH;
1552 }
1553 uint32_t val = *(uint32_t *)present_reading;
1554 val = htole32(val);
1555 memcpy(response->present_reading, &val, 4);
1556 }
1557
1558 return PLDM_SUCCESS;
1559}
1560
1561int decode_get_sensor_reading_req(const struct pldm_msg *msg,
1562 size_t payload_length, uint16_t *sensor_id,
1563 uint8_t *rearm_event_state)
1564{
1565 if (msg == NULL || sensor_id == NULL || rearm_event_state == NULL) {
1566 return PLDM_ERROR_INVALID_DATA;
1567 }
1568
1569 if (payload_length != PLDM_GET_SENSOR_READING_REQ_BYTES) {
1570 return PLDM_ERROR_INVALID_LENGTH;
1571 }
1572
1573 struct pldm_get_sensor_reading_req *request =
1574 (struct pldm_get_sensor_reading_req *)msg->payload;
1575
1576 *sensor_id = le16toh(request->sensor_id);
1577 *rearm_event_state = request->rearm_event_state;
1578
1579 return PLDM_SUCCESS;
1580}
1581
1582int encode_set_event_receiver_req(uint8_t instance_id,
1583 uint8_t event_message_global_enable,
1584 uint8_t transport_protocol_type,
1585 uint8_t event_receiver_address_info,
1586 uint16_t heartbeat_timer,
1587 struct pldm_msg *msg)
1588{
1589 if (msg == NULL) {
1590 return PLDM_ERROR_INVALID_DATA;
1591 }
1592
1593 if (transport_protocol_type != PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP) {
1594 return PLDM_ERROR_INVALID_DATA;
1595 }
1596
1597 struct pldm_header_info header = {0};
1598 header.msg_type = PLDM_REQUEST;
1599 header.instance = instance_id;
1600 header.pldm_type = PLDM_PLATFORM;
1601 header.command = PLDM_SET_EVENT_RECEIVER;
1602
1603 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1604 if (rc != PLDM_SUCCESS) {
1605 return rc;
1606 }
1607
1608 struct pldm_set_event_receiver_req *request =
1609 (struct pldm_set_event_receiver_req *)msg->payload;
1610 request->event_message_global_enable = event_message_global_enable;
1611
1612 request->transport_protocol_type = transport_protocol_type;
1613 request->event_receiver_address_info = event_receiver_address_info;
1614
1615 if (event_message_global_enable ==
1616 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) {
1617 if (heartbeat_timer == 0) {
1618 return PLDM_ERROR_INVALID_DATA;
1619 }
1620 request->heartbeat_timer = htole16(heartbeat_timer);
1621 }
1622
1623 return PLDM_SUCCESS;
1624}
1625
1626int decode_set_event_receiver_resp(const struct pldm_msg *msg,
1627 size_t payload_length,
1628 uint8_t *completion_code)
1629{
1630 if (msg == NULL || completion_code == NULL) {
1631 return PLDM_ERROR_INVALID_DATA;
1632 }
1633
1634 *completion_code = msg->payload[0];
1635 if (PLDM_SUCCESS != *completion_code) {
1636 return PLDM_SUCCESS;
1637 }
1638
1639 if (payload_length > PLDM_SET_EVENT_RECEIVER_RESP_BYTES) {
1640 return PLDM_ERROR_INVALID_LENGTH;
1641 }
1642
1643 return PLDM_SUCCESS;
1644}
1645
1646int decode_set_event_receiver_req(const struct pldm_msg *msg,
1647 size_t payload_length,
1648 uint8_t *event_message_global_enable,
1649 uint8_t *transport_protocol_type,
1650 uint8_t *event_receiver_address_info,
1651 uint16_t *heartbeat_timer)
1652
1653{
1654 if (msg == NULL || event_message_global_enable == NULL ||
1655 transport_protocol_type == NULL ||
1656 event_receiver_address_info == NULL || heartbeat_timer == NULL) {
1657 return PLDM_ERROR_INVALID_DATA;
1658 }
1659
1660 if (payload_length != PLDM_SET_EVENT_RECEIVER_REQ_BYTES) {
1661 return PLDM_ERROR_INVALID_LENGTH;
1662 }
1663
1664 struct pldm_set_event_receiver_req *request =
1665 (struct pldm_set_event_receiver_req *)msg->payload;
1666
1667 if ((*event_message_global_enable ==
1668 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) &&
1669 (*heartbeat_timer == 0)) {
1670 return PLDM_ERROR_INVALID_DATA;
1671 }
1672
1673 *event_message_global_enable = request->event_message_global_enable,
1674 *transport_protocol_type = request->transport_protocol_type,
1675 *event_receiver_address_info = request->event_receiver_address_info,
1676 *heartbeat_timer = le16toh(request->heartbeat_timer);
1677
1678 return PLDM_SUCCESS;
1679}
1680
1681int encode_set_event_receiver_resp(uint8_t instance_id, uint8_t completion_code,
1682 struct pldm_msg *msg)
1683
1684{
1685 if (msg == NULL) {
1686 return PLDM_ERROR_INVALID_DATA;
1687 }
1688
1689 struct pldm_header_info header = {0};
1690 header.instance = instance_id;
1691 header.msg_type = PLDM_RESPONSE;
1692 header.pldm_type = PLDM_PLATFORM;
1693 header.command = PLDM_SET_EVENT_RECEIVER;
1694
1695 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1696 if (rc != PLDM_SUCCESS) {
1697 return rc;
1698 }
1699
1700 msg->payload[0] = completion_code;
1701
1702 return PLDM_SUCCESS;
1703}