blob: 9b0403f845e0da53eceb907c501e7c88fe06344e [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
Tom Joseph250c4752020-04-15 10:32:45 +053043 if (comp_effecter_count < 0x1 || comp_effecter_count > 0x8 ||
44 field == NULL) {
vkaverap98a2c192019-04-03 05:33:52 -050045 return PLDM_ERROR_INVALID_DATA;
46 }
47
Priyanga7257fdf2019-06-10 01:59:45 -050048 struct pldm_set_state_effecter_states_req *request =
49 (struct pldm_set_state_effecter_states_req *)msg->payload;
vkaverap98a2c192019-04-03 05:33:52 -050050 effecter_id = htole16(effecter_id);
Priyanga7257fdf2019-06-10 01:59:45 -050051 request->effecter_id = effecter_id;
52 request->comp_effecter_count = comp_effecter_count;
53 memcpy(request->field, field,
vkaverap98a2c192019-04-03 05:33:52 -050054 (sizeof(set_effecter_state_field) * comp_effecter_count));
55
56 return PLDM_SUCCESS;
57}
58
Zahed Hossain223a73d2019-07-04 12:46:18 -050059int decode_set_state_effecter_states_resp(const struct pldm_msg *msg,
vkaverapa6575b82019-04-03 05:33:52 -050060 size_t payload_length,
vkaverap98a2c192019-04-03 05:33:52 -050061 uint8_t *completion_code)
62{
63 if (msg == NULL || completion_code == NULL) {
64 return PLDM_ERROR_INVALID_DATA;
65 }
66
George Liu684a7162019-12-06 15:10:52 +080067 *completion_code = msg->payload[0];
68 if (PLDM_SUCCESS != *completion_code) {
69 return PLDM_SUCCESS;
70 }
71
vkaverapa6575b82019-04-03 05:33:52 -050072 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES) {
73 return PLDM_ERROR_INVALID_LENGTH;
74 }
75
vkaverap98a2c192019-04-03 05:33:52 -050076 return PLDM_SUCCESS;
77}
78
Zahed Hossain223a73d2019-07-04 12:46:18 -050079int decode_set_state_effecter_states_req(const struct pldm_msg *msg,
vkaverapa6575b82019-04-03 05:33:52 -050080 size_t payload_length,
Sampa Misra0db1dfa2019-03-19 00:15:31 -050081 uint16_t *effecter_id,
82 uint8_t *comp_effecter_count,
83 set_effecter_state_field *field)
84{
85 if (msg == NULL || effecter_id == NULL || comp_effecter_count == NULL ||
86 field == NULL) {
87 return PLDM_ERROR_INVALID_DATA;
88 }
vkaverapa6575b82019-04-03 05:33:52 -050089
90 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) {
91 return PLDM_ERROR_INVALID_LENGTH;
92 }
93
Priyanga7257fdf2019-06-10 01:59:45 -050094 struct pldm_set_state_effecter_states_req *request =
Zahed Hossain223a73d2019-07-04 12:46:18 -050095 (struct pldm_set_state_effecter_states_req *)msg->payload;
Priyanga7257fdf2019-06-10 01:59:45 -050096
97 *effecter_id = le16toh(request->effecter_id);
98 *comp_effecter_count = request->comp_effecter_count;
99 memcpy(field, request->field,
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500100 (sizeof(set_effecter_state_field) * (*comp_effecter_count)));
101
102 return PLDM_SUCCESS;
103}
Sampa Misra7fcfb662019-05-08 13:13:53 -0500104
105int decode_get_pdr_req(const struct pldm_msg *msg, size_t payload_length,
106 uint32_t *record_hndl, uint32_t *data_transfer_hndl,
107 uint8_t *transfer_op_flag, uint16_t *request_cnt,
108 uint16_t *record_chg_num)
109{
110 if (msg == NULL || record_hndl == NULL || data_transfer_hndl == NULL ||
111 transfer_op_flag == NULL || request_cnt == NULL ||
112 record_chg_num == NULL) {
113 return PLDM_ERROR_INVALID_DATA;
114 }
115 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
116 return PLDM_ERROR_INVALID_LENGTH;
117 }
118
119 struct pldm_get_pdr_req *request =
120 (struct pldm_get_pdr_req *)msg->payload;
121 *record_hndl = le32toh(request->record_handle);
122 *data_transfer_hndl = le32toh(request->data_transfer_handle);
123 *transfer_op_flag = request->transfer_op_flag;
124 *request_cnt = le16toh(request->request_count);
125 *record_chg_num = le16toh(request->record_change_number);
126
127 return PLDM_SUCCESS;
128}
129
130int encode_get_pdr_resp(uint8_t instance_id, uint8_t completion_code,
131 uint32_t next_record_hndl,
132 uint32_t next_data_transfer_hndl, uint8_t transfer_flag,
133 uint16_t resp_cnt, const uint8_t *record_data,
134 uint8_t transfer_crc, struct pldm_msg *msg)
135{
136 struct pldm_header_info header = {0};
137 int rc = PLDM_SUCCESS;
138
139 if (msg == NULL) {
140 return PLDM_ERROR_INVALID_DATA;
141 }
142 struct pldm_get_pdr_resp *response =
143 (struct pldm_get_pdr_resp *)msg->payload;
144
145 response->completion_code = completion_code;
146
147 header.msg_type = PLDM_RESPONSE;
148 header.instance = instance_id;
149 header.pldm_type = PLDM_PLATFORM;
150 header.command = PLDM_GET_PDR;
151 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
152 return rc;
153 }
154
155 if (response->completion_code == PLDM_SUCCESS) {
156 response->next_record_handle = htole32(next_record_hndl);
157 response->next_data_transfer_handle =
158 htole32(next_data_transfer_hndl);
159 response->transfer_flag = transfer_flag;
160 response->response_count = htole16(resp_cnt);
161 if (record_data != NULL && resp_cnt > 0) {
162 memcpy(response->record_data, record_data, resp_cnt);
163 }
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500164 if (transfer_flag == PLDM_END) {
165 uint8_t *dst = msg->payload;
166 dst +=
167 (sizeof(struct pldm_get_pdr_resp) - 1) + resp_cnt;
168 *dst = transfer_crc;
169 }
Sampa Misra7fcfb662019-05-08 13:13:53 -0500170 }
171
172 return PLDM_SUCCESS;
173}
George Liu820a9a52019-11-26 14:43:59 +0800174
175int encode_get_pdr_req(uint8_t instance_id, uint32_t record_hndl,
176 uint32_t data_transfer_hndl, uint8_t transfer_op_flag,
177 uint16_t request_cnt, uint16_t record_chg_num,
178 struct pldm_msg *msg, size_t payload_length)
179{
180 struct pldm_header_info header = {0};
181 int rc = PLDM_SUCCESS;
182
183 if (msg == NULL) {
184 return PLDM_ERROR_INVALID_DATA;
185 }
186 struct pldm_get_pdr_req *request =
187 (struct pldm_get_pdr_req *)msg->payload;
188
189 header.msg_type = PLDM_REQUEST;
190 header.instance = instance_id;
191 header.pldm_type = PLDM_PLATFORM;
192 header.command = PLDM_GET_PDR;
193
194 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
195 return rc;
196 }
197
198 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
199 return PLDM_ERROR_INVALID_LENGTH;
200 }
201
202 request->record_handle = htole32(record_hndl);
203 request->data_transfer_handle = htole32(data_transfer_hndl);
204 request->transfer_op_flag = transfer_op_flag;
205 request->request_count = htole16(request_cnt);
206 request->record_change_number = htole16(record_chg_num);
207
208 return PLDM_SUCCESS;
209}
210
211int decode_get_pdr_resp(const struct pldm_msg *msg, size_t payload_length,
212 uint8_t *completion_code, uint32_t *next_record_hndl,
213 uint32_t *next_data_transfer_hndl,
214 uint8_t *transfer_flag, uint16_t *resp_cnt,
215 uint8_t *record_data, size_t record_data_length,
216 uint8_t *transfer_crc)
217{
218 if (msg == NULL || completion_code == NULL ||
219 next_record_hndl == NULL || next_data_transfer_hndl == NULL ||
Zach Clark3dba2bf2020-03-31 10:58:03 -0500220 transfer_flag == NULL || resp_cnt == NULL || transfer_crc == NULL) {
George Liu820a9a52019-11-26 14:43:59 +0800221 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
Zach Clark3dba2bf2020-03-31 10:58:03 -0500252 if (*resp_cnt > 0 && record_data != NULL) {
George Liu820a9a52019-11-26 14:43:59 +0800253 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);
George Liuda75abe2020-04-09 13:36:13 +0800311 uint16_t *val = (uint16_t *)(effecter_value);
312 *val = le16toh(*val);
George Liu30b859f2020-01-07 15:03:22 +0800313 }
314
315 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
316 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
317
318 if (payload_length !=
319 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
320 return PLDM_ERROR_INVALID_LENGTH;
321 }
322
323 memcpy(effecter_value, request->effecter_value, 4);
George Liuda75abe2020-04-09 13:36:13 +0800324 uint32_t *val = (uint32_t *)(effecter_value);
325 *val = le32toh(*val);
George Liu30b859f2020-01-07 15:03:22 +0800326 }
327
328 return PLDM_SUCCESS;
329}
330
331int encode_set_numeric_effecter_value_resp(uint8_t instance_id,
332 uint8_t completion_code,
333 struct pldm_msg *msg,
334 size_t payload_length)
335{
336 struct pldm_header_info header = {0};
337 int rc = PLDM_SUCCESS;
338
339 if (msg == NULL) {
340 return PLDM_ERROR_INVALID_DATA;
341 }
342
343 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
344 return PLDM_ERROR_INVALID_LENGTH;
345 }
346
347 msg->payload[0] = completion_code;
348
349 header.msg_type = PLDM_RESPONSE;
350 header.instance = instance_id;
351 header.pldm_type = PLDM_PLATFORM;
352 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
353
354 rc = pack_pldm_header(&header, &(msg->hdr));
355
356 return rc;
357}
358
359int encode_set_numeric_effecter_value_req(
360 uint8_t instance_id, uint16_t effecter_id, uint8_t effecter_data_size,
361 uint8_t *effecter_value, struct pldm_msg *msg, size_t payload_length)
362{
363 struct pldm_header_info header = {0};
364 int rc = PLDM_SUCCESS;
365
366 if (msg == NULL || effecter_value == NULL) {
367 return PLDM_ERROR_INVALID_DATA;
368 }
369 struct pldm_set_numeric_effecter_value_req *request =
370 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
371
372 header.msg_type = PLDM_REQUEST;
373 header.instance = instance_id;
374 header.pldm_type = PLDM_PLATFORM;
375 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
376
377 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
378 return rc;
379 }
380
381 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
382 return PLDM_ERROR_INVALID_DATA;
383 }
384
385 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
386 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
387 if (payload_length !=
388 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
389 return PLDM_ERROR_INVALID_LENGTH;
390 }
391 request->effecter_value[0] = *effecter_value;
392 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
393 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
394 if (payload_length !=
395 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
396 return PLDM_ERROR_INVALID_LENGTH;
397 }
398
George Liuab749502020-04-15 13:16:35 +0800399 uint16_t val = *(uint16_t *)(effecter_value);
400 val = htole16(val);
401 memcpy(request->effecter_value, &val, sizeof(uint16_t));
402
George Liu30b859f2020-01-07 15:03:22 +0800403 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
404 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
405 if (payload_length !=
406 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
407 return PLDM_ERROR_INVALID_LENGTH;
408 }
409
George Liuab749502020-04-15 13:16:35 +0800410 uint32_t val = *(uint32_t *)(effecter_value);
411 val = htole32(val);
412 memcpy(request->effecter_value, &val, sizeof(uint32_t));
George Liu30b859f2020-01-07 15:03:22 +0800413 }
414
415 request->effecter_id = htole16(effecter_id);
416 request->effecter_data_size = effecter_data_size;
417
418 return PLDM_SUCCESS;
419}
420
421int decode_set_numeric_effecter_value_resp(const struct pldm_msg *msg,
422 size_t payload_length,
423 uint8_t *completion_code)
424{
425 if (msg == NULL || completion_code == NULL) {
426 return PLDM_ERROR_INVALID_DATA;
427 }
428
429 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
430 return PLDM_ERROR_INVALID_LENGTH;
431 }
432
433 *completion_code = msg->payload[0];
434
435 return PLDM_SUCCESS;
436}
Jolie Ku3557bad2020-03-02 16:22:57 +0800437
438int encode_get_state_sensor_readings_resp(uint8_t instance_id,
439 uint8_t completion_code,
440 uint8_t comp_sensor_count,
441 get_sensor_state_field *field,
442 struct pldm_msg *msg)
443{
444 struct pldm_header_info header = {0};
445 int rc = PLDM_SUCCESS;
446
447 header.msg_type = PLDM_RESPONSE;
448 header.instance = instance_id;
449 header.pldm_type = PLDM_PLATFORM;
450 header.command = PLDM_GET_STATE_SENSOR_READINGS;
451
452 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
453 return rc;
454 }
455
456 if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
457 return PLDM_ERROR_INVALID_DATA;
458 }
459
460 struct pldm_get_state_sensor_readings_resp *response =
461 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
462
463 response->completion_code = completion_code;
464 response->comp_sensor_count = comp_sensor_count;
465 memcpy(response->field, field,
466 (sizeof(get_sensor_state_field) * comp_sensor_count));
467
468 return rc;
469}
470
471int encode_get_state_sensor_readings_req(uint8_t instance_id,
472 uint16_t sensor_id,
473 bitfield8_t sensor_rearm,
474 uint8_t reserved, struct pldm_msg *msg)
475{
476 struct pldm_header_info header = {0};
477 int rc = PLDM_SUCCESS;
478
479 header.msg_type = PLDM_REQUEST;
480 header.instance = instance_id;
481 header.pldm_type = PLDM_PLATFORM;
482 header.command = PLDM_GET_STATE_SENSOR_READINGS;
483
484 if (msg == NULL) {
485 return PLDM_ERROR_INVALID_DATA;
486 }
487
488 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
489 return rc;
490 }
491
492 struct pldm_get_state_sensor_readings_req *request =
493 (struct pldm_get_state_sensor_readings_req *)msg->payload;
494
495 request->sensor_id = htole16(sensor_id);
496 request->reserved = reserved;
497 request->sensor_rearm = sensor_rearm;
498
499 return PLDM_SUCCESS;
500}
501
502int decode_get_state_sensor_readings_resp(const struct pldm_msg *msg,
503 size_t payload_length,
504 uint8_t *completion_code,
505 uint8_t *comp_sensor_count,
506 get_sensor_state_field *field)
507{
508 if (msg == NULL || completion_code == NULL ||
509 comp_sensor_count == NULL || field == NULL) {
510 return PLDM_ERROR_INVALID_DATA;
511 }
512
513 *completion_code = msg->payload[0];
514 if (PLDM_SUCCESS != *completion_code) {
515 return PLDM_SUCCESS;
516 }
517
518 if (payload_length > PLDM_GET_STATE_SENSOR_READINGS_RESP_BYTES) {
519 return PLDM_ERROR_INVALID_LENGTH;
520 }
521
522 struct pldm_get_state_sensor_readings_resp *response =
523 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
524
525 if (response->comp_sensor_count < 0x1 ||
526 response->comp_sensor_count > 0x8) {
527 return PLDM_ERROR_INVALID_DATA;
528 }
529 if (response->comp_sensor_count > *comp_sensor_count) {
530 return PLDM_ERROR_INVALID_LENGTH;
531 }
532 *comp_sensor_count = response->comp_sensor_count;
533
534 memcpy(field, response->field,
535 (sizeof(get_sensor_state_field) * (*comp_sensor_count)));
536
537 return PLDM_SUCCESS;
538}
539
540int decode_get_state_sensor_readings_req(const struct pldm_msg *msg,
541 size_t payload_length,
542 uint16_t *sensor_id,
543 bitfield8_t *sensor_rearm,
544 uint8_t *reserved)
545{
546 if (msg == NULL || sensor_id == NULL || sensor_rearm == NULL) {
547 return PLDM_ERROR_INVALID_DATA;
548 }
549
550 if (payload_length != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES) {
551 return PLDM_ERROR_INVALID_LENGTH;
552 }
553
554 struct pldm_get_state_sensor_readings_req *request =
555 (struct pldm_get_state_sensor_readings_req *)msg->payload;
556
557 *sensor_id = le16toh(request->sensor_id);
558 *reserved = request->reserved;
559 memcpy(&(sensor_rearm->byte), &(request->sensor_rearm.byte),
560 sizeof(request->sensor_rearm.byte));
561
562 return PLDM_SUCCESS;
563}
Zahed Hossaind4abab12020-02-06 03:36:43 -0600564
565int decode_platform_event_message_req(const struct pldm_msg *msg,
566 size_t payload_length,
567 uint8_t *format_version, uint8_t *tid,
568 uint8_t *event_class,
569 size_t *event_data_offset)
570{
571
572 if (msg == NULL || format_version == NULL || tid == NULL ||
573 event_class == NULL || event_data_offset == NULL) {
574 return PLDM_ERROR_INVALID_DATA;
575 }
576
577 if (payload_length <= PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES) {
578 return PLDM_ERROR_INVALID_LENGTH;
579 }
580 struct pldm_platform_event_message_req *response =
581 (struct pldm_platform_event_message_req *)msg->payload;
582
583 *format_version = response->format_version;
584 *tid = response->tid;
585 *event_class = response->event_class;
586 *event_data_offset =
587 sizeof(*format_version) + sizeof(*tid) + sizeof(*event_class);
588
589 return PLDM_SUCCESS;
590}
591
592int encode_platform_event_message_resp(uint8_t instance_id,
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500593 uint8_t completion_code,
594 uint8_t platform_event_status,
Zahed Hossaind4abab12020-02-06 03:36:43 -0600595 struct pldm_msg *msg)
596{
597 int rc = PLDM_SUCCESS;
598
599 if (msg == NULL) {
600 return PLDM_ERROR_INVALID_DATA;
601 }
602
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500603 if (platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
604 return PLDM_ERROR_INVALID_DATA;
605 }
606
Zahed Hossaind4abab12020-02-06 03:36:43 -0600607 struct pldm_platform_event_message_resp *response =
608 (struct pldm_platform_event_message_resp *)msg->payload;
609 response->completion_code = completion_code;
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500610 response->platform_event_status = platform_event_status;
Zahed Hossaind4abab12020-02-06 03:36:43 -0600611
612 struct pldm_header_info header = {0};
613 header.msg_type = PLDM_RESPONSE;
614 header.instance = instance_id;
615 header.pldm_type = PLDM_PLATFORM;
616 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
617
618 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
619 return rc;
620 }
621 return PLDM_SUCCESS;
622}
Zahed Hossain1c861712020-03-04 08:55:19 -0600623
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500624int encode_platform_event_message_req(
625 uint8_t instance_id, uint8_t format_version, uint8_t tid,
626 uint8_t event_class, const uint8_t *event_data, size_t event_data_length,
627 struct pldm_msg *msg, size_t payload_length)
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500628
629{
630 struct pldm_header_info header = {0};
631 int rc = PLDM_SUCCESS;
632
633 header.msg_type = PLDM_REQUEST;
634 header.instance = instance_id;
635 header.pldm_type = PLDM_PLATFORM;
636 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
637
638 if (format_version != 1) {
639 return PLDM_ERROR_INVALID_DATA;
640 }
641
642 if (msg == NULL || event_data == NULL) {
643 return PLDM_ERROR_INVALID_DATA;
644 }
645
646 if (event_data_length == 0) {
647 return PLDM_ERROR_INVALID_DATA;
648 }
649
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500650 if (payload_length !=
651 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + event_data_length) {
652 return PLDM_ERROR_INVALID_LENGTH;
653 }
654
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500655 if (event_class > PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT &&
656 !(event_class >= 0xF0 && event_class <= 0xFE)) {
657 return PLDM_ERROR_INVALID_DATA;
658 }
659
660 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
661 return rc;
662 }
663
664 struct pldm_platform_event_message_req *request =
665 (struct pldm_platform_event_message_req *)msg->payload;
666 request->format_version = format_version;
667 request->tid = tid;
668 request->event_class = event_class;
669 memcpy(request->event_data, event_data, event_data_length);
670
671 return PLDM_SUCCESS;
672}
673
674int decode_platform_event_message_resp(const struct pldm_msg *msg,
675 size_t payload_length,
676 uint8_t *completion_code,
677 uint8_t *platform_event_status)
678{
679 if (msg == NULL || completion_code == NULL ||
680 platform_event_status == NULL) {
681 return PLDM_ERROR_INVALID_DATA;
682 }
683
684 *completion_code = msg->payload[0];
685 if (PLDM_SUCCESS != *completion_code) {
686 return PLDM_SUCCESS;
687 }
688 if (payload_length != PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES) {
689 return PLDM_ERROR_INVALID_LENGTH;
690 }
691
692 struct pldm_platform_event_message_resp *response =
693 (struct pldm_platform_event_message_resp *)msg->payload;
694 *platform_event_status = response->platform_event_status;
695
696 if (*platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
697 return PLDM_ERROR_INVALID_DATA;
698 }
699
700 return PLDM_SUCCESS;
701}
702
Zahed Hossain1c861712020-03-04 08:55:19 -0600703int decode_sensor_event_data(const uint8_t *event_data,
704 size_t event_data_length, uint16_t *sensor_id,
705 uint8_t *sensor_event_class_type,
706 size_t *event_class_data_offset)
707{
708 if (event_data == NULL) {
709 return PLDM_ERROR_INVALID_DATA;
710 }
711 if (event_data_length < PLDM_SENSOR_EVENT_DATA_MIN_LENGTH) {
712 return PLDM_ERROR_INVALID_LENGTH;
713 }
714
715 size_t event_class_data_length =
716 event_data_length - PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
717
718 struct pldm_sensor_event_data *sensor_event_data =
719 (struct pldm_sensor_event_data *)event_data;
720 *sensor_id = sensor_event_data->sensor_id;
721 *sensor_event_class_type = sensor_event_data->sensor_event_class_type;
722 if (sensor_event_data->sensor_event_class_type ==
723 PLDM_SENSOR_OP_STATE) {
724 if (event_class_data_length !=
725 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
726 return PLDM_ERROR_INVALID_LENGTH;
727 }
728 } else if (sensor_event_data->sensor_event_class_type ==
729 PLDM_STATE_SENSOR_STATE) {
730 if (event_class_data_length !=
731 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
732 return PLDM_ERROR_INVALID_LENGTH;
733 }
734 } else if (sensor_event_data->sensor_event_class_type ==
735 PLDM_NUMERIC_SENSOR_STATE) {
736 if (event_class_data_length <
737 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
738 event_class_data_length >
739 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
740 return PLDM_ERROR_INVALID_LENGTH;
741 }
742 } else {
743 return PLDM_ERROR_INVALID_DATA;
744 }
745 *event_class_data_offset =
746 sizeof(*sensor_id) + sizeof(*sensor_event_class_type);
747 return PLDM_SUCCESS;
748}
749
750int decode_sensor_op_data(const uint8_t *sensor_data, size_t sensor_data_length,
751 uint8_t *present_op_state, uint8_t *previous_op_state)
752{
753 if (sensor_data == NULL || present_op_state == NULL ||
754 previous_op_state == NULL) {
755 return PLDM_ERROR_INVALID_DATA;
756 }
757 if (sensor_data_length !=
758 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
759 return PLDM_ERROR_INVALID_LENGTH;
760 }
761
762 struct pldm_sensor_event_sensor_op_state *sensor_op_data =
763 (struct pldm_sensor_event_sensor_op_state *)sensor_data;
764 *present_op_state = sensor_op_data->present_op_state;
765 *previous_op_state = sensor_op_data->previous_op_state;
766 return PLDM_SUCCESS;
767}
768
769int decode_state_sensor_data(const uint8_t *sensor_data,
770 size_t sensor_data_length, uint8_t *sensor_offset,
771 uint8_t *event_state,
772 uint8_t *previous_event_state)
773{
774 if (sensor_data == NULL || sensor_offset == NULL ||
775 event_state == NULL || previous_event_state == NULL) {
776 return PLDM_ERROR_INVALID_DATA;
777 }
778 if (sensor_data_length !=
779 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
780 return PLDM_ERROR_INVALID_LENGTH;
781 }
782
783 struct pldm_sensor_event_state_sensor_state *sensor_state_data =
784 (struct pldm_sensor_event_state_sensor_state *)sensor_data;
785 *sensor_offset = sensor_state_data->sensor_offset;
786 *event_state = sensor_state_data->event_state;
787 *previous_event_state = sensor_state_data->previous_event_state;
788 return PLDM_SUCCESS;
789}
790
791int decode_numeric_sensor_data(const uint8_t *sensor_data,
792 size_t sensor_data_length, uint8_t *event_state,
793 uint8_t *previous_event_state,
794 uint8_t *sensor_data_size,
795 uint32_t *present_reading)
796{
797 if (sensor_data == NULL || sensor_data_size == NULL ||
798 event_state == NULL || previous_event_state == NULL ||
799 present_reading == NULL) {
800 return PLDM_ERROR_INVALID_DATA;
801 }
802 if (sensor_data_length <
803 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
804 sensor_data_length >
805 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
806 return PLDM_ERROR_INVALID_LENGTH;
807 }
808 struct pldm_sensor_event_numeric_sensor_state *numeric_sensor_data =
809 (struct pldm_sensor_event_numeric_sensor_state *)sensor_data;
810 *event_state = numeric_sensor_data->event_state;
811 *previous_event_state = numeric_sensor_data->previous_event_state;
812 *sensor_data_size = numeric_sensor_data->sensor_data_size;
813 uint8_t *present_reading_ptr = numeric_sensor_data->present_reading;
814
815 switch (*sensor_data_size) {
816 case PLDM_SENSOR_DATA_SIZE_UINT8:
817 case PLDM_SENSOR_DATA_SIZE_SINT8:
818 if (sensor_data_length !=
819 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_8BIT_DATA_LENGTH) {
820 return PLDM_ERROR_INVALID_LENGTH;
821 }
822 *present_reading = present_reading_ptr[0];
823 break;
824 case PLDM_SENSOR_DATA_SIZE_UINT16:
825 case PLDM_SENSOR_DATA_SIZE_SINT16:
826 if (sensor_data_length !=
827 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH) {
828 return PLDM_ERROR_INVALID_LENGTH;
829 }
830 *present_reading = le16toh(present_reading_ptr[1] |
831 (present_reading_ptr[0] << 8));
832 break;
833 case PLDM_SENSOR_DATA_SIZE_UINT32:
834 case PLDM_SENSOR_DATA_SIZE_SINT32:
835 if (sensor_data_length !=
836 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH) {
837 return PLDM_ERROR_INVALID_LENGTH;
838 }
839 *present_reading = le32toh(present_reading_ptr[3] |
840 (present_reading_ptr[2] << 8) |
841 (present_reading_ptr[1] << 16) |
842 (present_reading_ptr[0] << 24));
843 break;
844 default:
845 return PLDM_ERROR_INVALID_DATA;
846 }
847 return PLDM_SUCCESS;
848}
Jolie Ku6787f172020-03-19 11:15:53 +0800849
850int encode_get_numeric_effecter_value_req(uint8_t instance_id,
851 uint16_t effecter_id,
852 struct pldm_msg *msg)
853{
854 struct pldm_header_info header = {0};
855 int rc = PLDM_SUCCESS;
856
857 if (msg == NULL) {
858 return PLDM_ERROR_INVALID_DATA;
859 }
860
861 struct pldm_get_numeric_effecter_value_req *request =
862 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
863
864 header.msg_type = PLDM_REQUEST;
865 header.instance = instance_id;
866 header.pldm_type = PLDM_PLATFORM;
867 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
868
869 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
870 return rc;
871 }
872
873 request->effecter_id = htole16(effecter_id);
874
875 return PLDM_SUCCESS;
876}
877
878int encode_get_numeric_effecter_value_resp(
879 uint8_t instance_id, uint8_t completion_code, uint8_t effecter_data_size,
880 uint8_t effecter_oper_state, uint8_t *pending_value, uint8_t *present_value,
881 struct pldm_msg *msg, size_t payload_length)
882{
883 struct pldm_header_info header = {0};
884 int rc = PLDM_SUCCESS;
885
886 if (msg == NULL || pending_value == NULL || present_value == NULL) {
887 return PLDM_ERROR_INVALID_DATA;
888 }
889
890 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
891 return PLDM_ERROR_INVALID_DATA;
892 }
893
894 if (effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
895 return PLDM_ERROR_INVALID_DATA;
896 }
897
898 header.msg_type = PLDM_RESPONSE;
899 header.instance = instance_id;
900 header.pldm_type = PLDM_PLATFORM;
901 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
902
903 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
904 return rc;
905 }
906
907 struct pldm_get_numeric_effecter_value_resp *response =
908 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
909
910 response->completion_code = completion_code;
911 response->effecter_data_size = effecter_data_size;
912 response->effecter_oper_state = effecter_oper_state;
913
914 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
915 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
916 if (payload_length !=
917 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
918 return PLDM_ERROR_INVALID_LENGTH;
919 }
920 response->pending_and_present_values[0] = *pending_value;
921 response->pending_and_present_values[1] = *present_value;
922
923 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
924 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
925 if (payload_length !=
926 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
927 return PLDM_ERROR_INVALID_LENGTH;
928 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +0800929 uint16_t val_pending = *(uint16_t *)pending_value;
930 val_pending = htole16(val_pending);
931 memcpy(response->pending_and_present_values, &val_pending,
932 sizeof(uint16_t));
933 uint16_t val_present = *(uint16_t *)present_value;
934 val_present = htole16(val_present);
935 memcpy(
936 (response->pending_and_present_values + sizeof(uint16_t)),
937 &val_present, sizeof(uint16_t));
Jolie Ku6787f172020-03-19 11:15:53 +0800938
939 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
940 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
941 if (payload_length !=
942 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
943 return PLDM_ERROR_INVALID_LENGTH;
944 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +0800945 uint32_t val_pending = *(uint32_t *)pending_value;
946 val_pending = htole32(val_pending);
947 memcpy(response->pending_and_present_values, &val_pending,
948 sizeof(uint32_t));
949 uint32_t val_present = *(uint32_t *)present_value;
950 val_present = htole32(val_present);
951 memcpy(
952 (response->pending_and_present_values + sizeof(uint32_t)),
953 &val_present, sizeof(uint32_t));
Jolie Ku6787f172020-03-19 11:15:53 +0800954 }
955 return PLDM_SUCCESS;
956}
957
958int decode_get_numeric_effecter_value_req(const struct pldm_msg *msg,
959 size_t payload_length,
960 uint16_t *effecter_id)
961{
962 if (msg == NULL || effecter_id == NULL) {
963 return PLDM_ERROR_INVALID_DATA;
964 }
965
966 if (payload_length != PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES) {
967 return PLDM_ERROR_INVALID_LENGTH;
968 }
969
970 struct pldm_get_numeric_effecter_value_req *request =
971 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
972
973 *effecter_id = le16toh(request->effecter_id);
974
975 return PLDM_SUCCESS;
976}
977
978int decode_get_numeric_effecter_value_resp(
979 const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
980 uint8_t *effecter_data_size, uint8_t *effecter_oper_state,
981 uint8_t *pending_value, uint8_t *present_value)
982{
983 if (msg == NULL || effecter_data_size == NULL ||
984 effecter_oper_state == NULL || pending_value == NULL ||
985 present_value == NULL) {
986 return PLDM_ERROR_INVALID_DATA;
987 }
988
989 *completion_code = msg->payload[0];
990 if (PLDM_SUCCESS != *completion_code) {
991 return PLDM_SUCCESS;
992 }
993
994 if (payload_length < PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
995 return PLDM_ERROR_INVALID_LENGTH;
996 }
997
998 struct pldm_get_numeric_effecter_value_resp *response =
999 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
1000
1001 *effecter_data_size = response->effecter_data_size;
1002 *effecter_oper_state = response->effecter_oper_state;
1003
1004 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1005 return PLDM_ERROR_INVALID_DATA;
1006 }
1007
1008 if (*effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1009 return PLDM_ERROR_INVALID_DATA;
1010 }
1011
1012 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1013 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1014 if (payload_length !=
1015 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1016 return PLDM_ERROR_INVALID_LENGTH;
1017 }
1018 memcpy(pending_value, response->pending_and_present_values, 1);
1019 memcpy(present_value, &response->pending_and_present_values[1],
1020 1);
1021
1022 } else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1023 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1024 if (payload_length !=
1025 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1026 return PLDM_ERROR_INVALID_LENGTH;
1027 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001028 memcpy(pending_value, response->pending_and_present_values,
1029 sizeof(uint16_t));
1030 uint16_t *val_pending = (uint16_t *)pending_value;
1031 *val_pending = le16toh(*val_pending);
1032 memcpy(
1033 present_value,
1034 (response->pending_and_present_values + sizeof(uint16_t)),
1035 sizeof(uint16_t));
1036 uint16_t *val_present = (uint16_t *)present_value;
1037 *val_present = le16toh(*val_present);
Jolie Ku6787f172020-03-19 11:15:53 +08001038
1039 } else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1040 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1041 if (payload_length !=
1042 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1043 return PLDM_ERROR_INVALID_LENGTH;
1044 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001045 memcpy(pending_value, response->pending_and_present_values,
1046 sizeof(uint32_t));
1047 uint32_t *val_pending = (uint32_t *)pending_value;
1048 *val_pending = le32toh(*val_pending);
1049 memcpy(
1050 present_value,
1051 (response->pending_and_present_values + sizeof(uint32_t)),
1052 sizeof(uint32_t));
1053 uint32_t *val_present = (uint32_t *)present_value;
1054 *val_present = le32toh(*val_present);
Jolie Ku6787f172020-03-19 11:15:53 +08001055 }
1056 return PLDM_SUCCESS;
Zach Clark3dba2bf2020-03-31 10:58:03 -05001057}
Zahed Hossain9be087c2020-04-02 02:26:41 -05001058
Zach Clark36ad1f52020-04-13 07:04:15 -05001059int encode_pldm_pdr_repository_chg_event_data(
1060 uint8_t event_data_format, uint8_t number_of_change_records,
1061 const uint8_t *event_data_operations,
1062 const uint8_t *numbers_of_change_entries,
1063 const uint32_t *const *change_entries,
1064 struct pldm_pdr_repository_chg_event_data *event_data,
1065 size_t *actual_change_records_size, size_t max_change_records_size)
1066{
1067 if (event_data_operations == NULL ||
1068 numbers_of_change_entries == NULL || change_entries == NULL) {
1069 return PLDM_ERROR_INVALID_DATA;
1070 }
1071
1072 size_t expected_size =
1073 sizeof(event_data_format) + sizeof(number_of_change_records);
1074
1075 expected_size +=
1076 sizeof(*event_data_operations) * number_of_change_records;
1077 expected_size +=
1078 sizeof(*numbers_of_change_entries) * number_of_change_records;
1079
1080 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1081 expected_size +=
1082 sizeof(*change_entries[0]) * numbers_of_change_entries[i];
1083 }
1084
1085 *actual_change_records_size = expected_size;
1086
1087 if (event_data == NULL) {
1088 return PLDM_SUCCESS;
1089 }
1090
1091 if (max_change_records_size < expected_size) {
1092 return PLDM_ERROR_INVALID_LENGTH;
1093 }
1094
1095 event_data->event_data_format = event_data_format;
1096 event_data->number_of_change_records = number_of_change_records;
1097
1098 struct pldm_pdr_repository_change_record_data *record_data =
1099 (struct pldm_pdr_repository_change_record_data *)
1100 event_data->change_records;
1101
1102 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1103 record_data->event_data_operation = event_data_operations[i];
1104 record_data->number_of_change_entries =
1105 numbers_of_change_entries[i];
1106
1107 for (uint8_t j = 0; j < record_data->number_of_change_entries;
1108 ++j) {
1109 record_data->change_entry[j] =
1110 htole32(change_entries[i][j]);
1111 }
1112
1113 record_data = (struct pldm_pdr_repository_change_record_data
1114 *)(record_data->change_entry +
1115 record_data->number_of_change_entries);
1116 }
1117
1118 return PLDM_SUCCESS;
1119}
1120
Zahed Hossain9be087c2020-04-02 02:26:41 -05001121int decode_pldm_pdr_repository_chg_event_data(const uint8_t *event_data,
1122 size_t event_data_size,
1123 uint8_t *event_data_format,
1124 uint8_t *number_of_change_records,
1125 size_t *change_record_data_offset)
1126{
1127 if (event_data == NULL || event_data_format == NULL ||
1128 number_of_change_records == NULL ||
1129 change_record_data_offset == NULL) {
1130 return PLDM_ERROR_INVALID_DATA;
1131 }
1132 if (event_data_size < PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH) {
1133 return PLDM_ERROR_INVALID_LENGTH;
1134 }
1135
1136 struct pldm_pdr_repository_chg_event_data
1137 *pdr_repository_chg_event_data =
1138 (struct pldm_pdr_repository_chg_event_data *)event_data;
1139
1140 *event_data_format = pdr_repository_chg_event_data->event_data_format;
1141 *number_of_change_records =
1142 pdr_repository_chg_event_data->number_of_change_records;
1143 *change_record_data_offset =
1144 sizeof(*event_data_format) + sizeof(*number_of_change_records);
1145
1146 return PLDM_SUCCESS;
1147}
1148
1149int decode_pldm_pdr_repository_change_record_data(
1150 const uint8_t *change_record_data, size_t change_record_data_size,
1151 uint8_t *event_data_operation, uint8_t *number_of_change_entries,
1152 size_t *change_entry_data_offset)
1153{
1154 if (change_record_data == NULL || event_data_operation == NULL ||
1155 number_of_change_entries == NULL ||
1156 change_entry_data_offset == NULL) {
1157 return PLDM_ERROR_INVALID_DATA;
1158 }
1159 if (change_record_data_size <
1160 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH) {
1161 return PLDM_ERROR_INVALID_LENGTH;
1162 }
1163
1164 struct pldm_pdr_repository_change_record_data
1165 *pdr_repository_change_record_data =
1166 (struct pldm_pdr_repository_change_record_data *)
1167 change_record_data;
1168
1169 *event_data_operation =
1170 pdr_repository_change_record_data->event_data_operation;
1171 *number_of_change_entries =
1172 pdr_repository_change_record_data->number_of_change_entries;
1173 *change_entry_data_offset =
1174 sizeof(*event_data_operation) + sizeof(*number_of_change_entries);
1175
1176 return PLDM_SUCCESS;
1177}
Jolie Kuf798c8f2020-04-14 11:18:06 +08001178
1179int encode_get_sensor_reading_req(uint8_t instance_id, uint16_t sensor_id,
1180 uint8_t rearm_event_state,
1181 struct pldm_msg *msg)
1182{
1183 struct pldm_header_info header = {0};
1184 int rc = PLDM_SUCCESS;
1185
1186 header.msg_type = PLDM_REQUEST;
1187 header.instance = instance_id;
1188 header.pldm_type = PLDM_PLATFORM;
1189 header.command = PLDM_GET_SENSOR_READING;
1190
1191 if (msg == NULL) {
1192 return PLDM_ERROR_INVALID_DATA;
1193 }
1194
1195 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
1196 return rc;
1197 }
1198
1199 struct pldm_get_sensor_reading_req *request =
1200 (struct pldm_get_sensor_reading_req *)msg->payload;
1201
1202 request->sensor_id = htole16(sensor_id);
1203 request->rearm_event_state = rearm_event_state;
1204
1205 return PLDM_SUCCESS;
1206}
1207
1208int decode_get_sensor_reading_resp(
1209 const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
1210 uint8_t *sensor_data_size, uint8_t *sensor_operational_state,
1211 uint8_t *sensor_event_message_enable, uint8_t *present_state,
1212 uint8_t *previous_state, uint8_t *event_state, uint8_t *present_reading)
1213{
1214 if (msg == NULL || completion_code == NULL ||
1215 sensor_data_size == NULL || sensor_operational_state == NULL ||
1216 sensor_event_message_enable == NULL || present_state == NULL ||
1217 previous_state == NULL || event_state == NULL ||
1218 present_reading == NULL) {
1219 return PLDM_ERROR_INVALID_DATA;
1220 }
1221
1222 *completion_code = msg->payload[0];
1223 if (PLDM_SUCCESS != *completion_code) {
1224 return PLDM_SUCCESS;
1225 }
1226
1227 if (payload_length < PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1228 return PLDM_ERROR_INVALID_LENGTH;
1229 }
1230
1231 struct pldm_get_sensor_reading_resp *response =
1232 (struct pldm_get_sensor_reading_resp *)msg->payload;
1233
1234 if (response->sensor_data_size > PLDM_SENSOR_DATA_SIZE_SINT32) {
1235 return PLDM_ERROR_INVALID_DATA;
1236 }
1237 if (response->sensor_data_size > *sensor_data_size) {
1238 return PLDM_ERROR_INVALID_LENGTH;
1239 }
1240
1241 *sensor_data_size = response->sensor_data_size;
1242 *sensor_operational_state = response->sensor_operational_state;
1243 *sensor_event_message_enable = response->sensor_event_message_enable;
1244 *present_state = response->present_state;
1245 *previous_state = response->previous_state;
1246 *event_state = response->event_state;
1247
1248 if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1249 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1250 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1251 return PLDM_ERROR_INVALID_LENGTH;
1252 }
1253 *present_reading = response->present_reading[0];
1254
1255 } else if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1256 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1257 if (payload_length !=
1258 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
1259 return PLDM_ERROR_INVALID_LENGTH;
1260 }
1261 memcpy(present_reading, response->present_reading, 2);
1262 uint16_t *val = (uint16_t *)(present_reading);
1263 *val = le16toh(*val);
1264
1265 } else if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1266 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1267 if (payload_length !=
1268 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
1269 return PLDM_ERROR_INVALID_LENGTH;
1270 }
1271 memcpy(present_reading, response->present_reading, 4);
1272 uint32_t *val = (uint32_t *)(present_reading);
1273 *val = le32toh(*val);
1274 }
1275
1276 return PLDM_SUCCESS;
1277}
1278
1279int encode_get_sensor_reading_resp(
1280 uint8_t instance_id, uint8_t completion_code, uint8_t sensor_data_size,
1281 uint8_t sensor_operational_state, uint8_t sensor_event_message_enable,
1282 uint8_t present_state, uint8_t previous_state, uint8_t event_state,
1283 uint8_t *present_reading, struct pldm_msg *msg, size_t payload_length)
1284{
1285 struct pldm_header_info header = {0};
1286 int rc = PLDM_SUCCESS;
1287
1288 if (msg == NULL || present_reading == NULL) {
1289 return PLDM_ERROR_INVALID_DATA;
1290 }
1291
1292 if (sensor_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1293 return PLDM_ERROR_INVALID_DATA;
1294 }
1295
1296 header.msg_type = PLDM_RESPONSE;
1297 header.instance = instance_id;
1298 header.pldm_type = PLDM_PLATFORM;
1299 header.command = PLDM_GET_SENSOR_READING;
1300
1301 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
1302 return rc;
1303 }
1304
1305 struct pldm_get_sensor_reading_resp *response =
1306 (struct pldm_get_sensor_reading_resp *)msg->payload;
1307
1308 response->completion_code = completion_code;
1309 response->sensor_data_size = sensor_data_size;
1310 response->sensor_operational_state = sensor_operational_state;
1311 response->sensor_event_message_enable = sensor_event_message_enable;
1312 response->present_state = present_state;
1313 response->previous_state = previous_state;
1314 response->event_state = event_state;
1315
1316 if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1317 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1318 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1319 return PLDM_ERROR_INVALID_LENGTH;
1320 }
1321 response->present_reading[0] = *present_reading;
1322
1323 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1324 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1325 if (payload_length !=
1326 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
1327 return PLDM_ERROR_INVALID_LENGTH;
1328 }
1329 uint16_t val = *(uint16_t *)present_reading;
1330 val = htole16(val);
1331 memcpy(response->present_reading, &val, 2);
1332
1333 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1334 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1335 if (payload_length !=
1336 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
1337 return PLDM_ERROR_INVALID_LENGTH;
1338 }
1339 uint32_t val = *(uint32_t *)present_reading;
1340 val = htole32(val);
1341 memcpy(response->present_reading, &val, 4);
1342 }
1343
1344 return PLDM_SUCCESS;
1345}
1346
1347int decode_get_sensor_reading_req(const struct pldm_msg *msg,
1348 size_t payload_length, uint16_t *sensor_id,
1349 uint8_t *rearm_event_state)
1350{
1351 if (msg == NULL || sensor_id == NULL || rearm_event_state == NULL) {
1352 return PLDM_ERROR_INVALID_DATA;
1353 }
1354
1355 if (payload_length != PLDM_GET_SENSOR_READING_REQ_BYTES) {
1356 return PLDM_ERROR_INVALID_LENGTH;
1357 }
1358
1359 struct pldm_get_sensor_reading_req *request =
1360 (struct pldm_get_sensor_reading_req *)msg->payload;
1361
1362 *sensor_id = le16toh(request->sensor_id);
1363 *rearm_event_state = request->rearm_event_state;
1364
1365 return PLDM_SUCCESS;
1366}