d386b0dd92325bc05c8a908f14c08db7d9fe2a45
[platform/adaptation/emulator/vmodem-daemon-emulator.git] / libvmodem / lxtutil.c
1 /*
2  *  telephony-emulator
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  * Sooyoung Ha <yoosah.ha@samsung.com>
8  * Sungmin Ha <sungmin82.ha@samsung.com>
9  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
10  * 
11  * This library is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the
13  * Free Software Foundation; either version 2.1 of the License, or (at your option)
14  * any later version.
15  * 
16  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19  * License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this library; if not, write to the Free Software Foundation, Inc., 51
23  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  *
25  * Contributors:
26  * - S-Core Co., Ltd
27  * 
28  */
29
30 // ++++++++++++++++++++++++++++++++++++++++++++++include about standard library
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <assert.h>
35 #include <string.h>
36 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++include user define
37 #include "lxtutil.h"
38 #include "linuxtapi.h"
39
40 #include <errno.h>
41 #include <string.h>
42
43 #include "libvgsm_debug.h"
44 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++define area
45
46 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++global variable area
47
48 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++define public function
49
50 // read raw bytes
51 int lxt_util_readRawBytes(int fd, void *data, int size)
52 {
53     int rc;
54
55     // check
56     if ( (data == 0) || (fd < 0) )
57     {
58 #ifndef _NO_ESPRESSO_DEBUG_
59         LIBVGSM_DEBUG("Source Invalid \n");
60 #endif // _NO_ESPRESSO_DEBUG_
61
62         return -1;
63     }
64
65     rc = read(fd, data, size);
66
67     if (rc <= 0)
68     {
69 #ifndef _NO_ESPRESSO_DEBUG_
70                 fprintf(stderr,"File read error:%s\n",strerror(errno));
71                 LIBVGSM_DEBUG("Read Fail and size=%d,  rc=%d\n",size,rc);
72 #endif // _NO_ESPRESSO_DEBUG_
73
74         return -1;
75     }
76
77 #ifndef _NO_ESPRESSO_DEBUG_
78     assert(rc == size);
79 #endif
80
81     return rc;
82 }
83
84 // write raw bytes
85 int lxt_util_writeRawBytes(int fd, const void *data, int size)
86 {
87     int rc;
88
89         LIBVGSM_DEBUG("%s\n", __FUNCTION__);
90
91     // check
92     if ( (data == 0) || (fd < 0) )
93     {
94 #ifndef _NO_ESPRESSO_DEBUG_
95         LIBVGSM_DEBUG("Source Invalid\n");
96 #endif // _NO_ESPRESSO_DEBUG_
97
98         return -1;
99     }
100
101     rc = write(fd, data, size);
102
103     if (rc <= 0)
104     {
105 #ifndef _NO_ESPRESSO_DEBUG_
106         LIBVGSM_DEBUG("Write Failed\n");
107 #endif // _NO_ESPRESSO_DEBUG_
108
109         return -1;
110     }
111
112 #ifndef _NO_ESPRESSO_DEBUG_
113     assert(rc == size);
114 #endif
115
116     return rc;
117
118 }
119
120 void lxt_util_rawdataPrint(void *rawdata, int rawdatasize, const char *title)
121 {
122         int i;
123         int len;
124         unsigned char *p;
125
126         LIBVGSM_DEBUG("%s\n", __FUNCTION__);
127
128         if ( (rawdatasize > 0) && (rawdata == NULL) )
129         {
130                 return;
131         }
132
133         printf(title);
134
135     // save pointer
136     p = (unsigned char *)rawdata;
137
138     // save length
139     len = rawdatasize;
140
141     for (i = 0; i < len; i++)
142     {
143         if (!(i%16))
144             printf("\n");
145         printf("%02x ", p[i]);
146
147     }
148     printf("\n");
149 }
150
151 // tx data to phone server
152 int lxt_msg_send_to_server(int fd, const void *data, int size)
153 {
154     int rc = -1;
155
156         LIBVGSM_DEBUG("%s\n", __FUNCTION__);
157
158     // check
159     if ( (data == 0) || (fd < 0) )
160         return rc;
161
162     // tx to phone server
163     rc = lxt_util_writeRawBytes(fd, data, size);
164
165     return rc;
166 }
167
168 // tx lxt messge to server
169 int lxt_msg_send_message(int fd, unsigned char g, unsigned char a, unsigned int len, const void *data)
170 {
171     LXT_MESSAGE packet;
172     int rc;
173
174         LIBVGSM_DEBUG("lxt_msg_send_message : fd=%d, total size = 4+%d \n",fd, len);
175
176     // save group
177     packet.group = g;
178
179     // save action
180     packet.action = a;
181
182     // save length
183     packet.length = len;
184
185     // tx to phone server
186     rc = lxt_msg_send_to_server(fd, &packet, 4);
187
188         if(rc != 4 )
189                 printf("[ %s ] [WARNING] write fail : rc = %d \n", __FUNCTION__, rc);
190
191     if (len > 0)
192     {
193         // tx to phone server if exists data
194         rc = lxt_msg_send_to_server(fd, data, len);
195                 if(rc != len )
196                         printf("[ %s ] [WARNING] write fail : rc = %d \n", __FUNCTION__, rc);
197     }
198
199     return rc;
200 }
201