00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00040 #include <string.h>
00041 #include <stdlib.h>
00042
00043 #include "debug.h"
00044 #include "easydbus-core.h"
00045 #include "reply_internal.h"
00046 #include "introspect_internal.h"
00047 #include "register_obj_internal.h"
00048
00049
00058 EasyDbus_object *
00059 easydbus_object_create_skeleton (char *path, void *user_data)
00060 {
00061 struct EasyDbus_object_box *object;
00062 int path_length;
00063
00064 if (!path)
00065 return NULL;
00066
00067 object = (struct EasyDbus_object_box *)
00068 malloc (sizeof (struct EasyDbus_object_box));
00069
00070 if (!object)
00071 return NULL;
00072 memset (object, 0, sizeof (struct EasyDbus_object_box));
00073
00074 path_length = strlen (path) + 1;
00075
00076 object->core.path = (char *) malloc (path_length);
00077 if (!object->core.path)
00078 goto out_of_memory;
00079
00080 EASYDBUS_MEMCOPY (object->core.path, path, path_length);
00081
00082 object->core.handled_interfaces = NULL;
00083
00084 if (user_data)
00085 object->core.user_data = user_data;
00086
00087 object->next = NULL;
00088
00089 EasyDbusDebug ("Create object core with" " path %s", path);
00090
00091 return (EasyDbus_object *) object;
00092
00093 out_of_memory:
00094 if (object)
00095 free (object);
00096 return NULL;
00097 }
00098
00106 int
00107 easydbus_object_add_interface (EasyDbus_obj_interface * interface,
00108 EasyDbus_object * object)
00109 {
00110 struct EasyDbus_obj_interface *temp;
00111
00112 if (!interface || !object)
00113 return -1;
00114
00115 if (!object->handled_interfaces)
00116 object->handled_interfaces = interface;
00117 else {
00118 temp = object->handled_interfaces;
00119 while (temp->next)
00120 temp = temp->next;
00121 temp->next = interface;
00122 }
00123
00124 object->n_interfaces++;
00125
00126 EasyDbusDebug ("Added interface %s to object %s",
00127 interface->name, object->path);
00128
00129 return 0;
00130 }
00131
00139 int
00140 easydbus_object_unregister (EasyDbus_conn * data,
00141 EasyDbus_object * object)
00142 {
00143 struct EasyDbus_core *core = (struct EasyDbus_core *) data;
00144 struct EasyDbus_object_box *obj =
00145 (struct EasyDbus_object_box *) object;
00146
00147 if (!object || !data)
00148 return -1;
00149
00150 if (dbus_connection_unregister_object_path
00151 (core->conn, (const char *) obj->core.path))
00152 return -1;
00153
00154 return 0;
00155 }
00156
00163 int
00164 easydbus_conn_disable_filters (EasyDbus_conn * conn)
00165 {
00166 struct EasyDbus_core *core = (struct EasyDbus_core *) conn;
00167
00168 if (!conn)
00169 return -1;
00170
00171 dbus_connection_remove_filter (core->conn,
00172 easydbus_core_filter_func, core);
00173
00174 dbus_connection_remove_filter (core->conn,
00175 easydbus_filter_func, core);
00176
00177 if (core->handled_introspect)
00178 dbus_connection_remove_filter (core->conn,
00179 easydbus_introspect_filter_func,
00180 core);
00181
00182 EasyDbusDebug ("Filters Removed");
00183 return 0;
00184 }
00185
00200 int
00201 easydbus_conn_enable_filters (EasyDbus_conn * conn)
00202 {
00203 struct EasyDbus_core *core = (struct EasyDbus_core *) conn;
00204
00205 if (!conn)
00206 return -1;
00207
00209
00210
00211 if (!dbus_connection_add_filter (core->conn,
00212 easydbus_core_filter_func, core,
00213 NULL)) {
00214 EasyDbusDebug ("Error on add core filter");
00215 return -1;
00216 }
00217
00218 if (core->handled_introspect)
00219 if (!dbus_connection_add_filter (core->conn,
00220 easydbus_introspect_filter_func,
00221 core, NULL)) {
00222 EasyDbusDebug ("Error on add filter");
00223 return -1;
00224 }
00225
00226 if (!dbus_connection_add_filter (core->conn,
00227 easydbus_filter_func, core,
00228 NULL)) {
00229 EasyDbusDebug ("Error on add filter");
00230 return -1;
00231 }
00232
00233 EasyDbusDebug ("Filters activated");
00234
00235 return 0;
00236 }
00237
00245 int
00246 easydbus_object_register (EasyDbus_conn * data,
00247 EasyDbus_object * object)
00248 {
00249 struct EasyDbus_core *core = (struct EasyDbus_core *) data;
00250 struct EasyDbus_object_box *obj =
00251 (struct EasyDbus_object_box *) object;
00252 struct EasyDbus_object_box *temp = NULL;
00253
00254 if (!object || !data)
00255 return -1;
00256
00257 memset (&obj->vtable, 0, sizeof (DBusObjectPathVTable));
00258
00259 obj->vtable.message_function = easydbus_obj_path_msg_function;
00260 obj->vtable.unregister_function =
00261 easydbus_obj_path_unregister_function;
00262 obj->vtable.dbus_internal_pad1 = NULL;
00263 obj->vtable.dbus_internal_pad2 = NULL;
00264
00265 if (!dbus_connection_register_object_path (core->conn,
00266 (const char *) obj->
00267 core.path, &obj->vtable,
00268 core)) {
00269 EasyDbusDebug ("Error on register object %s", obj->core.path);
00270 return -1;
00271 }
00272 temp = core->objects;
00273
00274 if (core->n_objs) {
00275 while (temp->next)
00276 temp = temp->next;
00277 temp->next = obj;
00278 }
00279 else
00280 core->objects = obj;
00281 core->n_objs++;
00282
00283 EasyDbusDebug ("Register object %s", obj->core.path);
00284
00285 return 0;
00286 }
00287
00295 void
00296 easydbus_object_free_skeleton (EasyDbus_object * object)
00297 {
00298 struct EasyDbus_obj_interface *interface;
00299 struct EasyDbus_object_box *obj =
00300 (struct EasyDbus_object_box *) object;
00301
00302 if (!object)
00303 return;
00304
00305 EasyDbusDebug ("Free of object skeleton %s", object->path);
00306
00307 if (object->path)
00308 free (object->path);
00309
00310 while (object->handled_interfaces) {
00311 interface = object->handled_interfaces;
00312 object->handled_interfaces = interface->next;
00313 easydbus_obj_interface_free_skeleton (interface);
00314 }
00315
00316 object->user_data = NULL;
00317
00318 free (obj);
00319 }
00320
00327 inline const char *
00328 easydbus_object_get_path (EasyDbus_object * object)
00329 {
00330 return (object ? (const char *) object->path : NULL);
00331 }
00332
00339 inline int
00340 easydbus_object_get_n_interfaces (EasyDbus_object * object)
00341 {
00342 return (object ? object->n_interfaces : -1);
00343 }
00344
00355 EasyDbus_obj_interface *
00356 easydbus_object_get_interface (EasyDbus_object * object,
00357 unsigned int n)
00358 {
00359 unsigned int i;
00360 EasyDbus_obj_interface *ans = NULL;
00361
00362 if (object && ((int) n <= object->n_interfaces))
00363 for (i = 0, ans = object->handled_interfaces;
00364 i < n; i++, ans = ans->next);
00365
00366 return ans;
00367 }
00368
00374 inline void *
00375 easydbus_object_get_closure (EasyDbus_object * object)
00376 {
00377 return (object ? object->user_data : NULL);
00378 }
00379
00385 inline void
00386 easydbus_object_set_closure (EasyDbus_object * object,
00387 void *user_data)
00388 {
00389 if (object) object->user_data = user_data;
00390 }
00391
00392