Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5244f3e06b75fc907d605c53951286febc3a6241 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/********************************************************************************
 * Copyright (c) 2009 Motorola Inc. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Initial Contributor:
 * Otavio Ferranti (Motorola)
 *
 * Contributors:
 * {Name} (company) - description of contribution.
 ********************************************************************************/

package org.eclipse.sequoyah.device.linuxtools.tools.memorymap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.MatchResult;

import org.eclipse.sequoyah.device.common.utilities.logger.ILogger;
import org.eclipse.sequoyah.device.linuxtools.LinuxToolsPlugin;
import org.eclipse.sequoyah.device.linuxtools.network.IConnectionProvider;
import org.eclipse.sequoyah.device.linuxtools.network.IConstants.CommandCode;
import org.eclipse.sequoyah.device.linuxtools.network.IConstants.EventCode;
import org.eclipse.sequoyah.device.linuxtools.tools.AbstractNotifier;
import org.eclipse.sequoyah.device.linuxtools.tools.IListener;
import org.eclipse.sequoyah.device.linuxtools.tools.INotifier;

/**
 * @author Otavio Ferranti
 */
public class MemoryMapProcessor extends AbstractNotifier implements IListener {

	final private String CMD_FETCH_IOMEM = "/proc/iomem"; //$NON-NLS-1$
	final private String PARSE_PATTERN = "\\s*(\\w{8})-(\\w{8})\\s*:\\s*(.*)"; //$NON-NLS-1$

	final private String MSG_EXECUTING_COMMAND =
		Messages.MemoryMapProcessor_Msg_Executing_The_Command;

	final private String MSG_GOT_RESULT =
		Messages.MemoryMapProcessor_Msg_Got_The_Result;
	
	final private int MAX_COLUMNS = 4;
	private IConnectionProvider connectionProvider = null;

	private ILogger logger = null;
	
	/**
	 * The constructor;
	 * @param connectionProvider
	 */
	public MemoryMapProcessor(IConnectionProvider connectionProvider) {
		setConnectionProvider(connectionProvider);
		logger = LinuxToolsPlugin.getLogger();
	}
	
	/**
	 * @throws IOException
	 */
	public void gatherData() throws IOException {
		connectionProvider.setResponseLength(8192);
		connectionProvider.sendCommand(CommandCode.FETCH_FILE, this.CMD_FETCH_IOMEM);
		logger.debug(MSG_EXECUTING_COMMAND + "\n" + this.CMD_FETCH_IOMEM); //$NON-NLS-1$
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.sequoyah.device.linuxtools.network.IListener#notify(org.eclipse.sequoyah.device.linuxtools.network.INotifier, org.eclipse.sequoyah.device.linuxtools.network.IConstants.EventCode, java.lang.Object)
	 */
	public void notify(INotifier notifier,
						EventCode event,
						Object result) {
		if (notifier == this.connectionProvider &&
				event == EventCode.EVT_PROVIDER_SENDCOMMAND_FINISHED) {
			this.connectionProvider.setResponseLength(1024);
			Object[][] parsedResult = parseIomem((StringBuffer) result);
			this.notifyListeners(EventCode.EVT_PROCESSOR_GATHERDATA_FINISHED,
					parsedResult);
		}
	}
	
	/**
	 * @param connectionProvider
	 */
	public void setConnectionProvider (IConnectionProvider connectionProvider) {
		if (null != this.connectionProvider) {
			this.connectionProvider.removeListener(this);
		}
		this.connectionProvider = connectionProvider;
		if (null != this.connectionProvider) {
			this.connectionProvider.addListener(this);
		}
	}

	/**
	 * @param data
	 * @return
	 */
	private Object[][] parseIomem(StringBuffer data) {
		logger.debug(MSG_GOT_RESULT + "\n" + data.toString());
		
		Scanner s1 = new Scanner(data.toString());
		
		ArrayList<String[]> list = new ArrayList<String[]>();
		
		int j = 0;
		
		while (s1.hasNextLine()) {
			Scanner s2 = new Scanner(s1.nextLine());
			s2.findInLine(PARSE_PATTERN);

			String[] entry = null;
			try {
				MatchResult result = s2.match();
				entry = new String[MAX_COLUMNS];
			    for (int i = 1; i <= result.groupCount() && i <= MAX_COLUMNS - 1; i++) {
			    	entry[i-1] = result.group(i);
			    }
			    entry[MAX_COLUMNS - 1] = new Integer(j).toString();
			    j++;
			} catch (IllegalStateException ise) {
				//TODO: Nothing ?
			}

		    s2.close();
		    if (null != entry) {
		    	list.add(entry);
		    }
		}
	    s1.close();
	    
	    String[][] retVal = new String[list.size()][MAX_COLUMNS];
	    for (int i = 0; i < retVal.length; i++) {
	    	retVal[i] = list.get(i);
	    }
		return retVal;
	}
}

Back to the top