summaryrefslogtreecommitdiff
path: root/ytstenut/profile/yts-profile-adapter.c
blob: 16d02e5840e8b9433c8bf845ad8844bac5fc00a9 (plain)
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
/*
 * Copyright © 2011 Intel Corp.
 *
 * This  library is free  software; you can  redistribute it and/or
 * modify it  under  the terms  of the  GNU Lesser  General  Public
 * License  as published  by the Free  Software  Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authored by: Rob Staudinger <robsta@linux.intel.com>
 */

#include "config.h"

#include <stdbool.h>

#include "yts-profile.h"
#include "yts-profile-adapter.h"

G_DEFINE_TYPE (YtsProfileAdapter,
               yts_profile_adapter,
               YTS_TYPE_SERVICE_ADAPTER)

#define GET_PRIVATE(o)                                      \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o),                        \
                                YTS_TYPE_PROFILE_ADAPTER,  \
                                YtsProfileAdapterPrivate))

typedef struct {
  YtsProfile *profile;
} YtsProfileAdapterPrivate;

/*
 * YtsServiceAdapter overrides
 */

static bool
_service_adapter_invoke (YtsServiceAdapter *self,
                         char const         *invocation_id,
                         char const         *aspect,
                         GVariant           *arguments)
{
  YtsProfileAdapterPrivate *priv = GET_PRIVATE (self);

  /* Methods */

  if (0 == g_strcmp0 ("register-proxy", aspect) &&
      arguments &&
      g_variant_is_of_type (arguments, G_VARIANT_TYPE_STRING)) {

    char const *capability = g_variant_get_string (arguments, NULL);
    yts_profile_register_proxy (priv->profile,
                                 invocation_id,
                                 capability);

  } else if (0 == g_strcmp0 ("unregister-proxy", aspect) &&
             arguments &&
             g_variant_is_of_type (arguments, G_VARIANT_TYPE_STRING)) {

    char const *capability = g_variant_get_string (arguments, NULL);
    yts_profile_unregister_proxy (priv->profile,
                                   invocation_id,
                                   capability);

  } else {

    char *arg_string = arguments ?
                          g_variant_print (arguments, false) :
                          g_strdup ("NULL");
    g_warning ("%s : Unknown method %s.%s(%s)",
               G_STRLOC,
               G_OBJECT_TYPE_NAME (priv->profile),
               aspect,
               arg_string);
    g_free (arg_string);
  }

  /* No need to keep the return SAE. */
  return false;
}

/*
 * YtsProfileAdapter
 */

enum {
  PROP_0 = 0,
  PROP_SERVICE_ADAPTER_FQC_ID,
  PROP_SERVICE_ADAPTER_SERVICE
};

static void
_profile_register_proxy_response (YtsProfile         *profile,
                                  char const          *invocation_id,
                                  GVariant            *return_value,
                                  YtsProfileAdapter  *self)
{
  yts_service_adapter_send_response (YTS_SERVICE_ADAPTER (self),
                                      invocation_id,
                                      return_value);
}

static void
_profile_unregister_proxy_response (YtsProfile         *profile,
                                    char const          *invocation_id,
                                    bool                 return_value,
                                    YtsProfileAdapter  *self)
{
  yts_service_adapter_send_response (YTS_SERVICE_ADAPTER (self),
                                      invocation_id,
                                      g_variant_new_boolean (return_value));
}

static void
_profile_destroyed (YtsProfileAdapter  *self,
                    void                *stale_profile_ptr)
{
  YtsProfileAdapterPrivate *priv = GET_PRIVATE (self);

  priv->profile = NULL;
  g_object_unref (self);
}

static void
_get_property (GObject      *object,
               unsigned int  property_id,
               GValue       *value,
               GParamSpec   *pspec)
{
  YtsProfileAdapterPrivate *priv = GET_PRIVATE (object);

  switch (property_id) {
    case PROP_SERVICE_ADAPTER_FQC_ID:
      g_value_set_string (value, YTS_PROFILE_FQC_ID);
      break;
    case PROP_SERVICE_ADAPTER_SERVICE:
      g_value_set_object (value, priv->profile);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
_set_property (GObject      *object,
               unsigned int  property_id,
               const GValue *value,
               GParamSpec   *pspec)
{
  YtsProfileAdapterPrivate *priv = GET_PRIVATE (object);

  switch (property_id) {

    case PROP_SERVICE_ADAPTER_SERVICE:

      /* Construct-only */

      g_return_if_fail (priv->profile == NULL);
      g_return_if_fail (YTS_IS_PROFILE (g_value_get_object (value)));

      priv->profile = YTS_PROFILE (g_value_get_object (value));
      g_object_weak_ref (G_OBJECT (priv->profile),
                         (GWeakNotify) _profile_destroyed,
                         object);

      g_signal_connect (priv->profile, "register-proxy-response",
                        G_CALLBACK (_profile_register_proxy_response), object);
      g_signal_connect (priv->profile, "unregister-proxy-response",
                        G_CALLBACK (_profile_unregister_proxy_response), object);

      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
_dispose (GObject *object)
{
  YtsProfileAdapterPrivate *priv = GET_PRIVATE (object);

  if (priv->profile) {
    g_warning ("%s : Adapter disposed with adaptee still referenced.",
               G_STRLOC);
    g_object_weak_unref (G_OBJECT (priv->profile),
                         (GWeakNotify) _profile_destroyed,
                         object);
    priv->profile = NULL;
  }

  G_OBJECT_CLASS (yts_profile_adapter_parent_class)->dispose (object);
}

static void
yts_profile_adapter_class_init (YtsProfileAdapterClass *klass)
{
  GObjectClass  *object_class = G_OBJECT_CLASS (klass);
  YtsServiceAdapterClass *adapter_class = YTS_SERVICE_ADAPTER_CLASS (klass);

  g_type_class_add_private (klass, sizeof (YtsProfileAdapterPrivate));

  object_class->get_property = _get_property;
  object_class->set_property = _set_property;
  object_class->dispose = _dispose;

  adapter_class->invoke = _service_adapter_invoke;

  /* Properties */

  g_object_class_override_property (object_class,
                                    PROP_SERVICE_ADAPTER_FQC_ID,
                                    "fqc-id");

  g_object_class_override_property (object_class,
                                    PROP_SERVICE_ADAPTER_SERVICE,
                                    "service");
}

static void
yts_profile_adapter_init (YtsProfileAdapter *self)
{
}