blob: e355792a6d1a4e3cc4c3bffe24d671f0c3d58676 [file] [log] [blame]
Patrick Williams691668f2023-11-01 08:19:10 -05001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Tal Yacobif490a382024-05-31 09:57:36 +03002#include "dsp/base.h"
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303#include "msgbuf.h"
Andrew Jefferyb0c1d202023-11-07 22:08:44 +10304#include "msgbuf/platform.h"
5
6#include <libpldm/base.h>
7#include <libpldm/platform.h>
8#include <libpldm/pldm_types.h>
9
Andrew Jeffery9c766792022-08-10 23:12:49 +093010#include <endian.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +053011#include <stdint.h>
Thu Nguyen159a98b2022-11-02 10:00:10 +070012#include <stdlib.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +093013#include <string.h>
Thu Nguyendacfa352024-06-22 09:53:15 +000014#include <uchar.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +093015
Andrew Jeffery7992eb82023-04-06 16:13:53 +093016static int pldm_platform_pdr_hdr_validate(struct pldm_value_pdr_hdr *ctx,
17 size_t lower, size_t upper)
18{
19 if (ctx->length + sizeof(*ctx) < lower) {
20 return PLDM_ERROR_INVALID_LENGTH;
21 }
22
23 if (ctx->length > upper) {
24 return PLDM_ERROR_INVALID_LENGTH;
25 }
26
27 return PLDM_SUCCESS;
28}
Andrew Jeffery9c766792022-08-10 23:12:49 +093029
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093030LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +093031int encode_state_effecter_pdr(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093032 struct pldm_state_effecter_pdr *const effecter,
33 const size_t allocation_size,
34 const struct state_effecter_possible_states *const possible_states,
35 const size_t possible_states_size, size_t *const actual_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +093036{
37 // Encode possible states
38
39 size_t calculated_possible_states_size = 0;
40
41 {
42 char *states_ptr = (char *)possible_states;
43 char *const begin_states_ptr = states_ptr;
44
45 for (int i = 0; i < effecter->composite_effecter_count; ++i) {
46 struct state_effecter_possible_states *states =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093047 (struct state_effecter_possible_states *)
48 states_ptr;
Andrew Jeffery9c766792022-08-10 23:12:49 +093049
50 HTOLE16(states->state_set_id);
51
52 states_ptr +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093053 (sizeof(*states) - sizeof(states->states) +
54 states->possible_states_size);
Andrew Jeffery9c766792022-08-10 23:12:49 +093055 }
56
57 calculated_possible_states_size = states_ptr - begin_states_ptr;
58 }
59
60 // Check lengths
61
62 if (possible_states_size != calculated_possible_states_size) {
63 *actual_size = 0;
64 return PLDM_ERROR;
65 }
66
67 *actual_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093068 (sizeof(struct pldm_state_effecter_pdr) + possible_states_size -
69 sizeof(effecter->possible_states));
Andrew Jeffery9c766792022-08-10 23:12:49 +093070
71 if (allocation_size < *actual_size) {
72 *actual_size = 0;
73 return PLDM_ERROR_INVALID_LENGTH;
74 }
75
76 // Encode rest of PDR
77
78 effecter->hdr.version = 1;
79 effecter->hdr.type = PLDM_STATE_EFFECTER_PDR;
80 effecter->hdr.length = *actual_size - sizeof(struct pldm_pdr_hdr);
81
82 memcpy(effecter->possible_states, possible_states,
83 possible_states_size);
84
85 // Convert effecter PDR body
86 HTOLE16(effecter->terminus_handle);
87 HTOLE16(effecter->effecter_id);
88 HTOLE16(effecter->entity_type);
89 HTOLE16(effecter->entity_instance);
90 HTOLE16(effecter->container_id);
91 HTOLE16(effecter->effecter_semantic_id);
92
93 // Convert header
94 HTOLE32(effecter->hdr.record_handle);
95 HTOLE16(effecter->hdr.record_change_num);
96 HTOLE16(effecter->hdr.length);
97
98 return PLDM_SUCCESS;
99}
100
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930101LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930102int encode_state_sensor_pdr(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930103 struct pldm_state_sensor_pdr *const sensor,
104 const size_t allocation_size,
105 const struct state_sensor_possible_states *const possible_states,
106 const size_t possible_states_size, size_t *const actual_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930107{
108 // Encode possible states
109
110 size_t calculated_possible_states_size = 0;
111
112 {
Andrew Jefferyfbe61d72023-04-05 20:28:23 +0930113 char *states_ptr = (char *)possible_states;
114 char *const begin_states_ptr = states_ptr;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930115
116 for (int i = 0; i < sensor->composite_sensor_count; ++i) {
117 struct state_sensor_possible_states *states =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930118 (struct state_sensor_possible_states *)
119 states_ptr;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930120
121 HTOLE16(states->state_set_id);
122
123 states_ptr +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930124 (sizeof(*states) - sizeof(states->states) +
125 states->possible_states_size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930126 }
127
128 calculated_possible_states_size = states_ptr - begin_states_ptr;
129 }
130
131 // Check lengths
132
133 if (possible_states_size != calculated_possible_states_size) {
134 *actual_size = 0;
135 return PLDM_ERROR;
136 }
137
138 *actual_size = (sizeof(struct pldm_state_sensor_pdr) +
139 possible_states_size - sizeof(sensor->possible_states));
140
141 if (allocation_size < *actual_size) {
142 *actual_size = 0;
143 return PLDM_ERROR_INVALID_LENGTH;
144 }
145
146 // Encode rest of PDR
147
148 sensor->hdr.version = 1;
149 sensor->hdr.type = PLDM_STATE_SENSOR_PDR;
150 sensor->hdr.length = *actual_size - sizeof(struct pldm_pdr_hdr);
151
152 memcpy(sensor->possible_states, possible_states, possible_states_size);
153
154 // Convert sensor PDR body
155 HTOLE16(sensor->terminus_handle);
156 HTOLE16(sensor->sensor_id);
157 HTOLE16(sensor->entity_type);
158 HTOLE16(sensor->entity_instance);
159 HTOLE16(sensor->container_id);
160
161 // Convert header
162 HTOLE32(sensor->hdr.record_handle);
163 HTOLE16(sensor->hdr.record_change_num);
164 HTOLE16(sensor->hdr.length);
165
166 return PLDM_SUCCESS;
167}
168
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930169LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930170int encode_set_state_effecter_states_resp(uint8_t instance_id,
171 uint8_t completion_code,
172 struct pldm_msg *msg)
173{
174 if (msg == NULL) {
175 return PLDM_ERROR_INVALID_DATA;
176 }
177
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930178 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930179 header.msg_type = PLDM_RESPONSE;
180 header.instance = instance_id;
181 header.pldm_type = PLDM_PLATFORM;
182 header.command = PLDM_SET_STATE_EFFECTER_STATES;
183
184 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
185 if (rc != PLDM_SUCCESS) {
186 return rc;
187 }
188
189 msg->payload[0] = completion_code;
190
191 return PLDM_SUCCESS;
192}
193
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930194LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930195int encode_set_state_effecter_states_req(uint8_t instance_id,
196 uint16_t effecter_id,
197 uint8_t comp_effecter_count,
198 set_effecter_state_field *field,
199 struct pldm_msg *msg)
200{
201 if (msg == NULL) {
202 return PLDM_ERROR_INVALID_DATA;
203 }
204
205 if (comp_effecter_count < 0x1 || comp_effecter_count > 0x8 ||
206 field == NULL) {
207 return PLDM_ERROR_INVALID_DATA;
208 }
209
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930210 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930211 header.msg_type = PLDM_REQUEST;
212 header.instance = instance_id;
213 header.pldm_type = PLDM_PLATFORM;
214 header.command = PLDM_SET_STATE_EFFECTER_STATES;
215
216 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
217 if (rc != PLDM_SUCCESS) {
218 return rc;
219 }
220
221 struct pldm_set_state_effecter_states_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930222 (struct pldm_set_state_effecter_states_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930223 effecter_id = htole16(effecter_id);
224 request->effecter_id = effecter_id;
225 request->comp_effecter_count = comp_effecter_count;
226 memcpy(request->field, field,
227 (sizeof(set_effecter_state_field) * comp_effecter_count));
228
229 return PLDM_SUCCESS;
230}
231
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930232LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930233int decode_set_state_effecter_states_resp(const struct pldm_msg *msg,
234 size_t payload_length,
235 uint8_t *completion_code)
236{
237 if (msg == NULL || completion_code == NULL) {
238 return PLDM_ERROR_INVALID_DATA;
239 }
240
241 *completion_code = msg->payload[0];
242 if (PLDM_SUCCESS != *completion_code) {
243 return PLDM_SUCCESS;
244 }
245
246 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES) {
247 return PLDM_ERROR_INVALID_LENGTH;
248 }
249
250 return PLDM_SUCCESS;
251}
252
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930253#define PLDM_SET_STATE_EFFECTER_STATES_MIN_SIZE 3
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930254LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930255int decode_set_state_effecter_states_req(const struct pldm_msg *msg,
256 size_t payload_length,
257 uint16_t *effecter_id,
258 uint8_t *comp_effecter_count,
259 set_effecter_state_field *field)
260{
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930261 struct pldm_msgbuf _buf;
262 struct pldm_msgbuf *buf = &_buf;
263 int rc;
264 int i;
265
Andrew Jeffery9c766792022-08-10 23:12:49 +0930266 if (msg == NULL || effecter_id == NULL || comp_effecter_count == NULL ||
267 field == NULL) {
268 return PLDM_ERROR_INVALID_DATA;
269 }
270
271 if (payload_length > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) {
272 return PLDM_ERROR_INVALID_LENGTH;
273 }
274
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930275 rc = pldm_msgbuf_init_cc(buf, PLDM_SET_STATE_EFFECTER_STATES_MIN_SIZE,
276 msg->payload, payload_length);
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930277 if (rc) {
278 return rc;
279 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930280
Andrew Jeffery66c77232024-04-24 11:42:02 +0930281 pldm_msgbuf_extract_p(buf, effecter_id);
282 pldm_msgbuf_extract_p(buf, comp_effecter_count);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930283
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930284 if (*comp_effecter_count > 8) {
285 return PLDM_ERROR_INVALID_DATA;
286 }
287
288 for (i = 0; i < *comp_effecter_count; i++) {
Andrew Jeffery66c77232024-04-24 11:42:02 +0930289 pldm_msgbuf_extract(buf, field[i].set_request);
290 pldm_msgbuf_extract(buf, field[i].effecter_state);
Andrew Jeffery5d773b32023-04-04 10:52:57 +0930291 }
292
293 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930294}
295
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930296LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930297int decode_get_pdr_req(const struct pldm_msg *msg, size_t payload_length,
298 uint32_t *record_hndl, uint32_t *data_transfer_hndl,
299 uint8_t *transfer_op_flag, uint16_t *request_cnt,
300 uint16_t *record_chg_num)
301{
Andrew Jeffery891781e2023-04-04 11:04:18 +0930302 struct pldm_msgbuf _buf;
303 struct pldm_msgbuf *buf = &_buf;
304 int rc;
305
Andrew Jeffery9c766792022-08-10 23:12:49 +0930306 if (msg == NULL || record_hndl == NULL || data_transfer_hndl == NULL ||
307 transfer_op_flag == NULL || request_cnt == NULL ||
308 record_chg_num == NULL) {
309 return PLDM_ERROR_INVALID_DATA;
310 }
Andrew Jeffery891781e2023-04-04 11:04:18 +0930311
Andrew Jeffery9c766792022-08-10 23:12:49 +0930312 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
313 return PLDM_ERROR_INVALID_LENGTH;
314 }
315
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930316 rc = pldm_msgbuf_init_cc(buf, PLDM_GET_PDR_REQ_BYTES, msg->payload,
317 payload_length);
Andrew Jeffery891781e2023-04-04 11:04:18 +0930318 if (rc) {
319 return rc;
320 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930321
Andrew Jeffery66c77232024-04-24 11:42:02 +0930322 pldm_msgbuf_extract_p(buf, record_hndl);
323 pldm_msgbuf_extract_p(buf, data_transfer_hndl);
324 pldm_msgbuf_extract_p(buf, transfer_op_flag);
325 pldm_msgbuf_extract_p(buf, request_cnt);
326 pldm_msgbuf_extract_p(buf, record_chg_num);
Andrew Jeffery891781e2023-04-04 11:04:18 +0930327
328 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930329}
330
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930331LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930332int encode_get_pdr_resp(uint8_t instance_id, uint8_t completion_code,
333 uint32_t next_record_hndl,
334 uint32_t next_data_transfer_hndl, uint8_t transfer_flag,
335 uint16_t resp_cnt, const uint8_t *record_data,
336 uint8_t transfer_crc, struct pldm_msg *msg)
337{
338 if (msg == NULL) {
339 return PLDM_ERROR_INVALID_DATA;
340 }
341
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930342 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930343 header.msg_type = PLDM_RESPONSE;
344 header.instance = instance_id;
345 header.pldm_type = PLDM_PLATFORM;
346 header.command = PLDM_GET_PDR;
347
348 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
349 if (rc != PLDM_SUCCESS) {
350 return rc;
351 }
352
353 struct pldm_get_pdr_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930354 (struct pldm_get_pdr_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930355 response->completion_code = completion_code;
356
357 if (response->completion_code == PLDM_SUCCESS) {
358 response->next_record_handle = htole32(next_record_hndl);
359 response->next_data_transfer_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930360 htole32(next_data_transfer_hndl);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930361 response->transfer_flag = transfer_flag;
362 response->response_count = htole16(resp_cnt);
363 if (record_data != NULL && resp_cnt > 0) {
364 memcpy(response->record_data, record_data, resp_cnt);
365 }
366 if (transfer_flag == PLDM_END) {
367 uint8_t *dst = msg->payload;
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930368 dst += (sizeof(struct pldm_get_pdr_resp) - 1) +
369 resp_cnt;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930370 *dst = transfer_crc;
371 }
372 }
373
374 return PLDM_SUCCESS;
375}
376
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930377LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930378int encode_get_pdr_repository_info_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930379 uint8_t instance_id, uint8_t completion_code, uint8_t repository_state,
380 const uint8_t *update_time, const uint8_t *oem_update_time,
381 uint32_t record_count, uint32_t repository_size,
382 uint32_t largest_record_size, uint8_t data_transfer_handle_timeout,
383 struct pldm_msg *msg)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930384{
385 if (msg == NULL) {
386 return PLDM_ERROR_INVALID_DATA;
387 }
388
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930389 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930390 header.msg_type = PLDM_RESPONSE;
391 header.instance = instance_id;
392 header.pldm_type = PLDM_PLATFORM;
393 header.command = PLDM_GET_PDR_REPOSITORY_INFO;
394
395 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
396 if (rc != PLDM_SUCCESS) {
397 return rc;
398 }
399
400 struct pldm_pdr_repository_info_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930401 (struct pldm_pdr_repository_info_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930402 response->completion_code = completion_code;
403
404 if (response->completion_code == PLDM_SUCCESS) {
405 response->repository_state = repository_state;
406 if (update_time != NULL) {
407 memcpy(response->update_time, update_time,
408 PLDM_TIMESTAMP104_SIZE);
409 }
410 if (oem_update_time != NULL) {
411 memcpy(response->oem_update_time, oem_update_time,
412 PLDM_TIMESTAMP104_SIZE);
413 }
414 response->record_count = htole32(record_count);
415 response->repository_size = htole32(repository_size);
416 response->largest_record_size = htole32(largest_record_size);
417 response->data_transfer_handle_timeout =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930418 data_transfer_handle_timeout;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930419 }
420
421 return PLDM_SUCCESS;
422}
423
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000424LIBPLDM_ABI_DEPRECATED
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800425int decode_get_pdr_repository_info_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930426 const struct pldm_msg *msg, size_t payload_length,
427 uint8_t *completion_code, uint8_t *repository_state,
428 uint8_t *update_time, uint8_t *oem_update_time, uint32_t *record_count,
429 uint32_t *repository_size, uint32_t *largest_record_size,
430 uint8_t *data_transfer_handle_timeout)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800431{
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930432 struct pldm_msgbuf _buf;
433 struct pldm_msgbuf *buf = &_buf;
434 int rc;
435
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800436 if (msg == NULL || completion_code == NULL ||
437 repository_state == NULL || update_time == NULL ||
438 oem_update_time == NULL || record_count == NULL ||
439 repository_size == NULL || largest_record_size == NULL ||
440 data_transfer_handle_timeout == NULL) {
441 return PLDM_ERROR_INVALID_DATA;
442 }
443
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930444 rc = pldm_msgbuf_init_cc(buf, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES,
445 msg->payload, payload_length);
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930446 if (rc) {
447 return rc;
448 }
449
Andrew Jeffery66c77232024-04-24 11:42:02 +0930450 pldm_msgbuf_extract_p(buf, completion_code);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800451 if (PLDM_SUCCESS != *completion_code) {
452 return PLDM_SUCCESS;
453 }
454
Andrew Jeffery66c77232024-04-24 11:42:02 +0930455 pldm_msgbuf_extract_p(buf, repository_state);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800456 if (*repository_state > PLDM_FAILED) {
457 return PLDM_ERROR_INVALID_DATA;
458 }
459
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000460 /* NOTE: Memory safety */
461 rc = pldm_msgbuf_extract_array(buf, PLDM_TIMESTAMP104_SIZE, update_time,
462 PLDM_TIMESTAMP104_SIZE);
463 if (rc) {
464 return rc;
465 }
466
467 /* NOTE: Memory safety */
468 rc = pldm_msgbuf_extract_array(buf, PLDM_TIMESTAMP104_SIZE,
469 oem_update_time, PLDM_TIMESTAMP104_SIZE);
470 if (rc) {
471 return rc;
472 }
473
Andrew Jeffery66c77232024-04-24 11:42:02 +0930474 pldm_msgbuf_extract_p(buf, record_count);
475 pldm_msgbuf_extract_p(buf, repository_size);
476 pldm_msgbuf_extract_p(buf, largest_record_size);
477 pldm_msgbuf_extract_p(buf, data_transfer_handle_timeout);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800478
Andrew Jeffery5c651b82023-04-04 11:37:56 +0930479 return pldm_msgbuf_destroy(buf);
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800480}
481
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000482LIBPLDM_ABI_TESTING
483int decode_get_pdr_repository_info_resp_safe(
484 const struct pldm_msg *msg, size_t payload_length,
485 struct pldm_pdr_repository_info_resp *resp)
486{
487 struct pldm_msgbuf _buf;
488 struct pldm_msgbuf *buf = &_buf;
489 int rc;
490
491 if (msg == NULL || resp == NULL) {
492 return -EINVAL;
493 }
494
495 rc = pldm_msg_has_error(msg, payload_length);
496 if (rc) {
497 resp->completion_code = rc;
498 return 0;
499 }
500
501 rc = pldm_msgbuf_init_errno(buf,
502 PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES,
503 msg->payload, payload_length);
504 if (rc) {
505 return rc;
506 }
507
508 rc = pldm_msgbuf_extract(buf, resp->completion_code);
509 if (rc) {
510 return rc;
511 }
512
513 pldm_msgbuf_extract(buf, resp->repository_state);
514
515 rc = pldm_msgbuf_extract_array(buf, sizeof(resp->update_time),
516 resp->update_time,
517 sizeof(resp->update_time));
518 if (rc) {
519 return rc;
520 }
521
522 rc = pldm_msgbuf_extract_array(buf, sizeof(resp->oem_update_time),
523 resp->oem_update_time,
524 sizeof(resp->oem_update_time));
525 if (rc) {
526 return rc;
527 }
528
529 pldm_msgbuf_extract(buf, resp->record_count);
530 pldm_msgbuf_extract(buf, resp->repository_size);
531 pldm_msgbuf_extract(buf, resp->largest_record_size);
532 pldm_msgbuf_extract(buf, resp->data_transfer_handle_timeout);
533
534 return pldm_msgbuf_destroy_consumed(buf);
535}
536
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930537LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930538int encode_get_pdr_req(uint8_t instance_id, uint32_t record_hndl,
539 uint32_t data_transfer_hndl, uint8_t transfer_op_flag,
540 uint16_t request_cnt, uint16_t record_chg_num,
541 struct pldm_msg *msg, size_t payload_length)
542{
543 if (msg == NULL) {
544 return PLDM_ERROR_INVALID_DATA;
545 }
546
547 if (payload_length != PLDM_GET_PDR_REQ_BYTES) {
548 return PLDM_ERROR_INVALID_LENGTH;
549 }
550
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930551 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930552 header.msg_type = PLDM_REQUEST;
553 header.instance = instance_id;
554 header.pldm_type = PLDM_PLATFORM;
555 header.command = PLDM_GET_PDR;
556
557 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
558 if (rc != PLDM_SUCCESS) {
559 return rc;
560 }
561
562 struct pldm_get_pdr_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930563 (struct pldm_get_pdr_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930564 request->record_handle = htole32(record_hndl);
565 request->data_transfer_handle = htole32(data_transfer_hndl);
566 request->transfer_op_flag = transfer_op_flag;
567 request->request_count = htole16(request_cnt);
568 request->record_change_number = htole16(record_chg_num);
569
570 return PLDM_SUCCESS;
571}
572
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000573LIBPLDM_ABI_DEPRECATED
Andrew Jeffery9c766792022-08-10 23:12:49 +0930574int decode_get_pdr_resp(const struct pldm_msg *msg, size_t payload_length,
575 uint8_t *completion_code, uint32_t *next_record_hndl,
576 uint32_t *next_data_transfer_hndl,
577 uint8_t *transfer_flag, uint16_t *resp_cnt,
578 uint8_t *record_data, size_t record_data_length,
579 uint8_t *transfer_crc)
580{
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930581 struct pldm_msgbuf _buf;
582 struct pldm_msgbuf *buf = &_buf;
583 int rc;
584
Andrew Jeffery9c766792022-08-10 23:12:49 +0930585 if (msg == NULL || completion_code == NULL ||
586 next_record_hndl == NULL || next_data_transfer_hndl == NULL ||
587 transfer_flag == NULL || resp_cnt == NULL || transfer_crc == NULL) {
588 return PLDM_ERROR_INVALID_DATA;
589 }
590
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930591 rc = pldm_msgbuf_init_cc(buf, PLDM_GET_PDR_MIN_RESP_BYTES, msg->payload,
592 payload_length);
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930593 if (rc) {
594 return rc;
595 }
596
Andrew Jeffery66c77232024-04-24 11:42:02 +0930597 pldm_msgbuf_extract_p(buf, completion_code);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930598 if (PLDM_SUCCESS != *completion_code) {
599 return PLDM_SUCCESS;
600 }
601
Andrew Jeffery66c77232024-04-24 11:42:02 +0930602 pldm_msgbuf_extract_p(buf, next_record_hndl);
603 pldm_msgbuf_extract_p(buf, next_data_transfer_hndl);
604 pldm_msgbuf_extract_p(buf, transfer_flag);
605 rc = pldm_msgbuf_extract_p(buf, resp_cnt);
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930606 if (rc) {
607 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930608 }
609
610 if (*resp_cnt > 0 && record_data != NULL) {
611 if (record_data_length < *resp_cnt) {
612 return PLDM_ERROR_INVALID_LENGTH;
613 }
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000614 /* NOTE: Memory safety */
615 rc = pldm_msgbuf_extract_array(buf, *resp_cnt, record_data,
616 *resp_cnt);
617 if (rc) {
618 return rc;
619 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930620 }
621
622 if (*transfer_flag == PLDM_END) {
Andrew Jeffery66c77232024-04-24 11:42:02 +0930623 pldm_msgbuf_extract_p(buf, transfer_crc);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930624 }
625
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930626 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930627}
628
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000629LIBPLDM_ABI_TESTING
630int decode_get_pdr_resp_safe(const struct pldm_msg *msg, size_t payload_length,
631 struct pldm_get_pdr_resp *resp, size_t resp_len,
632 uint8_t *transfer_crc)
633{
634 struct pldm_msgbuf _buf;
635 struct pldm_msgbuf *buf = &_buf;
636 int rc;
637
638 if (msg == NULL || resp == NULL || transfer_crc == NULL) {
639 return -EINVAL;
640 }
641
642 rc = pldm_msg_has_error(msg, payload_length);
643 if (rc) {
644 resp->completion_code = rc;
645 return 0;
646 }
647
648 rc = pldm_msgbuf_init_errno(buf, PLDM_GET_PDR_MIN_RESP_BYTES,
649 msg->payload, payload_length);
650 if (rc) {
651 return rc;
652 }
653
654 pldm_msgbuf_extract(buf, resp->completion_code);
655 pldm_msgbuf_extract(buf, resp->next_record_handle);
656 pldm_msgbuf_extract(buf, resp->next_data_transfer_handle);
657
658 rc = pldm_msgbuf_extract(buf, resp->transfer_flag);
659 if (rc) {
660 return rc;
661 }
662
663 rc = pldm_msgbuf_extract(buf, resp->response_count);
664 if (rc) {
665 return rc;
666 }
667
668 rc = pldm_msgbuf_extract_array(
669 buf, resp->response_count, resp->record_data,
670 resp_len - (sizeof(*resp) - sizeof(resp->record_data)));
671 if (rc) {
672 return rc;
673 }
674
675 if (resp->transfer_flag == PLDM_END) {
676 pldm_msgbuf_extract_p(buf, transfer_crc);
677 }
678
679 return pldm_msgbuf_destroy_consumed(buf);
680}
681
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930682LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930683int decode_set_numeric_effecter_value_req(const struct pldm_msg *msg,
684 size_t payload_length,
685 uint16_t *effecter_id,
686 uint8_t *effecter_data_size,
Andrew Jeffery3884c442023-04-12 11:13:24 +0930687 uint8_t effecter_value[4])
Andrew Jeffery9c766792022-08-10 23:12:49 +0930688{
Andrew Jeffery3884c442023-04-12 11:13:24 +0930689 struct pldm_msgbuf _buf;
690 struct pldm_msgbuf *buf = &_buf;
691 int rc;
692
Andrew Jeffery9c766792022-08-10 23:12:49 +0930693 if (msg == NULL || effecter_id == NULL || effecter_data_size == NULL ||
694 effecter_value == NULL) {
695 return PLDM_ERROR_INVALID_DATA;
696 }
697
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930698 rc = pldm_msgbuf_init_cc(buf,
699 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES,
700 msg->payload, payload_length);
Andrew Jeffery3884c442023-04-12 11:13:24 +0930701 if (rc) {
702 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930703 }
704
Andrew Jeffery66c77232024-04-24 11:42:02 +0930705 pldm_msgbuf_extract_p(buf, effecter_id);
706 rc = pldm_msgbuf_extract_p(buf, effecter_data_size);
Andrew Jeffery3884c442023-04-12 11:13:24 +0930707 if (rc) {
708 return PLDM_ERROR_INVALID_DATA;
709 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930710
711 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
712 return PLDM_ERROR_INVALID_DATA;
713 }
714
Andrew Jeffery3884c442023-04-12 11:13:24 +0930715 pldm_msgbuf_extract_effecter_value(buf, *effecter_data_size,
716 effecter_value);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930717
Andrew Jeffery3884c442023-04-12 11:13:24 +0930718 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930719}
720
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930721LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930722int encode_set_numeric_effecter_value_resp(uint8_t instance_id,
723 uint8_t completion_code,
724 struct pldm_msg *msg,
725 size_t payload_length)
726{
727 if (msg == NULL) {
728 return PLDM_ERROR_INVALID_DATA;
729 }
730
731 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
732 return PLDM_ERROR_INVALID_LENGTH;
733 }
734
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930735 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930736 header.msg_type = PLDM_RESPONSE;
737 header.instance = instance_id;
738 header.pldm_type = PLDM_PLATFORM;
739 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
740
741 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
742 if (rc != PLDM_SUCCESS) {
743 return rc;
744 }
745
746 msg->payload[0] = completion_code;
747
748 return rc;
749}
750
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930751LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930752int encode_set_numeric_effecter_value_req(uint8_t instance_id,
753 uint16_t effecter_id,
754 uint8_t effecter_data_size,
755 const uint8_t *effecter_value,
756 struct pldm_msg *msg,
757 size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930758{
759 if (msg == NULL || effecter_value == NULL) {
760 return PLDM_ERROR_INVALID_DATA;
761 }
762
763 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
764 return PLDM_ERROR_INVALID_DATA;
765 }
766
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930767 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930768 header.msg_type = PLDM_REQUEST;
769 header.instance = instance_id;
770 header.pldm_type = PLDM_PLATFORM;
771 header.command = PLDM_SET_NUMERIC_EFFECTER_VALUE;
772
773 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
774 if (rc != PLDM_SUCCESS) {
775 return rc;
776 }
777
778 struct pldm_set_numeric_effecter_value_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930779 (struct pldm_set_numeric_effecter_value_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930780 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
781 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
782 if (payload_length !=
783 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES) {
784 return PLDM_ERROR_INVALID_LENGTH;
785 }
786 request->effecter_value[0] = *effecter_value;
787 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
788 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
789 if (payload_length !=
790 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1) {
791 return PLDM_ERROR_INVALID_LENGTH;
792 }
793
794 uint16_t val = *(uint16_t *)(effecter_value);
795 val = htole16(val);
796 memcpy(request->effecter_value, &val, sizeof(uint16_t));
797
798 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
799 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
800 if (payload_length !=
801 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3) {
802 return PLDM_ERROR_INVALID_LENGTH;
803 }
804
805 uint32_t val = *(uint32_t *)(effecter_value);
806 val = htole32(val);
807 memcpy(request->effecter_value, &val, sizeof(uint32_t));
808 }
809
810 request->effecter_id = htole16(effecter_id);
811 request->effecter_data_size = effecter_data_size;
812
813 return PLDM_SUCCESS;
814}
815
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930816LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930817int decode_set_numeric_effecter_value_resp(const struct pldm_msg *msg,
818 size_t payload_length,
819 uint8_t *completion_code)
820{
821 if (msg == NULL || completion_code == NULL) {
822 return PLDM_ERROR_INVALID_DATA;
823 }
824
825 if (payload_length != PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES) {
826 return PLDM_ERROR_INVALID_LENGTH;
827 }
828
829 *completion_code = msg->payload[0];
830
831 return PLDM_SUCCESS;
832}
833
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930834LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930835int encode_get_state_sensor_readings_resp(uint8_t instance_id,
836 uint8_t completion_code,
837 uint8_t comp_sensor_count,
838 get_sensor_state_field *field,
839 struct pldm_msg *msg)
840{
841 if (msg == NULL) {
842 return PLDM_ERROR_INVALID_DATA;
843 }
844
845 if (comp_sensor_count < 0x1 || comp_sensor_count > 0x8) {
846 return PLDM_ERROR_INVALID_DATA;
847 }
848
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930849 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930850 header.msg_type = PLDM_RESPONSE;
851 header.instance = instance_id;
852 header.pldm_type = PLDM_PLATFORM;
853 header.command = PLDM_GET_STATE_SENSOR_READINGS;
854
855 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
856 if (rc != PLDM_SUCCESS) {
857 return rc;
858 }
859
860 struct pldm_get_state_sensor_readings_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930861 (struct pldm_get_state_sensor_readings_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930862
863 response->completion_code = completion_code;
864 response->comp_sensor_count = comp_sensor_count;
865 memcpy(response->field, field,
866 (sizeof(get_sensor_state_field) * comp_sensor_count));
867
868 return PLDM_SUCCESS;
869}
870
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930871LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930872int encode_get_state_sensor_readings_req(uint8_t instance_id,
873 uint16_t sensor_id,
874 bitfield8_t sensor_rearm,
875 uint8_t reserved, struct pldm_msg *msg)
876{
877 if (msg == NULL) {
878 return PLDM_ERROR_INVALID_DATA;
879 }
880
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930881 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930882 header.msg_type = PLDM_REQUEST;
883 header.instance = instance_id;
884 header.pldm_type = PLDM_PLATFORM;
885 header.command = PLDM_GET_STATE_SENSOR_READINGS;
886
887 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
888 if (rc != PLDM_SUCCESS) {
889 return rc;
890 }
891
892 struct pldm_get_state_sensor_readings_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930893 (struct pldm_get_state_sensor_readings_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930894
895 request->sensor_id = htole16(sensor_id);
896 request->reserved = reserved;
897 request->sensor_rearm = sensor_rearm;
898
899 return PLDM_SUCCESS;
900}
901
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930902LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930903int decode_get_state_sensor_readings_resp(const struct pldm_msg *msg,
904 size_t payload_length,
905 uint8_t *completion_code,
906 uint8_t *comp_sensor_count,
907 get_sensor_state_field *field)
908{
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930909 struct pldm_msgbuf _buf;
910 struct pldm_msgbuf *buf = &_buf;
911 uint8_t i;
912 int rc;
913
Andrew Jeffery9c766792022-08-10 23:12:49 +0930914 if (msg == NULL || completion_code == NULL ||
915 comp_sensor_count == NULL || field == NULL) {
916 return PLDM_ERROR_INVALID_DATA;
917 }
918
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930919 rc = pldm_msgbuf_init_cc(buf,
920 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES,
921 msg->payload, payload_length);
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930922 if (rc) {
923 return rc;
924 }
925
Andrew Jeffery66c77232024-04-24 11:42:02 +0930926 rc = pldm_msgbuf_extract_p(buf, completion_code);
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930927 if (rc) {
928 return rc;
929 }
930
Andrew Jeffery9c766792022-08-10 23:12:49 +0930931 if (PLDM_SUCCESS != *completion_code) {
932 return PLDM_SUCCESS;
933 }
934
Andrew Jeffery66c77232024-04-24 11:42:02 +0930935 rc = pldm_msgbuf_extract_p(buf, comp_sensor_count);
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930936 if (rc) {
937 return rc;
938 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930939
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930940 if (*comp_sensor_count < 0x1 || *comp_sensor_count > 0x8) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930941 return PLDM_ERROR_INVALID_DATA;
942 }
943
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930944 for (i = 0; i < *comp_sensor_count; i++) {
Andrew Jeffery66c77232024-04-24 11:42:02 +0930945 pldm_msgbuf_extract(buf, field[i].sensor_op_state);
946 pldm_msgbuf_extract(buf, field[i].present_state);
947 pldm_msgbuf_extract(buf, field[i].previous_state);
948 pldm_msgbuf_extract(buf, field[i].event_state);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930949 }
950
Andrew Jefferyb0e22a72023-04-12 23:14:36 +0930951 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930952}
953
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930954LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930955int decode_get_state_sensor_readings_req(const struct pldm_msg *msg,
956 size_t payload_length,
957 uint16_t *sensor_id,
958 bitfield8_t *sensor_rearm,
959 uint8_t *reserved)
960{
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930961 struct pldm_msgbuf _buf;
962 struct pldm_msgbuf *buf = &_buf;
963 int rc;
964
Andrew Jeffery9c766792022-08-10 23:12:49 +0930965 if (msg == NULL || sensor_id == NULL || sensor_rearm == NULL) {
966 return PLDM_ERROR_INVALID_DATA;
967 }
968
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930969 rc = pldm_msgbuf_init_cc(buf, PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES,
970 msg->payload, payload_length);
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930971 if (rc) {
972 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930973 }
974
Andrew Jeffery66c77232024-04-24 11:42:02 +0930975 pldm_msgbuf_extract_p(buf, sensor_id);
976 pldm_msgbuf_extract(buf, sensor_rearm->byte);
977 pldm_msgbuf_extract_p(buf, reserved);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930978
Andrew Jefferyf75aca62023-04-13 11:27:07 +0930979 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930980}
981
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930982LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930983int encode_sensor_event_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930984 struct pldm_sensor_event_data *const event_data,
985 const size_t event_data_size, const uint16_t sensor_id,
986 const enum sensor_event_class_states sensor_event_class,
987 const uint8_t sensor_offset, const uint8_t event_state,
988 const uint8_t previous_event_state,
989 size_t *const actual_event_data_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930990{
991 *actual_event_data_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930992 (sizeof(*event_data) - sizeof(event_data->event_class) +
993 sizeof(struct pldm_sensor_event_state_sensor_state));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930994
995 if (!event_data) {
996 return PLDM_SUCCESS;
997 }
998
999 if (event_data_size < *actual_event_data_size) {
1000 *actual_event_data_size = 0;
1001 return PLDM_ERROR_INVALID_LENGTH;
1002 }
1003
1004 event_data->sensor_id = htole16(sensor_id);
1005 event_data->sensor_event_class_type = sensor_event_class;
1006
1007 struct pldm_sensor_event_state_sensor_state *const state_data =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301008 (struct pldm_sensor_event_state_sensor_state *)
1009 event_data->event_class;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301010
1011 state_data->sensor_offset = sensor_offset;
1012 state_data->event_state = event_state;
1013 state_data->previous_event_state = previous_event_state;
1014
1015 return PLDM_SUCCESS;
1016}
1017
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301018LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301019int decode_platform_event_message_req(const struct pldm_msg *msg,
1020 size_t payload_length,
1021 uint8_t *format_version, uint8_t *tid,
1022 uint8_t *event_class,
1023 size_t *event_data_offset)
1024{
Andrew Jefferydc48ce32023-04-13 12:01:42 +09301025 struct pldm_msgbuf _buf;
1026 struct pldm_msgbuf *buf = &_buf;
1027 int rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301028
1029 if (msg == NULL || format_version == NULL || tid == NULL ||
1030 event_class == NULL || event_data_offset == NULL) {
1031 return PLDM_ERROR_INVALID_DATA;
1032 }
1033
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301034 rc = pldm_msgbuf_init_cc(buf, PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES,
1035 msg->payload, payload_length);
Andrew Jefferydc48ce32023-04-13 12:01:42 +09301036 if (rc) {
1037 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301038 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301039
Andrew Jeffery66c77232024-04-24 11:42:02 +09301040 pldm_msgbuf_extract_p(buf, format_version);
1041 pldm_msgbuf_extract_p(buf, tid);
1042 pldm_msgbuf_extract_p(buf, event_class);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301043 *event_data_offset =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301044 sizeof(*format_version) + sizeof(*tid) + sizeof(*event_class);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301045
Andrew Jefferydc48ce32023-04-13 12:01:42 +09301046 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301047}
1048
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301049LIBPLDM_ABI_STABLE
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001050int decode_poll_for_platform_event_message_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301051 const struct pldm_msg *msg, size_t payload_length,
1052 uint8_t *format_version, uint8_t *transfer_operation_flag,
1053 uint32_t *data_transfer_handle, uint16_t *event_id_to_acknowledge)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001054{
1055 struct pldm_msgbuf _buf;
1056 struct pldm_msgbuf *buf = &_buf;
1057 int rc;
1058
1059 if (msg == NULL) {
1060 return PLDM_ERROR_INVALID_DATA;
1061 }
1062
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301063 rc = pldm_msgbuf_init_cc(buf,
1064 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES,
1065 msg->payload, payload_length);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001066 if (rc) {
1067 return rc;
1068 }
1069
Andrew Jeffery66c77232024-04-24 11:42:02 +09301070 pldm_msgbuf_extract_p(buf, format_version);
1071 rc = pldm_msgbuf_extract_p(buf, transfer_operation_flag);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001072 if (rc) {
1073 return rc;
1074 }
1075 if (*transfer_operation_flag > PLDM_ACKNOWLEDGEMENT_ONLY) {
1076 return PLDM_ERROR_INVALID_DATA;
1077 }
1078
Andrew Jeffery66c77232024-04-24 11:42:02 +09301079 pldm_msgbuf_extract_p(buf, data_transfer_handle);
1080 rc = pldm_msgbuf_extract_p(buf, event_id_to_acknowledge);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001081 if (rc) {
1082 return rc;
1083 }
1084
1085 if (!(((*transfer_operation_flag == PLDM_GET_NEXTPART) &&
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001086 (*event_id_to_acknowledge == 0xffff)) ||
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001087 ((*transfer_operation_flag == PLDM_GET_FIRSTPART) &&
1088 (*event_id_to_acknowledge == 0x000)) ||
1089 (*transfer_operation_flag == PLDM_ACKNOWLEDGEMENT_ONLY))) {
1090 return PLDM_ERROR_INVALID_DATA;
1091 }
1092
1093 return pldm_msgbuf_destroy(buf);
1094}
1095
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301096LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301097int encode_platform_event_message_resp(uint8_t instance_id,
1098 uint8_t completion_code,
1099 uint8_t platform_event_status,
1100 struct pldm_msg *msg)
1101{
1102 if (msg == NULL) {
1103 return PLDM_ERROR_INVALID_DATA;
1104 }
1105
1106 if (platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
1107 return PLDM_ERROR_INVALID_DATA;
1108 }
1109
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301110 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301111 header.msg_type = PLDM_RESPONSE;
1112 header.instance = instance_id;
1113 header.pldm_type = PLDM_PLATFORM;
1114 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
1115
1116 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1117 if (rc != PLDM_SUCCESS) {
1118 return rc;
1119 }
1120
1121 struct pldm_platform_event_message_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301122 (struct pldm_platform_event_message_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301123 response->completion_code = completion_code;
1124 response->platform_event_status = platform_event_status;
1125
1126 return PLDM_SUCCESS;
1127}
1128
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301129LIBPLDM_ABI_STABLE
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001130int encode_poll_for_platform_event_message_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301131 uint8_t instance_id, uint8_t completion_code, uint8_t tid,
1132 uint16_t event_id, uint32_t next_data_transfer_handle,
1133 uint8_t transfer_flag, uint8_t event_class, uint32_t event_data_size,
1134 uint8_t *event_data, uint32_t checksum, struct pldm_msg *msg,
1135 size_t payload_length)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001136{
1137 struct pldm_msgbuf _buf;
1138 struct pldm_msgbuf *buf = &_buf;
1139 int rc;
1140
1141 if (!msg) {
1142 return PLDM_ERROR_INVALID_DATA;
1143 }
1144
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301145 struct pldm_header_info header = { 0 };
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001146 header.msg_type = PLDM_RESPONSE;
1147 header.instance = instance_id;
1148 header.pldm_type = PLDM_PLATFORM;
1149 header.command = PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE;
1150
1151 rc = pack_pldm_header(&header, &(msg->hdr));
1152 if (rc != PLDM_SUCCESS) {
1153 return rc;
1154 }
1155
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301156 rc = pldm_msgbuf_init_cc(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301157 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
1158 msg->payload, payload_length);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001159 if (rc) {
1160 return rc;
1161 }
1162
1163 pldm_msgbuf_insert(buf, completion_code);
1164 pldm_msgbuf_insert(buf, tid);
1165 pldm_msgbuf_insert(buf, event_id);
1166
1167 if (event_id == 0xffff || event_id == 0x0000) {
1168 if (PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES !=
1169 payload_length) {
1170 return PLDM_ERROR_INVALID_LENGTH;
1171 }
1172 return pldm_msgbuf_destroy(buf);
1173 }
1174
1175 if ((event_data == NULL) && (event_data_size > 0)) {
1176 return PLDM_ERROR_INVALID_DATA;
1177 }
1178
1179 pldm_msgbuf_insert(buf, next_data_transfer_handle);
1180 pldm_msgbuf_insert(buf, transfer_flag);
1181 pldm_msgbuf_insert(buf, event_class);
1182 pldm_msgbuf_insert(buf, event_data_size);
1183
1184 if ((event_data_size > 0) && event_data) {
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001185 rc = pldm_msgbuf_insert_array(buf, event_data_size, event_data,
1186 event_data_size);
1187 if (rc) {
1188 return rc;
1189 }
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001190 }
1191
1192 if (transfer_flag == PLDM_END || transfer_flag == PLDM_START_AND_END) {
1193 pldm_msgbuf_insert(buf, checksum);
1194 }
1195
1196 return pldm_msgbuf_destroy(buf);
1197}
1198
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301199LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301200int encode_platform_event_message_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301201 uint8_t instance_id, uint8_t format_version, uint8_t tid,
1202 uint8_t event_class, const uint8_t *event_data,
1203 size_t event_data_length, struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301204
1205{
1206 if (format_version != 1) {
1207 return PLDM_ERROR_INVALID_DATA;
1208 }
1209
1210 if (msg == NULL || event_data == NULL) {
1211 return PLDM_ERROR_INVALID_DATA;
1212 }
1213
1214 if (event_data_length == 0) {
1215 return PLDM_ERROR_INVALID_DATA;
1216 }
1217
1218 if (payload_length !=
1219 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + event_data_length) {
1220 return PLDM_ERROR_INVALID_LENGTH;
1221 }
1222
1223 if (event_class > PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT &&
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001224 !(event_class >= 0xf0 && event_class <= 0xfe)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301225 return PLDM_ERROR_INVALID_DATA;
1226 }
1227
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301228 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301229 header.msg_type = PLDM_REQUEST;
1230 header.instance = instance_id;
1231 header.pldm_type = PLDM_PLATFORM;
1232 header.command = PLDM_PLATFORM_EVENT_MESSAGE;
1233
1234 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1235 if (rc != PLDM_SUCCESS) {
1236 return rc;
1237 }
1238
1239 struct pldm_platform_event_message_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301240 (struct pldm_platform_event_message_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301241 request->format_version = format_version;
1242 request->tid = tid;
1243 request->event_class = event_class;
1244 memcpy(request->event_data, event_data, event_data_length);
1245
1246 return PLDM_SUCCESS;
1247}
1248
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301249LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301250int decode_platform_event_message_resp(const struct pldm_msg *msg,
1251 size_t payload_length,
1252 uint8_t *completion_code,
1253 uint8_t *platform_event_status)
1254{
Andrew Jefferye5011772023-04-13 12:06:22 +09301255 struct pldm_msgbuf _buf;
1256 struct pldm_msgbuf *buf = &_buf;
1257 int rc;
1258
Andrew Jeffery9c766792022-08-10 23:12:49 +09301259 if (msg == NULL || completion_code == NULL ||
1260 platform_event_status == NULL) {
1261 return PLDM_ERROR_INVALID_DATA;
1262 }
1263
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301264 rc = pldm_msgbuf_init_cc(buf, PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES,
1265 msg->payload, payload_length);
Andrew Jefferye5011772023-04-13 12:06:22 +09301266 if (rc) {
1267 return rc;
1268 }
1269
Andrew Jeffery66c77232024-04-24 11:42:02 +09301270 rc = pldm_msgbuf_extract_p(buf, completion_code);
Andrew Jefferye5011772023-04-13 12:06:22 +09301271 if (rc) {
1272 return rc;
1273 }
1274
Andrew Jeffery9c766792022-08-10 23:12:49 +09301275 if (PLDM_SUCCESS != *completion_code) {
1276 return PLDM_SUCCESS;
1277 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301278
Andrew Jeffery66c77232024-04-24 11:42:02 +09301279 rc = pldm_msgbuf_extract_p(buf, platform_event_status);
Andrew Jefferye5011772023-04-13 12:06:22 +09301280 if (rc) {
1281 return rc;
1282 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301283
1284 if (*platform_event_status > PLDM_EVENT_LOGGING_REJECTED) {
1285 return PLDM_ERROR_INVALID_DATA;
1286 }
1287
Andrew Jefferye5011772023-04-13 12:06:22 +09301288 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301289}
1290
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301291LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301292int encode_event_message_buffer_size_req(uint8_t instance_id,
1293 uint16_t event_receiver_max_buffer_size,
1294 struct pldm_msg *msg)
Dung Caod6ae8982022-11-02 10:00:10 +07001295{
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301296 struct pldm_header_info header = { 0 };
Dung Caod6ae8982022-11-02 10:00:10 +07001297 header.msg_type = PLDM_REQUEST;
1298 header.instance = instance_id;
1299 header.pldm_type = PLDM_PLATFORM;
1300 header.command = PLDM_EVENT_MESSAGE_BUFFER_SIZE;
1301
1302 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1303 if (rc != PLDM_SUCCESS) {
1304 return rc;
1305 }
1306
1307 struct pldm_event_message_buffer_size_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301308 (struct pldm_event_message_buffer_size_req *)msg->payload;
Dung Caod6ae8982022-11-02 10:00:10 +07001309 request->event_receiver_max_buffer_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301310 event_receiver_max_buffer_size;
Dung Caod6ae8982022-11-02 10:00:10 +07001311
1312 return PLDM_SUCCESS;
1313}
1314
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301315LIBPLDM_ABI_STABLE
Dung Caod6ae8982022-11-02 10:00:10 +07001316int decode_event_message_buffer_size_resp(const struct pldm_msg *msg,
1317 size_t payload_length,
1318 uint8_t *completion_code,
1319 uint16_t *terminus_max_buffer_size)
1320{
Andrew Jeffery11126902023-04-13 12:12:10 +09301321 struct pldm_msgbuf _buf;
1322 struct pldm_msgbuf *buf = &_buf;
1323 int rc;
1324
Dung Caod6ae8982022-11-02 10:00:10 +07001325 if (msg == NULL || completion_code == NULL ||
1326 terminus_max_buffer_size == NULL) {
1327 return PLDM_ERROR_INVALID_DATA;
1328 }
1329
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301330 rc = pldm_msgbuf_init_cc(buf, PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES,
1331 msg->payload, payload_length);
Andrew Jeffery11126902023-04-13 12:12:10 +09301332 if (rc) {
1333 return rc;
1334 }
1335
Andrew Jeffery66c77232024-04-24 11:42:02 +09301336 rc = pldm_msgbuf_extract_p(buf, completion_code);
Andrew Jeffery11126902023-04-13 12:12:10 +09301337 if (rc) {
1338 return rc;
1339 }
1340
Dung Caod6ae8982022-11-02 10:00:10 +07001341 if (PLDM_SUCCESS != *completion_code) {
1342 return PLDM_SUCCESS;
1343 }
Dung Caod6ae8982022-11-02 10:00:10 +07001344
Andrew Jeffery66c77232024-04-24 11:42:02 +09301345 pldm_msgbuf_extract_p(buf, terminus_max_buffer_size);
Dung Caod6ae8982022-11-02 10:00:10 +07001346
Andrew Jeffery11126902023-04-13 12:12:10 +09301347 return pldm_msgbuf_destroy_consumed(buf);
Dung Caod6ae8982022-11-02 10:00:10 +07001348}
1349
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301350LIBPLDM_ABI_STABLE
Dung Cao1bf8c872022-11-29 05:32:58 +07001351int encode_event_message_supported_req(uint8_t instance_id,
1352 uint8_t format_version,
1353 struct pldm_msg *msg)
1354{
1355 if (format_version != 1) {
1356 return PLDM_ERROR_INVALID_DATA;
1357 }
1358
1359 if (msg == NULL) {
1360 return PLDM_ERROR_INVALID_DATA;
1361 }
1362
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301363 struct pldm_header_info header = { 0 };
Dung Cao1bf8c872022-11-29 05:32:58 +07001364 header.msg_type = PLDM_REQUEST;
1365 header.instance = instance_id;
1366 header.pldm_type = PLDM_PLATFORM;
1367 header.command = PLDM_EVENT_MESSAGE_SUPPORTED;
1368
1369 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1370 if (rc != PLDM_SUCCESS) {
1371 return rc;
1372 }
1373
1374 struct pldm_event_message_supported_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301375 (struct pldm_event_message_supported_req *)msg->payload;
Dung Cao1bf8c872022-11-29 05:32:58 +07001376 request->format_version = format_version;
1377
1378 return PLDM_SUCCESS;
1379}
1380
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301381LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301382int decode_event_message_supported_resp(const struct pldm_msg *msg,
1383 size_t payload_length,
1384 uint8_t *completion_code,
1385 uint8_t *synchrony_config,
1386 bitfield8_t *synchrony_config_support,
1387 uint8_t *number_event_class_returned,
1388 uint8_t *event_class,
1389 uint8_t event_class_count)
Dung Cao1bf8c872022-11-29 05:32:58 +07001390{
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301391 struct pldm_msgbuf _buf;
1392 struct pldm_msgbuf *buf = &_buf;
1393 int i;
1394 int rc;
1395
Dung Cao1bf8c872022-11-29 05:32:58 +07001396 if (msg == NULL || completion_code == NULL ||
1397 synchrony_config == NULL || synchrony_config_support == NULL ||
1398 number_event_class_returned == NULL || event_class == NULL) {
1399 return PLDM_ERROR_INVALID_DATA;
1400 }
1401
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301402 rc = pldm_msgbuf_init_cc(buf,
1403 PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES,
1404 msg->payload, payload_length);
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301405 if (rc) {
1406 return rc;
1407 }
1408
Andrew Jeffery66c77232024-04-24 11:42:02 +09301409 rc = pldm_msgbuf_extract_p(buf, completion_code);
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301410 if (rc) {
1411 return rc;
1412 }
1413
Dung Cao1bf8c872022-11-29 05:32:58 +07001414 if (PLDM_SUCCESS != *completion_code) {
1415 return PLDM_SUCCESS;
1416 }
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301417
Andrew Jeffery66c77232024-04-24 11:42:02 +09301418 rc = pldm_msgbuf_extract_p(buf, synchrony_config);
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301419 if (rc) {
1420 return rc;
Dung Cao1bf8c872022-11-29 05:32:58 +07001421 }
1422
Dung Cao1bf8c872022-11-29 05:32:58 +07001423 if (*synchrony_config > PLDM_MESSAGE_TYPE_ASYNCHRONOUS_WITH_HEARTBEAT) {
1424 return PLDM_ERROR_INVALID_DATA;
1425 }
1426
Andrew Jeffery66c77232024-04-24 11:42:02 +09301427 pldm_msgbuf_extract_p(buf, &synchrony_config_support->byte);
Dung Cao1bf8c872022-11-29 05:32:58 +07001428
Andrew Jeffery66c77232024-04-24 11:42:02 +09301429 rc = pldm_msgbuf_extract_p(buf, number_event_class_returned);
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301430 if (rc) {
1431 return rc;
Dung Cao1bf8c872022-11-29 05:32:58 +07001432 }
1433
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301434 if (*number_event_class_returned == 0) {
1435 return pldm_msgbuf_destroy(buf);
1436 }
1437
1438 if (event_class_count < *number_event_class_returned) {
1439 return PLDM_ERROR_INVALID_LENGTH;
1440 }
1441
1442 for (i = 0; i < *number_event_class_returned; i++) {
Andrew Jeffery66c77232024-04-24 11:42:02 +09301443 pldm_msgbuf_extract(buf, event_class[i]);
Andrew Jeffery2d00ff82023-04-13 12:34:27 +09301444 }
1445
1446 return pldm_msgbuf_destroy_consumed(buf);
Dung Cao1bf8c872022-11-29 05:32:58 +07001447}
1448
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301449LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301450int decode_sensor_event_data(const uint8_t *event_data,
1451 size_t event_data_length, uint16_t *sensor_id,
1452 uint8_t *sensor_event_class_type,
1453 size_t *event_class_data_offset)
1454{
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301455 struct pldm_msgbuf _buf;
1456 struct pldm_msgbuf *buf = &_buf;
1457 int rc;
1458
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301459 rc = pldm_msgbuf_init_cc(buf, PLDM_SENSOR_EVENT_DATA_MIN_LENGTH,
1460 event_data, event_data_length);
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301461 if (rc) {
1462 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301463 }
1464
1465 size_t event_class_data_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301466 event_data_length - PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301467
Andrew Jeffery66c77232024-04-24 11:42:02 +09301468 pldm_msgbuf_extract_p(buf, sensor_id);
1469 rc = pldm_msgbuf_extract_p(buf, sensor_event_class_type);
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301470 if (rc) {
1471 return rc;
1472 }
1473
1474 if (*sensor_event_class_type == PLDM_SENSOR_OP_STATE) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301475 if (event_class_data_length !=
1476 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH) {
1477 return PLDM_ERROR_INVALID_LENGTH;
1478 }
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301479 } else if (*sensor_event_class_type == PLDM_STATE_SENSOR_STATE) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301480 if (event_class_data_length !=
1481 PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH) {
1482 return PLDM_ERROR_INVALID_LENGTH;
1483 }
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301484 } else if (*sensor_event_class_type == PLDM_NUMERIC_SENSOR_STATE) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301485 if (event_class_data_length <
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301486 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH ||
Andrew Jeffery9c766792022-08-10 23:12:49 +09301487 event_class_data_length >
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301488 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301489 return PLDM_ERROR_INVALID_LENGTH;
1490 }
1491 } else {
1492 return PLDM_ERROR_INVALID_DATA;
1493 }
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301494
Andrew Jeffery9c766792022-08-10 23:12:49 +09301495 *event_class_data_offset =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301496 sizeof(*sensor_id) + sizeof(*sensor_event_class_type);
Andrew Jeffery4fd45952023-04-13 13:01:45 +09301497
1498 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301499}
1500
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301501LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301502int decode_sensor_op_data(const uint8_t *sensor_data, size_t sensor_data_length,
1503 uint8_t *present_op_state, uint8_t *previous_op_state)
1504{
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301505 struct pldm_msgbuf _buf;
1506 struct pldm_msgbuf *buf = &_buf;
1507 int rc;
1508
1509 if (present_op_state == NULL || previous_op_state == NULL) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301510 return PLDM_ERROR_INVALID_DATA;
1511 }
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301512
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301513 rc = pldm_msgbuf_init_cc(buf,
1514 PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH,
1515 sensor_data, sensor_data_length);
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301516 if (rc) {
1517 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301518 }
1519
Andrew Jeffery66c77232024-04-24 11:42:02 +09301520 pldm_msgbuf_extract_p(buf, present_op_state);
1521 pldm_msgbuf_extract_p(buf, previous_op_state);
Andrew Jeffery4888ee52023-04-13 13:14:05 +09301522
1523 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301524}
1525
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301526LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301527int decode_state_sensor_data(const uint8_t *sensor_data,
1528 size_t sensor_data_length, uint8_t *sensor_offset,
1529 uint8_t *event_state,
1530 uint8_t *previous_event_state)
1531{
Andrew Jeffery422790b2023-04-13 15:03:47 +09301532 struct pldm_msgbuf _buf;
1533 struct pldm_msgbuf *buf = &_buf;
1534 int rc;
1535
1536 if (sensor_offset == NULL || event_state == NULL ||
1537 previous_event_state == NULL) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301538 return PLDM_ERROR_INVALID_DATA;
1539 }
Andrew Jeffery422790b2023-04-13 15:03:47 +09301540
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301541 rc = pldm_msgbuf_init_cc(
1542 buf, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH,
1543 sensor_data, sensor_data_length);
Andrew Jeffery422790b2023-04-13 15:03:47 +09301544 if (rc) {
1545 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301546 }
1547
Andrew Jeffery66c77232024-04-24 11:42:02 +09301548 pldm_msgbuf_extract_p(buf, sensor_offset);
1549 pldm_msgbuf_extract_p(buf, event_state);
1550 pldm_msgbuf_extract_p(buf, previous_event_state);
Andrew Jeffery422790b2023-04-13 15:03:47 +09301551
1552 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301553}
1554
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301555LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301556int decode_numeric_sensor_data(const uint8_t *sensor_data,
1557 size_t sensor_data_length, uint8_t *event_state,
1558 uint8_t *previous_event_state,
1559 uint8_t *sensor_data_size,
1560 uint32_t *present_reading)
1561{
Andrew Jeffery155317e2023-04-13 18:36:51 +09301562 struct pldm_msgbuf _buf;
1563 struct pldm_msgbuf *buf = &_buf;
1564 int rc;
1565
1566 if (sensor_data_size == NULL || event_state == NULL ||
1567 previous_event_state == NULL || present_reading == NULL) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301568 return PLDM_ERROR_INVALID_DATA;
1569 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301570
1571 if (sensor_data_length >
1572 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301573 return PLDM_ERROR_INVALID_LENGTH;
1574 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301575
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301576 rc = pldm_msgbuf_init_cc(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301577 buf, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MIN_DATA_LENGTH,
1578 sensor_data, sensor_data_length);
Andrew Jeffery155317e2023-04-13 18:36:51 +09301579 if (rc) {
1580 return rc;
1581 }
1582
Andrew Jeffery66c77232024-04-24 11:42:02 +09301583 pldm_msgbuf_extract_p(buf, event_state);
1584 pldm_msgbuf_extract_p(buf, previous_event_state);
1585 rc = pldm_msgbuf_extract_p(buf, sensor_data_size);
Andrew Jeffery155317e2023-04-13 18:36:51 +09301586 if (rc) {
1587 return rc;
1588 }
1589
1590 /*
1591 * The implementation below is bonkers, but it's because the function
1592 * prototype is bonkers. The `present_reading` argument should have been
1593 * a tagged union.
1594 */
Andrew Jeffery9c766792022-08-10 23:12:49 +09301595 switch (*sensor_data_size) {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301596 case PLDM_SENSOR_DATA_SIZE_UINT8: {
1597 uint8_t val;
Andrew Jeffery66c77232024-04-24 11:42:02 +09301598 if (!pldm_msgbuf_extract(buf, val)) {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301599 *present_reading = (uint32_t)val;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301600 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301601 break;
Andrew Jeffery155317e2023-04-13 18:36:51 +09301602 }
1603 case PLDM_SENSOR_DATA_SIZE_SINT8: {
1604 int8_t val;
Andrew Jeffery66c77232024-04-24 11:42:02 +09301605 if (!pldm_msgbuf_extract(buf, val)) {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301606 *present_reading = (uint32_t)(int32_t)val;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301607 }
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301608 break;
Andrew Jeffery155317e2023-04-13 18:36:51 +09301609 }
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301610 case PLDM_SENSOR_DATA_SIZE_UINT16: {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301611 uint16_t val;
Andrew Jeffery66c77232024-04-24 11:42:02 +09301612 if (!pldm_msgbuf_extract(buf, val)) {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301613 *present_reading = (uint32_t)val;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301614 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301615 break;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301616 }
1617 case PLDM_SENSOR_DATA_SIZE_SINT16: {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301618 int16_t val;
Andrew Jeffery66c77232024-04-24 11:42:02 +09301619 if (!pldm_msgbuf_extract(buf, val)) {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301620 *present_reading = (uint32_t)(int32_t)val;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301621 }
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301622 break;
1623 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301624 case PLDM_SENSOR_DATA_SIZE_UINT32: {
1625 uint32_t val;
Andrew Jeffery66c77232024-04-24 11:42:02 +09301626 if (!pldm_msgbuf_extract(buf, val)) {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301627 *present_reading = (uint32_t)val;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301628 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301629 break;
1630 }
1631 case PLDM_SENSOR_DATA_SIZE_SINT32: {
1632 int32_t val;
Andrew Jeffery66c77232024-04-24 11:42:02 +09301633 if (!pldm_msgbuf_extract(buf, val)) {
Andrew Jeffery155317e2023-04-13 18:36:51 +09301634 *present_reading = (uint32_t)val;
1635 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301636 break;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301637 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301638 default:
1639 return PLDM_ERROR_INVALID_DATA;
1640 }
Andrew Jeffery155317e2023-04-13 18:36:51 +09301641
1642 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301643}
1644
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301645LIBPLDM_ABI_STABLE
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301646int decode_numeric_sensor_pdr_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301647 const void *pdr_data, size_t pdr_data_length,
1648 struct pldm_numeric_sensor_value_pdr *pdr_value)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301649{
1650 struct pldm_msgbuf _buf;
1651 struct pldm_msgbuf *buf = &_buf;
1652 int rc;
1653
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301654 rc = pldm_msgbuf_init_cc(buf, PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH,
1655 pdr_data, pdr_data_length);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301656 if (rc) {
1657 return rc;
1658 }
1659
1660 rc = pldm_msgbuf_extract_value_pdr_hdr(buf, &pdr_value->hdr);
1661 if (rc) {
1662 return rc;
1663 }
1664
1665 rc = pldm_platform_pdr_hdr_validate(
Thu Nguyen51230a02023-11-10 16:22:25 +07001666 &pdr_value->hdr, PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH,
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301667 pdr_data_length);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301668 if (rc) {
1669 return rc;
1670 }
1671
Andrew Jeffery66c77232024-04-24 11:42:02 +09301672 pldm_msgbuf_extract(buf, pdr_value->terminus_handle);
1673 pldm_msgbuf_extract(buf, pdr_value->sensor_id);
1674 pldm_msgbuf_extract(buf, pdr_value->entity_type);
1675 pldm_msgbuf_extract(buf, pdr_value->entity_instance_num);
1676 pldm_msgbuf_extract(buf, pdr_value->container_id);
1677 pldm_msgbuf_extract(buf, pdr_value->sensor_init);
1678 pldm_msgbuf_extract(buf, pdr_value->sensor_auxiliary_names_pdr);
1679 pldm_msgbuf_extract(buf, pdr_value->base_unit);
1680 pldm_msgbuf_extract(buf, pdr_value->unit_modifier);
1681 pldm_msgbuf_extract(buf, pdr_value->rate_unit);
1682 pldm_msgbuf_extract(buf, pdr_value->base_oem_unit_handle);
1683 pldm_msgbuf_extract(buf, pdr_value->aux_unit);
1684 pldm_msgbuf_extract(buf, pdr_value->aux_unit_modifier);
1685 pldm_msgbuf_extract(buf, pdr_value->aux_rate_unit);
1686 pldm_msgbuf_extract(buf, pdr_value->rel);
1687 pldm_msgbuf_extract(buf, pdr_value->aux_oem_unit_handle);
1688 pldm_msgbuf_extract(buf, pdr_value->is_linear);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301689
Andrew Jeffery66c77232024-04-24 11:42:02 +09301690 rc = pldm_msgbuf_extract(buf, pdr_value->sensor_data_size);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301691 if (rc) {
1692 return rc;
1693 }
1694 if (pdr_value->sensor_data_size > PLDM_SENSOR_DATA_SIZE_MAX) {
1695 return PLDM_ERROR_INVALID_DATA;
1696 }
1697
Andrew Jeffery66c77232024-04-24 11:42:02 +09301698 pldm_msgbuf_extract(buf, pdr_value->resolution);
1699 pldm_msgbuf_extract(buf, pdr_value->offset);
1700 pldm_msgbuf_extract(buf, pdr_value->accuracy);
1701 pldm_msgbuf_extract(buf, pdr_value->plus_tolerance);
1702 pldm_msgbuf_extract(buf, pdr_value->minus_tolerance);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301703 pldm_msgbuf_extract_sensor_data(buf, pdr_value->sensor_data_size,
1704 &pdr_value->hysteresis);
Andrew Jeffery66c77232024-04-24 11:42:02 +09301705 pldm_msgbuf_extract(buf, pdr_value->supported_thresholds.byte);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301706 pldm_msgbuf_extract(
Andrew Jeffery66c77232024-04-24 11:42:02 +09301707 buf, pdr_value->threshold_and_hysteresis_volatility.byte);
1708 pldm_msgbuf_extract(buf, pdr_value->state_transition_interval);
1709 pldm_msgbuf_extract(buf, pdr_value->update_interval);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301710 pldm_msgbuf_extract_sensor_data(buf, pdr_value->sensor_data_size,
1711 &pdr_value->max_readable);
1712 pldm_msgbuf_extract_sensor_data(buf, pdr_value->sensor_data_size,
1713 &pdr_value->min_readable);
1714
Andrew Jeffery66c77232024-04-24 11:42:02 +09301715 rc = pldm_msgbuf_extract(buf, pdr_value->range_field_format);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301716 if (rc) {
1717 return rc;
1718 }
1719 if (pdr_value->range_field_format > PLDM_RANGE_FIELD_FORMAT_MAX) {
1720 return PLDM_ERROR_INVALID_DATA;
1721 }
1722
Andrew Jeffery66c77232024-04-24 11:42:02 +09301723 pldm_msgbuf_extract(buf, pdr_value->range_field_support.byte);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301724 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery66c77232024-04-24 11:42:02 +09301725 buf, pdr_value->range_field_format, pdr_value->nominal_value);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301726 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery66c77232024-04-24 11:42:02 +09301727 buf, pdr_value->range_field_format, pdr_value->normal_max);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301728 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery66c77232024-04-24 11:42:02 +09301729 buf, pdr_value->range_field_format, pdr_value->normal_min);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301730 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery66c77232024-04-24 11:42:02 +09301731 buf, pdr_value->range_field_format, pdr_value->warning_high);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301732 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery66c77232024-04-24 11:42:02 +09301733 buf, pdr_value->range_field_format, pdr_value->warning_low);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301734 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery66c77232024-04-24 11:42:02 +09301735 buf, pdr_value->range_field_format, pdr_value->critical_high);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301736 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery66c77232024-04-24 11:42:02 +09301737 buf, pdr_value->range_field_format, pdr_value->critical_low);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301738 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery66c77232024-04-24 11:42:02 +09301739 buf, pdr_value->range_field_format, pdr_value->fatal_high);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301740 pldm_msgbuf_extract_range_field_format(
Andrew Jeffery66c77232024-04-24 11:42:02 +09301741 buf, pdr_value->range_field_format, pdr_value->fatal_low);
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301742
1743 return pldm_msgbuf_destroy(buf);
1744}
1745
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301746LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301747int encode_get_numeric_effecter_value_req(uint8_t instance_id,
1748 uint16_t effecter_id,
1749 struct pldm_msg *msg)
1750{
1751 if (msg == NULL) {
1752 return PLDM_ERROR_INVALID_DATA;
1753 }
1754
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301755 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301756 header.msg_type = PLDM_REQUEST;
1757 header.instance = instance_id;
1758 header.pldm_type = PLDM_PLATFORM;
1759 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1760
1761 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1762 if (rc != PLDM_SUCCESS) {
1763 return rc;
1764 }
1765
1766 struct pldm_get_numeric_effecter_value_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301767 (struct pldm_get_numeric_effecter_value_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301768 request->effecter_id = htole16(effecter_id);
1769
1770 return PLDM_SUCCESS;
1771}
1772
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301773LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301774int encode_get_numeric_effecter_value_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301775 uint8_t instance_id, uint8_t completion_code,
1776 uint8_t effecter_data_size, uint8_t effecter_oper_state,
1777 const uint8_t *pending_value, const uint8_t *present_value,
1778 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301779{
1780 if (msg == NULL || pending_value == NULL || present_value == NULL) {
1781 return PLDM_ERROR_INVALID_DATA;
1782 }
1783
1784 if (effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1785 return PLDM_ERROR_INVALID_DATA;
1786 }
1787
1788 if (effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1789 return PLDM_ERROR_INVALID_DATA;
1790 }
1791
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301792 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301793 header.msg_type = PLDM_RESPONSE;
1794 header.instance = instance_id;
1795 header.pldm_type = PLDM_PLATFORM;
1796 header.command = PLDM_GET_NUMERIC_EFFECTER_VALUE;
1797
1798 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1799 if (rc != PLDM_SUCCESS) {
1800 return rc;
1801 }
1802
1803 struct pldm_get_numeric_effecter_value_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301804 (struct pldm_get_numeric_effecter_value_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301805
1806 response->completion_code = completion_code;
1807 response->effecter_data_size = effecter_data_size;
1808 response->effecter_oper_state = effecter_oper_state;
1809
1810 if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
1811 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
1812 if (payload_length !=
1813 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES) {
1814 return PLDM_ERROR_INVALID_LENGTH;
1815 }
1816 response->pending_and_present_values[0] = *pending_value;
1817 response->pending_and_present_values[1] = *present_value;
1818
1819 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1820 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
1821 if (payload_length !=
1822 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2) {
1823 return PLDM_ERROR_INVALID_LENGTH;
1824 }
1825 uint16_t val_pending = *(uint16_t *)pending_value;
1826 val_pending = htole16(val_pending);
1827 memcpy(response->pending_and_present_values, &val_pending,
1828 sizeof(uint16_t));
1829 uint16_t val_present = *(uint16_t *)present_value;
1830 val_present = htole16(val_present);
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301831 memcpy((response->pending_and_present_values +
1832 sizeof(uint16_t)),
1833 &val_present, sizeof(uint16_t));
Andrew Jeffery9c766792022-08-10 23:12:49 +09301834
1835 } else if (effecter_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1836 effecter_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
1837 if (payload_length !=
1838 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6) {
1839 return PLDM_ERROR_INVALID_LENGTH;
1840 }
1841 uint32_t val_pending = *(uint32_t *)pending_value;
1842 val_pending = htole32(val_pending);
1843 memcpy(response->pending_and_present_values, &val_pending,
1844 sizeof(uint32_t));
1845 uint32_t val_present = *(uint32_t *)present_value;
1846 val_present = htole32(val_present);
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301847 memcpy((response->pending_and_present_values +
1848 sizeof(uint32_t)),
1849 &val_present, sizeof(uint32_t));
Andrew Jeffery9c766792022-08-10 23:12:49 +09301850 }
1851 return PLDM_SUCCESS;
1852}
1853
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301854LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301855int decode_get_numeric_effecter_value_req(const struct pldm_msg *msg,
1856 size_t payload_length,
1857 uint16_t *effecter_id)
1858{
Andrew Jefferydd265822023-04-13 22:42:44 +09301859 struct pldm_msgbuf _buf;
1860 struct pldm_msgbuf *buf = &_buf;
1861 int rc;
1862
Andrew Jeffery9c766792022-08-10 23:12:49 +09301863 if (msg == NULL || effecter_id == NULL) {
1864 return PLDM_ERROR_INVALID_DATA;
1865 }
1866
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301867 rc = pldm_msgbuf_init_cc(buf, PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES,
1868 msg->payload, payload_length);
Andrew Jefferydd265822023-04-13 22:42:44 +09301869 if (rc) {
1870 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301871 }
1872
Andrew Jeffery66c77232024-04-24 11:42:02 +09301873 pldm_msgbuf_extract_p(buf, effecter_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301874
Andrew Jefferydd265822023-04-13 22:42:44 +09301875 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301876}
1877
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301878LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301879int decode_get_numeric_effecter_value_resp(const struct pldm_msg *msg,
1880 size_t payload_length,
1881 uint8_t *completion_code,
1882 uint8_t *effecter_data_size,
1883 uint8_t *effecter_oper_state,
1884 uint8_t *pending_value,
1885 uint8_t *present_value)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301886{
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301887 struct pldm_msgbuf _buf;
1888 struct pldm_msgbuf *buf = &_buf;
1889 int rc;
1890
Andrew Jeffery9c766792022-08-10 23:12:49 +09301891 if (msg == NULL || effecter_data_size == NULL ||
1892 effecter_oper_state == NULL || pending_value == NULL ||
1893 present_value == NULL) {
1894 return PLDM_ERROR_INVALID_DATA;
1895 }
1896
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301897 rc = pldm_msgbuf_init_cc(buf,
1898 PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES,
1899 msg->payload, payload_length);
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301900 if (rc) {
1901 return rc;
1902 }
1903
Andrew Jeffery66c77232024-04-24 11:42:02 +09301904 rc = pldm_msgbuf_extract_p(buf, completion_code);
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301905 if (rc) {
1906 return rc;
1907 }
1908
Andrew Jeffery9c766792022-08-10 23:12:49 +09301909 if (PLDM_SUCCESS != *completion_code) {
1910 return PLDM_SUCCESS;
1911 }
1912
Andrew Jeffery66c77232024-04-24 11:42:02 +09301913 rc = pldm_msgbuf_extract_p(buf, effecter_data_size);
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301914 if (rc) {
1915 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301916 }
1917
Andrew Jeffery9c766792022-08-10 23:12:49 +09301918 if (*effecter_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
1919 return PLDM_ERROR_INVALID_DATA;
1920 }
1921
Andrew Jeffery66c77232024-04-24 11:42:02 +09301922 rc = pldm_msgbuf_extract_p(buf, effecter_oper_state);
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301923 if (rc) {
1924 return rc;
1925 }
1926
Andrew Jeffery9c766792022-08-10 23:12:49 +09301927 if (*effecter_oper_state > EFFECTER_OPER_STATE_INTEST) {
1928 return PLDM_ERROR_INVALID_DATA;
1929 }
1930
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301931 pldm_msgbuf_extract_effecter_value(buf, *effecter_data_size,
1932 pending_value);
1933 pldm_msgbuf_extract_effecter_value(buf, *effecter_data_size,
1934 present_value);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301935
Andrew Jeffery99c03f32023-04-13 22:45:30 +09301936 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301937}
1938
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301939LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301940int encode_pldm_pdr_repository_chg_event_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301941 uint8_t event_data_format, uint8_t number_of_change_records,
1942 const uint8_t *event_data_operations,
1943 const uint8_t *numbers_of_change_entries,
1944 const uint32_t *const *change_entries,
1945 struct pldm_pdr_repository_chg_event_data *event_data,
1946 size_t *actual_change_records_size, size_t max_change_records_size)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301947{
1948 if (event_data_operations == NULL ||
1949 numbers_of_change_entries == NULL || change_entries == NULL) {
1950 return PLDM_ERROR_INVALID_DATA;
1951 }
1952
1953 size_t expected_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301954 sizeof(event_data_format) + sizeof(number_of_change_records);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301955
1956 expected_size +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301957 sizeof(*event_data_operations) * number_of_change_records;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301958 expected_size +=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301959 sizeof(*numbers_of_change_entries) * number_of_change_records;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301960
1961 for (uint8_t i = 0; i < number_of_change_records; ++i) {
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301962 expected_size += sizeof(*change_entries[0]) *
1963 numbers_of_change_entries[i];
Andrew Jeffery9c766792022-08-10 23:12:49 +09301964 }
1965
1966 *actual_change_records_size = expected_size;
1967
1968 if (event_data == NULL) {
1969 return PLDM_SUCCESS;
1970 }
1971
1972 if (max_change_records_size < expected_size) {
1973 return PLDM_ERROR_INVALID_LENGTH;
1974 }
1975
1976 event_data->event_data_format = event_data_format;
1977 event_data->number_of_change_records = number_of_change_records;
1978
1979 struct pldm_pdr_repository_change_record_data *record_data =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301980 (struct pldm_pdr_repository_change_record_data *)
1981 event_data->change_records;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301982
1983 for (uint8_t i = 0; i < number_of_change_records; ++i) {
1984 record_data->event_data_operation = event_data_operations[i];
1985 record_data->number_of_change_entries =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301986 numbers_of_change_entries[i];
Andrew Jeffery9c766792022-08-10 23:12:49 +09301987
1988 for (uint8_t j = 0; j < record_data->number_of_change_entries;
1989 ++j) {
1990 record_data->change_entry[j] =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301991 htole32(change_entries[i][j]);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301992 }
1993
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301994 record_data =
1995 (struct pldm_pdr_repository_change_record_data
1996 *)(record_data->change_entry +
1997 record_data->number_of_change_entries);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301998 }
1999
2000 return PLDM_SUCCESS;
2001}
2002
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302003LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302004int decode_pldm_pdr_repository_chg_event_data(const uint8_t *event_data,
2005 size_t event_data_size,
2006 uint8_t *event_data_format,
2007 uint8_t *number_of_change_records,
2008 size_t *change_record_data_offset)
2009{
Andrew Jeffery2fe70122023-04-13 23:21:31 +09302010 struct pldm_msgbuf _buf;
2011 struct pldm_msgbuf *buf = &_buf;
2012 int rc;
2013
2014 if (event_data_format == NULL || number_of_change_records == NULL ||
Andrew Jeffery9c766792022-08-10 23:12:49 +09302015 change_record_data_offset == NULL) {
2016 return PLDM_ERROR_INVALID_DATA;
2017 }
Andrew Jeffery2fe70122023-04-13 23:21:31 +09302018
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302019 rc = pldm_msgbuf_init_cc(buf, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH,
2020 event_data, event_data_size);
Andrew Jeffery2fe70122023-04-13 23:21:31 +09302021 if (rc) {
2022 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302023 }
2024
Andrew Jeffery66c77232024-04-24 11:42:02 +09302025 pldm_msgbuf_extract_p(buf, event_data_format);
2026 pldm_msgbuf_extract_p(buf, number_of_change_records);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302027
Andrew Jeffery9c766792022-08-10 23:12:49 +09302028 *change_record_data_offset =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302029 sizeof(*event_data_format) + sizeof(*number_of_change_records);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302030
Andrew Jeffery2fe70122023-04-13 23:21:31 +09302031 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302032}
2033
Thu Nguyenf874b382024-07-24 11:22:34 +00002034LIBPLDM_ABI_STABLE
Thu Nguyen7739d122024-07-26 11:36:39 +00002035int decode_pldm_message_poll_event_data(
2036 const void *event_data, size_t event_data_length,
2037 struct pldm_message_poll_event *poll_event)
Dung Cao7c250342022-11-16 22:40:37 +07002038{
2039 struct pldm_msgbuf _buf;
2040 struct pldm_msgbuf *buf = &_buf;
2041 int rc;
2042
Thu Nguyen7739d122024-07-26 11:36:39 +00002043 if (!poll_event) {
2044 return -EINVAL;
Dung Cao7c250342022-11-16 22:40:37 +07002045 }
2046
Thu Nguyen7739d122024-07-26 11:36:39 +00002047 rc = pldm_msgbuf_init_errno(buf, PLDM_MSG_POLL_EVENT_LENGTH, event_data,
2048 event_data_length);
Dung Cao7c250342022-11-16 22:40:37 +07002049 if (rc) {
2050 return rc;
2051 }
2052
Thu Nguyen7739d122024-07-26 11:36:39 +00002053 pldm_msgbuf_extract(buf, poll_event->format_version);
2054 rc = pldm_msgbuf_extract(buf, poll_event->event_id);
Dung Cao7c250342022-11-16 22:40:37 +07002055 if (rc) {
2056 return rc;
2057 }
2058
Thu Nguyen7739d122024-07-26 11:36:39 +00002059 if (poll_event->event_id == 0x0000 || poll_event->event_id == 0xffff) {
2060 return -EPROTO;
Dung Cao7c250342022-11-16 22:40:37 +07002061 }
2062
Thu Nguyen7739d122024-07-26 11:36:39 +00002063 pldm_msgbuf_extract(buf, poll_event->data_transfer_handle);
Dung Cao7c250342022-11-16 22:40:37 +07002064
2065 return pldm_msgbuf_destroy_consumed(buf);
2066}
2067
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302068LIBPLDM_ABI_TESTING
Thu Nguyen7739d122024-07-26 11:36:39 +00002069int encode_pldm_message_poll_event_data(
2070 const struct pldm_message_poll_event *poll_event, void *event_data,
2071 size_t event_data_length)
Dung Cao7c250342022-11-16 22:40:37 +07002072{
2073 struct pldm_msgbuf _buf;
2074 struct pldm_msgbuf *buf = &_buf;
2075 int rc;
2076
Thu Nguyen7739d122024-07-26 11:36:39 +00002077 if (poll_event->event_id == 0x0000 || poll_event->event_id == 0xffff) {
2078 return -EPROTO;
Dung Cao7c250342022-11-16 22:40:37 +07002079 }
2080
Thu Nguyen7739d122024-07-26 11:36:39 +00002081 rc = pldm_msgbuf_init_errno(buf, PLDM_MSG_POLL_EVENT_LENGTH, event_data,
2082 event_data_length);
Dung Cao7c250342022-11-16 22:40:37 +07002083 if (rc) {
2084 return rc;
2085 }
Thu Nguyen7739d122024-07-26 11:36:39 +00002086 pldm_msgbuf_insert(buf, poll_event->format_version);
2087 pldm_msgbuf_insert(buf, poll_event->event_id);
2088 pldm_msgbuf_insert(buf, poll_event->data_transfer_handle);
Dung Cao7c250342022-11-16 22:40:37 +07002089
Thu Nguyen7739d122024-07-26 11:36:39 +00002090 return pldm_msgbuf_destroy_consumed(buf);
Dung Cao7c250342022-11-16 22:40:37 +07002091}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302092
2093LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302094int decode_pldm_pdr_repository_change_record_data(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302095 const uint8_t *change_record_data, size_t change_record_data_size,
2096 uint8_t *event_data_operation, uint8_t *number_of_change_entries,
2097 size_t *change_entry_data_offset)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302098{
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09302099 struct pldm_msgbuf _buf;
2100 struct pldm_msgbuf *buf = &_buf;
2101 int rc;
2102
2103 if (event_data_operation == NULL || number_of_change_entries == NULL ||
Andrew Jeffery9c766792022-08-10 23:12:49 +09302104 change_entry_data_offset == NULL) {
2105 return PLDM_ERROR_INVALID_DATA;
2106 }
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09302107
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302108 rc = pldm_msgbuf_init_cc(buf,
2109 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH,
2110 change_record_data, change_record_data_size);
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09302111 if (rc) {
2112 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302113 }
2114
Andrew Jeffery66c77232024-04-24 11:42:02 +09302115 pldm_msgbuf_extract_p(buf, event_data_operation);
2116 pldm_msgbuf_extract_p(buf, number_of_change_entries);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302117
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302118 *change_entry_data_offset = sizeof(*event_data_operation) +
2119 sizeof(*number_of_change_entries);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302120
Andrew Jeffery61cab6f2023-04-13 23:28:48 +09302121 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302122}
2123
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302124LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302125int encode_get_sensor_reading_req(uint8_t instance_id, uint16_t sensor_id,
2126 uint8_t rearm_event_state,
2127 struct pldm_msg *msg)
2128{
2129 if (msg == NULL) {
2130 return PLDM_ERROR_INVALID_DATA;
2131 }
2132
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302133 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302134 header.msg_type = PLDM_REQUEST;
2135 header.instance = instance_id;
2136 header.pldm_type = PLDM_PLATFORM;
2137 header.command = PLDM_GET_SENSOR_READING;
2138
2139 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2140 if (rc != PLDM_SUCCESS) {
2141 return rc;
2142 }
2143
2144 struct pldm_get_sensor_reading_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302145 (struct pldm_get_sensor_reading_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302146
2147 request->sensor_id = htole16(sensor_id);
2148 request->rearm_event_state = rearm_event_state;
2149
2150 return PLDM_SUCCESS;
2151}
2152
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302153LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302154int decode_get_sensor_reading_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302155 const struct pldm_msg *msg, size_t payload_length,
2156 uint8_t *completion_code, uint8_t *sensor_data_size,
2157 uint8_t *sensor_operational_state, uint8_t *sensor_event_message_enable,
2158 uint8_t *present_state, uint8_t *previous_state, uint8_t *event_state,
2159 uint8_t *present_reading)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302160{
Andrew Jeffery840b1402023-04-13 23:54:44 +09302161 struct pldm_msgbuf _buf;
2162 struct pldm_msgbuf *buf = &_buf;
2163 int rc;
2164
Andrew Jeffery9c766792022-08-10 23:12:49 +09302165 if (msg == NULL || completion_code == NULL ||
2166 sensor_data_size == NULL || sensor_operational_state == NULL ||
2167 sensor_event_message_enable == NULL || present_state == NULL ||
2168 previous_state == NULL || event_state == NULL ||
2169 present_reading == NULL) {
2170 return PLDM_ERROR_INVALID_DATA;
2171 }
2172
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302173 rc = pldm_msgbuf_init_cc(buf, PLDM_GET_SENSOR_READING_MIN_RESP_BYTES,
2174 msg->payload, payload_length);
Andrew Jeffery840b1402023-04-13 23:54:44 +09302175 if (rc) {
2176 return rc;
2177 }
2178
Andrew Jeffery66c77232024-04-24 11:42:02 +09302179 rc = pldm_msgbuf_extract_p(buf, completion_code);
Andrew Jeffery840b1402023-04-13 23:54:44 +09302180 if (rc) {
2181 return rc;
2182 }
2183
Andrew Jeffery9c766792022-08-10 23:12:49 +09302184 if (PLDM_SUCCESS != *completion_code) {
2185 return PLDM_SUCCESS;
2186 }
2187
Andrew Jeffery66c77232024-04-24 11:42:02 +09302188 rc = pldm_msgbuf_extract_p(buf, sensor_data_size);
Andrew Jeffery840b1402023-04-13 23:54:44 +09302189 if (rc) {
2190 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302191 }
2192
Andrew Jeffery840b1402023-04-13 23:54:44 +09302193 if (*sensor_data_size > PLDM_SENSOR_DATA_SIZE_SINT32) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09302194 return PLDM_ERROR_INVALID_DATA;
2195 }
2196
Andrew Jeffery66c77232024-04-24 11:42:02 +09302197 pldm_msgbuf_extract_p(buf, sensor_operational_state);
2198 pldm_msgbuf_extract_p(buf, sensor_event_message_enable);
2199 pldm_msgbuf_extract_p(buf, present_state);
2200 pldm_msgbuf_extract_p(buf, previous_state);
2201 pldm_msgbuf_extract_p(buf, event_state);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302202
Andrew Jeffery840b1402023-04-13 23:54:44 +09302203 pldm_msgbuf_extract_sensor_value(buf, *sensor_data_size,
2204 present_reading);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302205
Andrew Jeffery840b1402023-04-13 23:54:44 +09302206 return pldm_msgbuf_destroy_consumed(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302207}
2208
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302209LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302210int encode_get_sensor_reading_resp(uint8_t instance_id, uint8_t completion_code,
2211 uint8_t sensor_data_size,
2212 uint8_t sensor_operational_state,
2213 uint8_t sensor_event_message_enable,
2214 uint8_t present_state,
2215 uint8_t previous_state, uint8_t event_state,
2216 const uint8_t *present_reading,
2217 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302218{
2219 if (msg == NULL || present_reading == NULL) {
2220 return PLDM_ERROR_INVALID_DATA;
2221 }
2222
2223 if (sensor_data_size > PLDM_EFFECTER_DATA_SIZE_SINT32) {
2224 return PLDM_ERROR_INVALID_DATA;
2225 }
2226
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302227 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302228 header.msg_type = PLDM_RESPONSE;
2229 header.instance = instance_id;
2230 header.pldm_type = PLDM_PLATFORM;
2231 header.command = PLDM_GET_SENSOR_READING;
2232
2233 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2234 if (rc != PLDM_SUCCESS) {
2235 return rc;
2236 }
2237
2238 struct pldm_get_sensor_reading_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302239 (struct pldm_get_sensor_reading_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302240
2241 response->completion_code = completion_code;
2242 response->sensor_data_size = sensor_data_size;
2243 response->sensor_operational_state = sensor_operational_state;
2244 response->sensor_event_message_enable = sensor_event_message_enable;
2245 response->present_state = present_state;
2246 response->previous_state = previous_state;
2247 response->event_state = event_state;
2248
2249 if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT8 ||
2250 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT8) {
2251 if (payload_length != PLDM_GET_SENSOR_READING_MIN_RESP_BYTES) {
2252 return PLDM_ERROR_INVALID_LENGTH;
2253 }
2254 response->present_reading[0] = *present_reading;
2255
2256 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
2257 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT16) {
2258 if (payload_length !=
2259 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1) {
2260 return PLDM_ERROR_INVALID_LENGTH;
2261 }
2262 uint16_t val = *(uint16_t *)present_reading;
2263 val = htole16(val);
2264 memcpy(response->present_reading, &val, 2);
2265
2266 } else if (sensor_data_size == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
2267 sensor_data_size == PLDM_EFFECTER_DATA_SIZE_SINT32) {
2268 if (payload_length !=
2269 PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3) {
2270 return PLDM_ERROR_INVALID_LENGTH;
2271 }
2272 uint32_t val = *(uint32_t *)present_reading;
2273 val = htole32(val);
2274 memcpy(response->present_reading, &val, 4);
2275 }
2276
2277 return PLDM_SUCCESS;
2278}
2279
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302280LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302281int decode_get_sensor_reading_req(const struct pldm_msg *msg,
2282 size_t payload_length, uint16_t *sensor_id,
2283 uint8_t *rearm_event_state)
2284{
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302285 struct pldm_msgbuf _buf;
2286 struct pldm_msgbuf *buf = &_buf;
2287 int rc;
2288
Andrew Jeffery9c766792022-08-10 23:12:49 +09302289 if (msg == NULL || sensor_id == NULL || rearm_event_state == NULL) {
2290 return PLDM_ERROR_INVALID_DATA;
2291 }
2292
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302293 rc = pldm_msgbuf_init_cc(buf, PLDM_GET_SENSOR_READING_REQ_BYTES,
2294 msg->payload, payload_length);
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302295 if (rc) {
2296 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302297 }
2298
Andrew Jeffery66c77232024-04-24 11:42:02 +09302299 pldm_msgbuf_extract_p(buf, sensor_id);
2300 pldm_msgbuf_extract_p(buf, rearm_event_state);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302301
Andrew Jeffery2d1d1bd2023-04-13 23:58:05 +09302302 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302303}
2304
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302305LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302306int encode_set_event_receiver_req(uint8_t instance_id,
2307 uint8_t event_message_global_enable,
2308 uint8_t transport_protocol_type,
2309 uint8_t event_receiver_address_info,
2310 uint16_t heartbeat_timer,
2311 struct pldm_msg *msg)
2312{
2313 if (msg == NULL) {
2314 return PLDM_ERROR_INVALID_DATA;
2315 }
2316
2317 if (transport_protocol_type != PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP) {
2318 return PLDM_ERROR_INVALID_DATA;
2319 }
2320
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302321 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302322 header.msg_type = PLDM_REQUEST;
2323 header.instance = instance_id;
2324 header.pldm_type = PLDM_PLATFORM;
2325 header.command = PLDM_SET_EVENT_RECEIVER;
2326
2327 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2328 if (rc != PLDM_SUCCESS) {
2329 return rc;
2330 }
2331
2332 struct pldm_set_event_receiver_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302333 (struct pldm_set_event_receiver_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302334 request->event_message_global_enable = event_message_global_enable;
2335
2336 request->transport_protocol_type = transport_protocol_type;
2337 request->event_receiver_address_info = event_receiver_address_info;
2338
2339 if (event_message_global_enable ==
2340 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) {
2341 if (heartbeat_timer == 0) {
2342 return PLDM_ERROR_INVALID_DATA;
2343 }
2344 request->heartbeat_timer = htole16(heartbeat_timer);
2345 }
2346
2347 return PLDM_SUCCESS;
2348}
2349
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302350LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302351int decode_set_event_receiver_resp(const struct pldm_msg *msg,
2352 size_t payload_length,
2353 uint8_t *completion_code)
2354{
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302355 struct pldm_msgbuf _buf;
2356 struct pldm_msgbuf *buf = &_buf;
2357 int rc;
2358
Andrew Jeffery9c766792022-08-10 23:12:49 +09302359 if (msg == NULL || completion_code == NULL) {
2360 return PLDM_ERROR_INVALID_DATA;
2361 }
2362
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302363 rc = pldm_msgbuf_init_cc(buf, PLDM_SET_EVENT_RECEIVER_RESP_BYTES,
2364 msg->payload, payload_length);
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302365 if (rc) {
2366 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302367 }
2368
Andrew Jeffery66c77232024-04-24 11:42:02 +09302369 pldm_msgbuf_extract_p(buf, completion_code);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302370
Andrew Jeffery42dd4e52023-04-14 00:04:38 +09302371 return pldm_msgbuf_destroy(buf);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302372}
2373
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302374LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302375int decode_set_event_receiver_req(const struct pldm_msg *msg,
2376 size_t payload_length,
2377 uint8_t *event_message_global_enable,
2378 uint8_t *transport_protocol_type,
2379 uint8_t *event_receiver_address_info,
2380 uint16_t *heartbeat_timer)
2381
2382{
Andrew Jeffery9667f582023-04-14 00:39:21 +09302383 struct pldm_msgbuf _buf;
2384 struct pldm_msgbuf *buf = &_buf;
2385 int rc;
2386
Andrew Jeffery9c766792022-08-10 23:12:49 +09302387 if (msg == NULL || event_message_global_enable == NULL ||
2388 transport_protocol_type == NULL ||
2389 event_receiver_address_info == NULL || heartbeat_timer == NULL) {
2390 return PLDM_ERROR_INVALID_DATA;
2391 }
2392
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302393 rc = pldm_msgbuf_init_cc(buf, PLDM_SET_EVENT_RECEIVER_REQ_BYTES,
2394 msg->payload, payload_length);
Andrew Jeffery9667f582023-04-14 00:39:21 +09302395 if (rc) {
2396 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302397 }
2398
Andrew Jeffery66c77232024-04-24 11:42:02 +09302399 pldm_msgbuf_extract_p(buf, event_message_global_enable);
2400 pldm_msgbuf_extract_p(buf, transport_protocol_type);
2401 pldm_msgbuf_extract_p(buf, event_receiver_address_info);
2402 pldm_msgbuf_extract_p(buf, heartbeat_timer);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302403
Andrew Jeffery9667f582023-04-14 00:39:21 +09302404 rc = pldm_msgbuf_destroy(buf);
2405 if (rc) {
2406 return rc;
2407 }
Andrew Jeffery6ef2aa92023-04-14 00:21:27 +09302408
Andrew Jeffery9c766792022-08-10 23:12:49 +09302409 if ((*event_message_global_enable ==
2410 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE) &&
2411 (*heartbeat_timer == 0)) {
2412 return PLDM_ERROR_INVALID_DATA;
2413 }
2414
Andrew Jeffery9c766792022-08-10 23:12:49 +09302415 return PLDM_SUCCESS;
2416}
2417
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302418LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09302419int encode_set_event_receiver_resp(uint8_t instance_id, uint8_t completion_code,
2420 struct pldm_msg *msg)
2421
2422{
2423 if (msg == NULL) {
2424 return PLDM_ERROR_INVALID_DATA;
2425 }
2426
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302427 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09302428 header.instance = instance_id;
2429 header.msg_type = PLDM_RESPONSE;
2430 header.pldm_type = PLDM_PLATFORM;
2431 header.command = PLDM_SET_EVENT_RECEIVER;
2432
2433 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
2434 if (rc != PLDM_SUCCESS) {
2435 return rc;
2436 }
2437
2438 msg->payload[0] = completion_code;
2439
2440 return PLDM_SUCCESS;
2441}
Thu Nguyen159a98b2022-11-02 10:00:10 +07002442
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302443LIBPLDM_ABI_STABLE
Thu Nguyen159a98b2022-11-02 10:00:10 +07002444int encode_poll_for_platform_event_message_req(uint8_t instance_id,
2445 uint8_t format_version,
2446 uint8_t transfer_operation_flag,
2447 uint32_t data_transfer_handle,
2448 uint16_t event_id_to_acknowledge,
2449 struct pldm_msg *msg,
2450 size_t payload_length)
2451{
2452 struct pldm_msgbuf _buf;
2453 struct pldm_msgbuf *buf = &_buf;
2454 int rc;
2455
2456 if (msg == NULL) {
2457 return PLDM_ERROR_INVALID_DATA;
2458 }
2459
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302460 struct pldm_header_info header = { 0 };
Thu Nguyen159a98b2022-11-02 10:00:10 +07002461 header.msg_type = PLDM_REQUEST;
2462 header.instance = instance_id;
2463 header.pldm_type = PLDM_PLATFORM;
2464 header.command = PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE;
2465
2466 rc = pack_pldm_header(&header, &(msg->hdr));
2467 if (rc != PLDM_SUCCESS) {
2468 return rc;
2469 }
2470
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302471 rc = pldm_msgbuf_init_cc(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302472 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
2473 msg->payload, payload_length);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002474 if (rc) {
2475 return rc;
2476 }
2477
2478 pldm_msgbuf_insert(buf, format_version);
2479 pldm_msgbuf_insert(buf, transfer_operation_flag);
2480 pldm_msgbuf_insert(buf, data_transfer_handle);
2481 pldm_msgbuf_insert(buf, event_id_to_acknowledge);
2482
2483 return pldm_msgbuf_destroy(buf);
2484}
2485
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302486LIBPLDM_ABI_STABLE
Thu Nguyen159a98b2022-11-02 10:00:10 +07002487int decode_poll_for_platform_event_message_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302488 const struct pldm_msg *msg, size_t payload_length,
2489 uint8_t *completion_code, uint8_t *tid, uint16_t *event_id,
2490 uint32_t *next_data_transfer_handle, uint8_t *transfer_flag,
2491 uint8_t *event_class, uint32_t *event_data_size, void **event_data,
2492 uint32_t *event_data_integrity_checksum)
Thu Nguyen159a98b2022-11-02 10:00:10 +07002493{
2494 struct pldm_msgbuf _buf;
2495 struct pldm_msgbuf *buf = &_buf;
2496 int rc;
2497
2498 if (msg == NULL || completion_code == NULL || tid == NULL ||
2499 event_id == NULL || next_data_transfer_handle == NULL ||
2500 transfer_flag == NULL || event_class == NULL ||
2501 event_data_size == NULL || event_data == NULL ||
2502 event_data_integrity_checksum == NULL) {
2503 return PLDM_ERROR_INVALID_DATA;
2504 }
2505
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302506 rc = pldm_msgbuf_init_cc(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09302507 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
2508 msg->payload, payload_length);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002509 if (rc) {
2510 return rc;
2511 }
2512
Andrew Jeffery66c77232024-04-24 11:42:02 +09302513 rc = pldm_msgbuf_extract_p(buf, completion_code);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002514 if (rc) {
2515 return rc;
2516 }
2517 if (PLDM_SUCCESS != *completion_code) {
2518 return *completion_code;
2519 }
2520
Andrew Jeffery66c77232024-04-24 11:42:02 +09302521 pldm_msgbuf_extract_p(buf, tid);
2522 rc = pldm_msgbuf_extract_p(buf, event_id);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002523 if (rc) {
2524 return rc;
2525 }
2526 if ((*event_id == 0) || (*event_id == 0xffff)) {
2527 return PLDM_SUCCESS;
2528 }
2529
Andrew Jeffery66c77232024-04-24 11:42:02 +09302530 pldm_msgbuf_extract_p(buf, next_data_transfer_handle);
2531 rc = pldm_msgbuf_extract_p(buf, transfer_flag);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002532 if (rc) {
2533 return rc;
2534 }
2535
Andrew Jeffery66c77232024-04-24 11:42:02 +09302536 pldm_msgbuf_extract_p(buf, event_class);
2537 rc = pldm_msgbuf_extract_p(buf, event_data_size);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002538 if (rc) {
2539 return rc;
2540 }
2541 if (*event_data_size > payload_length) {
2542 return PLDM_ERROR_INVALID_DATA;
2543 }
2544
2545 if (*event_data_size > 0) {
2546 pldm_msgbuf_span_required(buf, *event_data_size, event_data);
2547 }
2548
2549 if (*transfer_flag == PLDM_END ||
2550 *transfer_flag == PLDM_START_AND_END) {
Andrew Jeffery66c77232024-04-24 11:42:02 +09302551 pldm_msgbuf_extract_p(buf, event_data_integrity_checksum);
Thu Nguyen159a98b2022-11-02 10:00:10 +07002552 }
2553
2554 return pldm_msgbuf_destroy_consumed(buf);
2555}
Thu Nguyend4878cd2023-11-09 10:18:33 +07002556
2557LIBPLDM_ABI_TESTING
2558int decode_numeric_effecter_pdr_data(
2559 const void *pdr_data, size_t pdr_data_length,
2560 struct pldm_numeric_effecter_value_pdr *pdr_value)
2561{
2562 struct pldm_msgbuf _buf;
2563 struct pldm_msgbuf *buf = &_buf;
2564 struct pldm_value_pdr_hdr hdr;
2565 int rc;
2566
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302567 rc = pldm_msgbuf_init_cc(buf, PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH,
2568 pdr_data, pdr_data_length);
Thu Nguyend4878cd2023-11-09 10:18:33 +07002569 if (rc) {
2570 return rc;
2571 }
2572
2573 rc = pldm_msgbuf_extract_value_pdr_hdr(buf, &hdr);
2574 if (rc) {
2575 return rc;
2576 }
2577
2578 rc = pldm_platform_pdr_hdr_validate(
2579 &hdr, PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH,
2580 pdr_data_length);
2581 if (rc) {
2582 return rc;
2583 }
2584
2585 memcpy(&pdr_value->hdr, &hdr, sizeof(hdr));
2586
2587 pldm_msgbuf_extract(buf, pdr_value->terminus_handle);
2588 pldm_msgbuf_extract(buf, pdr_value->effecter_id);
2589 pldm_msgbuf_extract(buf, pdr_value->entity_type);
2590 pldm_msgbuf_extract(buf, pdr_value->entity_instance);
2591 pldm_msgbuf_extract(buf, pdr_value->container_id);
2592 pldm_msgbuf_extract(buf, pdr_value->effecter_semantic_id);
2593 pldm_msgbuf_extract(buf, pdr_value->effecter_init);
2594 pldm_msgbuf_extract(buf, pdr_value->effecter_auxiliary_names);
2595 pldm_msgbuf_extract(buf, pdr_value->base_unit);
2596 pldm_msgbuf_extract(buf, pdr_value->unit_modifier);
2597 pldm_msgbuf_extract(buf, pdr_value->rate_unit);
2598 pldm_msgbuf_extract(buf, pdr_value->base_oem_unit_handle);
2599 pldm_msgbuf_extract(buf, pdr_value->aux_unit);
2600 pldm_msgbuf_extract(buf, pdr_value->aux_unit_modifier);
2601 pldm_msgbuf_extract(buf, pdr_value->aux_rate_unit);
2602 pldm_msgbuf_extract(buf, pdr_value->aux_oem_unit_handle);
2603 pldm_msgbuf_extract(buf, pdr_value->is_linear);
2604
2605 rc = pldm_msgbuf_extract(buf, pdr_value->effecter_data_size);
2606 if (rc) {
2607 return rc;
2608 }
2609 if (pdr_value->effecter_data_size > PLDM_SENSOR_DATA_SIZE_MAX) {
2610 return PLDM_ERROR_INVALID_DATA;
2611 }
2612
2613 pldm_msgbuf_extract(buf, pdr_value->resolution);
2614 pldm_msgbuf_extract(buf, pdr_value->offset);
2615 pldm_msgbuf_extract(buf, pdr_value->accuracy);
2616 pldm_msgbuf_extract(buf, pdr_value->plus_tolerance);
2617 pldm_msgbuf_extract(buf, pdr_value->minus_tolerance);
2618 pldm_msgbuf_extract(buf, pdr_value->state_transition_interval);
2619 pldm_msgbuf_extract(buf, pdr_value->transition_interval);
2620 pldm_msgbuf_extract_effecter_data(buf, pdr_value->effecter_data_size,
2621 pdr_value->max_settable);
2622 pldm_msgbuf_extract_effecter_data(buf, pdr_value->effecter_data_size,
2623 pdr_value->min_settable);
2624
2625 rc = pldm_msgbuf_extract(buf, pdr_value->range_field_format);
2626 if (rc) {
2627 return rc;
2628 }
2629 if (pdr_value->range_field_format > PLDM_RANGE_FIELD_FORMAT_MAX) {
2630 return PLDM_ERROR_INVALID_DATA;
2631 }
2632
2633 pldm_msgbuf_extract(buf, pdr_value->range_field_support.byte);
2634 pldm_msgbuf_extract_range_field_format(
2635 buf, pdr_value->range_field_format, pdr_value->nominal_value);
2636 pldm_msgbuf_extract_range_field_format(
2637 buf, pdr_value->range_field_format, pdr_value->normal_max);
2638 pldm_msgbuf_extract_range_field_format(
2639 buf, pdr_value->range_field_format, pdr_value->normal_min);
2640 pldm_msgbuf_extract_range_field_format(
2641 buf, pdr_value->range_field_format, pdr_value->rated_max);
2642 pldm_msgbuf_extract_range_field_format(
2643 buf, pdr_value->range_field_format, pdr_value->rated_min);
2644
2645 return pldm_msgbuf_destroy_consumed(buf);
2646}
Tal Yacobia6fa5552024-05-05 16:57:38 +03002647
Tal Yacobide67ab62024-05-30 22:36:50 +03002648LIBPLDM_ABI_STABLE
Tal Yacobia6fa5552024-05-05 16:57:38 +03002649int encode_get_state_effecter_states_req(uint8_t instance_id,
2650 uint16_t effecter_id,
2651 struct pldm_msg *msg,
2652 size_t payload_length)
2653{
2654 struct pldm_msgbuf _buf;
2655 struct pldm_msgbuf *buf = &_buf;
2656 int rc;
2657
2658 if (msg == NULL) {
Tal Yacobif490a382024-05-31 09:57:36 +03002659 return -EINVAL;
Tal Yacobia6fa5552024-05-05 16:57:38 +03002660 }
2661
2662 struct pldm_header_info header = { 0 };
2663 header.msg_type = PLDM_REQUEST;
2664 header.instance = instance_id;
2665 header.pldm_type = PLDM_PLATFORM;
2666 header.command = PLDM_GET_STATE_EFFECTER_STATES;
2667
Tal Yacobif490a382024-05-31 09:57:36 +03002668 rc = pack_pldm_header_errno(&header, &msg->hdr);
2669 if (rc < 0) {
Tal Yacobia6fa5552024-05-05 16:57:38 +03002670 return rc;
2671 }
2672
Tal Yacobif490a382024-05-31 09:57:36 +03002673 rc = pldm_msgbuf_init_errno(buf,
2674 PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES,
2675 msg->payload, payload_length);
Tal Yacobia6fa5552024-05-05 16:57:38 +03002676 if (rc) {
2677 return rc;
2678 }
2679
2680 pldm_msgbuf_insert(buf, effecter_id);
2681
2682 return pldm_msgbuf_destroy_consumed(buf);
2683}
2684
Tal Yacobide67ab62024-05-30 22:36:50 +03002685LIBPLDM_ABI_STABLE
Tal Yacobia6fa5552024-05-05 16:57:38 +03002686int decode_get_state_effecter_states_req(const struct pldm_msg *msg,
2687 size_t payload_length,
2688 uint16_t *effecter_id)
2689{
2690 struct pldm_msgbuf _buf;
2691 struct pldm_msgbuf *buf = &_buf;
2692 int rc;
2693
2694 if (msg == NULL || effecter_id == NULL) {
Tal Yacobif490a382024-05-31 09:57:36 +03002695 return -EINVAL;
Tal Yacobia6fa5552024-05-05 16:57:38 +03002696 }
2697
Tal Yacobif490a382024-05-31 09:57:36 +03002698 rc = pldm_msgbuf_init_errno(
2699 buf, PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES,
2700 msg->payload, payload_length);
Tal Yacobia6fa5552024-05-05 16:57:38 +03002701 if (rc) {
2702 return rc;
2703 }
2704
2705 pldm_msgbuf_extract_p(buf, effecter_id);
2706
2707 return pldm_msgbuf_destroy_consumed(buf);
2708}
2709
Tal Yacobide67ab62024-05-30 22:36:50 +03002710LIBPLDM_ABI_STABLE
Tal Yacobia6fa5552024-05-05 16:57:38 +03002711int decode_get_state_effecter_states_resp(
2712 const struct pldm_msg *msg, size_t payload_length,
2713 struct pldm_get_state_effecter_states_resp *resp)
2714{
2715 struct pldm_msgbuf _buf;
2716 struct pldm_msgbuf *buf = &_buf;
2717 get_effecter_state_field *field;
2718 int rc;
2719 int i;
2720
2721 if (msg == NULL || resp == NULL) {
Tal Yacobif490a382024-05-31 09:57:36 +03002722 return -EINVAL;
Tal Yacobia6fa5552024-05-05 16:57:38 +03002723 }
2724
Tal Yacobif490a382024-05-31 09:57:36 +03002725 rc = pldm_msgbuf_init_errno(
2726 buf, PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES,
2727 msg->payload, payload_length);
Tal Yacobia6fa5552024-05-05 16:57:38 +03002728 if (rc) {
2729 return rc;
2730 }
2731
2732 rc = pldm_msgbuf_extract(buf, resp->completion_code);
2733 if (rc) {
2734 return rc;
2735 }
2736
2737 if (PLDM_SUCCESS != resp->completion_code) {
Tal Yacobif490a382024-05-31 09:57:36 +03002738 return 0;
Tal Yacobia6fa5552024-05-05 16:57:38 +03002739 }
2740
2741 rc = pldm_msgbuf_extract(buf, resp->comp_effecter_count);
2742 if (rc) {
2743 return rc;
2744 }
2745
2746 uint8_t comp_effecter_count = resp->comp_effecter_count;
2747
2748 if (comp_effecter_count < PLDM_GET_EFFECTER_STATE_FIELD_COUNT_MIN ||
2749 comp_effecter_count > PLDM_GET_EFFECTER_STATE_FIELD_COUNT_MAX) {
Tal Yacobif490a382024-05-31 09:57:36 +03002750 return -EBADMSG;
Tal Yacobia6fa5552024-05-05 16:57:38 +03002751 }
2752
2753 for (i = 0, field = resp->field; i < comp_effecter_count;
2754 i++, field++) {
2755 pldm_msgbuf_extract(buf, field->effecter_op_state);
2756 pldm_msgbuf_extract(buf, field->pending_state);
2757 pldm_msgbuf_extract(buf, field->present_state);
2758 }
2759
2760 return pldm_msgbuf_destroy_consumed(buf);
2761}
2762
Tal Yacobide67ab62024-05-30 22:36:50 +03002763LIBPLDM_ABI_STABLE
Tal Yacobia6fa5552024-05-05 16:57:38 +03002764int encode_get_state_effecter_states_resp(
2765 uint8_t instance_id, struct pldm_get_state_effecter_states_resp *resp,
2766 struct pldm_msg *msg, size_t payload_length)
2767{
2768 struct pldm_msgbuf _buf;
2769 struct pldm_msgbuf *buf = &_buf;
2770 get_effecter_state_field *field;
2771 int rc;
2772 int i;
2773
2774 if (msg == NULL || resp == NULL) {
Tal Yacobif490a382024-05-31 09:57:36 +03002775 return -EINVAL;
Tal Yacobia6fa5552024-05-05 16:57:38 +03002776 }
2777
2778 uint8_t comp_effecter_count = resp->comp_effecter_count;
2779
2780 if (comp_effecter_count < PLDM_GET_EFFECTER_STATE_FIELD_COUNT_MIN ||
2781 comp_effecter_count > PLDM_GET_EFFECTER_STATE_FIELD_COUNT_MAX) {
Tal Yacobif490a382024-05-31 09:57:36 +03002782 return -EBADMSG;
Tal Yacobia6fa5552024-05-05 16:57:38 +03002783 }
2784
2785 struct pldm_header_info header = { 0 };
2786 header.msg_type = PLDM_RESPONSE;
2787 header.instance = instance_id;
2788 header.pldm_type = PLDM_PLATFORM;
2789 header.command = PLDM_GET_STATE_EFFECTER_STATES;
2790
Tal Yacobif490a382024-05-31 09:57:36 +03002791 rc = pack_pldm_header_errno(&header, &msg->hdr);
2792 if (rc < 0) {
Tal Yacobia6fa5552024-05-05 16:57:38 +03002793 return rc;
2794 }
2795
Tal Yacobif490a382024-05-31 09:57:36 +03002796 rc = pldm_msgbuf_init_errno(
2797 buf, PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES,
2798 msg->payload, payload_length);
Tal Yacobia6fa5552024-05-05 16:57:38 +03002799 if (rc) {
2800 return rc;
2801 }
2802
2803 pldm_msgbuf_insert(buf, resp->completion_code);
2804 pldm_msgbuf_insert(buf, comp_effecter_count);
2805
2806 for (i = 0, field = resp->field; i < comp_effecter_count;
2807 i++, field++) {
2808 pldm_msgbuf_insert(buf, field->effecter_op_state);
2809 pldm_msgbuf_insert(buf, field->pending_state);
2810 pldm_msgbuf_insert(buf, field->present_state);
2811 }
2812
2813 return pldm_msgbuf_destroy_consumed(buf);
2814}
Thu Nguyendacfa352024-06-22 09:53:15 +00002815
Thu Nguyen43cb4b52024-07-16 05:45:27 +00002816LIBPLDM_ABI_STABLE
Thu Nguyendacfa352024-06-22 09:53:15 +00002817int decode_entity_auxiliary_names_pdr(
2818 const void *data, size_t data_length,
2819 struct pldm_entity_auxiliary_names_pdr *pdr, size_t pdr_length)
2820{
2821 struct pldm_msgbuf _buf;
2822 struct pldm_msgbuf *buf = &_buf;
2823 struct pldm_msgbuf _src;
2824 struct pldm_msgbuf *src = &_src;
2825 struct pldm_msgbuf _dst;
2826 struct pldm_msgbuf *dst = &_dst;
2827 size_t names_len = 0;
2828 void *names = NULL;
2829 int rc;
2830 int i;
2831
2832 /*
2833 * Alignment of auxiliary_name_data is an invariant as we statically assert
2834 * its behaviour in the header.
2835 */
2836 assert(!((uintptr_t)pdr->auxiliary_name_data &
2837 (alignof(pldm_utf16be) - 1)));
2838
2839 /* Reject any lengths that are obviously invalid */
2840 if (pdr_length < data_length || pdr_length < sizeof(*pdr)) {
2841 return -EINVAL;
2842 }
2843
2844 rc = pldm_msgbuf_init_errno(
2845 buf, PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH, data,
2846 data_length);
2847 if (rc) {
2848 return rc;
2849 }
2850
2851 rc = pldm_msgbuf_extract_value_pdr_hdr(buf, &pdr->hdr);
2852 if (rc) {
2853 return rc;
2854 }
2855
2856 rc = pldm_platform_pdr_hdr_validate(
2857 &pdr->hdr, PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH,
2858 data_length);
2859 if (rc) {
2860 return rc;
2861 }
2862
2863 pldm_msgbuf_extract(buf, pdr->container.entity_type);
2864 pldm_msgbuf_extract(buf, pdr->container.entity_instance_num);
2865 pldm_msgbuf_extract(buf, pdr->container.entity_container_id);
2866 pldm_msgbuf_extract(buf, pdr->shared_name_count);
2867 rc = pldm_msgbuf_extract(buf, pdr->name_string_count);
2868 if (rc < 0) {
2869 return rc;
2870 }
2871
2872 rc = pldm_msgbuf_span_remaining(buf, &names, &names_len);
2873 if (rc < 0) {
2874 return rc;
2875 }
2876
2877 pdr->auxiliary_name_data_size = pdr_length - sizeof(*pdr);
2878
2879 rc = pldm_msgbuf_init_errno(dst, pdr->auxiliary_name_data_size,
2880 pdr->auxiliary_name_data,
2881 pdr->auxiliary_name_data_size);
2882 if (rc < 0) {
2883 return rc;
2884 }
2885
2886 /*
2887 * Below we do two passes over the same region. This is to first pack the
2888 * UTF16-BE strings into auxiliary_name_data, followed by the ASCII strings,
2889 * to maintain appropriate alignment.
2890 */
2891
2892 /* Initialise for the first pass to extract the UTF16-BE name strings */
2893 rc = pldm_msgbuf_init_errno(src, names_len, names, names_len);
2894 if (rc < 0) {
2895 return rc;
2896 }
2897
2898 for (i = 0; i < pdr->name_string_count; i++) {
2899 pldm_msgbuf_span_string_ascii(src, NULL, NULL);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00002900 rc = pldm_msgbuf_copy_string_utf16(dst, src);
2901 if (rc) {
2902 return rc;
2903 }
Thu Nguyendacfa352024-06-22 09:53:15 +00002904 }
2905
2906 rc = pldm_msgbuf_destroy_consumed(src);
2907 if (rc < 0) {
2908 return rc;
2909 }
2910
2911 /* Reinitialise for the second pass to extract the ASCII tag strings */
2912 rc = pldm_msgbuf_init_errno(src, names_len, names, names_len);
2913 if (rc < 0) {
2914 return rc;
2915 }
2916
2917 for (i = 0; i < pdr->name_string_count; i++) {
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00002918 rc = pldm_msgbuf_copy_string_ascii(dst, src);
2919 if (rc) {
2920 return rc;
2921 }
Thu Nguyendacfa352024-06-22 09:53:15 +00002922 pldm_msgbuf_span_string_utf16(src, NULL, NULL);
2923 }
2924
2925 if ((rc = pldm_msgbuf_destroy(dst)) ||
2926 (rc = pldm_msgbuf_destroy(src)) ||
2927 (rc = pldm_msgbuf_destroy(buf))) {
2928 return rc;
2929 }
2930
2931 return 0;
2932}
2933
Thu Nguyen43cb4b52024-07-16 05:45:27 +00002934LIBPLDM_ABI_STABLE
Thu Nguyendacfa352024-06-22 09:53:15 +00002935int decode_pldm_entity_auxiliary_names_pdr_index(
2936 struct pldm_entity_auxiliary_names_pdr *pdr)
2937{
2938 struct pldm_msgbuf _buf;
2939 struct pldm_msgbuf *buf = &_buf;
2940 int rc;
2941 int i;
2942
2943 if (!pdr) {
2944 return -EINVAL;
2945 }
2946
2947 if (pdr->name_string_count == 0 && pdr->names) {
2948 return -EINVAL;
2949 }
2950
2951 if (pdr->name_string_count > 0 && !pdr->names) {
2952 return -EINVAL;
2953 }
2954
2955 if (pdr->name_string_count == 0) {
2956 return 0;
2957 }
2958
2959 /*
2960 * Minimum size is one NUL for each member of each entry
2961 *
2962 * Note that the definition of nameLanguageTag in DSP0248 v1.2.2
2963 * states the following:
2964 *
2965 * > A null-terminated ISO646 ASCII string ...
2966 * >
2967 * > special value: null string = 0x0000 = unspecified.
2968 *
2969 * Until proven otherwise we will assume the "0x0000" is a
2970 * misrepresentation of an ASCII NUL, and that ASCII NUL is
2971 * represented by a single byte.
2972 */
2973 rc = pldm_msgbuf_init_errno(
2974 buf, pdr->name_string_count * (sizeof(char) + sizeof(char16_t)),
2975 pdr->auxiliary_name_data, pdr->auxiliary_name_data_size);
2976 if (rc) {
2977 return rc;
2978 }
2979
2980 for (i = 0; i < pdr->name_string_count; i++) {
2981 void *loc = NULL;
2982 pldm_msgbuf_span_string_utf16(buf, &loc, NULL);
2983 pdr->names[i].name = loc;
2984 }
2985
2986 for (i = 0; i < pdr->name_string_count; i++) {
2987 void *loc = NULL;
2988 pldm_msgbuf_span_string_ascii(buf, &loc, NULL);
2989 pdr->names[i].tag = loc;
2990 }
2991
2992 return pldm_msgbuf_destroy_consumed(buf);
2993}
Thu Nguyena5d18dc2024-08-07 08:29:34 +00002994
2995LIBPLDM_ABI_TESTING
Thu Nguyen02903032024-09-03 06:39:50 +00002996int decode_pldm_platform_cper_event(const void *event_data,
2997 size_t event_data_length,
2998 struct pldm_platform_cper_event *cper_event,
2999 size_t cper_event_length)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00003000{
3001 struct pldm_msgbuf _buf;
3002 struct pldm_msgbuf *buf = &_buf;
3003 int rc;
3004
3005 if (!cper_event || !event_data) {
3006 return -EINVAL;
3007 }
3008
3009 if (cper_event_length < sizeof(*cper_event)) {
3010 return -EINVAL;
3011 }
3012
3013 rc = pldm_msgbuf_init_errno(buf, PLDM_PLATFORM_CPER_EVENT_MIN_LENGTH,
3014 event_data, event_data_length);
3015 if (rc) {
3016 return rc;
3017 }
3018
3019 pldm_msgbuf_extract(buf, cper_event->format_version);
3020 rc = pldm_msgbuf_extract(buf, cper_event->format_type);
3021 if (rc) {
3022 return rc;
3023 }
3024 if (cper_event->format_type != PLDM_PLATFORM_CPER_EVENT_WITH_HEADER &&
3025 cper_event->format_type !=
3026 PLDM_PLATFORM_CPER_EVENT_WITHOUT_HEADER) {
3027 return -EPROTO;
3028 }
3029
3030 rc = pldm_msgbuf_extract(buf, cper_event->event_data_length);
3031 if (rc) {
3032 return rc;
3033 }
3034
3035 if (cper_event->event_data_length >
3036 (cper_event_length - sizeof(*cper_event))) {
3037 return -EOVERFLOW;
3038 }
3039
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00003040 rc = pldm_msgbuf_extract_array_uint8(
3041 buf, cper_event->event_data_length, cper_event->event_data,
3042 cper_event_length - sizeof(*cper_event));
3043 if (rc) {
3044 return rc;
3045 }
Thu Nguyena5d18dc2024-08-07 08:29:34 +00003046
3047 return pldm_msgbuf_destroy_consumed(buf);
3048}
3049
3050LIBPLDM_ABI_TESTING
3051uint8_t *
3052pldm_platform_cper_event_event_data(struct pldm_platform_cper_event *event)
3053{
3054 return event->event_data;
3055}