Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 328 lines (224 sloc) 7.147 kb
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
/*
* Copyright (c) 2012 Novell, Inc.
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail, you may
* find current contact information at www.novell.com.
*/


#ifndef SNAPPER_DBUSMESSAGE_H
#define SNAPPER_DBUSMESSAGE_H


#include <dbus/dbus.h>

#include <string>
#include <vector>
#include <list>
#include <map>


namespace DBus
{
    using std::string;
    using std::vector;
    using std::list;
    using std::map;


    struct Exception : public std::exception
    {
explicit Exception() throw() {}
virtual const char* what() const throw() { return "dbus generic exception"; }
    };


    struct ErrorException : public Exception
    {
explicit ErrorException(const DBusError err) throw() : err(err) {}
virtual ~ErrorException() throw() { dbus_error_free(&err); }
virtual const char* what() const throw() { return "dbus error exception"; }
virtual const char* name() const throw() { return err.name; }
virtual const char* message() const throw() { return err.message; }
DBusError err;
    };


    struct MarshallingException : public Exception
    {
explicit MarshallingException() throw() {}
virtual const char* what() const throw() { return "dbus marshalling exception"; }
    };


    struct FatalException : public Exception
    {
explicit FatalException() throw() {}
virtual const char* what() const throw() { return "dbus fatal exception"; }
    };


    class Message
    {
    public:

Message(DBusMessage* m, bool ref);
Message(const Message& m);
~Message();

Message& operator=(const Message& m);

DBusMessage* get_message() { return msg; }

int get_type() const { return dbus_message_get_type(msg); }
string get_member() const { return dbus_message_get_member(msg); }
string get_sender() const { return dbus_message_get_sender(msg); }
string get_path() const { return dbus_message_get_path(msg); }
string get_interface() const { return dbus_message_get_interface(msg); }
string get_error_name() const { return dbus_message_get_error_name(msg); }

bool is_method_call(const char* interface, const char* method) const
{
return dbus_message_is_method_call(msg, interface, method);
}

bool is_signal(const char* interface, const char* name) const
{
return dbus_message_is_signal(msg, interface, name);
}

    private:

DBusMessage* msg;

    };


    class MessageMethodCall : public Message
    {
    public:

MessageMethodCall(const char* service, const char* object, const char* interface,
const char* method)
: Message(dbus_message_new_method_call(service, object, interface, method), false)
{
}

    };


    class MessageMethodReturn : public Message
    {
    public:

MessageMethodReturn(Message& m)
: Message(dbus_message_new_method_return(m.get_message()), false)
{
if (m.get_type() != DBUS_MESSAGE_TYPE_METHOD_CALL)
throw FatalException();
}

    };


    class MessageError : public Message
    {
    public:

MessageError(Message& m, const char* error_msg, const char* error_code)
: Message(dbus_message_new_error(m.get_message(), error_msg, error_code), false)
{
if (m.get_type() != DBUS_MESSAGE_TYPE_METHOD_CALL)
throw FatalException();
}

    };


    class MessageSignal : public Message
    {
    public:

MessageSignal(const char* path, const char* interface, const char* name)
: Message(dbus_message_new_signal(path, interface, name), false)
{
}

    };


    template <typename Type> struct TypeInfo {};

    template <> struct TypeInfo<dbus_uint32_t> { static const char* signature; };
    template <> struct TypeInfo<string> { static const char* signature; };


    class Marshalling
    {

    public:

DBusMessageIter* top() { return iters.back(); }

int get_type() { return dbus_message_iter_get_arg_type(top()); }
string get_signature() { return dbus_message_iter_get_signature(top()); }

    protected:

vector<DBusMessageIter*> iters;

    };


    class Hihi : public Marshalling
    {

    public:

Hihi(Message& msg);
~Hihi();

void open_recurse();
void close_recurse();

static string unescape(const string&);

    };


    class Hoho : public Marshalling
    {

    public:

Hoho(Message& msg);
~Hoho();

void open_struct();
void close_struct();

void open_array(const char* signature);
void close_array();

void open_dict_entry();
void close_dict_entry();

static string escape(const string&);

    };


    Hihi& operator>>(Hihi& hihi, bool& data);
    Hoho& operator<<(Hoho& hoho, bool data);

    Hihi& operator>>(Hihi& hihi, dbus_uint16_t& data);
    Hoho& operator<<(Hoho& hoho, dbus_uint16_t data);

    Hihi& operator>>(Hihi& hihi, dbus_uint32_t& data);
    Hoho& operator<<(Hoho& hoho, dbus_uint32_t data);

    Hihi& operator>>(Hihi& hihi, time_t& data);
    Hoho& operator<<(Hoho& hoho, time_t data);

    Hoho& operator<<(Hoho& hoho, const char* data);

    Hihi& operator>>(Hihi& hihi, string& data);
    Hoho& operator<<(Hoho& hoho, const string& data);

    Hihi& operator>>(Hihi& hihi, map<string, string>& data);
    Hoho& operator<<(Hoho& hoho, const map<string, string>& data);


    template <typename Type>
    Hihi& operator>>(Hihi& hihi, vector<Type>& data)
    {
if (hihi.get_type() != DBUS_TYPE_ARRAY)
throw MarshallingException();

hihi.open_recurse();

while (hihi.get_type() != DBUS_TYPE_INVALID)
{
if (hihi.get_signature() != TypeInfo<Type>::signature)
throw MarshallingException();

Type tmp;
hihi >> tmp;
data.push_back(tmp);
}

hihi.close_recurse();

return hihi;
    }


    template <typename Type>
    Hoho& operator<<(Hoho& hoho, const vector<Type>& data)
    {
hoho.open_array(TypeInfo<Type>::signature);

for (typename vector<Type>::const_iterator it = data.begin(); it != data.end(); ++it)
{
hoho << *it;
}

hoho.close_array();

return hoho;
    }


    template <typename Type>
    Hihi& operator>>(Hihi& hihi, list<Type>& data)
    {
if (hihi.get_type() != DBUS_TYPE_ARRAY)
throw MarshallingException();

hihi.open_recurse();

while (hihi.get_type() != DBUS_TYPE_INVALID)
{
if (hihi.get_signature() != TypeInfo<Type>::signature)
throw MarshallingException();

Type tmp;
hihi >> tmp;
data.push_back(tmp);
}

hihi.close_recurse();

return hihi;
    }


    template <typename Type>
    Hoho& operator<<(Hoho& hoho, const list<Type>& data)
    {
hoho.open_array(TypeInfo<Type>::signature);

for (typename list<Type>::const_iterator it = data.begin(); it != data.end(); ++it)
{
hoho << *it;
}

hoho.close_array();

return hoho;
    }

}


#endif
Something went wrong with that request. Please try again.