|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 /* |
|
17 ============================================================================ |
|
18 Name : CommandLineHandler.cpp |
|
19 Author : |
|
20 Version : 1.0 |
|
21 Copyright : Your copyright notice |
|
22 Description : CCommandHandler implementation |
|
23 ============================================================================ |
|
24 */ |
|
25 |
|
26 #include "CommandHandler.h" |
|
27 #include "cenreptestconst.h" |
|
28 |
|
29 |
|
30 void CCommandHandler::ResetAndDestroyPtrArray(TAny* aPtr) |
|
31 { |
|
32 (STATIC_CAST(RPointerArray<HBufC>*, aPtr))->ResetAndDestroy(); |
|
33 (STATIC_CAST(RPointerArray<HBufC>*, aPtr))->Close(); |
|
34 } |
|
35 |
|
36 CCommandHandler::CCommandHandler(CConsoleBase* aConsole) : iConsole(aConsole) |
|
37 { |
|
38 // No implementation required |
|
39 } |
|
40 |
|
41 CCommandHandler::~CCommandHandler() |
|
42 { |
|
43 |
|
44 } |
|
45 |
|
46 CCommandHandler* CCommandHandler::NewLC(CConsoleBase* aConsole) |
|
47 { |
|
48 CCommandHandler* self = new (ELeave) CCommandHandler(aConsole); |
|
49 CleanupStack::PushL(self); |
|
50 self->ConstructL(); |
|
51 return self; |
|
52 } |
|
53 |
|
54 CCommandHandler* CCommandHandler::NewL(CConsoleBase* aConsole) |
|
55 { |
|
56 CCommandHandler* self = CCommandHandler::NewLC(aConsole); |
|
57 CleanupStack::Pop(); // self; |
|
58 return self; |
|
59 } |
|
60 |
|
61 void CCommandHandler::ConstructL() |
|
62 { |
|
63 |
|
64 } |
|
65 |
|
66 void CCommandHandler::HandleCommandL(const RArray<TPtrC>& aArgs) |
|
67 { |
|
68 |
|
69 RArray<TPtrC> cmdArgs; |
|
70 CleanupClosePushL(cmdArgs); |
|
71 |
|
72 if( aArgs.Count() < 2 ) |
|
73 { |
|
74 HandleCommandError(EInvalidCmdArgs); |
|
75 User::Leave(KErrArgument); |
|
76 } |
|
77 |
|
78 //copy command arguments |
|
79 for (int index = 2; index < aArgs.Count(); ++index) |
|
80 { |
|
81 cmdArgs.Append(aArgs[index]); |
|
82 } |
|
83 |
|
84 RBuf command; |
|
85 CleanupClosePushL(command); |
|
86 command.CreateL(aArgs[0]); |
|
87 command.LowerCase(); |
|
88 if(command.Find(KCmdIndicator) == 0) |
|
89 { |
|
90 if(command.Mid(1).Match(KCmdMode()) == 0) |
|
91 { |
|
92 RBuf mode; |
|
93 CleanupClosePushL(mode); |
|
94 mode.CreateL(aArgs[1]); |
|
95 mode.LowerCase(); |
|
96 if(mode.Match(KModeCreate) == 0) |
|
97 { |
|
98 iConsole->Write(_L("Executing mode : create")); |
|
99 iConsole->Write(_L("\n")); |
|
100 //create setting in central repositiory |
|
101 CreateSettingInRepositoryL(cmdArgs); |
|
102 } |
|
103 else if(mode.Match(KModeValidate) == 0) |
|
104 { |
|
105 iConsole->Write(_L("Executing mode : validate")); |
|
106 iConsole->Write(_L("\n")); |
|
107 //validate setting in cenrep |
|
108 ValidateSettingInRepositoryL(cmdArgs); |
|
109 } |
|
110 else |
|
111 { |
|
112 //Repote error unknown mode |
|
113 HandleCommandError(EInvalidMode); |
|
114 } |
|
115 CleanupStack::PopAndDestroy(&mode); |
|
116 } |
|
117 else |
|
118 { |
|
119 // report error invalid command |
|
120 HandleCommandError(EInvalidCmdArgs); |
|
121 } |
|
122 } |
|
123 else |
|
124 { |
|
125 // report error missing command |
|
126 HandleCommandError(EInvalidCmdArgs); |
|
127 } |
|
128 CleanupStack::PopAndDestroy(2); |
|
129 } |
|
130 |
|
131 void CCommandHandler::HandleCommandError(TInt aError) |
|
132 { |
|
133 switch(aError) |
|
134 { |
|
135 case EInvalidMode: |
|
136 case EInvalidCmdArgs: |
|
137 PrintUsage(); |
|
138 break; |
|
139 } |
|
140 } |
|
141 |
|
142 TUint CCommandHandler::HexDesToUint(const TPtrC& aDes) |
|
143 { |
|
144 TLex lex; |
|
145 lex.Assign(aDes); |
|
146 TUint intVal; |
|
147 lex.Val(intVal, EHex); |
|
148 return intVal; |
|
149 } |
|
150 |
|
151 TInt CCommandHandler::DesToInt(const TPtrC & aDes) |
|
152 { |
|
153 TLex lex; |
|
154 lex.Assign(aDes); |
|
155 TInt realVal; |
|
156 lex.Val(realVal); |
|
157 return realVal; |
|
158 } |
|
159 |
|
160 TReal CCommandHandler::DesToReal(const TPtrC & aDes) |
|
161 { |
|
162 TLex lex; |
|
163 lex.Assign(aDes); |
|
164 TReal realVal; |
|
165 lex.Val(realVal); |
|
166 return realVal; |
|
167 } |
|
168 |
|
169 void CCommandHandler::CreateSettingInRepositoryL(const RArray<TPtrC> & aCmdArgs) |
|
170 { |
|
171 //Create repository object and set setting |
|
172 TPtrC repositoryUid = aCmdArgs[0]; // repository UID in hex format |
|
173 TPtrC settingId = aCmdArgs[1]; // setting UID in hex format |
|
174 TPtrC settingType = aCmdArgs[2]; // setting data format |
|
175 TPtrC settingVal = aCmdArgs[3]; // setting value |
|
176 |
|
177 //print info message |
|
178 iConsole->Write(_L("Reading repository ")); |
|
179 iConsole->Write(repositoryUid); |
|
180 |
|
181 CRepository *repository = CRepository::NewL(HexDesToUid(repositoryUid)); |
|
182 CleanupStack::PushL(repository); |
|
183 if(repository != NULL) |
|
184 { |
|
185 iConsole->Write(_L(" ...repository exists \n")); |
|
186 //Set setting here |
|
187 TInt err = repository->Create(HexDesToUint(settingId), DesToInt(settingVal)); |
|
188 if(KErrAlreadyExists == err) |
|
189 { |
|
190 User::LeaveIfError(repository->Delete(HexDesToUint(settingId))); |
|
191 err = repository->Create(HexDesToUint(settingId), DesToInt(settingVal)); |
|
192 } |
|
193 if(KErrNone == err) |
|
194 { |
|
195 iConsole->Write(_L("Setting created, Id : ")); |
|
196 iConsole->Write(settingId); |
|
197 } |
|
198 else |
|
199 { |
|
200 User::Leave(err); |
|
201 } |
|
202 } |
|
203 CleanupStack::PopAndDestroy(repository); |
|
204 } |
|
205 |
|
206 void CCommandHandler::ValidateSettingL(CRepository *& repository, TPtrC settingId, TPtrC settingType, TPtrC settingVal) |
|
207 { |
|
208 |
|
209 iConsole->Write(_L(" ...repository exists \n")); |
|
210 |
|
211 iConsole->Write(_L("Reading setting, Id : ")); |
|
212 iConsole->Write(settingId); |
|
213 |
|
214 if(settingType.Match(KTypeInt) == 0) |
|
215 { |
|
216 ValidateIntValueL(repository, settingId, settingVal); |
|
217 } |
|
218 else if(settingType.Match(KTypeReal) == 0) |
|
219 { |
|
220 ValidateRealValueL(repository, settingId, settingVal); |
|
221 } |
|
222 else if(settingType.Match(KTypeString) == 0) |
|
223 { |
|
224 ValidateStringValueL(repository, settingId, settingVal); |
|
225 } |
|
226 else if(settingType.Match(KTypeString8) == 0) |
|
227 { |
|
228 ValidateString8ValueL(repository, settingId, settingVal); |
|
229 } |
|
230 } |
|
231 |
|
232 void CCommandHandler::ValidateIntValueL(CRepository *& repository, TPtrC settingId, TPtrC settingVal) |
|
233 { |
|
234 TInt readVal; |
|
235 User::LeaveIfError(repository->Get(DesToInt(settingId), readVal)); |
|
236 if(readVal == HexDesToUint(settingVal)) |
|
237 { |
|
238 iConsole->Write(_L(" ...setting exists")); |
|
239 } |
|
240 else |
|
241 { |
|
242 User::Leave(KErrCorrupt); |
|
243 } |
|
244 } |
|
245 |
|
246 void CCommandHandler::ValidateRealValueL(CRepository *& repository, TPtrC settingId, TPtrC settingVal) |
|
247 { |
|
248 TReal readVal; |
|
249 User::LeaveIfError(repository->Get(DesToInt(settingId), readVal)); |
|
250 if(readVal == DesToReal(settingVal)) |
|
251 { |
|
252 iConsole->Write(_L(" ...setting exists")); |
|
253 } |
|
254 else |
|
255 { |
|
256 User::Leave(KErrCorrupt); |
|
257 } |
|
258 } |
|
259 |
|
260 void CCommandHandler::ValidateStringValueL(CRepository *& repository, TPtrC settingId, TPtrC settingVal) |
|
261 { |
|
262 TBuf<1024> string; |
|
263 User::LeaveIfError(repository->Get(DesToInt(settingId), string)); |
|
264 if(string.Match(settingVal) == 0) |
|
265 { |
|
266 iConsole->Write(_L(" ...setting exists")); |
|
267 } |
|
268 else |
|
269 { |
|
270 User::Leave(KErrCorrupt); |
|
271 } |
|
272 } |
|
273 |
|
274 void CCommandHandler::ValidateString8ValueL(CRepository *& repository, TPtrC settingId, TPtrC settingVal) |
|
275 { |
|
276 TBuf8<1024> buf; |
|
277 User::LeaveIfError(repository->Get(DesToInt(settingId), buf)); |
|
278 TBuf<1024> string; |
|
279 string.Copy(buf); |
|
280 if(string.Match(settingVal) == 0) |
|
281 { |
|
282 iConsole->Write(_L(" ...setting exists")); |
|
283 } |
|
284 else |
|
285 { |
|
286 User::Leave(KErrCorrupt); |
|
287 } |
|
288 } |
|
289 |
|
290 void CCommandHandler::ValidateSettingInRepositoryL(const RArray<TPtrC> & aCmdArgs) |
|
291 { |
|
292 //Create repository object and validate setting. |
|
293 TPtrC repositoryUid = aCmdArgs[0]; // repository UID in hex format |
|
294 TPtrC settingId = aCmdArgs[1]; // setting UID in hex format |
|
295 TPtrC settingType = aCmdArgs[2]; // setting data format |
|
296 TPtrC settingVal = aCmdArgs[3]; |
|
297 |
|
298 //print info message |
|
299 iConsole->Write(_L("Reading repository ")); |
|
300 iConsole->Write(repositoryUid); |
|
301 |
|
302 CRepository *repository = CRepository::NewL(HexDesToUid(repositoryUid)); |
|
303 CleanupStack::PushL(repository); |
|
304 if(repository != NULL) |
|
305 { |
|
306 ValidateSettingL(repository, settingId, settingType, settingVal); |
|
307 } |
|
308 CleanupStack::PopAndDestroy(repository); |
|
309 } |
|
310 |
|
311 TUid CCommandHandler::HexDesToUid(const TPtrC & aDes) |
|
312 { |
|
313 return TUid::Uid(HexDesToUint(aDes)); |
|
314 } |
|
315 |
|
316 void CCommandHandler::PrintUsage() |
|
317 { |
|
318 iConsole->Write(_L("Invalid command or mode switch.\n")); |
|
319 iConsole->Write(_L("Usage:\n")); |
|
320 iConsole->Write(_L("CenrepTest -m <create|validate> <Repository Id> <Setting Id> <Setting Type> <Setting Value>\n")); |
|
321 } |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |