|
1 /* |
|
2 * Copyright (c) 2007-2010 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 * |
|
16 * Access point to TraceViewer API functions |
|
17 * |
|
18 */ |
|
19 package com.nokia.traceviewer.api; |
|
20 |
|
21 import java.util.ArrayList; |
|
22 |
|
23 import com.nokia.traceviewer.engine.TraceInformation; |
|
24 import com.nokia.traceviewer.engine.TraceMetaData; |
|
25 import com.nokia.traceviewer.engine.TraceViewerGlobals; |
|
26 import com.nokia.traceviewer.engine.activation.TraceActivationComponentItem; |
|
27 import com.nokia.traceviewer.engine.dataprocessor.DataProcessor; |
|
28 |
|
29 /** |
|
30 * Access point to TraceViewer API functions |
|
31 */ |
|
32 public final class TraceViewerAPI { |
|
33 |
|
34 /** |
|
35 * Enum indicating the error codes of TraceViewer API operations |
|
36 */ |
|
37 public enum TVAPIError { |
|
38 |
|
39 /** |
|
40 * No error, operation succeeded. |
|
41 */ |
|
42 NONE, |
|
43 |
|
44 /** |
|
45 * Not connected error. Is given when trying to operate with the |
|
46 * connection when it is not open. |
|
47 */ |
|
48 NOT_CONNECTED, |
|
49 |
|
50 /** |
|
51 * Disconnecting failed error. |
|
52 */ |
|
53 DISCONNECTING_FAILED, |
|
54 |
|
55 /** |
|
56 * Invalid connection parameters error. Parameters are not what they |
|
57 * should be for this connection method. |
|
58 */ |
|
59 INVALID_CONNECTION_PARAMETERS, |
|
60 |
|
61 /** |
|
62 * Already connected error. Connection is already open. |
|
63 */ |
|
64 ALREADY_CONNECTED, |
|
65 |
|
66 /** |
|
67 * Cannot open the connection port |
|
68 */ |
|
69 CANNOT_OPEN, |
|
70 |
|
71 /** |
|
72 * Connection port already open |
|
73 */ |
|
74 ALREADY_OPEN, |
|
75 |
|
76 /** |
|
77 * Invalid connection handle |
|
78 */ |
|
79 INVALID_CONNECTION_HANDLE, |
|
80 |
|
81 /** |
|
82 * Timeout when connecting |
|
83 */ |
|
84 TIMEOUT, |
|
85 |
|
86 /** |
|
87 * Tracefile is already open |
|
88 */ |
|
89 TRACEFILE_ALREADY_OPEN, |
|
90 |
|
91 /** |
|
92 * Tracefile doesn't exist |
|
93 */ |
|
94 TRACEFILE_DOESNT_EXIST, |
|
95 |
|
96 /** |
|
97 * No tracefile open where to write |
|
98 */ |
|
99 NO_TRACEFILE_OPEN, |
|
100 |
|
101 /** |
|
102 * Cannot write to the tracefile |
|
103 */ |
|
104 CANNOT_WRITE_TO_TRACEFILE, |
|
105 |
|
106 /** |
|
107 * Media seems not to be open |
|
108 */ |
|
109 MEDIA_NOT_OPEN, |
|
110 |
|
111 /** |
|
112 * Invalid connection settings |
|
113 */ |
|
114 INVALID_CONNECTION_SETTINGS, |
|
115 |
|
116 /** |
|
117 * Cannot find trace router application |
|
118 */ |
|
119 CANNOT_FIND_TRACEROUTER, |
|
120 |
|
121 /** |
|
122 * Cannot create trace router process |
|
123 */ |
|
124 CANNOT_CREATE_TRACEROUTER, |
|
125 |
|
126 /** |
|
127 * Media busy |
|
128 */ |
|
129 MEDIA_BUSY, |
|
130 |
|
131 /** |
|
132 * User canceled connection |
|
133 */ |
|
134 USER_CANCELED, |
|
135 |
|
136 /** |
|
137 * Unknown error |
|
138 */ |
|
139 UNKNOWN_ERROR, |
|
140 |
|
141 /** |
|
142 * DataProcessor already added error. DataProcessor is already in the |
|
143 * DataProcessor list. |
|
144 */ |
|
145 DATAPROCESSOR_ALREADY_ADDED, |
|
146 |
|
147 /** |
|
148 * DataProcessor could not be found error. DataProcessor was not in the |
|
149 * DataProcessor list. |
|
150 */ |
|
151 DATAPROCESSOR_NOT_FOUND, |
|
152 |
|
153 /** |
|
154 * Decode Provider plugin is missing |
|
155 */ |
|
156 DECODE_PROVIDER_PLUGIN_MISSING, |
|
157 |
|
158 /** |
|
159 * No Decode files loaded error. |
|
160 */ |
|
161 DECODE_FILE_NOT_LOADED, |
|
162 |
|
163 /** |
|
164 * Some groups couldn't be found from the decode model. They are |
|
165 * propably missing from the loaded decode files. |
|
166 */ |
|
167 SOME_GROUPS_NOT_FOUND_FROM_DECODE_MODEL, |
|
168 |
|
169 /** |
|
170 * File doesn't exist |
|
171 */ |
|
172 FILE_DOES_NOT_EXIST, |
|
173 |
|
174 /** |
|
175 * TraceViewer view not open |
|
176 */ |
|
177 TRACE_VIEW_NOT_OPEN; |
|
178 } |
|
179 |
|
180 /** |
|
181 * TCP / IP connection method value |
|
182 */ |
|
183 public static final int TVAPI_CONNECTION_TCP = 1; |
|
184 |
|
185 /** |
|
186 * USB Serial connection method value |
|
187 */ |
|
188 public static final int TVAPI_CONNECTION_USB_SERIAL = 2; |
|
189 |
|
190 /** |
|
191 * DataProcessor adder object |
|
192 */ |
|
193 private static DataProcessorAdder dpAdder; |
|
194 |
|
195 /** |
|
196 * Data sender object |
|
197 */ |
|
198 private static DataSender dataSender; |
|
199 |
|
200 /** |
|
201 * Connection handler object |
|
202 */ |
|
203 private static ConnectionHandler connectionHandler; |
|
204 |
|
205 /** |
|
206 * Decode handler object |
|
207 */ |
|
208 private static DecodeHandler decodeHandler; |
|
209 |
|
210 /** |
|
211 * View handler object |
|
212 */ |
|
213 private static ViewHandler viewHandler; |
|
214 |
|
215 /** |
|
216 * Log handler object |
|
217 */ |
|
218 private static LogHandler logHandler; |
|
219 |
|
220 /** |
|
221 * Activates traces using group ID's |
|
222 * |
|
223 * @param componentId |
|
224 * component ID |
|
225 * @param groupIds |
|
226 * array of group IDs |
|
227 * @param activate |
|
228 * if true, activate. If false, deactivate. |
|
229 * @return error code from TraceViewerAPI |
|
230 */ |
|
231 public static TVAPIError activateTraces(int componentId, int[] groupIds, |
|
232 boolean activate) { |
|
233 |
|
234 // Create the sender if it doesn't exist |
|
235 if (dataSender == null) { |
|
236 dataSender = new DataSender(); |
|
237 } |
|
238 |
|
239 // Send the data and check error status |
|
240 TVAPIError errorCode = dataSender.activateTraces(componentId, groupIds, |
|
241 activate); |
|
242 |
|
243 return errorCode; |
|
244 |
|
245 } |
|
246 |
|
247 /** |
|
248 * Activates traces using group names |
|
249 * |
|
250 * @param componentId |
|
251 * component ID |
|
252 * @param groupNames |
|
253 * array of group names |
|
254 * @param activate |
|
255 * if true, activate. If false, deactivate. |
|
256 * @return error code from TraceViewerAPI |
|
257 */ |
|
258 public static TVAPIError activateTraces(int componentId, |
|
259 String[] groupNames, boolean activate) { |
|
260 |
|
261 // Create the sender if it doesn't exist |
|
262 if (dataSender == null) { |
|
263 dataSender = new DataSender(); |
|
264 } |
|
265 |
|
266 // Send the data and check error status |
|
267 TVAPIError errorCode = dataSender.activateTraces(componentId, |
|
268 groupNames, activate); |
|
269 |
|
270 return errorCode; |
|
271 |
|
272 } |
|
273 |
|
274 /** |
|
275 * Adds own DataProcessor to a specific location in dataprocessor list. |
|
276 * DataProcessors registered through this method will receive all traces |
|
277 * that are coming from the device. |
|
278 * |
|
279 * @param dataProcessor |
|
280 * the dataprocessor to be added |
|
281 * @param location |
|
282 * location in the list |
|
283 * @param priority |
|
284 * priority of the dataprocessor. If two dataprocessors are in |
|
285 * the same location in the dataprocessor list, the one with |
|
286 * higher priority will be run first |
|
287 * @return error code from TraceViewerAPI |
|
288 */ |
|
289 public static TVAPIError addDataProcessor(DataProcessor dataProcessor, |
|
290 DPLocation location, int priority) { |
|
291 |
|
292 // Create the adder if it doesn't exist |
|
293 if (dpAdder == null) { |
|
294 dpAdder = new DataProcessorAdder(); |
|
295 } |
|
296 |
|
297 // Add the processor and check error status |
|
298 TVAPIError errorCode = dpAdder.addDataProcessor(dataProcessor, |
|
299 location, priority); |
|
300 |
|
301 return errorCode; |
|
302 } |
|
303 |
|
304 /** |
|
305 * Connect using current settings defined in TraceViewer's preferences |
|
306 * |
|
307 * @return error code from TraceViewerAPI |
|
308 */ |
|
309 public static TVAPIError connect() { |
|
310 |
|
311 // Create the connection handler if it doesn't exist |
|
312 if (connectionHandler == null) { |
|
313 connectionHandler = new ConnectionHandler(); |
|
314 } |
|
315 |
|
316 // Try to connect |
|
317 TVAPIError errorCode = connectionHandler.connect(); |
|
318 |
|
319 return errorCode; |
|
320 } |
|
321 |
|
322 /** |
|
323 * Connect using given parameters. Possible connection methods can be found |
|
324 * from TraceViewerAPI constants. Parameters should be given in this order: |
|
325 * When using TCP connection, parameters are IP address port number and |
|
326 * channel number in this order. When using USB serial connection, the only |
|
327 * parameter is the COM port number. This function will also set given |
|
328 * parameters to TraceViewer's connection preferences if the connecting |
|
329 * succeeds. |
|
330 * |
|
331 * @param connectionMethod |
|
332 * the connection method to use |
|
333 * @param parameters |
|
334 * array of parameters |
|
335 * @return error code from TraceViewerAPI |
|
336 */ |
|
337 public static TVAPIError connect(int connectionMethod, String[] parameters) { |
|
338 |
|
339 // Create the connection handler if it doesn't exist |
|
340 if (connectionHandler == null) { |
|
341 connectionHandler = new ConnectionHandler(); |
|
342 } |
|
343 |
|
344 // Try to connect |
|
345 TVAPIError errorCode = connectionHandler.connect(connectionMethod, |
|
346 parameters); |
|
347 |
|
348 return errorCode; |
|
349 } |
|
350 |
|
351 /** |
|
352 * Disconnects from the target |
|
353 * |
|
354 * @return error code from TraceViewerAPI |
|
355 */ |
|
356 public static TVAPIError disconnect() { |
|
357 |
|
358 // Create the connection handler if it doesn't exist |
|
359 if (connectionHandler == null) { |
|
360 connectionHandler = new ConnectionHandler(); |
|
361 } |
|
362 |
|
363 // Try to disconnect |
|
364 TVAPIError errorCode = connectionHandler.disconnect(); |
|
365 |
|
366 return errorCode; |
|
367 } |
|
368 |
|
369 /** |
|
370 * Gets components from all loaded Dictionaries |
|
371 * |
|
372 * @return list of components from all loaded Dictionaries. List can be |
|
373 * empty. |
|
374 */ |
|
375 public static ArrayList<TraceActivationComponentItem> getDictionaryComponents() { |
|
376 |
|
377 // Create the decode handler if it doesn't exist |
|
378 if (decodeHandler == null) { |
|
379 decodeHandler = new DecodeHandler(); |
|
380 } |
|
381 |
|
382 // Get the component list |
|
383 ArrayList<TraceActivationComponentItem> components = decodeHandler |
|
384 .getDictionaryComponents(); |
|
385 |
|
386 return components; |
|
387 } |
|
388 |
|
389 /** |
|
390 * Gets trace component name from the Decode model with a component ID |
|
391 * |
|
392 * @param componentId |
|
393 * component ID |
|
394 * @return component name or null if not found or no Decode files are loaded |
|
395 */ |
|
396 public static String getTraceComponentName(int componentId) { |
|
397 |
|
398 // Create the decode handler if it doesn't exist |
|
399 if (decodeHandler == null) { |
|
400 decodeHandler = new DecodeHandler(); |
|
401 } |
|
402 |
|
403 // Get the component name |
|
404 String componentName = decodeHandler.getTraceComponentName(componentId); |
|
405 |
|
406 return componentName; |
|
407 } |
|
408 |
|
409 /** |
|
410 * Gets trace group ID from the Decode model with a group name |
|
411 * |
|
412 * @param componentId |
|
413 * component ID |
|
414 * @param groupName |
|
415 * the group name |
|
416 * @return trace group ID or -1 if group ID not found or -2 if no Decode |
|
417 * files are loaded |
|
418 */ |
|
419 public static int getTraceGroupId(int componentId, String groupName) { |
|
420 |
|
421 // Create the decode handler if it doesn't exist |
|
422 if (decodeHandler == null) { |
|
423 decodeHandler = new DecodeHandler(); |
|
424 } |
|
425 |
|
426 // Get the group ID |
|
427 int groupId = decodeHandler.getTraceGroupId(componentId, groupName); |
|
428 |
|
429 return groupId; |
|
430 } |
|
431 |
|
432 /** |
|
433 * Gets trace group name from the Decode model with a component and group |
|
434 * IDs |
|
435 * |
|
436 * @param componentId |
|
437 * component ID |
|
438 * @param groupId |
|
439 * group ID |
|
440 * @return group name or null if not found or no Decode files are loaded |
|
441 */ |
|
442 public static String getTraceGroupName(int componentId, int groupId) { |
|
443 |
|
444 // Create the decode handler if it doesn't exist |
|
445 if (decodeHandler == null) { |
|
446 decodeHandler = new DecodeHandler(); |
|
447 } |
|
448 |
|
449 // Get the group name |
|
450 String groupName = decodeHandler |
|
451 .getTraceGroupName(componentId, groupId); |
|
452 |
|
453 return groupName; |
|
454 } |
|
455 |
|
456 /** |
|
457 * Gets trace name from the Decode model with a component, group and trace |
|
458 * IDs |
|
459 * |
|
460 * @param componentId |
|
461 * component ID |
|
462 * @param groupId |
|
463 * group ID |
|
464 * @param traceId |
|
465 * trace ID |
|
466 * @return trace name or null if not found or no Decode files are loaded |
|
467 */ |
|
468 public static String getTraceName(int componentId, int groupId, int traceId) { |
|
469 |
|
470 // Create the decode handler if it doesn't exist |
|
471 if (decodeHandler == null) { |
|
472 decodeHandler = new DecodeHandler(); |
|
473 } |
|
474 |
|
475 // Get the trace name |
|
476 String traceName = decodeHandler.getTraceName(componentId, groupId, |
|
477 traceId); |
|
478 |
|
479 return traceName; |
|
480 } |
|
481 |
|
482 /** |
|
483 * Gets trace metadata |
|
484 * |
|
485 * @param information |
|
486 * TraceInformation to be used when finding correct metadata |
|
487 * @return trace metadata or null if nothing is found |
|
488 */ |
|
489 public static TraceMetaData getTraceMetaData(TraceInformation information) { |
|
490 TraceMetaData metaData = null; |
|
491 if (information != null && information.isDefined() |
|
492 && TraceViewerGlobals.getDecodeProvider() != null) { |
|
493 metaData = TraceViewerGlobals.getDecodeProvider().getTraceMetaData( |
|
494 information); |
|
495 } |
|
496 |
|
497 return metaData; |
|
498 } |
|
499 |
|
500 /** |
|
501 * Loads decode file to the decode model. User must remember that loading |
|
502 * the decode file can take some time and this function will block until the |
|
503 * model is loaded or a maximum of 5 seconds. |
|
504 * |
|
505 * @param decodeFilePath |
|
506 * absolute path to the decode file. Path must be in correct |
|
507 * format for any operating system (Windows, Linux) |
|
508 * @param deleteExistingModel |
|
509 * if true, old decode model is removed before this decode file |
|
510 * is loaded |
|
511 * @return error code from TraceViewerAPI |
|
512 */ |
|
513 public static TVAPIError loadDecodeFile(String decodeFilePath, |
|
514 boolean deleteExistingModel) { |
|
515 |
|
516 // Create the decode handler if it doesn't exist |
|
517 if (decodeHandler == null) { |
|
518 decodeHandler = new DecodeHandler(); |
|
519 } |
|
520 |
|
521 // Load the decode file |
|
522 TVAPIError errorCode = decodeHandler.loadDecodeFile(decodeFilePath, |
|
523 deleteExistingModel); |
|
524 |
|
525 return errorCode; |
|
526 } |
|
527 |
|
528 /** |
|
529 * Opens log file to TraceViewer. If filePath is null and TraceViewer view |
|
530 * is not open, error will be returned. |
|
531 * |
|
532 * @param filePath |
|
533 * file path to open. If file path ends with .txt, file is opened |
|
534 * as ASCII log. If filePath is null and TraceViewer view is |
|
535 * open, a file selection dialog is opened. |
|
536 * @return error code from TraceViewerAPI |
|
537 */ |
|
538 public static TVAPIError openLogFile(String filePath) { |
|
539 |
|
540 // Create the handler if it doesn't exist |
|
541 if (logHandler == null) { |
|
542 logHandler = new LogHandler(); |
|
543 } |
|
544 |
|
545 // Save current traces to a log file |
|
546 TVAPIError errorCode = logHandler.openLogFile(filePath); |
|
547 |
|
548 return errorCode; |
|
549 } |
|
550 |
|
551 /** |
|
552 * Removes this DataProcessor from the list of DataProcessors. |
|
553 * |
|
554 * @param dataProcessor |
|
555 * the DataProcessor to be removed |
|
556 * @return error code from TraceViewerAPI |
|
557 */ |
|
558 public static TVAPIError removeDataProcessor(DataProcessor dataProcessor) { |
|
559 |
|
560 // Create the adder if it doesn't exist |
|
561 if (dpAdder == null) { |
|
562 dpAdder = new DataProcessorAdder(); |
|
563 } |
|
564 |
|
565 // Remove the Data Processor |
|
566 TVAPIError errorCode = dpAdder.removeDataProcessor(dataProcessor); |
|
567 |
|
568 return errorCode; |
|
569 } |
|
570 |
|
571 /** |
|
572 * Saves current traces shown in TraceViewer view to a Binary log. If |
|
573 * TraceViewer view is visible, a progress bar about the saving will be |
|
574 * shown for the user. If TraceViewer view is not visible, saving the file |
|
575 * will be done with the calling thread. Note that saving the file can take |
|
576 * a long time! |
|
577 * |
|
578 * @param filePath |
|
579 * file path where to save the log. If null and TraceViewer view |
|
580 * is visible, a file selection dialog is shown |
|
581 * @return error code from TraceViewerAPI |
|
582 */ |
|
583 public static TVAPIError saveCurrentTracesToLog(String filePath) { |
|
584 |
|
585 // Create the handler if it doesn't exist |
|
586 if (logHandler == null) { |
|
587 logHandler = new LogHandler(); |
|
588 } |
|
589 |
|
590 // Save current traces to a log file |
|
591 TVAPIError errorCode = logHandler.saveCurrentTracesToLog(filePath); |
|
592 |
|
593 return errorCode; |
|
594 } |
|
595 |
|
596 /** |
|
597 * Sends raw data to the device with header |
|
598 * |
|
599 * @param msg |
|
600 * byte array to be sent |
|
601 * @param changeMediaType |
|
602 * If true, changes media type to the header when sending the |
|
603 * message. Otherwise leaves the message as it is. |
|
604 * @return error code from TraceViewerAPI |
|
605 */ |
|
606 public static TVAPIError sendDataToDeviceWithHeader(byte[] msg, |
|
607 boolean changeMediaType) { |
|
608 |
|
609 // Create the sender if it doesn't exist |
|
610 if (dataSender == null) { |
|
611 dataSender = new DataSender(); |
|
612 } |
|
613 |
|
614 // Send the data and check error status |
|
615 byte b = 0x00; |
|
616 TVAPIError errorCode = dataSender.sendRawData(msg, false, b, |
|
617 changeMediaType); |
|
618 |
|
619 return errorCode; |
|
620 } |
|
621 |
|
622 /** |
|
623 * Sends raw data to the device without header |
|
624 * |
|
625 * @param msg |
|
626 * byte array to be sent |
|
627 * @param messageId |
|
628 * messageID to add to the header. Might not be needed, depends |
|
629 * on the protocol. |
|
630 * @return error code from TraceViewerAPI |
|
631 */ |
|
632 public static TVAPIError sendDataToDeviceWithoutHeader(byte[] msg, |
|
633 byte messageId) { |
|
634 |
|
635 // Create the sender if it doesn't exist |
|
636 if (dataSender == null) { |
|
637 dataSender = new DataSender(); |
|
638 } |
|
639 |
|
640 // Send the data and check error status |
|
641 TVAPIError errorCode = dataSender.sendRawData(msg, true, messageId, |
|
642 false); |
|
643 |
|
644 return errorCode; |
|
645 } |
|
646 |
|
647 /** |
|
648 * Stops or restarts the TraceViewer view update |
|
649 * |
|
650 * @param stop |
|
651 * if true, stops the view update. If false, restarts the update. |
|
652 */ |
|
653 public static void stopViewUpdate(boolean stop) { |
|
654 |
|
655 // Create the handler if it doesn't exist |
|
656 if (viewHandler == null) { |
|
657 viewHandler = new ViewHandler(); |
|
658 } |
|
659 |
|
660 // Ask handler to stop / restart view update |
|
661 viewHandler.stopViewUpdate(stop); |
|
662 } |
|
663 |
|
664 /** |
|
665 * Syncs to timestamp in the TraceViewer view. If both start and end |
|
666 * timestamps are given, the range is selected. This function assumes that |
|
667 * the traces in the TraceViewer view are in chronological order. Also, |
|
668 * endTimestamp must always be "bigger" than startTimestamp |
|
669 * |
|
670 * @param startTimestamp |
|
671 * start timestamp in the format of hh:mm:ss.SSS |
|
672 * @param endTimestamp |
|
673 * end timestamp in the format of hh:mm:ss.SSS or null if only |
|
674 * start timestamp is searched for |
|
675 * @return error code from TraceViewerAPI |
|
676 */ |
|
677 public static TVAPIError syncToTimestamp(String startTimestamp, |
|
678 String endTimestamp) { |
|
679 |
|
680 // Create the handler if it doesn't exist |
|
681 if (viewHandler == null) { |
|
682 viewHandler = new ViewHandler(); |
|
683 } |
|
684 |
|
685 // Try to sync and check error status |
|
686 TVAPIError errorCode = viewHandler.syncToTimestamp(startTimestamp, |
|
687 endTimestamp); |
|
688 |
|
689 return errorCode; |
|
690 } |
|
691 |
|
692 /** |
|
693 * Syncs to trace in the TraceViewer view |
|
694 * |
|
695 * @param startTrace |
|
696 * start trace number |
|
697 * @param endTrace |
|
698 * end trace number of 0 if only start trace is searched for |
|
699 * @return error code from TraceViewerAPI |
|
700 */ |
|
701 public static TVAPIError syncToTrace(int startTrace, int endTrace) { |
|
702 |
|
703 // Create the handler if it doesn't exist |
|
704 if (viewHandler == null) { |
|
705 viewHandler = new ViewHandler(); |
|
706 } |
|
707 |
|
708 // Try to sync and check error status |
|
709 TVAPIError errorCode = viewHandler.syncToTrace(startTrace, endTrace); |
|
710 |
|
711 return errorCode; |
|
712 } |
|
713 } |