blob: 48df1adba4d5b56d621e7f5301a16d85c870bdad [file] [log] [blame]
Sampa Misra0db1dfa2019-03-19 00:15:31 -05001#include <endian.h>
2#include <string.h>
3
4#include "platform.h"
5
6int encode_set_state_effecter_states_resp(uint8_t instance_id,
7 uint8_t completion_code,
8 struct pldm_msg *msg)
9{
10 struct pldm_header_info header = {0};
11 int rc = PLDM_SUCCESS;
12
vkaverapa6575b82019-04-03 05:33:52 -050013 msg->payload[0] = completion_code;
Sampa Misra0db1dfa2019-03-19 00:15:31 -050014
15 header.msg_type = PLDM_RESPONSE;
16 header.instance = instance_id;
17 header.pldm_type = PLDM_PLATFORM;
18 header.command = PLDM_SET_STATE_EFFECTER_STATES;
19
20 rc = pack_pldm_header(&header, &(msg->hdr));
21
22 return rc;
23}
24
vkaverap98a2c192019-04-03 05:33:52 -050025int encode_set_state_effecter_states_req(uint8_t instance_id,
26 uint16_t effecter_id,
27 uint8_t comp_effecter_count,
28 set_effecter_state_field *field,
29 struct pldm_msg *msg)
30{
31 struct pldm_header_info header = {0};
32 int rc = PLDM_SUCCESS;
33
34 header.msg_type = PLDM_REQUEST;
35 header.instance = instance_id;
36 header.pldm_type = PLDM_PLATFORM;
37 header.command = PLDM_SET_STATE_EFFECTER_STATES;
38
39 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
40 return rc;
41 }
42
43 if (comp_effecter_count < 0x1 || comp_effecter_count > 0x8) {
44 return PLDM_ERROR_INVALID_DATA;
45 }
46
Priyanga7257fdf2019-06-10 01:59:45 -050047 struct pldm_set_state_effecter_states_req *request =
48 (struct pldm_set_state_effecter_states_req *)msg->payload;
vkaverap98a2c192019-04-03 05:33:52 -050049 effecter_id = htole16(effecter_id);
Priyanga7257fdf2019-06-10 01:59:45 -050050 request->effecter_id = effecter_id;
51 request->comp_effecter_count = comp_effecter_count;
52 memcpy(request->field, field,
vkaverap98a2c192019-04-03 05:33:52 -050053 (sizeof(set_effecter_state_field) * comp_effecter_count));
54
55 return PLDM_SUCCESS;
56}
57
Zahed Hossain223a73d2019-07-04 12:46:18 -050058int decode_set_state_effecter_states_resp(const struct pldm_msg *msg,
vkaverapa6575b82019-04-03 05:33:52 -050059 size_t payload_length,
vkaverap98a2c192019-04-03 05:33:52 -050060 uint8_t *completion_code)
61{
62 if (msg == NULL || completion_code == NULL) {
63 return PLDM_ERROR_INVALID_DATA;
64 }
65
George Liu684a7162019-12-06 15:10:52 +080066 *completion_code = msg->payload[0];
67 if (PLDM_SUCCESS != *completion_code) {
68 return PLDM_SUCCESS;
69 }
70
vkaverapa6575b82019-04-03 05:33:52 -050071 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES) {
72 return PLDM_ERROR_INVALID_LENGTH;
73 }
74
vkaverap98a2c192019-04-03 05:33:52 -050075 return PLDM_SUCCESS;
76}
77
Zahed Hossain223a73d2019-07-04 12:46:18 -050078int decode_set_state_effecter_states_req(const struct pldm_msg *msg,
vkaverapa6575b82019-04-03 05:33:52 -050079 size_t payload_length,
Sampa Misra0db1dfa2019-03-19 00:15:31 -050080 uint16_t *effecter_id,
81 uint8_t *comp_effecter_count,
82 set_effecter_state_field *field)
83{
84 if (msg == NULL || effecter_id == NULL || comp_effecter_count == NULL ||
85 field == NULL) {
86 return PLDM_ERROR_INVALID_DATA;
87 }
vkaverapa6575b82019-04-03 05:33:52 -050088
89 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) {
90 return PLDM_ERROR_INVALID_LENGTH;
91 }
92
Priyanga7257fdf2019-06-10 01:59:45 -050093 struct pldm_set_state_effecter_states_req *request =
Zahed Hossain223a73d2019-07-04 12:46:18 -050094 (struct pldm_set_state_effecter_states_req *)msg->payload;
Priyanga7257fdf2019-06-10 01:59:45 -050095
96 *effecter_id = le16toh(request->effecter_id);
97 *comp_effecter_count = request->comp_effecter_count;
98 memcpy(field, request->field,
Sampa Misra0db1dfa2019-03-19 00:15:31 -050099 (sizeof(set_effecter_state_field) * (*comp_effecter_count)));
100
101 return PLDM_SUCCESS;
102}
Sampa Misra7fcfb662019-05-08 13:13:53 -0500103
104int decode_get_pdr_req(const struct pldm_msg *msg, size_t payload_length,
105 uint32_t *record_hndl, uint32_t *data_transfer_hndl,
106 uint8_t *transfer_op_flag, uint16_t *request_cnt,
107 uint16_t *record_chg_num)
108{
109 if (msg == NULL || record_hndl == NULL || data_transfer_hndl == NULL ||
110 transfer_op_flag == NULL || request_cnt == NULL ||
111 record_chg_num == NULL) {
112 return PLDM_ERROR_INVALID_DATA;
113 }
114 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
115 return PLDM_ERROR_INVALID_LENGTH;
116 }
117
118 struct pldm_get_pdr_req *request =
119 (struct pldm_get_pdr_req *)msg->payload;
120 *record_hndl = le32toh(request->record_handle);
121 *data_transfer_hndl = le32toh(request->data_transfer_handle);
122 *transfer_op_flag = request->transfer_op_flag;
123 *request_cnt = le16toh(request->request_count);
124 *record_chg_num = le16toh(request->record_change_number);
125
126 return PLDM_SUCCESS;
127}
128
129int encode_get_pdr_resp(uint8_t instance_id, uint8_t completion_code,
130 uint32_t next_record_hndl,
131 uint32_t next_data_transfer_hndl, uint8_t transfer_flag,
132 uint16_t resp_cnt, const uint8_t *record_data,
133 uint8_t transfer_crc, struct pldm_msg *msg)
134{
135 struct pldm_header_info header = {0};
136 int rc = PLDM_SUCCESS;
137
138 if (msg == NULL) {
139 return PLDM_ERROR_INVALID_DATA;
140 }
141 struct pldm_get_pdr_resp *response =
142 (struct pldm_get_pdr_resp *)msg->payload;
143
144 response->completion_code = completion_code;
145
146 header.msg_type = PLDM_RESPONSE;
147 header.instance = instance_id;
148 header.pldm_type = PLDM_PLATFORM;
149 header.command = PLDM_GET_PDR;
150 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
151 return rc;
152 }
153
154 if (response->completion_code == PLDM_SUCCESS) {
155 response->next_record_handle = htole32(next_record_hndl);
156 response->next_data_transfer_handle =
157 htole32(next_data_transfer_hndl);
158 response->transfer_flag = transfer_flag;
159 response->response_count = htole16(resp_cnt);
160 if (record_data != NULL && resp_cnt > 0) {
161 memcpy(response->record_data, record_data, resp_cnt);
162 }
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500163 if (transfer_flag == PLDM_END) {
164 uint8_t *dst = msg->payload;
165 dst +=
166 (sizeof(struct pldm_get_pdr_resp) - 1) + resp_cnt;
167 *dst = transfer_crc;
168 }
Sampa Misra7fcfb662019-05-08 13:13:53 -0500169 }
170
171 return PLDM_SUCCESS;
172}
George Liu820a9a52019-11-26 14:43:59 +0800173
174int encode_get_pdr_req(uint8_t instance_id, uint32_t record_hndl,
175 uint32_t data_transfer_hndl, uint8_t transfer_op_flag,
176 uint16_t request_cnt, uint16_t record_chg_num,
177 struct pldm_msg *msg, size_t payload_length)
178{
179 struct pldm_header_info header = {0};
180 int rc = PLDM_SUCCESS;
181
182 if (msg == NULL) {
183 return PLDM_ERROR_INVALID_DATA;
184 }
185 struct pldm_get_pdr_req *request =
186 (struct pldm_get_pdr_req *)msg->payload;
187
188 header.msg_type = PLDM_REQUEST;
189 header.instance = instance_id;
190 header.pldm_type = PLDM_PLATFORM;
191 header.command = PLDM_GET_PDR;
192
193 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
194 return rc;
195 }
196
197 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
198 return PLDM_ERROR_INVALID_LENGTH;
199 }
200
201 request->record_handle = htole32(record_hndl);
202 request->data_transfer_handle = htole32(data_transfer_hndl);
203 request->transfer_op_flag = transfer_op_flag;
204 request->request_count = htole16(request_cnt);
205 request->record_change_number = htole16(record_chg_num);
206
207 return PLDM_SUCCESS;
208}
209
210int decode_get_pdr_resp(const struct pldm_msg *msg, size_t payload_length,
211 uint8_t *completion_code, uint32_t *next_record_hndl,
212 uint32_t *next_data_transfer_hndl,
213 uint8_t *transfer_flag, uint16_t *resp_cnt,
214 uint8_t *record_data, size_t record_data_length,
215 uint8_t *transfer_crc)
216{
217 if (msg == NULL || completion_code == NULL ||
218 next_record_hndl == NULL || next_data_transfer_hndl == NULL ||
219 transfer_flag == NULL || resp_cnt == NULL || record_data == NULL ||
220 transfer_crc == NULL) {
221 return PLDM_ERROR_INVALID_DATA;
222 }
223
224 *completion_code = msg->payload[0];
225 if (PLDM_SUCCESS != *completion_code) {
George Liu684a7162019-12-06 15:10:52 +0800226 return PLDM_SUCCESS;
George Liu820a9a52019-11-26 14:43:59 +0800227 }
228
229 if (payload_length < PLDM_GET_PDR_MIN_RESP_BYTES) {
230 return PLDM_ERROR_INVALID_LENGTH;
231 }
232
233 struct pldm_get_pdr_resp *response =
234 (struct pldm_get_pdr_resp *)msg->payload;
235
236 *next_record_hndl = le32toh(response->next_record_handle);
237 *next_data_transfer_hndl = le32toh(response->next_data_transfer_handle);
238 *transfer_flag = response->transfer_flag;
239 *resp_cnt = le16toh(response->response_count);
240
241 if (*transfer_flag != PLDM_END &&
242 (int)payload_length != PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt) {
243 return PLDM_ERROR_INVALID_LENGTH;
244 }
245
246 if (*transfer_flag == PLDM_END &&
247 (int)payload_length !=
248 PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt + 1) {
249 return PLDM_ERROR_INVALID_LENGTH;
250 }
251
252 if (*resp_cnt > 0) {
253 if (record_data_length < *resp_cnt) {
254 return PLDM_ERROR_INVALID_LENGTH;
255 }
256 memcpy(record_data, response->record_data, *resp_cnt);
257 }
258
259 if (*transfer_flag == PLDM_END) {
260 *transfer_crc =
261 msg->payload[PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt];
262 }
263
264 return PLDM_SUCCESS;
265}
George Liu30b859f2020-01-07 15:03:22 +0800266
267int decode_set_numeric_effecter_value_req(const struct pldm_msg *msg,
268 size_t payload_length,
269 uint16_t *effecter_id,
270 uint8_t *effecter_data_size,
271 uint8_t *effecter_value)
272{
273 if (msg == NULL || effecter_id == NULL || effecter_data_size == NULL ||
274 effecter_value == NULL) {
275 return PLDM_ERROR_INVALID_DATA;
276 }
277
278 if (payload_length < PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
279 return PLDM_ERROR_INVALID_LENGTH;
280 }
281
282 struct pldm_set_numeric_effecter_value_req *request =
283 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
284 *effecter_id = le16toh(request->effecter_id);
285 *effecter_data_size = request->effecter_data_size;
286
287 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
288 return PLDM_ERROR_INVALID_DATA;
289 }
290
291 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
292 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
293
294 if (payload_length !=
295 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
296 return PLDM_ERROR_INVALID_LENGTH;
297 }
298
299 *effecter_value = request->effecter_value[0];
300 }
301
302 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
303 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
304
305 if (payload_length !=
306 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
307 return PLDM_ERROR_INVALID_LENGTH;
308 }
309
310 memcpy(effecter_value, request->effecter_value, 2);
311 *effecter_value = le16toh(*effecter_value);
312 }
313
314 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
315 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
316
317 if (payload_length !=
318 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
319 return PLDM_ERROR_INVALID_LENGTH;
320 }
321
322 memcpy(effecter_value, request->effecter_value, 4);
George Liu30b859f2020-01-07 15:03:22 +0800323 }
324
325 return PLDM_SUCCESS;
326}
327
328int encode_set_numeric_effecter_value_resp(uint8_t instance_id,
329 uint8_t completion_code,
330 struct pldm_msg *msg,
331 size_t payload_length)
332{
333 struct pldm_header_info header = {0};
334 int rc = PLDM_SUCCESS;
335
336 if (msg == NULL) {
337 return PLDM_ERROR_INVALID_DATA;
338 }
339
340 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
341 return PLDM_ERROR_INVALID_LENGTH;
342 }
343
344 msg->payload[0] = completion_code;
345
346 header.msg_type = PLDM_RESPONSE;
347 header.instance = instance_id;
348 header.pldm_type = PLDM_PLATFORM;
349 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
350
351 rc = pack_pldm_header(&header, &(msg->hdr));
352
353 return rc;
354}
355
356int encode_set_numeric_effecter_value_req(
357 uint8_t instance_id, uint16_t effecter_id, uint8_t effecter_data_size,
358 uint8_t *effecter_value, struct pldm_msg *msg, size_t payload_length)
359{
360 struct pldm_header_info header = {0};
361 int rc = PLDM_SUCCESS;
362
363 if (msg == NULL || effecter_value == NULL) {
364 return PLDM_ERROR_INVALID_DATA;
365 }
366 struct pldm_set_numeric_effecter_value_req *request =
367 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
368
369 header.msg_type = PLDM_REQUEST;
370 header.instance = instance_id;
371 header.pldm_type = PLDM_PLATFORM;
372 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
373
374 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
375 return rc;
376 }
377
378 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
379 return PLDM_ERROR_INVALID_DATA;
380 }
381
382 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
383 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
384 if (payload_length !=
385 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
386 return PLDM_ERROR_INVALID_LENGTH;
387 }
388 request->effecter_value[0] = *effecter_value;
389 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
390 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
391 if (payload_length !=
392 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
393 return PLDM_ERROR_INVALID_LENGTH;
394 }
395
396 memcpy(request->effecter_value, effecter_value, 2);
397 *request->effecter_value = htole16(*request->effecter_value);
398 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
399 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
400 if (payload_length !=
401 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
402 return PLDM_ERROR_INVALID_LENGTH;
403 }
404
405 memcpy(request->effecter_value, effecter_value, 4);
406 *request->effecter_value = htole32(*request->effecter_value);
407 }
408
409 request->effecter_id = htole16(effecter_id);
410 request->effecter_data_size = effecter_data_size;
411
412 return PLDM_SUCCESS;
413}
414
415int decode_set_numeric_effecter_value_resp(const struct pldm_msg *msg,
416 size_t payload_length,
417 uint8_t *completion_code)
418{
419 if (msg == NULL || completion_code == NULL) {
420 return PLDM_ERROR_INVALID_DATA;
421 }
422
423 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
424 return PLDM_ERROR_INVALID_LENGTH;
425 }
426
427 *completion_code = msg->payload[0];
428
429 return PLDM_SUCCESS;
430}
Jolie Ku3557bad2020-03-02 16:22:57 +0800431
432int encode_get_state_sensor_readings_resp(uint8_t instance_id,
433 uint8_t completion_code,
434 uint8_t comp_sensor_count,
435 get_sensor_state_field *field,
436 struct pldm_msg *msg)
437{
438 struct pldm_header_info header = {0};
439 int rc = PLDM_SUCCESS;
440
441 header.msg_type = PLDM_RESPONSE;
442 header.instance = instance_id;
443 header.pldm_type = PLDM_PLATFORM;
444 header.command = PLDM_GET_STATE_SENSOR_READINGS;
445
446 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
447 return rc;
448 }
449
450 if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
451 return PLDM_ERROR_INVALID_DATA;
452 }
453
454 struct pldm_get_state_sensor_readings_resp *response =
455 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
456
457 response->completion_code = completion_code;
458 response->comp_sensor_count = comp_sensor_count;
459 memcpy(response->field, field,
460 (sizeof(get_sensor_state_field) * comp_sensor_count));
461
462 return rc;
463}
464
465int encode_get_state_sensor_readings_req(uint8_t instance_id,
466 uint16_t sensor_id,
467 bitfield8_t sensor_rearm,
468 uint8_t reserved, struct pldm_msg *msg)
469{
470 struct pldm_header_info header = {0};
471 int rc = PLDM_SUCCESS;
472
473 header.msg_type = PLDM_REQUEST;
474 header.instance = instance_id;
475 header.pldm_type = PLDM_PLATFORM;
476 header.command = PLDM_GET_STATE_SENSOR_READINGS;
477
478 if (msg == NULL) {
479 return PLDM_ERROR_INVALID_DATA;
480 }
481
482 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
483 return rc;
484 }
485
486 struct pldm_get_state_sensor_readings_req *request =
487 (struct pldm_get_state_sensor_readings_req *)msg->payload;
488
489 request->sensor_id = htole16(sensor_id);
490 request->reserved = reserved;
491 request->sensor_rearm = sensor_rearm;
492
493 return PLDM_SUCCESS;
494}
495
496int decode_get_state_sensor_readings_resp(const struct pldm_msg *msg,
497 size_t payload_length,
498 uint8_t *completion_code,
499 uint8_t *comp_sensor_count,
500 get_sensor_state_field *field)
501{
502 if (msg == NULL || completion_code == NULL ||
503 comp_sensor_count == NULL || field == NULL) {
504 return PLDM_ERROR_INVALID_DATA;
505 }
506
507 *completion_code = msg->payload[0];
508 if (PLDM_SUCCESS != *completion_code) {
509 return PLDM_SUCCESS;
510 }
511
512 if (payload_length > PLDM_GET_STATE_SENSOR_READINGS_RESP_BYTES) {
513 return PLDM_ERROR_INVALID_LENGTH;
514 }
515
516 struct pldm_get_state_sensor_readings_resp *response =
517 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
518
519 if (response->comp_sensor_count < 0x1 ||
520 response->comp_sensor_count > 0x8) {
521 return PLDM_ERROR_INVALID_DATA;
522 }
523 if (response->comp_sensor_count > *comp_sensor_count) {
524 return PLDM_ERROR_INVALID_LENGTH;
525 }
526 *comp_sensor_count = response->comp_sensor_count;
527
528 memcpy(field, response->field,
529 (sizeof(get_sensor_state_field) * (*comp_sensor_count)));
530
531 return PLDM_SUCCESS;
532}
533
534int decode_get_state_sensor_readings_req(const struct pldm_msg *msg,
535 size_t payload_length,
536 uint16_t *sensor_id,
537 bitfield8_t *sensor_rearm,
538 uint8_t *reserved)
539{
540 if (msg == NULL || sensor_id == NULL || sensor_rearm == NULL) {
541 return PLDM_ERROR_INVALID_DATA;
542 }
543
544 if (payload_length != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES) {
545 return PLDM_ERROR_INVALID_LENGTH;
546 }
547
548 struct pldm_get_state_sensor_readings_req *request =
549 (struct pldm_get_state_sensor_readings_req *)msg->payload;
550
551 *sensor_id = le16toh(request->sensor_id);
552 *reserved = request->reserved;
553 memcpy(&(sensor_rearm->byte), &(request->sensor_rearm.byte),
554 sizeof(request->sensor_rearm.byte));
555
556 return PLDM_SUCCESS;
557}
Zahed Hossaind4abab12020-02-06 03:36:43 -0600558
559int decode_platform_event_message_req(const struct pldm_msg *msg,
560 size_t payload_length,
561 uint8_t *format_version, uint8_t *tid,
562 uint8_t *event_class,
563 size_t *event_data_offset)
564{
565
566 if (msg == NULL || format_version == NULL || tid == NULL ||
567 event_class == NULL || event_data_offset == NULL) {
568 return PLDM_ERROR_INVALID_DATA;
569 }
570
571 if (payload_length <= PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES) {
572 return PLDM_ERROR_INVALID_LENGTH;
573 }
574 struct pldm_platform_event_message_req *response =
575 (struct pldm_platform_event_message_req *)msg->payload;
576
577 *format_version = response->format_version;
578 *tid = response->tid;
579 *event_class = response->event_class;
580 *event_data_offset =
581 sizeof(*format_version) + sizeof(*tid) + sizeof(*event_class);
582
583 return PLDM_SUCCESS;
584}
585
586int encode_platform_event_message_resp(uint8_t instance_id,
587 uint8_t completion_code, uint8_t status,
588 struct pldm_msg *msg)
589{
590 int rc = PLDM_SUCCESS;
591
592 if (msg == NULL) {
593 return PLDM_ERROR_INVALID_DATA;
594 }
595
596 struct pldm_platform_event_message_resp *response =
597 (struct pldm_platform_event_message_resp *)msg->payload;
598 response->completion_code = completion_code;
599 response->status = status;
600
601 struct pldm_header_info header = {0};
602 header.msg_type = PLDM_RESPONSE;
603 header.instance = instance_id;
604 header.pldm_type = PLDM_PLATFORM;
605 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
606
607 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
608 return rc;
609 }
610 return PLDM_SUCCESS;
611}
Zahed Hossain1c861712020-03-04 08:55:19 -0600612
613int decode_sensor_event_data(const uint8_t *event_data,
614 size_t event_data_length, uint16_t *sensor_id,
615 uint8_t *sensor_event_class_type,
616 size_t *event_class_data_offset)
617{
618 if (event_data == NULL) {
619 return PLDM_ERROR_INVALID_DATA;
620 }
621 if (event_data_length < PLDM_SENSOR_EVENT_DATA_MIN_LENGTH) {
622 return PLDM_ERROR_INVALID_LENGTH;
623 }
624
625 size_t event_class_data_length =
626 event_data_length - PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
627
628 struct pldm_sensor_event_data *sensor_event_data =
629 (struct pldm_sensor_event_data *)event_data;
630 *sensor_id = sensor_event_data->sensor_id;
631 *sensor_event_class_type = sensor_event_data->sensor_event_class_type;
632 if (sensor_event_data->sensor_event_class_type ==
633 PLDM_SENSOR_OP_STATE) {
634 if (event_class_data_length !=
635 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
636 return PLDM_ERROR_INVALID_LENGTH;
637 }
638 } else if (sensor_event_data->sensor_event_class_type ==
639 PLDM_STATE_SENSOR_STATE) {
640 if (event_class_data_length !=
641 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
642 return PLDM_ERROR_INVALID_LENGTH;
643 }
644 } else if (sensor_event_data->sensor_event_class_type ==
645 PLDM_NUMERIC_SENSOR_STATE) {
646 if (event_class_data_length <
647 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
648 event_class_data_length >
649 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
650 return PLDM_ERROR_INVALID_LENGTH;
651 }
652 } else {
653 return PLDM_ERROR_INVALID_DATA;
654 }
655 *event_class_data_offset =
656 sizeof(*sensor_id) + sizeof(*sensor_event_class_type);
657 return PLDM_SUCCESS;
658}
659
660int decode_sensor_op_data(const uint8_t *sensor_data, size_t sensor_data_length,
661 uint8_t *present_op_state, uint8_t *previous_op_state)
662{
663 if (sensor_data == NULL || present_op_state == NULL ||
664 previous_op_state == NULL) {
665 return PLDM_ERROR_INVALID_DATA;
666 }
667 if (sensor_data_length !=
668 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
669 return PLDM_ERROR_INVALID_LENGTH;
670 }
671
672 struct pldm_sensor_event_sensor_op_state *sensor_op_data =
673 (struct pldm_sensor_event_sensor_op_state *)sensor_data;
674 *present_op_state = sensor_op_data->present_op_state;
675 *previous_op_state = sensor_op_data->previous_op_state;
676 return PLDM_SUCCESS;
677}
678
679int decode_state_sensor_data(const uint8_t *sensor_data,
680 size_t sensor_data_length, uint8_t *sensor_offset,
681 uint8_t *event_state,
682 uint8_t *previous_event_state)
683{
684 if (sensor_data == NULL || sensor_offset == NULL ||
685 event_state == NULL || previous_event_state == NULL) {
686 return PLDM_ERROR_INVALID_DATA;
687 }
688 if (sensor_data_length !=
689 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
690 return PLDM_ERROR_INVALID_LENGTH;
691 }
692
693 struct pldm_sensor_event_state_sensor_state *sensor_state_data =
694 (struct pldm_sensor_event_state_sensor_state *)sensor_data;
695 *sensor_offset = sensor_state_data->sensor_offset;
696 *event_state = sensor_state_data->event_state;
697 *previous_event_state = sensor_state_data->previous_event_state;
698 return PLDM_SUCCESS;
699}
700
701int decode_numeric_sensor_data(const uint8_t *sensor_data,
702 size_t sensor_data_length, uint8_t *event_state,
703 uint8_t *previous_event_state,
704 uint8_t *sensor_data_size,
705 uint32_t *present_reading)
706{
707 if (sensor_data == NULL || sensor_data_size == NULL ||
708 event_state == NULL || previous_event_state == NULL ||
709 present_reading == NULL) {
710 return PLDM_ERROR_INVALID_DATA;
711 }
712 if (sensor_data_length <
713 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
714 sensor_data_length >
715 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
716 return PLDM_ERROR_INVALID_LENGTH;
717 }
718 struct pldm_sensor_event_numeric_sensor_state *numeric_sensor_data =
719 (struct pldm_sensor_event_numeric_sensor_state *)sensor_data;
720 *event_state = numeric_sensor_data->event_state;
721 *previous_event_state = numeric_sensor_data->previous_event_state;
722 *sensor_data_size = numeric_sensor_data->sensor_data_size;
723 uint8_t *present_reading_ptr = numeric_sensor_data->present_reading;
724
725 switch (*sensor_data_size) {
726 case PLDM_SENSOR_DATA_SIZE_UINT8:
727 case PLDM_SENSOR_DATA_SIZE_SINT8:
728 if (sensor_data_length !=
729 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_8BIT_DATA_LENGTH) {
730 return PLDM_ERROR_INVALID_LENGTH;
731 }
732 *present_reading = present_reading_ptr[0];
733 break;
734 case PLDM_SENSOR_DATA_SIZE_UINT16:
735 case PLDM_SENSOR_DATA_SIZE_SINT16:
736 if (sensor_data_length !=
737 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH) {
738 return PLDM_ERROR_INVALID_LENGTH;
739 }
740 *present_reading = le16toh(present_reading_ptr[1] |
741 (present_reading_ptr[0] << 8));
742 break;
743 case PLDM_SENSOR_DATA_SIZE_UINT32:
744 case PLDM_SENSOR_DATA_SIZE_SINT32:
745 if (sensor_data_length !=
746 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH) {
747 return PLDM_ERROR_INVALID_LENGTH;
748 }
749 *present_reading = le32toh(present_reading_ptr[3] |
750 (present_reading_ptr[2] << 8) |
751 (present_reading_ptr[1] << 16) |
752 (present_reading_ptr[0] << 24));
753 break;
754 default:
755 return PLDM_ERROR_INVALID_DATA;
756 }
757 return PLDM_SUCCESS;
758}