summaryrefslogtreecommitdiffstats
path: root/lib/protocol.c
blob: 35a9485affb0f33e6ee7dee91bca9a07ed012029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* PipeWire AGL Cluster IPC
 *
 * Copyright © 2021 Collabora Ltd.
 *    @author Julian Bouzas <julian.bouzas@collabora.com>
 *
 * SPDX-License-Identifier: MIT
 */

#include <assert.h>

#include <spa/pod/builder.h>
#include <spa/pod/parser.h>

#include "protocol.h"

#define SIZE_PADDING 128

enum {
        REPLY_CODE_ERROR = 0,
        REPLY_CODE_OK,
};

static bool is_reply(const uint8_t *buffer, size_t size, int code) {
        const struct spa_pod *pod = (const struct spa_pod *)buffer;
        struct spa_pod_parser p;
        struct spa_pod_frame f;
        int parsed_code = 0;

        /* check if struct */
        if (!spa_pod_is_struct(pod))
                return false;

        /* parse */
        spa_pod_parser_pod(&p, pod);
        spa_pod_parser_push_struct(&p, &f);
        spa_pod_parser_get_int(&p, &parsed_code);

        return parsed_code == code;
}

/* API */

size_t icipc_protocol_calculate_request_size(
                const char *name,
                const struct spa_pod *args) {
        assert (name);
        return strlen(name) + (args ? SPA_POD_SIZE(args) : 8) + SIZE_PADDING;
}

void icipc_protocol_build_request(
                uint8_t *buffer,
                size_t size,
                const char *name,
                const struct spa_pod *args) {
        const struct spa_pod none = SPA_POD_INIT_None();
        struct spa_pod_builder b;
        struct spa_pod_frame f;

        memset(buffer, 0, size);

        if (args == NULL)
                args = &none;

        spa_pod_builder_init(&b, buffer, size);
        spa_pod_builder_push_struct(&b, &f);
        spa_pod_builder_string(&b, name);
        spa_pod_builder_primitive (&b, args);
        spa_pod_builder_pop(&b, &f);
}

bool icipc_protocol_parse_request(
                const uint8_t *buffer,
                size_t size,
                const char **name,
                const struct spa_pod **args) {
        const struct spa_pod *pod = (const struct spa_pod *)buffer;
        struct spa_pod_parser p;
        struct spa_pod_frame f;
        const char *parsed_name = NULL;
        struct spa_pod *parsed_args = NULL;

        /* check if struct */
        if (!spa_pod_is_struct(pod))
                return false;

        /* parse */
        spa_pod_parser_pod(&p, pod);
        spa_pod_parser_push_struct(&p, &f);
        spa_pod_parser_get_string(&p, &parsed_name);
        spa_pod_parser_get_pod(&p, &parsed_args);
        spa_pod_parser_pop(&p, &f);

        /* check name and args */
        if (name == NULL || args == NULL)
                return false;

        if (name != NULL)
                *name = parsed_name;
        if (args != NULL)
                *args = parsed_args;
        return true;
}

size_t icipc_protocol_calculate_reply_ok_size(const struct spa_pod *value) {
        return (value ? SPA_POD_SIZE(value) : 8) + SIZE_PADDING;
}

size_t icipc_protocol_calculate_reply_error_size(const char *msg) {
        assert (msg);
        return strlen(msg) + SIZE_PADDING;
}

void icipc_protocol_build_reply_ok(
                uint8_t *buffer,
                size_t size,
                const struct spa_pod *value) {
        const struct spa_pod none = SPA_POD_INIT_None();
        struct spa_pod_builder b;
        struct spa_pod_frame f;

        memset(buffer, 0, size);

        if (value == NULL)
                value = &none;

        spa_pod_builder_init(&b, buffer, size);
        spa_pod_builder_push_struct(&b, &f);
        spa_pod_builder_int(&b, REPLY_CODE_OK);
        spa_pod_builder_primitive(&b, value);
        spa_pod_builder_pop(&b, &f);
}

void icipc_protocol_build_reply_error(
                uint8_t *buffer,
                size_t size,
                const char *msg) {
        struct spa_pod_builder b;
        struct spa_pod_frame f;

        memset(buffer, 0, size);

        spa_pod_builder_init(&b, buffer, size);
        spa_pod_builder_push_struct(&b, &f);
        spa_pod_builder_int(&b, REPLY_CODE_ERROR);
        spa_pod_builder_string (&b, msg);
        spa_pod_builder_pop(&b, &f);
}

bool icipc_protocol_is_reply_ok(const uint8_t *buffer, size_t size) {
        return is_reply (buffer, size, REPLY_CODE_OK);
}

bool icipc_protocol_is_reply_error(const uint8_t *buffer, size_t size) {
        return is_reply (buffer, size, REPLY_CODE_ERROR);
}

bool icipc_protocol_parse_reply_ok (
                const uint8_t *buffer,
                size_t size,
                const struct spa_pod **value) {
        const struct spa_pod *pod = (const struct spa_pod *)buffer;
        struct spa_pod_parser p;
        struct spa_pod_frame f;
        int parsed_code = 0;
        struct spa_pod *parsed_value = NULL;

        /* check if struct */
        if (!spa_pod_is_struct(pod))
                return false;

        /* parse */
        spa_pod_parser_pod(&p, pod);
        spa_pod_parser_push_struct(&p, &f);
        spa_pod_parser_get_int(&p, &parsed_code);
        spa_pod_parser_get_pod(&p, &parsed_value);
        spa_pod_parser_pop(&p, &f);

        if (value != NULL)
                *value = parsed_value;
        return true;
}

bool icipc_protocol_parse_reply_error (
                const uint8_t *buffer,
                size_t size,
                const char **msg) {
        const struct spa_pod *pod = (const struct spa_pod *)buffer;
        struct spa_pod_parser p;
        struct spa_pod_frame f;
        int parsed_code = 0;
        const char *parsed_msg = NULL;

        /* check if struct */
        if (!spa_pod_is_struct(pod))
                return false;

        /* parse */
        spa_pod_parser_pod(&p, pod);
        spa_pod_parser_push_struct(&p, &f);
        spa_pod_parser_get_int(&p, &parsed_code);
        spa_pod_parser_get_string(&p, &parsed_msg);
        spa_pod_parser_pop(&p, &f);

        if (msg != NULL)
                *msg = parsed_msg;
        return true;
}