Correct some typo and add missing feature define
[platform/core/telephony/tel-plugin-packetservice.git] / src / ps_util.c
1 /*
2  * tel-plugin-packetservice
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongHoo Park <donghoo.park@samsung.com>
7  *          Arun Shukla <arun.shukla@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <unistd.h>
24 #include <wait.h>
25
26 #include <libxml/xmlmemory.h>
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
29 #include <vconf.h>
30 #include "ps_common.h"
31 #include <co_context.h>
32
33 GSource *ps_util_gsource_dispatch(GMainContext *main_context,
34                                   gint priority, GSourceFunc cb, gpointer data)
35 {
36         GSource *request_source = NULL;
37
38         request_source = g_idle_source_new();
39         g_source_set_callback(request_source, cb, data, NULL);
40         g_source_set_priority(request_source, priority);
41         g_source_attach(request_source, main_context);
42
43         return request_source;
44 }
45
46 gboolean ps_util_thread_dispatch(GMainContext *main_context,
47                                  gint priority, GSourceFunc cb, gpointer data)
48 {
49
50         GSource *request_source;
51
52         if (main_context == NULL || cb == NULL) {
53                 err("Failed to dispatch");
54                 return FALSE;
55         }
56
57         /*
58          * Dispatch to source
59          */
60         request_source = ps_util_gsource_dispatch(main_context, priority, cb, data);
61         g_source_unref(request_source);
62
63         return TRUE;
64 }
65
66 int ps_util_system_command(char *command)
67 {
68         int pid = 0,
69         status = 0;
70         const char *environ[] = { NULL };
71
72         if (command == NULL)
73                 return -1;
74
75         dbg("%s", command);
76
77         pid = fork();
78         if (pid == -1)
79                 return -1;
80
81         if (pid == 0) {
82                 char *argv[4];
83
84                 argv[0] = "sh";
85                 argv[1] = "-c";
86                 argv[2] = (char *)command;
87                 argv[3] = 0;
88
89                 execve("/bin/sh", argv, (char **)environ);
90                 err("execve() failed");
91         }
92
93         do {
94                 if (waitpid(pid, &status, 0) == -1) {
95                         if (errno != EINTR)
96                                 return -1;
97                 } else {
98                         if (WIFEXITED(status))
99                                 return WEXITSTATUS(status);
100                         else if (WIFSIGNALED(status))
101                                 return WTERMSIG(status);
102                         else if (WIFSTOPPED(status))
103                                 return WSTOPSIG(status);
104                 }
105         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
106
107         return 0;
108 }
109
110 void ps_util_load_xml_file(const char *docname,
111                            const char *groupname, void **i_doc, void **i_root_node)
112 {
113         xmlDocPtr *doc = (xmlDocPtr *)i_doc;
114         xmlNodePtr *root_node = (xmlNodePtr *)i_root_node;
115
116         dbg("docname: [%s] groupname: [%s]", docname, groupname);
117
118         *doc = xmlParseFile(docname);
119         if (*doc) {
120                 *root_node = xmlDocGetRootElement(*doc);
121                 if (*root_node) {
122                         dbg("*root_node->name: [%s]", (*root_node)->name);
123                         if (0 == xmlStrcmp((*root_node)->name,
124                                            (const unsigned char *)groupname)) {
125                                 dbg("root_node is found !!!");
126                                 return;
127                         } else {
128                                 err("Cannot find root node.");
129                                 *root_node = NULL;
130                         }
131                 }
132
133                 /* Free doc */
134                 xmlFreeDoc(*doc);
135                 *doc = NULL;
136         } else {
137                 err("Failed to parse doc: [%s]", docname);
138         }
139 }
140
141 void ps_util_unload_xml_file(void **i_doc, void **i_root_node)
142 {
143         xmlDocPtr *doc = (xmlDocPtr *)i_doc;
144         xmlNodePtr *root_node = (xmlNodePtr *)i_root_node;
145
146         dbg("unloading XML");
147         if (doc && *doc) {
148                 /* Free doc */
149                 xmlFreeDoc(*doc);
150                 *doc = NULL;
151
152                 if (root_node)
153                         *root_node = NULL;
154         }
155 }
156
157 gboolean ps_util_check_permanent_reject_cause(int cause, gboolean roaming)
158 {
159         /* No permanent reject cause in platform side */
160         return TRUE;
161 }
162
163 UserRequest *ps_util_peek_waiting_job(GQueue *queue, unsigned int id)
164 {
165         int iter = 0;
166         struct work_queue_data *wqd = NULL;
167
168         g_return_val_if_fail((NULL != queue), NULL);
169
170         do {
171                 wqd = g_queue_peek_nth(queue, iter);
172                 if (NULL == wqd)
173                         break;
174                 if (wqd->id == id)
175                         break;
176                 iter++;
177         } while (TRUE);
178
179         if (NULL == wqd) {
180                 warn("Work queue data is 0");
181                 return NULL;
182         }
183
184         return wqd->ur;
185 }
186
187 gboolean ps_util_add_waiting_job(GQueue *queue, unsigned int id, UserRequest *ur)
188 {
189         struct work_queue_data *wqd;
190
191         if (!queue)
192                 return FALSE;
193
194         wqd = calloc(sizeof(struct work_queue_data), 1);
195         if (!wqd)
196                 return FALSE;
197
198         wqd->id = id;
199         wqd->ur = ur;
200         g_queue_push_tail(queue, wqd);
201
202         dbg("id = %d, ur = 0x%x", wqd->id, wqd->ur);
203         return TRUE;
204 }
205
206 guint ps_util_get_waiting_job_count(GQueue *queue, unsigned int id)
207 {
208         guint i = 0;
209         guint count = 0;
210         struct work_queue_data *wqd = NULL;
211
212         if (!queue)
213                 return count;
214
215         dbg("job count: %d", g_queue_get_length(queue));
216
217         do {
218                 wqd = g_queue_peek_nth(queue, i);
219                 if (!wqd)
220                         break;
221
222                 if (wqd->id == id)
223                         count++;
224
225                 i++;
226         } while (wqd != NULL);
227
228         dbg("count: %d, id = %d", count, id);
229
230         return count;
231 }
232
233 UserRequest *ps_util_pop_waiting_job(GQueue *queue, unsigned int id)
234 {
235         int i = 0;
236         UserRequest *ur;
237         struct work_queue_data *wqd;
238
239         if (!queue)
240                 return NULL;
241
242         dbg("before waiting job count: %d", g_queue_get_length(queue));
243
244         do {
245                 wqd = g_queue_peek_nth(queue, i);
246                 if (!wqd)
247                         return NULL;
248
249                 if (wqd->id == id) {
250                         wqd = g_queue_pop_nth(queue, i);
251                         break;
252                 }
253
254                 i++;
255         } while (wqd != NULL);
256
257         dbg("after  waiting job count: %d", g_queue_get_length(queue));
258
259         if (!wqd)
260                 return NULL;
261
262         ur = wqd->ur;
263         free(wqd);
264
265         return ur;
266 }
267