fix get sim language issue
[platform/core/telephony/tel-plugin-imc.git] / src / imc_common.c
1 /*
2  * tel-plugin-imc
3  *
4  * Copyright (c) 2013 Samsung Electronics Co. Ltd. All rights reserved.
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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <glib.h>
24
25 #include <log.h>
26 #include <tcore.h>
27
28 #include "imc_common.h"
29
30 void on_send_imc_request(TcorePending *p,
31         TelReturn send_status, void *user_data)
32 {
33         dbg("Send - [%s]",
34                 (send_status == TEL_RETURN_SUCCESS ? "OK" : "NOK"));
35 }
36
37 ImcRespCbData *imc_create_resp_cb_data(TcoreObjectResponseCallback cb,
38         void *cb_data, void *data, guint data_len)
39 {
40         ImcRespCbData *resp_cb_data;
41
42         resp_cb_data = tcore_malloc0(sizeof(ImcRespCbData) + data_len);
43         resp_cb_data->cb = cb;
44         resp_cb_data->cb_data = cb_data;
45         if ((data != NULL) && (data_len > 0))
46                 memcpy(resp_cb_data->data, data, data_len);
47
48         return resp_cb_data;
49 }
50
51 void imc_destroy_resp_cb_data(ImcRespCbData *resp_cb_data)
52 {
53         if (resp_cb_data)
54                 tcore_free(resp_cb_data);
55 }
56
57 #if 0
58 #undef  MAX
59 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
60
61 #undef  MIN
62 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
63
64 #define bitsize(type) (sizeof(type) * 8)
65
66 #define copymask(type) ((0xffffffff) >> (32 - bitsize(type)))
67
68 #define MASK(width, offset, data) \
69         (((width) == bitsize(data)) ? (data) :   \
70          ((((copymask(data) << (bitsize(data) - ((width) % bitsize(data)))) & copymask(data)) >> (offset)) & (data))) \
71
72
73 #define MASK_AND_SHIFT(width, offset, shift, data)      \
74         ((((signed) (shift)) < 0) ?               \
75          MASK((width), (offset), (data)) << -(shift) :  \
76          MASK((width), (offset), (data)) >> (((signed) (shift)))) \
77
78 char _util_unpackb(const char *src, int pos, int len);
79 char _util_convert_byte_hexChar(char val);
80 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes);
81
82 void util_hex_dump(char *pad, int size, const void *data)
83 {
84         char buf[255] = {0, };
85         char hex[4] = {0, };
86         int i;
87         unsigned char *p;
88
89         if (size <= 0) {
90                 msg("%sno data", pad);
91                 return;
92         }
93
94         p = (unsigned char *) data;
95
96         snprintf(buf, 255, "%s%04X: ", pad, 0);
97         for (i = 0; i < size; i++) {
98                 snprintf(hex, 4, "%02X ", p[i]);
99                 strcat(buf, hex);
100
101                 if ((i + 1) % 8 == 0) {
102                         if ((i + 1) % 16 == 0) {
103                                 msg("%s", buf);
104                                 memset(buf, 0, 255);
105                                 snprintf(buf, 255, "%s%04X: ", pad, i + 1);
106                         } else {
107                                 strcat(buf, "  ");
108                         }
109                 }
110         }
111
112         msg("%s", buf);
113 }
114
115 unsigned char util_hexCharToInt(char c)
116 {
117         if (c >= '0' && c <= '9')
118                 return (c - '0');
119         else if (c >= 'A' && c <= 'F')
120                 return (c - 'A' + 10);
121         else if (c >= 'a' && c <= 'f')
122                 return (c - 'a' + 10);
123         else {
124                 dbg("invalid charater!!");
125                 return -1;
126         }
127 }
128
129 char *util_hex_to_string(const char *src, unsigned int src_len)
130 {
131         char *dest;
132         int i;
133
134         if (src == NULL)
135                 return NULL;
136
137         dest = g_malloc0(src_len * 2 + 1);
138         for (i = 0; i < src_len; i++) {
139                 sprintf(dest + (i * 2), "%02x", (unsigned char)src[i]);
140         }
141
142         dest[src_len * 2] = '\0';
143
144         return dest;
145 }
146
147 char* util_hexStringToBytes(char *s)
148 {
149         char *ret;
150         int i;
151         int sz;
152
153         if (s == NULL)
154                 return NULL;
155
156         sz = strlen(s);
157
158         ret = g_try_malloc0((sz / 2) + 1);
159
160         dbg("Convert String to Binary!!");
161
162         for (i = 0; i < sz; i += 2) {
163                 ret[i / 2] = (char) ((util_hexCharToInt(s[i]) << 4) | util_hexCharToInt(s[i + 1]));
164                 msg("           [%02x]", ret[i / 2]);
165         }
166
167         return ret;
168 }
169
170 char _util_unpackb(const char *src, int pos, int len)
171 {
172         char result = 0;
173         int rshift = 0;
174
175         src += pos / 8;
176         pos %= 8;
177
178         rshift = MAX(8 - (pos + len), 0);
179
180         if (rshift > 0) {
181                 result = MASK_AND_SHIFT(len, pos, rshift, (unsigned char)*src);
182         } else {
183                 result = MASK(8 - pos, pos, (unsigned char)*src);
184                 src++;
185                 len -= 8 - pos;
186
187                 if (len > 0) result = (result << len) | (*src >> (8 - len));   // if any bits left
188         }
189
190         return result;
191 }
192
193 char _util_convert_byte_hexChar(char val)
194 {
195         char hex_char;
196
197         if (val <= 9) {
198                 hex_char = (char) (val + '0');
199         } else if (val >= 10 && val <= 15) {
200                 hex_char = (char) (val - 10 + 'A');
201         } else {
202                 hex_char = '0';
203         }
204
205         return (hex_char);
206 }
207
208 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes)
209 {
210         int i;
211         char nibble;
212         int buf_pos = 0;
213
214         for (i = 0; i < num_bytes * 2; i++) {
215                 nibble = _util_unpackb(byte_pdu, buf_pos, 4);
216                 buf_pos += 4;
217                 hex_pdu[i] = _util_convert_byte_hexChar(nibble);
218         }
219
220         return TRUE;
221 }
222
223 char* util_removeQuotes(void *data)
224 {
225         char *tmp = NULL;
226         int data_len = 0;
227
228         data_len = strlen((const char *) data);
229         dbg("data_len: %d----%s", data_len, data);
230         if (data_len <= 0) {
231                 return NULL;
232         }
233
234         tmp = g_try_malloc0(data_len - 1);
235         memcpy(tmp, data + 1, data_len - 2);
236         dbg("tmp: [%s]", tmp);
237
238         return tmp;
239 }
240 #endif