Modify feedback operation
[platform/core/system/libsvi.git] / src / dbus.c
1 /*
2  * feedback
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <errno.h>
24
25 #include "common.h"
26 #include "log.h"
27 #include "dbus.h"
28
29 /* -1 is a default timeout value, it's converted to 25*1000 internally. */
30 #define DBUS_REPLY_TIMEOUT      (-1)
31
32 static int append_variant(DBusMessageIter *iter, const char *sig, char *param[])
33 {
34         char *ch;
35         int i;
36         int int_type;
37         uint64_t int64_type;
38         DBusMessageIter arr;
39         struct dbus_byte *byte;
40
41         if (!sig || !param)
42                 return 0;
43
44         for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) {
45                 switch (*ch) {
46                 case 'i':
47                         int_type = atoi(param[i]);
48                         dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &int_type);
49                         break;
50                 case 'u':
51                         int_type = strtoul(param[i], NULL, 10);
52                         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &int_type);
53                         break;
54                 case 't':
55                         int64_type = atoll(param[i]);
56                         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT64, &int64_type);
57                         break;
58                 case 's':
59                         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &param[i]);
60                         break;
61                 case 'a':
62                         ++i, ++ch;
63                         switch (*ch) {
64                         case 'y':
65                                 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &arr);
66                                 byte = (struct dbus_byte*)param[i];
67                                 dbus_message_iter_append_fixed_array(&arr, DBUS_TYPE_BYTE, &(byte->data), byte->size);
68                                 dbus_message_iter_close_container(iter, &arr);
69                                 break;
70                         default:
71                                 break;
72                         }
73                         break;
74                 default:
75                         return -EINVAL;
76                 }
77         }
78
79         return 0;
80 }
81
82 int dbus_method_sync(const char *dest, const char *path,
83                 const char *interface, const char *method,
84                 const char *sig, char *param[])
85 {
86         DBusConnection *conn;
87         DBusMessage *msg;
88         DBusMessageIter iter;
89         DBusMessage *reply;
90         DBusError err;
91         int ret, result;
92
93         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
94         if (!conn) {
95                 _E("dbus_bus_get error"); //LCOV_EXCL_LINE
96                 return -EPERM;
97         }
98
99         msg = dbus_message_new_method_call(dest, path, interface, method);
100         if (!msg) {
101                 _E("dbus_message_new_method_call(%s:%s-%s)", //LCOV_EXCL_LINE
102                         path, interface, method);
103                 return -EBADMSG;
104         }
105
106         dbus_message_iter_init_append(msg, &iter);
107         ret = append_variant(&iter, sig, param);
108         if (ret < 0) {
109                 _E("append_variant error(%d) %s %s:%s-%s", //LCOV_EXCL_LINE
110                         ret, dest, path, interface, method);
111                 dbus_message_unref(msg);
112                 return ret;
113         }
114
115         dbus_error_init(&err);
116
117         reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
118         dbus_message_unref(msg);
119         if (!reply) {
120                 _E("dbus_connection_send error(%s:%s) %s %s:%s-%s", //LCOV_EXCL_LINE
121                         err.name, err.message, dest, path, interface, method);
122                 dbus_error_free(&err); //LCOV_EXCL_LINE System Error
123                 return -ECOMM;
124         }
125
126         ret = dbus_message_get_args(reply, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
127         dbus_message_unref(reply);
128         if (!ret) {
129                 if (dbus_error_is_set(&err)) {
130                         _E("error : [%s:%s] %s %s:%s-%s", //LCOV_EXCL_LINE
131                                 err.name, err.message, dest, path, interface, method);
132                         dbus_error_free(&err); //LCOV_EXCL_LINE System Error
133                 } else
134                         _E("no message"); //LCOV_EXCL_LINE
135                 return -ENOMSG;
136         }
137
138         return result;
139 }