blob: 11f0900bcbf191a8ac91e3ef9a76bb952aceeb97 [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
Andrew Jefferycb569bc2024-09-01 09:38:09 +03005#include "../compiler.h"
Andrew Jeffery7992eb82023-04-06 16:13:53 +09306#include "../msgbuf.h"
7#include <libpldm/base.h>
8#include <libpldm/platform.h>
9
10static inline int
11pldm_msgbuf_extract_value_pdr_hdr(struct pldm_msgbuf *ctx,
12 struct pldm_value_pdr_hdr *hdr)
13{
Andrew Jeffery66c77232024-04-24 11:42:02 +093014 pldm_msgbuf_extract(ctx, hdr->record_handle);
15 pldm_msgbuf_extract(ctx, hdr->version);
16 pldm_msgbuf_extract(ctx, hdr->type);
17 pldm_msgbuf_extract(ctx, hdr->record_change_num);
18 pldm_msgbuf_extract(ctx, hdr->length);
Andrew Jeffery7992eb82023-04-06 16:13:53 +093019
20 return pldm_msgbuf_validate(ctx);
21}
22
Andrew Jefferycb569bc2024-09-01 09:38:09 +030023LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery7992eb82023-04-06 16:13:53 +093024pldm_msgbuf_extract_sensor_data(struct pldm_msgbuf *ctx,
25 enum pldm_sensor_readings_data_type tag,
26 union_sensor_data_size *dst)
27{
28 switch (tag) {
29 case PLDM_SENSOR_DATA_SIZE_UINT8:
Andrew Jeffery66c77232024-04-24 11:42:02 +093030 return pldm_msgbuf_extract(ctx, dst->value_u8);
Andrew Jeffery7992eb82023-04-06 16:13:53 +093031 case PLDM_SENSOR_DATA_SIZE_SINT8:
Andrew Jeffery66c77232024-04-24 11:42:02 +093032 return pldm_msgbuf_extract(ctx, dst->value_s8);
Andrew Jeffery7992eb82023-04-06 16:13:53 +093033 case PLDM_SENSOR_DATA_SIZE_UINT16:
Andrew Jeffery66c77232024-04-24 11:42:02 +093034 return pldm_msgbuf_extract(ctx, dst->value_u16);
Andrew Jeffery7992eb82023-04-06 16:13:53 +093035 case PLDM_SENSOR_DATA_SIZE_SINT16:
Andrew Jeffery66c77232024-04-24 11:42:02 +093036 return pldm_msgbuf_extract(ctx, dst->value_s16);
Andrew Jeffery7992eb82023-04-06 16:13:53 +093037 case PLDM_SENSOR_DATA_SIZE_UINT32:
Andrew Jeffery66c77232024-04-24 11:42:02 +093038 return pldm_msgbuf_extract(ctx, dst->value_u32);
Andrew Jeffery7992eb82023-04-06 16:13:53 +093039 case PLDM_SENSOR_DATA_SIZE_SINT32:
Andrew Jeffery66c77232024-04-24 11:42:02 +093040 return pldm_msgbuf_extract(ctx, dst->value_s32);
Andrew Jeffery7992eb82023-04-06 16:13:53 +093041 }
42
43 return -PLDM_ERROR_INVALID_DATA;
44}
45
Andrew Jeffery840b1402023-04-13 23:54:44 +093046/*
47 * This API is bad, but it's because the caller's APIs are also bad. They should
48 * have used the approach used by callers of pldm_msgbuf_extract_sensor_data()
49 * above
50 */
Andrew Jefferycb569bc2024-09-01 09:38:09 +030051LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery840b1402023-04-13 23:54:44 +093052pldm_msgbuf_extract_sensor_value(struct pldm_msgbuf *ctx,
Thu Nguyencd07fec2023-11-10 16:15:09 +070053 enum pldm_sensor_readings_data_type tag,
Andrew Jeffery66c77232024-04-24 11:42:02 +093054 void *val)
Andrew Jeffery840b1402023-04-13 23:54:44 +093055{
56 switch (tag) {
57 case PLDM_SENSOR_DATA_SIZE_UINT8:
Andrew Jeffery66c77232024-04-24 11:42:02 +093058 return pldm__msgbuf_extract_uint8(ctx, val);
Andrew Jeffery840b1402023-04-13 23:54:44 +093059 case PLDM_SENSOR_DATA_SIZE_SINT8:
Andrew Jeffery66c77232024-04-24 11:42:02 +093060 return pldm__msgbuf_extract_int8(ctx, val);
Andrew Jeffery840b1402023-04-13 23:54:44 +093061 case PLDM_SENSOR_DATA_SIZE_UINT16:
Andrew Jeffery66c77232024-04-24 11:42:02 +093062 return pldm__msgbuf_extract_uint16(ctx, val);
Andrew Jeffery840b1402023-04-13 23:54:44 +093063 case PLDM_SENSOR_DATA_SIZE_SINT16:
Andrew Jeffery66c77232024-04-24 11:42:02 +093064 return pldm__msgbuf_extract_int16(ctx, val);
Andrew Jeffery840b1402023-04-13 23:54:44 +093065 case PLDM_SENSOR_DATA_SIZE_UINT32:
Andrew Jeffery66c77232024-04-24 11:42:02 +093066 return pldm__msgbuf_extract_uint32(ctx, val);
Andrew Jeffery840b1402023-04-13 23:54:44 +093067 case PLDM_SENSOR_DATA_SIZE_SINT32:
Andrew Jeffery66c77232024-04-24 11:42:02 +093068 return pldm__msgbuf_extract_int32(ctx, val);
Andrew Jeffery840b1402023-04-13 23:54:44 +093069 }
70
71 return -PLDM_ERROR_INVALID_DATA;
72}
73
Andrew Jeffery66c77232024-04-24 11:42:02 +093074#define pldm_msgbuf_extract_range_field_format(ctx, tag, dst) \
75 pldm_msgbuf_extract_typecheck(union_range_field_format, \
76 pldm__msgbuf_extract_range_field_format, \
77 dst, ctx, tag, (void *)&(dst))
Andrew Jefferycb569bc2024-09-01 09:38:09 +030078LIBPLDM_CC_ALWAYS_INLINE int pldm__msgbuf_extract_range_field_format(
79 struct pldm_msgbuf *ctx, enum pldm_range_field_format tag, void *rff)
Andrew Jeffery7992eb82023-04-06 16:13:53 +093080{
81 switch (tag) {
82 case PLDM_RANGE_FIELD_FORMAT_UINT8:
Andrew Jeffery66c77232024-04-24 11:42:02 +093083 return pldm__msgbuf_extract_uint8(
84 ctx, ((char *)rff) + offsetof(union_range_field_format,
85 value_u8));
Andrew Jeffery7992eb82023-04-06 16:13:53 +093086 case PLDM_RANGE_FIELD_FORMAT_SINT8:
Andrew Jeffery66c77232024-04-24 11:42:02 +093087 return pldm__msgbuf_extract_int8(
88 ctx, ((char *)rff) + offsetof(union_range_field_format,
89 value_s8));
Andrew Jeffery7992eb82023-04-06 16:13:53 +093090 case PLDM_RANGE_FIELD_FORMAT_UINT16:
Andrew Jeffery66c77232024-04-24 11:42:02 +093091 return pldm__msgbuf_extract_uint16(
92 ctx, ((char *)rff) + offsetof(union_range_field_format,
93 value_u16));
Andrew Jeffery7992eb82023-04-06 16:13:53 +093094 case PLDM_RANGE_FIELD_FORMAT_SINT16:
Andrew Jeffery66c77232024-04-24 11:42:02 +093095 return pldm__msgbuf_extract_int16(
96 ctx, ((char *)rff) + offsetof(union_range_field_format,
97 value_s16));
Andrew Jeffery7992eb82023-04-06 16:13:53 +093098 case PLDM_RANGE_FIELD_FORMAT_UINT32:
Andrew Jeffery66c77232024-04-24 11:42:02 +093099 return pldm__msgbuf_extract_uint32(
100 ctx, ((char *)rff) + offsetof(union_range_field_format,
101 value_u32));
Andrew Jeffery7992eb82023-04-06 16:13:53 +0930102 case PLDM_RANGE_FIELD_FORMAT_SINT32:
Andrew Jeffery66c77232024-04-24 11:42:02 +0930103 return pldm__msgbuf_extract_int32(
104 ctx, ((char *)rff) + offsetof(union_range_field_format,
105 value_s32));
Andrew Jeffery7992eb82023-04-06 16:13:53 +0930106 case PLDM_RANGE_FIELD_FORMAT_REAL32:
Andrew Jeffery66c77232024-04-24 11:42:02 +0930107 return pldm__msgbuf_extract_real32(
108 ctx, ((char *)rff) + offsetof(union_range_field_format,
109 value_f32));
Andrew Jeffery7992eb82023-04-06 16:13:53 +0930110 }
111
112 return -PLDM_ERROR_INVALID_DATA;
113}
114
Andrew Jeffery3884c442023-04-12 11:13:24 +0930115/* This API is bad, but it's because the caller's APIs are also bad */
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300116LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery3884c442023-04-12 11:13:24 +0930117pldm_msgbuf_extract_effecter_value(struct pldm_msgbuf *ctx,
Andrew Jeffery66c77232024-04-24 11:42:02 +0930118 enum pldm_effecter_data_size tag, void *dst)
Andrew Jeffery3884c442023-04-12 11:13:24 +0930119{
120 switch (tag) {
121 case PLDM_EFFECTER_DATA_SIZE_UINT8:
Andrew Jeffery66c77232024-04-24 11:42:02 +0930122 return pldm__msgbuf_extract_uint8(ctx, dst);
Andrew Jeffery3884c442023-04-12 11:13:24 +0930123 case PLDM_EFFECTER_DATA_SIZE_SINT8:
Andrew Jeffery66c77232024-04-24 11:42:02 +0930124 return pldm__msgbuf_extract_int8(ctx, dst);
Andrew Jeffery3884c442023-04-12 11:13:24 +0930125 case PLDM_EFFECTER_DATA_SIZE_UINT16:
Andrew Jeffery66c77232024-04-24 11:42:02 +0930126 return pldm__msgbuf_extract_uint16(ctx, dst);
Andrew Jeffery3884c442023-04-12 11:13:24 +0930127 case PLDM_EFFECTER_DATA_SIZE_SINT16:
Andrew Jeffery66c77232024-04-24 11:42:02 +0930128 return pldm__msgbuf_extract_int16(ctx, dst);
Andrew Jeffery3884c442023-04-12 11:13:24 +0930129 case PLDM_EFFECTER_DATA_SIZE_UINT32:
Andrew Jeffery66c77232024-04-24 11:42:02 +0930130 return pldm__msgbuf_extract_uint32(ctx, dst);
Andrew Jeffery3884c442023-04-12 11:13:24 +0930131 case PLDM_EFFECTER_DATA_SIZE_SINT32:
Andrew Jeffery66c77232024-04-24 11:42:02 +0930132 return pldm__msgbuf_extract_int32(ctx, dst);
Andrew Jeffery3884c442023-04-12 11:13:24 +0930133 }
134
135 return -PLDM_ERROR_INVALID_DATA;
136}
137
Thu Nguyend4878cd2023-11-09 10:18:33 +0700138#define pldm_msgbuf_extract_effecter_data(ctx, tag, dst) \
139 pldm_msgbuf_extract_typecheck(union_effecter_data_size, \
140 pldm__msgbuf_extract_range_field_format, \
141 dst, ctx, tag, (void *)&(dst))
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300142LIBPLDM_CC_ALWAYS_INLINE int
Thu Nguyend4878cd2023-11-09 10:18:33 +0700143pldm__msgbuf_extract_effecter_data(struct pldm_msgbuf *ctx,
144 enum pldm_effecter_data_size tag, void *ed)
145{
146 switch (tag) {
147 case PLDM_EFFECTER_DATA_SIZE_UINT8:
148 return pldm__msgbuf_extract_uint8(
149 ctx, ((char *)ed) + offsetof(union_effecter_data_size,
150 value_u8));
151 case PLDM_EFFECTER_DATA_SIZE_SINT8:
152 return pldm__msgbuf_extract_int8(
153 ctx, ((char *)ed) + offsetof(union_effecter_data_size,
154 value_s8));
155 case PLDM_EFFECTER_DATA_SIZE_UINT16:
156 return pldm__msgbuf_extract_uint16(
157 ctx, ((char *)ed) + offsetof(union_effecter_data_size,
158 value_u16));
159 case PLDM_EFFECTER_DATA_SIZE_SINT16:
160 return pldm__msgbuf_extract_int16(
161 ctx, ((char *)ed) + offsetof(union_effecter_data_size,
162 value_s16));
163 case PLDM_EFFECTER_DATA_SIZE_UINT32:
164 return pldm__msgbuf_extract_uint32(
165 ctx, ((char *)ed) + offsetof(union_effecter_data_size,
166 value_u32));
167 case PLDM_EFFECTER_DATA_SIZE_SINT32:
168 return pldm__msgbuf_extract_int32(
169 ctx, ((char *)ed) + offsetof(union_effecter_data_size,
170 value_s32));
171 }
172
173 return -PLDM_ERROR_INVALID_DATA;
174}
175
Andrew Jeffery66c77232024-04-24 11:42:02 +0930176#ifdef __cplusplus
177#include <type_traits>
178
179template <typename T>
180static inline int pldm_msgbuf_typecheck_range_field_format(
181 struct pldm_msgbuf *ctx, enum pldm_range_field_format tag, void *_rff)
182{
183 static_assert(std::is_same<union_range_field_format, T>::value);
184 return pldm__msgbuf_extract_range_field_format(ctx, tag, _rff);
185}
186#endif
187
Andrew Jeffery7992eb82023-04-06 16:13:53 +0930188#endif