reply.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:  reply.c
00020  
00021   Description:  
00022  
00023   Version:  0.1
00024   Created:  06/19/07 23:15:41 CEST
00025   Revision: 
00026     - 0 created (Ge@@ru)
00027     - 1 add doxygen documentation and added support
00028         for opaque structs (Ge@@ru) - 11/02/07 21:25:00 CEST
00029     
00030  
00031   Author:   Daniele Rondina aka Ge@@ru (geaaru@gmail.com) 
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 "reply_internal.h"
00046 #include "elem_internal.h"
00047 #include "debug.h"
00048 #include "message_internal.h"
00049 
00063 int
00064 easydbus_reply_send_error (struct EasyDbus_core *core,
00065                            DBusMessage * msg, char *error_name,
00066                            char *error_string)
00067 {
00068    DBusMessage *error = NULL;
00069 
00070    if (!core || !msg || !error_name || !error_string) {
00071       EasyDbusDebug ("Error from input params");
00072       return -1;
00073    }
00074 
00075    error = dbus_message_new_error (msg,
00076                                    DBUS_ERROR_FAILED,
00077                                    (const char *) error_string);
00078 
00079    if (!error)
00080       return -1;
00081 
00082    if (easydbus_message_send (core, error, 1, NULL)) {
00083       dbus_message_unref (error);
00084       return -1;
00085    }
00086    dbus_message_unref (error);
00087    return 0;
00088 }
00089 
00100 inline int 
00101 easydbus_reply_set_type (EasyDbus_reply * reply, 
00102                          enum event_type type) 
00103 {
00104    if (!reply || (type != EASYDBUS_ET_REPLY && 
00105        type != EASYDBUS_ET_FAILURE))
00106       return -1;
00107 
00108    reply->type = type;
00109    return 0;
00110 }
00111 
00120 inline const EasyDbus_elem *
00121 easydbus_reply_get_element (const EasyDbus_reply * reply, 
00122                             unsigned int n) 
00123 {
00124    return (reply && n <= reply->args ? 
00125            easydbus_elem_get_element (reply->first, n) : NULL);
00126 }
00127 
00134 inline int 
00135 easydbus_reply_get_args (const EasyDbus_reply * reply) 
00136 {
00137    return (reply ? (int) reply->args : -1);
00138 }
00139 
00150 int
00151 easydbus_reply_send (struct EasyDbus_core *core,
00152                      DBusMessage * method_msg,
00153                      struct EasyDbus_reply *reply)
00154 {
00155    DBusMessage *msg = NULL;
00156    int ret = -1;
00157 
00158    if (!reply || !method_msg || !core) {
00159       EasyDbusDebug ("Error from input params");
00160       return -1;
00161    }
00162 
00163    msg = easydbus_reply_create_msg (core, reply, method_msg);
00164 
00165    if (msg &&
00166        !easydbus_message_send (core, msg, 1, NULL))
00167       ret = 0;
00168 
00169    if (msg)
00170       dbus_message_unref (msg);
00171 
00172    return ret;
00173 }
00174 
00187 inline DBusMessage *
00188 easydbus_reply_create_msg (struct EasyDbus_core * core,
00189                            struct EasyDbus_reply * reply,
00190                            DBusMessage * method_msg)
00191 {
00192    DBusMessage *msg = NULL;
00193 
00194    if (reply->type == EASYDBUS_ET_REPLY) {
00195       /* create msg */
00196       msg = dbus_message_new_method_return (method_msg);
00197    }
00198    else if (reply->type == EASYDBUS_ET_FAILURE) {
00199       if (reply->args == 1 &&
00200           reply->first->type == EASYDBUS_ELTYPE_STRING) {
00201          msg = dbus_message_new_error (method_msg,
00202                                        DBUS_ERROR_FAILED,
00203                                        (const char *) reply->first->
00204                                        payload.string);
00205       }
00206    }
00207 
00208    if (msg && 
00209        /* add method data if there are */
00210        (reply->args && reply->type == EASYDBUS_ET_REPLY))
00211       add_elems_to_msg (msg, reply->first);
00212 
00213    return msg;
00214 }
00215 
00222 inline void
00223 easydbus_reply_free_skeleton (EasyDbus_reply * reply) 
00224 {
00225    struct EasyDbus_elem *el = NULL;
00226 
00227    if (reply) {
00228       if (reply->src_path)
00229          free (reply->src_path);
00230       if (reply->interface)
00231          free (reply->interface);
00232       if (reply->sender)
00233          free (reply->sender);
00234       reply->src_path = reply->interface =
00235                         reply->sender = NULL;
00236 
00237       while (reply->args) {
00238          if (reply->first) {
00239             el = reply->first;
00240             reply->first = reply->first->next;
00241          }
00242          easydbus_elem_free (el);
00243          reply->args--;
00244       }
00245       free (reply);
00246    }
00247 }
00248 
00249 
00255 EasyDbus_reply *
00256 easydbus_reply_create_skeleton (void)
00257 {
00258    struct EasyDbus_reply *reply_skeleton = NULL;
00259 
00260    reply_skeleton = (struct EasyDbus_reply *) malloc
00261       (sizeof (struct EasyDbus_reply));
00262 
00263    if (reply_skeleton == NULL)
00264       return NULL;
00265 
00266    memset (reply_skeleton, 0, sizeof (struct EasyDbus_reply));
00267 
00268    reply_skeleton->src_path = reply_skeleton->interface = NULL;
00269    reply_skeleton->first = NULL;
00270    reply_skeleton->sender = NULL;
00271    reply_skeleton->type = EASYDBUS_ET_INVALID;
00272 
00273    return reply_skeleton;
00274 }
00275 
00285 int
00286 easydbus_reply_build_error (struct EasyDbus_method *method,
00287                             const char *error_msg)
00288 {
00289    struct EasyDbus_reply *reply_foruser = NULL;
00290 
00291    reply_foruser = easydbus_reply_create_skeleton ();
00292    if (!reply_foruser)
00293       return -1;
00294 
00295    if (easydbus_add_string_param_to_skeleton
00296        (EASYDBUS_ET_REPLY, reply_foruser, (char *) error_msg))
00297       goto out_of_memory;
00298 
00299    reply_foruser->type = EASYDBUS_ET_FAILURE;
00300    method->reply = reply_foruser;
00301 
00302    return 0;
00303  out_of_memory:
00304    if (reply_foruser)
00305       easydbus_reply_free_skeleton (reply_foruser);
00306    return -1;
00307 }
00308 
00318 int
00319 easydbus_reply_build_struct (struct EasyDbus_method *method,
00320                              DBusMessage * reply)
00321 {
00322    struct EasyDbus_reply *reply_foruser = NULL;
00323    const char *temp = NULL;
00324    int type_msg = 0;
00325 
00326    reply_foruser = easydbus_reply_create_skeleton ();
00327 
00328    if (!reply_foruser)
00329       return -1;
00330 
00331    temp = dbus_message_get_path (reply);
00332    /* see of manage case where path == NULL */
00333    //if (temp == NULL) goto out_of_memory_error;
00334 
00335    if (temp)
00336       if (easydbus_reply_set_path (reply_foruser, temp))
00337          goto out_of_memory_error;
00338 
00339    temp = dbus_message_get_interface (reply);
00340    /* see of manage case where path == NULL */
00341    //if (temp == NULL) goto out_of_memory_error;
00342 
00343    if (temp)
00344       if (easydbus_reply_set_interface (reply_foruser,
00345                                         temp))
00346          goto out_of_memory_error;
00347 
00348    type_msg = dbus_message_get_type (reply);
00349    if (type_msg == DBUS_MESSAGE_TYPE_METHOD_RETURN)
00350       reply_foruser->type = EASYDBUS_ET_REPLY;
00351    else
00352       reply_foruser->type = EASYDBUS_ET_INVALID;
00353 
00354    easydbus_build_skeleton_data (reply, EASYDBUS_ET_REPLY,
00355                                  reply_foruser);
00356 
00357    method->reply = reply_foruser;
00358    EasyDbusDebug ("Created reply object");
00359    return 0;
00360 
00361 out_of_memory_error:
00362 
00363    if (reply_foruser)
00364       easydbus_reply_free_skeleton (reply_foruser);
00365 
00366    return -1;
00367 }
00368 
00376 enum event_type
00377 easydbus_reply_get_type (EasyDbus_reply * reply)
00378 {
00379    return (reply ? reply->type : EASYDBUS_ET_INVALID);
00380 }
00381 
00388 const char *
00389 easydbus_reply_get_src_path (EasyDbus_reply * reply)
00390 {
00391    return (reply ? (const char *) reply->src_path : NULL);
00392 }
00393 
00400 const char *
00401 easydbus_reply_get_interface (EasyDbus_reply * reply)
00402 {
00403    return (reply ? (const char *) reply->interface : NULL);
00404 }
00405 
00414 const char *
00415 easydbus_reply_get_sender (EasyDbus_reply * reply)
00416 {
00417   return (reply ? (const char *) reply->sender : NULL);
00418 }
00419 
00429 inline int
00430 easydbus_reply_set_sender (EasyDbus_reply * reply, 
00431                            const char *sender)
00432 {
00433    unsigned int string_length = 0;
00434 
00435    if (!sender || !reply || reply->sender) 
00436       return -1;
00437 
00438    string_length = strlen (sender) + 1;
00439    reply->sender = (char *) malloc (string_length);
00440    if (!reply->sender)
00441       return -1;
00442 
00443    EASYDBUS_MEMCOPY (reply->sender, sender, string_length);
00444 
00445    return 0;
00446 }
00447 
00457 inline int
00458 easydbus_reply_set_interface (EasyDbus_reply * reply, 
00459                               const char *interface)
00460 {
00461    unsigned int string_length = 0;
00462 
00463    if (!interface || !reply || reply->interface) 
00464       return -1;
00465 
00466    string_length = strlen (interface) + 1;
00467    reply->interface = (char *) malloc (string_length);
00468    if (!reply->interface)
00469       return -1;
00470 
00471    EASYDBUS_MEMCOPY (reply->interface, interface,
00472                      string_length);
00473 
00474    return 0;
00475 }
00476 
00486 inline int
00487 easydbus_reply_set_path (EasyDbus_reply * reply, 
00488                          const char *path)
00489 {
00490    unsigned int string_length = 0;
00491 
00492    if (!path || !reply || reply->src_path) 
00493       return -1;
00494 
00495    string_length = strlen (path) + 1;
00496    reply->src_path = (char *) malloc (string_length);
00497    if (!reply->src_path)
00498       return -1;
00499 
00500    EASYDBUS_MEMCOPY (reply->src_path, path,
00501                      string_length);
00502 
00503    return 0;
00504 }
00505 
00506 // vim: ts=3 shiftwidth=3 expandtab

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