Initialize Tizen 2.3
[framework/system/deviced.git] / src / led / xml.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 <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <assert.h>
24 #include <libxml/parser.h>
25
26 #include "deviced/dd-led.h"
27 #include "xml.h"
28 #include "core/log.h"
29 #include "core/list.h"
30
31 #ifndef DATADIR
32 #define DATADIR "/usr/share/deviced"
33 #endif
34
35 #define LED_XML         DATADIR"/led.xml"
36 #define LED_STR         "led"
37
38 static const char* led_str[] = {
39         [LED_OFF] = "off",
40         [LED_LOW_BATTERY] = "low battery",
41         [LED_CHARGING] = "charging",
42         [LED_FULLY_CHARGED] = "fully charged",
43         [LED_CHARGING_ERROR] = "charging error",
44         [LED_MISSED_NOTI] = "missed noti",
45         [LED_VOICE_RECORDING] = "voice recording",
46         [LED_POWER_OFF] = "power off",
47         [LED_CUSTOM] = "custom",
48 };
49
50 static dd_list *led_head;
51
52 static xmlDocPtr xml_open(const char *xml)
53 {
54         xmlDocPtr doc;
55
56         doc = xmlReadFile(xml, NULL, 0);
57         if (doc == NULL) {
58                 _E("xmlReadFile fail");
59                 return NULL;
60         }
61
62         return doc;
63 }
64
65 static void xml_close(xmlDocPtr doc)
66 {
67         xmlFreeDoc(doc);
68 }
69
70 static xmlNodePtr xml_find(xmlDocPtr doc, const xmlChar* expr)
71 {
72         xmlNodePtr root;
73         xmlNodePtr cur;
74         xmlChar *key;
75
76         assert(doc);
77         assert(expr);
78
79         root = xmlDocGetRootElement(doc);
80         if (root == NULL) {
81                 _E("xmlDocGetRootElement fail");
82                 return NULL;
83         }
84
85         for (cur = root->children; cur != NULL; cur = cur->next) {
86                 if (xmlStrcmp(cur->name, (const xmlChar*)LED_STR))
87                         continue;
88
89                 key = xmlGetProp(cur, (const xmlChar*)"label");
90                 if (key && !xmlStrcmp(key, expr)) {
91                         _D("%s", key);
92                         xmlFree(key);
93                         return cur;
94                 }
95         }
96
97         return NULL;
98 }
99
100 static struct led_mode *xml_parse(xmlDocPtr doc, xmlNodePtr cur)
101 {
102         xmlNodePtr node;
103         xmlNodePtr attr;
104         struct led_mode *led;
105         xmlChar *key;
106         int i = 0;
107
108         assert(doc);
109         assert(cur);
110
111         led = (struct led_mode*)malloc(sizeof(struct led_mode));
112         if (led == NULL) {
113                 _E("out of memory");
114                 return NULL;
115         }
116
117         for (node = cur->children; node != NULL; node = node->next) {
118                 if (node->type != XML_ELEMENT_NODE)
119                         continue;
120
121                 if (!xmlStrcmp(node->name, "priority")) {
122                         key = xmlNodeListGetString(doc, node->children, 1);
123                         if (!key)
124                                 continue;
125                         led->data.priority = atoi(key);
126                         xmlFree(key);
127                 } else if (!xmlStrcmp(node->name, "data")) {
128                         key = xmlGetProp(node, (const xmlChar*)"on");
129                         if (!key)
130                                 continue;
131                         led->data.on = atoi(key);
132                         xmlFree(key);
133
134                         key = xmlGetProp(node, (const xmlChar*)"off");
135                         if (!key)
136                                 continue;
137                         led->data.off = atoi(key);
138                         xmlFree(key);
139
140                         key = xmlGetProp(node, (const xmlChar*)"color");
141                         if (!key)
142                                 continue;
143                         led->data.color = (unsigned int)strtoul(key, NULL, 16);
144                         xmlFree(key);
145
146                         key = xmlGetProp(node, (const xmlChar*)"wave");
147                         if (!key || xmlStrcmp(key, "on"))
148                                 led->data.wave = false;
149                         else
150                                 led->data.wave = true;
151                         xmlFree(key);
152                 }
153         }
154
155         _D("priority : %d, on : %d, off : %d, color : %x, wave : %d",
156                         led->data.priority, led->data.on, led->data.off,
157                         led->data.color, led->data.wave);
158         return led;
159 }
160
161 int get_led_data(void)
162 {
163         xmlDocPtr doc;
164         xmlNodePtr cur;
165         struct led_mode *led;
166         int i;
167         int r;
168
169         doc = xml_open(LED_XML);
170         if (doc == NULL) {
171                 _E("xml_open(%s) fail", LED_XML);
172                 errno = EPERM;
173                 return -1;
174         }
175
176         for (i = 0; i < LED_MODE_MAX; ++i) {
177                 cur = xml_find(doc, (const xmlChar*)led_str[i]);
178                 if (cur == NULL) {
179                         _E("xml_find(%s) fail", led_str[i]);
180                         break;
181                 }
182
183                 led = xml_parse(doc, cur);
184                 if (led == NULL) {
185                         _E("xml_parse fail");
186                         break;
187                 }
188
189                 led->mode = i;
190                 led->state = 0;
191                 DD_LIST_APPEND(led_head, led);
192         }
193
194         xml_close(doc);
195         return 0;
196 }
197
198 void release_led_data(void)
199 {
200         dd_list *l;
201         struct led_mode *node;
202
203         DD_LIST_FOREACH(led_head, l, node) {
204                 _D("node name : %d", node->mode);
205                 DD_LIST_REMOVE(led_head, node);         
206                 free(node);
207         }
208 }
209
210 struct led_mode *find_led_data(int mode)
211 {
212         dd_list *l;
213         struct led_mode *node;
214
215         DD_LIST_FOREACH(led_head, l, node) {
216                 if (node->mode == mode)
217                         return node;
218         }
219
220         return NULL;
221 }
222
223 struct led_mode *get_valid_led_data(void)
224 {
225         dd_list *l;
226         struct led_mode *node;
227         struct led_mode *cur = NULL;
228         int pr = 2;
229
230         DD_LIST_FOREACH(led_head, l, node) {
231                 if (!node->state)
232                         continue;
233                 if (node->data.priority <= pr) {
234                         pr = node->data.priority;
235                         cur = node;
236                 }
237         }
238
239         return cur;
240 }
241
242 void print_all_data(void)
243 {
244         dd_list *l;
245         struct led_mode *node;
246
247         _D("Mode State priority   on  off    color wave");
248         DD_LIST_FOREACH(led_head, l, node) {
249                 _D("%4d %5d %8d %4d %4d %x %4d",
250                                 node->mode, node->state, node->data.priority,
251                                 node->data.on, node->data.off, node->data.color, node->data.wave);
252         }
253 }