blob: 274ca4efdfbea8158a57ffd589014a2ca1cf67f3 [file] [log] [blame]
Patrick Williams691668f2023-11-01 08:19:10 -05001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10302#ifndef PLDM_MSGBUF_H
3#define PLDM_MSGBUF_H
4
John Chung7a8d9322025-08-27 20:56:19 -05005#include "msgbuf/core.h"
6
7#define PLDM__MSGBUF_DEFINE_P(name, mode) \
8 struct pldm_msgbuf_##mode _##name LIBPLDM_CC_CLEANUP( \
9 pldm__msgbuf_##mode##_cleanup) = { NULL, INTMAX_MIN }; \
10 struct pldm_msgbuf_##mode *(name) = &(_##name)
11
12#define PLDM_MSGBUF_RO_DEFINE_P(name) PLDM__MSGBUF_DEFINE_P(name, ro)
13#define PLDM_MSGBUF_RW_DEFINE_P(name) PLDM__MSGBUF_DEFINE_P(name, rw)
Andrew Jeffery860a43d2024-08-23 01:21:58 +000014
Andrew Jeffery66c77232024-04-24 11:42:02 +093015/*
John Chung7a8d9322025-08-27 20:56:19 -050016 * Use the C11 `_Generic` keyword to keep pldm_msgbuf related function consistent
17 * and to produce compile-time errors when the wrong pldm_msgbuf type is passed.
Andrew Jeffery66c77232024-04-24 11:42:02 +093018 *
John Chung7a8d9322025-08-27 20:56:19 -050019 * Previously we cast away `const` in `pldm_msgbuf_init_error()`, which was a hack.
20 * Instead, introduce:
21 * - pldm_msgbuf_ro: read-only buffer with a `const` cursor
22 * - pldm_msgbuf_rw: read-write buffer with a non-const cursor
Andrew Jeffery66c77232024-04-24 11:42:02 +093023 *
John Chung7a8d9322025-08-27 20:56:19 -050024 * `pldm_msgbuf_ro` is used by decode APIs to extract payloads into PLDM
25 * structures. `pldm_msgbuf_rw` is used by encode APIs to insert payloads from
26 * PLDM structures.
Andrew Jeffery66c77232024-04-24 11:42:02 +093027 */
28
John Chung7a8d9322025-08-27 20:56:19 -050029#define pldm_msgbuf_init_errno(ctx, minsize, buf, len) \
30 _Generic((ctx), \
31 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_init_errno, \
32 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_init_errno)( \
33 ctx, minsize, buf, len)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103034
John Chung7a8d9322025-08-27 20:56:19 -050035#define pldm_msgbuf_discard(ctx, error) \
36 _Generic((ctx), \
37 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_discard, \
38 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_discard)(ctx, error)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103039
John Chung7a8d9322025-08-27 20:56:19 -050040#define pldm_msgbuf_complete(ctx) \
41 _Generic((ctx), \
42 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_complete, \
43 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_complete)(ctx)
Andrew Jeffery66c77232024-04-24 11:42:02 +093044
John Chung7a8d9322025-08-27 20:56:19 -050045#define pldm_msgbuf_complete_consumed(ctx) \
46 _Generic((ctx), \
47 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_complete_consumed, \
48 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_complete_consumed)( \
49 ctx)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103050
John Chung7a8d9322025-08-27 20:56:19 -050051#define pldm_msgbuf_validate(ctx) \
52 _Generic((ctx), \
53 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_validate, \
54 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_validate)(ctx)
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +093055
John Chung7a8d9322025-08-27 20:56:19 -050056#define pldm_msgbuf_consumed(ctx) \
57 _Generic((ctx), \
58 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_consumed, \
59 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_consumed)(ctx)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103060
John Chung7a8d9322025-08-27 20:56:19 -050061#define pldm__msgbuf_invalidate(ctx) \
62 _Generic((ctx), \
63 struct pldm_msgbuf_rw *: pldm__msgbuf_rw_invalidate, \
64 struct pldm_msgbuf_ro *: pldm__msgbuf_ro_invalidate)(ctx)
Andrew Jefferya1896962025-03-03 21:41:25 +103065
John Chung7a8d9322025-08-27 20:56:19 -050066#define pldm_msgbuf_span_required(ctx, required, cursor) \
67 _Generic((ctx), \
68 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_span_required, \
69 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_span_required)( \
70 ctx, required, cursor)
Andrew Jefferya1896962025-03-03 21:41:25 +103071
John Chung7a8d9322025-08-27 20:56:19 -050072#define pldm_msgbuf_span_until(ctx, trailer, cursor, length) \
73 _Generic((ctx), \
74 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_span_until, \
75 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_span_until)( \
76 ctx, trailer, cursor, length)
Andrew Jefferya1896962025-03-03 21:41:25 +103077
John Chung7a8d9322025-08-27 20:56:19 -050078#define pldm_msgbuf_span_string_utf16(ctx, cursor, len) \
79 _Generic((ctx), \
80 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_span_string_utf16, \
81 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_span_string_utf16)( \
82 ctx, cursor, len)
Andrew Jefferya1896962025-03-03 21:41:25 +103083
John Chung7a8d9322025-08-27 20:56:19 -050084#define pldm_msgbuf_span_remaining(ctx, cursor, len) \
85 _Generic((ctx), \
86 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_span_remaining, \
87 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_span_remaining)( \
88 ctx, cursor, len)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103089
John Chung7a8d9322025-08-27 20:56:19 -050090#define pldm_msgbuf_span_string_ascii(ctx, cursor, len) \
91 _Generic((ctx), \
92 struct pldm_msgbuf_rw *: pldm_msgbuf_rw_span_string_ascii, \
93 struct pldm_msgbuf_ro *: pldm_msgbuf_ro_span_string_ascii)( \
94 ctx, cursor, len)
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +093095
Andrew Jeffery66c77232024-04-24 11:42:02 +093096#define pldm_msgbuf_extract_typecheck(ty, fn, dst, ...) \
97 (pldm_require_obj_type(dst, ty), fn(__VA_ARGS__))
Andrew Jeffery66c77232024-04-24 11:42:02 +093098
Andrew Jeffery66c77232024-04-24 11:42:02 +093099#define pldm_msgbuf_extract_uint8(ctx, dst) \
100 pldm_msgbuf_extract_typecheck(uint8_t, pldm__msgbuf_extract_uint8, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930101 dst, ctx, (void *)&(dst))
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030102
Andrew Jeffery66c77232024-04-24 11:42:02 +0930103#define pldm_msgbuf_extract_int8(ctx, dst) \
104 pldm_msgbuf_extract_typecheck(int8_t, pldm__msgbuf_extract_int8, dst, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930105 ctx, (void *)&(dst))
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030106
Andrew Jeffery66c77232024-04-24 11:42:02 +0930107#define pldm_msgbuf_extract_uint16(ctx, dst) \
108 pldm_msgbuf_extract_typecheck(uint16_t, pldm__msgbuf_extract_uint16, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930109 dst, ctx, (void *)&(dst))
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030110
Andrew Jeffery66c77232024-04-24 11:42:02 +0930111#define pldm_msgbuf_extract_int16(ctx, dst) \
112 pldm_msgbuf_extract_typecheck(int16_t, pldm__msgbuf_extract_int16, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930113 dst, ctx, (void *)&(dst))
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030114
Andrew Jeffery66c77232024-04-24 11:42:02 +0930115#define pldm_msgbuf_extract_uint32(ctx, dst) \
116 pldm_msgbuf_extract_typecheck(uint32_t, pldm__msgbuf_extract_uint32, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930117 dst, ctx, (void *)&(dst))
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030118
Andrew Jeffery66c77232024-04-24 11:42:02 +0930119#define pldm_msgbuf_extract_int32(ctx, dst) \
120 pldm_msgbuf_extract_typecheck(int32_t, pldm__msgbuf_extract_int32, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930121 dst, ctx, (void *)&(dst))
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030122
Andrew Jeffery66c77232024-04-24 11:42:02 +0930123#define pldm_msgbuf_extract_real32(ctx, dst) \
124 pldm_msgbuf_extract_typecheck(real32_t, pldm__msgbuf_extract_real32, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930125 dst, ctx, (void *)&(dst))
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030126
Andrew Jeffery66c77232024-04-24 11:42:02 +0930127/**
128 * Extract the field at the msgbuf cursor into the lvalue named by dst.
129 *
130 * @param ctx The msgbuf context object
131 * @param dst The lvalue into which the field at the msgbuf cursor should be
132 * extracted
133 *
134 * @return PLDM_SUCCESS on success, otherwise another value on error
135 */
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030136#define pldm_msgbuf_extract(ctx, dst) \
Andrew Jeffery66c77232024-04-24 11:42:02 +0930137 _Generic((dst), \
138 uint8_t: pldm__msgbuf_extract_uint8, \
139 int8_t: pldm__msgbuf_extract_int8, \
140 uint16_t: pldm__msgbuf_extract_uint16, \
141 int16_t: pldm__msgbuf_extract_int16, \
142 uint32_t: pldm__msgbuf_extract_uint32, \
143 int32_t: pldm__msgbuf_extract_int32, \
144 real32_t: pldm__msgbuf_extract_real32)(ctx, (void *)&(dst))
145
146/**
147 * Extract the field at the msgbuf cursor into the object pointed-to by dst.
148 *
149 * @param ctx The msgbuf context object
150 * @param dst The pointer to the object into which the field at the msgbuf
151 * cursor should be extracted
152 *
153 * @return PLDM_SUCCESS on success, otherwise another value on error
154 */
155#define pldm_msgbuf_extract_p(ctx, dst) \
156 _Generic((dst), \
157 uint8_t *: pldm__msgbuf_extract_uint8, \
158 int8_t *: pldm__msgbuf_extract_int8, \
159 uint16_t *: pldm__msgbuf_extract_uint16, \
160 int16_t *: pldm__msgbuf_extract_int16, \
161 uint32_t *: pldm__msgbuf_extract_uint32, \
162 int32_t *: pldm__msgbuf_extract_int32, \
163 real32_t *: pldm__msgbuf_extract_real32)(ctx, dst)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030164
Thu Nguyen062c8762023-04-22 20:45:04 +0700165#define pldm_msgbuf_insert(dst, src) \
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930166 _Generic((src), \
167 uint8_t: pldm_msgbuf_insert_uint8, \
168 int8_t: pldm_msgbuf_insert_int8, \
169 uint16_t: pldm_msgbuf_insert_uint16, \
170 int16_t: pldm_msgbuf_insert_int16, \
171 uint32_t: pldm_msgbuf_insert_uint32, \
Matt Johnstone8d8d332024-10-28 17:13:32 +0800172 int32_t: pldm_msgbuf_insert_int32, \
173 uint64_t: pldm_msgbuf_insert_uint64)(dst, src)
Thu Nguyen062c8762023-04-22 20:45:04 +0700174
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000175/**
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000176 * Insert an array of data into the msgbuf instance
177 *
178 * @param ctx - The msgbuf instance into which the array of data should be
179 * inserted
180 * @param count - The number of array elements to insert
181 * @param src - The array object from which elements should be inserted into
182 @p ctx
183 * @param src_count - The maximum number of elements to insert from @p src
184 *
185 * Note that both @p count and @p src_count can only be counted by `sizeof` for
186 * arrays where `sizeof(*dst) == 1` holds. Specifically, they count the number
187 * of array elements and _not_ the object size of the array.
188 */
189#define pldm_msgbuf_insert_array(dst, count, src, src_count) \
Andrew Jeffery1c571442024-07-08 10:25:48 +0930190 _Generic((*(src)), \
191 uint8_t: pldm_msgbuf_insert_array_uint8, \
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000192 char: pldm_msgbuf_insert_array_char)(dst, count, src, \
193 src_count)
Thu Nguyen062c8762023-04-22 20:45:04 +0700194
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030195#endif /* BUF_H */