skeleton.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:  skeleton.c
00020 
00021   Description:  
00022 
00023   Version:  0.1
00024   Created:  06/09/07 09:55:14 CEST
00025   Revision: 
00026       0 - created (Ge@@ru)
00027 
00028   Author:   Daniele Rondina aka ge@@ru (geaaru@gmail.com) 
00029   License:  GPL 2.0
00030 */
00031 
00037 #include <string.h>
00038 #include <stdlib.h>
00039 // easydbus includes
00040 #include "easydbus-core.h"
00041 #include "elem_internal.h"
00042 #include "debug.h"
00043 #include "variant_internal.h"
00044 #include "dict_entry_internal.h"
00045 #include "struct_internal.h"
00046 #include "array_internal.h"
00047 
00056 int
00057 easydbus_add_variant_param_to_skeleton (enum event_type type,
00058                                         void *skeleton, 
00059                                         struct EasyDbus_container *container) 
00060 {
00061    struct EasyDbus_elem *el = NULL;
00062 
00063    if (!container || !skeleton ||
00064        container->container_type != EASYDBUS_ELTYPE_VARIANT)
00065       return -1;
00066 
00067    el = easydbus_elem_create (EASYDBUS_ELTYPE_VARIANT);
00068    if (!el)
00069       return -1;
00070    el->payload.p_container = container;
00071 
00072    easydbus_elem_add_to_skeleton (type, skeleton, el);
00073 
00074    return 0;
00075 
00076 }
00077 
00086 int 
00087 easydbus_add_struct_param_to_skeleton (enum event_type type,
00088                                        void *skeleton, 
00089                                        struct EasyDbus_container *container) 
00090 {
00091    struct EasyDbus_elem *el = NULL;
00092 
00093    if (container == NULL ||
00094        skeleton == NULL ||
00095        container->container_type != EASYDBUS_ELTYPE_STRUCT)
00096       return -1;
00097 
00098    el = easydbus_elem_create (EASYDBUS_ELTYPE_STRUCT);
00099    if (!el)
00100       return -1;
00101    el->payload.p_container = container;
00102 
00103    easydbus_elem_add_to_skeleton (type, skeleton, el);
00104 
00105    return 0;
00106 }
00107 
00116 int
00117 easydbus_add_ui16_param_to_skeleton (enum event_type type, 
00118                                      void *skeleton, 
00119                                      unsigned short i) 
00120 {
00121    struct EasyDbus_elem *el = NULL;
00122 
00123    el = easydbus_elem_create (EASYDBUS_ELTYPE_UINT16);
00124    if (!el)
00125       return -1;
00126    el->payload.ui16 = i;
00127 
00128    easydbus_elem_add_to_skeleton (type, skeleton, el);
00129 
00130    return 0;
00131 }
00132 
00133 
00142 int
00143 easydbus_add_i16_param_to_skeleton (enum event_type type, 
00144                                     void *skeleton, short i) 
00145 {
00146    struct EasyDbus_elem *el = NULL;
00147 
00148    el = easydbus_elem_create (EASYDBUS_ELTYPE_INT16);
00149    if (!el)
00150       return -1;
00151    el->payload.i16 = i;
00152 
00153    easydbus_elem_add_to_skeleton (type, skeleton, el);
00154 
00155    return 0;
00156 }
00157 
00166 int
00167 easydbus_add_byte_param_to_skeleton (enum event_type type, 
00168                                      void *skeleton, char byte) 
00169 {
00170    struct EasyDbus_elem *el = NULL;
00171 
00172    el = easydbus_elem_create (EASYDBUS_ELTYPE_BYTE);
00173    if (!el)
00174       return -1;
00175    el->payload.byte = byte;
00176 
00177    easydbus_elem_add_to_skeleton (type, skeleton, el);
00178 
00179    return 0;
00180 }
00181 
00190 int
00191 easydbus_add_boolean_param_to_skeleton (enum event_type type, 
00192                                         void *skeleton, 
00193                                         unsigned char boolean) 
00194 {
00195    struct EasyDbus_elem *el = NULL;
00196 
00197    if (boolean > 1)
00198       return -1;
00199 
00200    el = easydbus_elem_create (EASYDBUS_ELTYPE_BOOLEAN);
00201    if (!el)
00202       return -1;
00203    el->payload.boolean = boolean;
00204 
00205    easydbus_elem_add_to_skeleton (type, skeleton, el);
00206 
00207    return 0;
00208 }
00209 
00218 int
00219 easydbus_add_array_param_to_skeleton (enum event_type type,
00220                                       void *skeleton, 
00221                                       struct EasyDbus_container *container)
00222 {
00223    struct EasyDbus_elem *el = NULL;
00224 
00225    if (!container || !skeleton)
00226       return -1;
00227 
00228    el = easydbus_elem_create (EASYDBUS_ELTYPE_ARRAY);
00229    if (!el)
00230       return -1;
00231 
00232    el->payload.p_container = container;
00233 
00234    easydbus_elem_add_to_skeleton (type, skeleton, el);
00235 
00236    return 0;
00237 }
00238 
00247 int
00248 easydbus_add_string_param_to_skeleton (enum event_type type, 
00249                                        void *skeleton, 
00250                                        char *string)
00251 {
00252    struct EasyDbus_elem *el = NULL;
00253    int string_length = 0;
00254 
00255    if (!string || !skeleton)
00256       return -1;
00257 
00258    el = easydbus_elem_create (EASYDBUS_ELTYPE_STRING);
00259    if (!el)
00260       return -1;
00261 
00262    string_length = strlen (string) + 1;
00263    el->payload.string = (char *) malloc (string_length);
00264 
00265    if (!el->payload.string) {
00266       easydbus_elem_free (el);
00267       return -1;
00268    }
00269 
00270    EASYDBUS_MEMCOPY (el->payload.string, string, string_length);
00271 
00272    easydbus_elem_add_to_skeleton (type, skeleton, el);
00273 
00274    return 0;
00275 }
00276 
00285 int
00286 easydbus_add_obj_path_param_to_skeleton (enum event_type type, 
00287                                          void *skeleton, 
00288                                          char *string) 
00289 {
00290    struct EasyDbus_elem *el = NULL;
00291    int string_length = 0;
00292 
00293    if (!string || !skeleton)
00294       return -1;
00295 
00296    el = easydbus_elem_create (EASYDBUS_ELTYPE_OBJECT_PATH);
00297    if (!el)
00298       return -1;
00299 
00300    string_length = strlen (string) + 1;
00301    el->payload.string = (char *) malloc (string_length);
00302 
00303    if (!el->payload.string) {
00304       easydbus_elem_free (el);
00305       return -1;
00306    }
00307 
00308    EASYDBUS_MEMCOPY (el->payload.string, string, string_length);
00309 
00310    easydbus_elem_add_to_skeleton (type, skeleton, el);
00311 
00312    return 0;
00313 }
00314 
00323 int
00324 easydbus_add_signature_param_to_skeleton (enum event_type type, 
00325                                           void *skeleton, 
00326                                           char *signature) 
00327 {
00328    struct EasyDbus_elem *el = NULL;
00329    int string_length = 0;
00330 
00331    if (!signature || !skeleton)
00332       return -1;
00333 
00334    el = easydbus_elem_create (EASYDBUS_ELTYPE_SIGNATURE);
00335    if (!el)
00336       return -1;
00337 
00338    string_length = strlen (signature) + 1;
00339    el->payload.string = (char *) malloc (string_length);
00340 
00341    if (!el->payload.string) {
00342       easydbus_elem_free (el);
00343       return -1;
00344    }
00345 
00346    EASYDBUS_MEMCOPY (el->payload.string, signature, string_length);
00347 
00348    easydbus_elem_add_to_skeleton (type, skeleton, el);
00349 
00350    return 0;
00351 }
00352 
00361 int 
00362 easydbus_add_ui32_param_to_skeleton (enum event_type type, 
00363                                      void *skeleton, 
00364                                      unsigned int i) 
00365 {
00366    struct EasyDbus_elem *el = NULL;
00367 
00368    el = easydbus_elem_create (EASYDBUS_ELTYPE_UINT32);
00369    if (!el)
00370       return -1;
00371    el->payload.ui32 = i;
00372 
00373    easydbus_elem_add_to_skeleton (type, skeleton, el);
00374 
00375    return 0;
00376 }
00377 
00386 int
00387 easydbus_add_double_param_to_skeleton (enum event_type type, 
00388                                        void *skeleton, 
00389                                        double d) 
00390 {
00391    struct EasyDbus_elem *el = NULL;
00392 
00393    el = easydbus_elem_create (EASYDBUS_ELTYPE_DOUBLE);
00394    if (!el)
00395       return -1;
00396    el->payload.Double = d;
00397 
00398    easydbus_elem_add_to_skeleton (type, skeleton, el);
00399 
00400    return 0;
00401 }
00402 
00411 int
00412 easydbus_add_i32_param_to_skeleton (enum event_type type, 
00413                                     void *skeleton, int i) 
00414 {
00415    struct EasyDbus_elem *el = NULL;
00416 
00417    if (!skeleton)
00418       return -1;
00419 
00420    el = easydbus_elem_create (EASYDBUS_ELTYPE_INT32);
00421    if (!el)
00422       return -1;
00423    el->payload.i32 = i;
00424 
00425    easydbus_elem_add_to_skeleton (type, skeleton, el);
00426 
00427    return 0;
00428 }
00429 
00438 int
00439 easydbus_add_ui64_param_to_skeleton (enum event_type type, 
00440                                      void *skeleton, 
00441                                      unsigned long long i) 
00442 {
00443    struct EasyDbus_elem *el = NULL;
00444 
00445    el = easydbus_elem_create (EASYDBUS_ELTYPE_UINT64);
00446    if (!el)
00447       return -1;
00448    el->payload.ui64 = i;
00449 
00450    easydbus_elem_add_to_skeleton (type, skeleton, el);
00451 
00452    return 0;
00453 }
00454 
00463 int
00464 easydbus_add_i64_param_to_skeleton (enum event_type type, 
00465                                     void *skeleton, 
00466                                     long long i) 
00467 {
00468    struct EasyDbus_elem *el = NULL;
00469 
00470    el = easydbus_elem_create (EASYDBUS_ELTYPE_INT64);
00471    if (!el)
00472       return -1;
00473    el->payload.i64 = i;
00474 
00475    easydbus_elem_add_to_skeleton (type, skeleton, el);
00476 
00477    return 0;
00478 }
00479 
00489 void
00490 easydbus_elem_add_to_skeleton (enum event_type type,
00491                                void *event, EasyDbus_elem * el)
00492 {
00493    struct EasyDbus_signal *signal;
00494    struct EasyDbus_method *method;
00495    struct EasyDbus_reply *reply;
00496    struct EasyDbus_elem *temp;
00497 
00498    temp = NULL;
00499    reply = NULL;
00500    method = NULL;
00501    signal = NULL;
00502 
00503    switch (type) {
00504       case EASYDBUS_ET_METHOD:
00505          method = (struct EasyDbus_method *) event;
00506          if (!method->first)
00507             method->first = el;
00508          else {
00509             temp = method->first;
00510             while (temp->next)
00511                temp = temp->next;
00512             temp->next = el;
00513          }
00514          method->args++;
00515          break;
00516       case EASYDBUS_ET_SIGNAL:
00517          signal = (struct EasyDbus_signal *) event;
00518          if (!signal->first)
00519             signal->first = el;
00520          else {
00521             temp = signal->first;
00522             while (temp->next)
00523                temp = temp->next;
00524             temp->next = el;
00525          }
00526          signal->args++;
00527          break;
00528       case EASYDBUS_ET_REPLY:
00529       case EASYDBUS_ET_FAILURE:
00530          reply = (struct EasyDbus_reply *) event;
00531          if (!reply->first)
00532             reply->first = el;
00533          else {
00534             temp = reply->first;
00535             while (temp->next)
00536                temp = temp->next;
00537             temp->next = el;
00538          }
00539          reply->args++;
00540          break;
00541       default:
00542          return;
00543          break;
00544    }
00545 }
00546 
00556 int
00557 easydbus_build_skeleton_data (DBusMessage * msg,
00558                               enum event_type type, void *container)
00559 {
00560    char *string = NULL;
00561    int i32 = 0;
00562    unsigned int ui32 = 0;
00563    short i16 = 0;
00564    char byte = 0;
00565    unsigned short ui16 = 0;
00566    long long i64 = 0;
00567    double d = 0;
00568    unsigned long long ui64 = 0;
00569    DBusMessageIter iter;
00570    struct EasyDbus_container *cont_marshal_data = NULL;
00571 
00572 #ifdef EASYDBUS_DEBUG
00573    char *signature = NULL;
00574 #endif
00575 
00576    dbus_message_iter_init (msg, &iter);
00577 
00578    while (1) {
00579 #ifdef EASYDBUS_DEBUG
00580       signature = dbus_message_iter_get_signature (&iter);
00581       EasyDbusDebug ("Current Signature = %s", signature);
00582       dbus_free (signature);
00583 #endif
00584       switch (dbus_message_iter_get_arg_type (&iter)) {
00585          case DBUS_TYPE_STRING:
00586             dbus_message_iter_get_basic (&iter, &string);
00587             EasyDbusDebug ("founded string = %s\n", string);
00588             easydbus_add_string_param_to_skeleton (type,
00589                                                    (void *) container,
00590                                                    string);
00591             break;
00592          case DBUS_TYPE_INT32:
00593             dbus_message_iter_get_basic (&iter, &i32);
00594             EasyDbusDebug ("founded int32 = %d", i32);
00595             easydbus_add_i32_param_to_skeleton (type,
00596                                                 (void *) container,
00597                                                 i32);
00598             break;
00599          case DBUS_TYPE_UINT32:
00600             dbus_message_iter_get_basic (&iter, &ui32);
00601             EasyDbusDebug ("founded uint32 = %d", ui32);
00602             easydbus_add_ui32_param_to_skeleton (type,
00603                                                  (void *) container,
00604                                                  ui32);
00605             break;
00606          case DBUS_TYPE_INT16:
00607             dbus_message_iter_get_basic (&iter, &i16);
00608             EasyDbusDebug ("founded int16 = %d", i16);
00609             easydbus_add_i16_param_to_skeleton (type,
00610                                                 (void *) container,
00611                                                 i16);
00612             break;
00613          case DBUS_TYPE_UINT16:
00614             dbus_message_iter_get_basic (&iter, &ui16);
00615             EasyDbusDebug ("founded uint16 = %d", ui16);
00616             easydbus_add_ui16_param_to_skeleton (type,
00617                                                  (void *) container,
00618                                                  ui16);
00619             break;
00620          case DBUS_TYPE_INT64:
00621             dbus_message_iter_get_basic (&iter, &i64);
00622             EasyDbusDebug ("founded int64 = %lld", i64);
00623             easydbus_add_i64_param_to_skeleton (type,
00624                                                 (void *) container,
00625                                                 i64);
00626             break;
00627          case DBUS_TYPE_UINT64:
00628             dbus_message_iter_get_basic (&iter, &ui64);
00629             EasyDbusDebug ("founded uint64 = %lld", ui64);
00630             easydbus_add_ui64_param_to_skeleton (type,
00631                                                  (void *) container,
00632                                                  ui64);
00633             break;
00634          case DBUS_TYPE_BYTE:
00635             dbus_message_iter_get_basic (&iter, &byte);
00636             EasyDbusDebug ("founded byte = %d", byte);
00637             easydbus_add_byte_param_to_skeleton (type,
00638                                                  (void *) container,
00639                                                  byte);
00640             break;
00641          case DBUS_TYPE_BOOLEAN:
00642             dbus_message_iter_get_basic (&iter, &byte);
00643             EasyDbusDebug ("founded boolean = %d", byte);
00644             easydbus_add_boolean_param_to_skeleton (type,
00645                                                     (void *) container,
00646                                                     byte);
00647             break;
00648          case DBUS_TYPE_DOUBLE:
00649             dbus_message_iter_get_basic (&iter, &d);
00650             EasyDbusDebug ("founded double = %f", d);
00651             easydbus_add_double_param_to_skeleton (type,
00652                                                    (void *) container, d);
00653             break;
00654          case DBUS_TYPE_OBJECT_PATH:
00655             dbus_message_iter_get_basic (&iter, &string);
00656             EasyDbusDebug ("founded obj_path = %s\n", string);
00657             easydbus_add_obj_path_param_to_skeleton (type,
00658                                                      (void *) container,
00659                                                      string);
00660             break;
00661          case DBUS_TYPE_SIGNATURE:
00662             dbus_message_iter_get_basic (&iter, &string);
00663             EasyDbusDebug ("founded signature = %s\n", string);
00664             easydbus_add_signature_param_to_skeleton (type,
00665                                                       (void *) container,
00666                                                       string);
00667             break;
00668          case DBUS_TYPE_ARRAY:
00669             cont_marshal_data =
00670                easydbus_get_marshal_data_from_array (type,
00671                                                      container,
00672                                                      dbus_message_iter_get_element_type
00673                                                      (&iter), &iter);
00674             if (cont_marshal_data) {
00675                easydbus_add_array_param_to_skeleton (type,
00676                                                      container,
00677                                                      cont_marshal_data);
00678                EasyDbusDebug ("Added array to container");
00679             }
00680             else
00681                EasyDbusDebug ("Error on manage array");
00682             break;
00683          case DBUS_TYPE_VARIANT:
00684             cont_marshal_data = easydbus_variant_build_elem
00685                (type, (void *) container, &iter);
00686             if (cont_marshal_data) {
00687                easydbus_add_variant_param_to_skeleton (type,
00688                                                        container,
00689                                                        cont_marshal_data);
00690                EasyDbusDebug ("Added variant to container");
00691             }
00692             else
00693                EasyDbusDebug ("Error on add variant to container");
00694             break;
00695          case DBUS_TYPE_DICT_ENTRY:
00696             // dict entry can be inserted only on an array
00697             goto error;
00698             break;
00699          case DBUS_TYPE_STRUCT:
00700             cont_marshal_data = easydbus_struct_build_elem
00701                (type, container, &iter);
00702             if (cont_marshal_data) {
00703                easydbus_add_struct_param_to_skeleton (type,
00704                                                       container,
00705                                                       cont_marshal_data);
00706                EasyDbusDebug ("Added struct to container");
00707             }
00708             else
00709                EasyDbusDebug ("Error on add struct to container");
00710             break;
00711       }
00712       if (dbus_message_iter_has_next (&iter))
00713          dbus_message_iter_next (&iter);
00714       else
00715          goto exit;
00716    }
00717  exit:
00718    return 0;
00719  error:
00720    return -1;
00721 }
00722 // vim: ts=3 shiftwidth=3 expandtab

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