blob: 69403db605e0d0ece614e8bf2696c03f638e0bef [file] [log] [blame]
Andrew Jeffery7992eb82023-04-06 16:13:53 +09301#ifndef PLDM_MSGBUF_PLATFORM_H
2#define PLDM_MSGBUF_PLATFORM_H
3
4#include "../msgbuf.h"
5#include <libpldm/base.h>
6#include <libpldm/platform.h>
7
8static inline int
9pldm_msgbuf_extract_value_pdr_hdr(struct pldm_msgbuf *ctx,
10 struct pldm_value_pdr_hdr *hdr)
11{
12 pldm_msgbuf_extract(ctx, &hdr->record_handle);
13 pldm_msgbuf_extract(ctx, &hdr->version);
14 pldm_msgbuf_extract(ctx, &hdr->type);
15 pldm_msgbuf_extract(ctx, &hdr->record_change_num);
16 pldm_msgbuf_extract(ctx, &hdr->length);
17
18 return pldm_msgbuf_validate(ctx);
19}
20
21/*
22 * We use __attribute__((always_inline)) below so the compiler has visibility of
23 * the switch() at the call site. It is often the case that the size of multiple
24 * fields depends on the tag. Inlining thus gives the compiler visibility to
25 * hoist one tag-based code-path condition to cover all invocations.
26 */
27
28__attribute__((always_inline)) static inline int
29pldm_msgbuf_extract_sensor_data(struct pldm_msgbuf *ctx,
30 enum pldm_sensor_readings_data_type tag,
31 union_sensor_data_size *dst)
32{
33 switch (tag) {
34 case PLDM_SENSOR_DATA_SIZE_UINT8:
35 return pldm_msgbuf_extract(ctx, &dst->value_u8);
36 case PLDM_SENSOR_DATA_SIZE_SINT8:
37 return pldm_msgbuf_extract(ctx, &dst->value_s8);
38 case PLDM_SENSOR_DATA_SIZE_UINT16:
39 return pldm_msgbuf_extract(ctx, &dst->value_u16);
40 case PLDM_SENSOR_DATA_SIZE_SINT16:
41 return pldm_msgbuf_extract(ctx, &dst->value_s16);
42 case PLDM_SENSOR_DATA_SIZE_UINT32:
43 return pldm_msgbuf_extract(ctx, &dst->value_u32);
44 case PLDM_SENSOR_DATA_SIZE_SINT32:
45 return pldm_msgbuf_extract(ctx, &dst->value_s32);
46 }
47
48 return -PLDM_ERROR_INVALID_DATA;
49}
50
51__attribute__((always_inline)) static inline int
52pldm_msgbuf_extract_range_field_format(struct pldm_msgbuf *ctx,
53 enum pldm_range_field_format tag,
54 union_range_field_format *dst)
55{
56 switch (tag) {
57 case PLDM_RANGE_FIELD_FORMAT_UINT8:
58 return pldm_msgbuf_extract(ctx, &dst->value_u8);
59 case PLDM_RANGE_FIELD_FORMAT_SINT8:
60 return pldm_msgbuf_extract(ctx, &dst->value_s8);
61 case PLDM_RANGE_FIELD_FORMAT_UINT16:
62 return pldm_msgbuf_extract(ctx, &dst->value_u16);
63 case PLDM_RANGE_FIELD_FORMAT_SINT16:
64 return pldm_msgbuf_extract(ctx, &dst->value_s16);
65 case PLDM_RANGE_FIELD_FORMAT_UINT32:
66 return pldm_msgbuf_extract(ctx, &dst->value_u32);
67 case PLDM_RANGE_FIELD_FORMAT_SINT32:
68 return pldm_msgbuf_extract(ctx, &dst->value_s32);
69 case PLDM_RANGE_FIELD_FORMAT_REAL32:
70 return pldm_msgbuf_extract(ctx, &dst->value_f32);
71 }
72
73 return -PLDM_ERROR_INVALID_DATA;
74}
75
Andrew Jeffery3884c442023-04-12 11:13:24 +093076/* This API is bad, but it's because the caller's APIs are also bad */
77__attribute__((always_inline)) static inline int
78pldm_msgbuf_extract_effecter_value(struct pldm_msgbuf *ctx,
79 enum pldm_effecter_data_size tag,
80 uint8_t *val)
81{
82 switch (tag) {
83 case PLDM_EFFECTER_DATA_SIZE_UINT8:
84 return pldm_msgbuf_extract_uint8(ctx, (uint8_t *)val);
85 case PLDM_EFFECTER_DATA_SIZE_SINT8:
86 return pldm_msgbuf_extract_int8(ctx, (int8_t *)val);
87 case PLDM_EFFECTER_DATA_SIZE_UINT16:
88 return pldm_msgbuf_extract_uint16(ctx, (uint16_t *)val);
89 case PLDM_EFFECTER_DATA_SIZE_SINT16:
90 return pldm_msgbuf_extract_int16(ctx, (int16_t *)val);
91 case PLDM_EFFECTER_DATA_SIZE_UINT32:
92 return pldm_msgbuf_extract_uint32(ctx, (uint32_t *)val);
93 case PLDM_EFFECTER_DATA_SIZE_SINT32:
94 return pldm_msgbuf_extract_int32(ctx, (int32_t *)val);
95 }
96
97 return -PLDM_ERROR_INVALID_DATA;
98}
99
Andrew Jeffery7992eb82023-04-06 16:13:53 +0930100#endif