4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Ja-young Gu <jygu@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
33 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
36 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
38 #define bitsize(type) (sizeof(type) * 8)
40 #define copymask(type) ((0xffffffff) >> (32 - bitsize(type)))
42 #define MASK(width, offset, data) \
43 (((width) == bitsize(data)) ? (data) : \
44 ((((copymask(data) << (bitsize(data) - ((width) % bitsize(data)))) & copymask(data)) >> (offset)) & (data))) \
47 #define MASK_AND_SHIFT(width, offset, shift, data) \
48 ((((signed) (shift)) < 0) ? \
49 MASK((width), (offset), (data)) << -(shift) : \
50 MASK((width), (offset), (data)) >> (((signed) (shift)))) \
52 char _util_unpackb(const char *src, int pos, int len);
53 char _util_convert_byte_hexChar(char val);
54 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes);
56 void util_hex_dump(char *pad, int size, const void *data)
58 char buf[255] = {0, };
64 msg("%sno data", pad);
68 p = (unsigned char *) data;
70 snprintf(buf, 255, "%s%04X: ", pad, 0);
71 for (i = 0; i < size; i++) {
72 snprintf(hex, 4, "%02X ", p[i]);
75 if ((i + 1) % 8 == 0) {
76 if ((i + 1) % 16 == 0) {
79 snprintf(buf, 255, "%s%04X: ", pad, i + 1);
89 void hook_hex_dump(enum direction_e d, int size, const void *data)
91 msg("=== TX data DUMP =====");
92 util_hex_dump(" ", size, data);
93 msg("=== TX data DUMP =====");
96 unsigned int util_assign_message_sequence_id(TcorePlugin *p)
98 struct global_data *gd;
101 dbg("plugin is NULL");
105 gd = tcore_plugin_ref_user_data(p);
107 dbg("global data is NULL");
111 if (gd->msg_auto_id_current == 0) {
112 gd->msg_auto_id_current = gd->msg_auto_id_start;
113 dbg("pending_auto_id_current is 0, reset to start");
114 } else if (gd->msg_auto_id_current >= gd->msg_auto_id_end) {
115 gd->msg_auto_id_current = gd->msg_auto_id_start;
116 dbg("pending_auto_id_current is over, reset to start");
118 gd->msg_auto_id_current++;
121 dbg("message_sequence_id = %d", gd->msg_auto_id_current);
123 return gd->msg_auto_id_current;
126 gboolean util_add_waiting_job(GQueue *queue, unsigned int id, UserRequest *ur)
128 struct work_queue_data *wqd;
133 wqd = calloc(sizeof(struct work_queue_data), 1);
138 wqd->ur = tcore_user_request_ref(ur);
139 g_queue_push_tail(queue, wqd);
141 dbg("id = %d, ur = 0x%x", wqd->id, wqd->ur);
145 UserRequest* util_pop_waiting_job(GQueue *queue, unsigned int id)
149 struct work_queue_data *wqd;
155 dbg("before waiting job count: %d", g_queue_get_length(queue));
158 wqd = g_queue_peek_nth(queue, i);
163 wqd = g_queue_pop_nth(queue, i);
168 } while (wqd != NULL);
170 dbg("after waiting job count: %d", g_queue_get_length(queue));
181 unsigned char util_hexCharToInt(char c)
183 if (c >= '0' && c <= '9')
185 else if (c >= 'A' && c <= 'F')
186 return (c - 'A' + 10);
187 else if (c >= 'a' && c <= 'f')
188 return (c - 'a' + 10);
190 dbg("invalid charater!!");
195 char* util_hexStringToBytes(char *s)
206 ret = calloc((sz / 2) + 1, 1);
208 dbg("Convert String to Binary!!");
210 for (i = 0; i < sz; i += 2) {
211 ret[i / 2] = (char) ((util_hexCharToInt(s[i]) << 4) | util_hexCharToInt(s[i + 1]));
212 dbg("[%02x]", ret[i / 2]);
218 char _util_unpackb(const char *src, int pos, int len)
226 rshift = MAX(8 - (pos + len), 0);
229 result = MASK_AND_SHIFT(len, pos, rshift, *src);
231 result = MASK(8 - pos, pos, *src);
235 if (len > 0) result = (result << len) | (*src >> (8 - len)); // if any bits left
241 char _util_convert_byte_hexChar(char val)
246 hex_char = (char) (val + '0');
247 } else if (val >= 10 && val <= 15) {
248 hex_char = (char) (val - 10 + 'A');
256 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes)
262 for (i = 0; i < num_bytes * 2; i++) {
263 nibble = _util_unpackb(byte_pdu, buf_pos, 4);
265 hex_pdu[i] = _util_convert_byte_hexChar(nibble);
271 char* util_removeQuotes(void *data)
276 data_len = strlen((const char *) data);
277 dbg("data_len: %d----%s", data_len, data);
281 tmp = calloc(1, data_len - 1);
282 memcpy(tmp, data + 1, data_len - 2);