Fix build error and do Boiler Plate.
[profile/ivi/tel-plugin-imc.git] / src / s_common.c
1 /*\r
2  * tel-plugin-imc\r
3  *\r
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.\r
5  *\r
6  * Contact: Ja-young Gu <jygu@samsung.com>\r
7  *\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  * http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  */\r
20 \r
21 #include <stdio.h>\r
22 #include <string.h>\r
23 #include <stdlib.h>\r
24 \r
25 #include <glib.h>\r
26 \r
27 \r
28 #include "s_common.h"\r
29 \r
30 #include <plugin.h>\r
31 \r
32 #undef  MAX\r
33 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))\r
34 \r
35 #undef  MIN\r
36 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))\r
37 \r
38 #define bitsize(type) (sizeof(type) * 8)\r
39 \r
40 #define copymask(type) ((0xffffffff) >> (32 - bitsize(type)))\r
41 \r
42 #define MASK(width, offset, data) \\r
43    (((width) == bitsize(data)) ? (data) :   \\r
44    ((((copymask(data) << (bitsize(data) - ((width) % bitsize(data)))) & copymask(data)) >>  (offset)) & (data))) \\r
45 \r
46 \r
47 #define MASK_AND_SHIFT(width, offset, shift, data)  \\r
48                   ((((signed) (shift)) < 0) ?       \\r
49                     MASK((width), (offset), (data)) << -(shift) :  \\r
50                     MASK((width), (offset), (data)) >>  (((unsigned) (shift)))) \\r
51 \r
52 char _util_unpackb(const char *src, int pos, int len);\r
53 char _util_convert_byte_hexChar (char val);\r
54 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes);\r
55 \r
56 void util_hex_dump(char *pad, int size, const void *data)\r
57 {\r
58         char buf[255] = {0, };\r
59         char hex[4] = {0, };\r
60         int i;\r
61         unsigned char *p;\r
62 \r
63         if (size <= 0) {\r
64                 msg("%sno data", pad);\r
65                 return;\r
66         }\r
67 \r
68         p = (unsigned char *)data;\r
69 \r
70         snprintf(buf, 255, "%s%04X: ", pad, 0);\r
71         for (i = 0; i<size; i++) {\r
72                 snprintf(hex, 4, "%02X ", p[i]);\r
73                 strcat(buf, hex);\r
74 \r
75                 if ((i + 1) % 8 == 0) {\r
76                         if ((i + 1) % 16 == 0) {\r
77                                 msg("%s", buf);\r
78                                 memset(buf, 0, 255);\r
79                                 snprintf(buf, 255, "%s%04X: ", pad, i + 1);\r
80                         }\r
81                         else {\r
82                                 strcat(buf, "  ");\r
83                         }\r
84                 }\r
85         }\r
86 \r
87         msg("%s", buf);\r
88 }\r
89 \r
90 void hook_hex_dump(enum direction_e d, int size, const void *data)\r
91 {\r
92         msg("=== TX data DUMP =====");\r
93         util_hex_dump("          ", size, data);\r
94         msg("=== TX data DUMP =====");\r
95 \r
96 }\r
97 \r
98 unsigned int util_assign_message_sequence_id(TcorePlugin *p)\r
99 {\r
100         struct global_data *gd;\r
101 \r
102         if (!p) {\r
103                 dbg("plugin is NULL");\r
104                 return -1;\r
105         }\r
106 \r
107         gd = tcore_plugin_ref_user_data(p);\r
108         if (!gd) {\r
109                 dbg("global data is NULL");\r
110                 return -1;\r
111         }\r
112 \r
113         if (gd->msg_auto_id_current == 0) {\r
114                 gd->msg_auto_id_current = gd->msg_auto_id_start;\r
115                 dbg("pending_auto_id_current is 0, reset to start");\r
116         }\r
117         else if (gd->msg_auto_id_current >= gd->msg_auto_id_end) {\r
118                 gd->msg_auto_id_current = gd->msg_auto_id_start;\r
119                 dbg("pending_auto_id_current is over, reset to start");\r
120         }\r
121         else {\r
122                 gd->msg_auto_id_current++;\r
123         }\r
124 \r
125         dbg("message_sequence_id = %d", gd->msg_auto_id_current);\r
126 \r
127         return gd->msg_auto_id_current;\r
128 }\r
129 \r
130 gboolean util_add_waiting_job(GQueue *queue, unsigned int id, UserRequest *ur)\r
131 {\r
132         struct work_queue_data *wqd;\r
133 \r
134         if (!queue)\r
135                 return FALSE;\r
136 \r
137         wqd = calloc(sizeof(struct work_queue_data), 1);\r
138         if (!wqd)\r
139                 return FALSE;\r
140 \r
141         wqd->id = id;\r
142         wqd->ur = tcore_user_request_ref(ur);\r
143         g_queue_push_tail(queue, wqd);\r
144 \r
145         dbg("id = %d, ur = 0x%x", wqd->id, wqd->ur);\r
146         return TRUE;\r
147 }\r
148 \r
149 UserRequest *util_pop_waiting_job(GQueue *queue, unsigned int id)\r
150 {\r
151         int i = 0;\r
152         UserRequest *ur;\r
153         struct work_queue_data *wqd;\r
154 \r
155         if (!queue)\r
156                 return NULL;\r
157 \r
158 \r
159         dbg("before waiting job count: %d", g_queue_get_length(queue));\r
160 \r
161         do {\r
162                 wqd = g_queue_peek_nth(queue, i);\r
163                 if (!wqd)\r
164                         return NULL;\r
165 \r
166                 if (wqd->id == id) {\r
167                         wqd = g_queue_pop_nth(queue, i);\r
168                         break;\r
169                 }\r
170 \r
171                 i++;\r
172         } while (wqd != NULL);\r
173 \r
174         dbg("after  waiting job count: %d", g_queue_get_length(queue));\r
175 \r
176         if (!wqd)\r
177                 return NULL;\r
178 \r
179         ur = wqd->ur;\r
180         free(wqd);\r
181 \r
182         return ur;\r
183 }\r
184 \r
185 unsigned char util_hexCharToInt(char c)\r
186 {\r
187     if (c >= '0' && c <= '9')\r
188         return (c - '0');\r
189     else if (c >= 'A' && c <= 'F')\r
190         return (c - 'A' + 10);\r
191     else if (c >= 'a' && c <= 'f')\r
192         return (c - 'a' + 10);\r
193     else\r
194     {\r
195         dbg("invalid charater!!");\r
196         return -1;\r
197     }\r
198 }\r
199 \r
200 char * util_hexStringToBytes(char * s)\r
201 {\r
202     char * ret;\r
203         int i;\r
204         int sz;\r
205 \r
206         if (s == NULL)\r
207                 return NULL;\r
208 \r
209         sz = strlen(s);\r
210 \r
211         ret = calloc((sz/2)+1, 1);\r
212 \r
213         dbg("Convert String to Binary!!");\r
214 \r
215         for (i = 0; i < sz; i += 2) {\r
216                 ret[i / 2] = (char) ((util_hexCharToInt(s[i]) << 4) | util_hexCharToInt(s[i + 1]));\r
217                 dbg("[%02x]", ret[i/2]);\r
218     }\r
219 \r
220     return ret;\r
221 }\r
222 \r
223 char _util_unpackb(const char *src, int pos, int len)\r
224 {\r
225         char result = 0;\r
226         int rshift = 0;\r
227 \r
228         src += pos/8;\r
229         pos %= 8;\r
230 \r
231         rshift = MAX( 8 - (pos + len), 0);\r
232 \r
233         if ( rshift > 0 ) {\r
234 \r
235          result = MASK_AND_SHIFT(len, pos, rshift, *src);\r
236 \r
237         } else {\r
238 \r
239          result = MASK(8-pos, pos, *src);\r
240          src++;\r
241          len -= 8 - pos;\r
242 \r
243           if ( len > 0 ) result = ( result<<len ) | (*src >> (8-len));  // if any bits left\r
244         }\r
245 \r
246         return result;\r
247 }\r
248 \r
249 char _util_convert_byte_hexChar (char val)\r
250 {\r
251         char hex_char;\r
252 \r
253         if (val <= 9)\r
254         {\r
255                 hex_char = (char)(val+'0');\r
256         }\r
257         else if (val >= 10 && val <= 15)\r
258         {\r
259                 hex_char = (char)(val-10+'A');\r
260         }\r
261         else\r
262         {\r
263                 hex_char = '0';\r
264         }\r
265 \r
266         return (hex_char);\r
267 }\r
268 \r
269 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes)\r
270 {\r
271         int i;\r
272         char nibble;\r
273         int buf_pos = 0;\r
274 \r
275         for (i=0; i<num_bytes*2; i++)\r
276         {\r
277                 nibble = _util_unpackb(byte_pdu,buf_pos,4);\r
278                 buf_pos += 4;\r
279                 hex_pdu[i] = _util_convert_byte_hexChar(nibble);\r
280         }\r
281 \r
282         return TRUE;\r
283 }\r