4 * Copyright (c) 2013 Samsung Electronics Co. Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include "imc_common.h"
30 void on_send_imc_request(TcorePending *p,
31 TelReturn send_status, void *user_data)
34 (send_status == TEL_RETURN_SUCCESS ? "OK" : "NOK"));
37 ImcRespCbData *imc_create_resp_cb_data(TcoreObjectResponseCallback cb,
38 void *cb_data, void *data, guint data_len)
40 ImcRespCbData *resp_cb_data;
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);
51 void imc_destroy_resp_cb_data(ImcRespCbData *resp_cb_data)
54 tcore_free(resp_cb_data);
59 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
62 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
64 #define bitsize(type) (sizeof(type) * 8)
66 #define copymask(type) ((0xffffffff) >> (32 - bitsize(type)))
68 #define MASK(width, offset, data) \
69 (((width) == bitsize(data)) ? (data) : \
70 ((((copymask(data) << (bitsize(data) - ((width) % bitsize(data)))) & copymask(data)) >> (offset)) & (data))) \
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)))) \
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);
82 void util_hex_dump(char *pad, int size, const void *data)
84 char buf[255] = {0, };
90 msg("%sno data", pad);
94 p = (unsigned char *) data;
96 snprintf(buf, 255, "%s%04X: ", pad, 0);
97 for (i = 0; i < size; i++) {
98 snprintf(hex, 4, "%02X ", p[i]);
101 if ((i + 1) % 8 == 0) {
102 if ((i + 1) % 16 == 0) {
105 snprintf(buf, 255, "%s%04X: ", pad, i + 1);
115 unsigned char util_hexCharToInt(char c)
117 if (c >= '0' && c <= '9')
119 else if (c >= 'A' && c <= 'F')
120 return (c - 'A' + 10);
121 else if (c >= 'a' && c <= 'f')
122 return (c - 'a' + 10);
124 dbg("invalid charater!!");
129 char *util_hex_to_string(const char *src, unsigned int src_len)
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]);
142 dest[src_len * 2] = '\0';
147 char* util_hexStringToBytes(char *s)
158 ret = g_try_malloc0((sz / 2) + 1);
160 dbg("Convert String to Binary!!");
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]);
170 char _util_unpackb(const char *src, int pos, int len)
178 rshift = MAX(8 - (pos + len), 0);
181 result = MASK_AND_SHIFT(len, pos, rshift, (unsigned char)*src);
183 result = MASK(8 - pos, pos, (unsigned char)*src);
187 if (len > 0) result = (result << len) | (*src >> (8 - len)); // if any bits left
193 char _util_convert_byte_hexChar(char val)
198 hex_char = (char) (val + '0');
199 } else if (val >= 10 && val <= 15) {
200 hex_char = (char) (val - 10 + 'A');
208 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes)
214 for (i = 0; i < num_bytes * 2; i++) {
215 nibble = _util_unpackb(byte_pdu, buf_pos, 4);
217 hex_pdu[i] = _util_convert_byte_hexChar(nibble);
223 char* util_removeQuotes(void *data)
228 data_len = strlen((const char *) data);
229 dbg("data_len: %d----%s", data_len, data);
234 tmp = g_try_malloc0(data_len - 1);
235 memcpy(tmp, data + 1, data_len - 2);
236 dbg("tmp: [%s]", tmp);