blob: 8e33239631d6051f04f41574fd4abcfc4e36b825 [file] [log] [blame]
Sampa Misra0db1dfa2019-03-19 00:15:31 -05001#include <endian.h>
2#include <string.h>
3
4#include "platform.h"
5
Zach Clarkb728eee2020-06-18 10:01:31 -05006int encode_state_effecter_pdr(
7 struct pldm_state_effecter_pdr *const effecter,
8 const size_t allocation_size,
9 const struct state_effecter_possible_states *const possible_states,
10 const size_t possible_states_size, size_t *const actual_size)
11{
12 // Encode possible states
13
14 size_t calculated_possible_states_size = 0;
15
16 {
17 char *states_ptr = (char *)possible_states;
18 char *const begin_states_ptr = states_ptr;
19
20 for (int i = 0; i < effecter->composite_effecter_count; ++i) {
21 struct state_effecter_possible_states *states =
22 (struct state_effecter_possible_states *)states_ptr;
23
24 HTOLE16(states->state_set_id);
25
26 states_ptr +=
27 (sizeof(*states) - sizeof(states->states) +
28 states->possible_states_size);
29 }
30
31 calculated_possible_states_size = states_ptr - begin_states_ptr;
32 }
33
34 // Check lengths
35
36 if (possible_states_size != calculated_possible_states_size) {
37 *actual_size = 0;
38 return PLDM_ERROR;
39 }
40
41 *actual_size =
42 (sizeof(struct pldm_state_effecter_pdr) + possible_states_size -
43 sizeof(effecter->possible_states));
44
45 if (allocation_size < *actual_size) {
46 *actual_size = 0;
47 return PLDM_ERROR_INVALID_LENGTH;
48 }
49
50 // Encode rest of PDR
51
52 effecter->hdr.version = 1;
53 effecter->hdr.type = PLDM_STATE_EFFECTER_PDR;
54 effecter->hdr.length = *actual_size - sizeof(struct pldm_pdr_hdr);
55
56 memcpy(effecter->possible_states, possible_states,
57 possible_states_size);
58
59 // Convert effecter PDR body
60 HTOLE16(effecter->terminus_handle);
61 HTOLE16(effecter->effecter_id);
62 HTOLE16(effecter->entity_type);
63 HTOLE16(effecter->entity_instance);
64 HTOLE16(effecter->container_id);
65 HTOLE16(effecter->effecter_semantic_id);
66
67 // Convert header
68 HTOLE32(effecter->hdr.record_handle);
69 HTOLE16(effecter->hdr.record_change_num);
70 HTOLE16(effecter->hdr.length);
71
72 return PLDM_SUCCESS;
73}
74
75int encode_state_sensor_pdr(
76 struct pldm_state_sensor_pdr *const sensor, const size_t allocation_size,
77 const struct state_sensor_possible_states *const possible_states,
78 const size_t possible_states_size, size_t *const actual_size)
79{
80 // Encode possible states
81
82 size_t calculated_possible_states_size = 0;
83
84 {
85 char *states_ptr = (char *)possible_states,
86 *const begin_states_ptr = states_ptr;
87
88 for (int i = 0; i < sensor->composite_sensor_count; ++i) {
89 struct state_sensor_possible_states *states =
90 (struct state_sensor_possible_states *)states_ptr;
91
92 HTOLE16(states->state_set_id);
93
94 states_ptr +=
95 (sizeof(*states) - sizeof(states->states) +
96 states->possible_states_size);
97 }
98
99 calculated_possible_states_size = states_ptr - begin_states_ptr;
100 }
101
102 // Check lengths
103
104 if (possible_states_size != calculated_possible_states_size) {
105 *actual_size = 0;
106 return PLDM_ERROR;
107 }
108
109 *actual_size = (sizeof(struct pldm_state_sensor_pdr) +
110 possible_states_size - sizeof(sensor->possible_states));
111
112 if (allocation_size < *actual_size) {
113 *actual_size = 0;
114 return PLDM_ERROR_INVALID_LENGTH;
115 }
116
117 // Encode rest of PDR
118
119 sensor->hdr.version = 1;
120 sensor->hdr.type = PLDM_STATE_SENSOR_PDR;
121 sensor->hdr.length = *actual_size - sizeof(struct pldm_pdr_hdr);
122
123 memcpy(sensor->possible_states, possible_states, possible_states_size);
124
125 // Convert sensor PDR body
126 HTOLE16(sensor->terminus_handle);
127 HTOLE16(sensor->sensor_id);
128 HTOLE16(sensor->entity_type);
129 HTOLE16(sensor->entity_instance);
130 HTOLE16(sensor->container_id);
131
132 // Convert header
133 HTOLE32(sensor->hdr.record_handle);
134 HTOLE16(sensor->hdr.record_change_num);
135 HTOLE16(sensor->hdr.length);
136
137 return PLDM_SUCCESS;
138}
139
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500140int encode_set_state_effecter_states_resp(uint8_t instance_id,
141 uint8_t completion_code,
142 struct pldm_msg *msg)
143{
George Liub7095ff2021-06-14 16:01:57 +0800144 if (msg == NULL) {
145 return PLDM_ERROR_INVALID_DATA;
146 }
147
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500148 struct pldm_header_info header = {0};
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500149 header.msg_type = PLDM_RESPONSE;
150 header.instance = instance_id;
151 header.pldm_type = PLDM_PLATFORM;
152 header.command = PLDM_SET_STATE_EFFECTER_STATES;
153
George Liub7095ff2021-06-14 16:01:57 +0800154 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
155 if (rc != PLDM_SUCCESS) {
156 return rc;
157 }
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500158
George Liub7095ff2021-06-14 16:01:57 +0800159 msg->payload[0] = completion_code;
160
161 return PLDM_SUCCESS;
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500162}
163
vkaverap98a2c192019-04-03 05:33:52 -0500164int encode_set_state_effecter_states_req(uint8_t instance_id,
165 uint16_t effecter_id,
166 uint8_t comp_effecter_count,
167 set_effecter_state_field *field,
168 struct pldm_msg *msg)
169{
George Liub7095ff2021-06-14 16:01:57 +0800170 if (msg == NULL) {
171 return PLDM_ERROR_INVALID_DATA;
vkaverap98a2c192019-04-03 05:33:52 -0500172 }
173
Tom Joseph250c4752020-04-15 10:32:45 +0530174 if (comp_effecter_count < 0x1 || comp_effecter_count > 0x8 ||
175 field == NULL) {
vkaverap98a2c192019-04-03 05:33:52 -0500176 return PLDM_ERROR_INVALID_DATA;
177 }
178
George Liub7095ff2021-06-14 16:01:57 +0800179 struct pldm_header_info header = {0};
180 header.msg_type = PLDM_REQUEST;
181 header.instance = instance_id;
182 header.pldm_type = PLDM_PLATFORM;
183 header.command = PLDM_SET_STATE_EFFECTER_STATES;
184
185 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
186 if (rc != PLDM_SUCCESS) {
187 return rc;
188 }
189
Priyanga7257fdf2019-06-10 01:59:45 -0500190 struct pldm_set_state_effecter_states_req *request =
191 (struct pldm_set_state_effecter_states_req *)msg->payload;
vkaverap98a2c192019-04-03 05:33:52 -0500192 effecter_id = htole16(effecter_id);
Priyanga7257fdf2019-06-10 01:59:45 -0500193 request->effecter_id = effecter_id;
194 request->comp_effecter_count = comp_effecter_count;
195 memcpy(request->field, field,
vkaverap98a2c192019-04-03 05:33:52 -0500196 (sizeof(set_effecter_state_field) * comp_effecter_count));
197
198 return PLDM_SUCCESS;
199}
200
Zahed Hossain223a73d2019-07-04 12:46:18 -0500201int decode_set_state_effecter_states_resp(const struct pldm_msg *msg,
vkaverapa6575b82019-04-03 05:33:52 -0500202 size_t payload_length,
vkaverap98a2c192019-04-03 05:33:52 -0500203 uint8_t *completion_code)
204{
205 if (msg == NULL || completion_code == NULL) {
206 return PLDM_ERROR_INVALID_DATA;
207 }
208
George Liu684a7162019-12-06 15:10:52 +0800209 *completion_code = msg->payload[0];
210 if (PLDM_SUCCESS != *completion_code) {
211 return PLDM_SUCCESS;
212 }
213
vkaverapa6575b82019-04-03 05:33:52 -0500214 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES) {
215 return PLDM_ERROR_INVALID_LENGTH;
216 }
217
vkaverap98a2c192019-04-03 05:33:52 -0500218 return PLDM_SUCCESS;
219}
220
Zahed Hossain223a73d2019-07-04 12:46:18 -0500221int decode_set_state_effecter_states_req(const struct pldm_msg *msg,
vkaverapa6575b82019-04-03 05:33:52 -0500222 size_t payload_length,
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500223 uint16_t *effecter_id,
224 uint8_t *comp_effecter_count,
225 set_effecter_state_field *field)
226{
227 if (msg == NULL || effecter_id == NULL || comp_effecter_count == NULL ||
228 field == NULL) {
229 return PLDM_ERROR_INVALID_DATA;
230 }
vkaverapa6575b82019-04-03 05:33:52 -0500231
232 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) {
233 return PLDM_ERROR_INVALID_LENGTH;
234 }
235
Priyanga7257fdf2019-06-10 01:59:45 -0500236 struct pldm_set_state_effecter_states_req *request =
Zahed Hossain223a73d2019-07-04 12:46:18 -0500237 (struct pldm_set_state_effecter_states_req *)msg->payload;
Priyanga7257fdf2019-06-10 01:59:45 -0500238
239 *effecter_id = le16toh(request->effecter_id);
240 *comp_effecter_count = request->comp_effecter_count;
241 memcpy(field, request->field,
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500242 (sizeof(set_effecter_state_field) * (*comp_effecter_count)));
243
244 return PLDM_SUCCESS;
245}
Sampa Misra7fcfb662019-05-08 13:13:53 -0500246
247int decode_get_pdr_req(const struct pldm_msg *msg, size_t payload_length,
248 uint32_t *record_hndl, uint32_t *data_transfer_hndl,
249 uint8_t *transfer_op_flag, uint16_t *request_cnt,
250 uint16_t *record_chg_num)
251{
252 if (msg == NULL || record_hndl == NULL || data_transfer_hndl == NULL ||
253 transfer_op_flag == NULL || request_cnt == NULL ||
254 record_chg_num == NULL) {
255 return PLDM_ERROR_INVALID_DATA;
256 }
257 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
258 return PLDM_ERROR_INVALID_LENGTH;
259 }
260
261 struct pldm_get_pdr_req *request =
262 (struct pldm_get_pdr_req *)msg->payload;
263 *record_hndl = le32toh(request->record_handle);
264 *data_transfer_hndl = le32toh(request->data_transfer_handle);
265 *transfer_op_flag = request->transfer_op_flag;
266 *request_cnt = le16toh(request->request_count);
267 *record_chg_num = le16toh(request->record_change_number);
268
269 return PLDM_SUCCESS;
270}
271
272int encode_get_pdr_resp(uint8_t instance_id, uint8_t completion_code,
273 uint32_t next_record_hndl,
274 uint32_t next_data_transfer_hndl, uint8_t transfer_flag,
275 uint16_t resp_cnt, const uint8_t *record_data,
276 uint8_t transfer_crc, struct pldm_msg *msg)
277{
Sampa Misra7fcfb662019-05-08 13:13:53 -0500278 if (msg == NULL) {
279 return PLDM_ERROR_INVALID_DATA;
280 }
Sampa Misra7fcfb662019-05-08 13:13:53 -0500281
George Liub7095ff2021-06-14 16:01:57 +0800282 struct pldm_header_info header = {0};
Sampa Misra7fcfb662019-05-08 13:13:53 -0500283 header.msg_type = PLDM_RESPONSE;
284 header.instance = instance_id;
285 header.pldm_type = PLDM_PLATFORM;
286 header.command = PLDM_GET_PDR;
George Liub7095ff2021-06-14 16:01:57 +0800287
288 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
289 if (rc != PLDM_SUCCESS) {
Sampa Misra7fcfb662019-05-08 13:13:53 -0500290 return rc;
291 }
292
George Liub7095ff2021-06-14 16:01:57 +0800293 struct pldm_get_pdr_resp *response =
294 (struct pldm_get_pdr_resp *)msg->payload;
295 response->completion_code = completion_code;
296
Sampa Misra7fcfb662019-05-08 13:13:53 -0500297 if (response->completion_code == PLDM_SUCCESS) {
298 response->next_record_handle = htole32(next_record_hndl);
299 response->next_data_transfer_handle =
300 htole32(next_data_transfer_hndl);
301 response->transfer_flag = transfer_flag;
302 response->response_count = htole16(resp_cnt);
303 if (record_data != NULL && resp_cnt > 0) {
304 memcpy(response->record_data, record_data, resp_cnt);
305 }
Deepak Kodihallie4b16ee2019-08-14 06:54:38 -0500306 if (transfer_flag == PLDM_END) {
307 uint8_t *dst = msg->payload;
308 dst +=
309 (sizeof(struct pldm_get_pdr_resp) - 1) + resp_cnt;
310 *dst = transfer_crc;
311 }
Sampa Misra7fcfb662019-05-08 13:13:53 -0500312 }
313
314 return PLDM_SUCCESS;
315}
George Liu820a9a52019-11-26 14:43:59 +0800316
George Liu9a3c6eb2021-10-27 10:50:28 +0800317int encode_get_pdr_repository_info_resp(
318 uint8_t instance_id, uint8_t completion_code, uint8_t repository_state,
319 const uint8_t *update_time, const uint8_t *oem_update_time,
320 uint32_t record_count, uint32_t repository_size,
321 uint32_t largest_record_size, uint8_t data_transfer_handle_timeout,
322 struct pldm_msg *msg)
323{
324 if (msg == NULL) {
325 return PLDM_ERROR_INVALID_DATA;
326 }
327
328 struct pldm_header_info header = {0};
329 header.msg_type = PLDM_RESPONSE;
330 header.instance = instance_id;
331 header.pldm_type = PLDM_PLATFORM;
332 header.command = PLDM_GET_PDR_REPOSITORY_INFO;
333
334 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
335 if (rc != PLDM_SUCCESS) {
336 return rc;
337 }
338
339 struct pldm_pdr_repository_info_resp *response =
340 (struct pldm_pdr_repository_info_resp *)msg->payload;
341 response->completion_code = completion_code;
342
343 if (response->completion_code == PLDM_SUCCESS) {
344 response->repository_state = repository_state;
345 if (update_time != NULL) {
346 memcpy(response->update_time, update_time,
347 PLDM_TIMESTAMP104_SIZE);
348 }
349 if (oem_update_time != NULL) {
350 memcpy(response->oem_update_time, oem_update_time,
351 PLDM_TIMESTAMP104_SIZE);
352 }
353 response->record_count = htole32(record_count);
354 response->repository_size = htole32(repository_size);
355 response->largest_record_size = htole32(largest_record_size);
356 response->data_transfer_handle_timeout =
357 data_transfer_handle_timeout;
358 }
359
360 return PLDM_SUCCESS;
361}
362
George Liu820a9a52019-11-26 14:43:59 +0800363int encode_get_pdr_req(uint8_t instance_id, uint32_t record_hndl,
364 uint32_t data_transfer_hndl, uint8_t transfer_op_flag,
365 uint16_t request_cnt, uint16_t record_chg_num,
366 struct pldm_msg *msg, size_t payload_length)
367{
George Liu820a9a52019-11-26 14:43:59 +0800368 if (msg == NULL) {
369 return PLDM_ERROR_INVALID_DATA;
370 }
George Liu820a9a52019-11-26 14:43:59 +0800371
372 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
373 return PLDM_ERROR_INVALID_LENGTH;
374 }
375
George Liub7095ff2021-06-14 16:01:57 +0800376 struct pldm_header_info header = {0};
377 header.msg_type = PLDM_REQUEST;
378 header.instance = instance_id;
379 header.pldm_type = PLDM_PLATFORM;
380 header.command = PLDM_GET_PDR;
381
382 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
383 if (rc != PLDM_SUCCESS) {
384 return rc;
385 }
386
387 struct pldm_get_pdr_req *request =
388 (struct pldm_get_pdr_req *)msg->payload;
George Liu820a9a52019-11-26 14:43:59 +0800389 request->record_handle = htole32(record_hndl);
390 request->data_transfer_handle = htole32(data_transfer_hndl);
391 request->transfer_op_flag = transfer_op_flag;
392 request->request_count = htole16(request_cnt);
393 request->record_change_number = htole16(record_chg_num);
394
395 return PLDM_SUCCESS;
396}
397
398int decode_get_pdr_resp(const struct pldm_msg *msg, size_t payload_length,
399 uint8_t *completion_code, uint32_t *next_record_hndl,
400 uint32_t *next_data_transfer_hndl,
401 uint8_t *transfer_flag, uint16_t *resp_cnt,
402 uint8_t *record_data, size_t record_data_length,
403 uint8_t *transfer_crc)
404{
405 if (msg == NULL || completion_code == NULL ||
406 next_record_hndl == NULL || next_data_transfer_hndl == NULL ||
Zach Clark3dba2bf2020-03-31 10:58:03 -0500407 transfer_flag == NULL || resp_cnt == NULL || transfer_crc == NULL) {
George Liu820a9a52019-11-26 14:43:59 +0800408 return PLDM_ERROR_INVALID_DATA;
409 }
410
411 *completion_code = msg->payload[0];
412 if (PLDM_SUCCESS != *completion_code) {
George Liu684a7162019-12-06 15:10:52 +0800413 return PLDM_SUCCESS;
George Liu820a9a52019-11-26 14:43:59 +0800414 }
415
416 if (payload_length < PLDM_GET_PDR_MIN_RESP_BYTES) {
417 return PLDM_ERROR_INVALID_LENGTH;
418 }
419
420 struct pldm_get_pdr_resp *response =
421 (struct pldm_get_pdr_resp *)msg->payload;
422
423 *next_record_hndl = le32toh(response->next_record_handle);
424 *next_data_transfer_hndl = le32toh(response->next_data_transfer_handle);
425 *transfer_flag = response->transfer_flag;
426 *resp_cnt = le16toh(response->response_count);
427
428 if (*transfer_flag != PLDM_END &&
429 (int)payload_length != PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt) {
430 return PLDM_ERROR_INVALID_LENGTH;
431 }
432
433 if (*transfer_flag == PLDM_END &&
434 (int)payload_length !=
435 PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt + 1) {
436 return PLDM_ERROR_INVALID_LENGTH;
437 }
438
Zach Clark3dba2bf2020-03-31 10:58:03 -0500439 if (*resp_cnt > 0 && record_data != NULL) {
George Liu820a9a52019-11-26 14:43:59 +0800440 if (record_data_length < *resp_cnt) {
441 return PLDM_ERROR_INVALID_LENGTH;
442 }
443 memcpy(record_data, response->record_data, *resp_cnt);
444 }
445
446 if (*transfer_flag == PLDM_END) {
447 *transfer_crc =
448 msg->payload[PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt];
449 }
450
451 return PLDM_SUCCESS;
452}
George Liu30b859f2020-01-07 15:03:22 +0800453
454int decode_set_numeric_effecter_value_req(const struct pldm_msg *msg,
455 size_t payload_length,
456 uint16_t *effecter_id,
457 uint8_t *effecter_data_size,
458 uint8_t *effecter_value)
459{
460 if (msg == NULL || effecter_id == NULL || effecter_data_size == NULL ||
461 effecter_value == NULL) {
462 return PLDM_ERROR_INVALID_DATA;
463 }
464
465 if (payload_length < PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
466 return PLDM_ERROR_INVALID_LENGTH;
467 }
468
469 struct pldm_set_numeric_effecter_value_req *request =
470 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
471 *effecter_id = le16toh(request->effecter_id);
472 *effecter_data_size = request->effecter_data_size;
473
474 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
475 return PLDM_ERROR_INVALID_DATA;
476 }
477
478 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
479 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
480
481 if (payload_length !=
482 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
483 return PLDM_ERROR_INVALID_LENGTH;
484 }
485
486 *effecter_value = request->effecter_value[0];
487 }
488
489 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
490 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
491
492 if (payload_length !=
493 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
494 return PLDM_ERROR_INVALID_LENGTH;
495 }
496
497 memcpy(effecter_value, request->effecter_value, 2);
George Liuda75abe2020-04-09 13:36:13 +0800498 uint16_t *val = (uint16_t *)(effecter_value);
499 *val = le16toh(*val);
George Liu30b859f2020-01-07 15:03:22 +0800500 }
501
502 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
503 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
504
505 if (payload_length !=
506 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
507 return PLDM_ERROR_INVALID_LENGTH;
508 }
509
510 memcpy(effecter_value, request->effecter_value, 4);
George Liuda75abe2020-04-09 13:36:13 +0800511 uint32_t *val = (uint32_t *)(effecter_value);
512 *val = le32toh(*val);
George Liu30b859f2020-01-07 15:03:22 +0800513 }
514
515 return PLDM_SUCCESS;
516}
517
518int encode_set_numeric_effecter_value_resp(uint8_t instance_id,
519 uint8_t completion_code,
520 struct pldm_msg *msg,
521 size_t payload_length)
522{
George Liu30b859f2020-01-07 15:03:22 +0800523 if (msg == NULL) {
524 return PLDM_ERROR_INVALID_DATA;
525 }
526
527 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
528 return PLDM_ERROR_INVALID_LENGTH;
529 }
530
George Liub7095ff2021-06-14 16:01:57 +0800531 struct pldm_header_info header = {0};
George Liu30b859f2020-01-07 15:03:22 +0800532 header.msg_type = PLDM_RESPONSE;
533 header.instance = instance_id;
534 header.pldm_type = PLDM_PLATFORM;
535 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
536
George Liub7095ff2021-06-14 16:01:57 +0800537 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
538 if (rc != PLDM_SUCCESS) {
539 return rc;
540 }
541
542 msg->payload[0] = completion_code;
George Liu30b859f2020-01-07 15:03:22 +0800543
544 return rc;
545}
546
547int encode_set_numeric_effecter_value_req(
548 uint8_t instance_id, uint16_t effecter_id, uint8_t effecter_data_size,
549 uint8_t *effecter_value, struct pldm_msg *msg, size_t payload_length)
550{
George Liu30b859f2020-01-07 15:03:22 +0800551 if (msg == NULL || effecter_value == NULL) {
552 return PLDM_ERROR_INVALID_DATA;
553 }
George Liu30b859f2020-01-07 15:03:22 +0800554
555 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
556 return PLDM_ERROR_INVALID_DATA;
557 }
558
George Liub7095ff2021-06-14 16:01:57 +0800559 struct pldm_header_info header = {0};
560 header.msg_type = PLDM_REQUEST;
561 header.instance = instance_id;
562 header.pldm_type = PLDM_PLATFORM;
563 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
564
565 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
566 if (rc != PLDM_SUCCESS) {
567 return rc;
568 }
569
570 struct pldm_set_numeric_effecter_value_req *request =
571 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
George Liu30b859f2020-01-07 15:03:22 +0800572 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
573 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
574 if (payload_length !=
575 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
576 return PLDM_ERROR_INVALID_LENGTH;
577 }
578 request->effecter_value[0] = *effecter_value;
579 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
580 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
581 if (payload_length !=
582 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
583 return PLDM_ERROR_INVALID_LENGTH;
584 }
585
George Liuab749502020-04-15 13:16:35 +0800586 uint16_t val = *(uint16_t *)(effecter_value);
587 val = htole16(val);
588 memcpy(request->effecter_value, &val, sizeof(uint16_t));
589
George Liu30b859f2020-01-07 15:03:22 +0800590 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
591 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
592 if (payload_length !=
593 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
594 return PLDM_ERROR_INVALID_LENGTH;
595 }
596
George Liuab749502020-04-15 13:16:35 +0800597 uint32_t val = *(uint32_t *)(effecter_value);
598 val = htole32(val);
599 memcpy(request->effecter_value, &val, sizeof(uint32_t));
George Liu30b859f2020-01-07 15:03:22 +0800600 }
601
602 request->effecter_id = htole16(effecter_id);
603 request->effecter_data_size = effecter_data_size;
604
605 return PLDM_SUCCESS;
606}
607
608int decode_set_numeric_effecter_value_resp(const struct pldm_msg *msg,
609 size_t payload_length,
610 uint8_t *completion_code)
611{
612 if (msg == NULL || completion_code == NULL) {
613 return PLDM_ERROR_INVALID_DATA;
614 }
615
616 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
617 return PLDM_ERROR_INVALID_LENGTH;
618 }
619
620 *completion_code = msg->payload[0];
621
622 return PLDM_SUCCESS;
623}
Jolie Ku3557bad2020-03-02 16:22:57 +0800624
625int encode_get_state_sensor_readings_resp(uint8_t instance_id,
626 uint8_t completion_code,
627 uint8_t comp_sensor_count,
628 get_sensor_state_field *field,
629 struct pldm_msg *msg)
630{
George Liub7095ff2021-06-14 16:01:57 +0800631 if (msg == NULL) {
632 return PLDM_ERROR_INVALID_DATA;
633 }
Jolie Ku3557bad2020-03-02 16:22:57 +0800634
George Liub7095ff2021-06-14 16:01:57 +0800635 if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
636 return PLDM_ERROR_INVALID_DATA;
637 }
638
639 struct pldm_header_info header = {0};
Jolie Ku3557bad2020-03-02 16:22:57 +0800640 header.msg_type = PLDM_RESPONSE;
641 header.instance = instance_id;
642 header.pldm_type = PLDM_PLATFORM;
643 header.command = PLDM_GET_STATE_SENSOR_READINGS;
644
George Liub7095ff2021-06-14 16:01:57 +0800645 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
646 if (rc != PLDM_SUCCESS) {
Jolie Ku3557bad2020-03-02 16:22:57 +0800647 return rc;
648 }
649
Jolie Ku3557bad2020-03-02 16:22:57 +0800650 struct pldm_get_state_sensor_readings_resp *response =
651 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
652
653 response->completion_code = completion_code;
654 response->comp_sensor_count = comp_sensor_count;
655 memcpy(response->field, field,
656 (sizeof(get_sensor_state_field) * comp_sensor_count));
657
George Liub7095ff2021-06-14 16:01:57 +0800658 return PLDM_SUCCESS;
Jolie Ku3557bad2020-03-02 16:22:57 +0800659}
660
661int encode_get_state_sensor_readings_req(uint8_t instance_id,
662 uint16_t sensor_id,
663 bitfield8_t sensor_rearm,
664 uint8_t reserved, struct pldm_msg *msg)
665{
George Liub7095ff2021-06-14 16:01:57 +0800666 if (msg == NULL) {
667 return PLDM_ERROR_INVALID_DATA;
668 }
Jolie Ku3557bad2020-03-02 16:22:57 +0800669
George Liub7095ff2021-06-14 16:01:57 +0800670 struct pldm_header_info header = {0};
Jolie Ku3557bad2020-03-02 16:22:57 +0800671 header.msg_type = PLDM_REQUEST;
672 header.instance = instance_id;
673 header.pldm_type = PLDM_PLATFORM;
674 header.command = PLDM_GET_STATE_SENSOR_READINGS;
675
George Liub7095ff2021-06-14 16:01:57 +0800676 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
677 if (rc != PLDM_SUCCESS) {
Jolie Ku3557bad2020-03-02 16:22:57 +0800678 return rc;
679 }
680
681 struct pldm_get_state_sensor_readings_req *request =
682 (struct pldm_get_state_sensor_readings_req *)msg->payload;
683
684 request->sensor_id = htole16(sensor_id);
685 request->reserved = reserved;
686 request->sensor_rearm = sensor_rearm;
687
688 return PLDM_SUCCESS;
689}
690
691int decode_get_state_sensor_readings_resp(const struct pldm_msg *msg,
692 size_t payload_length,
693 uint8_t *completion_code,
694 uint8_t *comp_sensor_count,
695 get_sensor_state_field *field)
696{
697 if (msg == NULL || completion_code == NULL ||
698 comp_sensor_count == NULL || field == NULL) {
699 return PLDM_ERROR_INVALID_DATA;
700 }
701
702 *completion_code = msg->payload[0];
703 if (PLDM_SUCCESS != *completion_code) {
704 return PLDM_SUCCESS;
705 }
706
Jolie Ku3557bad2020-03-02 16:22:57 +0800707 struct pldm_get_state_sensor_readings_resp *response =
708 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
709
710 if (response->comp_sensor_count < 0x1 ||
711 response->comp_sensor_count > 0x8) {
712 return PLDM_ERROR_INVALID_DATA;
713 }
George Liu38eaa272021-01-21 19:30:15 +0800714
715 if (payload_length >
716 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
717 sizeof(get_sensor_state_field) * response->comp_sensor_count) {
Jolie Ku3557bad2020-03-02 16:22:57 +0800718 return PLDM_ERROR_INVALID_LENGTH;
719 }
George Liu38eaa272021-01-21 19:30:15 +0800720
Jolie Ku3557bad2020-03-02 16:22:57 +0800721 *comp_sensor_count = response->comp_sensor_count;
722
723 memcpy(field, response->field,
724 (sizeof(get_sensor_state_field) * (*comp_sensor_count)));
725
726 return PLDM_SUCCESS;
727}
728
729int decode_get_state_sensor_readings_req(const struct pldm_msg *msg,
730 size_t payload_length,
731 uint16_t *sensor_id,
732 bitfield8_t *sensor_rearm,
733 uint8_t *reserved)
734{
735 if (msg == NULL || sensor_id == NULL || sensor_rearm == NULL) {
736 return PLDM_ERROR_INVALID_DATA;
737 }
738
739 if (payload_length != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES) {
740 return PLDM_ERROR_INVALID_LENGTH;
741 }
742
743 struct pldm_get_state_sensor_readings_req *request =
744 (struct pldm_get_state_sensor_readings_req *)msg->payload;
745
746 *sensor_id = le16toh(request->sensor_id);
747 *reserved = request->reserved;
748 memcpy(&(sensor_rearm->byte), &(request->sensor_rearm.byte),
749 sizeof(request->sensor_rearm.byte));
750
751 return PLDM_SUCCESS;
752}
Zahed Hossaind4abab12020-02-06 03:36:43 -0600753
Zach Clarkb728eee2020-06-18 10:01:31 -0500754int encode_sensor_event_data(
755 struct pldm_sensor_event_data *const event_data,
756 const size_t event_data_size, const uint16_t sensor_id,
757 const enum sensor_event_class_states sensor_event_class,
758 const uint8_t sensor_offset, const uint8_t event_state,
759 const uint8_t previous_event_state, size_t *const actual_event_data_size)
760{
761 *actual_event_data_size =
762 (sizeof(*event_data) - sizeof(event_data->event_class) +
763 sizeof(struct pldm_sensor_event_state_sensor_state));
764
765 if (!event_data) {
766 return PLDM_SUCCESS;
767 }
768
769 if (event_data_size < *actual_event_data_size) {
770 *actual_event_data_size = 0;
771 return PLDM_ERROR_INVALID_LENGTH;
772 }
773
Christian Geddes1aad2f92021-02-08 10:03:43 -0600774 event_data->sensor_id = htole16(sensor_id);
Zach Clarkb728eee2020-06-18 10:01:31 -0500775 event_data->sensor_event_class_type = sensor_event_class;
776
777 struct pldm_sensor_event_state_sensor_state *const state_data =
778 (struct pldm_sensor_event_state_sensor_state *)
779 event_data->event_class;
780
781 state_data->sensor_offset = sensor_offset;
782 state_data->event_state = event_state;
783 state_data->previous_event_state = previous_event_state;
784
785 return PLDM_SUCCESS;
786}
787
Zahed Hossaind4abab12020-02-06 03:36:43 -0600788int decode_platform_event_message_req(const struct pldm_msg *msg,
789 size_t payload_length,
790 uint8_t *format_version, uint8_t *tid,
791 uint8_t *event_class,
792 size_t *event_data_offset)
793{
794
795 if (msg == NULL || format_version == NULL || tid == NULL ||
796 event_class == NULL || event_data_offset == NULL) {
797 return PLDM_ERROR_INVALID_DATA;
798 }
799
800 if (payload_length <= PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES) {
801 return PLDM_ERROR_INVALID_LENGTH;
802 }
803 struct pldm_platform_event_message_req *response =
804 (struct pldm_platform_event_message_req *)msg->payload;
805
806 *format_version = response->format_version;
807 *tid = response->tid;
808 *event_class = response->event_class;
809 *event_data_offset =
810 sizeof(*format_version) + sizeof(*tid) + sizeof(*event_class);
811
812 return PLDM_SUCCESS;
813}
814
815int encode_platform_event_message_resp(uint8_t instance_id,
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500816 uint8_t completion_code,
817 uint8_t platform_event_status,
Zahed Hossaind4abab12020-02-06 03:36:43 -0600818 struct pldm_msg *msg)
819{
Zahed Hossaind4abab12020-02-06 03:36:43 -0600820 if (msg == NULL) {
821 return PLDM_ERROR_INVALID_DATA;
822 }
823
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500824 if (platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
825 return PLDM_ERROR_INVALID_DATA;
826 }
827
Zahed Hossaind4abab12020-02-06 03:36:43 -0600828 struct pldm_header_info header = {0};
829 header.msg_type = PLDM_RESPONSE;
830 header.instance = instance_id;
831 header.pldm_type = PLDM_PLATFORM;
832 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
833
George Liub7095ff2021-06-14 16:01:57 +0800834 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
835 if (rc != PLDM_SUCCESS) {
Zahed Hossaind4abab12020-02-06 03:36:43 -0600836 return rc;
837 }
George Liub7095ff2021-06-14 16:01:57 +0800838
839 struct pldm_platform_event_message_resp *response =
840 (struct pldm_platform_event_message_resp *)msg->payload;
841 response->completion_code = completion_code;
842 response->platform_event_status = platform_event_status;
843
Zahed Hossaind4abab12020-02-06 03:36:43 -0600844 return PLDM_SUCCESS;
845}
Zahed Hossain1c861712020-03-04 08:55:19 -0600846
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500847int encode_platform_event_message_req(
848 uint8_t instance_id, uint8_t format_version, uint8_t tid,
849 uint8_t event_class, const uint8_t *event_data, size_t event_data_length,
850 struct pldm_msg *msg, size_t payload_length)
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500851
852{
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500853 if (format_version != 1) {
854 return PLDM_ERROR_INVALID_DATA;
855 }
856
857 if (msg == NULL || event_data == NULL) {
858 return PLDM_ERROR_INVALID_DATA;
859 }
860
861 if (event_data_length == 0) {
862 return PLDM_ERROR_INVALID_DATA;
863 }
864
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500865 if (payload_length !=
866 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + event_data_length) {
867 return PLDM_ERROR_INVALID_LENGTH;
868 }
869
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500870 if (event_class > PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT &&
871 !(event_class >= 0xF0 && event_class <= 0xFE)) {
872 return PLDM_ERROR_INVALID_DATA;
873 }
874
George Liub7095ff2021-06-14 16:01:57 +0800875 struct pldm_header_info header = {0};
876 header.msg_type = PLDM_REQUEST;
877 header.instance = instance_id;
878 header.pldm_type = PLDM_PLATFORM;
879 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
880
881 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
882 if (rc != PLDM_SUCCESS) {
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500883 return rc;
884 }
885
886 struct pldm_platform_event_message_req *request =
887 (struct pldm_platform_event_message_req *)msg->payload;
888 request->format_version = format_version;
889 request->tid = tid;
890 request->event_class = event_class;
891 memcpy(request->event_data, event_data, event_data_length);
892
893 return PLDM_SUCCESS;
894}
895
896int decode_platform_event_message_resp(const struct pldm_msg *msg,
897 size_t payload_length,
898 uint8_t *completion_code,
899 uint8_t *platform_event_status)
900{
901 if (msg == NULL || completion_code == NULL ||
902 platform_event_status == NULL) {
903 return PLDM_ERROR_INVALID_DATA;
904 }
905
906 *completion_code = msg->payload[0];
907 if (PLDM_SUCCESS != *completion_code) {
908 return PLDM_SUCCESS;
909 }
910 if (payload_length != PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES) {
911 return PLDM_ERROR_INVALID_LENGTH;
912 }
913
914 struct pldm_platform_event_message_resp *response =
915 (struct pldm_platform_event_message_resp *)msg->payload;
916 *platform_event_status = response->platform_event_status;
917
918 if (*platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
919 return PLDM_ERROR_INVALID_DATA;
920 }
921
922 return PLDM_SUCCESS;
923}
924
Zahed Hossain1c861712020-03-04 08:55:19 -0600925int decode_sensor_event_data(const uint8_t *event_data,
926 size_t event_data_length, uint16_t *sensor_id,
927 uint8_t *sensor_event_class_type,
928 size_t *event_class_data_offset)
929{
930 if (event_data == NULL) {
931 return PLDM_ERROR_INVALID_DATA;
932 }
933 if (event_data_length < PLDM_SENSOR_EVENT_DATA_MIN_LENGTH) {
934 return PLDM_ERROR_INVALID_LENGTH;
935 }
936
937 size_t event_class_data_length =
938 event_data_length - PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
939
940 struct pldm_sensor_event_data *sensor_event_data =
941 (struct pldm_sensor_event_data *)event_data;
942 *sensor_id = sensor_event_data->sensor_id;
943 *sensor_event_class_type = sensor_event_data->sensor_event_class_type;
944 if (sensor_event_data->sensor_event_class_type ==
945 PLDM_SENSOR_OP_STATE) {
946 if (event_class_data_length !=
947 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
948 return PLDM_ERROR_INVALID_LENGTH;
949 }
950 } else if (sensor_event_data->sensor_event_class_type ==
951 PLDM_STATE_SENSOR_STATE) {
952 if (event_class_data_length !=
953 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
954 return PLDM_ERROR_INVALID_LENGTH;
955 }
956 } else if (sensor_event_data->sensor_event_class_type ==
957 PLDM_NUMERIC_SENSOR_STATE) {
958 if (event_class_data_length <
959 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
960 event_class_data_length >
961 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
962 return PLDM_ERROR_INVALID_LENGTH;
963 }
964 } else {
965 return PLDM_ERROR_INVALID_DATA;
966 }
967 *event_class_data_offset =
968 sizeof(*sensor_id) + sizeof(*sensor_event_class_type);
969 return PLDM_SUCCESS;
970}
971
972int decode_sensor_op_data(const uint8_t *sensor_data, size_t sensor_data_length,
973 uint8_t *present_op_state, uint8_t *previous_op_state)
974{
975 if (sensor_data == NULL || present_op_state == NULL ||
976 previous_op_state == NULL) {
977 return PLDM_ERROR_INVALID_DATA;
978 }
979 if (sensor_data_length !=
980 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
981 return PLDM_ERROR_INVALID_LENGTH;
982 }
983
984 struct pldm_sensor_event_sensor_op_state *sensor_op_data =
985 (struct pldm_sensor_event_sensor_op_state *)sensor_data;
986 *present_op_state = sensor_op_data->present_op_state;
987 *previous_op_state = sensor_op_data->previous_op_state;
988 return PLDM_SUCCESS;
989}
990
991int decode_state_sensor_data(const uint8_t *sensor_data,
992 size_t sensor_data_length, uint8_t *sensor_offset,
993 uint8_t *event_state,
994 uint8_t *previous_event_state)
995{
996 if (sensor_data == NULL || sensor_offset == NULL ||
997 event_state == NULL || previous_event_state == NULL) {
998 return PLDM_ERROR_INVALID_DATA;
999 }
1000 if (sensor_data_length !=
1001 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
1002 return PLDM_ERROR_INVALID_LENGTH;
1003 }
1004
1005 struct pldm_sensor_event_state_sensor_state *sensor_state_data =
1006 (struct pldm_sensor_event_state_sensor_state *)sensor_data;
1007 *sensor_offset = sensor_state_data->sensor_offset;
1008 *event_state = sensor_state_data->event_state;
1009 *previous_event_state = sensor_state_data->previous_event_state;
1010 return PLDM_SUCCESS;
1011}
1012
1013int decode_numeric_sensor_data(const uint8_t *sensor_data,
1014 size_t sensor_data_length, uint8_t *event_state,
1015 uint8_t *previous_event_state,
1016 uint8_t *sensor_data_size,
1017 uint32_t *present_reading)
1018{
1019 if (sensor_data == NULL || sensor_data_size == NULL ||
1020 event_state == NULL || previous_event_state == NULL ||
1021 present_reading == NULL) {
1022 return PLDM_ERROR_INVALID_DATA;
1023 }
1024 if (sensor_data_length <
1025 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
1026 sensor_data_length >
1027 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
1028 return PLDM_ERROR_INVALID_LENGTH;
1029 }
1030 struct pldm_sensor_event_numeric_sensor_state *numeric_sensor_data =
1031 (struct pldm_sensor_event_numeric_sensor_state *)sensor_data;
1032 *event_state = numeric_sensor_data->event_state;
1033 *previous_event_state = numeric_sensor_data->previous_event_state;
1034 *sensor_data_size = numeric_sensor_data->sensor_data_size;
1035 uint8_t *present_reading_ptr = numeric_sensor_data->present_reading;
1036
1037 switch (*sensor_data_size) {
1038 case PLDM_SENSOR_DATA_SIZE_UINT8:
1039 case PLDM_SENSOR_DATA_SIZE_SINT8:
1040 if (sensor_data_length !=
1041 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_8BIT_DATA_LENGTH) {
1042 return PLDM_ERROR_INVALID_LENGTH;
1043 }
1044 *present_reading = present_reading_ptr[0];
1045 break;
1046 case PLDM_SENSOR_DATA_SIZE_UINT16:
1047 case PLDM_SENSOR_DATA_SIZE_SINT16:
1048 if (sensor_data_length !=
1049 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH) {
1050 return PLDM_ERROR_INVALID_LENGTH;
1051 }
1052 *present_reading = le16toh(present_reading_ptr[1] |
1053 (present_reading_ptr[0] << 8));
1054 break;
1055 case PLDM_SENSOR_DATA_SIZE_UINT32:
1056 case PLDM_SENSOR_DATA_SIZE_SINT32:
1057 if (sensor_data_length !=
1058 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH) {
1059 return PLDM_ERROR_INVALID_LENGTH;
1060 }
1061 *present_reading = le32toh(present_reading_ptr[3] |
1062 (present_reading_ptr[2] << 8) |
1063 (present_reading_ptr[1] << 16) |
1064 (present_reading_ptr[0] << 24));
1065 break;
1066 default:
1067 return PLDM_ERROR_INVALID_DATA;
1068 }
1069 return PLDM_SUCCESS;
1070}
Jolie Ku6787f172020-03-19 11:15:53 +08001071
1072int encode_get_numeric_effecter_value_req(uint8_t instance_id,
1073 uint16_t effecter_id,
1074 struct pldm_msg *msg)
1075{
Jolie Ku6787f172020-03-19 11:15:53 +08001076 if (msg == NULL) {
1077 return PLDM_ERROR_INVALID_DATA;
1078 }
1079
George Liub7095ff2021-06-14 16:01:57 +08001080 struct pldm_header_info header = {0};
Jolie Ku6787f172020-03-19 11:15:53 +08001081 header.msg_type = PLDM_REQUEST;
1082 header.instance = instance_id;
1083 header.pldm_type = PLDM_PLATFORM;
1084 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1085
George Liub7095ff2021-06-14 16:01:57 +08001086 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1087 if (rc != PLDM_SUCCESS) {
Jolie Ku6787f172020-03-19 11:15:53 +08001088 return rc;
1089 }
1090
George Liub7095ff2021-06-14 16:01:57 +08001091 struct pldm_get_numeric_effecter_value_req *request =
1092 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
Jolie Ku6787f172020-03-19 11:15:53 +08001093 request->effecter_id = htole16(effecter_id);
1094
1095 return PLDM_SUCCESS;
1096}
1097
1098int encode_get_numeric_effecter_value_resp(
1099 uint8_t instance_id, uint8_t completion_code, uint8_t effecter_data_size,
1100 uint8_t effecter_oper_state, uint8_t *pending_value, uint8_t *present_value,
1101 struct pldm_msg *msg, size_t payload_length)
1102{
Jolie Ku6787f172020-03-19 11:15:53 +08001103 if (msg == NULL || pending_value == NULL || present_value == NULL) {
1104 return PLDM_ERROR_INVALID_DATA;
1105 }
1106
1107 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1108 return PLDM_ERROR_INVALID_DATA;
1109 }
1110
1111 if (effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1112 return PLDM_ERROR_INVALID_DATA;
1113 }
1114
George Liub7095ff2021-06-14 16:01:57 +08001115 struct pldm_header_info header = {0};
Jolie Ku6787f172020-03-19 11:15:53 +08001116 header.msg_type = PLDM_RESPONSE;
1117 header.instance = instance_id;
1118 header.pldm_type = PLDM_PLATFORM;
1119 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1120
George Liub7095ff2021-06-14 16:01:57 +08001121 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1122 if (rc != PLDM_SUCCESS) {
Jolie Ku6787f172020-03-19 11:15:53 +08001123 return rc;
1124 }
1125
1126 struct pldm_get_numeric_effecter_value_resp *response =
1127 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
1128
1129 response->completion_code = completion_code;
1130 response->effecter_data_size = effecter_data_size;
1131 response->effecter_oper_state = effecter_oper_state;
1132
1133 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1134 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1135 if (payload_length !=
1136 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1137 return PLDM_ERROR_INVALID_LENGTH;
1138 }
1139 response->pending_and_present_values[0] = *pending_value;
1140 response->pending_and_present_values[1] = *present_value;
1141
1142 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1143 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1144 if (payload_length !=
1145 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1146 return PLDM_ERROR_INVALID_LENGTH;
1147 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001148 uint16_t val_pending = *(uint16_t *)pending_value;
1149 val_pending = htole16(val_pending);
1150 memcpy(response->pending_and_present_values, &val_pending,
1151 sizeof(uint16_t));
1152 uint16_t val_present = *(uint16_t *)present_value;
1153 val_present = htole16(val_present);
1154 memcpy(
1155 (response->pending_and_present_values + sizeof(uint16_t)),
1156 &val_present, sizeof(uint16_t));
Jolie Ku6787f172020-03-19 11:15:53 +08001157
1158 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1159 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1160 if (payload_length !=
1161 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1162 return PLDM_ERROR_INVALID_LENGTH;
1163 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001164 uint32_t val_pending = *(uint32_t *)pending_value;
1165 val_pending = htole32(val_pending);
1166 memcpy(response->pending_and_present_values, &val_pending,
1167 sizeof(uint32_t));
1168 uint32_t val_present = *(uint32_t *)present_value;
1169 val_present = htole32(val_present);
1170 memcpy(
1171 (response->pending_and_present_values + sizeof(uint32_t)),
1172 &val_present, sizeof(uint32_t));
Jolie Ku6787f172020-03-19 11:15:53 +08001173 }
1174 return PLDM_SUCCESS;
1175}
1176
1177int decode_get_numeric_effecter_value_req(const struct pldm_msg *msg,
1178 size_t payload_length,
1179 uint16_t *effecter_id)
1180{
1181 if (msg == NULL || effecter_id == NULL) {
1182 return PLDM_ERROR_INVALID_DATA;
1183 }
1184
1185 if (payload_length != PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES) {
1186 return PLDM_ERROR_INVALID_LENGTH;
1187 }
1188
1189 struct pldm_get_numeric_effecter_value_req *request =
1190 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
1191
1192 *effecter_id = le16toh(request->effecter_id);
1193
1194 return PLDM_SUCCESS;
1195}
1196
1197int decode_get_numeric_effecter_value_resp(
1198 const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
1199 uint8_t *effecter_data_size, uint8_t *effecter_oper_state,
1200 uint8_t *pending_value, uint8_t *present_value)
1201{
1202 if (msg == NULL || effecter_data_size == NULL ||
1203 effecter_oper_state == NULL || pending_value == NULL ||
1204 present_value == NULL) {
1205 return PLDM_ERROR_INVALID_DATA;
1206 }
1207
1208 *completion_code = msg->payload[0];
1209 if (PLDM_SUCCESS != *completion_code) {
1210 return PLDM_SUCCESS;
1211 }
1212
1213 if (payload_length < PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1214 return PLDM_ERROR_INVALID_LENGTH;
1215 }
1216
1217 struct pldm_get_numeric_effecter_value_resp *response =
1218 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
1219
1220 *effecter_data_size = response->effecter_data_size;
1221 *effecter_oper_state = response->effecter_oper_state;
1222
1223 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1224 return PLDM_ERROR_INVALID_DATA;
1225 }
1226
1227 if (*effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1228 return PLDM_ERROR_INVALID_DATA;
1229 }
1230
1231 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1232 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1233 if (payload_length !=
1234 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1235 return PLDM_ERROR_INVALID_LENGTH;
1236 }
1237 memcpy(pending_value, response->pending_and_present_values, 1);
1238 memcpy(present_value, &response->pending_and_present_values[1],
1239 1);
1240
1241 } else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1242 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1243 if (payload_length !=
1244 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1245 return PLDM_ERROR_INVALID_LENGTH;
1246 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001247 memcpy(pending_value, response->pending_and_present_values,
1248 sizeof(uint16_t));
1249 uint16_t *val_pending = (uint16_t *)pending_value;
1250 *val_pending = le16toh(*val_pending);
1251 memcpy(
1252 present_value,
1253 (response->pending_and_present_values + sizeof(uint16_t)),
1254 sizeof(uint16_t));
1255 uint16_t *val_present = (uint16_t *)present_value;
1256 *val_present = le16toh(*val_present);
Jolie Ku6787f172020-03-19 11:15:53 +08001257
1258 } else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1259 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1260 if (payload_length !=
1261 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1262 return PLDM_ERROR_INVALID_LENGTH;
1263 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001264 memcpy(pending_value, response->pending_and_present_values,
1265 sizeof(uint32_t));
1266 uint32_t *val_pending = (uint32_t *)pending_value;
1267 *val_pending = le32toh(*val_pending);
1268 memcpy(
1269 present_value,
1270 (response->pending_and_present_values + sizeof(uint32_t)),
1271 sizeof(uint32_t));
1272 uint32_t *val_present = (uint32_t *)present_value;
1273 *val_present = le32toh(*val_present);
Jolie Ku6787f172020-03-19 11:15:53 +08001274 }
1275 return PLDM_SUCCESS;
Zach Clark3dba2bf2020-03-31 10:58:03 -05001276}
Zahed Hossain9be087c2020-04-02 02:26:41 -05001277
Zach Clark36ad1f52020-04-13 07:04:15 -05001278int encode_pldm_pdr_repository_chg_event_data(
1279 uint8_t event_data_format, uint8_t number_of_change_records,
1280 const uint8_t *event_data_operations,
1281 const uint8_t *numbers_of_change_entries,
1282 const uint32_t *const *change_entries,
1283 struct pldm_pdr_repository_chg_event_data *event_data,
1284 size_t *actual_change_records_size, size_t max_change_records_size)
1285{
1286 if (event_data_operations == NULL ||
1287 numbers_of_change_entries == NULL || change_entries == NULL) {
1288 return PLDM_ERROR_INVALID_DATA;
1289 }
1290
1291 size_t expected_size =
1292 sizeof(event_data_format) + sizeof(number_of_change_records);
1293
1294 expected_size +=
1295 sizeof(*event_data_operations) * number_of_change_records;
1296 expected_size +=
1297 sizeof(*numbers_of_change_entries) * number_of_change_records;
1298
1299 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1300 expected_size +=
1301 sizeof(*change_entries[0]) * numbers_of_change_entries[i];
1302 }
1303
1304 *actual_change_records_size = expected_size;
1305
1306 if (event_data == NULL) {
1307 return PLDM_SUCCESS;
1308 }
1309
1310 if (max_change_records_size < expected_size) {
1311 return PLDM_ERROR_INVALID_LENGTH;
1312 }
1313
1314 event_data->event_data_format = event_data_format;
1315 event_data->number_of_change_records = number_of_change_records;
1316
1317 struct pldm_pdr_repository_change_record_data *record_data =
1318 (struct pldm_pdr_repository_change_record_data *)
1319 event_data->change_records;
1320
1321 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1322 record_data->event_data_operation = event_data_operations[i];
1323 record_data->number_of_change_entries =
1324 numbers_of_change_entries[i];
1325
1326 for (uint8_t j = 0; j < record_data->number_of_change_entries;
1327 ++j) {
1328 record_data->change_entry[j] =
1329 htole32(change_entries[i][j]);
1330 }
1331
1332 record_data = (struct pldm_pdr_repository_change_record_data
1333 *)(record_data->change_entry +
1334 record_data->number_of_change_entries);
1335 }
1336
1337 return PLDM_SUCCESS;
1338}
1339
Zahed Hossain9be087c2020-04-02 02:26:41 -05001340int decode_pldm_pdr_repository_chg_event_data(const uint8_t *event_data,
1341 size_t event_data_size,
1342 uint8_t *event_data_format,
1343 uint8_t *number_of_change_records,
1344 size_t *change_record_data_offset)
1345{
1346 if (event_data == NULL || event_data_format == NULL ||
1347 number_of_change_records == NULL ||
1348 change_record_data_offset == NULL) {
1349 return PLDM_ERROR_INVALID_DATA;
1350 }
1351 if (event_data_size < PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH) {
1352 return PLDM_ERROR_INVALID_LENGTH;
1353 }
1354
1355 struct pldm_pdr_repository_chg_event_data
1356 *pdr_repository_chg_event_data =
1357 (struct pldm_pdr_repository_chg_event_data *)event_data;
1358
1359 *event_data_format = pdr_repository_chg_event_data->event_data_format;
1360 *number_of_change_records =
1361 pdr_repository_chg_event_data->number_of_change_records;
1362 *change_record_data_offset =
1363 sizeof(*event_data_format) + sizeof(*number_of_change_records);
1364
1365 return PLDM_SUCCESS;
1366}
1367
1368int decode_pldm_pdr_repository_change_record_data(
1369 const uint8_t *change_record_data, size_t change_record_data_size,
1370 uint8_t *event_data_operation, uint8_t *number_of_change_entries,
1371 size_t *change_entry_data_offset)
1372{
1373 if (change_record_data == NULL || event_data_operation == NULL ||
1374 number_of_change_entries == NULL ||
1375 change_entry_data_offset == NULL) {
1376 return PLDM_ERROR_INVALID_DATA;
1377 }
1378 if (change_record_data_size <
1379 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH) {
1380 return PLDM_ERROR_INVALID_LENGTH;
1381 }
1382
1383 struct pldm_pdr_repository_change_record_data
1384 *pdr_repository_change_record_data =
1385 (struct pldm_pdr_repository_change_record_data *)
1386 change_record_data;
1387
1388 *event_data_operation =
1389 pdr_repository_change_record_data->event_data_operation;
1390 *number_of_change_entries =
1391 pdr_repository_change_record_data->number_of_change_entries;
1392 *change_entry_data_offset =
1393 sizeof(*event_data_operation) + sizeof(*number_of_change_entries);
1394
1395 return PLDM_SUCCESS;
1396}
Jolie Kuf798c8f2020-04-14 11:18:06 +08001397
1398int encode_get_sensor_reading_req(uint8_t instance_id, uint16_t sensor_id,
1399 uint8_t rearm_event_state,
1400 struct pldm_msg *msg)
1401{
George Liub7095ff2021-06-14 16:01:57 +08001402 if (msg == NULL) {
1403 return PLDM_ERROR_INVALID_DATA;
1404 }
Jolie Kuf798c8f2020-04-14 11:18:06 +08001405
George Liub7095ff2021-06-14 16:01:57 +08001406 struct pldm_header_info header = {0};
Jolie Kuf798c8f2020-04-14 11:18:06 +08001407 header.msg_type = PLDM_REQUEST;
1408 header.instance = instance_id;
1409 header.pldm_type = PLDM_PLATFORM;
1410 header.command = PLDM_GET_SENSOR_READING;
1411
George Liub7095ff2021-06-14 16:01:57 +08001412 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1413 if (rc != PLDM_SUCCESS) {
Jolie Kuf798c8f2020-04-14 11:18:06 +08001414 return rc;
1415 }
1416
1417 struct pldm_get_sensor_reading_req *request =
1418 (struct pldm_get_sensor_reading_req *)msg->payload;
1419
1420 request->sensor_id = htole16(sensor_id);
1421 request->rearm_event_state = rearm_event_state;
1422
1423 return PLDM_SUCCESS;
1424}
1425
1426int decode_get_sensor_reading_resp(
1427 const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
1428 uint8_t *sensor_data_size, uint8_t *sensor_operational_state,
1429 uint8_t *sensor_event_message_enable, uint8_t *present_state,
1430 uint8_t *previous_state, uint8_t *event_state, uint8_t *present_reading)
1431{
1432 if (msg == NULL || completion_code == NULL ||
1433 sensor_data_size == NULL || sensor_operational_state == NULL ||
1434 sensor_event_message_enable == NULL || present_state == NULL ||
1435 previous_state == NULL || event_state == NULL ||
1436 present_reading == NULL) {
1437 return PLDM_ERROR_INVALID_DATA;
1438 }
1439
1440 *completion_code = msg->payload[0];
1441 if (PLDM_SUCCESS != *completion_code) {
1442 return PLDM_SUCCESS;
1443 }
1444
1445 if (payload_length < PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1446 return PLDM_ERROR_INVALID_LENGTH;
1447 }
1448
1449 struct pldm_get_sensor_reading_resp *response =
1450 (struct pldm_get_sensor_reading_resp *)msg->payload;
1451
1452 if (response->sensor_data_size > PLDM_SENSOR_DATA_SIZE_SINT32) {
1453 return PLDM_ERROR_INVALID_DATA;
1454 }
1455 if (response->sensor_data_size > *sensor_data_size) {
1456 return PLDM_ERROR_INVALID_LENGTH;
1457 }
1458
1459 *sensor_data_size = response->sensor_data_size;
1460 *sensor_operational_state = response->sensor_operational_state;
1461 *sensor_event_message_enable = response->sensor_event_message_enable;
1462 *present_state = response->present_state;
1463 *previous_state = response->previous_state;
1464 *event_state = response->event_state;
1465
1466 if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1467 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1468 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1469 return PLDM_ERROR_INVALID_LENGTH;
1470 }
1471 *present_reading = response->present_reading[0];
1472
1473 } else if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1474 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1475 if (payload_length !=
1476 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
1477 return PLDM_ERROR_INVALID_LENGTH;
1478 }
1479 memcpy(present_reading, response->present_reading, 2);
1480 uint16_t *val = (uint16_t *)(present_reading);
1481 *val = le16toh(*val);
1482
1483 } else if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1484 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1485 if (payload_length !=
1486 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
1487 return PLDM_ERROR_INVALID_LENGTH;
1488 }
1489 memcpy(present_reading, response->present_reading, 4);
1490 uint32_t *val = (uint32_t *)(present_reading);
1491 *val = le32toh(*val);
1492 }
1493
1494 return PLDM_SUCCESS;
1495}
1496
1497int encode_get_sensor_reading_resp(
1498 uint8_t instance_id, uint8_t completion_code, uint8_t sensor_data_size,
1499 uint8_t sensor_operational_state, uint8_t sensor_event_message_enable,
1500 uint8_t present_state, uint8_t previous_state, uint8_t event_state,
1501 uint8_t *present_reading, struct pldm_msg *msg, size_t payload_length)
1502{
Jolie Kuf798c8f2020-04-14 11:18:06 +08001503 if (msg == NULL || present_reading == NULL) {
1504 return PLDM_ERROR_INVALID_DATA;
1505 }
1506
1507 if (sensor_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1508 return PLDM_ERROR_INVALID_DATA;
1509 }
1510
George Liub7095ff2021-06-14 16:01:57 +08001511 struct pldm_header_info header = {0};
Jolie Kuf798c8f2020-04-14 11:18:06 +08001512 header.msg_type = PLDM_RESPONSE;
1513 header.instance = instance_id;
1514 header.pldm_type = PLDM_PLATFORM;
1515 header.command = PLDM_GET_SENSOR_READING;
1516
George Liub7095ff2021-06-14 16:01:57 +08001517 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1518 if (rc != PLDM_SUCCESS) {
Jolie Kuf798c8f2020-04-14 11:18:06 +08001519 return rc;
1520 }
1521
1522 struct pldm_get_sensor_reading_resp *response =
1523 (struct pldm_get_sensor_reading_resp *)msg->payload;
1524
1525 response->completion_code = completion_code;
1526 response->sensor_data_size = sensor_data_size;
1527 response->sensor_operational_state = sensor_operational_state;
1528 response->sensor_event_message_enable = sensor_event_message_enable;
1529 response->present_state = present_state;
1530 response->previous_state = previous_state;
1531 response->event_state = event_state;
1532
1533 if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1534 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1535 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1536 return PLDM_ERROR_INVALID_LENGTH;
1537 }
1538 response->present_reading[0] = *present_reading;
1539
1540 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1541 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1542 if (payload_length !=
1543 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
1544 return PLDM_ERROR_INVALID_LENGTH;
1545 }
1546 uint16_t val = *(uint16_t *)present_reading;
1547 val = htole16(val);
1548 memcpy(response->present_reading, &val, 2);
1549
1550 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1551 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1552 if (payload_length !=
1553 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
1554 return PLDM_ERROR_INVALID_LENGTH;
1555 }
1556 uint32_t val = *(uint32_t *)present_reading;
1557 val = htole32(val);
1558 memcpy(response->present_reading, &val, 4);
1559 }
1560
1561 return PLDM_SUCCESS;
1562}
1563
1564int decode_get_sensor_reading_req(const struct pldm_msg *msg,
1565 size_t payload_length, uint16_t *sensor_id,
1566 uint8_t *rearm_event_state)
1567{
1568 if (msg == NULL || sensor_id == NULL || rearm_event_state == NULL) {
1569 return PLDM_ERROR_INVALID_DATA;
1570 }
1571
1572 if (payload_length != PLDM_GET_SENSOR_READING_REQ_BYTES) {
1573 return PLDM_ERROR_INVALID_LENGTH;
1574 }
1575
1576 struct pldm_get_sensor_reading_req *request =
1577 (struct pldm_get_sensor_reading_req *)msg->payload;
1578
1579 *sensor_id = le16toh(request->sensor_id);
1580 *rearm_event_state = request->rearm_event_state;
1581
1582 return PLDM_SUCCESS;
1583}
Sagar Srinivas535bccd2021-03-24 12:18:26 -05001584
1585int encode_set_event_receiver_req(uint8_t instance_id,
1586 uint8_t event_message_global_enable,
1587 uint8_t transport_protocol_type,
1588 uint8_t event_receiver_address_info,
1589 uint16_t heartbeat_timer,
1590 struct pldm_msg *msg)
1591{
Sagar Srinivas535bccd2021-03-24 12:18:26 -05001592 if (msg == NULL) {
1593 return PLDM_ERROR_INVALID_DATA;
1594 }
1595
George Liub7095ff2021-06-14 16:01:57 +08001596 if (transport_protocol_type != PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP) {
1597 return PLDM_ERROR_INVALID_DATA;
1598 }
1599
1600 struct pldm_header_info header = {0};
Sagar Srinivas535bccd2021-03-24 12:18:26 -05001601 header.msg_type = PLDM_REQUEST;
1602 header.instance = instance_id;
1603 header.pldm_type = PLDM_PLATFORM;
1604 header.command = PLDM_SET_EVENT_RECEIVER;
1605
George Liub7095ff2021-06-14 16:01:57 +08001606 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1607 if (rc != PLDM_SUCCESS) {
Sagar Srinivas535bccd2021-03-24 12:18:26 -05001608 return rc;
1609 }
1610
1611 struct pldm_set_event_receiver_req *request =
1612 (struct pldm_set_event_receiver_req *)msg->payload;
1613 request->event_message_global_enable = event_message_global_enable;
1614
Sagar Srinivas535bccd2021-03-24 12:18:26 -05001615 request->transport_protocol_type = transport_protocol_type;
1616 request->event_receiver_address_info = event_receiver_address_info;
1617
1618 if (event_message_global_enable ==
1619 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) {
1620 if (heartbeat_timer == 0) {
1621 return PLDM_ERROR_INVALID_DATA;
1622 }
1623 request->heartbeat_timer = htole16(heartbeat_timer);
1624 }
1625
1626 return PLDM_SUCCESS;
1627}
1628
1629int decode_set_event_receiver_resp(const struct pldm_msg *msg,
1630 size_t payload_length,
1631 uint8_t *completion_code)
1632{
1633 if (msg == NULL || completion_code == NULL) {
1634 return PLDM_ERROR_INVALID_DATA;
1635 }
1636
1637 *completion_code = msg->payload[0];
1638 if (PLDM_SUCCESS != *completion_code) {
1639 return PLDM_SUCCESS;
1640 }
1641
1642 if (payload_length > PLDM_SET_EVENT_RECEIVER_RESP_BYTES) {
1643 return PLDM_ERROR_INVALID_LENGTH;
1644 }
1645
1646 return PLDM_SUCCESS;
1647}
Sridevi Ramesh74187522021-04-08 09:50:22 -05001648
1649int decode_set_event_receiver_req(const struct pldm_msg *msg,
1650 size_t payload_length,
1651 uint8_t *event_message_global_enable,
1652 uint8_t *transport_protocol_type,
1653 uint8_t *event_receiver_address_info,
1654 uint16_t *heartbeat_timer)
1655
1656{
1657 if (msg == NULL || event_message_global_enable == NULL ||
1658 transport_protocol_type == NULL ||
1659 event_receiver_address_info == NULL || heartbeat_timer == NULL) {
1660 return PLDM_ERROR_INVALID_DATA;
1661 }
1662
1663 if (payload_length != PLDM_SET_EVENT_RECEIVER_REQ_BYTES) {
1664 return PLDM_ERROR_INVALID_LENGTH;
1665 }
1666
1667 struct pldm_set_event_receiver_req *request =
1668 (struct pldm_set_event_receiver_req *)msg->payload;
1669
1670 if ((*event_message_global_enable ==
1671 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) &&
1672 (*heartbeat_timer == 0)) {
1673 return PLDM_ERROR_INVALID_DATA;
1674 }
1675
1676 *event_message_global_enable = request->event_message_global_enable,
1677 *transport_protocol_type = request->transport_protocol_type,
1678 *event_receiver_address_info = request->event_receiver_address_info,
1679 *heartbeat_timer = le16toh(request->heartbeat_timer);
1680
1681 return PLDM_SUCCESS;
1682}
1683
1684int encode_set_event_receiver_resp(uint8_t instance_id, uint8_t completion_code,
1685 struct pldm_msg *msg)
1686
1687{
Sridevi Ramesh74187522021-04-08 09:50:22 -05001688 if (msg == NULL) {
1689 return PLDM_ERROR_INVALID_DATA;
1690 }
1691
George Liub7095ff2021-06-14 16:01:57 +08001692 struct pldm_header_info header = {0};
Sridevi Ramesh74187522021-04-08 09:50:22 -05001693 header.instance = instance_id;
1694 header.msg_type = PLDM_RESPONSE;
1695 header.pldm_type = PLDM_PLATFORM;
1696 header.command = PLDM_SET_EVENT_RECEIVER;
1697
George Liub7095ff2021-06-14 16:01:57 +08001698 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1699 if (rc != PLDM_SUCCESS) {
1700 return rc;
1701 }
Sridevi Ramesh74187522021-04-08 09:50:22 -05001702
George Liub7095ff2021-06-14 16:01:57 +08001703 msg->payload[0] = completion_code;
1704
1705 return PLDM_SUCCESS;
Sridevi Ramesh74187522021-04-08 09:50:22 -05001706}