Tizen 2.0 Release
[framework/system/sync-agent.git] / src / fw-plugins / common-public / slp-device / src / plugin_interface_devinfo.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <glib-object.h>
22 #include <vconf.h>
23 #include <vconf-keys.h>
24
25 #include "slp_device_info.h"
26
27 #include "utility/sync_util.h"
28 #include "plugin/device_info_interface.h"
29
30 #ifndef EXPORT_API
31 #define EXPORT_API __attribute__ ((visibility("default")))
32 #endif
33
34 #ifndef SYNC_AGENT_LOG
35 #undef LOG_TAG
36 #define LOG_TAG "PLUGIN_DEVINFO_SLP"
37 #endif
38
39 #define IMEI_LEN                                        5
40 #define FW_VERSION_LEN          20
41 #define BUFF_SIZE                                       1000
42
43 /* todo : temporary definition */
44 #define HW_VERSION                              "1.0"
45
46 typedef struct {
47         char *info_name;
48         char *vconf_key;
49 } dici_name_key_map_s;
50
51 dici_name_key_map_s dev_key_map[] = {
52         {"SwV", VCONFKEY_TELEPHONY_SWVERSION},
53         {"HwV", VCONFKEY_TELEPHONY_HWVERSION},
54         /*{"Lang", "db/menu_widget/language"}, */
55         {"Lang", VCONFKEY_LANGSET},
56         {NULL, NULL},
57 };
58
59 static GList *dev_info;
60 static void _free_device_info(sync_agent_plugin_device_info_s * devinfo);
61
62 /*static int find_key(const char *info_name, char *key);*/
63
64 EXPORT_API int sync_agent_plugin_load_devinfo()
65 {
66         _EXTERN_FUNC_ENTER;
67
68         g_type_init();
69
70         int res = 1;
71
72         dev_info = NULL;
73         int idx = 0;
74
75         sync_agent_plugin_device_info_s *dev_id = NULL;
76         sync_agent_plugin_device_info_s *man = NULL;
77         sync_agent_plugin_device_info_s *mod = NULL;
78         sync_agent_plugin_device_info_s *fw = NULL;
79         sync_agent_plugin_device_info_s *oem = NULL;
80         sync_agent_plugin_device_info_s *dev_type = NULL;
81         sync_agent_plugin_device_info_s *modem_version = NULL;
82         sync_agent_plugin_device_info_s *pda_version = NULL;
83         sync_agent_plugin_device_info_s *csc_version = NULL;
84
85         dev_id = (sync_agent_plugin_device_info_s *) malloc(sizeof(sync_agent_plugin_device_info_s));
86         if (dev_id == NULL) {
87                 _DEBUG_ERROR("malloc() dev_id FAIL !!!");
88                 goto error_part;
89         }
90         memset(dev_id, 0x00, sizeof(sync_agent_plugin_device_info_s));
91         man = (sync_agent_plugin_device_info_s *) malloc(sizeof(sync_agent_plugin_device_info_s));
92         if (man == NULL) {
93                 _DEBUG_ERROR("malloc() man FAIL !!!");
94                 goto error_part;
95         }
96         memset(man, 0x00, sizeof(sync_agent_plugin_device_info_s));
97         mod = (sync_agent_plugin_device_info_s *) malloc(sizeof(sync_agent_plugin_device_info_s));
98         if (mod == NULL) {
99                 _DEBUG_ERROR("malloc() mod FAIL !!!");
100                 goto error_part;
101         }
102         memset(mod, 0x00, sizeof(sync_agent_plugin_device_info_s));
103         fw = (sync_agent_plugin_device_info_s *) malloc(sizeof(sync_agent_plugin_device_info_s));
104         if (fw == NULL) {
105                 _DEBUG_ERROR("malloc() fw FAIL !!!");
106                 goto error_part;
107         }
108         memset(fw, 0x00, sizeof(sync_agent_plugin_device_info_s));
109         oem = (sync_agent_plugin_device_info_s *) malloc(sizeof(sync_agent_plugin_device_info_s));
110         if (oem == NULL) {
111                 _DEBUG_ERROR("malloc() oem FAIL !!!");
112                 goto error_part;
113         }
114         memset(oem, 0x00, sizeof(sync_agent_plugin_device_info_s));
115         dev_type = (sync_agent_plugin_device_info_s *) malloc(sizeof(sync_agent_plugin_device_info_s));
116         if (dev_type == NULL) {
117                 _DEBUG_ERROR("malloc() dev_type FAIL !!!");
118                 goto error_part;
119         }
120         memset(dev_type, 0x00, sizeof(sync_agent_plugin_device_info_s));
121         modem_version = (sync_agent_plugin_device_info_s *) malloc(sizeof(sync_agent_plugin_device_info_s));
122         if (modem_version == NULL) {
123                 _DEBUG_ERROR("malloc() modem_version FAIL !!!");
124                 goto error_part;
125         }
126         memset(modem_version, 0x00, sizeof(sync_agent_plugin_device_info_s));
127         pda_version = (sync_agent_plugin_device_info_s *) malloc(sizeof(sync_agent_plugin_device_info_s));
128         if (pda_version == NULL) {
129                 _DEBUG_ERROR("malloc() pda_version FAIL !!!");
130                 goto error_part;
131         }
132         memset(pda_version, 0x00, sizeof(sync_agent_plugin_device_info_s));
133         csc_version = (sync_agent_plugin_device_info_s *) malloc(sizeof(sync_agent_plugin_device_info_s));
134         if (csc_version == NULL) {
135                 _DEBUG_ERROR("malloc() csc_version FAIL !!!");
136                 goto error_part;
137         }
138         memset(csc_version, 0x00, sizeof(sync_agent_plugin_device_info_s));
139
140         /* get SwV, HwV, Lang */
141         sync_agent_plugin_device_info_s *info = NULL;
142         while (dev_key_map[idx].info_name != NULL) {
143
144                 info = (sync_agent_plugin_device_info_s *) malloc(sizeof(sync_agent_plugin_device_info_s));
145                 if (info == NULL) {
146                         _DEBUG_ERROR("malloc() info FAIL !!!");
147                         goto error_part;
148                 }
149                 memset(info, 0x00, sizeof(sync_agent_plugin_device_info_s));
150
151                 info->info_name = strdup(dev_key_map[idx].info_name);
152                 info->value = strdup(vconf_get_str(dev_key_map[idx].vconf_key));
153                 _DEBUG_INFO("[devInfo_slp_plugIn] info_name : %s\n", info->info_name);
154                 _DEBUG_INFO("[devInfo_slp_plugIn] value : %s\n", info->value);
155
156                 if (!strcmp(info->info_name, "HwV")) {
157                         if ((info->value == NULL) || (!strcmp(info->value, "")) || (!strcmp(info->value, "REV__"))) {
158                                 free(info->value);
159                                 info->value = strdup(HW_VERSION);
160                         }
161                 }
162
163                 _DEBUG_INFO("[devInfo_slp_plugIn] value : %s\n", info->value);
164                 dev_info = g_list_append(dev_info, info);
165                 ++idx;
166         }
167
168         /* get DevID */
169         dev_id->info_name = strdup("DevID");
170         dev_id->value = slp_device_info_get_device_id();
171         dev_info = g_list_append(dev_info, dev_id);
172         _DEBUG_INFO("mdi ============= \n");
173
174         /* get Manufacture */
175         man->info_name = strdup("Man");
176         man->value = slp_device_info_get_manufacturer_info();
177         dev_info = g_list_append(dev_info, man);
178
179         /* get Model info */
180         mod->info_name = strdup("Mod");
181         mod->value = slp_device_info_get_model_info();
182         dev_info = g_list_append(dev_info, mod);
183
184         /* get FwV */
185         fw->info_name = strdup("FwV");
186         fw->value = slp_device_info_get_firmware_version();
187         dev_info = g_list_append(dev_info, fw);
188
189         /* get OEM */
190         oem->info_name = strdup("OEM");
191         oem->value = slp_device_info_get_oem_info();
192         dev_info = g_list_append(dev_info, oem);
193
194         /* get DevTyp */
195         dev_type->info_name = strdup("DevTyp");
196         dev_type->value = slp_device_info_get_device_type();
197         dev_info = g_list_append(dev_info, dev_type);
198
199         /* get Modem version */
200         modem_version->info_name = strdup("ModemV");
201         modem_version->value = slp_device_info_get_modem_version();
202         dev_info = g_list_append(dev_info, modem_version);
203
204         /* get PDA version */
205         pda_version->info_name = strdup("PDAV");
206         pda_version->value = slp_device_info_get_pda_version();
207         dev_info = g_list_append(dev_info, pda_version);
208
209         /* get CSC version */
210         csc_version->info_name = strdup("CSCV");
211         csc_version->value = slp_device_info_get_csc_version();
212         dev_info = g_list_append(dev_info, csc_version);
213
214         /* for test */
215         int cnt = 0;
216         GList *iter = NULL;
217         for (iter = dev_info; iter != NULL; iter = g_list_next(iter)) {
218                 _DEBUG_INFO("[devInfo_slp_plugIn] cnt : %d,  info_name : %s, value : %s\n", cnt, ((sync_agent_plugin_device_info_s *) (iter->data))->info_name, ((sync_agent_plugin_device_info_s *) (iter->data))->value);
219                 cnt++;
220         }
221
222         _EXTERN_FUNC_EXIT;
223
224         return res;
225
226  error_part:
227
228         _free_device_info(dev_id);
229         _free_device_info(man);
230         _free_device_info(mod);
231         _free_device_info(fw);
232         _free_device_info(oem);
233         _free_device_info(dev_type);
234         _free_device_info(modem_version);
235         _free_device_info(pda_version);
236         _free_device_info(csc_version);
237
238         res = sync_agent_plugin_clear_devinfo();
239
240         _DEBUG_ERROR("Returning with error.. means FAIL !!!");
241
242         _EXTERN_FUNC_EXIT;
243
244         return 0;
245 }
246
247 EXPORT_API int sync_agent_plugin_get_devinfo(char *info_name, char **value)
248 {
249         _EXTERN_FUNC_ENTER;
250
251         retvm_if(info_name == NULL, 0, "info_name is NULL. FAIL !!!");
252
253         int res = 1;
254         int is_exist = 0;
255         GList *iter = NULL;
256         sync_agent_plugin_device_info_s *iter_data;
257
258         for (iter = dev_info; iter != NULL; iter = g_list_next(iter)) {
259                 iter_data = NULL;
260                 iter_data = (sync_agent_plugin_device_info_s *) (iter->data);
261                 if (iter_data->info_name != NULL) {
262                         if (!strcmp(iter_data->info_name, info_name)) {
263                                 if (iter_data->value != NULL) {
264
265                                         *value = strdup(iter_data->value);
266
267                                         is_exist = 1;
268                                         break;
269                                 } else {
270                                         _DEBUG_INFO("[devInfo_slp_plugIn] %s  value is NULL !!\n", info_name);
271                                         res = 0;
272                                         return res;
273                                 }
274                         }
275                 }
276         }
277
278         if (strcmp("Battery", info_name) == 0) {
279                 *value = slp_device_info_get_battery_level();
280                 if (*value != NULL)
281                         is_exist = 1;
282         }
283
284         if (is_exist != 1) {
285                 _DEBUG_INFO("[devInfo_slp_plugIn] not exist  %s  information !!\n", info_name);
286                 res = -1;
287                 return res;
288         }
289
290         _EXTERN_FUNC_EXIT;
291
292         return res;
293 }
294
295 EXPORT_API int sync_agent_plugin_clear_devinfo()
296 {
297         _EXTERN_FUNC_ENTER;
298
299         int res = 1;
300
301         GList *iter = NULL;
302         sync_agent_plugin_device_info_s *iter_data;
303
304         if (dev_info != NULL) {
305                 for (iter = dev_info; iter != NULL;) {
306                         iter_data = NULL;
307                         iter_data = ((sync_agent_plugin_device_info_s *) (iter->data));
308                         iter = g_list_next(iter);
309                         dev_info = g_list_remove(dev_info, iter_data);
310
311                         _free_device_info((sync_agent_plugin_device_info_s *) (iter_data));
312                 }
313
314                 g_list_free(dev_info);
315         }
316
317         _EXTERN_FUNC_EXIT;
318
319         return res;
320 }
321
322 static void _free_device_info(sync_agent_plugin_device_info_s * devinfo)
323 {
324         if (devinfo != NULL) {
325                 if (devinfo->info_name != NULL) {
326                         free(devinfo->info_name);
327                         devinfo->info_name = NULL;
328                 }
329                 if (devinfo->value != NULL) {
330                         free(devinfo->value);
331                         devinfo->value = NULL;
332                 }
333
334                 free(devinfo);
335         }
336 }
337
338 /*static int find_key(const char *info_name, char *key)
339 {
340         int res = 1;
341         int idx = 0;
342
343         while (dev_key_map[idx].info_name != NULL) {
344                 if (!strcmp(dev_key_map[idx].info_name, info_name)) {
345                         if (dev_key_map[idx].vconf_key != NULL) {
346                                 key = dev_key_map[idx].vconf_key;
347                                 break;
348                         } else {
349                                 _DEBUG_INFO("[devInfo_slp_plugIn] vconf_key is NULL ( %s )\n", info_name);
350                                 res = 0;
351                                 return res;
352                         }
353                 }
354
355                 ++idx;
356         }
357
358         _DEBUG_INFO("[devInfo_slp_plugIn] vconf_key : %s\n", key);
359
360         return res;
361 }*/