board: fix parameter of get_deviec_serial_number()
[platform/core/system/deviced.git] / src / board / board-info.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2019 - 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 <libsyscommon/libgdbus.h>
21
22 #include "core/log.h"
23 #include "core/devices.h"
24 #include <hal/device/hal-board.h>
25
26 #define SERIAL_MAX    128
27 static bool board_dev_available = false;
28
29 static GVariant *dbus_revision_handler(GDBusConnection *conn,
30                 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
31                 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
32 {
33         int ret;
34         int revision;
35
36         ret = hal_device_board_get_device_revision(&revision);
37         if (!board_dev_available || (ret == -ENODEV)) {
38                 _E("GetHWRev not supported.");
39                 ret = -ENOTSUP;
40                 goto revision_fail;
41         }
42
43         if (ret < 0) {
44                 _E("Fail to get revision.");
45                 goto revision_fail;
46         }
47
48         _D("Revision: %d", revision);
49
50         return g_variant_new("(i)", revision);
51
52 revision_fail:
53         return g_variant_new("(i)", ret);
54 }
55
56 static GVariant *dbus_num_handler(GDBusConnection *conn,
57                 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
58                 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
59 {
60         int ret;
61         GVariant *result;
62         char serial[SERIAL_MAX] = {0, };
63
64         ret = hal_device_board_get_device_serial_number(serial, SERIAL_MAX);
65         if (!board_dev_available || (ret == -ENODEV)) {
66                 _E("GetSerial not supported.");
67                 ret = -ENOTSUP;
68                 goto num_out;
69         }
70
71         if (ret < 0) {
72                 _E("Failed to get serial.");
73                 goto num_out;
74         }
75
76         _D("Num(%s) len(%zu).", serial, strlen(serial));
77
78 num_out:
79         if (ret < 0)
80                 result = g_variant_new("(si)", strdup(""), ret);
81         else
82                 result = g_variant_new("(si)", serial, ret);
83
84         return result;
85 }
86
87 static GVariant *dbus_serial_handler(GDBusConnection *conn,
88                 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
89                 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
90 {
91         int ret;
92         char *p;
93         GVariant *result;
94         char serial[SERIAL_MAX] = {0, };
95
96         ret = hal_device_board_get_device_serial_number(serial, SERIAL_MAX);
97         if (!board_dev_available || (ret == -ENODEV)) {
98                 _E("GetNum not supported.");
99                 ret = -ENOTSUP;
100                 goto seiral_out;
101         }
102
103         if (ret < 0) {
104                 _E("Failed to get num.");
105                 goto seiral_out;
106         }
107
108         p = strchr(serial, ',');
109         if (p)
110                 *p = '\0';
111
112         _D("Serial(%s) len(%zu).", serial, strlen(serial));
113
114 seiral_out:
115         if (ret < 0)
116                 result = g_variant_new("(si)", strdup(""), ret);
117         else
118                 result = g_variant_new("(si)", serial, ret);
119
120         return result;
121 }
122
123 static const dbus_method_s dbus_methods[] = {
124         { "GetSerial",  NULL, "si",     dbus_serial_handler },
125         { "GetHWRev",   NULL, "i",      dbus_revision_handler },
126         { "GetNum",             NULL, "si",     dbus_num_handler},
127 };
128
129 static const dbus_interface_u dbus_interface = {
130         .oh             = NULL,
131         .name           = DEVICED_INTERFACE_BOARD,
132         .methods        = dbus_methods,
133         .nr_methods = ARRAY_SIZE(dbus_methods),
134 };
135
136 static int board_probe(void *data)
137 {
138         int ret;
139
140         if (board_dev_available)
141                 return 0;
142
143         ret = hal_device_board_get_backend();
144         if (ret < 0) {
145                 _E("There is no HAL for board.");
146                 board_dev_available = false;
147
148                 return -ENODEV;
149         }
150
151         _I("Board device structure load success.");
152         board_dev_available = true;
153         return 0;
154 }
155
156 static void board_init(void *data)
157 {
158         int ret;
159
160         ret = gdbus_add_object(NULL, DEVICED_PATH_BOARD, &dbus_interface);
161         if (ret < 0)
162                 _E("Failed to init dbus method: %d", ret);
163 }
164
165 static const struct device_ops board_device_ops = {
166         DECLARE_NAME_LEN("board"),
167         .probe  = board_probe,
168         .init   = board_init,
169 };
170
171 DEVICE_OPS_REGISTER(&board_device_ops)