blob: 3fbe077ce06f5202bf006dc0b8ebc996735b512b [file] [log] [blame]
Patrick Williams691668f2023-11-01 08:19:10 -05001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Andrew Jeffery7992eb82023-04-06 16:13:53 +09302#ifndef PLDM_MSGBUF_PLATFORM_H
3#define PLDM_MSGBUF_PLATFORM_H
4
5#include "../msgbuf.h"
6#include <libpldm/base.h>
7#include <libpldm/platform.h>
8
9static inline int
10pldm_msgbuf_extract_value_pdr_hdr(struct pldm_msgbuf *ctx,
11 struct pldm_value_pdr_hdr *hdr)
12{
13 pldm_msgbuf_extract(ctx, &hdr->record_handle);
14 pldm_msgbuf_extract(ctx, &hdr->version);
15 pldm_msgbuf_extract(ctx, &hdr->type);
16 pldm_msgbuf_extract(ctx, &hdr->record_change_num);
17 pldm_msgbuf_extract(ctx, &hdr->length);
18
19 return pldm_msgbuf_validate(ctx);
20}
21
22/*
23 * We use __attribute__((always_inline)) below so the compiler has visibility of
24 * the switch() at the call site. It is often the case that the size of multiple
25 * fields depends on the tag. Inlining thus gives the compiler visibility to
26 * hoist one tag-based code-path condition to cover all invocations.
27 */
28
29__attribute__((always_inline)) static inline int
30pldm_msgbuf_extract_sensor_data(struct pldm_msgbuf *ctx,
31 enum pldm_sensor_readings_data_type tag,
32 union_sensor_data_size *dst)
33{
34 switch (tag) {
35 case PLDM_SENSOR_DATA_SIZE_UINT8:
36 return pldm_msgbuf_extract(ctx, &dst->value_u8);
37 case PLDM_SENSOR_DATA_SIZE_SINT8:
38 return pldm_msgbuf_extract(ctx, &dst->value_s8);
39 case PLDM_SENSOR_DATA_SIZE_UINT16:
40 return pldm_msgbuf_extract(ctx, &dst->value_u16);
41 case PLDM_SENSOR_DATA_SIZE_SINT16:
42 return pldm_msgbuf_extract(ctx, &dst->value_s16);
43 case PLDM_SENSOR_DATA_SIZE_UINT32:
44 return pldm_msgbuf_extract(ctx, &dst->value_u32);
45 case PLDM_SENSOR_DATA_SIZE_SINT32:
46 return pldm_msgbuf_extract(ctx, &dst->value_s32);
47 }
48
49 return -PLDM_ERROR_INVALID_DATA;
50}
51
Andrew Jeffery840b1402023-04-13 23:54:44 +093052/*
53 * This API is bad, but it's because the caller's APIs are also bad. They should
54 * have used the approach used by callers of pldm_msgbuf_extract_sensor_data()
55 * above
56 */
57__attribute__((always_inline)) static inline int
58pldm_msgbuf_extract_sensor_value(struct pldm_msgbuf *ctx,
59 enum pldm_effecter_data_size tag, uint8_t *val)
60{
61 switch (tag) {
62 case PLDM_SENSOR_DATA_SIZE_UINT8:
63 return pldm_msgbuf_extract_uint8(ctx, (uint8_t *)val);
64 case PLDM_SENSOR_DATA_SIZE_SINT8:
65 return pldm_msgbuf_extract_int8(ctx, (int8_t *)val);
66 case PLDM_SENSOR_DATA_SIZE_UINT16:
67 return pldm_msgbuf_extract_uint16(ctx, (uint16_t *)val);
68 case PLDM_SENSOR_DATA_SIZE_SINT16:
69 return pldm_msgbuf_extract_int16(ctx, (int16_t *)val);
70 case PLDM_SENSOR_DATA_SIZE_UINT32:
71 return pldm_msgbuf_extract_uint32(ctx, (uint32_t *)val);
72 case PLDM_SENSOR_DATA_SIZE_SINT32:
73 return pldm_msgbuf_extract_int32(ctx, (int32_t *)val);
74 }
75
76 return -PLDM_ERROR_INVALID_DATA;
77}
78
Andrew Jeffery7992eb82023-04-06 16:13:53 +093079__attribute__((always_inline)) static inline int
80pldm_msgbuf_extract_range_field_format(struct pldm_msgbuf *ctx,
81 enum pldm_range_field_format tag,
82 union_range_field_format *dst)
83{
84 switch (tag) {
85 case PLDM_RANGE_FIELD_FORMAT_UINT8:
86 return pldm_msgbuf_extract(ctx, &dst->value_u8);
87 case PLDM_RANGE_FIELD_FORMAT_SINT8:
88 return pldm_msgbuf_extract(ctx, &dst->value_s8);
89 case PLDM_RANGE_FIELD_FORMAT_UINT16:
90 return pldm_msgbuf_extract(ctx, &dst->value_u16);
91 case PLDM_RANGE_FIELD_FORMAT_SINT16:
92 return pldm_msgbuf_extract(ctx, &dst->value_s16);
93 case PLDM_RANGE_FIELD_FORMAT_UINT32:
94 return pldm_msgbuf_extract(ctx, &dst->value_u32);
95 case PLDM_RANGE_FIELD_FORMAT_SINT32:
96 return pldm_msgbuf_extract(ctx, &dst->value_s32);
97 case PLDM_RANGE_FIELD_FORMAT_REAL32:
98 return pldm_msgbuf_extract(ctx, &dst->value_f32);
99 }
100
101 return -PLDM_ERROR_INVALID_DATA;
102}
103
Andrew Jeffery3884c442023-04-12 11:13:24 +0930104/* This API is bad, but it's because the caller's APIs are also bad */
105__attribute__((always_inline)) static inline int
106pldm_msgbuf_extract_effecter_value(struct pldm_msgbuf *ctx,
107 enum pldm_effecter_data_size tag,
108 uint8_t *val)
109{
110 switch (tag) {
111 case PLDM_EFFECTER_DATA_SIZE_UINT8:
112 return pldm_msgbuf_extract_uint8(ctx, (uint8_t *)val);
113 case PLDM_EFFECTER_DATA_SIZE_SINT8:
114 return pldm_msgbuf_extract_int8(ctx, (int8_t *)val);
115 case PLDM_EFFECTER_DATA_SIZE_UINT16:
116 return pldm_msgbuf_extract_uint16(ctx, (uint16_t *)val);
117 case PLDM_EFFECTER_DATA_SIZE_SINT16:
118 return pldm_msgbuf_extract_int16(ctx, (int16_t *)val);
119 case PLDM_EFFECTER_DATA_SIZE_UINT32:
120 return pldm_msgbuf_extract_uint32(ctx, (uint32_t *)val);
121 case PLDM_EFFECTER_DATA_SIZE_SINT32:
122 return pldm_msgbuf_extract_int32(ctx, (int32_t *)val);
123 }
124
125 return -PLDM_ERROR_INVALID_DATA;
126}
127
Andrew Jeffery7992eb82023-04-06 16:13:53 +0930128#endif