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