signal.c

Go to the documentation of this file.
00001 /*
00002  EasyDbus: DBUS Binding library. 
00003  Copyright (C) 2007  Daniele Rondina aka ge@@ru, geaaru@gmail.com 
00004 
00005  This program is free software; you can redistribute it and/or
00006  modify it under the terms of the GNU General Public License
00007  as published by the Free Software Foundation; either version 2
00008  of the License, or (at your option) any later version.
00009 
00010  This program is distributed in the hope that it will be useful,
00011  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  GNU General Public License for more details.
00014 
00015  You should have received a copy of the GNU General Public License
00016  along with this program; if not, write to the Free Software
00017  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 
00019   Filename:  signal.c
00020  
00021   Description: Functions used for manage signal on dbus. 
00022  
00023   Version:  1.0
00024   Created:  04/07/07 23:06:58 CEST
00025   Revision:
00026     0 - created (Ge@@ru)
00027     1 - added doxygen documentation and manage of opaque structs.
00028         11/02/07 10:41:00 CEST (Ge@@ru)
00029  
00030   Author:   Daniele Rondina aka ge@@ru, (geaaru@gmail.com) 
00031   Company:  
00032   License:  GPL 2.0
00033 */
00034 
00041 #include <string.h>
00042 #include <stdlib.h>
00043 // easydbus includes
00044 #include "easydbus-core.h"
00045 #include "debug.h"
00046 #include "utils.h"
00047 #include "elem_internal.h"
00048 #include "message_internal.h"
00049 
00058 EasyDbus_signal *
00059 easydbus_signal_create_skeleton (char *path,
00060                                  char *interface, 
00061                                  char *signal)
00062 {
00063    struct EasyDbus_signal *signal_skeleton = NULL;
00064    int string_length = 0;
00065 
00066    if (path == NULL || interface == NULL || signal == NULL)
00067       return NULL;
00068 
00069    signal_skeleton = (struct EasyDbus_signal *) malloc
00070       (sizeof (struct EasyDbus_signal));
00071 
00072    if (signal_skeleton == NULL)
00073       return NULL;
00074 
00075    memset (signal_skeleton, 0, sizeof (struct EasyDbus_signal));
00076 
00077    signal_skeleton->name = signal_skeleton->path =
00078       signal_skeleton->interface = 
00079       signal_skeleton->destination = 
00080       signal_skeleton->sender = NULL;
00081    signal_skeleton->first = NULL;
00082 
00083    string_length = strlen (signal) + 1;
00084    signal_skeleton->name = (char *) malloc (string_length);
00085    if (!signal_skeleton->name)
00086       goto out_of_memory_error;
00087    EASYDBUS_MEMCOPY (signal_skeleton->name, signal,
00088                      string_length);
00089 
00090    string_length = strlen (path) + 1;
00091    signal_skeleton->path = (char *) malloc (string_length);
00092    if (!signal_skeleton->path)
00093       goto out_of_memory_error;
00094    EASYDBUS_MEMCOPY (signal_skeleton->path, path,
00095                      string_length);
00096 
00097    string_length = strlen (interface) + 1;
00098    signal_skeleton->interface = (char *) malloc (string_length);
00099    if (!signal_skeleton->interface)
00100       goto out_of_memory_error;
00101    EASYDBUS_MEMCOPY (signal_skeleton->interface, interface,
00102                      string_length);
00103 
00104    return signal_skeleton;
00105 
00106  out_of_memory_error:
00107    if (signal_skeleton->name)
00108       free (signal_skeleton->name);
00109    if (signal_skeleton->path)
00110       free (signal_skeleton->path);
00111    if (signal_skeleton->interface)
00112       free (signal_skeleton->interface);
00113    free (signal_skeleton);
00114 
00115    return NULL;
00116 }
00117 
00124 inline void
00125 easydbus_signal_free_skeleton (EasyDbus_signal * signal)
00126 {
00127    struct EasyDbus_elem *el = NULL;
00128 
00129    if (signal) {
00130       if (signal->name)
00131          free (signal->name);
00132       if (signal->path)
00133          free (signal->path);
00134       if (signal->interface)
00135          free (signal->interface);
00136       if (signal->sender)
00137          free (signal->sender);
00138       if (signal->destination)
00139          free (signal->destination);
00140       signal->name = signal->path =
00141          signal->destination =
00142          signal->interface = signal->sender = NULL;
00143 
00144       while (signal->args) {
00145          if (signal->first) {
00146             el = signal->first;
00147             signal->first = signal->first->next;
00148             easydbus_elem_free (el);
00149          }
00150          signal->args--;
00151       }
00152       free (signal);
00153    }
00154 }
00155 
00165 inline DBusMessage *
00166 create_signal_msg (struct EasyDbus_core *core,
00167                    struct EasyDbus_signal *signal)
00168 {
00169 
00170    /* create msg */
00171    DBusMessage *msg =
00172       dbus_message_new_signal (signal->path, signal->interface,
00173                                signal->name);
00174 
00175    /* add signal data is there are */
00176    if (msg && signal->args)
00177       add_elems_to_msg (msg, signal->first);
00178 
00179    return msg;
00180 }
00181 
00182 
00191 struct EasyDbus_signal *
00192 easydbus_signal_build_skeleton (DBusMessage * msg)
00193 {
00194    struct EasyDbus_signal *signal = NULL;
00195 
00196    EasyDbusDebug ("Build Signal Skeleton");
00197 
00198    signal =
00199      easydbus_signal_create_skeleton (
00200                                       // path
00201                                       (char *) dbus_message_get_path (msg),
00202                                       // interface
00203                                       (char *) dbus_message_get_interface (msg),
00204                                       // method
00205                                       (char *) dbus_message_get_member (msg));
00206 
00207    if (!signal)
00208       return NULL;
00209 
00210    if (dbus_message_get_sender (msg))
00211       easydbus_signal_set_sender (signal, dbus_message_get_sender (msg));
00212 
00213    if (dbus_message_get_destination (msg))
00214       easydbus_signal_set_destination (signal,
00215                                        dbus_message_get_destination (msg));
00216 
00217    if (easydbus_build_skeleton_data (msg, EASYDBUS_ET_SIGNAL, signal))
00218       goto out_of_memory_error;
00219 
00220    EasyDbusDebug ("Created signal object");
00221 
00222    return signal;
00223 
00224  out_of_memory_error:
00225    if (signal)
00226       easydbus_signal_free_skeleton (signal);
00227    return NULL;
00228 }
00229 
00238 const char *
00239 easydbus_signal_get_name (EasyDbus_signal * signal)
00240 {
00241    return (signal ? (const char *) signal->name : NULL);
00242 }
00243 
00252 const char *
00253 easydbus_signal_get_path (EasyDbus_signal * signal)
00254 {
00255    return (signal ? (const char *) signal->path : NULL);
00256 }
00257 
00266 const char *
00267 easydbus_signal_get_interface (EasyDbus_signal * signal)
00268 {
00269    return (signal ? (const char *) signal->interface : NULL);
00270 }
00271 
00280 const char *
00281 easydbus_signal_get_destination (EasyDbus_signal * signal)
00282 {
00283    return (signal ? (const char *) signal->destination : NULL);
00284 }
00285 
00294 const char *
00295 easydbus_signal_get_sender (EasyDbus_signal * signal)
00296 {
00297   return (signal ? (const char *) signal->sender : NULL);
00298 }
00299 
00309 int
00310 easydbus_signal_set_sender (EasyDbus_signal * signal, 
00311                             const char *sender)
00312 {
00313    unsigned int string_length = 0;
00314 
00315    if (!sender || !signal || signal->sender) 
00316       return -1;
00317 
00318    string_length = strlen (sender) + 1;
00319    signal->sender = (char *) malloc (string_length);
00320    if (!signal->sender)
00321       return -1;
00322 
00323    EASYDBUS_MEMCOPY (signal->sender, sender, string_length);
00324 
00325    return 0;
00326 }
00327 
00337 int
00338 easydbus_signal_set_destination (EasyDbus_signal * signal, 
00339                                  const char *destination)
00340 {
00341    unsigned int string_length = 0;
00342 
00343    if (!destination || !signal || signal->destination) 
00344       return -1;
00345 
00346    string_length = strlen (destination) + 1;
00347    signal->destination = (char *) malloc (string_length);
00348    if (!signal->destination)
00349       return -1;
00350 
00351    EASYDBUS_MEMCOPY (signal->destination, destination,
00352                      string_length);
00353 
00354    return 0;
00355 }
00356 
00365 int
00366 easydbus_signal_get_args (EasyDbus_signal * signal)
00367 {
00368    return (signal ? (int) signal->args : -1);
00369 }
00370 
00381 const EasyDbus_elem *
00382 easydbus_signal_get_element (EasyDbus_signal * signal, 
00383                              unsigned int n)
00384 {
00385    return ((signal && n <= signal->args) ? 
00386             easydbus_elem_get_element (signal->first, n) : NULL);
00387 }
00388 
00399 enum easydbus_ret_values
00400 easydbus_signal_send (EasyDbus_conn * data, 
00401                       EasyDbus_signal * signal)
00402 {
00403    struct EasyDbus_core *core = (struct EasyDbus_core *) data;
00404    int ret_val = EASYDBUS_SIGNAL_NOT_SENT;
00405 
00406    if (!data || !signal)
00407       return EASYDBUS_ERROR;
00408 
00409    DBusMessage *msg = create_signal_msg (core, signal);
00410 
00411    if (!msg)
00412       return EASYDBUS_ERROR;
00413 
00414    if (!easydbus_message_send (core, msg, 1, NULL))
00415       ret_val = EASYDBUS_SIGNAL_SENT;
00416 
00417    dbus_message_unref (msg);
00418 
00419    return ret_val;
00420 }
00421 
00422 // vim: ts=3 shiftwidth=3 expandtab

Generated on Thu Apr 10 10:00:18 2008 for EasyDbus-0.2 by  doxygen 1.5.4