Initialize Tizen 2.3
[framework/system/deviced.git] / src / board / board-info.c
1 /*
2  * deviced
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 <fcntl.h>
21
22 #include "core/log.h"
23 #include "core/devices.h"
24 #include "core/edbus-handler.h"
25 #include "core/common.h"
26
27 #define FILE_BUFF_MAX   1024
28 #define NOT_INITIALIZED (-1)
29
30 #define METHOD_GET_SERIAL       "GetSerial"
31 #define SERIAL_PATH_NAME        "/csa/imei/serialno.dat"
32
33 #define METHOD_GET_REVISION     "GetHWRev"
34 #define PATH_NAME               "/proc/cpuinfo"
35 #define REVISION_NAME   "Revision"
36 #define TOK_DELIMITER   ":"
37 #define END_DELIMITER   " \n"
38
39 #define CONVERT_TYPE    10
40 #define REVISION_SIZE   4
41
42 static int read_from_file(const char *path, char *buf, size_t size)
43 {
44         int fd;
45         size_t count;
46
47         if (!path)
48                 return -1;
49
50         fd = open(path, O_RDONLY, 0);
51         if (fd == -1) {
52                 _E("Could not open '%s'", path);
53                 return -1;
54         }
55
56         count = read(fd, buf, size);
57
58         if (count > 0) {
59                 count = (count < size) ? count : size - 1;
60                 while (count > 0 && buf[count - 1] == '\n')
61                         count--;
62                 buf[count] = '\0';
63         } else {
64                 buf[0] = '\0';
65         }
66
67         close(fd);
68
69         return 0;
70 }
71
72 static int get_revision(char *rev)
73 {
74         char buf[FILE_BUFF_MAX];
75         char *tag;
76         char *start, *ptr;
77         long rev_num;
78         const int radix = 16;
79
80         if (rev == NULL) {
81                 _E("Invalid argument !\n");
82                 return -1;
83         }
84
85         if (read_from_file(PATH_NAME, buf, FILE_BUFF_MAX) < 0) {
86                 _E("fail to read %s\n", PATH_NAME);
87                 return -1;
88         }
89
90         tag = strstr(buf, REVISION_NAME);
91         if (tag == NULL) {
92                 _E("cannot find Hardware in %s\n", PATH_NAME);
93                 return -1;
94         }
95
96         start = strstr(tag, TOK_DELIMITER);
97         if (start == NULL) {
98                 _E("cannot find Hardware in %s\n", PATH_NAME);
99                 return -1;
100         }
101
102         start ++;
103         ptr = strtok(start, END_DELIMITER);
104         ptr += strlen(ptr);
105         ptr -=2;
106
107         memset(rev, 0x00, REVISION_SIZE);
108         rev_num = strtol(ptr, NULL, radix);
109         sprintf(rev, "%d", rev_num);
110
111         return 0;
112 }
113
114 static DBusMessage *dbus_revision_handler(E_DBus_Object *obj, DBusMessage *msg)
115 {
116         DBusMessageIter iter;
117         DBusMessage *reply;
118         char rev[FILE_BUFF_MAX];
119         char *ptr;
120         static int ret = NOT_INITIALIZED;
121
122         if (ret != NOT_INITIALIZED)
123                 goto out;
124         ret = get_revision(rev);
125         if (ret == 0)
126                 ret = strtol(rev, &ptr, CONVERT_TYPE);
127 out:
128         _D("rev : %d", ret);
129
130         reply = dbus_message_new_method_return(msg);
131         dbus_message_iter_init_append(reply, &iter);
132         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
133         return reply;
134 }
135
136 static int get_serial(unsigned char *buf)
137 {
138         int fd;
139         int r;
140         int ret = 0;
141
142         fd = open(SERIAL_PATH_NAME, O_RDONLY, 0);
143         if (fd < 0)
144                 return -ENOENT;
145
146         r = read(fd, buf, FILE_BUFF_MAX);
147         if ((r >= 0) && (r < FILE_BUFF_MAX))
148                 buf[r] = '\0';
149         else
150                 ret = -EIO;
151
152         close(fd);
153         return ret;
154 }
155
156 static DBusMessage *dbus_serial_handler(E_DBus_Object *obj, DBusMessage *msg)
157 {
158         DBusMessageIter iter, arr;
159         DBusMessage *reply;
160         static int len = 0;
161         static char buf[FILE_BUFF_MAX];
162         static char *param = buf;
163         static int ret = NOT_INITIALIZED;
164
165         if (ret == 0)
166                 goto out;
167         ret = get_serial(buf);
168         if (ret == 0) {
169                 len = strlen(buf);
170                 goto out;
171         }
172         _E("fail to get serial");
173 out:
174         _D("serial : %s %d", buf, len);
175         reply = dbus_message_new_method_return(msg);
176         dbus_message_iter_init_append(reply, &iter);
177         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &param);
178         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
179         return reply;
180 }
181
182 static const struct edbus_method edbus_methods[] = {
183         { METHOD_GET_SERIAL,   NULL, "si", dbus_serial_handler },
184         { METHOD_GET_REVISION,   NULL, "i", dbus_revision_handler },
185 };
186
187 static void board_init(void *data)
188 {
189         int ret;
190
191         ret = register_edbus_method(DEVICED_PATH_BOARD, edbus_methods, ARRAY_SIZE(edbus_methods));
192         if (ret < 0)
193                 _E("fail to init edbus method(%d)", ret);
194 }
195
196 static const struct device_ops board_device_ops = {
197         .priority = DEVICE_PRIORITY_NORMAL,
198         .name     = "board",
199         .init     = board_init,
200 };
201
202 DEVICE_OPS_REGISTER(&board_device_ops)