blob: 812e8b72ca7672b628136a88775e7181c0dead46 [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
317int encode_get_pdr_req(uint8_t instance_id, uint32_t record_hndl,
318 uint32_t data_transfer_hndl, uint8_t transfer_op_flag,
319 uint16_t request_cnt, uint16_t record_chg_num,
320 struct pldm_msg *msg, size_t payload_length)
321{
George Liu820a9a52019-11-26 14:43:59 +0800322 if (msg == NULL) {
323 return PLDM_ERROR_INVALID_DATA;
324 }
George Liu820a9a52019-11-26 14:43:59 +0800325
326 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
327 return PLDM_ERROR_INVALID_LENGTH;
328 }
329
George Liub7095ff2021-06-14 16:01:57 +0800330 struct pldm_header_info header = {0};
331 header.msg_type = PLDM_REQUEST;
332 header.instance = instance_id;
333 header.pldm_type = PLDM_PLATFORM;
334 header.command = PLDM_GET_PDR;
335
336 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
337 if (rc != PLDM_SUCCESS) {
338 return rc;
339 }
340
341 struct pldm_get_pdr_req *request =
342 (struct pldm_get_pdr_req *)msg->payload;
George Liu820a9a52019-11-26 14:43:59 +0800343 request->record_handle = htole32(record_hndl);
344 request->data_transfer_handle = htole32(data_transfer_hndl);
345 request->transfer_op_flag = transfer_op_flag;
346 request->request_count = htole16(request_cnt);
347 request->record_change_number = htole16(record_chg_num);
348
349 return PLDM_SUCCESS;
350}
351
352int decode_get_pdr_resp(const struct pldm_msg *msg, size_t payload_length,
353 uint8_t *completion_code, uint32_t *next_record_hndl,
354 uint32_t *next_data_transfer_hndl,
355 uint8_t *transfer_flag, uint16_t *resp_cnt,
356 uint8_t *record_data, size_t record_data_length,
357 uint8_t *transfer_crc)
358{
359 if (msg == NULL || completion_code == NULL ||
360 next_record_hndl == NULL || next_data_transfer_hndl == NULL ||
Zach Clark3dba2bf2020-03-31 10:58:03 -0500361 transfer_flag == NULL || resp_cnt == NULL || transfer_crc == NULL) {
George Liu820a9a52019-11-26 14:43:59 +0800362 return PLDM_ERROR_INVALID_DATA;
363 }
364
365 *completion_code = msg->payload[0];
366 if (PLDM_SUCCESS != *completion_code) {
George Liu684a7162019-12-06 15:10:52 +0800367 return PLDM_SUCCESS;
George Liu820a9a52019-11-26 14:43:59 +0800368 }
369
370 if (payload_length < PLDM_GET_PDR_MIN_RESP_BYTES) {
371 return PLDM_ERROR_INVALID_LENGTH;
372 }
373
374 struct pldm_get_pdr_resp *response =
375 (struct pldm_get_pdr_resp *)msg->payload;
376
377 *next_record_hndl = le32toh(response->next_record_handle);
378 *next_data_transfer_hndl = le32toh(response->next_data_transfer_handle);
379 *transfer_flag = response->transfer_flag;
380 *resp_cnt = le16toh(response->response_count);
381
382 if (*transfer_flag != PLDM_END &&
383 (int)payload_length != PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt) {
384 return PLDM_ERROR_INVALID_LENGTH;
385 }
386
387 if (*transfer_flag == PLDM_END &&
388 (int)payload_length !=
389 PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt + 1) {
390 return PLDM_ERROR_INVALID_LENGTH;
391 }
392
Zach Clark3dba2bf2020-03-31 10:58:03 -0500393 if (*resp_cnt > 0 && record_data != NULL) {
George Liu820a9a52019-11-26 14:43:59 +0800394 if (record_data_length < *resp_cnt) {
395 return PLDM_ERROR_INVALID_LENGTH;
396 }
397 memcpy(record_data, response->record_data, *resp_cnt);
398 }
399
400 if (*transfer_flag == PLDM_END) {
401 *transfer_crc =
402 msg->payload[PLDM_GET_PDR_MIN_RESP_BYTES + *resp_cnt];
403 }
404
405 return PLDM_SUCCESS;
406}
George Liu30b859f2020-01-07 15:03:22 +0800407
408int decode_set_numeric_effecter_value_req(const struct pldm_msg *msg,
409 size_t payload_length,
410 uint16_t *effecter_id,
411 uint8_t *effecter_data_size,
412 uint8_t *effecter_value)
413{
414 if (msg == NULL || effecter_id == NULL || effecter_data_size == NULL ||
415 effecter_value == NULL) {
416 return PLDM_ERROR_INVALID_DATA;
417 }
418
419 if (payload_length < PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
420 return PLDM_ERROR_INVALID_LENGTH;
421 }
422
423 struct pldm_set_numeric_effecter_value_req *request =
424 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
425 *effecter_id = le16toh(request->effecter_id);
426 *effecter_data_size = request->effecter_data_size;
427
428 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
429 return PLDM_ERROR_INVALID_DATA;
430 }
431
432 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
433 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
434
435 if (payload_length !=
436 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
437 return PLDM_ERROR_INVALID_LENGTH;
438 }
439
440 *effecter_value = request->effecter_value[0];
441 }
442
443 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
444 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
445
446 if (payload_length !=
447 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
448 return PLDM_ERROR_INVALID_LENGTH;
449 }
450
451 memcpy(effecter_value, request->effecter_value, 2);
George Liuda75abe2020-04-09 13:36:13 +0800452 uint16_t *val = (uint16_t *)(effecter_value);
453 *val = le16toh(*val);
George Liu30b859f2020-01-07 15:03:22 +0800454 }
455
456 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
457 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
458
459 if (payload_length !=
460 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
461 return PLDM_ERROR_INVALID_LENGTH;
462 }
463
464 memcpy(effecter_value, request->effecter_value, 4);
George Liuda75abe2020-04-09 13:36:13 +0800465 uint32_t *val = (uint32_t *)(effecter_value);
466 *val = le32toh(*val);
George Liu30b859f2020-01-07 15:03:22 +0800467 }
468
469 return PLDM_SUCCESS;
470}
471
472int encode_set_numeric_effecter_value_resp(uint8_t instance_id,
473 uint8_t completion_code,
474 struct pldm_msg *msg,
475 size_t payload_length)
476{
George Liu30b859f2020-01-07 15:03:22 +0800477 if (msg == NULL) {
478 return PLDM_ERROR_INVALID_DATA;
479 }
480
481 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
482 return PLDM_ERROR_INVALID_LENGTH;
483 }
484
George Liub7095ff2021-06-14 16:01:57 +0800485 struct pldm_header_info header = {0};
George Liu30b859f2020-01-07 15:03:22 +0800486 header.msg_type = PLDM_RESPONSE;
487 header.instance = instance_id;
488 header.pldm_type = PLDM_PLATFORM;
489 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
490
George Liub7095ff2021-06-14 16:01:57 +0800491 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
492 if (rc != PLDM_SUCCESS) {
493 return rc;
494 }
495
496 msg->payload[0] = completion_code;
George Liu30b859f2020-01-07 15:03:22 +0800497
498 return rc;
499}
500
501int encode_set_numeric_effecter_value_req(
502 uint8_t instance_id, uint16_t effecter_id, uint8_t effecter_data_size,
503 uint8_t *effecter_value, struct pldm_msg *msg, size_t payload_length)
504{
George Liu30b859f2020-01-07 15:03:22 +0800505 if (msg == NULL || effecter_value == NULL) {
506 return PLDM_ERROR_INVALID_DATA;
507 }
George Liu30b859f2020-01-07 15:03:22 +0800508
509 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
510 return PLDM_ERROR_INVALID_DATA;
511 }
512
George Liub7095ff2021-06-14 16:01:57 +0800513 struct pldm_header_info header = {0};
514 header.msg_type = PLDM_REQUEST;
515 header.instance = instance_id;
516 header.pldm_type = PLDM_PLATFORM;
517 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
518
519 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
520 if (rc != PLDM_SUCCESS) {
521 return rc;
522 }
523
524 struct pldm_set_numeric_effecter_value_req *request =
525 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
George Liu30b859f2020-01-07 15:03:22 +0800526 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
527 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
528 if (payload_length !=
529 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
530 return PLDM_ERROR_INVALID_LENGTH;
531 }
532 request->effecter_value[0] = *effecter_value;
533 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
534 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
535 if (payload_length !=
536 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
537 return PLDM_ERROR_INVALID_LENGTH;
538 }
539
George Liuab749502020-04-15 13:16:35 +0800540 uint16_t val = *(uint16_t *)(effecter_value);
541 val = htole16(val);
542 memcpy(request->effecter_value, &val, sizeof(uint16_t));
543
George Liu30b859f2020-01-07 15:03:22 +0800544 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
545 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
546 if (payload_length !=
547 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
548 return PLDM_ERROR_INVALID_LENGTH;
549 }
550
George Liuab749502020-04-15 13:16:35 +0800551 uint32_t val = *(uint32_t *)(effecter_value);
552 val = htole32(val);
553 memcpy(request->effecter_value, &val, sizeof(uint32_t));
George Liu30b859f2020-01-07 15:03:22 +0800554 }
555
556 request->effecter_id = htole16(effecter_id);
557 request->effecter_data_size = effecter_data_size;
558
559 return PLDM_SUCCESS;
560}
561
562int decode_set_numeric_effecter_value_resp(const struct pldm_msg *msg,
563 size_t payload_length,
564 uint8_t *completion_code)
565{
566 if (msg == NULL || completion_code == NULL) {
567 return PLDM_ERROR_INVALID_DATA;
568 }
569
570 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
571 return PLDM_ERROR_INVALID_LENGTH;
572 }
573
574 *completion_code = msg->payload[0];
575
576 return PLDM_SUCCESS;
577}
Jolie Ku3557bad2020-03-02 16:22:57 +0800578
579int encode_get_state_sensor_readings_resp(uint8_t instance_id,
580 uint8_t completion_code,
581 uint8_t comp_sensor_count,
582 get_sensor_state_field *field,
583 struct pldm_msg *msg)
584{
George Liub7095ff2021-06-14 16:01:57 +0800585 if (msg == NULL) {
586 return PLDM_ERROR_INVALID_DATA;
587 }
Jolie Ku3557bad2020-03-02 16:22:57 +0800588
George Liub7095ff2021-06-14 16:01:57 +0800589 if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
590 return PLDM_ERROR_INVALID_DATA;
591 }
592
593 struct pldm_header_info header = {0};
Jolie Ku3557bad2020-03-02 16:22:57 +0800594 header.msg_type = PLDM_RESPONSE;
595 header.instance = instance_id;
596 header.pldm_type = PLDM_PLATFORM;
597 header.command = PLDM_GET_STATE_SENSOR_READINGS;
598
George Liub7095ff2021-06-14 16:01:57 +0800599 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
600 if (rc != PLDM_SUCCESS) {
Jolie Ku3557bad2020-03-02 16:22:57 +0800601 return rc;
602 }
603
Jolie Ku3557bad2020-03-02 16:22:57 +0800604 struct pldm_get_state_sensor_readings_resp *response =
605 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
606
607 response->completion_code = completion_code;
608 response->comp_sensor_count = comp_sensor_count;
609 memcpy(response->field, field,
610 (sizeof(get_sensor_state_field) * comp_sensor_count));
611
George Liub7095ff2021-06-14 16:01:57 +0800612 return PLDM_SUCCESS;
Jolie Ku3557bad2020-03-02 16:22:57 +0800613}
614
615int encode_get_state_sensor_readings_req(uint8_t instance_id,
616 uint16_t sensor_id,
617 bitfield8_t sensor_rearm,
618 uint8_t reserved, struct pldm_msg *msg)
619{
George Liub7095ff2021-06-14 16:01:57 +0800620 if (msg == NULL) {
621 return PLDM_ERROR_INVALID_DATA;
622 }
Jolie Ku3557bad2020-03-02 16:22:57 +0800623
George Liub7095ff2021-06-14 16:01:57 +0800624 struct pldm_header_info header = {0};
Jolie Ku3557bad2020-03-02 16:22:57 +0800625 header.msg_type = PLDM_REQUEST;
626 header.instance = instance_id;
627 header.pldm_type = PLDM_PLATFORM;
628 header.command = PLDM_GET_STATE_SENSOR_READINGS;
629
George Liub7095ff2021-06-14 16:01:57 +0800630 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
631 if (rc != PLDM_SUCCESS) {
Jolie Ku3557bad2020-03-02 16:22:57 +0800632 return rc;
633 }
634
635 struct pldm_get_state_sensor_readings_req *request =
636 (struct pldm_get_state_sensor_readings_req *)msg->payload;
637
638 request->sensor_id = htole16(sensor_id);
639 request->reserved = reserved;
640 request->sensor_rearm = sensor_rearm;
641
642 return PLDM_SUCCESS;
643}
644
645int decode_get_state_sensor_readings_resp(const struct pldm_msg *msg,
646 size_t payload_length,
647 uint8_t *completion_code,
648 uint8_t *comp_sensor_count,
649 get_sensor_state_field *field)
650{
651 if (msg == NULL || completion_code == NULL ||
652 comp_sensor_count == NULL || field == NULL) {
653 return PLDM_ERROR_INVALID_DATA;
654 }
655
656 *completion_code = msg->payload[0];
657 if (PLDM_SUCCESS != *completion_code) {
658 return PLDM_SUCCESS;
659 }
660
Jolie Ku3557bad2020-03-02 16:22:57 +0800661 struct pldm_get_state_sensor_readings_resp *response =
662 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
663
664 if (response->comp_sensor_count < 0x1 ||
665 response->comp_sensor_count > 0x8) {
666 return PLDM_ERROR_INVALID_DATA;
667 }
George Liu38eaa272021-01-21 19:30:15 +0800668
669 if (payload_length >
670 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
671 sizeof(get_sensor_state_field) * response->comp_sensor_count) {
Jolie Ku3557bad2020-03-02 16:22:57 +0800672 return PLDM_ERROR_INVALID_LENGTH;
673 }
George Liu38eaa272021-01-21 19:30:15 +0800674
Jolie Ku3557bad2020-03-02 16:22:57 +0800675 *comp_sensor_count = response->comp_sensor_count;
676
677 memcpy(field, response->field,
678 (sizeof(get_sensor_state_field) * (*comp_sensor_count)));
679
680 return PLDM_SUCCESS;
681}
682
683int decode_get_state_sensor_readings_req(const struct pldm_msg *msg,
684 size_t payload_length,
685 uint16_t *sensor_id,
686 bitfield8_t *sensor_rearm,
687 uint8_t *reserved)
688{
689 if (msg == NULL || sensor_id == NULL || sensor_rearm == NULL) {
690 return PLDM_ERROR_INVALID_DATA;
691 }
692
693 if (payload_length != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES) {
694 return PLDM_ERROR_INVALID_LENGTH;
695 }
696
697 struct pldm_get_state_sensor_readings_req *request =
698 (struct pldm_get_state_sensor_readings_req *)msg->payload;
699
700 *sensor_id = le16toh(request->sensor_id);
701 *reserved = request->reserved;
702 memcpy(&(sensor_rearm->byte), &(request->sensor_rearm.byte),
703 sizeof(request->sensor_rearm.byte));
704
705 return PLDM_SUCCESS;
706}
Zahed Hossaind4abab12020-02-06 03:36:43 -0600707
Zach Clarkb728eee2020-06-18 10:01:31 -0500708int encode_sensor_event_data(
709 struct pldm_sensor_event_data *const event_data,
710 const size_t event_data_size, const uint16_t sensor_id,
711 const enum sensor_event_class_states sensor_event_class,
712 const uint8_t sensor_offset, const uint8_t event_state,
713 const uint8_t previous_event_state, size_t *const actual_event_data_size)
714{
715 *actual_event_data_size =
716 (sizeof(*event_data) - sizeof(event_data->event_class) +
717 sizeof(struct pldm_sensor_event_state_sensor_state));
718
719 if (!event_data) {
720 return PLDM_SUCCESS;
721 }
722
723 if (event_data_size < *actual_event_data_size) {
724 *actual_event_data_size = 0;
725 return PLDM_ERROR_INVALID_LENGTH;
726 }
727
Christian Geddes1aad2f92021-02-08 10:03:43 -0600728 event_data->sensor_id = htole16(sensor_id);
Zach Clarkb728eee2020-06-18 10:01:31 -0500729 event_data->sensor_event_class_type = sensor_event_class;
730
731 struct pldm_sensor_event_state_sensor_state *const state_data =
732 (struct pldm_sensor_event_state_sensor_state *)
733 event_data->event_class;
734
735 state_data->sensor_offset = sensor_offset;
736 state_data->event_state = event_state;
737 state_data->previous_event_state = previous_event_state;
738
739 return PLDM_SUCCESS;
740}
741
Zahed Hossaind4abab12020-02-06 03:36:43 -0600742int decode_platform_event_message_req(const struct pldm_msg *msg,
743 size_t payload_length,
744 uint8_t *format_version, uint8_t *tid,
745 uint8_t *event_class,
746 size_t *event_data_offset)
747{
748
749 if (msg == NULL || format_version == NULL || tid == NULL ||
750 event_class == NULL || event_data_offset == NULL) {
751 return PLDM_ERROR_INVALID_DATA;
752 }
753
754 if (payload_length <= PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES) {
755 return PLDM_ERROR_INVALID_LENGTH;
756 }
757 struct pldm_platform_event_message_req *response =
758 (struct pldm_platform_event_message_req *)msg->payload;
759
760 *format_version = response->format_version;
761 *tid = response->tid;
762 *event_class = response->event_class;
763 *event_data_offset =
764 sizeof(*format_version) + sizeof(*tid) + sizeof(*event_class);
765
766 return PLDM_SUCCESS;
767}
768
769int encode_platform_event_message_resp(uint8_t instance_id,
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500770 uint8_t completion_code,
771 uint8_t platform_event_status,
Zahed Hossaind4abab12020-02-06 03:36:43 -0600772 struct pldm_msg *msg)
773{
Zahed Hossaind4abab12020-02-06 03:36:43 -0600774 if (msg == NULL) {
775 return PLDM_ERROR_INVALID_DATA;
776 }
777
Pavithra Barithaya2ea1f072020-04-03 09:30:23 -0500778 if (platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
779 return PLDM_ERROR_INVALID_DATA;
780 }
781
Zahed Hossaind4abab12020-02-06 03:36:43 -0600782 struct pldm_header_info header = {0};
783 header.msg_type = PLDM_RESPONSE;
784 header.instance = instance_id;
785 header.pldm_type = PLDM_PLATFORM;
786 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
787
George Liub7095ff2021-06-14 16:01:57 +0800788 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
789 if (rc != PLDM_SUCCESS) {
Zahed Hossaind4abab12020-02-06 03:36:43 -0600790 return rc;
791 }
George Liub7095ff2021-06-14 16:01:57 +0800792
793 struct pldm_platform_event_message_resp *response =
794 (struct pldm_platform_event_message_resp *)msg->payload;
795 response->completion_code = completion_code;
796 response->platform_event_status = platform_event_status;
797
Zahed Hossaind4abab12020-02-06 03:36:43 -0600798 return PLDM_SUCCESS;
799}
Zahed Hossain1c861712020-03-04 08:55:19 -0600800
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500801int encode_platform_event_message_req(
802 uint8_t instance_id, uint8_t format_version, uint8_t tid,
803 uint8_t event_class, const uint8_t *event_data, size_t event_data_length,
804 struct pldm_msg *msg, size_t payload_length)
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500805
806{
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500807 if (format_version != 1) {
808 return PLDM_ERROR_INVALID_DATA;
809 }
810
811 if (msg == NULL || event_data == NULL) {
812 return PLDM_ERROR_INVALID_DATA;
813 }
814
815 if (event_data_length == 0) {
816 return PLDM_ERROR_INVALID_DATA;
817 }
818
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500819 if (payload_length !=
820 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + event_data_length) {
821 return PLDM_ERROR_INVALID_LENGTH;
822 }
823
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500824 if (event_class > PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT &&
825 !(event_class >= 0xF0 && event_class <= 0xFE)) {
826 return PLDM_ERROR_INVALID_DATA;
827 }
828
George Liub7095ff2021-06-14 16:01:57 +0800829 struct pldm_header_info header = {0};
830 header.msg_type = PLDM_REQUEST;
831 header.instance = instance_id;
832 header.pldm_type = PLDM_PLATFORM;
833 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
834
835 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
836 if (rc != PLDM_SUCCESS) {
Pavithra Barithaya35f2b2c2020-04-08 01:35:56 -0500837 return rc;
838 }
839
840 struct pldm_platform_event_message_req *request =
841 (struct pldm_platform_event_message_req *)msg->payload;
842 request->format_version = format_version;
843 request->tid = tid;
844 request->event_class = event_class;
845 memcpy(request->event_data, event_data, event_data_length);
846
847 return PLDM_SUCCESS;
848}
849
850int decode_platform_event_message_resp(const struct pldm_msg *msg,
851 size_t payload_length,
852 uint8_t *completion_code,
853 uint8_t *platform_event_status)
854{
855 if (msg == NULL || completion_code == NULL ||
856 platform_event_status == NULL) {
857 return PLDM_ERROR_INVALID_DATA;
858 }
859
860 *completion_code = msg->payload[0];
861 if (PLDM_SUCCESS != *completion_code) {
862 return PLDM_SUCCESS;
863 }
864 if (payload_length != PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES) {
865 return PLDM_ERROR_INVALID_LENGTH;
866 }
867
868 struct pldm_platform_event_message_resp *response =
869 (struct pldm_platform_event_message_resp *)msg->payload;
870 *platform_event_status = response->platform_event_status;
871
872 if (*platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
873 return PLDM_ERROR_INVALID_DATA;
874 }
875
876 return PLDM_SUCCESS;
877}
878
Zahed Hossain1c861712020-03-04 08:55:19 -0600879int decode_sensor_event_data(const uint8_t *event_data,
880 size_t event_data_length, uint16_t *sensor_id,
881 uint8_t *sensor_event_class_type,
882 size_t *event_class_data_offset)
883{
884 if (event_data == NULL) {
885 return PLDM_ERROR_INVALID_DATA;
886 }
887 if (event_data_length < PLDM_SENSOR_EVENT_DATA_MIN_LENGTH) {
888 return PLDM_ERROR_INVALID_LENGTH;
889 }
890
891 size_t event_class_data_length =
892 event_data_length - PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
893
894 struct pldm_sensor_event_data *sensor_event_data =
895 (struct pldm_sensor_event_data *)event_data;
896 *sensor_id = sensor_event_data->sensor_id;
897 *sensor_event_class_type = sensor_event_data->sensor_event_class_type;
898 if (sensor_event_data->sensor_event_class_type ==
899 PLDM_SENSOR_OP_STATE) {
900 if (event_class_data_length !=
901 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
902 return PLDM_ERROR_INVALID_LENGTH;
903 }
904 } else if (sensor_event_data->sensor_event_class_type ==
905 PLDM_STATE_SENSOR_STATE) {
906 if (event_class_data_length !=
907 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
908 return PLDM_ERROR_INVALID_LENGTH;
909 }
910 } else if (sensor_event_data->sensor_event_class_type ==
911 PLDM_NUMERIC_SENSOR_STATE) {
912 if (event_class_data_length <
913 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
914 event_class_data_length >
915 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
916 return PLDM_ERROR_INVALID_LENGTH;
917 }
918 } else {
919 return PLDM_ERROR_INVALID_DATA;
920 }
921 *event_class_data_offset =
922 sizeof(*sensor_id) + sizeof(*sensor_event_class_type);
923 return PLDM_SUCCESS;
924}
925
926int decode_sensor_op_data(const uint8_t *sensor_data, size_t sensor_data_length,
927 uint8_t *present_op_state, uint8_t *previous_op_state)
928{
929 if (sensor_data == NULL || present_op_state == NULL ||
930 previous_op_state == NULL) {
931 return PLDM_ERROR_INVALID_DATA;
932 }
933 if (sensor_data_length !=
934 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
935 return PLDM_ERROR_INVALID_LENGTH;
936 }
937
938 struct pldm_sensor_event_sensor_op_state *sensor_op_data =
939 (struct pldm_sensor_event_sensor_op_state *)sensor_data;
940 *present_op_state = sensor_op_data->present_op_state;
941 *previous_op_state = sensor_op_data->previous_op_state;
942 return PLDM_SUCCESS;
943}
944
945int decode_state_sensor_data(const uint8_t *sensor_data,
946 size_t sensor_data_length, uint8_t *sensor_offset,
947 uint8_t *event_state,
948 uint8_t *previous_event_state)
949{
950 if (sensor_data == NULL || sensor_offset == NULL ||
951 event_state == NULL || previous_event_state == NULL) {
952 return PLDM_ERROR_INVALID_DATA;
953 }
954 if (sensor_data_length !=
955 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
956 return PLDM_ERROR_INVALID_LENGTH;
957 }
958
959 struct pldm_sensor_event_state_sensor_state *sensor_state_data =
960 (struct pldm_sensor_event_state_sensor_state *)sensor_data;
961 *sensor_offset = sensor_state_data->sensor_offset;
962 *event_state = sensor_state_data->event_state;
963 *previous_event_state = sensor_state_data->previous_event_state;
964 return PLDM_SUCCESS;
965}
966
967int decode_numeric_sensor_data(const uint8_t *sensor_data,
968 size_t sensor_data_length, uint8_t *event_state,
969 uint8_t *previous_event_state,
970 uint8_t *sensor_data_size,
971 uint32_t *present_reading)
972{
973 if (sensor_data == NULL || sensor_data_size == NULL ||
974 event_state == NULL || previous_event_state == NULL ||
975 present_reading == NULL) {
976 return PLDM_ERROR_INVALID_DATA;
977 }
978 if (sensor_data_length <
979 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
980 sensor_data_length >
981 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
982 return PLDM_ERROR_INVALID_LENGTH;
983 }
984 struct pldm_sensor_event_numeric_sensor_state *numeric_sensor_data =
985 (struct pldm_sensor_event_numeric_sensor_state *)sensor_data;
986 *event_state = numeric_sensor_data->event_state;
987 *previous_event_state = numeric_sensor_data->previous_event_state;
988 *sensor_data_size = numeric_sensor_data->sensor_data_size;
989 uint8_t *present_reading_ptr = numeric_sensor_data->present_reading;
990
991 switch (*sensor_data_size) {
992 case PLDM_SENSOR_DATA_SIZE_UINT8:
993 case PLDM_SENSOR_DATA_SIZE_SINT8:
994 if (sensor_data_length !=
995 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_8BIT_DATA_LENGTH) {
996 return PLDM_ERROR_INVALID_LENGTH;
997 }
998 *present_reading = present_reading_ptr[0];
999 break;
1000 case PLDM_SENSOR_DATA_SIZE_UINT16:
1001 case PLDM_SENSOR_DATA_SIZE_SINT16:
1002 if (sensor_data_length !=
1003 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH) {
1004 return PLDM_ERROR_INVALID_LENGTH;
1005 }
1006 *present_reading = le16toh(present_reading_ptr[1] |
1007 (present_reading_ptr[0] << 8));
1008 break;
1009 case PLDM_SENSOR_DATA_SIZE_UINT32:
1010 case PLDM_SENSOR_DATA_SIZE_SINT32:
1011 if (sensor_data_length !=
1012 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH) {
1013 return PLDM_ERROR_INVALID_LENGTH;
1014 }
1015 *present_reading = le32toh(present_reading_ptr[3] |
1016 (present_reading_ptr[2] << 8) |
1017 (present_reading_ptr[1] << 16) |
1018 (present_reading_ptr[0] << 24));
1019 break;
1020 default:
1021 return PLDM_ERROR_INVALID_DATA;
1022 }
1023 return PLDM_SUCCESS;
1024}
Jolie Ku6787f172020-03-19 11:15:53 +08001025
1026int encode_get_numeric_effecter_value_req(uint8_t instance_id,
1027 uint16_t effecter_id,
1028 struct pldm_msg *msg)
1029{
Jolie Ku6787f172020-03-19 11:15:53 +08001030 if (msg == NULL) {
1031 return PLDM_ERROR_INVALID_DATA;
1032 }
1033
George Liub7095ff2021-06-14 16:01:57 +08001034 struct pldm_header_info header = {0};
Jolie Ku6787f172020-03-19 11:15:53 +08001035 header.msg_type = PLDM_REQUEST;
1036 header.instance = instance_id;
1037 header.pldm_type = PLDM_PLATFORM;
1038 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1039
George Liub7095ff2021-06-14 16:01:57 +08001040 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1041 if (rc != PLDM_SUCCESS) {
Jolie Ku6787f172020-03-19 11:15:53 +08001042 return rc;
1043 }
1044
George Liub7095ff2021-06-14 16:01:57 +08001045 struct pldm_get_numeric_effecter_value_req *request =
1046 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
Jolie Ku6787f172020-03-19 11:15:53 +08001047 request->effecter_id = htole16(effecter_id);
1048
1049 return PLDM_SUCCESS;
1050}
1051
1052int encode_get_numeric_effecter_value_resp(
1053 uint8_t instance_id, uint8_t completion_code, uint8_t effecter_data_size,
1054 uint8_t effecter_oper_state, uint8_t *pending_value, uint8_t *present_value,
1055 struct pldm_msg *msg, size_t payload_length)
1056{
Jolie Ku6787f172020-03-19 11:15:53 +08001057 if (msg == NULL || pending_value == NULL || present_value == NULL) {
1058 return PLDM_ERROR_INVALID_DATA;
1059 }
1060
1061 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1062 return PLDM_ERROR_INVALID_DATA;
1063 }
1064
1065 if (effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1066 return PLDM_ERROR_INVALID_DATA;
1067 }
1068
George Liub7095ff2021-06-14 16:01:57 +08001069 struct pldm_header_info header = {0};
Jolie Ku6787f172020-03-19 11:15:53 +08001070 header.msg_type = PLDM_RESPONSE;
1071 header.instance = instance_id;
1072 header.pldm_type = PLDM_PLATFORM;
1073 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1074
George Liub7095ff2021-06-14 16:01:57 +08001075 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1076 if (rc != PLDM_SUCCESS) {
Jolie Ku6787f172020-03-19 11:15:53 +08001077 return rc;
1078 }
1079
1080 struct pldm_get_numeric_effecter_value_resp *response =
1081 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
1082
1083 response->completion_code = completion_code;
1084 response->effecter_data_size = effecter_data_size;
1085 response->effecter_oper_state = effecter_oper_state;
1086
1087 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1088 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1089 if (payload_length !=
1090 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1091 return PLDM_ERROR_INVALID_LENGTH;
1092 }
1093 response->pending_and_present_values[0] = *pending_value;
1094 response->pending_and_present_values[1] = *present_value;
1095
1096 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1097 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1098 if (payload_length !=
1099 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1100 return PLDM_ERROR_INVALID_LENGTH;
1101 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001102 uint16_t val_pending = *(uint16_t *)pending_value;
1103 val_pending = htole16(val_pending);
1104 memcpy(response->pending_and_present_values, &val_pending,
1105 sizeof(uint16_t));
1106 uint16_t val_present = *(uint16_t *)present_value;
1107 val_present = htole16(val_present);
1108 memcpy(
1109 (response->pending_and_present_values + sizeof(uint16_t)),
1110 &val_present, sizeof(uint16_t));
Jolie Ku6787f172020-03-19 11:15:53 +08001111
1112 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1113 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1114 if (payload_length !=
1115 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1116 return PLDM_ERROR_INVALID_LENGTH;
1117 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001118 uint32_t val_pending = *(uint32_t *)pending_value;
1119 val_pending = htole32(val_pending);
1120 memcpy(response->pending_and_present_values, &val_pending,
1121 sizeof(uint32_t));
1122 uint32_t val_present = *(uint32_t *)present_value;
1123 val_present = htole32(val_present);
1124 memcpy(
1125 (response->pending_and_present_values + sizeof(uint32_t)),
1126 &val_present, sizeof(uint32_t));
Jolie Ku6787f172020-03-19 11:15:53 +08001127 }
1128 return PLDM_SUCCESS;
1129}
1130
1131int decode_get_numeric_effecter_value_req(const struct pldm_msg *msg,
1132 size_t payload_length,
1133 uint16_t *effecter_id)
1134{
1135 if (msg == NULL || effecter_id == NULL) {
1136 return PLDM_ERROR_INVALID_DATA;
1137 }
1138
1139 if (payload_length != PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES) {
1140 return PLDM_ERROR_INVALID_LENGTH;
1141 }
1142
1143 struct pldm_get_numeric_effecter_value_req *request =
1144 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
1145
1146 *effecter_id = le16toh(request->effecter_id);
1147
1148 return PLDM_SUCCESS;
1149}
1150
1151int decode_get_numeric_effecter_value_resp(
1152 const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
1153 uint8_t *effecter_data_size, uint8_t *effecter_oper_state,
1154 uint8_t *pending_value, uint8_t *present_value)
1155{
1156 if (msg == NULL || effecter_data_size == NULL ||
1157 effecter_oper_state == NULL || pending_value == NULL ||
1158 present_value == NULL) {
1159 return PLDM_ERROR_INVALID_DATA;
1160 }
1161
1162 *completion_code = msg->payload[0];
1163 if (PLDM_SUCCESS != *completion_code) {
1164 return PLDM_SUCCESS;
1165 }
1166
1167 if (payload_length < PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1168 return PLDM_ERROR_INVALID_LENGTH;
1169 }
1170
1171 struct pldm_get_numeric_effecter_value_resp *response =
1172 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
1173
1174 *effecter_data_size = response->effecter_data_size;
1175 *effecter_oper_state = response->effecter_oper_state;
1176
1177 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1178 return PLDM_ERROR_INVALID_DATA;
1179 }
1180
1181 if (*effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1182 return PLDM_ERROR_INVALID_DATA;
1183 }
1184
1185 if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1186 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1187 if (payload_length !=
1188 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1189 return PLDM_ERROR_INVALID_LENGTH;
1190 }
1191 memcpy(pending_value, response->pending_and_present_values, 1);
1192 memcpy(present_value, &response->pending_and_present_values[1],
1193 1);
1194
1195 } else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1196 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1197 if (payload_length !=
1198 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1199 return PLDM_ERROR_INVALID_LENGTH;
1200 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001201 memcpy(pending_value, response->pending_and_present_values,
1202 sizeof(uint16_t));
1203 uint16_t *val_pending = (uint16_t *)pending_value;
1204 *val_pending = le16toh(*val_pending);
1205 memcpy(
1206 present_value,
1207 (response->pending_and_present_values + sizeof(uint16_t)),
1208 sizeof(uint16_t));
1209 uint16_t *val_present = (uint16_t *)present_value;
1210 *val_present = le16toh(*val_present);
Jolie Ku6787f172020-03-19 11:15:53 +08001211
1212 } else if (*effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1213 *effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1214 if (payload_length !=
1215 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1216 return PLDM_ERROR_INVALID_LENGTH;
1217 }
Jolie Ku3cdbcfb2020-04-17 10:30:59 +08001218 memcpy(pending_value, response->pending_and_present_values,
1219 sizeof(uint32_t));
1220 uint32_t *val_pending = (uint32_t *)pending_value;
1221 *val_pending = le32toh(*val_pending);
1222 memcpy(
1223 present_value,
1224 (response->pending_and_present_values + sizeof(uint32_t)),
1225 sizeof(uint32_t));
1226 uint32_t *val_present = (uint32_t *)present_value;
1227 *val_present = le32toh(*val_present);
Jolie Ku6787f172020-03-19 11:15:53 +08001228 }
1229 return PLDM_SUCCESS;
Zach Clark3dba2bf2020-03-31 10:58:03 -05001230}
Zahed Hossain9be087c2020-04-02 02:26:41 -05001231
Zach Clark36ad1f52020-04-13 07:04:15 -05001232int encode_pldm_pdr_repository_chg_event_data(
1233 uint8_t event_data_format, uint8_t number_of_change_records,
1234 const uint8_t *event_data_operations,
1235 const uint8_t *numbers_of_change_entries,
1236 const uint32_t *const *change_entries,
1237 struct pldm_pdr_repository_chg_event_data *event_data,
1238 size_t *actual_change_records_size, size_t max_change_records_size)
1239{
1240 if (event_data_operations == NULL ||
1241 numbers_of_change_entries == NULL || change_entries == NULL) {
1242 return PLDM_ERROR_INVALID_DATA;
1243 }
1244
1245 size_t expected_size =
1246 sizeof(event_data_format) + sizeof(number_of_change_records);
1247
1248 expected_size +=
1249 sizeof(*event_data_operations) * number_of_change_records;
1250 expected_size +=
1251 sizeof(*numbers_of_change_entries) * number_of_change_records;
1252
1253 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1254 expected_size +=
1255 sizeof(*change_entries[0]) * numbers_of_change_entries[i];
1256 }
1257
1258 *actual_change_records_size = expected_size;
1259
1260 if (event_data == NULL) {
1261 return PLDM_SUCCESS;
1262 }
1263
1264 if (max_change_records_size < expected_size) {
1265 return PLDM_ERROR_INVALID_LENGTH;
1266 }
1267
1268 event_data->event_data_format = event_data_format;
1269 event_data->number_of_change_records = number_of_change_records;
1270
1271 struct pldm_pdr_repository_change_record_data *record_data =
1272 (struct pldm_pdr_repository_change_record_data *)
1273 event_data->change_records;
1274
1275 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1276 record_data->event_data_operation = event_data_operations[i];
1277 record_data->number_of_change_entries =
1278 numbers_of_change_entries[i];
1279
1280 for (uint8_t j = 0; j < record_data->number_of_change_entries;
1281 ++j) {
1282 record_data->change_entry[j] =
1283 htole32(change_entries[i][j]);
1284 }
1285
1286 record_data = (struct pldm_pdr_repository_change_record_data
1287 *)(record_data->change_entry +
1288 record_data->number_of_change_entries);
1289 }
1290
1291 return PLDM_SUCCESS;
1292}
1293
Zahed Hossain9be087c2020-04-02 02:26:41 -05001294int decode_pldm_pdr_repository_chg_event_data(const uint8_t *event_data,
1295 size_t event_data_size,
1296 uint8_t *event_data_format,
1297 uint8_t *number_of_change_records,
1298 size_t *change_record_data_offset)
1299{
1300 if (event_data == NULL || event_data_format == NULL ||
1301 number_of_change_records == NULL ||
1302 change_record_data_offset == NULL) {
1303 return PLDM_ERROR_INVALID_DATA;
1304 }
1305 if (event_data_size < PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH) {
1306 return PLDM_ERROR_INVALID_LENGTH;
1307 }
1308
1309 struct pldm_pdr_repository_chg_event_data
1310 *pdr_repository_chg_event_data =
1311 (struct pldm_pdr_repository_chg_event_data *)event_data;
1312
1313 *event_data_format = pdr_repository_chg_event_data->event_data_format;
1314 *number_of_change_records =
1315 pdr_repository_chg_event_data->number_of_change_records;
1316 *change_record_data_offset =
1317 sizeof(*event_data_format) + sizeof(*number_of_change_records);
1318
1319 return PLDM_SUCCESS;
1320}
1321
1322int decode_pldm_pdr_repository_change_record_data(
1323 const uint8_t *change_record_data, size_t change_record_data_size,
1324 uint8_t *event_data_operation, uint8_t *number_of_change_entries,
1325 size_t *change_entry_data_offset)
1326{
1327 if (change_record_data == NULL || event_data_operation == NULL ||
1328 number_of_change_entries == NULL ||
1329 change_entry_data_offset == NULL) {
1330 return PLDM_ERROR_INVALID_DATA;
1331 }
1332 if (change_record_data_size <
1333 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH) {
1334 return PLDM_ERROR_INVALID_LENGTH;
1335 }
1336
1337 struct pldm_pdr_repository_change_record_data
1338 *pdr_repository_change_record_data =
1339 (struct pldm_pdr_repository_change_record_data *)
1340 change_record_data;
1341
1342 *event_data_operation =
1343 pdr_repository_change_record_data->event_data_operation;
1344 *number_of_change_entries =
1345 pdr_repository_change_record_data->number_of_change_entries;
1346 *change_entry_data_offset =
1347 sizeof(*event_data_operation) + sizeof(*number_of_change_entries);
1348
1349 return PLDM_SUCCESS;
1350}
Jolie Kuf798c8f2020-04-14 11:18:06 +08001351
1352int encode_get_sensor_reading_req(uint8_t instance_id, uint16_t sensor_id,
1353 uint8_t rearm_event_state,
1354 struct pldm_msg *msg)
1355{
George Liub7095ff2021-06-14 16:01:57 +08001356 if (msg == NULL) {
1357 return PLDM_ERROR_INVALID_DATA;
1358 }
Jolie Kuf798c8f2020-04-14 11:18:06 +08001359
George Liub7095ff2021-06-14 16:01:57 +08001360 struct pldm_header_info header = {0};
Jolie Kuf798c8f2020-04-14 11:18:06 +08001361 header.msg_type = PLDM_REQUEST;
1362 header.instance = instance_id;
1363 header.pldm_type = PLDM_PLATFORM;
1364 header.command = PLDM_GET_SENSOR_READING;
1365
George Liub7095ff2021-06-14 16:01:57 +08001366 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1367 if (rc != PLDM_SUCCESS) {
Jolie Kuf798c8f2020-04-14 11:18:06 +08001368 return rc;
1369 }
1370
1371 struct pldm_get_sensor_reading_req *request =
1372 (struct pldm_get_sensor_reading_req *)msg->payload;
1373
1374 request->sensor_id = htole16(sensor_id);
1375 request->rearm_event_state = rearm_event_state;
1376
1377 return PLDM_SUCCESS;
1378}
1379
1380int decode_get_sensor_reading_resp(
1381 const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
1382 uint8_t *sensor_data_size, uint8_t *sensor_operational_state,
1383 uint8_t *sensor_event_message_enable, uint8_t *present_state,
1384 uint8_t *previous_state, uint8_t *event_state, uint8_t *present_reading)
1385{
1386 if (msg == NULL || completion_code == NULL ||
1387 sensor_data_size == NULL || sensor_operational_state == NULL ||
1388 sensor_event_message_enable == NULL || present_state == NULL ||
1389 previous_state == NULL || event_state == NULL ||
1390 present_reading == NULL) {
1391 return PLDM_ERROR_INVALID_DATA;
1392 }
1393
1394 *completion_code = msg->payload[0];
1395 if (PLDM_SUCCESS != *completion_code) {
1396 return PLDM_SUCCESS;
1397 }
1398
1399 if (payload_length < PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1400 return PLDM_ERROR_INVALID_LENGTH;
1401 }
1402
1403 struct pldm_get_sensor_reading_resp *response =
1404 (struct pldm_get_sensor_reading_resp *)msg->payload;
1405
1406 if (response->sensor_data_size > PLDM_SENSOR_DATA_SIZE_SINT32) {
1407 return PLDM_ERROR_INVALID_DATA;
1408 }
1409 if (response->sensor_data_size > *sensor_data_size) {
1410 return PLDM_ERROR_INVALID_LENGTH;
1411 }
1412
1413 *sensor_data_size = response->sensor_data_size;
1414 *sensor_operational_state = response->sensor_operational_state;
1415 *sensor_event_message_enable = response->sensor_event_message_enable;
1416 *present_state = response->present_state;
1417 *previous_state = response->previous_state;
1418 *event_state = response->event_state;
1419
1420 if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1421 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1422 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1423 return PLDM_ERROR_INVALID_LENGTH;
1424 }
1425 *present_reading = response->present_reading[0];
1426
1427 } else if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1428 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1429 if (payload_length !=
1430 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
1431 return PLDM_ERROR_INVALID_LENGTH;
1432 }
1433 memcpy(present_reading, response->present_reading, 2);
1434 uint16_t *val = (uint16_t *)(present_reading);
1435 *val = le16toh(*val);
1436
1437 } else if (*sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1438 *sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1439 if (payload_length !=
1440 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
1441 return PLDM_ERROR_INVALID_LENGTH;
1442 }
1443 memcpy(present_reading, response->present_reading, 4);
1444 uint32_t *val = (uint32_t *)(present_reading);
1445 *val = le32toh(*val);
1446 }
1447
1448 return PLDM_SUCCESS;
1449}
1450
1451int encode_get_sensor_reading_resp(
1452 uint8_t instance_id, uint8_t completion_code, uint8_t sensor_data_size,
1453 uint8_t sensor_operational_state, uint8_t sensor_event_message_enable,
1454 uint8_t present_state, uint8_t previous_state, uint8_t event_state,
1455 uint8_t *present_reading, struct pldm_msg *msg, size_t payload_length)
1456{
Jolie Kuf798c8f2020-04-14 11:18:06 +08001457 if (msg == NULL || present_reading == NULL) {
1458 return PLDM_ERROR_INVALID_DATA;
1459 }
1460
1461 if (sensor_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1462 return PLDM_ERROR_INVALID_DATA;
1463 }
1464
George Liub7095ff2021-06-14 16:01:57 +08001465 struct pldm_header_info header = {0};
Jolie Kuf798c8f2020-04-14 11:18:06 +08001466 header.msg_type = PLDM_RESPONSE;
1467 header.instance = instance_id;
1468 header.pldm_type = PLDM_PLATFORM;
1469 header.command = PLDM_GET_SENSOR_READING;
1470
George Liub7095ff2021-06-14 16:01:57 +08001471 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1472 if (rc != PLDM_SUCCESS) {
Jolie Kuf798c8f2020-04-14 11:18:06 +08001473 return rc;
1474 }
1475
1476 struct pldm_get_sensor_reading_resp *response =
1477 (struct pldm_get_sensor_reading_resp *)msg->payload;
1478
1479 response->completion_code = completion_code;
1480 response->sensor_data_size = sensor_data_size;
1481 response->sensor_operational_state = sensor_operational_state;
1482 response->sensor_event_message_enable = sensor_event_message_enable;
1483 response->present_state = present_state;
1484 response->previous_state = previous_state;
1485 response->event_state = event_state;
1486
1487 if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1488 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1489 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
1490 return PLDM_ERROR_INVALID_LENGTH;
1491 }
1492 response->present_reading[0] = *present_reading;
1493
1494 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1495 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1496 if (payload_length !=
1497 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
1498 return PLDM_ERROR_INVALID_LENGTH;
1499 }
1500 uint16_t val = *(uint16_t *)present_reading;
1501 val = htole16(val);
1502 memcpy(response->present_reading, &val, 2);
1503
1504 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1505 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1506 if (payload_length !=
1507 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
1508 return PLDM_ERROR_INVALID_LENGTH;
1509 }
1510 uint32_t val = *(uint32_t *)present_reading;
1511 val = htole32(val);
1512 memcpy(response->present_reading, &val, 4);
1513 }
1514
1515 return PLDM_SUCCESS;
1516}
1517
1518int decode_get_sensor_reading_req(const struct pldm_msg *msg,
1519 size_t payload_length, uint16_t *sensor_id,
1520 uint8_t *rearm_event_state)
1521{
1522 if (msg == NULL || sensor_id == NULL || rearm_event_state == NULL) {
1523 return PLDM_ERROR_INVALID_DATA;
1524 }
1525
1526 if (payload_length != PLDM_GET_SENSOR_READING_REQ_BYTES) {
1527 return PLDM_ERROR_INVALID_LENGTH;
1528 }
1529
1530 struct pldm_get_sensor_reading_req *request =
1531 (struct pldm_get_sensor_reading_req *)msg->payload;
1532
1533 *sensor_id = le16toh(request->sensor_id);
1534 *rearm_event_state = request->rearm_event_state;
1535
1536 return PLDM_SUCCESS;
1537}
Sagar Srinivas535bccd2021-03-24 12:18:26 -05001538
1539int encode_set_event_receiver_req(uint8_t instance_id,
1540 uint8_t event_message_global_enable,
1541 uint8_t transport_protocol_type,
1542 uint8_t event_receiver_address_info,
1543 uint16_t heartbeat_timer,
1544 struct pldm_msg *msg)
1545{
Sagar Srinivas535bccd2021-03-24 12:18:26 -05001546 if (msg == NULL) {
1547 return PLDM_ERROR_INVALID_DATA;
1548 }
1549
George Liub7095ff2021-06-14 16:01:57 +08001550 if (transport_protocol_type != PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP) {
1551 return PLDM_ERROR_INVALID_DATA;
1552 }
1553
1554 struct pldm_header_info header = {0};
Sagar Srinivas535bccd2021-03-24 12:18:26 -05001555 header.msg_type = PLDM_REQUEST;
1556 header.instance = instance_id;
1557 header.pldm_type = PLDM_PLATFORM;
1558 header.command = PLDM_SET_EVENT_RECEIVER;
1559
George Liub7095ff2021-06-14 16:01:57 +08001560 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1561 if (rc != PLDM_SUCCESS) {
Sagar Srinivas535bccd2021-03-24 12:18:26 -05001562 return rc;
1563 }
1564
1565 struct pldm_set_event_receiver_req *request =
1566 (struct pldm_set_event_receiver_req *)msg->payload;
1567 request->event_message_global_enable = event_message_global_enable;
1568
Sagar Srinivas535bccd2021-03-24 12:18:26 -05001569 request->transport_protocol_type = transport_protocol_type;
1570 request->event_receiver_address_info = event_receiver_address_info;
1571
1572 if (event_message_global_enable ==
1573 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) {
1574 if (heartbeat_timer == 0) {
1575 return PLDM_ERROR_INVALID_DATA;
1576 }
1577 request->heartbeat_timer = htole16(heartbeat_timer);
1578 }
1579
1580 return PLDM_SUCCESS;
1581}
1582
1583int decode_set_event_receiver_resp(const struct pldm_msg *msg,
1584 size_t payload_length,
1585 uint8_t *completion_code)
1586{
1587 if (msg == NULL || completion_code == NULL) {
1588 return PLDM_ERROR_INVALID_DATA;
1589 }
1590
1591 *completion_code = msg->payload[0];
1592 if (PLDM_SUCCESS != *completion_code) {
1593 return PLDM_SUCCESS;
1594 }
1595
1596 if (payload_length > PLDM_SET_EVENT_RECEIVER_RESP_BYTES) {
1597 return PLDM_ERROR_INVALID_LENGTH;
1598 }
1599
1600 return PLDM_SUCCESS;
1601}
Sridevi Ramesh74187522021-04-08 09:50:22 -05001602
1603int decode_set_event_receiver_req(const struct pldm_msg *msg,
1604 size_t payload_length,
1605 uint8_t *event_message_global_enable,
1606 uint8_t *transport_protocol_type,
1607 uint8_t *event_receiver_address_info,
1608 uint16_t *heartbeat_timer)
1609
1610{
1611 if (msg == NULL || event_message_global_enable == NULL ||
1612 transport_protocol_type == NULL ||
1613 event_receiver_address_info == NULL || heartbeat_timer == NULL) {
1614 return PLDM_ERROR_INVALID_DATA;
1615 }
1616
1617 if (payload_length != PLDM_SET_EVENT_RECEIVER_REQ_BYTES) {
1618 return PLDM_ERROR_INVALID_LENGTH;
1619 }
1620
1621 struct pldm_set_event_receiver_req *request =
1622 (struct pldm_set_event_receiver_req *)msg->payload;
1623
1624 if ((*event_message_global_enable ==
1625 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) &&
1626 (*heartbeat_timer == 0)) {
1627 return PLDM_ERROR_INVALID_DATA;
1628 }
1629
1630 *event_message_global_enable = request->event_message_global_enable,
1631 *transport_protocol_type = request->transport_protocol_type,
1632 *event_receiver_address_info = request->event_receiver_address_info,
1633 *heartbeat_timer = le16toh(request->heartbeat_timer);
1634
1635 return PLDM_SUCCESS;
1636}
1637
1638int encode_set_event_receiver_resp(uint8_t instance_id, uint8_t completion_code,
1639 struct pldm_msg *msg)
1640
1641{
Sridevi Ramesh74187522021-04-08 09:50:22 -05001642 if (msg == NULL) {
1643 return PLDM_ERROR_INVALID_DATA;
1644 }
1645
George Liub7095ff2021-06-14 16:01:57 +08001646 struct pldm_header_info header = {0};
Sridevi Ramesh74187522021-04-08 09:50:22 -05001647 header.instance = instance_id;
1648 header.msg_type = PLDM_RESPONSE;
1649 header.pldm_type = PLDM_PLATFORM;
1650 header.command = PLDM_SET_EVENT_RECEIVER;
1651
George Liub7095ff2021-06-14 16:01:57 +08001652 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1653 if (rc != PLDM_SUCCESS) {
1654 return rc;
1655 }
Sridevi Ramesh74187522021-04-08 09:50:22 -05001656
George Liub7095ff2021-06-14 16:01:57 +08001657 msg->payload[0] = completion_code;
1658
1659 return PLDM_SUCCESS;
Sridevi Ramesh74187522021-04-08 09:50:22 -05001660}