|
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 * CConsolePort |
|
16 * System Includes |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 |
|
22 #include <stdio.h> |
|
23 #include <assert.h> |
|
24 |
|
25 /********************************************************************* |
|
26 * |
|
27 * Local Includes |
|
28 * |
|
29 ********************************************************************/ |
|
30 #include "CConsolePort.h" |
|
31 #include "../Core/UCCS_ErrorCodes.h" |
|
32 #include "CProtocolTypes.h" |
|
33 |
|
34 /********************************************************************* |
|
35 * |
|
36 * File-scope statics |
|
37 * |
|
38 ********************************************************************/ |
|
39 char *gKnownCommands[] = { "RUBBISH", "startusecase", "signal", "rendezvous", "endusecase", "quit", "waitforsignal", "runcmd", NULL }; |
|
40 |
|
41 |
|
42 /********************************************************************* |
|
43 * |
|
44 * Construction |
|
45 * |
|
46 ********************************************************************/ |
|
47 CConsolePort::CConsolePort() |
|
48 : iReadingData(0), iUcid(0), iCommand(0) |
|
49 { |
|
50 memset( iCommandLine, 0x00, sizeof(iCommandLine) ); |
|
51 } |
|
52 |
|
53 |
|
54 CConsolePort::~CConsolePort() |
|
55 { |
|
56 } |
|
57 |
|
58 |
|
59 /********************************************************************* |
|
60 * |
|
61 * OpenPort() -- |
|
62 * |
|
63 ********************************************************************/ |
|
64 int CConsolePort::OpenPort( char *port ) |
|
65 { |
|
66 return 0; |
|
67 } |
|
68 |
|
69 |
|
70 /********************************************************************* |
|
71 * |
|
72 * ClosePort() -- |
|
73 * |
|
74 ********************************************************************/ |
|
75 void CConsolePort::ClosePort() |
|
76 { |
|
77 } |
|
78 |
|
79 /********************************************************************* |
|
80 * |
|
81 * ReadData() |
|
82 * |
|
83 ********************************************************************/ |
|
84 int CConsolePort::ReadData( char* aBuff, int *aSize) |
|
85 { |
|
86 int rand_uid = 1; |
|
87 int eng_result = 0; |
|
88 |
|
89 TEndUsecaseRequest* endusecase; |
|
90 TRunCommandRequest* runcommand; |
|
91 // Note we are using the startusecase structure for all the commands, as they |
|
92 // are identical in the data they contain. |
|
93 TStartUsecaseRequest* othercases; |
|
94 |
|
95 // If the command we are reading the header for is Endusecase, then we |
|
96 // have 8 bytes of data as opposed to 4, like the rest of the commands. |
|
97 // Therefore, need to distinguish between the commands. |
|
98 |
|
99 // If the Endusecase case |
|
100 if (iCommand == 4) |
|
101 { |
|
102 endusecase = (TEndUsecaseRequest*)aBuff; |
|
103 endusecase->iUsecaseID = iUcid; |
|
104 endusecase->iResult = eng_result; |
|
105 *aSize = sizeof(endusecase); |
|
106 iReadingData = 0; |
|
107 return 0; |
|
108 } |
|
109 else if( iCommand == 7 ) |
|
110 { |
|
111 runcommand = (TRunCommandRequest*)aBuff; |
|
112 if( strlen(iCommandLine) > 0 ) |
|
113 { |
|
114 memset( runcommand->iCommandLine, 0x00, sizeof(MAXCOMMANDLINELEN) ); |
|
115 strcpy( runcommand->iCommandLine, iCommandLine ); |
|
116 memset( iCommandLine, 0x00, sizeof(iCommandLine) ); |
|
117 } |
|
118 runcommand->iResult = eng_result; |
|
119 *aSize = sizeof(runcommand); |
|
120 iReadingData = 0; |
|
121 return 0; |
|
122 } |
|
123 else |
|
124 { |
|
125 othercases = (TStartUsecaseRequest*)aBuff; |
|
126 othercases->iUsecaseID = iUcid; |
|
127 *aSize = sizeof(othercases); |
|
128 iReadingData = 0; |
|
129 return 0; |
|
130 } |
|
131 } |
|
132 |
|
133 /********************************************************************* |
|
134 * |
|
135 * ReadHeader() |
|
136 * |
|
137 ********************************************************************/ |
|
138 int CConsolePort::ReadHeader( char* aBuff, int *aSize) |
|
139 { |
|
140 int uid; |
|
141 int rand_uid = 1; |
|
142 int eng_result = 0; |
|
143 int ret; |
|
144 |
|
145 TPHeader *header = (TPHeader*)aBuff; |
|
146 |
|
147 do { |
|
148 |
|
149 // Get the next command |
|
150 iCommand = ParseNextLine( &uid ); |
|
151 |
|
152 // If we have encountered an invalid command return |
|
153 if( (iCommand == -2) ) |
|
154 { |
|
155 return -1; //EInvalidCommand |
|
156 } |
|
157 |
|
158 header->iUid = rand_uid; |
|
159 ret = GetTPCommand(iCommand, &(header->iCmdID)); |
|
160 if ( ret != 0 ) |
|
161 { |
|
162 return -1; //EInvalidCommand |
|
163 } |
|
164 |
|
165 // If the command is startusecase, for every other command we just ignore this |
|
166 if( iCommand == 1 ) |
|
167 { |
|
168 iUcid = uid; |
|
169 } |
|
170 |
|
171 // If the command is endusecase - iDataLen is 8 - as currently we have 4 bytes |
|
172 // for the uid and 4 for the result. |
|
173 if(iCommand == 4 ) |
|
174 { |
|
175 header->iDataLen = E8DataLen; |
|
176 } |
|
177 else |
|
178 { |
|
179 header->iDataLen = E4DataLen; |
|
180 } |
|
181 |
|
182 // 4 bytes each for each field in the header. |
|
183 *aSize = sizeof(*header); |
|
184 } while( iCommand == -1 ); |
|
185 |
|
186 return 0; |
|
187 } |
|
188 |
|
189 |
|
190 /********************************************************************* |
|
191 * |
|
192 * ReceiveBytes() |
|
193 * |
|
194 ********************************************************************/ |
|
195 int CConsolePort::ReceiveBytes( char* aBuff, int *aSize) |
|
196 { |
|
197 int ret = 0; |
|
198 |
|
199 // If we are just the data part of the frame iReadingFlag is true |
|
200 if( iReadingData == 1 ) |
|
201 { |
|
202 ret = ReadData( aBuff, aSize ); |
|
203 if ( ret != 0 ) |
|
204 return -1; |
|
205 |
|
206 } |
|
207 else |
|
208 { |
|
209 // Else we are reading the header - so get the next command |
|
210 ret = ReadHeader( aBuff, aSize ); |
|
211 if ( ret != 0 ) |
|
212 return -1; |
|
213 |
|
214 // Set the flag to true now that we have read the header, |
|
215 // so that the data part is read in the next iteration. |
|
216 iReadingData = 1; |
|
217 } |
|
218 |
|
219 // Return |
|
220 return ret; |
|
221 } |
|
222 |
|
223 |
|
224 /********************************************************************* |
|
225 * |
|
226 * SendBytes() |
|
227 * |
|
228 ********************************************************************/ |
|
229 int CConsolePort::SendBytes( char *aBuff, int *aSize ) |
|
230 { |
|
231 int data_base_index; |
|
232 TPHeader* header; |
|
233 int *engine_result, *script_result; |
|
234 |
|
235 // extract the info from the buffer |
|
236 header = (TPHeader*)aBuff; |
|
237 data_base_index = sizeof(TPHeader); |
|
238 engine_result = ((int*)(&aBuff[data_base_index])); |
|
239 script_result = ((int*)(&aBuff[data_base_index+4])); |
|
240 |
|
241 // print out the information for the endusecase command |
|
242 if( header->iCmdID == CMD_REP_ENDUSECASEID ) { |
|
243 assert( header->iDataLen == E8DataLen ); |
|
244 if( (*script_result) == 0 ) { |
|
245 fprintf( stdout, " %s - No script errors occured.\n\n", GetUccsErrorStringI(*engine_result) ); |
|
246 } else if( ((*script_result) > UCCS_OK) && ((*script_result) < UCCS_LAST) ) { |
|
247 fprintf( stdout, " %s - %s\n\n", GetUccsErrorStringI(*engine_result), GetUccsErrorStringI(*script_result) ); |
|
248 } else { |
|
249 fprintf( stdout, " %s - %d\n\n", GetUccsErrorStringI(*engine_result), *script_result ); |
|
250 } |
|
251 *aSize = 8; |
|
252 } |
|
253 |
|
254 // print out the information for all other commands |
|
255 if( header->iCmdID != CMD_REP_ENDUSECASEID ) |
|
256 { |
|
257 assert ( header->iDataLen == E4DataLen ); |
|
258 if (*engine_result < 0) |
|
259 fprintf(stdout, "Error! %s (%d)\n\n", GetUccsErrorStringI(*engine_result), *engine_result); |
|
260 else |
|
261 fprintf(stdout, "Success! (0x%08x)\n\n", *engine_result); |
|
262 *aSize = 4; |
|
263 } |
|
264 |
|
265 // done |
|
266 return 0; |
|
267 } |
|
268 |
|
269 |
|
270 /********************************************************************* |
|
271 * |
|
272 * ParseNextLine() |
|
273 * |
|
274 ********************************************************************/ |
|
275 int CConsolePort::ParseNextLine( int *uid ) |
|
276 { |
|
277 char *cp; |
|
278 char cline[MAXLINESIZE]; |
|
279 char command[MAXLINESIZE]; |
|
280 int token_count; |
|
281 int i; |
|
282 int match; |
|
283 |
|
284 // check param |
|
285 assert( uid != NULL ); |
|
286 |
|
287 // write a litte prompt |
|
288 fprintf( stdout, "> " ); |
|
289 |
|
290 // read the line |
|
291 cp = fgets( cline, MAXLINESIZE, stdin ); |
|
292 if( cp == NULL ) { |
|
293 return -2; |
|
294 } |
|
295 |
|
296 // tokenise the line |
|
297 token_count = sscanf( cline, "%s %d\n", command, uid ); |
|
298 |
|
299 // if there are no tokens then leave |
|
300 if( token_count <= 0 ) { |
|
301 return -1; |
|
302 } |
|
303 |
|
304 // otherwise try and match the token |
|
305 for( i = 0; gKnownCommands[i] != NULL; i++ ) { |
|
306 match = strcmp( gKnownCommands[i], command ); |
|
307 if( match == 0 ) { |
|
308 break; |
|
309 } |
|
310 } |
|
311 |
|
312 // If runcmd then set the command line data |
|
313 if( i == 7 ) |
|
314 { |
|
315 if( strlen(iCommandLine) > 0 ) |
|
316 { |
|
317 memset( iCommandLine, 0x00, sizeof(iCommandLine) ); |
|
318 } |
|
319 strcpy( iCommandLine, cline ); |
|
320 } |
|
321 |
|
322 // check that a command was found |
|
323 if( gKnownCommands[i] == NULL ) { |
|
324 fprintf( stderr, "WARNING: unknown command '%s' (ignoring)\n\n", command ); |
|
325 return -1; |
|
326 } |
|
327 |
|
328 // otherwise we have a command -- if startusecase then make sure that it has a uid |
|
329 if( (i == 1) && (token_count == 1) ) { |
|
330 fprintf( stderr, "WARNING: command '%s' requires a use case ID argument\n\n", gKnownCommands[i] ); |
|
331 return -1; |
|
332 } |
|
333 |
|
334 // OK -- return the command |
|
335 return i; |
|
336 } |
|
337 |
|
338 |
|
339 /********************************************************************* |
|
340 * |
|
341 * GetTPCommand() |
|
342 * |
|
343 ********************************************************************/ |
|
344 int CConsolePort::GetTPCommand( int aCommandID, TPCommand* aTPCommand ) |
|
345 { |
|
346 |
|
347 switch (aCommandID) |
|
348 { |
|
349 case 1: // startusecase |
|
350 *aTPCommand = CMD_REQ_STARTUSECASEID; |
|
351 break; |
|
352 |
|
353 case 2: // signal |
|
354 *aTPCommand = CMD_REQ_SIGNALID; |
|
355 break; |
|
356 |
|
357 case 3: // rendezvous |
|
358 *aTPCommand = CMD_REQ_RENDEZVOUSID; |
|
359 break; |
|
360 |
|
361 case 4: // endusecase |
|
362 *aTPCommand = CMD_REQ_ENDUSECASEID; |
|
363 break; |
|
364 |
|
365 case 5: // quit |
|
366 *aTPCommand = CMD_QUITID; |
|
367 break; |
|
368 |
|
369 case 6: // waitforsignal |
|
370 *aTPCommand = CMD_REQ_WAITID; |
|
371 break; |
|
372 |
|
373 case 7: // runcmd |
|
374 *aTPCommand = CMD_REQ_RUNCOMMAND; |
|
375 break; |
|
376 |
|
377 default: |
|
378 return -1; |
|
379 } |
|
380 |
|
381 return 0; |
|
382 } |
|
383 |
|
384 |