~bregma/geis/lp-785321

« back to all changes in this revision

Viewing changes to libs/geis-dbus/geis_dbus_device.c

  • Committer: Stephen M. Webb
  • Date: 2011-10-18 20:30:02 UTC
  • mfrom: (158.1.42 client-arch)
  • Revision ID: stephen.webb@canonical.com-20111018203002-ne8h3n25fbey3fqr
Merged client-arch branch.

Provided a new DBus-client back end and a dbus-server facility.  Included a test driver for the server.  Made the DBus client the default back-end with an automatic fall back to the XCB back end for seamless support of existing client software.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file geis_dbus_device.c
 
3
 * @brief Implementations of the GEIS DBus device transport.
 
4
 */
 
5
 
 
6
/*
 
7
 * Copyright 2011 Canonical Ltd.
 
8
 *
 
9
 * This library is free software; you can redistribute it and/or modify it under
 
10
 * the terms of the GNU Lesser General Public License as published by the Free
 
11
 * Software Foundation; either version 3 of the License, or (at your option) any
 
12
 * later version.
 
13
 *
 
14
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
16
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
17
 * details.
 
18
 *
 
19
 * You should have received a copy of the GNU Lesser General Public License
 
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 */
 
22
#include "geis_config.h"
 
23
#include "geis_dbus_device.h"
 
24
 
 
25
#include "geis_dbus.h"
 
26
#include "geis_dbus_attr.h"
 
27
#include "geis_device.h"
 
28
#include "geis_logging.h"
 
29
 
 
30
 
 
31
/*
 
32
 * Creates a Dbus "device available" message from a GEIS device.
 
33
 *
 
34
 * The Wire protocol for this message is device_id and device_name followed by
 
35
 * the list of device attributes.
 
36
 */
 
37
DBusMessage *
 
38
geis_dbus_device_available_message_from_device(GeisDevice device)
 
39
{
 
40
  DBusMessage *message = dbus_message_new_signal(GEIS_DBUS_SERVICE_PATH,
 
41
                                                 GEIS_DBUS_SERVICE_INTERFACE,
 
42
                                                 GEIS_DBUS_DEVICE_AVAILABLE);
 
43
  DBusMessageIter iter;
 
44
  dbus_message_iter_init_append(message, &iter);
 
45
 
 
46
  dbus_int32_t device_id = geis_device_id(device);
 
47
  dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &device_id);
 
48
 
 
49
  const char *device_name = geis_device_name(device);
 
50
  dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &device_name);
 
51
 
 
52
  DBusMessageIter array_iter;
 
53
  dbus_message_iter_open_container(&iter,
 
54
                                   DBUS_TYPE_ARRAY,
 
55
                                   GEIS_DBUS_TYPE_SIGNATURE_ATTR,
 
56
                                   &array_iter);
 
57
  GeisSize attr_count = geis_device_attr_count(device);
 
58
  for (GeisSize i = 0; i < attr_count; ++i)
 
59
  {
 
60
    geis_dbus_attr_marshall(geis_device_attr(device, i), &array_iter);
 
61
  }
 
62
  dbus_message_iter_close_container(&iter, &array_iter);
 
63
  return message;
 
64
}
 
65
 
 
66
 
 
67
/*
 
68
 * Creates GEIS device from a DBus "device available" message.
 
69
 */
 
70
GeisDevice
 
71
geis_dbus_device_device_from_available_message(DBusMessage *message)
 
72
{
 
73
  geis_debug("begins");
 
74
  GeisDevice device = NULL;
 
75
  DBusMessageIter iter;
 
76
  dbus_message_iter_init(message, &iter);
 
77
 
 
78
  int type = dbus_message_iter_get_arg_type(&iter);
 
79
  if (type != DBUS_TYPE_INT32)
 
80
  {
 
81
    geis_error("error getting device ID from DBus message.");
 
82
    goto final_exit;
 
83
  }
 
84
  dbus_int32_t device_id;
 
85
  dbus_message_iter_get_basic(&iter, &device_id);
 
86
 
 
87
  dbus_message_iter_next(&iter);
 
88
  type = dbus_message_iter_get_arg_type(&iter);
 
89
  if (type != DBUS_TYPE_STRING)
 
90
  {
 
91
    geis_error("error getting device name from DBus message.");
 
92
    goto final_exit;
 
93
  }
 
94
 
 
95
  char *device_name;
 
96
  dbus_message_iter_get_basic(&iter, &device_name);
 
97
  device = geis_device_new(device_name, device_id);
 
98
 
 
99
  dbus_message_iter_next(&iter);
 
100
  type = dbus_message_iter_get_arg_type(&iter);
 
101
  if (type != DBUS_TYPE_ARRAY)
 
102
  {
 
103
    geis_error("error getting device attr list from DBus message.");
 
104
    goto final_exit;
 
105
  }
 
106
 
 
107
  DBusMessageIter array_iter;
 
108
  dbus_message_iter_recurse(&iter, &array_iter);
 
109
  int atype = dbus_message_iter_get_arg_type(&array_iter);
 
110
  while (atype == DBUS_TYPE_DICT_ENTRY)
 
111
  {
 
112
    GeisAttr attr = geis_dbus_attr_unmarshall(&array_iter);
 
113
    if (attr)
 
114
    {
 
115
      geis_device_add_attr(device, attr);
 
116
    }
 
117
 
 
118
    dbus_message_iter_next(&array_iter);
 
119
    atype = dbus_message_iter_get_arg_type(&array_iter);
 
120
  }
 
121
 
 
122
final_exit:
 
123
  geis_debug("ends");
 
124
  return device;
 
125
}
 
126
 
 
127
 
 
128
/*
 
129
 * Creates a Dbus "device unavailable" message from a GEIS device.
 
130
 */
 
131
DBusMessage *
 
132
geis_dbus_device_unavailable_message_from_device(GeisDevice device GEIS_UNUSED)
 
133
{
 
134
  geis_debug("begins");
 
135
  DBusMessage *message = NULL;
 
136
  geis_debug("ends");
 
137
  return message;
 
138
}
 
139
 
 
140
 
 
141
/*
 
142
 * Creates GEIS device from a DBus "device unavailable" message.
 
143
 */
 
144
GeisDevice
 
145
geis_dbus_device_device_from_unavailable_message(DBusMessage *message GEIS_UNUSED)
 
146
{
 
147
  geis_debug("begins");
 
148
  GeisDevice device = NULL;
 
149
  geis_debug("ends");
 
150
  return device;
 
151
}
 
152