blob: 775b1817aa1733e59776d363fd12f5337680226c [file] [log] [blame]
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301#include "config.h"
Andrew Jeffery7992eb82023-04-06 16:13:53 +09302#include "msgbuf/platform.h"
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05303#include "base.h"
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304#include "msgbuf.h"
5#include "platform.h"
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05306#include "pldm_types.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +09307#include <endian.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05308#include <stdint.h>
Thu Nguyen159a98b2022-11-02 10:00:10 +07009#include <stdlib.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +093010#include <string.h>
11
Andrew Jeffery7992eb82023-04-06 16:13:53 +093012static int pldm_platform_pdr_hdr_validate(struct pldm_value_pdr_hdr *ctx,
13 size_t lower, size_t upper)
14{
15 if (ctx->length + sizeof(*ctx) < lower) {
16 return PLDM_ERROR_INVALID_LENGTH;
17 }
18
19 if (ctx->length > upper) {
20 return PLDM_ERROR_INVALID_LENGTH;
21 }
22
23 return PLDM_SUCCESS;
24}
Andrew Jeffery9c766792022-08-10 23:12:49 +093025
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093026LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +093027int encode_state_effecter_pdr(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093028 struct pldm_state_effecter_pdr *const effecter,
29 const size_t allocation_size,
30 const struct state_effecter_possible_states *const possible_states,
31 const size_t possible_states_size, size_t *const actual_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +093032{
33 // Encode possible states
34
35 size_t calculated_possible_states_size = 0;
36
37 {
38 char *states_ptr = (char *)possible_states;
39 char *const begin_states_ptr = states_ptr;
40
41 for (int i = 0; i < effecter->composite_effecter_count; ++i) {
42 struct state_effecter_possible_states *states =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093043 (struct state_effecter_possible_states *)
44 states_ptr;
Andrew Jeffery9c766792022-08-10 23:12:49 +093045
46 HTOLE16(states->state_set_id);
47
48 states_ptr +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093049 (sizeof(*states) - sizeof(states->states) +
50 states->possible_states_size);
Andrew Jeffery9c766792022-08-10 23:12:49 +093051 }
52
53 calculated_possible_states_size = states_ptr - begin_states_ptr;
54 }
55
56 // Check lengths
57
58 if (possible_states_size != calculated_possible_states_size) {
59 *actual_size = 0;
60 return PLDM_ERROR;
61 }
62
63 *actual_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093064 (sizeof(struct pldm_state_effecter_pdr) + possible_states_size -
65 sizeof(effecter->possible_states));
Andrew Jeffery9c766792022-08-10 23:12:49 +093066
67 if (allocation_size < *actual_size) {
68 *actual_size = 0;
69 return PLDM_ERROR_INVALID_LENGTH;
70 }
71
72 // Encode rest of PDR
73
74 effecter->hdr.version = 1;
75 effecter->hdr.type = PLDM_STATE_EFFECTER_PDR;
76 effecter->hdr.length = *actual_size - sizeof(struct pldm_pdr_hdr);
77
78 memcpy(effecter->possible_states, possible_states,
79 possible_states_size);
80
81 // Convert effecter PDR body
82 HTOLE16(effecter->terminus_handle);
83 HTOLE16(effecter->effecter_id);
84 HTOLE16(effecter->entity_type);
85 HTOLE16(effecter->entity_instance);
86 HTOLE16(effecter->container_id);
87 HTOLE16(effecter->effecter_semantic_id);
88
89 // Convert header
90 HTOLE32(effecter->hdr.record_handle);
91 HTOLE16(effecter->hdr.record_change_num);
92 HTOLE16(effecter->hdr.length);
93
94 return PLDM_SUCCESS;
95}
96
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093097LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +093098int encode_state_sensor_pdr(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093099 struct pldm_state_sensor_pdr *const sensor,
100 const size_t allocation_size,
101 const struct state_sensor_possible_states *const possible_states,
102 const size_t possible_states_size, size_t *const actual_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930103{
104 // Encode possible states
105
106 size_t calculated_possible_states_size = 0;
107
108 {
Andrew Jefferyfbe61d72023-04-05 20:28:23 +0930109 char *states_ptr = (char *)possible_states;
110 char *const begin_states_ptr = states_ptr;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930111
112 for (int i = 0; i < sensor->composite_sensor_count; ++i) {
113 struct state_sensor_possible_states *states =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930114 (struct state_sensor_possible_states *)
115 states_ptr;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930116
117 HTOLE16(states->state_set_id);
118
119 states_ptr +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930120 (sizeof(*states) - sizeof(states->states) +
121 states->possible_states_size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930122 }
123
124 calculated_possible_states_size = states_ptr - begin_states_ptr;
125 }
126
127 // Check lengths
128
129 if (possible_states_size != calculated_possible_states_size) {
130 *actual_size = 0;
131 return PLDM_ERROR;
132 }
133
134 *actual_size = (sizeof(struct pldm_state_sensor_pdr) +
135 possible_states_size - sizeof(sensor->possible_states));
136
137 if (allocation_size < *actual_size) {
138 *actual_size = 0;
139 return PLDM_ERROR_INVALID_LENGTH;
140 }
141
142 // Encode rest of PDR
143
144 sensor->hdr.version = 1;
145 sensor->hdr.type = PLDM_STATE_SENSOR_PDR;
146 sensor->hdr.length = *actual_size - sizeof(struct pldm_pdr_hdr);
147
148 memcpy(sensor->possible_states, possible_states, possible_states_size);
149
150 // Convert sensor PDR body
151 HTOLE16(sensor->terminus_handle);
152 HTOLE16(sensor->sensor_id);
153 HTOLE16(sensor->entity_type);
154 HTOLE16(sensor->entity_instance);
155 HTOLE16(sensor->container_id);
156
157 // Convert header
158 HTOLE32(sensor->hdr.record_handle);
159 HTOLE16(sensor->hdr.record_change_num);
160 HTOLE16(sensor->hdr.length);
161
162 return PLDM_SUCCESS;
163}
164
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930165LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930166int encode_set_state_effecter_states_resp(uint8_t instance_id,
167 uint8_t completion_code,
168 struct pldm_msg *msg)
169{
170 if (msg == NULL) {
171 return PLDM_ERROR_INVALID_DATA;
172 }
173
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930174 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930175 header.msg_type = PLDM_RESPONSE;
176 header.instance = instance_id;
177 header.pldm_type = PLDM_PLATFORM;
178 header.command = PLDM_SET_STATE_EFFECTER_STATES;
179
180 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
181 if (rc != PLDM_SUCCESS) {
182 return rc;
183 }
184
185 msg->payload[0] = completion_code;
186
187 return PLDM_SUCCESS;
188}
189
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930190LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930191int encode_set_state_effecter_states_req(uint8_t instance_id,
192 uint16_t effecter_id,
193 uint8_t comp_effecter_count,
194 set_effecter_state_field *field,
195 struct pldm_msg *msg)
196{
197 if (msg == NULL) {
198 return PLDM_ERROR_INVALID_DATA;
199 }
200
201 if (comp_effecter_count < 0x1 || comp_effecter_count > 0x8 ||
202 field == NULL) {
203 return PLDM_ERROR_INVALID_DATA;
204 }
205
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930206 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930207 header.msg_type = PLDM_REQUEST;
208 header.instance = instance_id;
209 header.pldm_type = PLDM_PLATFORM;
210 header.command = PLDM_SET_STATE_EFFECTER_STATES;
211
212 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
213 if (rc != PLDM_SUCCESS) {
214 return rc;
215 }
216
217 struct pldm_set_state_effecter_states_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930218 (struct pldm_set_state_effecter_states_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930219 effecter_id = htole16(effecter_id);
220 request->effecter_id = effecter_id;
221 request->comp_effecter_count = comp_effecter_count;
222 memcpy(request->field, field,
223 (sizeof(set_effecter_state_field) * comp_effecter_count));
224
225 return PLDM_SUCCESS;
226}
227
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930228LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930229int decode_set_state_effecter_states_resp(const struct pldm_msg *msg,
230 size_t payload_length,
231 uint8_t *completion_code)
232{
233 if (msg == NULL || completion_code == NULL) {
234 return PLDM_ERROR_INVALID_DATA;
235 }
236
237 *completion_code = msg->payload[0];
238 if (PLDM_SUCCESS != *completion_code) {
239 return PLDM_SUCCESS;
240 }
241
242 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES) {
243 return PLDM_ERROR_INVALID_LENGTH;
244 }
245
246 return PLDM_SUCCESS;
247}
248
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930249#define PLDM_SET_STATE_EFFECTER_STATES_MIN_SIZE 3
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930250LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930251int decode_set_state_effecter_states_req(const struct pldm_msg *msg,
252 size_t payload_length,
253 uint16_t *effecter_id,
254 uint8_t *comp_effecter_count,
255 set_effecter_state_field *field)
256{
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930257 struct pldm_msgbuf _buf;
258 struct pldm_msgbuf *buf = &_buf;
259 int rc;
260 int i;
261
Andrew Jeffery9c766792022-08-10 23:12:49 +0930262 if (msg == NULL || effecter_id == NULL || comp_effecter_count == NULL ||
263 field == NULL) {
264 return PLDM_ERROR_INVALID_DATA;
265 }
266
267 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) {
268 return PLDM_ERROR_INVALID_LENGTH;
269 }
270
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930271 rc = pldm_msgbuf_init(buf, PLDM_SET_STATE_EFFECTER_STATES_MIN_SIZE,
272 msg->payload, payload_length);
273 if (rc) {
274 return rc;
275 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930276
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930277 pldm_msgbuf_extract(buf, effecter_id);
278 pldm_msgbuf_extract(buf, comp_effecter_count);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930279
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930280 if (*comp_effecter_count > 8) {
281 return PLDM_ERROR_INVALID_DATA;
282 }
283
284 for (i = 0; i < *comp_effecter_count; i++) {
285 pldm_msgbuf_extract(buf, &field[i].set_request);
286 pldm_msgbuf_extract(buf, &field[i].effecter_state);
287 }
288
289 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930290}
291
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930292LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930293int decode_get_pdr_req(const struct pldm_msg *msg, size_t payload_length,
294 uint32_t *record_hndl, uint32_t *data_transfer_hndl,
295 uint8_t *transfer_op_flag, uint16_t *request_cnt,
296 uint16_t *record_chg_num)
297{
Andrew Jeffery891781e2023-04-04 11:04:18 +0930298 struct pldm_msgbuf _buf;
299 struct pldm_msgbuf *buf = &_buf;
300 int rc;
301
Andrew Jeffery9c766792022-08-10 23:12:49 +0930302 if (msg == NULL || record_hndl == NULL || data_transfer_hndl == NULL ||
303 transfer_op_flag == NULL || request_cnt == NULL ||
304 record_chg_num == NULL) {
305 return PLDM_ERROR_INVALID_DATA;
306 }
Andrew Jeffery891781e2023-04-04 11:04:18 +0930307
Andrew Jeffery9c766792022-08-10 23:12:49 +0930308 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
309 return PLDM_ERROR_INVALID_LENGTH;
310 }
311
Andrew Jeffery891781e2023-04-04 11:04:18 +0930312 rc = pldm_msgbuf_init(buf, PLDM_GET_PDR_REQ_BYTES, msg->payload,
313 payload_length);
314 if (rc) {
315 return rc;
316 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930317
Andrew Jeffery891781e2023-04-04 11:04:18 +0930318 pldm_msgbuf_extract(buf, record_hndl);
319 pldm_msgbuf_extract(buf, data_transfer_hndl);
320 pldm_msgbuf_extract(buf, transfer_op_flag);
321 pldm_msgbuf_extract(buf, request_cnt);
322 pldm_msgbuf_extract(buf, record_chg_num);
323
324 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930325}
326
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930327LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930328int encode_get_pdr_resp(uint8_t instance_id, uint8_t completion_code,
329 uint32_t next_record_hndl,
330 uint32_t next_data_transfer_hndl, uint8_t transfer_flag,
331 uint16_t resp_cnt, const uint8_t *record_data,
332 uint8_t transfer_crc, struct pldm_msg *msg)
333{
334 if (msg == NULL) {
335 return PLDM_ERROR_INVALID_DATA;
336 }
337
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930338 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930339 header.msg_type = PLDM_RESPONSE;
340 header.instance = instance_id;
341 header.pldm_type = PLDM_PLATFORM;
342 header.command = PLDM_GET_PDR;
343
344 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
345 if (rc != PLDM_SUCCESS) {
346 return rc;
347 }
348
349 struct pldm_get_pdr_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930350 (struct pldm_get_pdr_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930351 response->completion_code = completion_code;
352
353 if (response->completion_code == PLDM_SUCCESS) {
354 response->next_record_handle = htole32(next_record_hndl);
355 response->next_data_transfer_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930356 htole32(next_data_transfer_hndl);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930357 response->transfer_flag = transfer_flag;
358 response->response_count = htole16(resp_cnt);
359 if (record_data != NULL && resp_cnt > 0) {
360 memcpy(response->record_data, record_data, resp_cnt);
361 }
362 if (transfer_flag == PLDM_END) {
363 uint8_t *dst = msg->payload;
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930364 dst += (sizeof(struct pldm_get_pdr_resp) - 1) +
365 resp_cnt;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930366 *dst = transfer_crc;
367 }
368 }
369
370 return PLDM_SUCCESS;
371}
372
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930373LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930374int encode_get_pdr_repository_info_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930375 uint8_t instance_id, uint8_t completion_code, uint8_t repository_state,
376 const uint8_t *update_time, const uint8_t *oem_update_time,
377 uint32_t record_count, uint32_t repository_size,
378 uint32_t largest_record_size, uint8_t data_transfer_handle_timeout,
379 struct pldm_msg *msg)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930380{
381 if (msg == NULL) {
382 return PLDM_ERROR_INVALID_DATA;
383 }
384
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930385 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930386 header.msg_type = PLDM_RESPONSE;
387 header.instance = instance_id;
388 header.pldm_type = PLDM_PLATFORM;
389 header.command = PLDM_GET_PDR_REPOSITORY_INFO;
390
391 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
392 if (rc != PLDM_SUCCESS) {
393 return rc;
394 }
395
396 struct pldm_pdr_repository_info_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930397 (struct pldm_pdr_repository_info_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930398 response->completion_code = completion_code;
399
400 if (response->completion_code == PLDM_SUCCESS) {
401 response->repository_state = repository_state;
402 if (update_time != NULL) {
403 memcpy(response->update_time, update_time,
404 PLDM_TIMESTAMP104_SIZE);
405 }
406 if (oem_update_time != NULL) {
407 memcpy(response->oem_update_time, oem_update_time,
408 PLDM_TIMESTAMP104_SIZE);
409 }
410 response->record_count = htole32(record_count);
411 response->repository_size = htole32(repository_size);
412 response->largest_record_size = htole32(largest_record_size);
413 response->data_transfer_handle_timeout =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930414 data_transfer_handle_timeout;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930415 }
416
417 return PLDM_SUCCESS;
418}
419
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930420LIBPLDM_ABI_STABLE
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800421int decode_get_pdr_repository_info_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930422 const struct pldm_msg *msg, size_t payload_length,
423 uint8_t *completion_code, uint8_t *repository_state,
424 uint8_t *update_time, uint8_t *oem_update_time, uint32_t *record_count,
425 uint32_t *repository_size, uint32_t *largest_record_size,
426 uint8_t *data_transfer_handle_timeout)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800427{
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930428 struct pldm_msgbuf _buf;
429 struct pldm_msgbuf *buf = &_buf;
430 int rc;
431
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800432 if (msg == NULL || completion_code == NULL ||
433 repository_state == NULL || update_time == NULL ||
434 oem_update_time == NULL || record_count == NULL ||
435 repository_size == NULL || largest_record_size == NULL ||
436 data_transfer_handle_timeout == NULL) {
437 return PLDM_ERROR_INVALID_DATA;
438 }
439
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930440 rc = pldm_msgbuf_init(buf, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES,
441 msg->payload, payload_length);
442 if (rc) {
443 return rc;
444 }
445
446 pldm_msgbuf_extract(buf, completion_code);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800447 if (PLDM_SUCCESS != *completion_code) {
448 return PLDM_SUCCESS;
449 }
450
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930451 pldm_msgbuf_extract(buf, repository_state);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800452 if (*repository_state > PLDM_FAILED) {
453 return PLDM_ERROR_INVALID_DATA;
454 }
455
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930456 pldm_msgbuf_extract_array(buf, update_time, PLDM_TIMESTAMP104_SIZE);
457 pldm_msgbuf_extract_array(buf, oem_update_time, PLDM_TIMESTAMP104_SIZE);
458 pldm_msgbuf_extract(buf, record_count);
459 pldm_msgbuf_extract(buf, repository_size);
460 pldm_msgbuf_extract(buf, largest_record_size);
461 pldm_msgbuf_extract(buf, data_transfer_handle_timeout);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800462
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930463 return pldm_msgbuf_destroy(buf);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800464}
465
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930466LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930467int encode_get_pdr_req(uint8_t instance_id, uint32_t record_hndl,
468 uint32_t data_transfer_hndl, uint8_t transfer_op_flag,
469 uint16_t request_cnt, uint16_t record_chg_num,
470 struct pldm_msg *msg, size_t payload_length)
471{
472 if (msg == NULL) {
473 return PLDM_ERROR_INVALID_DATA;
474 }
475
476 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
477 return PLDM_ERROR_INVALID_LENGTH;
478 }
479
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930480 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930481 header.msg_type = PLDM_REQUEST;
482 header.instance = instance_id;
483 header.pldm_type = PLDM_PLATFORM;
484 header.command = PLDM_GET_PDR;
485
486 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
487 if (rc != PLDM_SUCCESS) {
488 return rc;
489 }
490
491 struct pldm_get_pdr_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930492 (struct pldm_get_pdr_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930493 request->record_handle = htole32(record_hndl);
494 request->data_transfer_handle = htole32(data_transfer_hndl);
495 request->transfer_op_flag = transfer_op_flag;
496 request->request_count = htole16(request_cnt);
497 request->record_change_number = htole16(record_chg_num);
498
499 return PLDM_SUCCESS;
500}
501
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930502LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930503int decode_get_pdr_resp(const struct pldm_msg *msg, size_t payload_length,
504 uint8_t *completion_code, uint32_t *next_record_hndl,
505 uint32_t *next_data_transfer_hndl,
506 uint8_t *transfer_flag, uint16_t *resp_cnt,
507 uint8_t *record_data, size_t record_data_length,
508 uint8_t *transfer_crc)
509{
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930510 struct pldm_msgbuf _buf;
511 struct pldm_msgbuf *buf = &_buf;
512 int rc;
513
Andrew Jeffery9c766792022-08-10 23:12:49 +0930514 if (msg == NULL || completion_code == NULL ||
515 next_record_hndl == NULL || next_data_transfer_hndl == NULL ||
516 transfer_flag == NULL || resp_cnt == NULL || transfer_crc == NULL) {
517 return PLDM_ERROR_INVALID_DATA;
518 }
519
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930520 rc = pldm_msgbuf_init(buf, PLDM_GET_PDR_MIN_RESP_BYTES, msg->payload,
521 payload_length);
522 if (rc) {
523 return rc;
524 }
525
526 pldm_msgbuf_extract(buf, completion_code);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930527 if (PLDM_SUCCESS != *completion_code) {
528 return PLDM_SUCCESS;
529 }
530
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930531 pldm_msgbuf_extract(buf, next_record_hndl);
532 pldm_msgbuf_extract(buf, next_data_transfer_hndl);
533 pldm_msgbuf_extract(buf, transfer_flag);
534 rc = pldm_msgbuf_extract(buf, resp_cnt);
535 if (rc) {
536 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930537 }
538
539 if (*resp_cnt > 0 && record_data != NULL) {
540 if (record_data_length < *resp_cnt) {
541 return PLDM_ERROR_INVALID_LENGTH;
542 }
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930543 pldm_msgbuf_extract_array(buf, record_data, *resp_cnt);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930544 }
545
546 if (*transfer_flag == PLDM_END) {
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930547 pldm_msgbuf_extract(buf, transfer_crc);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930548 }
549
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930550 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930551}
552
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930553LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930554int decode_set_numeric_effecter_value_req(const struct pldm_msg *msg,
555 size_t payload_length,
556 uint16_t *effecter_id,
557 uint8_t *effecter_data_size,
Andrew Jeffery3884c442023-04-12 11:13:24 +0930558 uint8_t effecter_value[4])
Andrew Jeffery9c766792022-08-10 23:12:49 +0930559{
Andrew Jeffery3884c442023-04-12 11:13:24 +0930560 struct pldm_msgbuf _buf;
561 struct pldm_msgbuf *buf = &_buf;
562 int rc;
563
Andrew Jeffery9c766792022-08-10 23:12:49 +0930564 if (msg == NULL || effecter_id == NULL || effecter_data_size == NULL ||
565 effecter_value == NULL) {
566 return PLDM_ERROR_INVALID_DATA;
567 }
568
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930569 rc = pldm_msgbuf_init(buf,
570 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES,
571 msg->payload, payload_length);
Andrew Jeffery3884c442023-04-12 11:13:24 +0930572 if (rc) {
573 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930574 }
575
Andrew Jeffery3884c442023-04-12 11:13:24 +0930576 pldm_msgbuf_extract(buf, effecter_id);
577 rc = pldm_msgbuf_extract(buf, effecter_data_size);
578 if (rc) {
579 return PLDM_ERROR_INVALID_DATA;
580 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930581
582 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
583 return PLDM_ERROR_INVALID_DATA;
584 }
585
Andrew Jeffery3884c442023-04-12 11:13:24 +0930586 pldm_msgbuf_extract_effecter_value(buf, *effecter_data_size,
587 effecter_value);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930588
Andrew Jeffery3884c442023-04-12 11:13:24 +0930589 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930590}
591
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930592LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930593int encode_set_numeric_effecter_value_resp(uint8_t instance_id,
594 uint8_t completion_code,
595 struct pldm_msg *msg,
596 size_t payload_length)
597{
598 if (msg == NULL) {
599 return PLDM_ERROR_INVALID_DATA;
600 }
601
602 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
603 return PLDM_ERROR_INVALID_LENGTH;
604 }
605
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930606 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930607 header.msg_type = PLDM_RESPONSE;
608 header.instance = instance_id;
609 header.pldm_type = PLDM_PLATFORM;
610 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
611
612 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
613 if (rc != PLDM_SUCCESS) {
614 return rc;
615 }
616
617 msg->payload[0] = completion_code;
618
619 return rc;
620}
621
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930622LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930623int encode_set_numeric_effecter_value_req(uint8_t instance_id,
624 uint16_t effecter_id,
625 uint8_t effecter_data_size,
626 const uint8_t *effecter_value,
627 struct pldm_msg *msg,
628 size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930629{
630 if (msg == NULL || effecter_value == NULL) {
631 return PLDM_ERROR_INVALID_DATA;
632 }
633
634 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
635 return PLDM_ERROR_INVALID_DATA;
636 }
637
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930638 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930639 header.msg_type = PLDM_REQUEST;
640 header.instance = instance_id;
641 header.pldm_type = PLDM_PLATFORM;
642 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
643
644 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
645 if (rc != PLDM_SUCCESS) {
646 return rc;
647 }
648
649 struct pldm_set_numeric_effecter_value_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930650 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930651 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
652 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
653 if (payload_length !=
654 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
655 return PLDM_ERROR_INVALID_LENGTH;
656 }
657 request->effecter_value[0] = *effecter_value;
658 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
659 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
660 if (payload_length !=
661 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
662 return PLDM_ERROR_INVALID_LENGTH;
663 }
664
665 uint16_t val = *(uint16_t *)(effecter_value);
666 val = htole16(val);
667 memcpy(request->effecter_value, &val, sizeof(uint16_t));
668
669 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
670 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
671 if (payload_length !=
672 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
673 return PLDM_ERROR_INVALID_LENGTH;
674 }
675
676 uint32_t val = *(uint32_t *)(effecter_value);
677 val = htole32(val);
678 memcpy(request->effecter_value, &val, sizeof(uint32_t));
679 }
680
681 request->effecter_id = htole16(effecter_id);
682 request->effecter_data_size = effecter_data_size;
683
684 return PLDM_SUCCESS;
685}
686
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930687LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930688int decode_set_numeric_effecter_value_resp(const struct pldm_msg *msg,
689 size_t payload_length,
690 uint8_t *completion_code)
691{
692 if (msg == NULL || completion_code == NULL) {
693 return PLDM_ERROR_INVALID_DATA;
694 }
695
696 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
697 return PLDM_ERROR_INVALID_LENGTH;
698 }
699
700 *completion_code = msg->payload[0];
701
702 return PLDM_SUCCESS;
703}
704
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930705LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930706int encode_get_state_sensor_readings_resp(uint8_t instance_id,
707 uint8_t completion_code,
708 uint8_t comp_sensor_count,
709 get_sensor_state_field *field,
710 struct pldm_msg *msg)
711{
712 if (msg == NULL) {
713 return PLDM_ERROR_INVALID_DATA;
714 }
715
716 if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
717 return PLDM_ERROR_INVALID_DATA;
718 }
719
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930720 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930721 header.msg_type = PLDM_RESPONSE;
722 header.instance = instance_id;
723 header.pldm_type = PLDM_PLATFORM;
724 header.command = PLDM_GET_STATE_SENSOR_READINGS;
725
726 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
727 if (rc != PLDM_SUCCESS) {
728 return rc;
729 }
730
731 struct pldm_get_state_sensor_readings_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930732 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930733
734 response->completion_code = completion_code;
735 response->comp_sensor_count = comp_sensor_count;
736 memcpy(response->field, field,
737 (sizeof(get_sensor_state_field) * comp_sensor_count));
738
739 return PLDM_SUCCESS;
740}
741
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930742LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930743int encode_get_state_sensor_readings_req(uint8_t instance_id,
744 uint16_t sensor_id,
745 bitfield8_t sensor_rearm,
746 uint8_t reserved, struct pldm_msg *msg)
747{
748 if (msg == NULL) {
749 return PLDM_ERROR_INVALID_DATA;
750 }
751
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930752 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930753 header.msg_type = PLDM_REQUEST;
754 header.instance = instance_id;
755 header.pldm_type = PLDM_PLATFORM;
756 header.command = PLDM_GET_STATE_SENSOR_READINGS;
757
758 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
759 if (rc != PLDM_SUCCESS) {
760 return rc;
761 }
762
763 struct pldm_get_state_sensor_readings_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930764 (struct pldm_get_state_sensor_readings_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930765
766 request->sensor_id = htole16(sensor_id);
767 request->reserved = reserved;
768 request->sensor_rearm = sensor_rearm;
769
770 return PLDM_SUCCESS;
771}
772
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930773LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930774int decode_get_state_sensor_readings_resp(const struct pldm_msg *msg,
775 size_t payload_length,
776 uint8_t *completion_code,
777 uint8_t *comp_sensor_count,
778 get_sensor_state_field *field)
779{
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930780 struct pldm_msgbuf _buf;
781 struct pldm_msgbuf *buf = &_buf;
782 uint8_t i;
783 int rc;
784
Andrew Jeffery9c766792022-08-10 23:12:49 +0930785 if (msg == NULL || completion_code == NULL ||
786 comp_sensor_count == NULL || field == NULL) {
787 return PLDM_ERROR_INVALID_DATA;
788 }
789
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930790 rc = pldm_msgbuf_init(buf,
791 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES,
792 msg->payload, payload_length);
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930793 if (rc) {
794 return rc;
795 }
796
797 rc = pldm_msgbuf_extract(buf, completion_code);
798 if (rc) {
799 return rc;
800 }
801
Andrew Jeffery9c766792022-08-10 23:12:49 +0930802 if (PLDM_SUCCESS != *completion_code) {
803 return PLDM_SUCCESS;
804 }
805
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930806 rc = pldm_msgbuf_extract(buf, comp_sensor_count);
807 if (rc) {
808 return rc;
809 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930810
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930811 if (*comp_sensor_count < 0x1 || *comp_sensor_count > 0x8) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930812 return PLDM_ERROR_INVALID_DATA;
813 }
814
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930815 for (i = 0; i < *comp_sensor_count; i++) {
816 pldm_msgbuf_extract(buf, &field[i].sensor_op_state);
817 pldm_msgbuf_extract(buf, &field[i].present_state);
818 pldm_msgbuf_extract(buf, &field[i].previous_state);
819 pldm_msgbuf_extract(buf, &field[i].event_state);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930820 }
821
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930822 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930823}
824
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930825LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930826int decode_get_state_sensor_readings_req(const struct pldm_msg *msg,
827 size_t payload_length,
828 uint16_t *sensor_id,
829 bitfield8_t *sensor_rearm,
830 uint8_t *reserved)
831{
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930832 struct pldm_msgbuf _buf;
833 struct pldm_msgbuf *buf = &_buf;
834 int rc;
835
Andrew Jeffery9c766792022-08-10 23:12:49 +0930836 if (msg == NULL || sensor_id == NULL || sensor_rearm == NULL) {
837 return PLDM_ERROR_INVALID_DATA;
838 }
839
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930840 rc = pldm_msgbuf_init(buf, PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES,
841 msg->payload, payload_length);
842 if (rc) {
843 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930844 }
845
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930846 pldm_msgbuf_extract(buf, sensor_id);
847 pldm_msgbuf_extract(buf, &sensor_rearm->byte);
848 pldm_msgbuf_extract(buf, reserved);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930849
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930850 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930851}
852
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930853LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930854int encode_sensor_event_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930855 struct pldm_sensor_event_data *const event_data,
856 const size_t event_data_size, const uint16_t sensor_id,
857 const enum sensor_event_class_states sensor_event_class,
858 const uint8_t sensor_offset, const uint8_t event_state,
859 const uint8_t previous_event_state,
860 size_t *const actual_event_data_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930861{
862 *actual_event_data_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930863 (sizeof(*event_data) - sizeof(event_data->event_class) +
864 sizeof(struct pldm_sensor_event_state_sensor_state));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930865
866 if (!event_data) {
867 return PLDM_SUCCESS;
868 }
869
870 if (event_data_size < *actual_event_data_size) {
871 *actual_event_data_size = 0;
872 return PLDM_ERROR_INVALID_LENGTH;
873 }
874
875 event_data->sensor_id = htole16(sensor_id);
876 event_data->sensor_event_class_type = sensor_event_class;
877
878 struct pldm_sensor_event_state_sensor_state *const state_data =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930879 (struct pldm_sensor_event_state_sensor_state *)
880 event_data->event_class;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930881
882 state_data->sensor_offset = sensor_offset;
883 state_data->event_state = event_state;
884 state_data->previous_event_state = previous_event_state;
885
886 return PLDM_SUCCESS;
887}
888
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930889LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930890int decode_platform_event_message_req(const struct pldm_msg *msg,
891 size_t payload_length,
892 uint8_t *format_version, uint8_t *tid,
893 uint8_t *event_class,
894 size_t *event_data_offset)
895{
Andrew Jefferydc48ce32023-04-13 12:01:42 +0930896 struct pldm_msgbuf _buf;
897 struct pldm_msgbuf *buf = &_buf;
898 int rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930899
900 if (msg == NULL || format_version == NULL || tid == NULL ||
901 event_class == NULL || event_data_offset == NULL) {
902 return PLDM_ERROR_INVALID_DATA;
903 }
904
Andrew Jefferydc48ce32023-04-13 12:01:42 +0930905 rc = pldm_msgbuf_init(buf, PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES,
906 msg->payload, payload_length);
907 if (rc) {
908 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930909 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930910
Andrew Jefferydc48ce32023-04-13 12:01:42 +0930911 pldm_msgbuf_extract(buf, format_version);
912 pldm_msgbuf_extract(buf, tid);
913 pldm_msgbuf_extract(buf, event_class);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930914 *event_data_offset =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930915 sizeof(*format_version) + sizeof(*tid) + sizeof(*event_class);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930916
Andrew Jefferydc48ce32023-04-13 12:01:42 +0930917 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930918}
919
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930920LIBPLDM_ABI_STABLE
Thu Nguyen8eb20f22022-11-16 22:34:55 +0700921int decode_poll_for_platform_event_message_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930922 const struct pldm_msg *msg, size_t payload_length,
923 uint8_t *format_version, uint8_t *transfer_operation_flag,
924 uint32_t *data_transfer_handle, uint16_t *event_id_to_acknowledge)
Thu Nguyen8eb20f22022-11-16 22:34:55 +0700925{
926 struct pldm_msgbuf _buf;
927 struct pldm_msgbuf *buf = &_buf;
928 int rc;
929
930 if (msg == NULL) {
931 return PLDM_ERROR_INVALID_DATA;
932 }
933
934 rc = pldm_msgbuf_init(buf,
935 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES,
936 msg->payload, payload_length);
937 if (rc) {
938 return rc;
939 }
940
941 pldm_msgbuf_extract(buf, format_version);
942 rc = pldm_msgbuf_extract(buf, transfer_operation_flag);
943 if (rc) {
944 return rc;
945 }
946 if (*transfer_operation_flag > PLDM_ACKNOWLEDGEMENT_ONLY) {
947 return PLDM_ERROR_INVALID_DATA;
948 }
949
950 pldm_msgbuf_extract(buf, data_transfer_handle);
951 rc = pldm_msgbuf_extract(buf, event_id_to_acknowledge);
952 if (rc) {
953 return rc;
954 }
955
956 if (!(((*transfer_operation_flag == PLDM_GET_NEXTPART) &&
957 (*event_id_to_acknowledge == 0xFFFF)) ||
958 ((*transfer_operation_flag == PLDM_GET_FIRSTPART) &&
959 (*event_id_to_acknowledge == 0x000)) ||
960 (*transfer_operation_flag == PLDM_ACKNOWLEDGEMENT_ONLY))) {
961 return PLDM_ERROR_INVALID_DATA;
962 }
963
964 return pldm_msgbuf_destroy(buf);
965}
966
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930967LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930968int encode_platform_event_message_resp(uint8_t instance_id,
969 uint8_t completion_code,
970 uint8_t platform_event_status,
971 struct pldm_msg *msg)
972{
973 if (msg == NULL) {
974 return PLDM_ERROR_INVALID_DATA;
975 }
976
977 if (platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
978 return PLDM_ERROR_INVALID_DATA;
979 }
980
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930981 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930982 header.msg_type = PLDM_RESPONSE;
983 header.instance = instance_id;
984 header.pldm_type = PLDM_PLATFORM;
985 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
986
987 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
988 if (rc != PLDM_SUCCESS) {
989 return rc;
990 }
991
992 struct pldm_platform_event_message_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930993 (struct pldm_platform_event_message_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930994 response->completion_code = completion_code;
995 response->platform_event_status = platform_event_status;
996
997 return PLDM_SUCCESS;
998}
999
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301000LIBPLDM_ABI_STABLE
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001001int encode_poll_for_platform_event_message_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301002 uint8_t instance_id, uint8_t completion_code, uint8_t tid,
1003 uint16_t event_id, uint32_t next_data_transfer_handle,
1004 uint8_t transfer_flag, uint8_t event_class, uint32_t event_data_size,
1005 uint8_t *event_data, uint32_t checksum, struct pldm_msg *msg,
1006 size_t payload_length)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001007{
1008 struct pldm_msgbuf _buf;
1009 struct pldm_msgbuf *buf = &_buf;
1010 int rc;
1011
1012 if (!msg) {
1013 return PLDM_ERROR_INVALID_DATA;
1014 }
1015
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301016 struct pldm_header_info header = { 0 };
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001017 header.msg_type = PLDM_RESPONSE;
1018 header.instance = instance_id;
1019 header.pldm_type = PLDM_PLATFORM;
1020 header.command = PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE;
1021
1022 rc = pack_pldm_header(&header, &(msg->hdr));
1023 if (rc != PLDM_SUCCESS) {
1024 return rc;
1025 }
1026
1027 rc = pldm_msgbuf_init(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301028 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
1029 msg->payload, payload_length);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001030 if (rc) {
1031 return rc;
1032 }
1033
1034 pldm_msgbuf_insert(buf, completion_code);
1035 pldm_msgbuf_insert(buf, tid);
1036 pldm_msgbuf_insert(buf, event_id);
1037
1038 if (event_id == 0xffff || event_id == 0x0000) {
1039 if (PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES !=
1040 payload_length) {
1041 return PLDM_ERROR_INVALID_LENGTH;
1042 }
1043 return pldm_msgbuf_destroy(buf);
1044 }
1045
1046 if ((event_data == NULL) && (event_data_size > 0)) {
1047 return PLDM_ERROR_INVALID_DATA;
1048 }
1049
1050 pldm_msgbuf_insert(buf, next_data_transfer_handle);
1051 pldm_msgbuf_insert(buf, transfer_flag);
1052 pldm_msgbuf_insert(buf, event_class);
1053 pldm_msgbuf_insert(buf, event_data_size);
1054
1055 if ((event_data_size > 0) && event_data) {
1056 pldm_msgbuf_insert_array(buf, event_data, event_data_size);
1057 }
1058
1059 if (transfer_flag == PLDM_END || transfer_flag == PLDM_START_AND_END) {
1060 pldm_msgbuf_insert(buf, checksum);
1061 }
1062
1063 return pldm_msgbuf_destroy(buf);
1064}
1065
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301066LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301067int encode_platform_event_message_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301068 uint8_t instance_id, uint8_t format_version, uint8_t tid,
1069 uint8_t event_class, const uint8_t *event_data,
1070 size_t event_data_length, struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301071
1072{
1073 if (format_version != 1) {
1074 return PLDM_ERROR_INVALID_DATA;
1075 }
1076
1077 if (msg == NULL || event_data == NULL) {
1078 return PLDM_ERROR_INVALID_DATA;
1079 }
1080
1081 if (event_data_length == 0) {
1082 return PLDM_ERROR_INVALID_DATA;
1083 }
1084
1085 if (payload_length !=
1086 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + event_data_length) {
1087 return PLDM_ERROR_INVALID_LENGTH;
1088 }
1089
1090 if (event_class > PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT &&
1091 !(event_class >= 0xF0 && event_class <= 0xFE)) {
1092 return PLDM_ERROR_INVALID_DATA;
1093 }
1094
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301095 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301096 header.msg_type = PLDM_REQUEST;
1097 header.instance = instance_id;
1098 header.pldm_type = PLDM_PLATFORM;
1099 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
1100
1101 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1102 if (rc != PLDM_SUCCESS) {
1103 return rc;
1104 }
1105
1106 struct pldm_platform_event_message_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301107 (struct pldm_platform_event_message_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301108 request->format_version = format_version;
1109 request->tid = tid;
1110 request->event_class = event_class;
1111 memcpy(request->event_data, event_data, event_data_length);
1112
1113 return PLDM_SUCCESS;
1114}
1115
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301116LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301117int decode_platform_event_message_resp(const struct pldm_msg *msg,
1118 size_t payload_length,
1119 uint8_t *completion_code,
1120 uint8_t *platform_event_status)
1121{
Andrew Jefferye5011772023-04-13 12:06:22 +09301122 struct pldm_msgbuf _buf;
1123 struct pldm_msgbuf *buf = &_buf;
1124 int rc;
1125
Andrew Jeffery9c766792022-08-10 23:12:49 +09301126 if (msg == NULL || completion_code == NULL ||
1127 platform_event_status == NULL) {
1128 return PLDM_ERROR_INVALID_DATA;
1129 }
1130
Andrew Jefferye5011772023-04-13 12:06:22 +09301131 rc = pldm_msgbuf_init(buf, PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES,
1132 msg->payload, payload_length);
1133 if (rc) {
1134 return rc;
1135 }
1136
1137 rc = pldm_msgbuf_extract(buf, completion_code);
1138 if (rc) {
1139 return rc;
1140 }
1141
Andrew Jeffery9c766792022-08-10 23:12:49 +09301142 if (PLDM_SUCCESS != *completion_code) {
1143 return PLDM_SUCCESS;
1144 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301145
Andrew Jefferye5011772023-04-13 12:06:22 +09301146 rc = pldm_msgbuf_extract(buf, platform_event_status);
1147 if (rc) {
1148 return rc;
1149 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301150
1151 if (*platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
1152 return PLDM_ERROR_INVALID_DATA;
1153 }
1154
Andrew Jefferye5011772023-04-13 12:06:22 +09301155 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301156}
1157
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301158LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301159int encode_event_message_buffer_size_req(uint8_t instance_id,
1160 uint16_t event_receiver_max_buffer_size,
1161 struct pldm_msg *msg)
Dung Caod6ae8982022-11-02 10:00:10 +07001162{
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301163 struct pldm_header_info header = { 0 };
Dung Caod6ae8982022-11-02 10:00:10 +07001164 header.msg_type = PLDM_REQUEST;
1165 header.instance = instance_id;
1166 header.pldm_type = PLDM_PLATFORM;
1167 header.command = PLDM_EVENT_MESSAGE_BUFFER_SIZE;
1168
1169 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1170 if (rc != PLDM_SUCCESS) {
1171 return rc;
1172 }
1173
1174 struct pldm_event_message_buffer_size_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301175 (struct pldm_event_message_buffer_size_req *)msg->payload;
Dung Caod6ae8982022-11-02 10:00:10 +07001176 request->event_receiver_max_buffer_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301177 event_receiver_max_buffer_size;
Dung Caod6ae8982022-11-02 10:00:10 +07001178
1179 return PLDM_SUCCESS;
1180}
1181
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301182LIBPLDM_ABI_STABLE
Dung Caod6ae8982022-11-02 10:00:10 +07001183int decode_event_message_buffer_size_resp(const struct pldm_msg *msg,
1184 size_t payload_length,
1185 uint8_t *completion_code,
1186 uint16_t *terminus_max_buffer_size)
1187{
Andrew Jeffery11126902023-04-13 12:12:10 +09301188 struct pldm_msgbuf _buf;
1189 struct pldm_msgbuf *buf = &_buf;
1190 int rc;
1191
Dung Caod6ae8982022-11-02 10:00:10 +07001192 if (msg == NULL || completion_code == NULL ||
1193 terminus_max_buffer_size == NULL) {
1194 return PLDM_ERROR_INVALID_DATA;
1195 }
1196
Andrew Jeffery11126902023-04-13 12:12:10 +09301197 rc = pldm_msgbuf_init(buf, PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES,
1198 msg->payload, payload_length);
1199 if (rc) {
1200 return rc;
1201 }
1202
1203 rc = pldm_msgbuf_extract(buf, completion_code);
1204 if (rc) {
1205 return rc;
1206 }
1207
Dung Caod6ae8982022-11-02 10:00:10 +07001208 if (PLDM_SUCCESS != *completion_code) {
1209 return PLDM_SUCCESS;
1210 }
Dung Caod6ae8982022-11-02 10:00:10 +07001211
Andrew Jeffery11126902023-04-13 12:12:10 +09301212 pldm_msgbuf_extract(buf, terminus_max_buffer_size);
Dung Caod6ae8982022-11-02 10:00:10 +07001213
Andrew Jeffery11126902023-04-13 12:12:10 +09301214 return pldm_msgbuf_destroy_consumed(buf);
Dung Caod6ae8982022-11-02 10:00:10 +07001215}
1216
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301217LIBPLDM_ABI_STABLE
Dung Cao1bf8c872022-11-29 05:32:58 +07001218int encode_event_message_supported_req(uint8_t instance_id,
1219 uint8_t format_version,
1220 struct pldm_msg *msg)
1221{
1222 if (format_version != 1) {
1223 return PLDM_ERROR_INVALID_DATA;
1224 }
1225
1226 if (msg == NULL) {
1227 return PLDM_ERROR_INVALID_DATA;
1228 }
1229
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301230 struct pldm_header_info header = { 0 };
Dung Cao1bf8c872022-11-29 05:32:58 +07001231 header.msg_type = PLDM_REQUEST;
1232 header.instance = instance_id;
1233 header.pldm_type = PLDM_PLATFORM;
1234 header.command = PLDM_EVENT_MESSAGE_SUPPORTED;
1235
1236 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1237 if (rc != PLDM_SUCCESS) {
1238 return rc;
1239 }
1240
1241 struct pldm_event_message_supported_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301242 (struct pldm_event_message_supported_req *)msg->payload;
Dung Cao1bf8c872022-11-29 05:32:58 +07001243 request->format_version = format_version;
1244
1245 return PLDM_SUCCESS;
1246}
1247
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301248LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301249int decode_event_message_supported_resp(const struct pldm_msg *msg,
1250 size_t payload_length,
1251 uint8_t *completion_code,
1252 uint8_t *synchrony_config,
1253 bitfield8_t *synchrony_config_support,
1254 uint8_t *number_event_class_returned,
1255 uint8_t *event_class,
1256 uint8_t event_class_count)
Dung Cao1bf8c872022-11-29 05:32:58 +07001257{
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301258 struct pldm_msgbuf _buf;
1259 struct pldm_msgbuf *buf = &_buf;
1260 int i;
1261 int rc;
1262
Dung Cao1bf8c872022-11-29 05:32:58 +07001263 if (msg == NULL || completion_code == NULL ||
1264 synchrony_config == NULL || synchrony_config_support == NULL ||
1265 number_event_class_returned == NULL || event_class == NULL) {
1266 return PLDM_ERROR_INVALID_DATA;
1267 }
1268
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301269 rc = pldm_msgbuf_init(buf, PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES,
1270 msg->payload, payload_length);
1271 if (rc) {
1272 return rc;
1273 }
1274
1275 rc = pldm_msgbuf_extract(buf, completion_code);
1276 if (rc) {
1277 return rc;
1278 }
1279
Dung Cao1bf8c872022-11-29 05:32:58 +07001280 if (PLDM_SUCCESS != *completion_code) {
1281 return PLDM_SUCCESS;
1282 }
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301283
1284 rc = pldm_msgbuf_extract(buf, synchrony_config);
1285 if (rc) {
1286 return rc;
Dung Cao1bf8c872022-11-29 05:32:58 +07001287 }
1288
Dung Cao1bf8c872022-11-29 05:32:58 +07001289 if (*synchrony_config > PLDM_MESSAGE_TYPE_ASYNCHRONOUS_WITH_HEARTBEAT) {
1290 return PLDM_ERROR_INVALID_DATA;
1291 }
1292
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301293 pldm_msgbuf_extract(buf, &synchrony_config_support->byte);
Dung Cao1bf8c872022-11-29 05:32:58 +07001294
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301295 rc = pldm_msgbuf_extract(buf, number_event_class_returned);
1296 if (rc) {
1297 return rc;
Dung Cao1bf8c872022-11-29 05:32:58 +07001298 }
1299
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301300 if (*number_event_class_returned == 0) {
1301 return pldm_msgbuf_destroy(buf);
1302 }
1303
1304 if (event_class_count < *number_event_class_returned) {
1305 return PLDM_ERROR_INVALID_LENGTH;
1306 }
1307
1308 for (i = 0; i < *number_event_class_returned; i++) {
1309 pldm_msgbuf_extract(buf, &event_class[i]);
1310 }
1311
1312 return pldm_msgbuf_destroy_consumed(buf);
Dung Cao1bf8c872022-11-29 05:32:58 +07001313}
1314
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301315LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301316int decode_sensor_event_data(const uint8_t *event_data,
1317 size_t event_data_length, uint16_t *sensor_id,
1318 uint8_t *sensor_event_class_type,
1319 size_t *event_class_data_offset)
1320{
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301321 struct pldm_msgbuf _buf;
1322 struct pldm_msgbuf *buf = &_buf;
1323 int rc;
1324
1325 rc = pldm_msgbuf_init(buf, PLDM_SENSOR_EVENT_DATA_MIN_LENGTH,
1326 event_data, event_data_length);
1327 if (rc) {
1328 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301329 }
1330
1331 size_t event_class_data_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301332 event_data_length - PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301333
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301334 pldm_msgbuf_extract(buf, sensor_id);
1335 rc = pldm_msgbuf_extract(buf, sensor_event_class_type);
1336 if (rc) {
1337 return rc;
1338 }
1339
1340 if (*sensor_event_class_type == PLDM_SENSOR_OP_STATE) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301341 if (event_class_data_length !=
1342 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
1343 return PLDM_ERROR_INVALID_LENGTH;
1344 }
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301345 } else if (*sensor_event_class_type == PLDM_STATE_SENSOR_STATE) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301346 if (event_class_data_length !=
1347 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
1348 return PLDM_ERROR_INVALID_LENGTH;
1349 }
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301350 } else if (*sensor_event_class_type == PLDM_NUMERIC_SENSOR_STATE) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301351 if (event_class_data_length <
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301352 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
Andrew Jeffery9c766792022-08-10 23:12:49 +09301353 event_class_data_length >
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301354 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301355 return PLDM_ERROR_INVALID_LENGTH;
1356 }
1357 } else {
1358 return PLDM_ERROR_INVALID_DATA;
1359 }
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301360
Andrew Jeffery9c766792022-08-10 23:12:49 +09301361 *event_class_data_offset =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301362 sizeof(*sensor_id) + sizeof(*sensor_event_class_type);
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301363
1364 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301365}
1366
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301367LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301368int decode_sensor_op_data(const uint8_t *sensor_data, size_t sensor_data_length,
1369 uint8_t *present_op_state, uint8_t *previous_op_state)
1370{
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301371 struct pldm_msgbuf _buf;
1372 struct pldm_msgbuf *buf = &_buf;
1373 int rc;
1374
1375 if (present_op_state == NULL || previous_op_state == NULL) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301376 return PLDM_ERROR_INVALID_DATA;
1377 }
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301378
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301379 rc = pldm_msgbuf_init(buf,
1380 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH,
1381 sensor_data, sensor_data_length);
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301382 if (rc) {
1383 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301384 }
1385
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301386 pldm_msgbuf_extract(buf, present_op_state);
1387 pldm_msgbuf_extract(buf, previous_op_state);
1388
1389 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301390}
1391
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301392LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301393int decode_state_sensor_data(const uint8_t *sensor_data,
1394 size_t sensor_data_length, uint8_t *sensor_offset,
1395 uint8_t *event_state,
1396 uint8_t *previous_event_state)
1397{
Andrew Jeffery422790b2023-04-13 15:03:47 +09301398 struct pldm_msgbuf _buf;
1399 struct pldm_msgbuf *buf = &_buf;
1400 int rc;
1401
1402 if (sensor_offset == NULL || event_state == NULL ||
1403 previous_event_state == NULL) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301404 return PLDM_ERROR_INVALID_DATA;
1405 }
Andrew Jeffery422790b2023-04-13 15:03:47 +09301406
1407 rc = pldm_msgbuf_init(buf,
1408 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH,
1409 sensor_data, sensor_data_length);
1410 if (rc) {
1411 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301412 }
1413
Andrew Jeffery422790b2023-04-13 15:03:47 +09301414 pldm_msgbuf_extract(buf, sensor_offset);
1415 pldm_msgbuf_extract(buf, event_state);
1416 pldm_msgbuf_extract(buf, previous_event_state);
1417
1418 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301419}
1420
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301421LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301422int decode_numeric_sensor_data(const uint8_t *sensor_data,
1423 size_t sensor_data_length, uint8_t *event_state,
1424 uint8_t *previous_event_state,
1425 uint8_t *sensor_data_size,
1426 uint32_t *present_reading)
1427{
Andrew Jeffery155317e2023-04-13 18:36:51 +09301428 struct pldm_msgbuf _buf;
1429 struct pldm_msgbuf *buf = &_buf;
1430 int rc;
1431
1432 if (sensor_data_size == NULL || event_state == NULL ||
1433 previous_event_state == NULL || present_reading == NULL) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301434 return PLDM_ERROR_INVALID_DATA;
1435 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301436
1437 if (sensor_data_length >
1438 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301439 return PLDM_ERROR_INVALID_LENGTH;
1440 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301441
Andrew Jeffery155317e2023-04-13 18:36:51 +09301442 rc = pldm_msgbuf_init(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301443 buf, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH,
1444 sensor_data, sensor_data_length);
Andrew Jeffery155317e2023-04-13 18:36:51 +09301445 if (rc) {
1446 return rc;
1447 }
1448
1449 pldm_msgbuf_extract(buf, event_state);
1450 pldm_msgbuf_extract(buf, previous_event_state);
1451 rc = pldm_msgbuf_extract(buf, sensor_data_size);
1452 if (rc) {
1453 return rc;
1454 }
1455
1456 /*
1457 * The implementation below is bonkers, but it's because the function
1458 * prototype is bonkers. The `present_reading` argument should have been
1459 * a tagged union.
1460 */
Andrew Jeffery9c766792022-08-10 23:12:49 +09301461 switch (*sensor_data_size) {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301462 case PLDM_SENSOR_DATA_SIZE_UINT8: {
1463 uint8_t val;
1464 if (!pldm_msgbuf_extract(buf, &val)) {
1465 *present_reading = (uint32_t)val;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301466 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301467 break;
Andrew Jeffery155317e2023-04-13 18:36:51 +09301468 }
1469 case PLDM_SENSOR_DATA_SIZE_SINT8: {
1470 int8_t val;
1471 if (!pldm_msgbuf_extract(buf, &val)) {
1472 *present_reading = (uint32_t)(int32_t)val;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301473 }
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301474 break;
Andrew Jeffery155317e2023-04-13 18:36:51 +09301475 }
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301476 case PLDM_SENSOR_DATA_SIZE_UINT16: {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301477 uint16_t val;
1478 if (!pldm_msgbuf_extract(buf, &val)) {
1479 *present_reading = (uint32_t)val;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301480 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301481 break;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301482 }
1483 case PLDM_SENSOR_DATA_SIZE_SINT16: {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301484 int16_t val;
1485 if (!pldm_msgbuf_extract(buf, &val)) {
1486 *present_reading = (uint32_t)(int32_t)val;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301487 }
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301488 break;
1489 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301490 case PLDM_SENSOR_DATA_SIZE_UINT32: {
1491 uint32_t val;
1492 if (!pldm_msgbuf_extract(buf, &val)) {
1493 *present_reading = (uint32_t)val;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301494 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301495 break;
1496 }
1497 case PLDM_SENSOR_DATA_SIZE_SINT32: {
1498 int32_t val;
1499 if (!pldm_msgbuf_extract(buf, &val)) {
1500 *present_reading = (uint32_t)val;
1501 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301502 break;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301503 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301504 default:
1505 return PLDM_ERROR_INVALID_DATA;
1506 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301507
1508 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301509}
1510
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301511#define PLDM_NUMERIC_SENSOR_VALUE_PDR_MIN_SIZE 69
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301512LIBPLDM_ABI_STABLE
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301513int decode_numeric_sensor_pdr_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301514 const void *pdr_data, size_t pdr_data_length,
1515 struct pldm_numeric_sensor_value_pdr *pdr_value)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301516{
1517 struct pldm_msgbuf _buf;
1518 struct pldm_msgbuf *buf = &_buf;
1519 int rc;
1520
1521 rc = pldm_msgbuf_init(buf, PLDM_NUMERIC_SENSOR_VALUE_PDR_MIN_SIZE,
1522 pdr_data, pdr_data_length);
1523 if (rc) {
1524 return rc;
1525 }
1526
1527 rc = pldm_msgbuf_extract_value_pdr_hdr(buf, &pdr_value->hdr);
1528 if (rc) {
1529 return rc;
1530 }
1531
1532 rc = pldm_platform_pdr_hdr_validate(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301533 &pdr_value->hdr, PLDM_NUMERIC_SENSOR_VALUE_PDR_MIN_SIZE,
1534 pdr_data_length);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301535 if (rc) {
1536 return rc;
1537 }
1538
1539 pldm_msgbuf_extract(buf, &pdr_value->terminus_handle);
1540 pldm_msgbuf_extract(buf, &pdr_value->sensor_id);
1541 pldm_msgbuf_extract(buf, &pdr_value->entity_type);
1542 pldm_msgbuf_extract(buf, &pdr_value->entity_instance_num);
1543 pldm_msgbuf_extract(buf, &pdr_value->container_id);
1544 pldm_msgbuf_extract(buf, &pdr_value->sensor_init);
1545 pldm_msgbuf_extract(buf, &pdr_value->sensor_auxiliary_names_pdr);
1546 pldm_msgbuf_extract(buf, &pdr_value->base_unit);
1547 pldm_msgbuf_extract(buf, &pdr_value->unit_modifier);
1548 pldm_msgbuf_extract(buf, &pdr_value->rate_unit);
1549 pldm_msgbuf_extract(buf, &pdr_value->base_oem_unit_handle);
1550 pldm_msgbuf_extract(buf, &pdr_value->aux_unit);
1551 pldm_msgbuf_extract(buf, &pdr_value->aux_unit_modifier);
1552 pldm_msgbuf_extract(buf, &pdr_value->aux_rate_unit);
1553 pldm_msgbuf_extract(buf, &pdr_value->rel);
1554 pldm_msgbuf_extract(buf, &pdr_value->aux_oem_unit_handle);
1555 pldm_msgbuf_extract(buf, &pdr_value->is_linear);
1556
1557 rc = pldm_msgbuf_extract(buf, &pdr_value->sensor_data_size);
1558 if (rc) {
1559 return rc;
1560 }
1561 if (pdr_value->sensor_data_size > PLDM_SENSOR_DATA_SIZE_MAX) {
1562 return PLDM_ERROR_INVALID_DATA;
1563 }
1564
1565 pldm_msgbuf_extract(buf, &pdr_value->resolution);
1566 pldm_msgbuf_extract(buf, &pdr_value->offset);
1567 pldm_msgbuf_extract(buf, &pdr_value->accuracy);
1568 pldm_msgbuf_extract(buf, &pdr_value->plus_tolerance);
1569 pldm_msgbuf_extract(buf, &pdr_value->minus_tolerance);
1570 pldm_msgbuf_extract_sensor_data(buf, pdr_value->sensor_data_size,
1571 &pdr_value->hysteresis);
1572 pldm_msgbuf_extract(buf, &pdr_value->supported_thresholds.byte);
1573 pldm_msgbuf_extract(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301574 buf, &pdr_value->threshold_and_hysteresis_volatility.byte);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301575 pldm_msgbuf_extract(buf, &pdr_value->state_transition_interval);
1576 pldm_msgbuf_extract(buf, &pdr_value->update_interval);
1577 pldm_msgbuf_extract_sensor_data(buf, pdr_value->sensor_data_size,
1578 &pdr_value->max_readable);
1579 pldm_msgbuf_extract_sensor_data(buf, pdr_value->sensor_data_size,
1580 &pdr_value->min_readable);
1581
1582 rc = pldm_msgbuf_extract(buf, &pdr_value->range_field_format);
1583 if (rc) {
1584 return rc;
1585 }
1586 if (pdr_value->range_field_format > PLDM_RANGE_FIELD_FORMAT_MAX) {
1587 return PLDM_ERROR_INVALID_DATA;
1588 }
1589
1590 pldm_msgbuf_extract(buf, &pdr_value->range_field_support.byte);
1591 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301592 buf, pdr_value->range_field_format, &pdr_value->nominal_value);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301593 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301594 buf, pdr_value->range_field_format, &pdr_value->normal_max);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301595 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301596 buf, pdr_value->range_field_format, &pdr_value->normal_min);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301597 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301598 buf, pdr_value->range_field_format, &pdr_value->warning_high);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301599 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301600 buf, pdr_value->range_field_format, &pdr_value->warning_low);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301601 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301602 buf, pdr_value->range_field_format, &pdr_value->critical_high);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301603 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301604 buf, pdr_value->range_field_format, &pdr_value->critical_low);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301605 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301606 buf, pdr_value->range_field_format, &pdr_value->fatal_high);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301607 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301608 buf, pdr_value->range_field_format, &pdr_value->fatal_low);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301609
1610 return pldm_msgbuf_destroy(buf);
1611}
1612
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301613LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301614int encode_get_numeric_effecter_value_req(uint8_t instance_id,
1615 uint16_t effecter_id,
1616 struct pldm_msg *msg)
1617{
1618 if (msg == NULL) {
1619 return PLDM_ERROR_INVALID_DATA;
1620 }
1621
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301622 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301623 header.msg_type = PLDM_REQUEST;
1624 header.instance = instance_id;
1625 header.pldm_type = PLDM_PLATFORM;
1626 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1627
1628 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1629 if (rc != PLDM_SUCCESS) {
1630 return rc;
1631 }
1632
1633 struct pldm_get_numeric_effecter_value_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301634 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301635 request->effecter_id = htole16(effecter_id);
1636
1637 return PLDM_SUCCESS;
1638}
1639
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301640LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301641int encode_get_numeric_effecter_value_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301642 uint8_t instance_id, uint8_t completion_code,
1643 uint8_t effecter_data_size, uint8_t effecter_oper_state,
1644 const uint8_t *pending_value, const uint8_t *present_value,
1645 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301646{
1647 if (msg == NULL || pending_value == NULL || present_value == NULL) {
1648 return PLDM_ERROR_INVALID_DATA;
1649 }
1650
1651 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1652 return PLDM_ERROR_INVALID_DATA;
1653 }
1654
1655 if (effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1656 return PLDM_ERROR_INVALID_DATA;
1657 }
1658
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301659 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301660 header.msg_type = PLDM_RESPONSE;
1661 header.instance = instance_id;
1662 header.pldm_type = PLDM_PLATFORM;
1663 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1664
1665 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1666 if (rc != PLDM_SUCCESS) {
1667 return rc;
1668 }
1669
1670 struct pldm_get_numeric_effecter_value_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301671 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301672
1673 response->completion_code = completion_code;
1674 response->effecter_data_size = effecter_data_size;
1675 response->effecter_oper_state = effecter_oper_state;
1676
1677 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1678 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1679 if (payload_length !=
1680 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1681 return PLDM_ERROR_INVALID_LENGTH;
1682 }
1683 response->pending_and_present_values[0] = *pending_value;
1684 response->pending_and_present_values[1] = *present_value;
1685
1686 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1687 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1688 if (payload_length !=
1689 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1690 return PLDM_ERROR_INVALID_LENGTH;
1691 }
1692 uint16_t val_pending = *(uint16_t *)pending_value;
1693 val_pending = htole16(val_pending);
1694 memcpy(response->pending_and_present_values, &val_pending,
1695 sizeof(uint16_t));
1696 uint16_t val_present = *(uint16_t *)present_value;
1697 val_present = htole16(val_present);
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301698 memcpy((response->pending_and_present_values +
1699 sizeof(uint16_t)),
1700 &val_present, sizeof(uint16_t));
Andrew Jeffery9c766792022-08-10 23:12:49 +09301701
1702 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1703 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1704 if (payload_length !=
1705 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1706 return PLDM_ERROR_INVALID_LENGTH;
1707 }
1708 uint32_t val_pending = *(uint32_t *)pending_value;
1709 val_pending = htole32(val_pending);
1710 memcpy(response->pending_and_present_values, &val_pending,
1711 sizeof(uint32_t));
1712 uint32_t val_present = *(uint32_t *)present_value;
1713 val_present = htole32(val_present);
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301714 memcpy((response->pending_and_present_values +
1715 sizeof(uint32_t)),
1716 &val_present, sizeof(uint32_t));
Andrew Jeffery9c766792022-08-10 23:12:49 +09301717 }
1718 return PLDM_SUCCESS;
1719}
1720
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301721LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301722int decode_get_numeric_effecter_value_req(const struct pldm_msg *msg,
1723 size_t payload_length,
1724 uint16_t *effecter_id)
1725{
Andrew Jefferydd265822023-04-13 22:42:44 +09301726 struct pldm_msgbuf _buf;
1727 struct pldm_msgbuf *buf = &_buf;
1728 int rc;
1729
Andrew Jeffery9c766792022-08-10 23:12:49 +09301730 if (msg == NULL || effecter_id == NULL) {
1731 return PLDM_ERROR_INVALID_DATA;
1732 }
1733
Andrew Jefferydd265822023-04-13 22:42:44 +09301734 rc = pldm_msgbuf_init(buf, PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES,
1735 msg->payload, payload_length);
1736 if (rc) {
1737 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301738 }
1739
Andrew Jefferydd265822023-04-13 22:42:44 +09301740 pldm_msgbuf_extract(buf, effecter_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301741
Andrew Jefferydd265822023-04-13 22:42:44 +09301742 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301743}
1744
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301745LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301746int decode_get_numeric_effecter_value_resp(const struct pldm_msg *msg,
1747 size_t payload_length,
1748 uint8_t *completion_code,
1749 uint8_t *effecter_data_size,
1750 uint8_t *effecter_oper_state,
1751 uint8_t *pending_value,
1752 uint8_t *present_value)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301753{
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301754 struct pldm_msgbuf _buf;
1755 struct pldm_msgbuf *buf = &_buf;
1756 int rc;
1757
Andrew Jeffery9c766792022-08-10 23:12:49 +09301758 if (msg == NULL || effecter_data_size == NULL ||
1759 effecter_oper_state == NULL || pending_value == NULL ||
1760 present_value == NULL) {
1761 return PLDM_ERROR_INVALID_DATA;
1762 }
1763
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301764 rc = pldm_msgbuf_init(buf,
1765 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES,
1766 msg->payload, payload_length);
1767 if (rc) {
1768 return rc;
1769 }
1770
1771 rc = pldm_msgbuf_extract(buf, completion_code);
1772 if (rc) {
1773 return rc;
1774 }
1775
Andrew Jeffery9c766792022-08-10 23:12:49 +09301776 if (PLDM_SUCCESS != *completion_code) {
1777 return PLDM_SUCCESS;
1778 }
1779
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301780 rc = pldm_msgbuf_extract(buf, effecter_data_size);
1781 if (rc) {
1782 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301783 }
1784
Andrew Jeffery9c766792022-08-10 23:12:49 +09301785 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1786 return PLDM_ERROR_INVALID_DATA;
1787 }
1788
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301789 rc = pldm_msgbuf_extract(buf, effecter_oper_state);
1790 if (rc) {
1791 return rc;
1792 }
1793
Andrew Jeffery9c766792022-08-10 23:12:49 +09301794 if (*effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1795 return PLDM_ERROR_INVALID_DATA;
1796 }
1797
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301798 pldm_msgbuf_extract_effecter_value(buf, *effecter_data_size,
1799 pending_value);
1800 pldm_msgbuf_extract_effecter_value(buf, *effecter_data_size,
1801 present_value);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301802
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301803 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301804}
1805
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301806LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301807int encode_pldm_pdr_repository_chg_event_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301808 uint8_t event_data_format, uint8_t number_of_change_records,
1809 const uint8_t *event_data_operations,
1810 const uint8_t *numbers_of_change_entries,
1811 const uint32_t *const *change_entries,
1812 struct pldm_pdr_repository_chg_event_data *event_data,
1813 size_t *actual_change_records_size, size_t max_change_records_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301814{
1815 if (event_data_operations == NULL ||
1816 numbers_of_change_entries == NULL || change_entries == NULL) {
1817 return PLDM_ERROR_INVALID_DATA;
1818 }
1819
1820 size_t expected_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301821 sizeof(event_data_format) + sizeof(number_of_change_records);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301822
1823 expected_size +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301824 sizeof(*event_data_operations) * number_of_change_records;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301825 expected_size +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301826 sizeof(*numbers_of_change_entries) * number_of_change_records;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301827
1828 for (uint8_t i = 0; i < number_of_change_records; ++i) {
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301829 expected_size += sizeof(*change_entries[0]) *
1830 numbers_of_change_entries[i];
Andrew Jeffery9c766792022-08-10 23:12:49 +09301831 }
1832
1833 *actual_change_records_size = expected_size;
1834
1835 if (event_data == NULL) {
1836 return PLDM_SUCCESS;
1837 }
1838
1839 if (max_change_records_size < expected_size) {
1840 return PLDM_ERROR_INVALID_LENGTH;
1841 }
1842
1843 event_data->event_data_format = event_data_format;
1844 event_data->number_of_change_records = number_of_change_records;
1845
1846 struct pldm_pdr_repository_change_record_data *record_data =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301847 (struct pldm_pdr_repository_change_record_data *)
1848 event_data->change_records;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301849
1850 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1851 record_data->event_data_operation = event_data_operations[i];
1852 record_data->number_of_change_entries =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301853 numbers_of_change_entries[i];
Andrew Jeffery9c766792022-08-10 23:12:49 +09301854
1855 for (uint8_t j = 0; j < record_data->number_of_change_entries;
1856 ++j) {
1857 record_data->change_entry[j] =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301858 htole32(change_entries[i][j]);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301859 }
1860
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301861 record_data =
1862 (struct pldm_pdr_repository_change_record_data
1863 *)(record_data->change_entry +
1864 record_data->number_of_change_entries);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301865 }
1866
1867 return PLDM_SUCCESS;
1868}
1869
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301870LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301871int decode_pldm_pdr_repository_chg_event_data(const uint8_t *event_data,
1872 size_t event_data_size,
1873 uint8_t *event_data_format,
1874 uint8_t *number_of_change_records,
1875 size_t *change_record_data_offset)
1876{
Andrew Jeffery2fe70122023-04-13 23:21:31 +09301877 struct pldm_msgbuf _buf;
1878 struct pldm_msgbuf *buf = &_buf;
1879 int rc;
1880
1881 if (event_data_format == NULL || number_of_change_records == NULL ||
Andrew Jeffery9c766792022-08-10 23:12:49 +09301882 change_record_data_offset == NULL) {
1883 return PLDM_ERROR_INVALID_DATA;
1884 }
Andrew Jeffery2fe70122023-04-13 23:21:31 +09301885
1886 rc = pldm_msgbuf_init(buf, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH,
1887 event_data, event_data_size);
1888 if (rc) {
1889 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301890 }
1891
Andrew Jeffery2fe70122023-04-13 23:21:31 +09301892 pldm_msgbuf_extract(buf, event_data_format);
1893 pldm_msgbuf_extract(buf, number_of_change_records);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301894
Andrew Jeffery9c766792022-08-10 23:12:49 +09301895 *change_record_data_offset =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301896 sizeof(*event_data_format) + sizeof(*number_of_change_records);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301897
Andrew Jeffery2fe70122023-04-13 23:21:31 +09301898 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301899}
1900
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301901LIBPLDM_ABI_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07001902int decode_pldm_message_poll_event_data(const uint8_t *event_data,
1903 size_t event_data_length,
1904 uint8_t *format_version,
1905 uint16_t *event_id,
1906 uint32_t *data_transfer_handle)
1907{
1908 struct pldm_msgbuf _buf;
1909 struct pldm_msgbuf *buf = &_buf;
1910 int rc;
1911
1912 if (event_data == NULL || format_version == NULL || event_id == NULL ||
1913 data_transfer_handle == NULL) {
1914 return PLDM_ERROR_INVALID_DATA;
1915 }
1916
1917 rc = pldm_msgbuf_init(buf, PLDM_MSG_POLL_EVENT_LENGTH, event_data,
1918 event_data_length);
1919 if (rc) {
1920 return rc;
1921 }
1922
1923 pldm_msgbuf_extract(buf, format_version);
1924 rc = pldm_msgbuf_extract(buf, event_id);
1925 if (rc) {
1926 return rc;
1927 }
1928
1929 if (*event_id == 0x0000 || *event_id == 0xffff) {
1930 return PLDM_ERROR_INVALID_DATA;
1931 }
1932
1933 pldm_msgbuf_extract(buf, data_transfer_handle);
1934
1935 return pldm_msgbuf_destroy_consumed(buf);
1936}
1937
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301938LIBPLDM_ABI_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07001939int encode_pldm_message_poll_event_data(uint8_t format_version,
1940 uint16_t event_id,
1941 uint32_t data_transfer_handle,
1942 uint8_t *event_data,
1943 size_t event_data_length)
1944{
1945 struct pldm_msgbuf _buf;
1946 struct pldm_msgbuf *buf = &_buf;
1947 int rc;
1948
1949 if (event_data == NULL) {
1950 return PLDM_ERROR_INVALID_DATA;
1951 }
1952
1953 if (event_id == 0x0000 || event_id == 0xffff) {
1954 return PLDM_ERROR_INVALID_DATA;
1955 }
1956
1957 rc = pldm_msgbuf_init(buf, PLDM_MSG_POLL_EVENT_LENGTH, event_data,
1958 event_data_length);
1959 if (rc) {
1960 return rc;
1961 }
1962 pldm_msgbuf_insert(buf, format_version);
1963 pldm_msgbuf_insert(buf, event_id);
1964 pldm_msgbuf_insert(buf, data_transfer_handle);
1965
1966 return pldm_msgbuf_destroy(buf);
1967}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301968
1969LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301970int decode_pldm_pdr_repository_change_record_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301971 const uint8_t *change_record_data, size_t change_record_data_size,
1972 uint8_t *event_data_operation, uint8_t *number_of_change_entries,
1973 size_t *change_entry_data_offset)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301974{
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09301975 struct pldm_msgbuf _buf;
1976 struct pldm_msgbuf *buf = &_buf;
1977 int rc;
1978
1979 if (event_data_operation == NULL || number_of_change_entries == NULL ||
Andrew Jeffery9c766792022-08-10 23:12:49 +09301980 change_entry_data_offset == NULL) {
1981 return PLDM_ERROR_INVALID_DATA;
1982 }
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09301983
1984 rc = pldm_msgbuf_init(buf, PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH,
1985 change_record_data, change_record_data_size);
1986 if (rc) {
1987 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301988 }
1989
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09301990 pldm_msgbuf_extract(buf, event_data_operation);
1991 pldm_msgbuf_extract(buf, number_of_change_entries);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301992
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301993 *change_entry_data_offset = sizeof(*event_data_operation) +
1994 sizeof(*number_of_change_entries);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301995
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09301996 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301997}
1998
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301999LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302000int encode_get_sensor_reading_req(uint8_t instance_id, uint16_t sensor_id,
2001 uint8_t rearm_event_state,
2002 struct pldm_msg *msg)
2003{
2004 if (msg == NULL) {
2005 return PLDM_ERROR_INVALID_DATA;
2006 }
2007
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302008 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302009 header.msg_type = PLDM_REQUEST;
2010 header.instance = instance_id;
2011 header.pldm_type = PLDM_PLATFORM;
2012 header.command = PLDM_GET_SENSOR_READING;
2013
2014 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2015 if (rc != PLDM_SUCCESS) {
2016 return rc;
2017 }
2018
2019 struct pldm_get_sensor_reading_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302020 (struct pldm_get_sensor_reading_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302021
2022 request->sensor_id = htole16(sensor_id);
2023 request->rearm_event_state = rearm_event_state;
2024
2025 return PLDM_SUCCESS;
2026}
2027
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302028LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302029int decode_get_sensor_reading_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302030 const struct pldm_msg *msg, size_t payload_length,
2031 uint8_t *completion_code, uint8_t *sensor_data_size,
2032 uint8_t *sensor_operational_state, uint8_t *sensor_event_message_enable,
2033 uint8_t *present_state, uint8_t *previous_state, uint8_t *event_state,
2034 uint8_t *present_reading)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302035{
Andrew Jeffery840b1402023-04-13 23:54:44 +09302036 struct pldm_msgbuf _buf;
2037 struct pldm_msgbuf *buf = &_buf;
2038 int rc;
2039
Andrew Jeffery9c766792022-08-10 23:12:49 +09302040 if (msg == NULL || completion_code == NULL ||
2041 sensor_data_size == NULL || sensor_operational_state == NULL ||
2042 sensor_event_message_enable == NULL || present_state == NULL ||
2043 previous_state == NULL || event_state == NULL ||
2044 present_reading == NULL) {
2045 return PLDM_ERROR_INVALID_DATA;
2046 }
2047
Andrew Jeffery840b1402023-04-13 23:54:44 +09302048 rc = pldm_msgbuf_init(buf, PLDM_GET_SENSOR_READING_MIN_RESP_BYTES,
2049 msg->payload, payload_length);
2050 if (rc) {
2051 return rc;
2052 }
2053
2054 rc = pldm_msgbuf_extract(buf, completion_code);
2055 if (rc) {
2056 return rc;
2057 }
2058
Andrew Jeffery9c766792022-08-10 23:12:49 +09302059 if (PLDM_SUCCESS != *completion_code) {
2060 return PLDM_SUCCESS;
2061 }
2062
Andrew Jeffery840b1402023-04-13 23:54:44 +09302063 rc = pldm_msgbuf_extract(buf, sensor_data_size);
2064 if (rc) {
2065 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302066 }
2067
Andrew Jeffery840b1402023-04-13 23:54:44 +09302068 if (*sensor_data_size > PLDM_SENSOR_DATA_SIZE_SINT32) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09302069 return PLDM_ERROR_INVALID_DATA;
2070 }
2071
Andrew Jeffery840b1402023-04-13 23:54:44 +09302072 pldm_msgbuf_extract(buf, sensor_operational_state);
2073 pldm_msgbuf_extract(buf, sensor_event_message_enable);
2074 pldm_msgbuf_extract(buf, present_state);
2075 pldm_msgbuf_extract(buf, previous_state);
2076 pldm_msgbuf_extract(buf, event_state);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302077
Andrew Jeffery840b1402023-04-13 23:54:44 +09302078 pldm_msgbuf_extract_sensor_value(buf, *sensor_data_size,
2079 present_reading);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302080
Andrew Jeffery840b1402023-04-13 23:54:44 +09302081 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302082}
2083
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302084LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302085int encode_get_sensor_reading_resp(uint8_t instance_id, uint8_t completion_code,
2086 uint8_t sensor_data_size,
2087 uint8_t sensor_operational_state,
2088 uint8_t sensor_event_message_enable,
2089 uint8_t present_state,
2090 uint8_t previous_state, uint8_t event_state,
2091 const uint8_t *present_reading,
2092 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302093{
2094 if (msg == NULL || present_reading == NULL) {
2095 return PLDM_ERROR_INVALID_DATA;
2096 }
2097
2098 if (sensor_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
2099 return PLDM_ERROR_INVALID_DATA;
2100 }
2101
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302102 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302103 header.msg_type = PLDM_RESPONSE;
2104 header.instance = instance_id;
2105 header.pldm_type = PLDM_PLATFORM;
2106 header.command = PLDM_GET_SENSOR_READING;
2107
2108 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2109 if (rc != PLDM_SUCCESS) {
2110 return rc;
2111 }
2112
2113 struct pldm_get_sensor_reading_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302114 (struct pldm_get_sensor_reading_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302115
2116 response->completion_code = completion_code;
2117 response->sensor_data_size = sensor_data_size;
2118 response->sensor_operational_state = sensor_operational_state;
2119 response->sensor_event_message_enable = sensor_event_message_enable;
2120 response->present_state = present_state;
2121 response->previous_state = previous_state;
2122 response->event_state = event_state;
2123
2124 if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
2125 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
2126 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
2127 return PLDM_ERROR_INVALID_LENGTH;
2128 }
2129 response->present_reading[0] = *present_reading;
2130
2131 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
2132 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
2133 if (payload_length !=
2134 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
2135 return PLDM_ERROR_INVALID_LENGTH;
2136 }
2137 uint16_t val = *(uint16_t *)present_reading;
2138 val = htole16(val);
2139 memcpy(response->present_reading, &val, 2);
2140
2141 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
2142 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
2143 if (payload_length !=
2144 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
2145 return PLDM_ERROR_INVALID_LENGTH;
2146 }
2147 uint32_t val = *(uint32_t *)present_reading;
2148 val = htole32(val);
2149 memcpy(response->present_reading, &val, 4);
2150 }
2151
2152 return PLDM_SUCCESS;
2153}
2154
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302155LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302156int decode_get_sensor_reading_req(const struct pldm_msg *msg,
2157 size_t payload_length, uint16_t *sensor_id,
2158 uint8_t *rearm_event_state)
2159{
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302160 struct pldm_msgbuf _buf;
2161 struct pldm_msgbuf *buf = &_buf;
2162 int rc;
2163
Andrew Jeffery9c766792022-08-10 23:12:49 +09302164 if (msg == NULL || sensor_id == NULL || rearm_event_state == NULL) {
2165 return PLDM_ERROR_INVALID_DATA;
2166 }
2167
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302168 rc = pldm_msgbuf_init(buf, PLDM_GET_SENSOR_READING_REQ_BYTES,
2169 msg->payload, payload_length);
2170 if (rc) {
2171 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302172 }
2173
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302174 pldm_msgbuf_extract(buf, sensor_id);
2175 pldm_msgbuf_extract(buf, rearm_event_state);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302176
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302177 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302178}
2179
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302180LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302181int encode_set_event_receiver_req(uint8_t instance_id,
2182 uint8_t event_message_global_enable,
2183 uint8_t transport_protocol_type,
2184 uint8_t event_receiver_address_info,
2185 uint16_t heartbeat_timer,
2186 struct pldm_msg *msg)
2187{
2188 if (msg == NULL) {
2189 return PLDM_ERROR_INVALID_DATA;
2190 }
2191
2192 if (transport_protocol_type != PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP) {
2193 return PLDM_ERROR_INVALID_DATA;
2194 }
2195
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302196 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302197 header.msg_type = PLDM_REQUEST;
2198 header.instance = instance_id;
2199 header.pldm_type = PLDM_PLATFORM;
2200 header.command = PLDM_SET_EVENT_RECEIVER;
2201
2202 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2203 if (rc != PLDM_SUCCESS) {
2204 return rc;
2205 }
2206
2207 struct pldm_set_event_receiver_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302208 (struct pldm_set_event_receiver_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302209 request->event_message_global_enable = event_message_global_enable;
2210
2211 request->transport_protocol_type = transport_protocol_type;
2212 request->event_receiver_address_info = event_receiver_address_info;
2213
2214 if (event_message_global_enable ==
2215 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) {
2216 if (heartbeat_timer == 0) {
2217 return PLDM_ERROR_INVALID_DATA;
2218 }
2219 request->heartbeat_timer = htole16(heartbeat_timer);
2220 }
2221
2222 return PLDM_SUCCESS;
2223}
2224
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302225LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302226int decode_set_event_receiver_resp(const struct pldm_msg *msg,
2227 size_t payload_length,
2228 uint8_t *completion_code)
2229{
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302230 struct pldm_msgbuf _buf;
2231 struct pldm_msgbuf *buf = &_buf;
2232 int rc;
2233
Andrew Jeffery9c766792022-08-10 23:12:49 +09302234 if (msg == NULL || completion_code == NULL) {
2235 return PLDM_ERROR_INVALID_DATA;
2236 }
2237
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302238 rc = pldm_msgbuf_init(buf, PLDM_SET_EVENT_RECEIVER_RESP_BYTES,
2239 msg->payload, payload_length);
2240 if (rc) {
2241 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302242 }
2243
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302244 pldm_msgbuf_extract(buf, completion_code);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302245
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302246 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302247}
2248
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302249LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302250int decode_set_event_receiver_req(const struct pldm_msg *msg,
2251 size_t payload_length,
2252 uint8_t *event_message_global_enable,
2253 uint8_t *transport_protocol_type,
2254 uint8_t *event_receiver_address_info,
2255 uint16_t *heartbeat_timer)
2256
2257{
Andrew Jeffery9667f582023-04-14 00:39:21 +09302258 struct pldm_msgbuf _buf;
2259 struct pldm_msgbuf *buf = &_buf;
2260 int rc;
2261
Andrew Jeffery9c766792022-08-10 23:12:49 +09302262 if (msg == NULL || event_message_global_enable == NULL ||
2263 transport_protocol_type == NULL ||
2264 event_receiver_address_info == NULL || heartbeat_timer == NULL) {
2265 return PLDM_ERROR_INVALID_DATA;
2266 }
2267
Andrew Jeffery9667f582023-04-14 00:39:21 +09302268 rc = pldm_msgbuf_init(buf, PLDM_SET_EVENT_RECEIVER_REQ_BYTES,
2269 msg->payload, payload_length);
2270 if (rc) {
2271 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302272 }
2273
Andrew Jeffery9667f582023-04-14 00:39:21 +09302274 pldm_msgbuf_extract(buf, event_message_global_enable);
2275 pldm_msgbuf_extract(buf, transport_protocol_type);
2276 pldm_msgbuf_extract(buf, event_receiver_address_info);
2277 pldm_msgbuf_extract(buf, heartbeat_timer);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302278
Andrew Jeffery9667f582023-04-14 00:39:21 +09302279 rc = pldm_msgbuf_destroy(buf);
2280 if (rc) {
2281 return rc;
2282 }
Andrew Jeffery6ef2aa92023-04-14 00:21:27 +09302283
Andrew Jeffery9c766792022-08-10 23:12:49 +09302284 if ((*event_message_global_enable ==
2285 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) &&
2286 (*heartbeat_timer == 0)) {
2287 return PLDM_ERROR_INVALID_DATA;
2288 }
2289
Andrew Jeffery9c766792022-08-10 23:12:49 +09302290 return PLDM_SUCCESS;
2291}
2292
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302293LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302294int encode_set_event_receiver_resp(uint8_t instance_id, uint8_t completion_code,
2295 struct pldm_msg *msg)
2296
2297{
2298 if (msg == NULL) {
2299 return PLDM_ERROR_INVALID_DATA;
2300 }
2301
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302302 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302303 header.instance = instance_id;
2304 header.msg_type = PLDM_RESPONSE;
2305 header.pldm_type = PLDM_PLATFORM;
2306 header.command = PLDM_SET_EVENT_RECEIVER;
2307
2308 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2309 if (rc != PLDM_SUCCESS) {
2310 return rc;
2311 }
2312
2313 msg->payload[0] = completion_code;
2314
2315 return PLDM_SUCCESS;
2316}
Thu Nguyen159a98b2022-11-02 10:00:10 +07002317
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302318LIBPLDM_ABI_STABLE
Thu Nguyen159a98b2022-11-02 10:00:10 +07002319int encode_poll_for_platform_event_message_req(uint8_t instance_id,
2320 uint8_t format_version,
2321 uint8_t transfer_operation_flag,
2322 uint32_t data_transfer_handle,
2323 uint16_t event_id_to_acknowledge,
2324 struct pldm_msg *msg,
2325 size_t payload_length)
2326{
2327 struct pldm_msgbuf _buf;
2328 struct pldm_msgbuf *buf = &_buf;
2329 int rc;
2330
2331 if (msg == NULL) {
2332 return PLDM_ERROR_INVALID_DATA;
2333 }
2334
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302335 struct pldm_header_info header = { 0 };
Thu Nguyen159a98b2022-11-02 10:00:10 +07002336 header.msg_type = PLDM_REQUEST;
2337 header.instance = instance_id;
2338 header.pldm_type = PLDM_PLATFORM;
2339 header.command = PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE;
2340
2341 rc = pack_pldm_header(&header, &(msg->hdr));
2342 if (rc != PLDM_SUCCESS) {
2343 return rc;
2344 }
2345
2346 rc = pldm_msgbuf_init(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302347 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
2348 msg->payload, payload_length);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002349 if (rc) {
2350 return rc;
2351 }
2352
2353 pldm_msgbuf_insert(buf, format_version);
2354 pldm_msgbuf_insert(buf, transfer_operation_flag);
2355 pldm_msgbuf_insert(buf, data_transfer_handle);
2356 pldm_msgbuf_insert(buf, event_id_to_acknowledge);
2357
2358 return pldm_msgbuf_destroy(buf);
2359}
2360
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302361LIBPLDM_ABI_STABLE
Thu Nguyen159a98b2022-11-02 10:00:10 +07002362int decode_poll_for_platform_event_message_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302363 const struct pldm_msg *msg, size_t payload_length,
2364 uint8_t *completion_code, uint8_t *tid, uint16_t *event_id,
2365 uint32_t *next_data_transfer_handle, uint8_t *transfer_flag,
2366 uint8_t *event_class, uint32_t *event_data_size, void **event_data,
2367 uint32_t *event_data_integrity_checksum)
Thu Nguyen159a98b2022-11-02 10:00:10 +07002368{
2369 struct pldm_msgbuf _buf;
2370 struct pldm_msgbuf *buf = &_buf;
2371 int rc;
2372
2373 if (msg == NULL || completion_code == NULL || tid == NULL ||
2374 event_id == NULL || next_data_transfer_handle == NULL ||
2375 transfer_flag == NULL || event_class == NULL ||
2376 event_data_size == NULL || event_data == NULL ||
2377 event_data_integrity_checksum == NULL) {
2378 return PLDM_ERROR_INVALID_DATA;
2379 }
2380
2381 rc = pldm_msgbuf_init(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302382 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
2383 msg->payload, payload_length);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002384 if (rc) {
2385 return rc;
2386 }
2387
2388 rc = pldm_msgbuf_extract(buf, completion_code);
2389 if (rc) {
2390 return rc;
2391 }
2392 if (PLDM_SUCCESS != *completion_code) {
2393 return *completion_code;
2394 }
2395
2396 pldm_msgbuf_extract(buf, tid);
2397 rc = pldm_msgbuf_extract(buf, event_id);
2398 if (rc) {
2399 return rc;
2400 }
2401 if ((*event_id == 0) || (*event_id == 0xffff)) {
2402 return PLDM_SUCCESS;
2403 }
2404
2405 pldm_msgbuf_extract(buf, next_data_transfer_handle);
2406 rc = pldm_msgbuf_extract(buf, transfer_flag);
2407 if (rc) {
2408 return rc;
2409 }
2410
2411 pldm_msgbuf_extract(buf, event_class);
2412 rc = pldm_msgbuf_extract(buf, event_data_size);
2413 if (rc) {
2414 return rc;
2415 }
2416 if (*event_data_size > payload_length) {
2417 return PLDM_ERROR_INVALID_DATA;
2418 }
2419
2420 if (*event_data_size > 0) {
2421 pldm_msgbuf_span_required(buf, *event_data_size, event_data);
2422 }
2423
2424 if (*transfer_flag == PLDM_END ||
2425 *transfer_flag == PLDM_START_AND_END) {
2426 pldm_msgbuf_extract(buf, event_data_integrity_checksum);
2427 }
2428
2429 return pldm_msgbuf_destroy_consumed(buf);
2430}