summaryrefslogtreecommitdiffstats
path: root/lib/protocol.c
blob: ac53d2b423ee91377eb0caa4136294e65da2396f (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
/* PipeWire AGL Cluster IPC
 *
 * Copyright © 2021 Collabora Ltd.
 *    @author Julian Bouzas <julian.bouzas@collabora.com>
 *
 * SPDX-License-Identifier: MIT
 */

#include "data.h"
#include "protocol.h"

enum {
        REPLY_CODE_ERROR = 0,
        REPLY_CODE_OK,
};

static bool is_reply(const uint8_t *buffer, size_t size, uint32_t code) {
        DataParser p;
        uint32_t parsed_code = 0;

        if (!data_parser_init(&p, buffer, size) ||
            !data_parser_get_id(&p, &parsed_code))
                return false;

        return parsed_code == code;
}

/* API */

size_t icipc_protocol_calculate_request_size(
                const char *name,
                const struct icipc_data *args) {
        assert (name);
        return sizeof(struct icipc_data) +
               data_type_string_calc_size(name) +
               (args ? data_type_raw_calc_size(args) : sizeof(struct icipc_data));
}

void icipc_protocol_build_request(
                uint8_t *buffer,
                size_t size,
                const char *name,
                const struct icipc_data *args) {
        DataBuilder b;
        const struct icipc_data none = { 0, DATA_TYPE_NONE };
        if (args == NULL)
                args = &none;

        data_builder_init(&b, buffer, size);
        data_builder_push_string(&b, name);
        data_builder_push_raw(&b, args);
}

bool icipc_protocol_parse_request(
                const uint8_t *buffer,
                size_t size,
                const char **name,
                const struct icipc_data **args) {
        DataParser p;
        const char *parsed_name = NULL;
        const struct icipc_data *parsed_args = NULL;

        if (!data_parser_init(&p, buffer, size) ||
            !data_parser_get_string(&p, &parsed_name) ||
            !data_parser_get_raw(&p, &parsed_args))
                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 icipc_data *value) {
        return sizeof(struct icipc_data) +
               data_type_id_calc_size() +
               (value ? data_type_raw_calc_size(value) : sizeof(struct icipc_data));
}

size_t icipc_protocol_calculate_reply_error_size(const char *msg) {
        assert (msg);
        return sizeof(struct icipc_data) +
               data_type_id_calc_size() +
               data_type_string_calc_size(msg);
}

void icipc_protocol_build_reply_ok(
                uint8_t *buffer,
                size_t size,
                const struct icipc_data *value) {
        DataBuilder b;
        const struct icipc_data none = { 0, DATA_TYPE_NONE };
        if (value == NULL)
                value = &none;

        data_builder_init(&b, buffer, size);
        data_builder_push_id(&b, REPLY_CODE_OK);
        data_builder_push_raw(&b, value);
}

void icipc_protocol_build_reply_error(
                uint8_t *buffer,
                size_t size,
                const char *msg) {
        DataBuilder b;
        data_builder_init(&b, buffer, size);
        data_builder_push_id(&b, REPLY_CODE_ERROR);
        data_builder_push_string(&b, msg);
}

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 icipc_data **value) {
        DataParser p;
        uint32_t parsed_code = 0;
        const struct icipc_data *parsed_value = NULL;

        if (!data_parser_init(&p, buffer, size) ||
            !data_parser_get_id(&p, &parsed_code) ||
            !data_parser_get_raw(&p, &parsed_value))
                return false;

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

bool icipc_protocol_parse_reply_error(
                const uint8_t *buffer,
                size_t size,
                const char **msg) {
        DataParser p;
        uint32_t parsed_code = 0;
        const char *parsed_msg = NULL;

        if (!data_parser_init(&p, buffer, size) ||
            !data_parser_get_id(&p, &parsed_code) ||
            !data_parser_get_string(&p, &parsed_msg))
                return false;

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