blob: 9c696fc75692542a8fc63b57aee5277c7da941c2 [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 ||
Zach Clark3dba2bf2020-03-31 10:58:03 -0500219 transfer_flag == NULL || resp_cnt == NULL || transfer_crc == NULL) {
George Liu820a9a52019-11-26 14:43:59 +0800220 return PLDM_ERROR_INVALID_DATA;
221 }
222
223 *completion_code = msg->payload[0];
224 if (PLDM_SUCCESS != *completion_code) {
George Liu684a7162019-12-06 15:10:52 +0800225 return PLDM_SUCCESS;
George Liu820a9a52019-11-26 14:43:59 +0800226 }
227
228 if (payload_length < PLDM_GET_PDR_MIN_RESP_BYTES) {
229 return PLDM_ERROR_INVALID_LENGTH;
230 }
231
232 struct pldm_get_pdr_resp *response =
233 (struct pldm_get_pdr_resp *)msg->payload;
234
235 *next_record_hndl = le32toh(response->next_record_handle);
236 *next_data_transfer_hndl = le32toh(response->next_data_transfer_handle);
237 *transfer_flag = response->transfer_flag;
238 *resp_cnt = le16toh(response->response_count);
239
240 if (*transfer_flag != PLDM_END &&
241 (int)payload_length != PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt) {
242 return PLDM_ERROR_INVALID_LENGTH;
243 }
244
245 if (*transfer_flag == PLDM_END &&
246 (int)payload_length !=
247 PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt + 1) {
248 return PLDM_ERROR_INVALID_LENGTH;
249 }
250
Zach Clark3dba2bf2020-03-31 10:58:03 -0500251 if (*resp_cnt > 0 && record_data != NULL) {
George Liu820a9a52019-11-26 14:43:59 +0800252 if (record_data_length < *resp_cnt) {
253 return PLDM_ERROR_INVALID_LENGTH;
254 }
255 memcpy(record_data, response->record_data, *resp_cnt);
256 }
257
258 if (*transfer_flag == PLDM_END) {
259 *transfer_crc =
260 msg->payload[PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt];
261 }
262
263 return PLDM_SUCCESS;
264}
George Liu30b859f2020-01-07 15:03:22 +0800265
266int decode_set_numeric_effecter_value_req(const struct pldm_msg *msg,
267 size_t payload_length,
268 uint16_t *effecter_id,
269 uint8_t *effecter_data_size,
270 uint8_t *effecter_value)
271{
272 if (msg == NULL || effecter_id == NULL || effecter_data_size == NULL ||
273 effecter_value == NULL) {
274 return PLDM_ERROR_INVALID_DATA;
275 }
276
277 if (payload_length < PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
278 return PLDM_ERROR_INVALID_LENGTH;
279 }
280
281 struct pldm_set_numeric_effecter_value_req *request =
282 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
283 *effecter_id = le16toh(request->effecter_id);
284 *effecter_data_size = request->effecter_data_size;
285
286 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
287 return PLDM_ERROR_INVALID_DATA;
288 }
289
290 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
291 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
292
293 if (payload_length !=
294 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
295 return PLDM_ERROR_INVALID_LENGTH;
296 }
297
298 *effecter_value = request->effecter_value[0];
299 }
300
301 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
302 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
303
304 if (payload_length !=
305 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
306 return PLDM_ERROR_INVALID_LENGTH;
307 }
308
309 memcpy(effecter_value, request->effecter_value, 2);
George Liuda75abe2020-04-09 13:36:13 +0800310 uint16_t *val = (uint16_t *)(effecter_value);
311 *val = le16toh(*val);
George Liu30b859f2020-01-07 15:03:22 +0800312 }
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 Liuda75abe2020-04-09 13:36:13 +0800323 uint32_t *val = (uint32_t *)(effecter_value);
324 *val = le32toh(*val);
George Liu30b859f2020-01-07 15:03:22 +0800325 }
326
327 return PLDM_SUCCESS;
328}
329
330int encode_set_numeric_effecter_value_resp(uint8_t instance_id,
331 uint8_t completion_code,
332 struct pldm_msg *msg,
333 size_t payload_length)
334{
335 struct pldm_header_info header = {0};
336 int rc = PLDM_SUCCESS;
337
338 if (msg == NULL) {
339 return PLDM_ERROR_INVALID_DATA;
340 }
341
342 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
343 return PLDM_ERROR_INVALID_LENGTH;
344 }
345
346 msg->payload[0] = completion_code;
347
348 header.msg_type = PLDM_RESPONSE;
349 header.instance = instance_id;
350 header.pldm_type = PLDM_PLATFORM;
351 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
352
353 rc = pack_pldm_header(&header, &(msg->hdr));
354
355 return rc;
356}
357
358int encode_set_numeric_effecter_value_req(
359 uint8_t instance_id, uint16_t effecter_id, uint8_t effecter_data_size,
360 uint8_t *effecter_value, struct pldm_msg *msg, size_t payload_length)
361{
362 struct pldm_header_info header = {0};
363 int rc = PLDM_SUCCESS;
364
365 if (msg == NULL || effecter_value == NULL) {
366 return PLDM_ERROR_INVALID_DATA;
367 }
368 struct pldm_set_numeric_effecter_value_req *request =
369 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
370
371 header.msg_type = PLDM_REQUEST;
372 header.instance = instance_id;
373 header.pldm_type = PLDM_PLATFORM;
374 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
375
376 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
377 return rc;
378 }
379
380 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
381 return PLDM_ERROR_INVALID_DATA;
382 }
383
384 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
385 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
386 if (payload_length !=
387 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
388 return PLDM_ERROR_INVALID_LENGTH;
389 }
390 request->effecter_value[0] = *effecter_value;
391 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
392 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
393 if (payload_length !=
394 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
395 return PLDM_ERROR_INVALID_LENGTH;
396 }
397
George Liuab749502020-04-15 13:16:35 +0800398 uint16_t val = *(uint16_t *)(effecter_value);
399 val = htole16(val);
400 memcpy(request->effecter_value, &val, sizeof(uint16_t));
401
George Liu30b859f2020-01-07 15:03:22 +0800402 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
403 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
404 if (payload_length !=
405 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
406 return PLDM_ERROR_INVALID_LENGTH;
407 }
408
George Liuab749502020-04-15 13:16:35 +0800409 uint32_t val = *(uint32_t *)(effecter_value);
410 val = htole32(val);
411 memcpy(request->effecter_value, &val, sizeof(uint32_t));
George Liu30b859f2020-01-07 15:03:22 +0800412 }
413
414 request->effecter_id = htole16(effecter_id);
415 request->effecter_data_size = effecter_data_size;
416
417 return PLDM_SUCCESS;
418}
419
420int decode_set_numeric_effecter_value_resp(const struct pldm_msg *msg,
421 size_t payload_length,
422 uint8_t *completion_code)
423{
424 if (msg == NULL || completion_code == NULL) {
425 return PLDM_ERROR_INVALID_DATA;
426 }
427
428 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
429 return PLDM_ERROR_INVALID_LENGTH;
430 }
431
432 *completion_code = msg->payload[0];
433
434 return PLDM_SUCCESS;
435}
Jolie Ku3557bad2020-03-02 16:22:57 +0800436
437int encode_get_state_sensor_readings_resp(uint8_t instance_id,
438 uint8_t completion_code,
439 uint8_t comp_sensor_count,
440 get_sensor_state_field *field,
441 struct pldm_msg *msg)
442{
443 struct pldm_header_info header = {0};
444 int rc = PLDM_SUCCESS;
445
446 header.msg_type = PLDM_RESPONSE;
447 header.instance = instance_id;
448 header.pldm_type = PLDM_PLATFORM;
449 header.command = PLDM_GET_STATE_SENSOR_READINGS;
450
451 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
452 return rc;
453 }
454
455 if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
456 return PLDM_ERROR_INVALID_DATA;
457 }
458
459 struct pldm_get_state_sensor_readings_resp *response =
460 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
461
462 response->completion_code = completion_code;
463 response->comp_sensor_count = comp_sensor_count;
464 memcpy(response->field, field,
465 (sizeof(get_sensor_state_field) * comp_sensor_count));
466
467 return rc;
468}
469
470int encode_get_state_sensor_readings_req(uint8_t instance_id,
471 uint16_t sensor_id,
472 bitfield8_t sensor_rearm,
473 uint8_t reserved, struct pldm_msg *msg)
474{
475 struct pldm_header_info header = {0};
476 int rc = PLDM_SUCCESS;
477
478 header.msg_type = PLDM_REQUEST;
479 header.instance = instance_id;
480 header.pldm_type = PLDM_PLATFORM;
481 header.command = PLDM_GET_STATE_SENSOR_READINGS;
482
483 if (msg == NULL) {
484 return PLDM_ERROR_INVALID_DATA;
485 }
486
487 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
488 return rc;
489 }
490
491 struct pldm_get_state_sensor_readings_req *request =
492 (struct pldm_get_state_sensor_readings_req *)msg->payload;
493
494 request->sensor_id = htole16(sensor_id);
495 request->reserved = reserved;
496 request->sensor_rearm = sensor_rearm;
497
498 return PLDM_SUCCESS;
499}
500
501int decode_get_state_sensor_readings_resp(const struct pldm_msg *msg,
502 size_t payload_length,
503 uint8_t *completion_code,
504 uint8_t *comp_sensor_count,
505 get_sensor_state_field *field)
506{
507 if (msg == NULL || completion_code == NULL ||
508 comp_sensor_count == NULL || field == NULL) {
509 return PLDM_ERROR_INVALID_DATA;
510 }
511
512 *completion_code = msg->payload[0];
513 if (PLDM_SUCCESS != *completion_code) {
514 return PLDM_SUCCESS;
515 }
516
517 if (payload_length > PLDM_GET_STATE_SENSOR_READINGS_RESP_BYTES) {
518 return PLDM_ERROR_INVALID_LENGTH;
519 }
520
521 struct pldm_get_state_sensor_readings_resp *response =
522 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
523
524 if (response->comp_sensor_count < 0x1 ||
525 response->comp_sensor_count > 0x8) {
526 return PLDM_ERROR_INVALID_DATA;
527 }
528 if (response->comp_sensor_count > *comp_sensor_count) {
529 return PLDM_ERROR_INVALID_LENGTH;
530 }
531 *comp_sensor_count = response->comp_sensor_count;
532
533 memcpy(field, response->field,
534 (sizeof(get_sensor_state_field) * (*comp_sensor_count)));
535
536 return PLDM_SUCCESS;
537}
538
539int decode_get_state_sensor_readings_req(const struct pldm_msg *msg,
540 size_t payload_length,
541 uint16_t *sensor_id,
542 bitfield8_t *sensor_rearm,
543 uint8_t *reserved)
544{
545 if (msg == NULL || sensor_id == NULL || sensor_rearm == NULL) {
546 return PLDM_ERROR_INVALID_DATA;
547 }
548
549 if (payload_length != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES) {
550 return PLDM_ERROR_INVALID_LENGTH;
551 }
552
553 struct pldm_get_state_sensor_readings_req *request =
554 (struct pldm_get_state_sensor_readings_req *)msg->payload;
555
556 *sensor_id = le16toh(request->sensor_id);
557 *reserved = request->reserved;
558 memcpy(&(sensor_rearm->byte), &(request->sensor_rearm.byte),
559 sizeof(request->sensor_rearm.byte));
560
561 return PLDM_SUCCESS;
562}
Zahed Hossaind4abab12020-02-06 03:36:43 -0600563
564int decode_platform_event_message_req(const struct pldm_msg *msg,
565 size_t payload_length,
566 uint8_t *format_version, uint8_t *tid,
567 uint8_t *event_class,
568 size_t *event_data_offset)
569{
570
571 if (msg == NULL || format_version == NULL || tid == NULL ||
572 event_class == NULL || event_data_offset == NULL) {
573 return PLDM_ERROR_INVALID_DATA;
574 }
575
576 if (payload_length <= PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES) {
577 return PLDM_ERROR_INVALID_LENGTH;
578 }
579 struct pldm_platform_event_message_req *response =
580 (struct pldm_platform_event_message_req *)msg->payload;
581
582 *format_version = response->format_version;
583 *tid = response->tid;
584 *event_class = response->event_class;
585 *event_data_offset =
586 sizeof(*format_version) + sizeof(*tid) + sizeof(*event_class);
587
588 return PLDM_SUCCESS;
589}
590
591int encode_platform_event_message_resp(uint8_t instance_id,
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500592 uint8_t completion_code,
593 uint8_t platform_event_status,
Zahed Hossaind4abab12020-02-06 03:36:43 -0600594 struct pldm_msg *msg)
595{
596 int rc = PLDM_SUCCESS;
597
598 if (msg == NULL) {
599 return PLDM_ERROR_INVALID_DATA;
600 }
601
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500602 if (platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
603 return PLDM_ERROR_INVALID_DATA;
604 }
605
Zahed Hossaind4abab12020-02-06 03:36:43 -0600606 struct pldm_platform_event_message_resp *response =
607 (struct pldm_platform_event_message_resp *)msg->payload;
608 response->completion_code = completion_code;
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500609 response->platform_event_status = platform_event_status;
Zahed Hossaind4abab12020-02-06 03:36:43 -0600610
611 struct pldm_header_info header = {0};
612 header.msg_type = PLDM_RESPONSE;
613 header.instance = instance_id;
614 header.pldm_type = PLDM_PLATFORM;
615 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
616
617 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
618 return rc;
619 }
620 return PLDM_SUCCESS;
621}
Zahed Hossain1c861712020-03-04 08:55:19 -0600622
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500623int encode_platform_event_message_req(
624 uint8_t instance_id, uint8_t format_version, uint8_t tid,
625 uint8_t event_class, const uint8_t *event_data, size_t event_data_length,
626 struct pldm_msg *msg, size_t payload_length)
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500627
628{
629 struct pldm_header_info header = {0};
630 int rc = PLDM_SUCCESS;
631
632 header.msg_type = PLDM_REQUEST;
633 header.instance = instance_id;
634 header.pldm_type = PLDM_PLATFORM;
635 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
636
637 if (format_version != 1) {
638 return PLDM_ERROR_INVALID_DATA;
639 }
640
641 if (msg == NULL || event_data == NULL) {
642 return PLDM_ERROR_INVALID_DATA;
643 }
644
645 if (event_data_length == 0) {
646 return PLDM_ERROR_INVALID_DATA;
647 }
648
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500649 if (payload_length !=
650 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + event_data_length) {
651 return PLDM_ERROR_INVALID_LENGTH;
652 }
653
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500654 if (event_class > PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT &&
655 !(event_class >= 0xF0 && event_class <= 0xFE)) {
656 return PLDM_ERROR_INVALID_DATA;
657 }
658
659 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
660 return rc;
661 }
662
663 struct pldm_platform_event_message_req *request =
664 (struct pldm_platform_event_message_req *)msg->payload;
665 request->format_version = format_version;
666 request->tid = tid;
667 request->event_class = event_class;
668 memcpy(request->event_data, event_data, event_data_length);
669
670 return PLDM_SUCCESS;
671}
672
673int decode_platform_event_message_resp(const struct pldm_msg *msg,
674 size_t payload_length,
675 uint8_t *completion_code,
676 uint8_t *platform_event_status)
677{
678 if (msg == NULL || completion_code == NULL ||
679 platform_event_status == NULL) {
680 return PLDM_ERROR_INVALID_DATA;
681 }
682
683 *completion_code = msg->payload[0];
684 if (PLDM_SUCCESS != *completion_code) {
685 return PLDM_SUCCESS;
686 }
687 if (payload_length != PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES) {
688 return PLDM_ERROR_INVALID_LENGTH;
689 }
690
691 struct pldm_platform_event_message_resp *response =
692 (struct pldm_platform_event_message_resp *)msg->payload;
693 *platform_event_status = response->platform_event_status;
694
695 if (*platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
696 return PLDM_ERROR_INVALID_DATA;
697 }
698
699 return PLDM_SUCCESS;
700}
701
Zahed Hossain1c861712020-03-04 08:55:19 -0600702int decode_sensor_event_data(const uint8_t *event_data,
703 size_t event_data_length, uint16_t *sensor_id,
704 uint8_t *sensor_event_class_type,
705 size_t *event_class_data_offset)
706{
707 if (event_data == NULL) {
708 return PLDM_ERROR_INVALID_DATA;
709 }
710 if (event_data_length < PLDM_SENSOR_EVENT_DATA_MIN_LENGTH) {
711 return PLDM_ERROR_INVALID_LENGTH;
712 }
713
714 size_t event_class_data_length =
715 event_data_length - PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
716
717 struct pldm_sensor_event_data *sensor_event_data =
718 (struct pldm_sensor_event_data *)event_data;
719 *sensor_id = sensor_event_data->sensor_id;
720 *sensor_event_class_type = sensor_event_data->sensor_event_class_type;
721 if (sensor_event_data->sensor_event_class_type ==
722 PLDM_SENSOR_OP_STATE) {
723 if (event_class_data_length !=
724 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
725 return PLDM_ERROR_INVALID_LENGTH;
726 }
727 } else if (sensor_event_data->sensor_event_class_type ==
728 PLDM_STATE_SENSOR_STATE) {
729 if (event_class_data_length !=
730 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
731 return PLDM_ERROR_INVALID_LENGTH;
732 }
733 } else if (sensor_event_data->sensor_event_class_type ==
734 PLDM_NUMERIC_SENSOR_STATE) {
735 if (event_class_data_length <
736 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
737 event_class_data_length >
738 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
739 return PLDM_ERROR_INVALID_LENGTH;
740 }
741 } else {
742 return PLDM_ERROR_INVALID_DATA;
743 }
744 *event_class_data_offset =
745 sizeof(*sensor_id) + sizeof(*sensor_event_class_type);
746 return PLDM_SUCCESS;
747}
748
749int decode_sensor_op_data(const uint8_t *sensor_data, size_t sensor_data_length,
750 uint8_t *present_op_state, uint8_t *previous_op_state)
751{
752 if (sensor_data == NULL || present_op_state == NULL ||
753 previous_op_state == NULL) {
754 return PLDM_ERROR_INVALID_DATA;
755 }
756 if (sensor_data_length !=
757 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
758 return PLDM_ERROR_INVALID_LENGTH;
759 }
760
761 struct pldm_sensor_event_sensor_op_state *sensor_op_data =
762 (struct pldm_sensor_event_sensor_op_state *)sensor_data;
763 *present_op_state = sensor_op_data->present_op_state;
764 *previous_op_state = sensor_op_data->previous_op_state;
765 return PLDM_SUCCESS;
766}
767
768int decode_state_sensor_data(const uint8_t *sensor_data,
769 size_t sensor_data_length, uint8_t *sensor_offset,
770 uint8_t *event_state,
771 uint8_t *previous_event_state)
772{
773 if (sensor_data == NULL || sensor_offset == NULL ||
774 event_state == NULL || previous_event_state == NULL) {
775 return PLDM_ERROR_INVALID_DATA;
776 }
777 if (sensor_data_length !=
778 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
779 return PLDM_ERROR_INVALID_LENGTH;
780 }
781
782 struct pldm_sensor_event_state_sensor_state *sensor_state_data =
783 (struct pldm_sensor_event_state_sensor_state *)sensor_data;
784 *sensor_offset = sensor_state_data->sensor_offset;
785 *event_state = sensor_state_data->event_state;
786 *previous_event_state = sensor_state_data->previous_event_state;
787 return PLDM_SUCCESS;
788}
789
790int decode_numeric_sensor_data(const uint8_t *sensor_data,
791 size_t sensor_data_length, uint8_t *event_state,
792 uint8_t *previous_event_state,
793 uint8_t *sensor_data_size,
794 uint32_t *present_reading)
795{
796 if (sensor_data == NULL || sensor_data_size == NULL ||
797 event_state == NULL || previous_event_state == NULL ||
798 present_reading == NULL) {
799 return PLDM_ERROR_INVALID_DATA;
800 }
801 if (sensor_data_length <
802 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
803 sensor_data_length >
804 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
805 return PLDM_ERROR_INVALID_LENGTH;
806 }
807 struct pldm_sensor_event_numeric_sensor_state *numeric_sensor_data =
808 (struct pldm_sensor_event_numeric_sensor_state *)sensor_data;
809 *event_state = numeric_sensor_data->event_state;
810 *previous_event_state = numeric_sensor_data->previous_event_state;
811 *sensor_data_size = numeric_sensor_data->sensor_data_size;
812 uint8_t *present_reading_ptr = numeric_sensor_data->present_reading;
813
814 switch (*sensor_data_size) {
815 case PLDM_SENSOR_DATA_SIZE_UINT8:
816 case PLDM_SENSOR_DATA_SIZE_SINT8:
817 if (sensor_data_length !=
818 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_8BIT_DATA_LENGTH) {
819 return PLDM_ERROR_INVALID_LENGTH;
820 }
821 *present_reading = present_reading_ptr[0];
822 break;
823 case PLDM_SENSOR_DATA_SIZE_UINT16:
824 case PLDM_SENSOR_DATA_SIZE_SINT16:
825 if (sensor_data_length !=
826 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH) {
827 return PLDM_ERROR_INVALID_LENGTH;
828 }
829 *present_reading = le16toh(present_reading_ptr[1] |
830 (present_reading_ptr[0] << 8));
831 break;
832 case PLDM_SENSOR_DATA_SIZE_UINT32:
833 case PLDM_SENSOR_DATA_SIZE_SINT32:
834 if (sensor_data_length !=
835 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH) {
836 return PLDM_ERROR_INVALID_LENGTH;
837 }
838 *present_reading = le32toh(present_reading_ptr[3] |
839 (present_reading_ptr[2] << 8) |
840 (present_reading_ptr[1] << 16) |
841 (present_reading_ptr[0] << 24));
842 break;
843 default:
844 return PLDM_ERROR_INVALID_DATA;
845 }
846 return PLDM_SUCCESS;
847}
Jolie Ku6787f172020-03-19 11:15:53 +0800848
849int encode_get_numeric_effecter_value_req(uint8_t instance_id,
850 uint16_t effecter_id,
851 struct pldm_msg *msg)
852{
853 struct pldm_header_info header = {0};
854 int rc = PLDM_SUCCESS;
855
856 if (msg == NULL) {
857 return PLDM_ERROR_INVALID_DATA;
858 }
859
860 struct pldm_get_numeric_effecter_value_req *request =
861 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
862
863 header.msg_type = PLDM_REQUEST;
864 header.instance = instance_id;
865 header.pldm_type = PLDM_PLATFORM;
866 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
867
868 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
869 return rc;
870 }
871
872 request->effecter_id = htole16(effecter_id);
873
874 return PLDM_SUCCESS;
875}
876
877int encode_get_numeric_effecter_value_resp(
878 uint8_t instance_id, uint8_t completion_code, uint8_t effecter_data_size,
879 uint8_t effecter_oper_state, uint8_t *pending_value, uint8_t *present_value,
880 struct pldm_msg *msg, size_t payload_length)
881{
882 struct pldm_header_info header = {0};
883 int rc = PLDM_SUCCESS;
884
885 if (msg == NULL || pending_value == NULL || present_value == NULL) {
886 return PLDM_ERROR_INVALID_DATA;
887 }
888
889 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
890 return PLDM_ERROR_INVALID_DATA;
891 }
892
893 if (effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
894 return PLDM_ERROR_INVALID_DATA;
895 }
896
897 header.msg_type = PLDM_RESPONSE;
898 header.instance = instance_id;
899 header.pldm_type = PLDM_PLATFORM;
900 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
901
902 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
903 return rc;
904 }
905
906 struct pldm_get_numeric_effecter_value_resp *response =
907 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
908
909 response->completion_code = completion_code;
910 response->effecter_data_size = effecter_data_size;
911 response->effecter_oper_state = effecter_oper_state;
912
913 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
914 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
915 if (payload_length !=
916 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
917 return PLDM_ERROR_INVALID_LENGTH;
918 }
919 response->pending_and_present_values[0] = *pending_value;
920 response->pending_and_present_values[1] = *present_value;
921
922 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
923 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
924 if (payload_length !=
925 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
926 return PLDM_ERROR_INVALID_LENGTH;
927 }
928 memcpy(response->pending_and_present_values, pending_value, 2);
929 memcpy(&response->pending_and_present_values[2], present_value,
930 2);
931
932 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
933 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
934 if (payload_length !=
935 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
936 return PLDM_ERROR_INVALID_LENGTH;
937 }
938 memcpy(response->pending_and_present_values, pending_value, 4);
939 memcpy(&response->pending_and_present_values[4], present_value,
940 4);
941 }
942 return PLDM_SUCCESS;
943}
944
945int decode_get_numeric_effecter_value_req(const struct pldm_msg *msg,
946 size_t payload_length,
947 uint16_t *effecter_id)
948{
949 if (msg == NULL || effecter_id == NULL) {
950 return PLDM_ERROR_INVALID_DATA;
951 }
952
953 if (payload_length != PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES) {
954 return PLDM_ERROR_INVALID_LENGTH;
955 }
956
957 struct pldm_get_numeric_effecter_value_req *request =
958 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
959
960 *effecter_id = le16toh(request->effecter_id);
961
962 return PLDM_SUCCESS;
963}
964
965int decode_get_numeric_effecter_value_resp(
966 const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
967 uint8_t *effecter_data_size, uint8_t *effecter_oper_state,
968 uint8_t *pending_value, uint8_t *present_value)
969{
970 if (msg == NULL || effecter_data_size == NULL ||
971 effecter_oper_state == NULL || pending_value == NULL ||
972 present_value == NULL) {
973 return PLDM_ERROR_INVALID_DATA;
974 }
975
976 *completion_code = msg->payload[0];
977 if (PLDM_SUCCESS != *completion_code) {
978 return PLDM_SUCCESS;
979 }
980
981 if (payload_length < PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
982 return PLDM_ERROR_INVALID_LENGTH;
983 }
984
985 struct pldm_get_numeric_effecter_value_resp *response =
986 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
987
988 *effecter_data_size = response->effecter_data_size;
989 *effecter_oper_state = response->effecter_oper_state;
990
991 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
992 return PLDM_ERROR_INVALID_DATA;
993 }
994
995 if (*effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
996 return PLDM_ERROR_INVALID_DATA;
997 }
998
999 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1000 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1001 if (payload_length !=
1002 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1003 return PLDM_ERROR_INVALID_LENGTH;
1004 }
1005 memcpy(pending_value, response->pending_and_present_values, 1);
1006 memcpy(present_value, &response->pending_and_present_values[1],
1007 1);
1008
1009 } else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1010 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1011 if (payload_length !=
1012 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1013 return PLDM_ERROR_INVALID_LENGTH;
1014 }
1015 memcpy(pending_value, response->pending_and_present_values, 2);
1016 memcpy(present_value, &response->pending_and_present_values[2],
1017 2);
1018
1019 } else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1020 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1021 if (payload_length !=
1022 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1023 return PLDM_ERROR_INVALID_LENGTH;
1024 }
1025 memcpy(pending_value, response->pending_and_present_values, 4);
1026 memcpy(present_value, &response->pending_and_present_values[4],
1027 4);
1028 }
1029 return PLDM_SUCCESS;
Zach Clark3dba2bf2020-03-31 10:58:03 -05001030}
Zahed Hossain9be087c2020-04-02 02:26:41 -05001031
Zach Clark36ad1f52020-04-13 07:04:15 -05001032int encode_pldm_pdr_repository_chg_event_data(
1033 uint8_t event_data_format, uint8_t number_of_change_records,
1034 const uint8_t *event_data_operations,
1035 const uint8_t *numbers_of_change_entries,
1036 const uint32_t *const *change_entries,
1037 struct pldm_pdr_repository_chg_event_data *event_data,
1038 size_t *actual_change_records_size, size_t max_change_records_size)
1039{
1040 if (event_data_operations == NULL ||
1041 numbers_of_change_entries == NULL || change_entries == NULL) {
1042 return PLDM_ERROR_INVALID_DATA;
1043 }
1044
1045 size_t expected_size =
1046 sizeof(event_data_format) + sizeof(number_of_change_records);
1047
1048 expected_size +=
1049 sizeof(*event_data_operations) * number_of_change_records;
1050 expected_size +=
1051 sizeof(*numbers_of_change_entries) * number_of_change_records;
1052
1053 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1054 expected_size +=
1055 sizeof(*change_entries[0]) * numbers_of_change_entries[i];
1056 }
1057
1058 *actual_change_records_size = expected_size;
1059
1060 if (event_data == NULL) {
1061 return PLDM_SUCCESS;
1062 }
1063
1064 if (max_change_records_size < expected_size) {
1065 return PLDM_ERROR_INVALID_LENGTH;
1066 }
1067
1068 event_data->event_data_format = event_data_format;
1069 event_data->number_of_change_records = number_of_change_records;
1070
1071 struct pldm_pdr_repository_change_record_data *record_data =
1072 (struct pldm_pdr_repository_change_record_data *)
1073 event_data->change_records;
1074
1075 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1076 record_data->event_data_operation = event_data_operations[i];
1077 record_data->number_of_change_entries =
1078 numbers_of_change_entries[i];
1079
1080 for (uint8_t j = 0; j < record_data->number_of_change_entries;
1081 ++j) {
1082 record_data->change_entry[j] =
1083 htole32(change_entries[i][j]);
1084 }
1085
1086 record_data = (struct pldm_pdr_repository_change_record_data
1087 *)(record_data->change_entry +
1088 record_data->number_of_change_entries);
1089 }
1090
1091 return PLDM_SUCCESS;
1092}
1093
Zahed Hossain9be087c2020-04-02 02:26:41 -05001094int decode_pldm_pdr_repository_chg_event_data(const uint8_t *event_data,
1095 size_t event_data_size,
1096 uint8_t *event_data_format,
1097 uint8_t *number_of_change_records,
1098 size_t *change_record_data_offset)
1099{
1100 if (event_data == NULL || event_data_format == NULL ||
1101 number_of_change_records == NULL ||
1102 change_record_data_offset == NULL) {
1103 return PLDM_ERROR_INVALID_DATA;
1104 }
1105 if (event_data_size < PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH) {
1106 return PLDM_ERROR_INVALID_LENGTH;
1107 }
1108
1109 struct pldm_pdr_repository_chg_event_data
1110 *pdr_repository_chg_event_data =
1111 (struct pldm_pdr_repository_chg_event_data *)event_data;
1112
1113 *event_data_format = pdr_repository_chg_event_data->event_data_format;
1114 *number_of_change_records =
1115 pdr_repository_chg_event_data->number_of_change_records;
1116 *change_record_data_offset =
1117 sizeof(*event_data_format) + sizeof(*number_of_change_records);
1118
1119 return PLDM_SUCCESS;
1120}
1121
1122int decode_pldm_pdr_repository_change_record_data(
1123 const uint8_t *change_record_data, size_t change_record_data_size,
1124 uint8_t *event_data_operation, uint8_t *number_of_change_entries,
1125 size_t *change_entry_data_offset)
1126{
1127 if (change_record_data == NULL || event_data_operation == NULL ||
1128 number_of_change_entries == NULL ||
1129 change_entry_data_offset == NULL) {
1130 return PLDM_ERROR_INVALID_DATA;
1131 }
1132 if (change_record_data_size <
1133 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH) {
1134 return PLDM_ERROR_INVALID_LENGTH;
1135 }
1136
1137 struct pldm_pdr_repository_change_record_data
1138 *pdr_repository_change_record_data =
1139 (struct pldm_pdr_repository_change_record_data *)
1140 change_record_data;
1141
1142 *event_data_operation =
1143 pdr_repository_change_record_data->event_data_operation;
1144 *number_of_change_entries =
1145 pdr_repository_change_record_data->number_of_change_entries;
1146 *change_entry_data_offset =
1147 sizeof(*event_data_operation) + sizeof(*number_of_change_entries);
1148
1149 return PLDM_SUCCESS;
1150}
Jolie Kuf798c8f2020-04-14 11:18:06 +08001151
1152int encode_get_sensor_reading_req(uint8_t instance_id, uint16_t sensor_id,
1153 uint8_t rearm_event_state,
1154 struct pldm_msg *msg)
1155{
1156 struct pldm_header_info header = {0};
1157 int rc = PLDM_SUCCESS;
1158
1159 header.msg_type = PLDM_REQUEST;
1160 header.instance = instance_id;
1161 header.pldm_type = PLDM_PLATFORM;
1162 header.command = PLDM_GET_SENSOR_READING;
1163
1164 if (msg == NULL) {
1165 return PLDM_ERROR_INVALID_DATA;
1166 }
1167
1168 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
1169 return rc;
1170 }
1171
1172 struct pldm_get_sensor_reading_req *request =
1173 (struct pldm_get_sensor_reading_req *)msg->payload;
1174
1175 request->sensor_id = htole16(sensor_id);
1176 request->rearm_event_state = rearm_event_state;
1177
1178 return PLDM_SUCCESS;
1179}
1180
1181int decode_get_sensor_reading_resp(
1182 const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
1183 uint8_t *sensor_data_size, uint8_t *sensor_operational_state,
1184 uint8_t *sensor_event_message_enable, uint8_t *present_state,
1185 uint8_t *previous_state, uint8_t *event_state, uint8_t *present_reading)
1186{
1187 if (msg == NULL || completion_code == NULL ||
1188 sensor_data_size == NULL || sensor_operational_state == NULL ||
1189 sensor_event_message_enable == NULL || present_state == NULL ||
1190 previous_state == NULL || event_state == NULL ||
1191 present_reading == NULL) {
1192 return PLDM_ERROR_INVALID_DATA;
1193 }
1194
1195 *completion_code = msg->payload[0];
1196 if (PLDM_SUCCESS != *completion_code) {
1197 return PLDM_SUCCESS;
1198 }
1199
1200 if (payload_length < PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1201 return PLDM_ERROR_INVALID_LENGTH;
1202 }
1203
1204 struct pldm_get_sensor_reading_resp *response =
1205 (struct pldm_get_sensor_reading_resp *)msg->payload;
1206
1207 if (response->sensor_data_size > PLDM_SENSOR_DATA_SIZE_SINT32) {
1208 return PLDM_ERROR_INVALID_DATA;
1209 }
1210 if (response->sensor_data_size > *sensor_data_size) {
1211 return PLDM_ERROR_INVALID_LENGTH;
1212 }
1213
1214 *sensor_data_size = response->sensor_data_size;
1215 *sensor_operational_state = response->sensor_operational_state;
1216 *sensor_event_message_enable = response->sensor_event_message_enable;
1217 *present_state = response->present_state;
1218 *previous_state = response->previous_state;
1219 *event_state = response->event_state;
1220
1221 if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1222 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1223 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1224 return PLDM_ERROR_INVALID_LENGTH;
1225 }
1226 *present_reading = response->present_reading[0];
1227
1228 } else if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1229 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1230 if (payload_length !=
1231 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
1232 return PLDM_ERROR_INVALID_LENGTH;
1233 }
1234 memcpy(present_reading, response->present_reading, 2);
1235 uint16_t *val = (uint16_t *)(present_reading);
1236 *val = le16toh(*val);
1237
1238 } else if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1239 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1240 if (payload_length !=
1241 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
1242 return PLDM_ERROR_INVALID_LENGTH;
1243 }
1244 memcpy(present_reading, response->present_reading, 4);
1245 uint32_t *val = (uint32_t *)(present_reading);
1246 *val = le32toh(*val);
1247 }
1248
1249 return PLDM_SUCCESS;
1250}
1251
1252int encode_get_sensor_reading_resp(
1253 uint8_t instance_id, uint8_t completion_code, uint8_t sensor_data_size,
1254 uint8_t sensor_operational_state, uint8_t sensor_event_message_enable,
1255 uint8_t present_state, uint8_t previous_state, uint8_t event_state,
1256 uint8_t *present_reading, struct pldm_msg *msg, size_t payload_length)
1257{
1258 struct pldm_header_info header = {0};
1259 int rc = PLDM_SUCCESS;
1260
1261 if (msg == NULL || present_reading == NULL) {
1262 return PLDM_ERROR_INVALID_DATA;
1263 }
1264
1265 if (sensor_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1266 return PLDM_ERROR_INVALID_DATA;
1267 }
1268
1269 header.msg_type = PLDM_RESPONSE;
1270 header.instance = instance_id;
1271 header.pldm_type = PLDM_PLATFORM;
1272 header.command = PLDM_GET_SENSOR_READING;
1273
1274 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
1275 return rc;
1276 }
1277
1278 struct pldm_get_sensor_reading_resp *response =
1279 (struct pldm_get_sensor_reading_resp *)msg->payload;
1280
1281 response->completion_code = completion_code;
1282 response->sensor_data_size = sensor_data_size;
1283 response->sensor_operational_state = sensor_operational_state;
1284 response->sensor_event_message_enable = sensor_event_message_enable;
1285 response->present_state = present_state;
1286 response->previous_state = previous_state;
1287 response->event_state = event_state;
1288
1289 if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1290 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1291 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1292 return PLDM_ERROR_INVALID_LENGTH;
1293 }
1294 response->present_reading[0] = *present_reading;
1295
1296 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1297 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1298 if (payload_length !=
1299 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
1300 return PLDM_ERROR_INVALID_LENGTH;
1301 }
1302 uint16_t val = *(uint16_t *)present_reading;
1303 val = htole16(val);
1304 memcpy(response->present_reading, &val, 2);
1305
1306 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1307 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1308 if (payload_length !=
1309 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
1310 return PLDM_ERROR_INVALID_LENGTH;
1311 }
1312 uint32_t val = *(uint32_t *)present_reading;
1313 val = htole32(val);
1314 memcpy(response->present_reading, &val, 4);
1315 }
1316
1317 return PLDM_SUCCESS;
1318}
1319
1320int decode_get_sensor_reading_req(const struct pldm_msg *msg,
1321 size_t payload_length, uint16_t *sensor_id,
1322 uint8_t *rearm_event_state)
1323{
1324 if (msg == NULL || sensor_id == NULL || rearm_event_state == NULL) {
1325 return PLDM_ERROR_INVALID_DATA;
1326 }
1327
1328 if (payload_length != PLDM_GET_SENSOR_READING_REQ_BYTES) {
1329 return PLDM_ERROR_INVALID_LENGTH;
1330 }
1331
1332 struct pldm_get_sensor_reading_req *request =
1333 (struct pldm_get_sensor_reading_req *)msg->payload;
1334
1335 *sensor_id = le16toh(request->sensor_id);
1336 *rearm_event_state = request->rearm_event_state;
1337
1338 return PLDM_SUCCESS;
1339}