blob: 9364da385d52439bb496bb6e415e72bd2914d6d3 [file] [log] [blame]
Patrick Williams691668f2023-11-01 08:19:10 -05001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Andrew Jeffery7992eb82023-04-06 16:13:53 +09302#include "msgbuf.h"
Andrew Jefferyb0c1d202023-11-07 22:08:44 +10303#include "msgbuf/platform.h"
4
5#include <libpldm/base.h>
6#include <libpldm/platform.h>
7#include <libpldm/pldm_types.h>
8
Andrew Jeffery9c766792022-08-10 23:12:49 +09309#include <endian.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +053010#include <stdint.h>
Thu Nguyen159a98b2022-11-02 10:00:10 +070011#include <stdlib.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +093012#include <string.h>
13
Andrew Jeffery7992eb82023-04-06 16:13:53 +093014static int pldm_platform_pdr_hdr_validate(struct pldm_value_pdr_hdr *ctx,
15 size_t lower, size_t upper)
16{
17 if (ctx->length + sizeof(*ctx) < lower) {
18 return PLDM_ERROR_INVALID_LENGTH;
19 }
20
21 if (ctx->length > upper) {
22 return PLDM_ERROR_INVALID_LENGTH;
23 }
24
25 return PLDM_SUCCESS;
26}
Andrew Jeffery9c766792022-08-10 23:12:49 +093027
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093028LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +093029int encode_state_effecter_pdr(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093030 struct pldm_state_effecter_pdr *const effecter,
31 const size_t allocation_size,
32 const struct state_effecter_possible_states *const possible_states,
33 const size_t possible_states_size, size_t *const actual_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +093034{
35 // Encode possible states
36
37 size_t calculated_possible_states_size = 0;
38
39 {
40 char *states_ptr = (char *)possible_states;
41 char *const begin_states_ptr = states_ptr;
42
43 for (int i = 0; i < effecter->composite_effecter_count; ++i) {
44 struct state_effecter_possible_states *states =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093045 (struct state_effecter_possible_states *)
46 states_ptr;
Andrew Jeffery9c766792022-08-10 23:12:49 +093047
48 HTOLE16(states->state_set_id);
49
50 states_ptr +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093051 (sizeof(*states) - sizeof(states->states) +
52 states->possible_states_size);
Andrew Jeffery9c766792022-08-10 23:12:49 +093053 }
54
55 calculated_possible_states_size = states_ptr - begin_states_ptr;
56 }
57
58 // Check lengths
59
60 if (possible_states_size != calculated_possible_states_size) {
61 *actual_size = 0;
62 return PLDM_ERROR;
63 }
64
65 *actual_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093066 (sizeof(struct pldm_state_effecter_pdr) + possible_states_size -
67 sizeof(effecter->possible_states));
Andrew Jeffery9c766792022-08-10 23:12:49 +093068
69 if (allocation_size < *actual_size) {
70 *actual_size = 0;
71 return PLDM_ERROR_INVALID_LENGTH;
72 }
73
74 // Encode rest of PDR
75
76 effecter->hdr.version = 1;
77 effecter->hdr.type = PLDM_STATE_EFFECTER_PDR;
78 effecter->hdr.length = *actual_size - sizeof(struct pldm_pdr_hdr);
79
80 memcpy(effecter->possible_states, possible_states,
81 possible_states_size);
82
83 // Convert effecter PDR body
84 HTOLE16(effecter->terminus_handle);
85 HTOLE16(effecter->effecter_id);
86 HTOLE16(effecter->entity_type);
87 HTOLE16(effecter->entity_instance);
88 HTOLE16(effecter->container_id);
89 HTOLE16(effecter->effecter_semantic_id);
90
91 // Convert header
92 HTOLE32(effecter->hdr.record_handle);
93 HTOLE16(effecter->hdr.record_change_num);
94 HTOLE16(effecter->hdr.length);
95
96 return PLDM_SUCCESS;
97}
98
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093099LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930100int encode_state_sensor_pdr(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930101 struct pldm_state_sensor_pdr *const sensor,
102 const size_t allocation_size,
103 const struct state_sensor_possible_states *const possible_states,
104 const size_t possible_states_size, size_t *const actual_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930105{
106 // Encode possible states
107
108 size_t calculated_possible_states_size = 0;
109
110 {
Andrew Jefferyfbe61d72023-04-05 20:28:23 +0930111 char *states_ptr = (char *)possible_states;
112 char *const begin_states_ptr = states_ptr;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930113
114 for (int i = 0; i < sensor->composite_sensor_count; ++i) {
115 struct state_sensor_possible_states *states =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930116 (struct state_sensor_possible_states *)
117 states_ptr;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930118
119 HTOLE16(states->state_set_id);
120
121 states_ptr +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930122 (sizeof(*states) - sizeof(states->states) +
123 states->possible_states_size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930124 }
125
126 calculated_possible_states_size = states_ptr - begin_states_ptr;
127 }
128
129 // Check lengths
130
131 if (possible_states_size != calculated_possible_states_size) {
132 *actual_size = 0;
133 return PLDM_ERROR;
134 }
135
136 *actual_size = (sizeof(struct pldm_state_sensor_pdr) +
137 possible_states_size - sizeof(sensor->possible_states));
138
139 if (allocation_size < *actual_size) {
140 *actual_size = 0;
141 return PLDM_ERROR_INVALID_LENGTH;
142 }
143
144 // Encode rest of PDR
145
146 sensor->hdr.version = 1;
147 sensor->hdr.type = PLDM_STATE_SENSOR_PDR;
148 sensor->hdr.length = *actual_size - sizeof(struct pldm_pdr_hdr);
149
150 memcpy(sensor->possible_states, possible_states, possible_states_size);
151
152 // Convert sensor PDR body
153 HTOLE16(sensor->terminus_handle);
154 HTOLE16(sensor->sensor_id);
155 HTOLE16(sensor->entity_type);
156 HTOLE16(sensor->entity_instance);
157 HTOLE16(sensor->container_id);
158
159 // Convert header
160 HTOLE32(sensor->hdr.record_handle);
161 HTOLE16(sensor->hdr.record_change_num);
162 HTOLE16(sensor->hdr.length);
163
164 return PLDM_SUCCESS;
165}
166
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930167LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930168int encode_set_state_effecter_states_resp(uint8_t instance_id,
169 uint8_t completion_code,
170 struct pldm_msg *msg)
171{
172 if (msg == NULL) {
173 return PLDM_ERROR_INVALID_DATA;
174 }
175
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930176 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930177 header.msg_type = PLDM_RESPONSE;
178 header.instance = instance_id;
179 header.pldm_type = PLDM_PLATFORM;
180 header.command = PLDM_SET_STATE_EFFECTER_STATES;
181
182 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
183 if (rc != PLDM_SUCCESS) {
184 return rc;
185 }
186
187 msg->payload[0] = completion_code;
188
189 return PLDM_SUCCESS;
190}
191
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930192LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930193int encode_set_state_effecter_states_req(uint8_t instance_id,
194 uint16_t effecter_id,
195 uint8_t comp_effecter_count,
196 set_effecter_state_field *field,
197 struct pldm_msg *msg)
198{
199 if (msg == NULL) {
200 return PLDM_ERROR_INVALID_DATA;
201 }
202
203 if (comp_effecter_count < 0x1 || comp_effecter_count > 0x8 ||
204 field == NULL) {
205 return PLDM_ERROR_INVALID_DATA;
206 }
207
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930208 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930209 header.msg_type = PLDM_REQUEST;
210 header.instance = instance_id;
211 header.pldm_type = PLDM_PLATFORM;
212 header.command = PLDM_SET_STATE_EFFECTER_STATES;
213
214 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
215 if (rc != PLDM_SUCCESS) {
216 return rc;
217 }
218
219 struct pldm_set_state_effecter_states_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930220 (struct pldm_set_state_effecter_states_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930221 effecter_id = htole16(effecter_id);
222 request->effecter_id = effecter_id;
223 request->comp_effecter_count = comp_effecter_count;
224 memcpy(request->field, field,
225 (sizeof(set_effecter_state_field) * comp_effecter_count));
226
227 return PLDM_SUCCESS;
228}
229
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930230LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930231int decode_set_state_effecter_states_resp(const struct pldm_msg *msg,
232 size_t payload_length,
233 uint8_t *completion_code)
234{
235 if (msg == NULL || completion_code == NULL) {
236 return PLDM_ERROR_INVALID_DATA;
237 }
238
239 *completion_code = msg->payload[0];
240 if (PLDM_SUCCESS != *completion_code) {
241 return PLDM_SUCCESS;
242 }
243
244 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES) {
245 return PLDM_ERROR_INVALID_LENGTH;
246 }
247
248 return PLDM_SUCCESS;
249}
250
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930251#define PLDM_SET_STATE_EFFECTER_STATES_MIN_SIZE 3
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930252LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930253int decode_set_state_effecter_states_req(const struct pldm_msg *msg,
254 size_t payload_length,
255 uint16_t *effecter_id,
256 uint8_t *comp_effecter_count,
257 set_effecter_state_field *field)
258{
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930259 struct pldm_msgbuf _buf;
260 struct pldm_msgbuf *buf = &_buf;
261 int rc;
262 int i;
263
Andrew Jeffery9c766792022-08-10 23:12:49 +0930264 if (msg == NULL || effecter_id == NULL || comp_effecter_count == NULL ||
265 field == NULL) {
266 return PLDM_ERROR_INVALID_DATA;
267 }
268
269 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) {
270 return PLDM_ERROR_INVALID_LENGTH;
271 }
272
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930273 rc = pldm_msgbuf_init(buf, PLDM_SET_STATE_EFFECTER_STATES_MIN_SIZE,
274 msg->payload, payload_length);
275 if (rc) {
276 return rc;
277 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930278
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930279 pldm_msgbuf_extract(buf, effecter_id);
280 pldm_msgbuf_extract(buf, comp_effecter_count);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930281
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930282 if (*comp_effecter_count > 8) {
283 return PLDM_ERROR_INVALID_DATA;
284 }
285
286 for (i = 0; i < *comp_effecter_count; i++) {
287 pldm_msgbuf_extract(buf, &field[i].set_request);
288 pldm_msgbuf_extract(buf, &field[i].effecter_state);
289 }
290
291 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930292}
293
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930294LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930295int decode_get_pdr_req(const struct pldm_msg *msg, size_t payload_length,
296 uint32_t *record_hndl, uint32_t *data_transfer_hndl,
297 uint8_t *transfer_op_flag, uint16_t *request_cnt,
298 uint16_t *record_chg_num)
299{
Andrew Jeffery891781e2023-04-04 11:04:18 +0930300 struct pldm_msgbuf _buf;
301 struct pldm_msgbuf *buf = &_buf;
302 int rc;
303
Andrew Jeffery9c766792022-08-10 23:12:49 +0930304 if (msg == NULL || record_hndl == NULL || data_transfer_hndl == NULL ||
305 transfer_op_flag == NULL || request_cnt == NULL ||
306 record_chg_num == NULL) {
307 return PLDM_ERROR_INVALID_DATA;
308 }
Andrew Jeffery891781e2023-04-04 11:04:18 +0930309
Andrew Jeffery9c766792022-08-10 23:12:49 +0930310 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
311 return PLDM_ERROR_INVALID_LENGTH;
312 }
313
Andrew Jeffery891781e2023-04-04 11:04:18 +0930314 rc = pldm_msgbuf_init(buf, PLDM_GET_PDR_REQ_BYTES, msg->payload,
315 payload_length);
316 if (rc) {
317 return rc;
318 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930319
Andrew Jeffery891781e2023-04-04 11:04:18 +0930320 pldm_msgbuf_extract(buf, record_hndl);
321 pldm_msgbuf_extract(buf, data_transfer_hndl);
322 pldm_msgbuf_extract(buf, transfer_op_flag);
323 pldm_msgbuf_extract(buf, request_cnt);
324 pldm_msgbuf_extract(buf, record_chg_num);
325
326 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930327}
328
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930329LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930330int encode_get_pdr_resp(uint8_t instance_id, uint8_t completion_code,
331 uint32_t next_record_hndl,
332 uint32_t next_data_transfer_hndl, uint8_t transfer_flag,
333 uint16_t resp_cnt, const uint8_t *record_data,
334 uint8_t transfer_crc, struct pldm_msg *msg)
335{
336 if (msg == NULL) {
337 return PLDM_ERROR_INVALID_DATA;
338 }
339
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930340 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930341 header.msg_type = PLDM_RESPONSE;
342 header.instance = instance_id;
343 header.pldm_type = PLDM_PLATFORM;
344 header.command = PLDM_GET_PDR;
345
346 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
347 if (rc != PLDM_SUCCESS) {
348 return rc;
349 }
350
351 struct pldm_get_pdr_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930352 (struct pldm_get_pdr_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930353 response->completion_code = completion_code;
354
355 if (response->completion_code == PLDM_SUCCESS) {
356 response->next_record_handle = htole32(next_record_hndl);
357 response->next_data_transfer_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930358 htole32(next_data_transfer_hndl);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930359 response->transfer_flag = transfer_flag;
360 response->response_count = htole16(resp_cnt);
361 if (record_data != NULL && resp_cnt > 0) {
362 memcpy(response->record_data, record_data, resp_cnt);
363 }
364 if (transfer_flag == PLDM_END) {
365 uint8_t *dst = msg->payload;
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930366 dst += (sizeof(struct pldm_get_pdr_resp) - 1) +
367 resp_cnt;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930368 *dst = transfer_crc;
369 }
370 }
371
372 return PLDM_SUCCESS;
373}
374
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930375LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930376int encode_get_pdr_repository_info_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930377 uint8_t instance_id, uint8_t completion_code, uint8_t repository_state,
378 const uint8_t *update_time, const uint8_t *oem_update_time,
379 uint32_t record_count, uint32_t repository_size,
380 uint32_t largest_record_size, uint8_t data_transfer_handle_timeout,
381 struct pldm_msg *msg)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930382{
383 if (msg == NULL) {
384 return PLDM_ERROR_INVALID_DATA;
385 }
386
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930387 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930388 header.msg_type = PLDM_RESPONSE;
389 header.instance = instance_id;
390 header.pldm_type = PLDM_PLATFORM;
391 header.command = PLDM_GET_PDR_REPOSITORY_INFO;
392
393 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
394 if (rc != PLDM_SUCCESS) {
395 return rc;
396 }
397
398 struct pldm_pdr_repository_info_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930399 (struct pldm_pdr_repository_info_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930400 response->completion_code = completion_code;
401
402 if (response->completion_code == PLDM_SUCCESS) {
403 response->repository_state = repository_state;
404 if (update_time != NULL) {
405 memcpy(response->update_time, update_time,
406 PLDM_TIMESTAMP104_SIZE);
407 }
408 if (oem_update_time != NULL) {
409 memcpy(response->oem_update_time, oem_update_time,
410 PLDM_TIMESTAMP104_SIZE);
411 }
412 response->record_count = htole32(record_count);
413 response->repository_size = htole32(repository_size);
414 response->largest_record_size = htole32(largest_record_size);
415 response->data_transfer_handle_timeout =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930416 data_transfer_handle_timeout;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930417 }
418
419 return PLDM_SUCCESS;
420}
421
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930422LIBPLDM_ABI_STABLE
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800423int decode_get_pdr_repository_info_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930424 const struct pldm_msg *msg, size_t payload_length,
425 uint8_t *completion_code, uint8_t *repository_state,
426 uint8_t *update_time, uint8_t *oem_update_time, uint32_t *record_count,
427 uint32_t *repository_size, uint32_t *largest_record_size,
428 uint8_t *data_transfer_handle_timeout)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800429{
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930430 struct pldm_msgbuf _buf;
431 struct pldm_msgbuf *buf = &_buf;
432 int rc;
433
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800434 if (msg == NULL || completion_code == NULL ||
435 repository_state == NULL || update_time == NULL ||
436 oem_update_time == NULL || record_count == NULL ||
437 repository_size == NULL || largest_record_size == NULL ||
438 data_transfer_handle_timeout == NULL) {
439 return PLDM_ERROR_INVALID_DATA;
440 }
441
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930442 rc = pldm_msgbuf_init(buf, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES,
443 msg->payload, payload_length);
444 if (rc) {
445 return rc;
446 }
447
448 pldm_msgbuf_extract(buf, completion_code);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800449 if (PLDM_SUCCESS != *completion_code) {
450 return PLDM_SUCCESS;
451 }
452
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930453 pldm_msgbuf_extract(buf, repository_state);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800454 if (*repository_state > PLDM_FAILED) {
455 return PLDM_ERROR_INVALID_DATA;
456 }
457
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930458 pldm_msgbuf_extract_array(buf, update_time, PLDM_TIMESTAMP104_SIZE);
459 pldm_msgbuf_extract_array(buf, oem_update_time, PLDM_TIMESTAMP104_SIZE);
460 pldm_msgbuf_extract(buf, record_count);
461 pldm_msgbuf_extract(buf, repository_size);
462 pldm_msgbuf_extract(buf, largest_record_size);
463 pldm_msgbuf_extract(buf, data_transfer_handle_timeout);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800464
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930465 return pldm_msgbuf_destroy(buf);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800466}
467
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930468LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930469int encode_get_pdr_req(uint8_t instance_id, uint32_t record_hndl,
470 uint32_t data_transfer_hndl, uint8_t transfer_op_flag,
471 uint16_t request_cnt, uint16_t record_chg_num,
472 struct pldm_msg *msg, size_t payload_length)
473{
474 if (msg == NULL) {
475 return PLDM_ERROR_INVALID_DATA;
476 }
477
478 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
479 return PLDM_ERROR_INVALID_LENGTH;
480 }
481
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930482 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930483 header.msg_type = PLDM_REQUEST;
484 header.instance = instance_id;
485 header.pldm_type = PLDM_PLATFORM;
486 header.command = PLDM_GET_PDR;
487
488 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
489 if (rc != PLDM_SUCCESS) {
490 return rc;
491 }
492
493 struct pldm_get_pdr_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930494 (struct pldm_get_pdr_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930495 request->record_handle = htole32(record_hndl);
496 request->data_transfer_handle = htole32(data_transfer_hndl);
497 request->transfer_op_flag = transfer_op_flag;
498 request->request_count = htole16(request_cnt);
499 request->record_change_number = htole16(record_chg_num);
500
501 return PLDM_SUCCESS;
502}
503
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930504LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930505int decode_get_pdr_resp(const struct pldm_msg *msg, size_t payload_length,
506 uint8_t *completion_code, uint32_t *next_record_hndl,
507 uint32_t *next_data_transfer_hndl,
508 uint8_t *transfer_flag, uint16_t *resp_cnt,
509 uint8_t *record_data, size_t record_data_length,
510 uint8_t *transfer_crc)
511{
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930512 struct pldm_msgbuf _buf;
513 struct pldm_msgbuf *buf = &_buf;
514 int rc;
515
Andrew Jeffery9c766792022-08-10 23:12:49 +0930516 if (msg == NULL || completion_code == NULL ||
517 next_record_hndl == NULL || next_data_transfer_hndl == NULL ||
518 transfer_flag == NULL || resp_cnt == NULL || transfer_crc == NULL) {
519 return PLDM_ERROR_INVALID_DATA;
520 }
521
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930522 rc = pldm_msgbuf_init(buf, PLDM_GET_PDR_MIN_RESP_BYTES, msg->payload,
523 payload_length);
524 if (rc) {
525 return rc;
526 }
527
528 pldm_msgbuf_extract(buf, completion_code);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930529 if (PLDM_SUCCESS != *completion_code) {
530 return PLDM_SUCCESS;
531 }
532
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930533 pldm_msgbuf_extract(buf, next_record_hndl);
534 pldm_msgbuf_extract(buf, next_data_transfer_hndl);
535 pldm_msgbuf_extract(buf, transfer_flag);
536 rc = pldm_msgbuf_extract(buf, resp_cnt);
537 if (rc) {
538 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930539 }
540
541 if (*resp_cnt > 0 && record_data != NULL) {
542 if (record_data_length < *resp_cnt) {
543 return PLDM_ERROR_INVALID_LENGTH;
544 }
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930545 pldm_msgbuf_extract_array(buf, record_data, *resp_cnt);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930546 }
547
548 if (*transfer_flag == PLDM_END) {
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930549 pldm_msgbuf_extract(buf, transfer_crc);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930550 }
551
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930552 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930553}
554
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930555LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930556int decode_set_numeric_effecter_value_req(const struct pldm_msg *msg,
557 size_t payload_length,
558 uint16_t *effecter_id,
559 uint8_t *effecter_data_size,
Andrew Jeffery3884c442023-04-12 11:13:24 +0930560 uint8_t effecter_value[4])
Andrew Jeffery9c766792022-08-10 23:12:49 +0930561{
Andrew Jeffery3884c442023-04-12 11:13:24 +0930562 struct pldm_msgbuf _buf;
563 struct pldm_msgbuf *buf = &_buf;
564 int rc;
565
Andrew Jeffery9c766792022-08-10 23:12:49 +0930566 if (msg == NULL || effecter_id == NULL || effecter_data_size == NULL ||
567 effecter_value == NULL) {
568 return PLDM_ERROR_INVALID_DATA;
569 }
570
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930571 rc = pldm_msgbuf_init(buf,
572 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES,
573 msg->payload, payload_length);
Andrew Jeffery3884c442023-04-12 11:13:24 +0930574 if (rc) {
575 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930576 }
577
Andrew Jeffery3884c442023-04-12 11:13:24 +0930578 pldm_msgbuf_extract(buf, effecter_id);
579 rc = pldm_msgbuf_extract(buf, effecter_data_size);
580 if (rc) {
581 return PLDM_ERROR_INVALID_DATA;
582 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930583
584 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
585 return PLDM_ERROR_INVALID_DATA;
586 }
587
Andrew Jeffery3884c442023-04-12 11:13:24 +0930588 pldm_msgbuf_extract_effecter_value(buf, *effecter_data_size,
589 effecter_value);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930590
Andrew Jeffery3884c442023-04-12 11:13:24 +0930591 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930592}
593
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930594LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930595int encode_set_numeric_effecter_value_resp(uint8_t instance_id,
596 uint8_t completion_code,
597 struct pldm_msg *msg,
598 size_t payload_length)
599{
600 if (msg == NULL) {
601 return PLDM_ERROR_INVALID_DATA;
602 }
603
604 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
605 return PLDM_ERROR_INVALID_LENGTH;
606 }
607
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930608 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930609 header.msg_type = PLDM_RESPONSE;
610 header.instance = instance_id;
611 header.pldm_type = PLDM_PLATFORM;
612 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
613
614 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
615 if (rc != PLDM_SUCCESS) {
616 return rc;
617 }
618
619 msg->payload[0] = completion_code;
620
621 return rc;
622}
623
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930624LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930625int encode_set_numeric_effecter_value_req(uint8_t instance_id,
626 uint16_t effecter_id,
627 uint8_t effecter_data_size,
628 const uint8_t *effecter_value,
629 struct pldm_msg *msg,
630 size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930631{
632 if (msg == NULL || effecter_value == NULL) {
633 return PLDM_ERROR_INVALID_DATA;
634 }
635
636 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
637 return PLDM_ERROR_INVALID_DATA;
638 }
639
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930640 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930641 header.msg_type = PLDM_REQUEST;
642 header.instance = instance_id;
643 header.pldm_type = PLDM_PLATFORM;
644 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
645
646 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
647 if (rc != PLDM_SUCCESS) {
648 return rc;
649 }
650
651 struct pldm_set_numeric_effecter_value_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930652 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930653 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
654 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
655 if (payload_length !=
656 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
657 return PLDM_ERROR_INVALID_LENGTH;
658 }
659 request->effecter_value[0] = *effecter_value;
660 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
661 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
662 if (payload_length !=
663 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
664 return PLDM_ERROR_INVALID_LENGTH;
665 }
666
667 uint16_t val = *(uint16_t *)(effecter_value);
668 val = htole16(val);
669 memcpy(request->effecter_value, &val, sizeof(uint16_t));
670
671 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
672 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
673 if (payload_length !=
674 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
675 return PLDM_ERROR_INVALID_LENGTH;
676 }
677
678 uint32_t val = *(uint32_t *)(effecter_value);
679 val = htole32(val);
680 memcpy(request->effecter_value, &val, sizeof(uint32_t));
681 }
682
683 request->effecter_id = htole16(effecter_id);
684 request->effecter_data_size = effecter_data_size;
685
686 return PLDM_SUCCESS;
687}
688
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930689LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930690int decode_set_numeric_effecter_value_resp(const struct pldm_msg *msg,
691 size_t payload_length,
692 uint8_t *completion_code)
693{
694 if (msg == NULL || completion_code == NULL) {
695 return PLDM_ERROR_INVALID_DATA;
696 }
697
698 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
699 return PLDM_ERROR_INVALID_LENGTH;
700 }
701
702 *completion_code = msg->payload[0];
703
704 return PLDM_SUCCESS;
705}
706
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930707LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930708int encode_get_state_sensor_readings_resp(uint8_t instance_id,
709 uint8_t completion_code,
710 uint8_t comp_sensor_count,
711 get_sensor_state_field *field,
712 struct pldm_msg *msg)
713{
714 if (msg == NULL) {
715 return PLDM_ERROR_INVALID_DATA;
716 }
717
718 if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
719 return PLDM_ERROR_INVALID_DATA;
720 }
721
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930722 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930723 header.msg_type = PLDM_RESPONSE;
724 header.instance = instance_id;
725 header.pldm_type = PLDM_PLATFORM;
726 header.command = PLDM_GET_STATE_SENSOR_READINGS;
727
728 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
729 if (rc != PLDM_SUCCESS) {
730 return rc;
731 }
732
733 struct pldm_get_state_sensor_readings_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930734 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930735
736 response->completion_code = completion_code;
737 response->comp_sensor_count = comp_sensor_count;
738 memcpy(response->field, field,
739 (sizeof(get_sensor_state_field) * comp_sensor_count));
740
741 return PLDM_SUCCESS;
742}
743
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930744LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930745int encode_get_state_sensor_readings_req(uint8_t instance_id,
746 uint16_t sensor_id,
747 bitfield8_t sensor_rearm,
748 uint8_t reserved, struct pldm_msg *msg)
749{
750 if (msg == NULL) {
751 return PLDM_ERROR_INVALID_DATA;
752 }
753
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930754 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930755 header.msg_type = PLDM_REQUEST;
756 header.instance = instance_id;
757 header.pldm_type = PLDM_PLATFORM;
758 header.command = PLDM_GET_STATE_SENSOR_READINGS;
759
760 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
761 if (rc != PLDM_SUCCESS) {
762 return rc;
763 }
764
765 struct pldm_get_state_sensor_readings_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930766 (struct pldm_get_state_sensor_readings_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930767
768 request->sensor_id = htole16(sensor_id);
769 request->reserved = reserved;
770 request->sensor_rearm = sensor_rearm;
771
772 return PLDM_SUCCESS;
773}
774
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930775LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930776int decode_get_state_sensor_readings_resp(const struct pldm_msg *msg,
777 size_t payload_length,
778 uint8_t *completion_code,
779 uint8_t *comp_sensor_count,
780 get_sensor_state_field *field)
781{
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930782 struct pldm_msgbuf _buf;
783 struct pldm_msgbuf *buf = &_buf;
784 uint8_t i;
785 int rc;
786
Andrew Jeffery9c766792022-08-10 23:12:49 +0930787 if (msg == NULL || completion_code == NULL ||
788 comp_sensor_count == NULL || field == NULL) {
789 return PLDM_ERROR_INVALID_DATA;
790 }
791
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930792 rc = pldm_msgbuf_init(buf,
793 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES,
794 msg->payload, payload_length);
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930795 if (rc) {
796 return rc;
797 }
798
799 rc = pldm_msgbuf_extract(buf, completion_code);
800 if (rc) {
801 return rc;
802 }
803
Andrew Jeffery9c766792022-08-10 23:12:49 +0930804 if (PLDM_SUCCESS != *completion_code) {
805 return PLDM_SUCCESS;
806 }
807
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930808 rc = pldm_msgbuf_extract(buf, comp_sensor_count);
809 if (rc) {
810 return rc;
811 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930812
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930813 if (*comp_sensor_count < 0x1 || *comp_sensor_count > 0x8) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930814 return PLDM_ERROR_INVALID_DATA;
815 }
816
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930817 for (i = 0; i < *comp_sensor_count; i++) {
818 pldm_msgbuf_extract(buf, &field[i].sensor_op_state);
819 pldm_msgbuf_extract(buf, &field[i].present_state);
820 pldm_msgbuf_extract(buf, &field[i].previous_state);
821 pldm_msgbuf_extract(buf, &field[i].event_state);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930822 }
823
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930824 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930825}
826
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930827LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930828int decode_get_state_sensor_readings_req(const struct pldm_msg *msg,
829 size_t payload_length,
830 uint16_t *sensor_id,
831 bitfield8_t *sensor_rearm,
832 uint8_t *reserved)
833{
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930834 struct pldm_msgbuf _buf;
835 struct pldm_msgbuf *buf = &_buf;
836 int rc;
837
Andrew Jeffery9c766792022-08-10 23:12:49 +0930838 if (msg == NULL || sensor_id == NULL || sensor_rearm == NULL) {
839 return PLDM_ERROR_INVALID_DATA;
840 }
841
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930842 rc = pldm_msgbuf_init(buf, PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES,
843 msg->payload, payload_length);
844 if (rc) {
845 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930846 }
847
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930848 pldm_msgbuf_extract(buf, sensor_id);
849 pldm_msgbuf_extract(buf, &sensor_rearm->byte);
850 pldm_msgbuf_extract(buf, reserved);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930851
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930852 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930853}
854
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930855LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930856int encode_sensor_event_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930857 struct pldm_sensor_event_data *const event_data,
858 const size_t event_data_size, const uint16_t sensor_id,
859 const enum sensor_event_class_states sensor_event_class,
860 const uint8_t sensor_offset, const uint8_t event_state,
861 const uint8_t previous_event_state,
862 size_t *const actual_event_data_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930863{
864 *actual_event_data_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930865 (sizeof(*event_data) - sizeof(event_data->event_class) +
866 sizeof(struct pldm_sensor_event_state_sensor_state));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930867
868 if (!event_data) {
869 return PLDM_SUCCESS;
870 }
871
872 if (event_data_size < *actual_event_data_size) {
873 *actual_event_data_size = 0;
874 return PLDM_ERROR_INVALID_LENGTH;
875 }
876
877 event_data->sensor_id = htole16(sensor_id);
878 event_data->sensor_event_class_type = sensor_event_class;
879
880 struct pldm_sensor_event_state_sensor_state *const state_data =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930881 (struct pldm_sensor_event_state_sensor_state *)
882 event_data->event_class;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930883
884 state_data->sensor_offset = sensor_offset;
885 state_data->event_state = event_state;
886 state_data->previous_event_state = previous_event_state;
887
888 return PLDM_SUCCESS;
889}
890
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930891LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930892int decode_platform_event_message_req(const struct pldm_msg *msg,
893 size_t payload_length,
894 uint8_t *format_version, uint8_t *tid,
895 uint8_t *event_class,
896 size_t *event_data_offset)
897{
Andrew Jefferydc48ce32023-04-13 12:01:42 +0930898 struct pldm_msgbuf _buf;
899 struct pldm_msgbuf *buf = &_buf;
900 int rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930901
902 if (msg == NULL || format_version == NULL || tid == NULL ||
903 event_class == NULL || event_data_offset == NULL) {
904 return PLDM_ERROR_INVALID_DATA;
905 }
906
Andrew Jefferydc48ce32023-04-13 12:01:42 +0930907 rc = pldm_msgbuf_init(buf, PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES,
908 msg->payload, payload_length);
909 if (rc) {
910 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930911 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930912
Andrew Jefferydc48ce32023-04-13 12:01:42 +0930913 pldm_msgbuf_extract(buf, format_version);
914 pldm_msgbuf_extract(buf, tid);
915 pldm_msgbuf_extract(buf, event_class);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930916 *event_data_offset =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930917 sizeof(*format_version) + sizeof(*tid) + sizeof(*event_class);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930918
Andrew Jefferydc48ce32023-04-13 12:01:42 +0930919 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930920}
921
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930922LIBPLDM_ABI_STABLE
Thu Nguyen8eb20f22022-11-16 22:34:55 +0700923int decode_poll_for_platform_event_message_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930924 const struct pldm_msg *msg, size_t payload_length,
925 uint8_t *format_version, uint8_t *transfer_operation_flag,
926 uint32_t *data_transfer_handle, uint16_t *event_id_to_acknowledge)
Thu Nguyen8eb20f22022-11-16 22:34:55 +0700927{
928 struct pldm_msgbuf _buf;
929 struct pldm_msgbuf *buf = &_buf;
930 int rc;
931
932 if (msg == NULL) {
933 return PLDM_ERROR_INVALID_DATA;
934 }
935
936 rc = pldm_msgbuf_init(buf,
937 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES,
938 msg->payload, payload_length);
939 if (rc) {
940 return rc;
941 }
942
943 pldm_msgbuf_extract(buf, format_version);
944 rc = pldm_msgbuf_extract(buf, transfer_operation_flag);
945 if (rc) {
946 return rc;
947 }
948 if (*transfer_operation_flag > PLDM_ACKNOWLEDGEMENT_ONLY) {
949 return PLDM_ERROR_INVALID_DATA;
950 }
951
952 pldm_msgbuf_extract(buf, data_transfer_handle);
953 rc = pldm_msgbuf_extract(buf, event_id_to_acknowledge);
954 if (rc) {
955 return rc;
956 }
957
958 if (!(((*transfer_operation_flag == PLDM_GET_NEXTPART) &&
959 (*event_id_to_acknowledge == 0xFFFF)) ||
960 ((*transfer_operation_flag == PLDM_GET_FIRSTPART) &&
961 (*event_id_to_acknowledge == 0x000)) ||
962 (*transfer_operation_flag == PLDM_ACKNOWLEDGEMENT_ONLY))) {
963 return PLDM_ERROR_INVALID_DATA;
964 }
965
966 return pldm_msgbuf_destroy(buf);
967}
968
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930969LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930970int encode_platform_event_message_resp(uint8_t instance_id,
971 uint8_t completion_code,
972 uint8_t platform_event_status,
973 struct pldm_msg *msg)
974{
975 if (msg == NULL) {
976 return PLDM_ERROR_INVALID_DATA;
977 }
978
979 if (platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
980 return PLDM_ERROR_INVALID_DATA;
981 }
982
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930983 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930984 header.msg_type = PLDM_RESPONSE;
985 header.instance = instance_id;
986 header.pldm_type = PLDM_PLATFORM;
987 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
988
989 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
990 if (rc != PLDM_SUCCESS) {
991 return rc;
992 }
993
994 struct pldm_platform_event_message_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930995 (struct pldm_platform_event_message_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930996 response->completion_code = completion_code;
997 response->platform_event_status = platform_event_status;
998
999 return PLDM_SUCCESS;
1000}
1001
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301002LIBPLDM_ABI_STABLE
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001003int encode_poll_for_platform_event_message_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301004 uint8_t instance_id, uint8_t completion_code, uint8_t tid,
1005 uint16_t event_id, uint32_t next_data_transfer_handle,
1006 uint8_t transfer_flag, uint8_t event_class, uint32_t event_data_size,
1007 uint8_t *event_data, uint32_t checksum, struct pldm_msg *msg,
1008 size_t payload_length)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001009{
1010 struct pldm_msgbuf _buf;
1011 struct pldm_msgbuf *buf = &_buf;
1012 int rc;
1013
1014 if (!msg) {
1015 return PLDM_ERROR_INVALID_DATA;
1016 }
1017
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301018 struct pldm_header_info header = { 0 };
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001019 header.msg_type = PLDM_RESPONSE;
1020 header.instance = instance_id;
1021 header.pldm_type = PLDM_PLATFORM;
1022 header.command = PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE;
1023
1024 rc = pack_pldm_header(&header, &(msg->hdr));
1025 if (rc != PLDM_SUCCESS) {
1026 return rc;
1027 }
1028
1029 rc = pldm_msgbuf_init(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301030 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
1031 msg->payload, payload_length);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001032 if (rc) {
1033 return rc;
1034 }
1035
1036 pldm_msgbuf_insert(buf, completion_code);
1037 pldm_msgbuf_insert(buf, tid);
1038 pldm_msgbuf_insert(buf, event_id);
1039
1040 if (event_id == 0xffff || event_id == 0x0000) {
1041 if (PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES !=
1042 payload_length) {
1043 return PLDM_ERROR_INVALID_LENGTH;
1044 }
1045 return pldm_msgbuf_destroy(buf);
1046 }
1047
1048 if ((event_data == NULL) && (event_data_size > 0)) {
1049 return PLDM_ERROR_INVALID_DATA;
1050 }
1051
1052 pldm_msgbuf_insert(buf, next_data_transfer_handle);
1053 pldm_msgbuf_insert(buf, transfer_flag);
1054 pldm_msgbuf_insert(buf, event_class);
1055 pldm_msgbuf_insert(buf, event_data_size);
1056
1057 if ((event_data_size > 0) && event_data) {
1058 pldm_msgbuf_insert_array(buf, event_data, event_data_size);
1059 }
1060
1061 if (transfer_flag == PLDM_END || transfer_flag == PLDM_START_AND_END) {
1062 pldm_msgbuf_insert(buf, checksum);
1063 }
1064
1065 return pldm_msgbuf_destroy(buf);
1066}
1067
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301068LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301069int encode_platform_event_message_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301070 uint8_t instance_id, uint8_t format_version, uint8_t tid,
1071 uint8_t event_class, const uint8_t *event_data,
1072 size_t event_data_length, struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301073
1074{
1075 if (format_version != 1) {
1076 return PLDM_ERROR_INVALID_DATA;
1077 }
1078
1079 if (msg == NULL || event_data == NULL) {
1080 return PLDM_ERROR_INVALID_DATA;
1081 }
1082
1083 if (event_data_length == 0) {
1084 return PLDM_ERROR_INVALID_DATA;
1085 }
1086
1087 if (payload_length !=
1088 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + event_data_length) {
1089 return PLDM_ERROR_INVALID_LENGTH;
1090 }
1091
1092 if (event_class > PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT &&
1093 !(event_class >= 0xF0 && event_class <= 0xFE)) {
1094 return PLDM_ERROR_INVALID_DATA;
1095 }
1096
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301097 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301098 header.msg_type = PLDM_REQUEST;
1099 header.instance = instance_id;
1100 header.pldm_type = PLDM_PLATFORM;
1101 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
1102
1103 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1104 if (rc != PLDM_SUCCESS) {
1105 return rc;
1106 }
1107
1108 struct pldm_platform_event_message_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301109 (struct pldm_platform_event_message_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301110 request->format_version = format_version;
1111 request->tid = tid;
1112 request->event_class = event_class;
1113 memcpy(request->event_data, event_data, event_data_length);
1114
1115 return PLDM_SUCCESS;
1116}
1117
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301118LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301119int decode_platform_event_message_resp(const struct pldm_msg *msg,
1120 size_t payload_length,
1121 uint8_t *completion_code,
1122 uint8_t *platform_event_status)
1123{
Andrew Jefferye5011772023-04-13 12:06:22 +09301124 struct pldm_msgbuf _buf;
1125 struct pldm_msgbuf *buf = &_buf;
1126 int rc;
1127
Andrew Jeffery9c766792022-08-10 23:12:49 +09301128 if (msg == NULL || completion_code == NULL ||
1129 platform_event_status == NULL) {
1130 return PLDM_ERROR_INVALID_DATA;
1131 }
1132
Andrew Jefferye5011772023-04-13 12:06:22 +09301133 rc = pldm_msgbuf_init(buf, PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES,
1134 msg->payload, payload_length);
1135 if (rc) {
1136 return rc;
1137 }
1138
1139 rc = pldm_msgbuf_extract(buf, completion_code);
1140 if (rc) {
1141 return rc;
1142 }
1143
Andrew Jeffery9c766792022-08-10 23:12:49 +09301144 if (PLDM_SUCCESS != *completion_code) {
1145 return PLDM_SUCCESS;
1146 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301147
Andrew Jefferye5011772023-04-13 12:06:22 +09301148 rc = pldm_msgbuf_extract(buf, platform_event_status);
1149 if (rc) {
1150 return rc;
1151 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301152
1153 if (*platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
1154 return PLDM_ERROR_INVALID_DATA;
1155 }
1156
Andrew Jefferye5011772023-04-13 12:06:22 +09301157 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301158}
1159
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301160LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301161int encode_event_message_buffer_size_req(uint8_t instance_id,
1162 uint16_t event_receiver_max_buffer_size,
1163 struct pldm_msg *msg)
Dung Caod6ae8982022-11-02 10:00:10 +07001164{
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301165 struct pldm_header_info header = { 0 };
Dung Caod6ae8982022-11-02 10:00:10 +07001166 header.msg_type = PLDM_REQUEST;
1167 header.instance = instance_id;
1168 header.pldm_type = PLDM_PLATFORM;
1169 header.command = PLDM_EVENT_MESSAGE_BUFFER_SIZE;
1170
1171 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1172 if (rc != PLDM_SUCCESS) {
1173 return rc;
1174 }
1175
1176 struct pldm_event_message_buffer_size_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301177 (struct pldm_event_message_buffer_size_req *)msg->payload;
Dung Caod6ae8982022-11-02 10:00:10 +07001178 request->event_receiver_max_buffer_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301179 event_receiver_max_buffer_size;
Dung Caod6ae8982022-11-02 10:00:10 +07001180
1181 return PLDM_SUCCESS;
1182}
1183
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301184LIBPLDM_ABI_STABLE
Dung Caod6ae8982022-11-02 10:00:10 +07001185int decode_event_message_buffer_size_resp(const struct pldm_msg *msg,
1186 size_t payload_length,
1187 uint8_t *completion_code,
1188 uint16_t *terminus_max_buffer_size)
1189{
Andrew Jeffery11126902023-04-13 12:12:10 +09301190 struct pldm_msgbuf _buf;
1191 struct pldm_msgbuf *buf = &_buf;
1192 int rc;
1193
Dung Caod6ae8982022-11-02 10:00:10 +07001194 if (msg == NULL || completion_code == NULL ||
1195 terminus_max_buffer_size == NULL) {
1196 return PLDM_ERROR_INVALID_DATA;
1197 }
1198
Andrew Jeffery11126902023-04-13 12:12:10 +09301199 rc = pldm_msgbuf_init(buf, PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES,
1200 msg->payload, payload_length);
1201 if (rc) {
1202 return rc;
1203 }
1204
1205 rc = pldm_msgbuf_extract(buf, completion_code);
1206 if (rc) {
1207 return rc;
1208 }
1209
Dung Caod6ae8982022-11-02 10:00:10 +07001210 if (PLDM_SUCCESS != *completion_code) {
1211 return PLDM_SUCCESS;
1212 }
Dung Caod6ae8982022-11-02 10:00:10 +07001213
Andrew Jeffery11126902023-04-13 12:12:10 +09301214 pldm_msgbuf_extract(buf, terminus_max_buffer_size);
Dung Caod6ae8982022-11-02 10:00:10 +07001215
Andrew Jeffery11126902023-04-13 12:12:10 +09301216 return pldm_msgbuf_destroy_consumed(buf);
Dung Caod6ae8982022-11-02 10:00:10 +07001217}
1218
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301219LIBPLDM_ABI_STABLE
Dung Cao1bf8c872022-11-29 05:32:58 +07001220int encode_event_message_supported_req(uint8_t instance_id,
1221 uint8_t format_version,
1222 struct pldm_msg *msg)
1223{
1224 if (format_version != 1) {
1225 return PLDM_ERROR_INVALID_DATA;
1226 }
1227
1228 if (msg == NULL) {
1229 return PLDM_ERROR_INVALID_DATA;
1230 }
1231
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301232 struct pldm_header_info header = { 0 };
Dung Cao1bf8c872022-11-29 05:32:58 +07001233 header.msg_type = PLDM_REQUEST;
1234 header.instance = instance_id;
1235 header.pldm_type = PLDM_PLATFORM;
1236 header.command = PLDM_EVENT_MESSAGE_SUPPORTED;
1237
1238 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1239 if (rc != PLDM_SUCCESS) {
1240 return rc;
1241 }
1242
1243 struct pldm_event_message_supported_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301244 (struct pldm_event_message_supported_req *)msg->payload;
Dung Cao1bf8c872022-11-29 05:32:58 +07001245 request->format_version = format_version;
1246
1247 return PLDM_SUCCESS;
1248}
1249
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301250LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301251int decode_event_message_supported_resp(const struct pldm_msg *msg,
1252 size_t payload_length,
1253 uint8_t *completion_code,
1254 uint8_t *synchrony_config,
1255 bitfield8_t *synchrony_config_support,
1256 uint8_t *number_event_class_returned,
1257 uint8_t *event_class,
1258 uint8_t event_class_count)
Dung Cao1bf8c872022-11-29 05:32:58 +07001259{
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301260 struct pldm_msgbuf _buf;
1261 struct pldm_msgbuf *buf = &_buf;
1262 int i;
1263 int rc;
1264
Dung Cao1bf8c872022-11-29 05:32:58 +07001265 if (msg == NULL || completion_code == NULL ||
1266 synchrony_config == NULL || synchrony_config_support == NULL ||
1267 number_event_class_returned == NULL || event_class == NULL) {
1268 return PLDM_ERROR_INVALID_DATA;
1269 }
1270
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301271 rc = pldm_msgbuf_init(buf, PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES,
1272 msg->payload, payload_length);
1273 if (rc) {
1274 return rc;
1275 }
1276
1277 rc = pldm_msgbuf_extract(buf, completion_code);
1278 if (rc) {
1279 return rc;
1280 }
1281
Dung Cao1bf8c872022-11-29 05:32:58 +07001282 if (PLDM_SUCCESS != *completion_code) {
1283 return PLDM_SUCCESS;
1284 }
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301285
1286 rc = pldm_msgbuf_extract(buf, synchrony_config);
1287 if (rc) {
1288 return rc;
Dung Cao1bf8c872022-11-29 05:32:58 +07001289 }
1290
Dung Cao1bf8c872022-11-29 05:32:58 +07001291 if (*synchrony_config > PLDM_MESSAGE_TYPE_ASYNCHRONOUS_WITH_HEARTBEAT) {
1292 return PLDM_ERROR_INVALID_DATA;
1293 }
1294
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301295 pldm_msgbuf_extract(buf, &synchrony_config_support->byte);
Dung Cao1bf8c872022-11-29 05:32:58 +07001296
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301297 rc = pldm_msgbuf_extract(buf, number_event_class_returned);
1298 if (rc) {
1299 return rc;
Dung Cao1bf8c872022-11-29 05:32:58 +07001300 }
1301
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301302 if (*number_event_class_returned == 0) {
1303 return pldm_msgbuf_destroy(buf);
1304 }
1305
1306 if (event_class_count < *number_event_class_returned) {
1307 return PLDM_ERROR_INVALID_LENGTH;
1308 }
1309
1310 for (i = 0; i < *number_event_class_returned; i++) {
1311 pldm_msgbuf_extract(buf, &event_class[i]);
1312 }
1313
1314 return pldm_msgbuf_destroy_consumed(buf);
Dung Cao1bf8c872022-11-29 05:32:58 +07001315}
1316
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301317LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301318int decode_sensor_event_data(const uint8_t *event_data,
1319 size_t event_data_length, uint16_t *sensor_id,
1320 uint8_t *sensor_event_class_type,
1321 size_t *event_class_data_offset)
1322{
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301323 struct pldm_msgbuf _buf;
1324 struct pldm_msgbuf *buf = &_buf;
1325 int rc;
1326
1327 rc = pldm_msgbuf_init(buf, PLDM_SENSOR_EVENT_DATA_MIN_LENGTH,
1328 event_data, event_data_length);
1329 if (rc) {
1330 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301331 }
1332
1333 size_t event_class_data_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301334 event_data_length - PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301335
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301336 pldm_msgbuf_extract(buf, sensor_id);
1337 rc = pldm_msgbuf_extract(buf, sensor_event_class_type);
1338 if (rc) {
1339 return rc;
1340 }
1341
1342 if (*sensor_event_class_type == PLDM_SENSOR_OP_STATE) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301343 if (event_class_data_length !=
1344 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
1345 return PLDM_ERROR_INVALID_LENGTH;
1346 }
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301347 } else if (*sensor_event_class_type == PLDM_STATE_SENSOR_STATE) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301348 if (event_class_data_length !=
1349 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
1350 return PLDM_ERROR_INVALID_LENGTH;
1351 }
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301352 } else if (*sensor_event_class_type == PLDM_NUMERIC_SENSOR_STATE) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301353 if (event_class_data_length <
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301354 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
Andrew Jeffery9c766792022-08-10 23:12:49 +09301355 event_class_data_length >
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301356 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301357 return PLDM_ERROR_INVALID_LENGTH;
1358 }
1359 } else {
1360 return PLDM_ERROR_INVALID_DATA;
1361 }
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301362
Andrew Jeffery9c766792022-08-10 23:12:49 +09301363 *event_class_data_offset =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301364 sizeof(*sensor_id) + sizeof(*sensor_event_class_type);
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301365
1366 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301367}
1368
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301369LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301370int decode_sensor_op_data(const uint8_t *sensor_data, size_t sensor_data_length,
1371 uint8_t *present_op_state, uint8_t *previous_op_state)
1372{
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301373 struct pldm_msgbuf _buf;
1374 struct pldm_msgbuf *buf = &_buf;
1375 int rc;
1376
1377 if (present_op_state == NULL || previous_op_state == NULL) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301378 return PLDM_ERROR_INVALID_DATA;
1379 }
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301380
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301381 rc = pldm_msgbuf_init(buf,
1382 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH,
1383 sensor_data, sensor_data_length);
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301384 if (rc) {
1385 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301386 }
1387
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301388 pldm_msgbuf_extract(buf, present_op_state);
1389 pldm_msgbuf_extract(buf, previous_op_state);
1390
1391 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301392}
1393
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301394LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301395int decode_state_sensor_data(const uint8_t *sensor_data,
1396 size_t sensor_data_length, uint8_t *sensor_offset,
1397 uint8_t *event_state,
1398 uint8_t *previous_event_state)
1399{
Andrew Jeffery422790b2023-04-13 15:03:47 +09301400 struct pldm_msgbuf _buf;
1401 struct pldm_msgbuf *buf = &_buf;
1402 int rc;
1403
1404 if (sensor_offset == NULL || event_state == NULL ||
1405 previous_event_state == NULL) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301406 return PLDM_ERROR_INVALID_DATA;
1407 }
Andrew Jeffery422790b2023-04-13 15:03:47 +09301408
1409 rc = pldm_msgbuf_init(buf,
1410 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH,
1411 sensor_data, sensor_data_length);
1412 if (rc) {
1413 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301414 }
1415
Andrew Jeffery422790b2023-04-13 15:03:47 +09301416 pldm_msgbuf_extract(buf, sensor_offset);
1417 pldm_msgbuf_extract(buf, event_state);
1418 pldm_msgbuf_extract(buf, previous_event_state);
1419
1420 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301421}
1422
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301423LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301424int decode_numeric_sensor_data(const uint8_t *sensor_data,
1425 size_t sensor_data_length, uint8_t *event_state,
1426 uint8_t *previous_event_state,
1427 uint8_t *sensor_data_size,
1428 uint32_t *present_reading)
1429{
Andrew Jeffery155317e2023-04-13 18:36:51 +09301430 struct pldm_msgbuf _buf;
1431 struct pldm_msgbuf *buf = &_buf;
1432 int rc;
1433
1434 if (sensor_data_size == NULL || event_state == NULL ||
1435 previous_event_state == NULL || present_reading == NULL) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301436 return PLDM_ERROR_INVALID_DATA;
1437 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301438
1439 if (sensor_data_length >
1440 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301441 return PLDM_ERROR_INVALID_LENGTH;
1442 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301443
Andrew Jeffery155317e2023-04-13 18:36:51 +09301444 rc = pldm_msgbuf_init(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301445 buf, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH,
1446 sensor_data, sensor_data_length);
Andrew Jeffery155317e2023-04-13 18:36:51 +09301447 if (rc) {
1448 return rc;
1449 }
1450
1451 pldm_msgbuf_extract(buf, event_state);
1452 pldm_msgbuf_extract(buf, previous_event_state);
1453 rc = pldm_msgbuf_extract(buf, sensor_data_size);
1454 if (rc) {
1455 return rc;
1456 }
1457
1458 /*
1459 * The implementation below is bonkers, but it's because the function
1460 * prototype is bonkers. The `present_reading` argument should have been
1461 * a tagged union.
1462 */
Andrew Jeffery9c766792022-08-10 23:12:49 +09301463 switch (*sensor_data_size) {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301464 case PLDM_SENSOR_DATA_SIZE_UINT8: {
1465 uint8_t val;
1466 if (!pldm_msgbuf_extract(buf, &val)) {
1467 *present_reading = (uint32_t)val;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301468 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301469 break;
Andrew Jeffery155317e2023-04-13 18:36:51 +09301470 }
1471 case PLDM_SENSOR_DATA_SIZE_SINT8: {
1472 int8_t val;
1473 if (!pldm_msgbuf_extract(buf, &val)) {
1474 *present_reading = (uint32_t)(int32_t)val;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301475 }
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301476 break;
Andrew Jeffery155317e2023-04-13 18:36:51 +09301477 }
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301478 case PLDM_SENSOR_DATA_SIZE_UINT16: {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301479 uint16_t val;
1480 if (!pldm_msgbuf_extract(buf, &val)) {
1481 *present_reading = (uint32_t)val;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301482 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301483 break;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301484 }
1485 case PLDM_SENSOR_DATA_SIZE_SINT16: {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301486 int16_t val;
1487 if (!pldm_msgbuf_extract(buf, &val)) {
1488 *present_reading = (uint32_t)(int32_t)val;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301489 }
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301490 break;
1491 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301492 case PLDM_SENSOR_DATA_SIZE_UINT32: {
1493 uint32_t val;
1494 if (!pldm_msgbuf_extract(buf, &val)) {
1495 *present_reading = (uint32_t)val;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301496 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301497 break;
1498 }
1499 case PLDM_SENSOR_DATA_SIZE_SINT32: {
1500 int32_t val;
1501 if (!pldm_msgbuf_extract(buf, &val)) {
1502 *present_reading = (uint32_t)val;
1503 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301504 break;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301505 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301506 default:
1507 return PLDM_ERROR_INVALID_DATA;
1508 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301509
1510 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301511}
1512
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301513#define PLDM_NUMERIC_SENSOR_VALUE_PDR_MIN_SIZE 69
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301514LIBPLDM_ABI_STABLE
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301515int decode_numeric_sensor_pdr_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301516 const void *pdr_data, size_t pdr_data_length,
1517 struct pldm_numeric_sensor_value_pdr *pdr_value)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301518{
1519 struct pldm_msgbuf _buf;
1520 struct pldm_msgbuf *buf = &_buf;
1521 int rc;
1522
1523 rc = pldm_msgbuf_init(buf, PLDM_NUMERIC_SENSOR_VALUE_PDR_MIN_SIZE,
1524 pdr_data, pdr_data_length);
1525 if (rc) {
1526 return rc;
1527 }
1528
1529 rc = pldm_msgbuf_extract_value_pdr_hdr(buf, &pdr_value->hdr);
1530 if (rc) {
1531 return rc;
1532 }
1533
1534 rc = pldm_platform_pdr_hdr_validate(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301535 &pdr_value->hdr, PLDM_NUMERIC_SENSOR_VALUE_PDR_MIN_SIZE,
1536 pdr_data_length);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301537 if (rc) {
1538 return rc;
1539 }
1540
1541 pldm_msgbuf_extract(buf, &pdr_value->terminus_handle);
1542 pldm_msgbuf_extract(buf, &pdr_value->sensor_id);
1543 pldm_msgbuf_extract(buf, &pdr_value->entity_type);
1544 pldm_msgbuf_extract(buf, &pdr_value->entity_instance_num);
1545 pldm_msgbuf_extract(buf, &pdr_value->container_id);
1546 pldm_msgbuf_extract(buf, &pdr_value->sensor_init);
1547 pldm_msgbuf_extract(buf, &pdr_value->sensor_auxiliary_names_pdr);
1548 pldm_msgbuf_extract(buf, &pdr_value->base_unit);
1549 pldm_msgbuf_extract(buf, &pdr_value->unit_modifier);
1550 pldm_msgbuf_extract(buf, &pdr_value->rate_unit);
1551 pldm_msgbuf_extract(buf, &pdr_value->base_oem_unit_handle);
1552 pldm_msgbuf_extract(buf, &pdr_value->aux_unit);
1553 pldm_msgbuf_extract(buf, &pdr_value->aux_unit_modifier);
1554 pldm_msgbuf_extract(buf, &pdr_value->aux_rate_unit);
1555 pldm_msgbuf_extract(buf, &pdr_value->rel);
1556 pldm_msgbuf_extract(buf, &pdr_value->aux_oem_unit_handle);
1557 pldm_msgbuf_extract(buf, &pdr_value->is_linear);
1558
1559 rc = pldm_msgbuf_extract(buf, &pdr_value->sensor_data_size);
1560 if (rc) {
1561 return rc;
1562 }
1563 if (pdr_value->sensor_data_size > PLDM_SENSOR_DATA_SIZE_MAX) {
1564 return PLDM_ERROR_INVALID_DATA;
1565 }
1566
1567 pldm_msgbuf_extract(buf, &pdr_value->resolution);
1568 pldm_msgbuf_extract(buf, &pdr_value->offset);
1569 pldm_msgbuf_extract(buf, &pdr_value->accuracy);
1570 pldm_msgbuf_extract(buf, &pdr_value->plus_tolerance);
1571 pldm_msgbuf_extract(buf, &pdr_value->minus_tolerance);
1572 pldm_msgbuf_extract_sensor_data(buf, pdr_value->sensor_data_size,
1573 &pdr_value->hysteresis);
1574 pldm_msgbuf_extract(buf, &pdr_value->supported_thresholds.byte);
1575 pldm_msgbuf_extract(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301576 buf, &pdr_value->threshold_and_hysteresis_volatility.byte);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301577 pldm_msgbuf_extract(buf, &pdr_value->state_transition_interval);
1578 pldm_msgbuf_extract(buf, &pdr_value->update_interval);
1579 pldm_msgbuf_extract_sensor_data(buf, pdr_value->sensor_data_size,
1580 &pdr_value->max_readable);
1581 pldm_msgbuf_extract_sensor_data(buf, pdr_value->sensor_data_size,
1582 &pdr_value->min_readable);
1583
1584 rc = pldm_msgbuf_extract(buf, &pdr_value->range_field_format);
1585 if (rc) {
1586 return rc;
1587 }
1588 if (pdr_value->range_field_format > PLDM_RANGE_FIELD_FORMAT_MAX) {
1589 return PLDM_ERROR_INVALID_DATA;
1590 }
1591
1592 pldm_msgbuf_extract(buf, &pdr_value->range_field_support.byte);
1593 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301594 buf, pdr_value->range_field_format, &pdr_value->nominal_value);
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_max);
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->normal_min);
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_high);
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->warning_low);
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_high);
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->critical_low);
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_high);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301609 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301610 buf, pdr_value->range_field_format, &pdr_value->fatal_low);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301611
1612 return pldm_msgbuf_destroy(buf);
1613}
1614
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301615LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301616int encode_get_numeric_effecter_value_req(uint8_t instance_id,
1617 uint16_t effecter_id,
1618 struct pldm_msg *msg)
1619{
1620 if (msg == NULL) {
1621 return PLDM_ERROR_INVALID_DATA;
1622 }
1623
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301624 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301625 header.msg_type = PLDM_REQUEST;
1626 header.instance = instance_id;
1627 header.pldm_type = PLDM_PLATFORM;
1628 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1629
1630 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1631 if (rc != PLDM_SUCCESS) {
1632 return rc;
1633 }
1634
1635 struct pldm_get_numeric_effecter_value_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301636 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301637 request->effecter_id = htole16(effecter_id);
1638
1639 return PLDM_SUCCESS;
1640}
1641
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301642LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301643int encode_get_numeric_effecter_value_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301644 uint8_t instance_id, uint8_t completion_code,
1645 uint8_t effecter_data_size, uint8_t effecter_oper_state,
1646 const uint8_t *pending_value, const uint8_t *present_value,
1647 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301648{
1649 if (msg == NULL || pending_value == NULL || present_value == NULL) {
1650 return PLDM_ERROR_INVALID_DATA;
1651 }
1652
1653 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1654 return PLDM_ERROR_INVALID_DATA;
1655 }
1656
1657 if (effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1658 return PLDM_ERROR_INVALID_DATA;
1659 }
1660
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301661 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301662 header.msg_type = PLDM_RESPONSE;
1663 header.instance = instance_id;
1664 header.pldm_type = PLDM_PLATFORM;
1665 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1666
1667 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1668 if (rc != PLDM_SUCCESS) {
1669 return rc;
1670 }
1671
1672 struct pldm_get_numeric_effecter_value_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301673 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301674
1675 response->completion_code = completion_code;
1676 response->effecter_data_size = effecter_data_size;
1677 response->effecter_oper_state = effecter_oper_state;
1678
1679 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1680 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1681 if (payload_length !=
1682 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1683 return PLDM_ERROR_INVALID_LENGTH;
1684 }
1685 response->pending_and_present_values[0] = *pending_value;
1686 response->pending_and_present_values[1] = *present_value;
1687
1688 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1689 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1690 if (payload_length !=
1691 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1692 return PLDM_ERROR_INVALID_LENGTH;
1693 }
1694 uint16_t val_pending = *(uint16_t *)pending_value;
1695 val_pending = htole16(val_pending);
1696 memcpy(response->pending_and_present_values, &val_pending,
1697 sizeof(uint16_t));
1698 uint16_t val_present = *(uint16_t *)present_value;
1699 val_present = htole16(val_present);
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301700 memcpy((response->pending_and_present_values +
1701 sizeof(uint16_t)),
1702 &val_present, sizeof(uint16_t));
Andrew Jeffery9c766792022-08-10 23:12:49 +09301703
1704 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1705 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1706 if (payload_length !=
1707 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1708 return PLDM_ERROR_INVALID_LENGTH;
1709 }
1710 uint32_t val_pending = *(uint32_t *)pending_value;
1711 val_pending = htole32(val_pending);
1712 memcpy(response->pending_and_present_values, &val_pending,
1713 sizeof(uint32_t));
1714 uint32_t val_present = *(uint32_t *)present_value;
1715 val_present = htole32(val_present);
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301716 memcpy((response->pending_and_present_values +
1717 sizeof(uint32_t)),
1718 &val_present, sizeof(uint32_t));
Andrew Jeffery9c766792022-08-10 23:12:49 +09301719 }
1720 return PLDM_SUCCESS;
1721}
1722
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301723LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301724int decode_get_numeric_effecter_value_req(const struct pldm_msg *msg,
1725 size_t payload_length,
1726 uint16_t *effecter_id)
1727{
Andrew Jefferydd265822023-04-13 22:42:44 +09301728 struct pldm_msgbuf _buf;
1729 struct pldm_msgbuf *buf = &_buf;
1730 int rc;
1731
Andrew Jeffery9c766792022-08-10 23:12:49 +09301732 if (msg == NULL || effecter_id == NULL) {
1733 return PLDM_ERROR_INVALID_DATA;
1734 }
1735
Andrew Jefferydd265822023-04-13 22:42:44 +09301736 rc = pldm_msgbuf_init(buf, PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES,
1737 msg->payload, payload_length);
1738 if (rc) {
1739 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301740 }
1741
Andrew Jefferydd265822023-04-13 22:42:44 +09301742 pldm_msgbuf_extract(buf, effecter_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301743
Andrew Jefferydd265822023-04-13 22:42:44 +09301744 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301745}
1746
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301747LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301748int decode_get_numeric_effecter_value_resp(const struct pldm_msg *msg,
1749 size_t payload_length,
1750 uint8_t *completion_code,
1751 uint8_t *effecter_data_size,
1752 uint8_t *effecter_oper_state,
1753 uint8_t *pending_value,
1754 uint8_t *present_value)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301755{
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301756 struct pldm_msgbuf _buf;
1757 struct pldm_msgbuf *buf = &_buf;
1758 int rc;
1759
Andrew Jeffery9c766792022-08-10 23:12:49 +09301760 if (msg == NULL || effecter_data_size == NULL ||
1761 effecter_oper_state == NULL || pending_value == NULL ||
1762 present_value == NULL) {
1763 return PLDM_ERROR_INVALID_DATA;
1764 }
1765
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301766 rc = pldm_msgbuf_init(buf,
1767 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES,
1768 msg->payload, payload_length);
1769 if (rc) {
1770 return rc;
1771 }
1772
1773 rc = pldm_msgbuf_extract(buf, completion_code);
1774 if (rc) {
1775 return rc;
1776 }
1777
Andrew Jeffery9c766792022-08-10 23:12:49 +09301778 if (PLDM_SUCCESS != *completion_code) {
1779 return PLDM_SUCCESS;
1780 }
1781
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301782 rc = pldm_msgbuf_extract(buf, effecter_data_size);
1783 if (rc) {
1784 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301785 }
1786
Andrew Jeffery9c766792022-08-10 23:12:49 +09301787 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1788 return PLDM_ERROR_INVALID_DATA;
1789 }
1790
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301791 rc = pldm_msgbuf_extract(buf, effecter_oper_state);
1792 if (rc) {
1793 return rc;
1794 }
1795
Andrew Jeffery9c766792022-08-10 23:12:49 +09301796 if (*effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1797 return PLDM_ERROR_INVALID_DATA;
1798 }
1799
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301800 pldm_msgbuf_extract_effecter_value(buf, *effecter_data_size,
1801 pending_value);
1802 pldm_msgbuf_extract_effecter_value(buf, *effecter_data_size,
1803 present_value);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301804
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301805 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301806}
1807
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301808LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301809int encode_pldm_pdr_repository_chg_event_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301810 uint8_t event_data_format, uint8_t number_of_change_records,
1811 const uint8_t *event_data_operations,
1812 const uint8_t *numbers_of_change_entries,
1813 const uint32_t *const *change_entries,
1814 struct pldm_pdr_repository_chg_event_data *event_data,
1815 size_t *actual_change_records_size, size_t max_change_records_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301816{
1817 if (event_data_operations == NULL ||
1818 numbers_of_change_entries == NULL || change_entries == NULL) {
1819 return PLDM_ERROR_INVALID_DATA;
1820 }
1821
1822 size_t expected_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301823 sizeof(event_data_format) + sizeof(number_of_change_records);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301824
1825 expected_size +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301826 sizeof(*event_data_operations) * number_of_change_records;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301827 expected_size +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301828 sizeof(*numbers_of_change_entries) * number_of_change_records;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301829
1830 for (uint8_t i = 0; i < number_of_change_records; ++i) {
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301831 expected_size += sizeof(*change_entries[0]) *
1832 numbers_of_change_entries[i];
Andrew Jeffery9c766792022-08-10 23:12:49 +09301833 }
1834
1835 *actual_change_records_size = expected_size;
1836
1837 if (event_data == NULL) {
1838 return PLDM_SUCCESS;
1839 }
1840
1841 if (max_change_records_size < expected_size) {
1842 return PLDM_ERROR_INVALID_LENGTH;
1843 }
1844
1845 event_data->event_data_format = event_data_format;
1846 event_data->number_of_change_records = number_of_change_records;
1847
1848 struct pldm_pdr_repository_change_record_data *record_data =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301849 (struct pldm_pdr_repository_change_record_data *)
1850 event_data->change_records;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301851
1852 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1853 record_data->event_data_operation = event_data_operations[i];
1854 record_data->number_of_change_entries =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301855 numbers_of_change_entries[i];
Andrew Jeffery9c766792022-08-10 23:12:49 +09301856
1857 for (uint8_t j = 0; j < record_data->number_of_change_entries;
1858 ++j) {
1859 record_data->change_entry[j] =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301860 htole32(change_entries[i][j]);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301861 }
1862
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301863 record_data =
1864 (struct pldm_pdr_repository_change_record_data
1865 *)(record_data->change_entry +
1866 record_data->number_of_change_entries);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301867 }
1868
1869 return PLDM_SUCCESS;
1870}
1871
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301872LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301873int decode_pldm_pdr_repository_chg_event_data(const uint8_t *event_data,
1874 size_t event_data_size,
1875 uint8_t *event_data_format,
1876 uint8_t *number_of_change_records,
1877 size_t *change_record_data_offset)
1878{
Andrew Jeffery2fe70122023-04-13 23:21:31 +09301879 struct pldm_msgbuf _buf;
1880 struct pldm_msgbuf *buf = &_buf;
1881 int rc;
1882
1883 if (event_data_format == NULL || number_of_change_records == NULL ||
Andrew Jeffery9c766792022-08-10 23:12:49 +09301884 change_record_data_offset == NULL) {
1885 return PLDM_ERROR_INVALID_DATA;
1886 }
Andrew Jeffery2fe70122023-04-13 23:21:31 +09301887
1888 rc = pldm_msgbuf_init(buf, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH,
1889 event_data, event_data_size);
1890 if (rc) {
1891 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301892 }
1893
Andrew Jeffery2fe70122023-04-13 23:21:31 +09301894 pldm_msgbuf_extract(buf, event_data_format);
1895 pldm_msgbuf_extract(buf, number_of_change_records);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301896
Andrew Jeffery9c766792022-08-10 23:12:49 +09301897 *change_record_data_offset =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301898 sizeof(*event_data_format) + sizeof(*number_of_change_records);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301899
Andrew Jeffery2fe70122023-04-13 23:21:31 +09301900 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301901}
1902
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301903LIBPLDM_ABI_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07001904int decode_pldm_message_poll_event_data(const uint8_t *event_data,
1905 size_t event_data_length,
1906 uint8_t *format_version,
1907 uint16_t *event_id,
1908 uint32_t *data_transfer_handle)
1909{
1910 struct pldm_msgbuf _buf;
1911 struct pldm_msgbuf *buf = &_buf;
1912 int rc;
1913
1914 if (event_data == NULL || format_version == NULL || event_id == NULL ||
1915 data_transfer_handle == NULL) {
1916 return PLDM_ERROR_INVALID_DATA;
1917 }
1918
1919 rc = pldm_msgbuf_init(buf, PLDM_MSG_POLL_EVENT_LENGTH, event_data,
1920 event_data_length);
1921 if (rc) {
1922 return rc;
1923 }
1924
1925 pldm_msgbuf_extract(buf, format_version);
1926 rc = pldm_msgbuf_extract(buf, event_id);
1927 if (rc) {
1928 return rc;
1929 }
1930
1931 if (*event_id == 0x0000 || *event_id == 0xffff) {
1932 return PLDM_ERROR_INVALID_DATA;
1933 }
1934
1935 pldm_msgbuf_extract(buf, data_transfer_handle);
1936
1937 return pldm_msgbuf_destroy_consumed(buf);
1938}
1939
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301940LIBPLDM_ABI_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07001941int encode_pldm_message_poll_event_data(uint8_t format_version,
1942 uint16_t event_id,
1943 uint32_t data_transfer_handle,
1944 uint8_t *event_data,
1945 size_t event_data_length)
1946{
1947 struct pldm_msgbuf _buf;
1948 struct pldm_msgbuf *buf = &_buf;
1949 int rc;
1950
1951 if (event_data == NULL) {
1952 return PLDM_ERROR_INVALID_DATA;
1953 }
1954
1955 if (event_id == 0x0000 || event_id == 0xffff) {
1956 return PLDM_ERROR_INVALID_DATA;
1957 }
1958
1959 rc = pldm_msgbuf_init(buf, PLDM_MSG_POLL_EVENT_LENGTH, event_data,
1960 event_data_length);
1961 if (rc) {
1962 return rc;
1963 }
1964 pldm_msgbuf_insert(buf, format_version);
1965 pldm_msgbuf_insert(buf, event_id);
1966 pldm_msgbuf_insert(buf, data_transfer_handle);
1967
1968 return pldm_msgbuf_destroy(buf);
1969}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301970
1971LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301972int decode_pldm_pdr_repository_change_record_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301973 const uint8_t *change_record_data, size_t change_record_data_size,
1974 uint8_t *event_data_operation, uint8_t *number_of_change_entries,
1975 size_t *change_entry_data_offset)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301976{
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09301977 struct pldm_msgbuf _buf;
1978 struct pldm_msgbuf *buf = &_buf;
1979 int rc;
1980
1981 if (event_data_operation == NULL || number_of_change_entries == NULL ||
Andrew Jeffery9c766792022-08-10 23:12:49 +09301982 change_entry_data_offset == NULL) {
1983 return PLDM_ERROR_INVALID_DATA;
1984 }
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09301985
1986 rc = pldm_msgbuf_init(buf, PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH,
1987 change_record_data, change_record_data_size);
1988 if (rc) {
1989 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301990 }
1991
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09301992 pldm_msgbuf_extract(buf, event_data_operation);
1993 pldm_msgbuf_extract(buf, number_of_change_entries);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301994
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301995 *change_entry_data_offset = sizeof(*event_data_operation) +
1996 sizeof(*number_of_change_entries);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301997
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09301998 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301999}
2000
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302001LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302002int encode_get_sensor_reading_req(uint8_t instance_id, uint16_t sensor_id,
2003 uint8_t rearm_event_state,
2004 struct pldm_msg *msg)
2005{
2006 if (msg == NULL) {
2007 return PLDM_ERROR_INVALID_DATA;
2008 }
2009
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302010 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302011 header.msg_type = PLDM_REQUEST;
2012 header.instance = instance_id;
2013 header.pldm_type = PLDM_PLATFORM;
2014 header.command = PLDM_GET_SENSOR_READING;
2015
2016 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2017 if (rc != PLDM_SUCCESS) {
2018 return rc;
2019 }
2020
2021 struct pldm_get_sensor_reading_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302022 (struct pldm_get_sensor_reading_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302023
2024 request->sensor_id = htole16(sensor_id);
2025 request->rearm_event_state = rearm_event_state;
2026
2027 return PLDM_SUCCESS;
2028}
2029
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302030LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302031int decode_get_sensor_reading_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302032 const struct pldm_msg *msg, size_t payload_length,
2033 uint8_t *completion_code, uint8_t *sensor_data_size,
2034 uint8_t *sensor_operational_state, uint8_t *sensor_event_message_enable,
2035 uint8_t *present_state, uint8_t *previous_state, uint8_t *event_state,
2036 uint8_t *present_reading)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302037{
Andrew Jeffery840b1402023-04-13 23:54:44 +09302038 struct pldm_msgbuf _buf;
2039 struct pldm_msgbuf *buf = &_buf;
2040 int rc;
2041
Andrew Jeffery9c766792022-08-10 23:12:49 +09302042 if (msg == NULL || completion_code == NULL ||
2043 sensor_data_size == NULL || sensor_operational_state == NULL ||
2044 sensor_event_message_enable == NULL || present_state == NULL ||
2045 previous_state == NULL || event_state == NULL ||
2046 present_reading == NULL) {
2047 return PLDM_ERROR_INVALID_DATA;
2048 }
2049
Andrew Jeffery840b1402023-04-13 23:54:44 +09302050 rc = pldm_msgbuf_init(buf, PLDM_GET_SENSOR_READING_MIN_RESP_BYTES,
2051 msg->payload, payload_length);
2052 if (rc) {
2053 return rc;
2054 }
2055
2056 rc = pldm_msgbuf_extract(buf, completion_code);
2057 if (rc) {
2058 return rc;
2059 }
2060
Andrew Jeffery9c766792022-08-10 23:12:49 +09302061 if (PLDM_SUCCESS != *completion_code) {
2062 return PLDM_SUCCESS;
2063 }
2064
Andrew Jeffery840b1402023-04-13 23:54:44 +09302065 rc = pldm_msgbuf_extract(buf, sensor_data_size);
2066 if (rc) {
2067 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302068 }
2069
Andrew Jeffery840b1402023-04-13 23:54:44 +09302070 if (*sensor_data_size > PLDM_SENSOR_DATA_SIZE_SINT32) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09302071 return PLDM_ERROR_INVALID_DATA;
2072 }
2073
Andrew Jeffery840b1402023-04-13 23:54:44 +09302074 pldm_msgbuf_extract(buf, sensor_operational_state);
2075 pldm_msgbuf_extract(buf, sensor_event_message_enable);
2076 pldm_msgbuf_extract(buf, present_state);
2077 pldm_msgbuf_extract(buf, previous_state);
2078 pldm_msgbuf_extract(buf, event_state);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302079
Andrew Jeffery840b1402023-04-13 23:54:44 +09302080 pldm_msgbuf_extract_sensor_value(buf, *sensor_data_size,
2081 present_reading);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302082
Andrew Jeffery840b1402023-04-13 23:54:44 +09302083 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302084}
2085
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302086LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302087int encode_get_sensor_reading_resp(uint8_t instance_id, uint8_t completion_code,
2088 uint8_t sensor_data_size,
2089 uint8_t sensor_operational_state,
2090 uint8_t sensor_event_message_enable,
2091 uint8_t present_state,
2092 uint8_t previous_state, uint8_t event_state,
2093 const uint8_t *present_reading,
2094 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302095{
2096 if (msg == NULL || present_reading == NULL) {
2097 return PLDM_ERROR_INVALID_DATA;
2098 }
2099
2100 if (sensor_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
2101 return PLDM_ERROR_INVALID_DATA;
2102 }
2103
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302104 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302105 header.msg_type = PLDM_RESPONSE;
2106 header.instance = instance_id;
2107 header.pldm_type = PLDM_PLATFORM;
2108 header.command = PLDM_GET_SENSOR_READING;
2109
2110 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2111 if (rc != PLDM_SUCCESS) {
2112 return rc;
2113 }
2114
2115 struct pldm_get_sensor_reading_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302116 (struct pldm_get_sensor_reading_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302117
2118 response->completion_code = completion_code;
2119 response->sensor_data_size = sensor_data_size;
2120 response->sensor_operational_state = sensor_operational_state;
2121 response->sensor_event_message_enable = sensor_event_message_enable;
2122 response->present_state = present_state;
2123 response->previous_state = previous_state;
2124 response->event_state = event_state;
2125
2126 if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
2127 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
2128 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
2129 return PLDM_ERROR_INVALID_LENGTH;
2130 }
2131 response->present_reading[0] = *present_reading;
2132
2133 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
2134 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
2135 if (payload_length !=
2136 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
2137 return PLDM_ERROR_INVALID_LENGTH;
2138 }
2139 uint16_t val = *(uint16_t *)present_reading;
2140 val = htole16(val);
2141 memcpy(response->present_reading, &val, 2);
2142
2143 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
2144 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
2145 if (payload_length !=
2146 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
2147 return PLDM_ERROR_INVALID_LENGTH;
2148 }
2149 uint32_t val = *(uint32_t *)present_reading;
2150 val = htole32(val);
2151 memcpy(response->present_reading, &val, 4);
2152 }
2153
2154 return PLDM_SUCCESS;
2155}
2156
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302157LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302158int decode_get_sensor_reading_req(const struct pldm_msg *msg,
2159 size_t payload_length, uint16_t *sensor_id,
2160 uint8_t *rearm_event_state)
2161{
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302162 struct pldm_msgbuf _buf;
2163 struct pldm_msgbuf *buf = &_buf;
2164 int rc;
2165
Andrew Jeffery9c766792022-08-10 23:12:49 +09302166 if (msg == NULL || sensor_id == NULL || rearm_event_state == NULL) {
2167 return PLDM_ERROR_INVALID_DATA;
2168 }
2169
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302170 rc = pldm_msgbuf_init(buf, PLDM_GET_SENSOR_READING_REQ_BYTES,
2171 msg->payload, payload_length);
2172 if (rc) {
2173 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302174 }
2175
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302176 pldm_msgbuf_extract(buf, sensor_id);
2177 pldm_msgbuf_extract(buf, rearm_event_state);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302178
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302179 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302180}
2181
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302182LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302183int encode_set_event_receiver_req(uint8_t instance_id,
2184 uint8_t event_message_global_enable,
2185 uint8_t transport_protocol_type,
2186 uint8_t event_receiver_address_info,
2187 uint16_t heartbeat_timer,
2188 struct pldm_msg *msg)
2189{
2190 if (msg == NULL) {
2191 return PLDM_ERROR_INVALID_DATA;
2192 }
2193
2194 if (transport_protocol_type != PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP) {
2195 return PLDM_ERROR_INVALID_DATA;
2196 }
2197
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302198 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302199 header.msg_type = PLDM_REQUEST;
2200 header.instance = instance_id;
2201 header.pldm_type = PLDM_PLATFORM;
2202 header.command = PLDM_SET_EVENT_RECEIVER;
2203
2204 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2205 if (rc != PLDM_SUCCESS) {
2206 return rc;
2207 }
2208
2209 struct pldm_set_event_receiver_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302210 (struct pldm_set_event_receiver_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302211 request->event_message_global_enable = event_message_global_enable;
2212
2213 request->transport_protocol_type = transport_protocol_type;
2214 request->event_receiver_address_info = event_receiver_address_info;
2215
2216 if (event_message_global_enable ==
2217 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) {
2218 if (heartbeat_timer == 0) {
2219 return PLDM_ERROR_INVALID_DATA;
2220 }
2221 request->heartbeat_timer = htole16(heartbeat_timer);
2222 }
2223
2224 return PLDM_SUCCESS;
2225}
2226
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302227LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302228int decode_set_event_receiver_resp(const struct pldm_msg *msg,
2229 size_t payload_length,
2230 uint8_t *completion_code)
2231{
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302232 struct pldm_msgbuf _buf;
2233 struct pldm_msgbuf *buf = &_buf;
2234 int rc;
2235
Andrew Jeffery9c766792022-08-10 23:12:49 +09302236 if (msg == NULL || completion_code == NULL) {
2237 return PLDM_ERROR_INVALID_DATA;
2238 }
2239
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302240 rc = pldm_msgbuf_init(buf, PLDM_SET_EVENT_RECEIVER_RESP_BYTES,
2241 msg->payload, payload_length);
2242 if (rc) {
2243 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302244 }
2245
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302246 pldm_msgbuf_extract(buf, completion_code);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302247
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302248 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302249}
2250
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302251LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302252int decode_set_event_receiver_req(const struct pldm_msg *msg,
2253 size_t payload_length,
2254 uint8_t *event_message_global_enable,
2255 uint8_t *transport_protocol_type,
2256 uint8_t *event_receiver_address_info,
2257 uint16_t *heartbeat_timer)
2258
2259{
Andrew Jeffery9667f582023-04-14 00:39:21 +09302260 struct pldm_msgbuf _buf;
2261 struct pldm_msgbuf *buf = &_buf;
2262 int rc;
2263
Andrew Jeffery9c766792022-08-10 23:12:49 +09302264 if (msg == NULL || event_message_global_enable == NULL ||
2265 transport_protocol_type == NULL ||
2266 event_receiver_address_info == NULL || heartbeat_timer == NULL) {
2267 return PLDM_ERROR_INVALID_DATA;
2268 }
2269
Andrew Jeffery9667f582023-04-14 00:39:21 +09302270 rc = pldm_msgbuf_init(buf, PLDM_SET_EVENT_RECEIVER_REQ_BYTES,
2271 msg->payload, payload_length);
2272 if (rc) {
2273 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302274 }
2275
Andrew Jeffery9667f582023-04-14 00:39:21 +09302276 pldm_msgbuf_extract(buf, event_message_global_enable);
2277 pldm_msgbuf_extract(buf, transport_protocol_type);
2278 pldm_msgbuf_extract(buf, event_receiver_address_info);
2279 pldm_msgbuf_extract(buf, heartbeat_timer);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302280
Andrew Jeffery9667f582023-04-14 00:39:21 +09302281 rc = pldm_msgbuf_destroy(buf);
2282 if (rc) {
2283 return rc;
2284 }
Andrew Jeffery6ef2aa92023-04-14 00:21:27 +09302285
Andrew Jeffery9c766792022-08-10 23:12:49 +09302286 if ((*event_message_global_enable ==
2287 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) &&
2288 (*heartbeat_timer == 0)) {
2289 return PLDM_ERROR_INVALID_DATA;
2290 }
2291
Andrew Jeffery9c766792022-08-10 23:12:49 +09302292 return PLDM_SUCCESS;
2293}
2294
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302295LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302296int encode_set_event_receiver_resp(uint8_t instance_id, uint8_t completion_code,
2297 struct pldm_msg *msg)
2298
2299{
2300 if (msg == NULL) {
2301 return PLDM_ERROR_INVALID_DATA;
2302 }
2303
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302304 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302305 header.instance = instance_id;
2306 header.msg_type = PLDM_RESPONSE;
2307 header.pldm_type = PLDM_PLATFORM;
2308 header.command = PLDM_SET_EVENT_RECEIVER;
2309
2310 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2311 if (rc != PLDM_SUCCESS) {
2312 return rc;
2313 }
2314
2315 msg->payload[0] = completion_code;
2316
2317 return PLDM_SUCCESS;
2318}
Thu Nguyen159a98b2022-11-02 10:00:10 +07002319
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302320LIBPLDM_ABI_STABLE
Thu Nguyen159a98b2022-11-02 10:00:10 +07002321int encode_poll_for_platform_event_message_req(uint8_t instance_id,
2322 uint8_t format_version,
2323 uint8_t transfer_operation_flag,
2324 uint32_t data_transfer_handle,
2325 uint16_t event_id_to_acknowledge,
2326 struct pldm_msg *msg,
2327 size_t payload_length)
2328{
2329 struct pldm_msgbuf _buf;
2330 struct pldm_msgbuf *buf = &_buf;
2331 int rc;
2332
2333 if (msg == NULL) {
2334 return PLDM_ERROR_INVALID_DATA;
2335 }
2336
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302337 struct pldm_header_info header = { 0 };
Thu Nguyen159a98b2022-11-02 10:00:10 +07002338 header.msg_type = PLDM_REQUEST;
2339 header.instance = instance_id;
2340 header.pldm_type = PLDM_PLATFORM;
2341 header.command = PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE;
2342
2343 rc = pack_pldm_header(&header, &(msg->hdr));
2344 if (rc != PLDM_SUCCESS) {
2345 return rc;
2346 }
2347
2348 rc = pldm_msgbuf_init(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302349 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
2350 msg->payload, payload_length);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002351 if (rc) {
2352 return rc;
2353 }
2354
2355 pldm_msgbuf_insert(buf, format_version);
2356 pldm_msgbuf_insert(buf, transfer_operation_flag);
2357 pldm_msgbuf_insert(buf, data_transfer_handle);
2358 pldm_msgbuf_insert(buf, event_id_to_acknowledge);
2359
2360 return pldm_msgbuf_destroy(buf);
2361}
2362
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302363LIBPLDM_ABI_STABLE
Thu Nguyen159a98b2022-11-02 10:00:10 +07002364int decode_poll_for_platform_event_message_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302365 const struct pldm_msg *msg, size_t payload_length,
2366 uint8_t *completion_code, uint8_t *tid, uint16_t *event_id,
2367 uint32_t *next_data_transfer_handle, uint8_t *transfer_flag,
2368 uint8_t *event_class, uint32_t *event_data_size, void **event_data,
2369 uint32_t *event_data_integrity_checksum)
Thu Nguyen159a98b2022-11-02 10:00:10 +07002370{
2371 struct pldm_msgbuf _buf;
2372 struct pldm_msgbuf *buf = &_buf;
2373 int rc;
2374
2375 if (msg == NULL || completion_code == NULL || tid == NULL ||
2376 event_id == NULL || next_data_transfer_handle == NULL ||
2377 transfer_flag == NULL || event_class == NULL ||
2378 event_data_size == NULL || event_data == NULL ||
2379 event_data_integrity_checksum == NULL) {
2380 return PLDM_ERROR_INVALID_DATA;
2381 }
2382
2383 rc = pldm_msgbuf_init(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302384 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
2385 msg->payload, payload_length);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002386 if (rc) {
2387 return rc;
2388 }
2389
2390 rc = pldm_msgbuf_extract(buf, completion_code);
2391 if (rc) {
2392 return rc;
2393 }
2394 if (PLDM_SUCCESS != *completion_code) {
2395 return *completion_code;
2396 }
2397
2398 pldm_msgbuf_extract(buf, tid);
2399 rc = pldm_msgbuf_extract(buf, event_id);
2400 if (rc) {
2401 return rc;
2402 }
2403 if ((*event_id == 0) || (*event_id == 0xffff)) {
2404 return PLDM_SUCCESS;
2405 }
2406
2407 pldm_msgbuf_extract(buf, next_data_transfer_handle);
2408 rc = pldm_msgbuf_extract(buf, transfer_flag);
2409 if (rc) {
2410 return rc;
2411 }
2412
2413 pldm_msgbuf_extract(buf, event_class);
2414 rc = pldm_msgbuf_extract(buf, event_data_size);
2415 if (rc) {
2416 return rc;
2417 }
2418 if (*event_data_size > payload_length) {
2419 return PLDM_ERROR_INVALID_DATA;
2420 }
2421
2422 if (*event_data_size > 0) {
2423 pldm_msgbuf_span_required(buf, *event_data_size, event_data);
2424 }
2425
2426 if (*transfer_flag == PLDM_END ||
2427 *transfer_flag == PLDM_START_AND_END) {
2428 pldm_msgbuf_extract(buf, event_data_integrity_checksum);
2429 }
2430
2431 return pldm_msgbuf_destroy_consumed(buf);
2432}