Commit 9a17484d authored by Lennart Poettering's avatar Lennart Poettering

bus: implement demarshaller

parent 5407f2de
This diff is collapsed.
......@@ -34,6 +34,7 @@ struct bus_container {
unsigned index;
uint32_t *array_size;
size_t begin;
};
_packed_ struct bus_header {
......@@ -77,14 +78,18 @@ struct sd_bus_message {
void *fields;
void *body;
size_t rindex;
uint32_t n_fds;
int *fds;
struct bus_container root_container, *sub_containers;
struct bus_container root_container, *containers;
unsigned n_containers;
struct iovec iovec[4];
unsigned n_iovec;
char *peeked_signature;
};
#if __BYTE_ORDER == __BIG_ENDIAN
......@@ -93,20 +98,28 @@ struct sd_bus_message {
#define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != SD_BUS_LITTLE_ENDIAN)
#endif
static inline uint32_t BUS_MESSAGE_BSWAP(sd_bus_message *m, uint32_t u) {
static inline uint16_t BUS_MESSAGE_BSWAP16(sd_bus_message *m, uint16_t u) {
return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_16(u) : u;
}
static inline uint32_t BUS_MESSAGE_BSWAP32(sd_bus_message *m, uint32_t u) {
return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_32(u) : u;
}
static inline uint64_t BUS_MESSAGE_BSWAP64(sd_bus_message *m, uint64_t u) {
return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_64(u) : u;
}
static inline uint32_t BUS_MESSAGE_SERIAL(sd_bus_message *m) {
return BUS_MESSAGE_BSWAP(m, m->header->serial);
return BUS_MESSAGE_BSWAP32(m, m->header->serial);
}
static inline uint32_t BUS_MESSAGE_BODY_SIZE(sd_bus_message *m) {
return BUS_MESSAGE_BSWAP(m, m->header->body_size);
return BUS_MESSAGE_BSWAP32(m, m->header->body_size);
}
static inline uint32_t BUS_MESSAGE_FIELDS_SIZE(sd_bus_message *m) {
return BUS_MESSAGE_BSWAP(m, m->header->fields_size);
return BUS_MESSAGE_BSWAP32(m, m->header->fields_size);
}
static inline void bus_message_unrefp(sd_bus_message **m) {
......@@ -115,7 +128,7 @@ static inline void bus_message_unrefp(sd_bus_message **m) {
#define _cleanup_bus_message_unref_ __attribute__((cleanup(bus_message_unrefp)))
int message_parse(sd_bus_message *m);
int message_seal(sd_bus_message *m, uint64_t serial);
void message_dump(sd_bus_message *m);
int bus_message_parse(sd_bus_message *m);
int bus_message_seal(sd_bus_message *m, uint64_t serial);
int bus_message_dump(sd_bus_message *m);
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
......@@ -760,7 +760,7 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
if (m->sealed)
return 0;
return message_seal(m, ++b->serial);
return bus_message_seal(m, ++b->serial);
}
static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
......@@ -856,7 +856,7 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
bus->rbuffer = b;
bus->rbuffer_size -= size;
r = message_parse(t);
r = bus_message_parse(t);
if (r < 0) {
sd_bus_message_unref(t);
return r;
......
......@@ -27,6 +27,13 @@
#include "sd-bus-protocol.h"
/* TODO:
*
* - make unix fd passing work
* - add page donation logic
* - api for appending/reading fixed arrays
*/
typedef struct sd_bus sd_bus;
typedef struct sd_bus_message sd_bus_message;
......@@ -105,9 +112,12 @@ int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p);
int sd_bus_message_open_container(sd_bus_message *m, char type, const char *contents);
int sd_bus_message_close_container(sd_bus_message *m);
int sd_bus_message_read_type(sd_bus_message *m, char *type, char *element, size_t *length);
int sd_bus_message_read_basic(sd_bus_message *m, char type, char element, const void **p, size_t *length);
int sd_bus_message_read(sd_bus_message *m, const char *types, ...);
int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p);
int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents);
int sd_bus_message_exit_container(sd_bus_message *m);
int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents);
int sd_bus_message_rewind(sd_bus_message *m, bool complete);
/* Bus management */
......
......@@ -37,8 +37,8 @@
int main(int argc, char *argv[]) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
int r;
const char *x, *y, *z, *a, *b, *c;
int r, boolean;
const char *x, *y, *z, *a, *b, *c, *d;
uint8_t u, v;
void *buffer = NULL;
size_t sz;
......@@ -74,10 +74,10 @@ int main(int argc, char *argv[]) {
r = sd_bus_message_close_container(m);
assert_se(r >= 0);
r = message_seal(m, 4711);
r = bus_message_seal(m, 4711);
assert_se(r >= 0);
message_dump(m);
bus_message_dump(m);
r = bus_message_get_blob(m, &buffer, &sz);
assert_se(r >= 0);
......@@ -118,12 +118,48 @@ int main(int argc, char *argv[]) {
free(buffer);
/* r = sd_bus_message_read(m, "sas", &x, 5, &y, &z, &a, &b, &c); */
/* assert_se(r >= 0); */
/* r = sd_bus_message_read(m, "a{yv}", 2, */
/* &u, "s", &x, */
/* &v, "s", &y); */
assert_se(sd_bus_message_rewind(m, true) >= 0);
r = sd_bus_message_read(m, "sas", &x, 2, &y, &z);
assert_se(r > 0);
assert_se(streq(x, "a string"));
assert_se(streq(y, "string #1"));
assert_se(streq(z, "string #2"));
r = sd_bus_message_read(m, "sass", &x, 5, &y, &z, &a, &b, &c, &d);
assert_se(r > 0);
assert_se(streq(x, "foobar"));
assert_se(streq(y, "foo"));
assert_se(streq(z, "bar"));
assert_se(streq(a, "waldo"));
assert_se(streq(b, "piep"));
assert_se(streq(c, "pap"));
assert_se(streq(d, "after"));
r = sd_bus_message_read(m, "a{yv}", 2, &u, "s", &x, &v, "s", &y);
assert_se(r > 0);
assert_se(u == 3);
assert_se(streq(x, "foo"));
assert_se(v == 5);
assert_se(streq(y, "waldo"));
r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d);
assert_se(r > 0);
assert_se(boolean);
assert_se(streq(x, "aaa"));
assert_se(streq(y, "1"));
assert_se(streq(a, "bbb"));
assert_se(streq(b, "2"));
assert_se(streq(c, "ccc"));
assert_se(streq(d, "3"));
r = sd_bus_message_read(m, "as", 2, &x, &y);
assert_se(r > 0);
assert_se(streq(x, "foobar"));
assert_se(streq(y, "waldo"));
r = sd_bus_message_peek_type(m, NULL, NULL);
assert_se(r == 0);
return 0;
}
......@@ -5797,3 +5797,22 @@ char *strextend(char **x, ...) {
return r + l;
}
char *strrep(const char *s, unsigned n) {
size_t l;
char *r, *p;
unsigned i;
assert(s);
l = strlen(s);
p = r = malloc(l * n + 1);
if (!r)
return NULL;
for (i = 0; i < n; i++)
p = stpcpy(p, s);
*p = 0;
return r;
}
......@@ -608,3 +608,4 @@ static inline void *mempset(void *s, int c, size_t n) {
char *hexmem(const void *p, size_t l);
char *strextend(char **x, ...);
char *strrep(const char *s, unsigned n);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment