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