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