-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathudbus.h
237 lines (213 loc) · 7.93 KB
/
udbus.h
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright (c) 2010-2011 Vincent Hanquez <[email protected]>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef UDBUS_H
#define UDBUS_H
#include <stdint.h>
#include <stdbool.h>
#define UDBUS_VERSION_MAJOR 0
#define UDBUS_VERSION_MINOR 9
typedef enum {
DBUS_SIGNATURE,
DBUS_OBJECTPATH,
DBUS_BOOLEAN,
DBUS_BYTE,
DBUS_STRING,
DBUS_INT16,
DBUS_UINT16,
DBUS_INT32,
DBUS_UINT32,
DBUS_INT64,
DBUS_UINT64,
DBUS_DOUBLE,
DBUS_ARRAY,
DBUS_VARIANT,
DBUS_STRUCT_BEGIN,
DBUS_STRUCT_END,
DBUS_DICT_BEGIN,
DBUS_DICT_END,
DBUS_INVALID = -1,
} dbus_type;
typedef struct {
dbus_type a[256];
} dbus_sig;
struct dbus_reader {
uint8_t *data;
uint32_t align_offset;
uint32_t offset;
uint32_t length;
int endianness; /* 0 = little endian, 1 = big endian */
};
struct dbus_writer {
uint8_t *buffer;
uint32_t offset;
uint32_t length;
int endianness; /* 0 = little endian, 1 = big endian */
};
struct dbus_header {
uint8_t endianness; /* 0 = little endian, 1 = big endian */
uint8_t messagetype;
uint8_t flags;
uint8_t ver;
uint32_t bodylen;
uint32_t serial;
uint32_t fieldslen;
};
typedef struct { uint32_t *ptr; uint32_t offset; } dbus_array_writer;
typedef struct { uint32_t length; uint32_t offset; } dbus_array_reader;
typedef enum {
DBUS_FIELD_INVALID = 0,
DBUS_FIELD_PATH = 1,
DBUS_FIELD_INTERFACE = 2,
DBUS_FIELD_MEMBER = 3,
DBUS_FIELD_ERROR_NAME = 4,
DBUS_FIELD_REPLY_SERIAL = 5,
DBUS_FIELD_DESTINATION = 6,
DBUS_FIELD_SENDER = 7,
DBUS_FIELD_SIGNATURE = 8,
DBUS_FIELD_UNIX_FDS = 9,
} dbus_field_type;
typedef enum {
DBUS_TYPE_INVALID = 0,
DBUS_TYPE_METHOD_CALL = 1,
DBUS_TYPE_METHOD_RETURN = 2,
DBUS_TYPE_ERROR = 3,
DBUS_TYPE_SIGNAL = 4,
} dbus_msg_type;
typedef struct {
int type;
uint32_t serial;
char *destination;
char *path;
char *interface;
char *method;
char *error_name;
char *sender;
dbus_sig signature;
uint32_t reply_serial;
int w;
uint8_t *body;
union {
struct dbus_writer writer; /* writing body */
struct dbus_reader reader; /* reading body */
};
} dbus_msg;
typedef struct {
int state;
/* s1 */
uint8_t headerdata[16];
int headeroff;
/* s2 */
struct dbus_header header;
/* s3 */
uint8_t *fielddata; /* alloc'ed */
int fieldoff;
/* s4 */
dbus_msg *msg;
int bodyoff;
} dbus_eventpart;
/* vectorise IO operations so that user can decide about buffering
* and how to read/write to whatever is providing the data (channel, handle, etc)
*/
typedef struct {
/* return 0 if succeed to write count bytes, non-0 otherwise */
int (*io_write)(void *priv, const void *buf, uint32_t count);
/* return number of bytes read negative on error */
int (*io_read)(void *priv, void *buf, uint32_t count);
/* debugging logging */
void (*io_debug)(void *logpriv, const char *buf);
/* retrieve fd for select operation */
int (*io_get_fd)(void *priv);
/* private pointer passed to write/read (eg. handle, channel, buffer, etc) */
void *priv;
/* private pointer passed for debugging */
void *logpriv;
/* next serial */
int next_serial;
} dbus_io;
void dbus_msg_free(dbus_msg *msg);
dbus_msg *dbus_msg_new(uint32_t serial);
dbus_msg *dbus_msg_new_method_call(uint32_t serial, const char *destination, const char *path,
const char *interface, const char *method);
dbus_msg *dbus_msg_new_signal(uint32_t serial, const char *path, const char *interface, const char *name);
void dbus_msg_set_destination(dbus_msg *msg, const char *destination);
void dbus_msg_set_path(dbus_msg *msg, const char *path);
void dbus_msg_set_method(dbus_msg *msg, const char *method);
void dbus_msg_set_error_name(dbus_msg *msg, const char *error_name);
void dbus_msg_set_sender(dbus_msg *msg, const char *sender);
void dbus_msg_set_interface(dbus_msg *msg, const char *interface);
void dbus_msg_set_signature(dbus_msg *msg, dbus_sig *signature);
/* create a body buffer of specified length.
BEWARE: need to be called before adding any elements to the body */
int dbus_msg_body_add(dbus_msg *msg, uint32_t length);
/* method to add differents types of elements to the body. */
int dbus_msg_body_add_byte (dbus_msg *msg, uint8_t val);
int dbus_msg_body_add_boolean (dbus_msg *msg, bool val);
int dbus_msg_body_add_int16 (dbus_msg *msg, int16_t val);
int dbus_msg_body_add_uint16 (dbus_msg *msg, uint16_t val);
int dbus_msg_body_add_int32 (dbus_msg *msg, int32_t val);
int dbus_msg_body_add_uint32 (dbus_msg *msg, uint32_t val);
int dbus_msg_body_add_int64 (dbus_msg *msg, int64_t val);
int dbus_msg_body_add_uint64 (dbus_msg *msg, uint64_t val);
int dbus_msg_body_add_double (dbus_msg *msg, double val);
int dbus_msg_body_add_string (dbus_msg *msg, const char *val);
int dbus_msg_body_add_objectpath (dbus_msg *msg, const char *val);
int dbus_msg_body_add_array_begin(dbus_msg *msg, dbus_type element, dbus_array_writer *ptr);
int dbus_msg_body_add_array_end (dbus_msg *msg, dbus_array_writer *ptr);
int dbus_msg_body_add_structure (dbus_msg *msg);
int dbus_msg_body_add_variant (dbus_msg *msg, dbus_sig *signature);
/* methods to introspect a received message body for all different types */
int dbus_msg_body_get_byte (dbus_msg *msg, uint8_t *val);
int dbus_msg_body_get_boolean (dbus_msg *msg, bool *val);
int dbus_msg_body_get_int16 (dbus_msg *msg, int16_t *val);
int dbus_msg_body_get_uint16 (dbus_msg *msg, uint16_t *val);
int dbus_msg_body_get_int32 (dbus_msg *msg, int32_t *val);
int dbus_msg_body_get_uint32 (dbus_msg *msg, uint32_t *val);
int dbus_msg_body_get_int64 (dbus_msg *msg, int64_t *val);
int dbus_msg_body_get_uint64 (dbus_msg *msg, uint64_t *val);
int dbus_msg_body_get_double (dbus_msg *msg, double *val);
int dbus_msg_body_get_string (dbus_msg *msg, char **val);
int dbus_msg_body_get_object_path (dbus_msg *msg, char **val);
int dbus_msg_body_get_array (dbus_msg *msg, dbus_type element, dbus_array_reader *ptr);
int dbus_msg_body_get_array_left (dbus_msg *msg, dbus_array_reader *ptr);
int dbus_msg_body_get_structure (dbus_msg *msg);
int dbus_msg_body_get_variant (dbus_msg *msg, dbus_sig *signature);
int dbus_msg_send(dbus_io *dio, dbus_msg *msg);
int dbus_msg_recv(dbus_io *dio, dbus_msg **msg);
/* event based recv */
int dbus_event_init(dbus_eventpart *evpart);
int dbus_event_has_message(dbus_eventpart *evpart);
int dbus_event_recv(dbus_io *dio, dbus_eventpart *evpart, dbus_msg **msg);
int dbus_event_get_fd(dbus_io *dio);
/* connection method */
int dbus_connect_session(void);
int dbus_connect_system (void);
/* auth handshake */
int dbus_auth(dbus_io *dio, char *auth);
#endif