s_sat.c: Fix envelope cmd and enable Setup Event List
[platform/core/telephony/tel-plugin-imc.git] / src / s_common.c
1 /*
2  * tel-plugin-imc
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include <glib.h>
26 #include <log.h>
27
28
29 #include "s_common.h"
30
31 #undef  MAX
32 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
33
34 #undef  MIN
35 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
36
37 #define bitsize(type) (sizeof(type) * 8)
38
39 #define copymask(type) ((0xffffffff) >> (32 - bitsize(type)))
40
41 #define MASK(width, offset, data) \
42         (((width) == bitsize(data)) ? (data) :   \
43          ((((copymask(data) << (bitsize(data) - ((width) % bitsize(data)))) & copymask(data)) >> (offset)) & (data))) \
44
45
46 #define MASK_AND_SHIFT(width, offset, shift, data)      \
47         ((((signed) (shift)) < 0) ?               \
48          MASK((width), (offset), (data)) << -(shift) :  \
49          MASK((width), (offset), (data)) >> (((signed) (shift)))) \
50
51 char _util_unpackb(const char *src, int pos, int len);
52 char _util_convert_byte_hexChar(char val);
53 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes);
54
55 void util_hex_dump(char *pad, int size, const void *data)
56 {
57         char buf[255] = {0, };
58         char hex[4] = {0, };
59         int i;
60         unsigned char *p;
61
62         if (size <= 0) {
63                 msg("%sno data", pad);
64                 return;
65         }
66
67         p = (unsigned char *) data;
68
69         snprintf(buf, 255, "%s%04X: ", pad, 0);
70         for (i = 0; i < size; i++) {
71                 snprintf(hex, 4, "%02X ", p[i]);
72                 strcat(buf, hex);
73
74                 if ((i + 1) % 8 == 0) {
75                         if ((i + 1) % 16 == 0) {
76                                 msg("%s", buf);
77                                 memset(buf, 0, 255);
78                                 snprintf(buf, 255, "%s%04X: ", pad, i + 1);
79                         } else {
80                                 strcat(buf, "  ");
81                         }
82                 }
83         }
84
85         msg("%s", buf);
86 }
87
88 unsigned char util_hexCharToInt(char c)
89 {
90         if (c >= '0' && c <= '9')
91                 return (c - '0');
92         else if (c >= 'A' && c <= 'F')
93                 return (c - 'A' + 10);
94         else if (c >= 'a' && c <= 'f')
95                 return (c - 'a' + 10);
96         else {
97                 dbg("invalid charater!!");
98                 return -1;
99         }
100 }
101
102 char* util_hexStringToBytes(char *s)
103 {
104         char *ret;
105         int i;
106         int sz;
107
108         if (s == NULL)
109                 return NULL;
110
111         sz = strlen(s);
112
113         ret = calloc((sz / 2) + 1, 1);
114
115         dbg("Convert String to Binary!!");
116
117         for (i = 0; i < sz; i += 2) {
118                 ret[i / 2] = (char) ((util_hexCharToInt(s[i]) << 4) | util_hexCharToInt(s[i + 1]));
119                 dbg("[%02x]", ret[i / 2]);
120         }
121
122         return ret;
123 }
124
125 char _util_unpackb(const char *src, int pos, int len)
126 {
127         char result = 0;
128         int rshift = 0;
129
130         src += pos / 8;
131         pos %= 8;
132
133         rshift = MAX(8 - (pos + len), 0);
134
135         if (rshift > 0) {
136                 result = MASK_AND_SHIFT(len, pos, rshift, (unsigned char)*src);
137         } else {
138                 result = MASK(8 - pos, pos, (unsigned char)*src);
139                 src++;
140                 len -= 8 - pos;
141
142                 if (len > 0) result = (result << len) | (*src >> (8 - len));   // if any bits left
143         }
144
145         return result;
146 }
147
148 char _util_convert_byte_hexChar(char val)
149 {
150         char hex_char;
151
152         if (val <= 9) {
153                 hex_char = (char) (val + '0');
154         } else if (val >= 10 && val <= 15) {
155                 hex_char = (char) (val - 10 + 'A');
156         } else {
157                 hex_char = '0';
158         }
159
160         return (hex_char);
161 }
162
163 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes)
164 {
165         int i;
166         char nibble;
167         int buf_pos = 0;
168
169         for (i = 0; i < num_bytes * 2; i++) {
170                 nibble = _util_unpackb(byte_pdu, buf_pos, 4);
171                 buf_pos += 4;
172                 hex_pdu[i] = _util_convert_byte_hexChar(nibble);
173         }
174
175         return TRUE;
176 }
177
178 char* util_removeQuotes(void *data)
179 {
180         char *tmp = NULL;
181         int data_len = 0;
182
183         data_len = strlen((const char *) data);
184         dbg("data_len: %d----%s", data_len, data);
185         if (data_len <= 0) {
186                 return NULL;
187         }
188         tmp = calloc(1, data_len - 1);
189         memcpy(tmp, data + 1, data_len - 2);
190         dbg("tmp: %s", tmp);
191
192         return tmp;
193 }