Move monitoring & caching logic to daemon
[platform/core/iot/iotcon.git] / lib / icl-device.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <stdio.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <glib.h>
22
23 #include "iotcon.h"
24 #include "ic-utils.h"
25 #include "icl.h"
26 #include "icl-representation.h"
27 #include "icl-dbus.h"
28 #include "icl-dbus-type.h"
29 #include "icl-device.h"
30
31 /**
32  * @brief The maximum length which can be held in a manufacturer name.
33  *
34  * @since_tizen 3.0
35  */
36 #define ICL_MANUFACTURER_NAME_LENGTH_MAX 15
37
38 /**
39  * @brief The maximum length which can be held in a manufacturer url.
40  *
41  * @since_tizen 3.0
42  */
43 #define ICL_MANUFACTURER_URL_LENGTH_MAX 32
44
45
46 typedef struct {
47         iotcon_device_info_cb cb;
48         void *user_data;
49         unsigned int id;
50         int timeout_id;
51 } icl_device_info_s;
52
53 typedef struct {
54         iotcon_platform_info_cb cb;
55         void *user_data;
56         unsigned int id;
57         int timeout_id;
58 } icl_platform_info_s;
59
60 typedef struct {
61         iotcon_tizen_info_cb cb;
62         void *user_data;
63 } icl_tizen_info_s;
64
65
66 API int iotcon_device_info_get_property(iotcon_device_info_h device_info,
67                 iotcon_device_info_e property, char **value)
68 {
69         RETV_IF(NULL == device_info, IOTCON_ERROR_INVALID_PARAMETER);
70         RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
71
72         switch (property) {
73         case IOTCON_DEVICE_INFO_NAME:
74                 *value = device_info->device_name;
75                 break;
76         case IOTCON_DEVICE_INFO_SPEC_VER:
77                 *value = device_info->spec_ver;
78                 break;
79         case IOTCON_DEVICE_INFO_ID:
80                 *value = device_info->device_id;
81                 break;
82         case IOTCON_DEVICE_INFO_DATA_MODEL_VER:
83                 *value = device_info->data_model_ver;
84                 break;
85         default:
86                 ERR("Invalid property(%d)", property);
87                 return IOTCON_ERROR_INVALID_PARAMETER;
88         }
89
90         return IOTCON_ERROR_NONE;
91 }
92
93 static void _icl_device_info_cb(GDBusConnection *connection,
94                 const gchar *sender_name,
95                 const gchar *object_path,
96                 const gchar *interface_name,
97                 const gchar *signal_name,
98                 GVariant *parameters,
99                 gpointer user_data)
100 {
101         char *uri_path;
102         struct icl_device_info info = {0};
103         icl_device_info_s *cb_container = user_data;
104         iotcon_device_info_cb cb = cb_container->cb;
105
106         if (cb_container->timeout_id)
107                 cb_container->timeout_id = 0;
108
109         g_variant_get(parameters, "(&s&s&s&s&s)", &uri_path, &info.device_name,
110                         &info.spec_ver, &info.device_id, &info.data_model_ver);
111
112         /* From iotivity, we can get uri_path. But, the value is always "/oic/d". */
113
114         if (cb)
115                 cb(&info, IOTCON_ERROR_NONE, cb_container->user_data);
116 }
117
118 static gboolean _icl_timeout_get_device_info(gpointer p)
119 {
120         FN_CALL;
121         icl_device_info_s *cb_container = p;
122         struct icl_device_info info = {0};
123
124         if (NULL == cb_container) {
125                 ERR("cb_container is NULL");
126                 return G_SOURCE_REMOVE;
127         }
128
129         if (cb_container->timeout_id && cb_container->cb)
130                 cb_container->cb(&info, IOTCON_ERROR_TIMEOUT, cb_container->user_data);
131
132         icl_dbus_unsubscribe_signal(cb_container->id);
133
134         return G_SOURCE_REMOVE;
135 }
136
137 API int iotcon_get_device_info(const char *host_address,
138                 iotcon_connectivity_type_e connectivity_type,
139                 iotcon_device_info_cb cb,
140                 void *user_data)
141 {
142         int ret;
143         unsigned int sub_id;
144         GError *error = NULL;
145         icl_device_info_s *cb_container;
146         int64_t signal_number;
147         char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
148
149         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
150         RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
151
152         ic_dbus_call_get_device_info_sync(icl_dbus_get_object(),
153                         ic_utils_dbus_encode_str(host_address),
154                         connectivity_type,
155                         &signal_number,
156                         &ret,
157                         NULL,
158                         &error);
159         if (error) {
160                 ERR("ic_dbus_call_get_device_info_sync() Fail(%s)", error->message);
161                 ret = icl_dbus_convert_dbus_error(error->code);
162                 g_error_free(error);
163                 return ret;
164         }
165
166         if (IOTCON_ERROR_NONE != ret) {
167                 ERR("iotcon-daemon Fail(%d)", ret);
168                 return icl_dbus_convert_daemon_error(ret);
169         }
170
171         snprintf(signal_name, sizeof(signal_name), "%s_%llx", IC_DBUS_SIGNAL_DEVICE,
172                         signal_number);
173
174         cb_container = calloc(1, sizeof(icl_device_info_s));
175         if (NULL == cb_container) {
176                 ERR("calloc() Fail(%d)", errno);
177                 return IOTCON_ERROR_OUT_OF_MEMORY;
178         }
179
180         cb_container->cb = cb;
181         cb_container->user_data = user_data;
182
183         sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, free,
184                         _icl_device_info_cb);
185         if (0 == sub_id) {
186                 ERR("icl_dbus_subscribe_signal() Fail");
187                 free(cb_container);
188                 return IOTCON_ERROR_DBUS;
189         }
190
191         cb_container->id = sub_id;
192
193         cb_container->timeout_id = g_timeout_add_seconds(icl_dbus_get_timeout(),
194                         _icl_timeout_get_device_info, cb_container);
195
196         return ret;
197 }
198
199 API int iotcon_platform_info_get_property(iotcon_platform_info_h platform_info,
200                 iotcon_platform_info_e property, char **value)
201 {
202         RETV_IF(NULL == platform_info, IOTCON_ERROR_INVALID_PARAMETER);
203         RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
204
205         switch (property) {
206         case IOTCON_PLATFORM_INFO_ID:
207                 *value = platform_info->platform_id;
208                 break;
209         case IOTCON_PLATFORM_INFO_MANUF_NAME:
210                 *value = platform_info->manuf_name;
211                 break;
212         case IOTCON_PLATFORM_INFO_MANUF_URL:
213                 *value = platform_info->manuf_url;
214                 break;
215         case IOTCON_PLATFORM_INFO_MODEL_NUMBER:
216                 *value = platform_info->model_number;
217                 break;
218         case IOTCON_PLATFORM_INFO_DATE_OF_MANUF:
219                 *value = platform_info->date_of_manuf;
220                 break;
221         case IOTCON_PLATFORM_INFO_PLATFORM_VER:
222                 *value = platform_info->platform_ver;
223                 break;
224         case IOTCON_PLATFORM_INFO_OS_VER:
225                 *value = platform_info->os_ver;
226                 break;
227         case IOTCON_PLATFORM_INFO_HARDWARE_VER:
228                 *value = platform_info->hardware_ver;
229                 break;
230         case IOTCON_PLATFORM_INFO_FIRMWARE_VER:
231                 *value = platform_info->firmware_ver;
232                 break;
233         case IOTCON_PLATFORM_INFO_SUPPORT_URL:
234                 *value = platform_info->support_url;
235                 break;
236         case IOTCON_PLATFORM_INFO_SYSTEM_TIME:
237                 *value = platform_info->system_time;
238                 break;
239         default:
240                 ERR("Invalid property(%d)", property);
241                 return IOTCON_ERROR_INVALID_PARAMETER;
242         }
243
244         return IOTCON_ERROR_NONE;
245 }
246
247
248 static void _icl_platform_info_cb(GDBusConnection *connection,
249                 const gchar *sender_name,
250                 const gchar *object_path,
251                 const gchar *interface_name,
252                 const gchar *signal_name,
253                 GVariant *parameters,
254                 gpointer user_data)
255 {
256         char *uri_path;
257         struct icl_platform_info info = {0};
258         icl_platform_info_s *cb_container = user_data;
259         iotcon_platform_info_cb cb = cb_container->cb;
260
261         if (cb_container->timeout_id)
262                 cb_container->timeout_id = 0;
263
264         g_variant_get(parameters, "(&s&s&s&s&s&s&s&s&s&s&s&s)",
265                         &uri_path,
266                         &info.platform_id,
267                         &info.manuf_name,
268                         &info.manuf_url,
269                         &info.model_number,
270                         &info.date_of_manuf,
271                         &info.platform_ver,
272                         &info.os_ver,
273                         &info.hardware_ver,
274                         &info.firmware_ver,
275                         &info.support_url,
276                         &info.system_time);
277
278         info.manuf_url = ic_utils_dbus_decode_str(info.manuf_url);
279         info.model_number = ic_utils_dbus_decode_str(info.model_number);
280         info.date_of_manuf = ic_utils_dbus_decode_str(info.date_of_manuf);
281         info.platform_ver = ic_utils_dbus_decode_str(info.platform_ver);
282         info.os_ver = ic_utils_dbus_decode_str(info.os_ver);
283         info.hardware_ver = ic_utils_dbus_decode_str(info.hardware_ver);
284         info.firmware_ver = ic_utils_dbus_decode_str(info.firmware_ver);
285         info.support_url = ic_utils_dbus_decode_str(info.support_url);
286         info.system_time = ic_utils_dbus_decode_str(info.system_time);
287
288         /* From iotivity, we can get uri_path. But, the value is always "/oic/p". */
289
290         if (cb)
291                 cb(&info, IOTCON_ERROR_NONE, cb_container->user_data);
292 }
293
294 static gboolean _icl_timeout_get_platform_info(gpointer p)
295 {
296         FN_CALL;
297         icl_platform_info_s *cb_container = p;
298         struct icl_platform_info info = {0};
299
300         if (NULL == cb_container) {
301                 ERR("cb_container is NULL");
302                 return G_SOURCE_REMOVE;
303         }
304
305         if (cb_container->timeout_id && cb_container->cb)
306                 cb_container->cb(&info, IOTCON_ERROR_TIMEOUT, cb_container->user_data);
307
308         icl_dbus_unsubscribe_signal(cb_container->id);
309
310         return G_SOURCE_REMOVE;
311 }
312
313 API int iotcon_get_platform_info(const char *host_address,
314                 iotcon_connectivity_type_e connectivity_type,
315                 iotcon_platform_info_cb cb,
316                 void *user_data)
317 {
318         int ret;
319         unsigned int sub_id;
320         GError *error = NULL;
321         icl_platform_info_s *cb_container;
322         int64_t signal_number;
323         char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
324
325         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
326         RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
327
328         ic_dbus_call_get_platform_info_sync(icl_dbus_get_object(),
329                         ic_utils_dbus_encode_str(host_address),
330                         connectivity_type,
331                         &signal_number,
332                         &ret,
333                         NULL,
334                         &error);
335         if (error) {
336                 ERR("ic_dbus_call_get_platform_info_sync() Fail(%s)", error->message);
337                 ret = icl_dbus_convert_dbus_error(error->code);
338                 g_error_free(error);
339                 return ret;
340         }
341
342         if (IOTCON_ERROR_NONE != ret) {
343                 ERR("iotcon-daemon Fail(%d)", ret);
344                 return icl_dbus_convert_daemon_error(ret);
345         }
346
347         snprintf(signal_name, sizeof(signal_name), "%s_%llx", IC_DBUS_SIGNAL_PLATFORM,
348                         signal_number);
349
350         cb_container = calloc(1, sizeof(icl_platform_info_s));
351         if (NULL == cb_container) {
352                 ERR("calloc() Fail(%d)", errno);
353                 return IOTCON_ERROR_OUT_OF_MEMORY;
354         }
355
356         cb_container->cb = cb;
357         cb_container->user_data = user_data;
358
359         sub_id = icl_dbus_subscribe_signal(signal_name, cb_container, free,
360                         _icl_platform_info_cb);
361         if (0 == sub_id) {
362                 ERR("icl_dbus_subscribe_signal() Fail");
363                 free(cb_container);
364                 return IOTCON_ERROR_DBUS;
365         }
366
367         cb_container->id = sub_id;
368         cb_container->timeout_id = g_timeout_add_seconds(icl_dbus_get_timeout(),
369                         _icl_timeout_get_platform_info, cb_container);
370
371         return ret;
372 }
373
374
375 static void _icl_tizen_info_cb(GObject *object, GAsyncResult *g_async_res,
376                 gpointer user_data)
377 {
378         int res;
379         GVariant *result;
380         char *device_name;
381         char *tizen_device_id;
382         GError *error = NULL;
383         struct icl_tizen_info info = {0};
384         icl_tizen_info_s *cb_container = user_data;
385         iotcon_tizen_info_cb cb = cb_container->cb;
386
387         ic_dbus_call_get_tizen_info_finish(IC_DBUS(object), &result, g_async_res, &error);
388         if (error) {
389                 ERR("ic_dbus_call_get_tizen_info_finish() Fail(%s)", error->message);
390                 if (cb) {
391                         int ret = icl_dbus_convert_dbus_error(error->code);
392                         cb(&info, ret, cb_container->user_data);
393                 }
394                 g_error_free(error);
395                 /* TODO contain time out error */
396                 free(cb_container);
397                 return;
398         }
399
400         g_variant_get(result, "(&s&si)", &device_name, &tizen_device_id, &res);
401
402         if (IOTCON_ERROR_NONE == res && NULL != ic_utils_dbus_decode_str(tizen_device_id)) {
403                 info.device_name = ic_utils_dbus_decode_str(device_name);
404                 info.tizen_device_id = tizen_device_id;
405         }
406
407         if (cb)
408                 cb(&info, res, cb_container->user_data);
409
410         free(cb_container);
411 }
412
413
414 API int iotcon_get_tizen_info(const char *host_address,
415                 iotcon_connectivity_type_e connectivity_type,
416                 iotcon_tizen_info_cb cb,
417                 void *user_data)
418 {
419         icl_tizen_info_s *cb_container;
420
421         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
422         RETV_IF(NULL == host_address, IOTCON_ERROR_INVALID_PARAMETER);
423         RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
424
425         cb_container = calloc(1, sizeof(icl_tizen_info_s));
426         if (NULL == cb_container) {
427                 ERR("calloc() Fail(%d)", errno);
428                 return IOTCON_ERROR_OUT_OF_MEMORY;
429         }
430
431         cb_container->cb = cb;
432         cb_container->user_data = user_data;
433
434         ic_dbus_call_get_tizen_info(icl_dbus_get_object(), host_address, connectivity_type,
435                         NULL, _icl_tizen_info_cb, cb_container);
436
437         return IOTCON_ERROR_NONE;
438 }
439
440
441 API int iotcon_tizen_info_get_property(iotcon_tizen_info_h tizen_info,
442                 iotcon_tizen_info_e property, char **value)
443 {
444         RETV_IF(NULL == tizen_info, IOTCON_ERROR_INVALID_PARAMETER);
445         RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
446
447         switch (property) {
448         case IOTCON_TIZEN_INFO_DEVICE_NAME:
449                 *value = tizen_info->device_name;
450                 break;
451         case IOTCON_TIZEN_INFO_TIZEN_DEVICE_ID:
452                 *value = tizen_info->tizen_device_id;
453                 break;
454         default:
455                 ERR("Invalid property(%d)", property);
456                 return IOTCON_ERROR_INVALID_PARAMETER;
457         }
458
459         return IOTCON_ERROR_NONE;
460 }