|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * System Includes |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 #include <time.h> |
|
22 #include <assert.h> |
|
23 |
|
24 /********************************************************************************************** |
|
25 * |
|
26 * Local Includes |
|
27 * |
|
28 *********************************************************************************************/ |
|
29 #include "CUCCSCommandProtocol.h" |
|
30 #include "CFrameBuffer.h" |
|
31 #include "strncpynt.h" |
|
32 |
|
33 /********************************************************************************************** |
|
34 * |
|
35 * Defines |
|
36 * |
|
37 *********************************************************************************************/ |
|
38 |
|
39 /********************************************************************************************** |
|
40 * |
|
41 * Constructor |
|
42 * |
|
43 *********************************************************************************************/ |
|
44 CUCCSCommandProtocol::CUCCSCommandProtocol() |
|
45 { |
|
46 iPort = NULL; |
|
47 iOutput = NULL; |
|
48 |
|
49 // Seed the random number generator |
|
50 srand( (unsigned)time(NULL) ); |
|
51 } |
|
52 |
|
53 /********************************************************************************************** |
|
54 * |
|
55 * Destructor |
|
56 * |
|
57 *********************************************************************************************/ |
|
58 CUCCSCommandProtocol::~CUCCSCommandProtocol() |
|
59 { |
|
60 } |
|
61 |
|
62 /********************************************************************************************** |
|
63 * |
|
64 * Initialise |
|
65 * |
|
66 *********************************************************************************************/ |
|
67 TCPError CUCCSCommandProtocol::initialise(TRemoteControlTransport aTransport, char* aRemoteHost, IOutput* aOutput) |
|
68 { |
|
69 int ret; |
|
70 |
|
71 // Check params are not equal to NULL |
|
72 assert (aRemoteHost != NULL); |
|
73 assert (strlen(aRemoteHost) < (KMAXPORTNAME -1)); |
|
74 assert (aOutput != NULL); |
|
75 |
|
76 // Assign the params to the relevant member data. |
|
77 if ( aTransport == RCI_SERIAL ) |
|
78 { |
|
79 iPort = &iSerialPort; |
|
80 } |
|
81 else if ( aTransport == RCI_CONSOLE) |
|
82 { |
|
83 iPort = &iConsolePort; |
|
84 } |
|
85 else if ( aTransport == RCI_TCP) |
|
86 { |
|
87 iPort = &iTcpPort; |
|
88 } |
|
89 else |
|
90 { |
|
91 return TCP_INVALIDTRANSPORT; |
|
92 } |
|
93 |
|
94 // Set the state members |
|
95 STRNCPY_NULL_TERMINATE( iRemoteHost, aRemoteHost, KMAXPORTNAME ); |
|
96 iOutput = aOutput; |
|
97 iRand_UID = (unsigned)rand(); |
|
98 |
|
99 // Open the serial port |
|
100 ret = iPort->OpenPort(iRemoteHost); |
|
101 if( ret != 0 ) { |
|
102 return TCP_FAILEDTOOPENPORT; |
|
103 } |
|
104 return TCP_SUCCESS; |
|
105 } |
|
106 |
|
107 /********************************************************************************************** |
|
108 * |
|
109 * Disconnect |
|
110 * |
|
111 *********************************************************************************************/ |
|
112 TCPError CUCCSCommandProtocol::disconnect() |
|
113 { |
|
114 // Close the serial port. |
|
115 iPort->ClosePort(); |
|
116 iPort = NULL; |
|
117 |
|
118 return TCP_SUCCESS; |
|
119 } |
|
120 |
|
121 /********************************************************************************************** |
|
122 * |
|
123 * SendReply |
|
124 * |
|
125 *********************************************************************************************/ |
|
126 TCPError CUCCSCommandProtocol::sendReply( TPCommand aCommand, int aDataSize, void* aData ) |
|
127 { |
|
128 int iFrameSize, ret; |
|
129 TFBError err; |
|
130 |
|
131 if (!isValidCMDID(aCommand)) |
|
132 return TCP_INVALIDCMDID; |
|
133 |
|
134 iFrameSize = KMAXFRAMESIZE; |
|
135 err = CFrameBuffer::createSendBuffer( iRand_UID, aCommand, aDataSize, (char*)aData, iFrame, &iFrameSize ); |
|
136 if ( err != TFB_SUCCESS) |
|
137 return TCP_ERRCREATINGBUFFER; |
|
138 |
|
139 ret = iPort->SendBytes(iFrame, &iFrameSize); |
|
140 if ( ret != 0 ) |
|
141 return TCP_ERRSENDINGBYTES; |
|
142 |
|
143 return TCP_SUCCESS; |
|
144 } |
|
145 |
|
146 /********************************************************************************************** |
|
147 * |
|
148 * ReceiveMessage |
|
149 * |
|
150 *********************************************************************************************/ |
|
151 TCPError CUCCSCommandProtocol::receiveMessage( TPCommand* aCommand, int* aDataLength, void* aData ) |
|
152 { |
|
153 int res, len; |
|
154 TPHeader header; |
|
155 |
|
156 len = sizeof(header); |
|
157 |
|
158 // Read the first 12 bytes of header - the uid, command and the data size. |
|
159 res = iPort->ReceiveBytes((char*)&header, &len ); |
|
160 if ( res != 0 ) |
|
161 return TCP_ERRRECVINGBYTES; |
|
162 |
|
163 // Decode the response |
|
164 iRand_UID = header.iUid; |
|
165 *aCommand = header.iCmdID; |
|
166 *aDataLength = header.iDataLen; |
|
167 |
|
168 // Get the rest |
|
169 res = iPort->ReceiveBytes((char*)aData, aDataLength); |
|
170 if ( res != 0 ) |
|
171 return TCP_ERRRECVINGBYTES; |
|
172 |
|
173 return TCP_SUCCESS; |
|
174 } |
|
175 |
|
176 /********************************************************************************************** |
|
177 * |
|
178 * Check IDs |
|
179 * |
|
180 *********************************************************************************************/ |
|
181 bool CUCCSCommandProtocol::isValidCMDID(TPCommand aCommand) |
|
182 { |
|
183 bool ret_val = true; |
|
184 |
|
185 switch (aCommand) |
|
186 { |
|
187 case CMD_REP_SIGNALID: |
|
188 break; |
|
189 |
|
190 case CMD_REP_RENDEZVOUSID: |
|
191 break; |
|
192 |
|
193 case CMD_REP_WAITID: |
|
194 break; |
|
195 |
|
196 case CMD_REP_STARTUSECASEID: |
|
197 break; |
|
198 |
|
199 case CMD_REP_ENDUSECASEID: |
|
200 break; |
|
201 |
|
202 case CMD_REP_GETVARIABLENAMEID: |
|
203 break; |
|
204 |
|
205 case CMD_QUITID: |
|
206 break; |
|
207 |
|
208 case CMD_REP_RUNCOMMAND: |
|
209 break; |
|
210 |
|
211 default: |
|
212 ret_val = false; |
|
213 break; |
|
214 } |
|
215 |
|
216 return ret_val; |
|
217 } |
|
218 |