4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
\r
6 * Contact: Ja-young Gu <jygu@samsung.com>
\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
12 * http://www.apache.org/licenses/LICENSE-2.0
\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
28 #include "s_common.h"
\r
33 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
\r
36 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
\r
38 #define bitsize(type) (sizeof(type) * 8)
\r
40 #define copymask(type) ((0xffffffff) >> (32 - bitsize(type)))
\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
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
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
56 void util_hex_dump(char *pad, int size, const void *data)
\r
58 char buf[255] = {0, };
\r
59 char hex[4] = {0, };
\r
64 msg("%sno data", pad);
\r
68 p = (unsigned char *)data;
\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
75 if ((i + 1) % 8 == 0) {
\r
76 if ((i + 1) % 16 == 0) {
\r
78 memset(buf, 0, 255);
\r
79 snprintf(buf, 255, "%s%04X: ", pad, i + 1);
\r
90 void hook_hex_dump(enum direction_e d, int size, const void *data)
\r
92 msg("=== TX data DUMP =====");
\r
93 util_hex_dump(" ", size, data);
\r
94 msg("=== TX data DUMP =====");
\r
98 unsigned int util_assign_message_sequence_id(TcorePlugin *p)
\r
100 struct global_data *gd;
\r
103 dbg("plugin is NULL");
\r
107 gd = tcore_plugin_ref_user_data(p);
\r
109 dbg("global data is NULL");
\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
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
122 gd->msg_auto_id_current++;
\r
125 dbg("message_sequence_id = %d", gd->msg_auto_id_current);
\r
127 return gd->msg_auto_id_current;
\r
130 gboolean util_add_waiting_job(GQueue *queue, unsigned int id, UserRequest *ur)
\r
132 struct work_queue_data *wqd;
\r
137 wqd = calloc(sizeof(struct work_queue_data), 1);
\r
142 wqd->ur = tcore_user_request_ref(ur);
\r
143 g_queue_push_tail(queue, wqd);
\r
145 dbg("id = %d, ur = 0x%x", wqd->id, wqd->ur);
\r
149 UserRequest *util_pop_waiting_job(GQueue *queue, unsigned int id)
\r
153 struct work_queue_data *wqd;
\r
159 dbg("before waiting job count: %d", g_queue_get_length(queue));
\r
162 wqd = g_queue_peek_nth(queue, i);
\r
166 if (wqd->id == id) {
\r
167 wqd = g_queue_pop_nth(queue, i);
\r
172 } while (wqd != NULL);
\r
174 dbg("after waiting job count: %d", g_queue_get_length(queue));
\r
185 unsigned char util_hexCharToInt(char c)
\r
187 if (c >= '0' && c <= '9')
\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
195 dbg("invalid charater!!");
\r
200 char * util_hexStringToBytes(char * s)
\r
211 ret = calloc((sz/2)+1, 1);
\r
213 dbg("Convert String to Binary!!");
\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
223 char _util_unpackb(const char *src, int pos, int len)
\r
231 rshift = MAX( 8 - (pos + len), 0);
\r
233 if ( rshift > 0 ) {
\r
235 result = MASK_AND_SHIFT(len, pos, rshift, *src);
\r
239 result = MASK(8-pos, pos, *src);
\r
243 if ( len > 0 ) result = ( result<<len ) | (*src >> (8-len)); // if any bits left
\r
249 char _util_convert_byte_hexChar (char val)
\r
255 hex_char = (char)(val+'0');
\r
257 else if (val >= 10 && val <= 15)
\r
259 hex_char = (char)(val-10+'A');
\r
269 gboolean util_byte_to_hex(const char *byte_pdu, char *hex_pdu, int num_bytes)
\r
275 for (i=0; i<num_bytes*2; i++)
\r
277 nibble = _util_unpackb(byte_pdu,buf_pos,4);
\r
279 hex_pdu[i] = _util_convert_byte_hexChar(nibble);
\r