gator: Version 5.19
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / EventsXML.cpp
1 /**
2  * Copyright (C) ARM Limited 2013-2014. All rights reserved.
3  *
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.
7  */
8
9 #include "EventsXML.h"
10
11 #include "CapturedXML.h"
12 #include "Logging.h"
13 #include "OlyUtility.h"
14 #include "SessionData.h"
15
16 mxml_node_t *EventsXML::getTree() {
17 #include "events_xml.h" // defines and initializes char events_xml[] and int events_xml_len
18         char path[PATH_MAX];
19         mxml_node_t *xml;
20         FILE *fl;
21
22         // Avoid unused variable warning
23         (void)events_xml_len;
24
25         // Load the provided or default events xml
26         if (gSessionData->mEventsXMLPath) {
27                 strncpy(path, gSessionData->mEventsXMLPath, PATH_MAX);
28         } else {
29                 util->getApplicationFullPath(path, PATH_MAX);
30                 strncat(path, "events.xml", PATH_MAX - strlen(path) - 1);
31         }
32         fl = fopen(path, "r");
33         if (fl) {
34                 xml = mxmlLoadFile(NULL, fl, MXML_NO_CALLBACK);
35                 fclose(fl);
36         } else {
37                 logg->logMessage("Unable to locate events.xml, using default");
38                 xml = mxmlLoadString(NULL, (const char *)events_xml, MXML_NO_CALLBACK);
39         }
40
41         return xml;
42 }
43
44 char *EventsXML::getXML() {
45         mxml_node_t *xml = getTree();
46
47         // Add dynamic events from the drivers
48         mxml_node_t *events = mxmlFindElement(xml, xml, "events", NULL, NULL, MXML_DESCEND);
49         if (!events) {
50                 logg->logMessage("Unable to find <events> node in the events.xml");
51                 handleException();
52         }
53         for (Driver *driver = Driver::getHead(); driver != NULL; driver = driver->getNext()) {
54                 driver->writeEvents(events);
55         }
56
57         char *string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
58         mxmlDelete(xml);
59
60         return string;
61 }
62
63 void EventsXML::write(const char *path) {
64         char file[PATH_MAX];
65
66         // Set full path
67         snprintf(file, PATH_MAX, "%s/events.xml", path);
68
69         char *buf = getXML();
70         if (util->writeToDisk(file, buf) < 0) {
71                 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
72                 handleException();
73         }
74
75         free(buf);
76 }