2 * Copyright (C) ARM Limited 2011-2014. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include "StreamlineSetup.h"
12 #include "CapturedXML.h"
13 #include "ConfigurationXML.h"
15 #include "EventsXML.h"
17 #include "OlySocket.h"
18 #include "OlyUtility.h"
20 #include "SessionData.h"
22 static const char* TAG_SESSION = "session";
23 static const char* TAG_REQUEST = "request";
24 static const char* TAG_CONFIGURATIONS = "configurations";
26 static const char* ATTR_TYPE = "type";
27 static const char* VALUE_EVENTS = "events";
28 static const char* VALUE_CONFIGURATION = "configuration";
29 static const char* VALUE_COUNTERS = "counters";
30 static const char* VALUE_CAPTURED = "captured";
31 static const char* VALUE_DEFAULTS = "defaults";
33 StreamlineSetup::StreamlineSetup(OlySocket* s) {
40 // Receive commands from Streamline (master)
42 // receive command over socket
43 gSessionData->mWaitingOnCommand = true;
44 data = readCommand(&type);
46 // parse and handle data
48 case COMMAND_REQUEST_XML:
51 case COMMAND_DELIVER_XML:
54 case COMMAND_APC_START:
55 logg->logMessage("Received apc start request");
58 case COMMAND_APC_STOP:
59 logg->logMessage("Received apc stop request before apc start request");
62 case COMMAND_DISCONNECT:
63 logg->logMessage("Received disconnect command");
67 logg->logMessage("Received ping command");
68 sendData(NULL, 0, RESPONSE_ACK);
71 logg->logError(__FILE__, __LINE__, "Target error: Unknown command type, %d", type);
78 if (gSessionData->mCounterOverflow > 0) {
79 logg->logError(__FILE__, __LINE__, "Only %i performance counters are permitted, %i are selected", MAX_PERFORMANCE_COUNTERS, gSessionData->mCounterOverflow);
84 StreamlineSetup::~StreamlineSetup() {
87 char* StreamlineSetup::readCommand(int* command) {
88 unsigned char header[5];
92 // receive type and length
93 response = mSocket->receiveNBytes((char*)&header, sizeof(header));
95 // After receiving a single byte, we are no longer waiting on a command
96 gSessionData->mWaitingOnCommand = false;
99 logg->logError(__FILE__, __LINE__, "Target error: Unexpected socket disconnect");
103 const char type = header[0];
104 const int length = (header[1] << 0) | (header[2] << 8) | (header[3] << 16) | (header[4] << 24);
106 // add artificial limit
107 if ((length < 0) || length > 1024 * 1024) {
108 logg->logError(__FILE__, __LINE__, "Target error: Invalid length received, %d", length);
112 // allocate memory to contain the xml file, size of zero returns a zero size object
113 data = (char*)calloc(length + 1, 1);
115 logg->logError(__FILE__, __LINE__, "Unable to allocate memory for xml");
120 response = mSocket->receiveNBytes(data, length);
122 logg->logError(__FILE__, __LINE__, "Target error: Unexpected socket disconnect");
126 // null terminate the data for string parsing
135 void StreamlineSetup::handleRequest(char* xml) {
136 mxml_node_t *tree, *node;
137 const char * attr = NULL;
139 tree = mxmlLoadString(NULL, xml, MXML_NO_CALLBACK);
140 node = mxmlFindElement(tree, tree, TAG_REQUEST, ATTR_TYPE, NULL, MXML_DESCEND_FIRST);
142 attr = mxmlElementGetAttr(node, ATTR_TYPE);
144 if (attr && strcmp(attr, VALUE_EVENTS) == 0) {
146 logg->logMessage("Sent events xml response");
147 } else if (attr && strcmp(attr, VALUE_CONFIGURATION) == 0) {
149 logg->logMessage("Sent configuration xml response");
150 } else if (attr && strcmp(attr, VALUE_COUNTERS) == 0) {
152 logg->logMessage("Sent counters xml response");
153 } else if (attr && strcmp(attr, VALUE_CAPTURED) == 0) {
154 CapturedXML capturedXML;
155 char* capturedText = capturedXML.getXML(false);
156 sendData(capturedText, strlen(capturedText), RESPONSE_XML);
158 logg->logMessage("Sent captured xml response");
159 } else if (attr && strcmp(attr, VALUE_DEFAULTS) == 0) {
161 logg->logMessage("Sent default configuration xml response");
163 char error[] = "Unknown request";
164 sendData(error, strlen(error), RESPONSE_NAK);
165 logg->logMessage("Received unknown request:\n%s", xml);
171 void StreamlineSetup::handleDeliver(char* xml) {
174 // Determine xml type
175 tree = mxmlLoadString(NULL, xml, MXML_NO_CALLBACK);
176 if (mxmlFindElement(tree, tree, TAG_SESSION, NULL, NULL, MXML_DESCEND_FIRST)) {
178 gSessionData->parseSessionXML(xml);
179 sendData(NULL, 0, RESPONSE_ACK);
180 logg->logMessage("Received session xml");
181 } else if (mxmlFindElement(tree, tree, TAG_CONFIGURATIONS, NULL, NULL, MXML_DESCEND_FIRST)) {
183 writeConfiguration(xml);
184 sendData(NULL, 0, RESPONSE_ACK);
185 logg->logMessage("Received configuration xml");
188 logg->logMessage("Received unknown XML delivery type");
189 sendData(NULL, 0, RESPONSE_NAK);
195 void StreamlineSetup::sendData(const char* data, uint32_t length, char type) {
196 unsigned char header[5];
198 Buffer::writeLEInt(header + 1, length);
199 mSocket->send((char*)&header, sizeof(header));
200 mSocket->send((const char*)data, length);
203 void StreamlineSetup::sendEvents() {
205 char* string = eventsXML.getXML();
206 sendString(string, RESPONSE_XML);
210 void StreamlineSetup::sendConfiguration() {
211 ConfigurationXML xml;
213 const char* string = xml.getConfigurationXML();
214 sendData(string, strlen(string), RESPONSE_XML);
217 void StreamlineSetup::sendDefaults() {
218 // Send the config built into the binary
221 ConfigurationXML::getDefaultConfigurationXml(xml, size);
223 // Artificial size restriction
224 if (size > 1024*1024) {
225 logg->logError(__FILE__, __LINE__, "Corrupt default configuration file");
229 sendData(xml, size, RESPONSE_XML);
232 void StreamlineSetup::sendCounters() {
234 mxml_node_t *counters;
236 xml = mxmlNewXML("1.0");
237 counters = mxmlNewElement(xml, "counters");
239 for (Driver *driver = Driver::getHead(); driver != NULL; driver = driver->getNext()) {
240 count += driver->writeCounters(counters);
244 logg->logError(__FILE__, __LINE__, "No counters found, this could be because /dev/gator/events can not be read or because perf is not working correctly");
248 char* string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
249 sendString(string, RESPONSE_XML);
255 void StreamlineSetup::writeConfiguration(char* xml) {
258 ConfigurationXML::getPath(path);
260 if (util->writeToDisk(path, xml) < 0) {
261 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify write permissions to this path.", path);
265 // Re-populate gSessionData with the configuration, as it has now changed
266 { ConfigurationXML configuration; }
268 if (gSessionData->mCounterOverflow > 0) {
269 logg->logError(__FILE__, __LINE__, "Only %i performance counters are permitted, %i are selected", MAX_PERFORMANCE_COUNTERS, gSessionData->mCounterOverflow);