4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "core/devices.h"
24 #include "core/edbus-handler.h"
25 #include "core/common.h"
27 #define FILE_BUFF_MAX 1024
28 #define NOT_INITIALIZED (-1)
30 #define METHOD_GET_SERIAL "GetSerial"
31 #define SERIAL_PATH_NAME "/csa/imei/serialno.dat"
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"
39 #define CONVERT_TYPE 10
40 #define REVISION_SIZE 4
42 static int read_from_file(const char *path, char *buf, size_t size)
50 fd = open(path, O_RDONLY, 0);
52 _E("Could not open '%s'", path);
56 count = read(fd, buf, size);
59 count = (count < size) ? count : size - 1;
60 while (count > 0 && buf[count - 1] == '\n')
72 static int get_revision(char *rev)
74 char buf[FILE_BUFF_MAX];
81 _E("Invalid argument !\n");
85 if (read_from_file(PATH_NAME, buf, FILE_BUFF_MAX) < 0) {
86 _E("fail to read %s\n", PATH_NAME);
90 tag = strstr(buf, REVISION_NAME);
92 _E("cannot find Hardware in %s\n", PATH_NAME);
96 start = strstr(tag, TOK_DELIMITER);
98 _E("cannot find Hardware in %s\n", PATH_NAME);
103 ptr = strtok(start, END_DELIMITER);
107 memset(rev, 0x00, REVISION_SIZE);
108 rev_num = strtol(ptr, NULL, radix);
109 sprintf(rev, "%d", rev_num);
114 static DBusMessage *dbus_revision_handler(E_DBus_Object *obj, DBusMessage *msg)
116 DBusMessageIter iter;
118 char rev[FILE_BUFF_MAX];
120 static int ret = NOT_INITIALIZED;
122 if (ret != NOT_INITIALIZED)
124 ret = get_revision(rev);
126 ret = strtol(rev, &ptr, CONVERT_TYPE);
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);
136 static int get_serial(unsigned char *buf)
142 fd = open(SERIAL_PATH_NAME, O_RDONLY, 0);
146 r = read(fd, buf, FILE_BUFF_MAX);
147 if ((r >= 0) && (r < FILE_BUFF_MAX))
156 static DBusMessage *dbus_serial_handler(E_DBus_Object *obj, DBusMessage *msg)
158 DBusMessageIter iter, arr;
161 static char buf[FILE_BUFF_MAX];
162 static char *param = buf;
163 static int ret = NOT_INITIALIZED;
167 ret = get_serial(buf);
172 _E("fail to get serial");
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, ¶m);
178 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
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 },
187 static void board_init(void *data)
191 ret = register_edbus_method(DEVICED_PATH_BOARD, edbus_methods, ARRAY_SIZE(edbus_methods));
193 _E("fail to init edbus method(%d)", ret);
196 static const struct device_ops board_device_ops = {
197 .priority = DEVICE_PRIORITY_NORMAL,
202 DEVICE_OPS_REGISTER(&board_device_ops)