3c713f605e815b1c16f10d2a86c606a4e58d00f5
[platform/adaptation/emulator/vmodem-daemon-emulator.git] / vmodem / at / at_msg.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 /*
31 /////////////////////////////////////////////////////////////////////
32 // at_msg.c
33 */
34
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "at_msg.h"
39
40 static HDLCFrame_t s_FrameSentInfo;
41 static HDLCFrame_t s_FrameRecvInfo;
42
43 // Last sent HDLC frame info
44 void ATSetLastSentHDLCFrameInfo(HDLCFrame_t const* pframe)
45 {
46         s_FrameSentInfo.m_CtrlInfo = pframe->m_CtrlInfo;
47         s_FrameSentInfo.m_Length = pframe->m_Length;
48 }
49
50 HDLCFrame_t const* ATGetLastSentHDLCFrameInfo(void)
51 {
52         return &s_FrameSentInfo;
53 }
54
55 // Last receive HDLC frame info
56 void ATSetLastRecvHDLCFrameInfo(HDLCFrame_t const* pframe)
57 {
58 //      TRACE(MSGL_VGSM_INFO, "\n");
59         s_FrameRecvInfo.m_CtrlInfo = pframe->m_CtrlInfo;
60         s_FrameRecvInfo.m_Length = pframe->m_Length;
61 }
62
63 HDLCFrame_t const* ATGetLastRecvHDLCFrameInfo(void)
64 {
65         return &s_FrameRecvInfo;
66 }
67
68 static char* s_ATSentInfo;
69 static char* s_ATRecvInfo;
70
71 // Last sent AT frame info
72 void SetLastSentAT(char* atmsg)
73 {
74         s_ATSentInfo = atmsg;
75 }
76
77 char* GetLastSentAT(void)
78 {
79         return s_ATSentInfo;
80 }
81
82 // Last recv AT info
83 void SetLastRecvAT(char* atmsg)
84 {
85         s_ATRecvInfo = atmsg;
86 }
87
88 char* GetLastRecvAT(void)
89 {
90         return s_ATRecvInfo;
91 }
92
93 // For multi HDLC frame.
94 typedef struct tagHDLCNode {
95         struct tagHDLCNode* m_pNext;
96         HDLCFrame_t m_HDLCNode;
97 } HDLCNode;
98
99 HDLCNode* g_ATpHead = NULL;
100 HDLCNode* g_ATpTail = NULL;
101
102 void ATFreeAllMultiNodeList(void)
103 {
104         TRACE(MSGL_VGSM_INFO, "\n");
105
106         HDLCNode* pnode_tmp;
107         HDLCNode* pnode = g_ATpHead;
108         while (pnode) {
109                 pnode_tmp = pnode;
110                 pnode = pnode->m_pNext;
111
112                 // free data
113                 if (pnode_tmp->m_HDLCNode.m_pValue)
114                         free(pnode_tmp->m_HDLCNode.m_pValue);
115                 free(pnode_tmp);
116         }
117
118         g_ATpHead = g_ATpTail = NULL;
119 }
120
121 /*      ++ 2008-09-29.
122         ++ Name : PushHDLCMultiFrameInfo
123         ++ Args : HDLCFrame_t const* pframe
124         ++ Multi frame ÀÏ ¶§, ID¸¦ check Çϴ ºÎºÐÀÌ ¾ø´Ù.
125         ++ ID ºñ±³Çؼ­ ÀÌ°ÍÀÌ single messageÀÎÁö multiple messageÀÎÁö ÆÇ´ÜÇϴ ºÎºÐÀÌ ÇÊ¿äÇÏ´Ù.
126 */
127 void ATPushHDLCMultiFrameInfo(HDLCFrame_t const* pframe)
128 {
129         TRACE(MSGL_VGSM_INFO, "\n");
130         if (!pframe)
131                 return;
132
133         HDLCNode* pnode = malloc(sizeof(HDLCNode));
134
135         if (!pnode)
136                 return;
137
138         memset(pnode, 0, sizeof(HDLCNode));
139
140         pnode->m_HDLCNode.m_Length = pframe->m_Length;
141         pnode->m_HDLCNode.m_CtrlInfo = pframe->m_CtrlInfo;
142
143         if (pframe->m_Length - 3 > 0) {
144                 pnode->m_HDLCNode.m_pValue = malloc(pframe->m_Length - 3);
145                 memcpy(pnode->m_HDLCNode.m_pValue, pframe->m_pValue, pframe->m_Length - 3);
146         }
147
148         if (!g_ATpHead) {
149                 g_ATpHead = pnode;
150         } else {
151                 if (g_ATpTail)
152                         g_ATpTail->m_pNext = pnode;
153         }
154
155         g_ATpTail = pnode;
156 }
157
158 HDLCFrame_t const* ATGetLastRecvHDLCMultiFrameInfo(void)
159 {
160 //      TRACE(MSGL_VGSM_INFO, "\n");
161
162         if (g_ATpTail)
163                 return &(g_ATpTail->m_HDLCNode);
164
165         return NULL;
166 }
167
168 char* ConvertHDLCMultiFrameToAT(void)
169 {
170         TRACE(MSGL_VGSM_INFO, "\n");
171
172         unsigned char* atmsg_data = NULL;
173         unsigned char* ptr = NULL;
174         int at_size = 0;
175         HDLCNode* pnode = g_ATpHead;
176
177         while (pnode) {
178                 at_size += pnode->m_HDLCNode.m_Length - 3;
179                 pnode = pnode->m_pNext;
180         }
181
182         if (at_size > 0) {
183                 atmsg_data = malloc(at_size);
184                 ptr = atmsg_data;
185                 pnode = g_ATpHead;
186                 while (pnode) {
187                         memcpy(ptr, pnode->m_HDLCNode.m_pValue, pnode->m_HDLCNode.m_Length - 3);
188                         ptr += pnode->m_HDLCNode.m_Length - 3;
189
190                         pnode = pnode->m_pNext;
191                 }
192         }
193
194         ATFreeAllMultiNodeList();
195
196         return (char*)atmsg_data;
197 }
198