gator: Version 5.19
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / SessionXML.cpp
1 /**
2  * Copyright (C) ARM Limited 2010-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 "SessionXML.h"
10
11 #include <string.h>
12 #include <stdlib.h>
13 #include <limits.h>
14
15 #include "Logging.h"
16 #include "OlyUtility.h"
17 #include "SessionData.h"
18
19 static const char*      TAG_SESSION = "session";
20 static const char*      TAG_IMAGE   = "image";
21
22 static const char*      ATTR_VERSION            = "version";
23 static const char*      ATTR_CALL_STACK_UNWINDING = "call_stack_unwinding";
24 static const char*      ATTR_BUFFER_MODE        = "buffer_mode";
25 static const char*      ATTR_SAMPLE_RATE        = "sample_rate";
26 static const char*      ATTR_DURATION           = "duration";
27 static const char*      ATTR_PATH               = "path";
28 static const char*      ATTR_LIVE_RATE          = "live_rate";
29
30 SessionXML::SessionXML(const char *str) {
31         parameters.buffer_mode[0] = 0;
32         parameters.sample_rate[0] = 0;
33         parameters.duration = 0;
34         parameters.call_stack_unwinding = false;
35         parameters.live_rate = 0;
36         parameters.images = NULL;
37         mPath = 0;
38         mSessionXML = (const char *)str;
39         logg->logMessage(mSessionXML);
40 }
41
42 SessionXML::~SessionXML() {
43         if (mPath != 0) {
44                 free((char *)mSessionXML);
45         }
46 }
47
48 void SessionXML::parse() {
49         mxml_node_t *tree;
50         mxml_node_t *node;
51
52         tree = mxmlLoadString(NULL, mSessionXML, MXML_NO_CALLBACK);
53         node = mxmlFindElement(tree, tree, TAG_SESSION, NULL, NULL, MXML_DESCEND);
54
55         if (node) {
56                 sessionTag(tree, node);
57                 mxmlDelete(tree);
58                 return;
59         }
60
61         logg->logError(__FILE__, __LINE__, "No session tag found in the session.xml file");
62         handleException();
63 }
64
65 void SessionXML::sessionTag(mxml_node_t *tree, mxml_node_t *node) {
66         int version = 0;
67         if (mxmlElementGetAttr(node, ATTR_VERSION)) version = strtol(mxmlElementGetAttr(node, ATTR_VERSION), NULL, 10);
68         if (version != 1) {
69                 logg->logError(__FILE__, __LINE__, "Invalid session.xml version: %d", version);
70                 handleException();
71         }
72
73         // copy to pre-allocated strings
74         if (mxmlElementGetAttr(node, ATTR_BUFFER_MODE)) {
75                 strncpy(parameters.buffer_mode, mxmlElementGetAttr(node, ATTR_BUFFER_MODE), sizeof(parameters.buffer_mode));
76                 parameters.buffer_mode[sizeof(parameters.buffer_mode) - 1] = 0; // strncpy does not guarantee a null-terminated string
77         }
78         if (mxmlElementGetAttr(node, ATTR_SAMPLE_RATE)) {
79                 strncpy(parameters.sample_rate, mxmlElementGetAttr(node, ATTR_SAMPLE_RATE), sizeof(parameters.sample_rate));
80                 parameters.sample_rate[sizeof(parameters.sample_rate) - 1] = 0; // strncpy does not guarantee a null-terminated string
81         }
82
83         // integers/bools
84         parameters.call_stack_unwinding = util->stringToBool(mxmlElementGetAttr(node, ATTR_CALL_STACK_UNWINDING), false);
85         if (mxmlElementGetAttr(node, ATTR_DURATION)) parameters.duration = strtol(mxmlElementGetAttr(node, ATTR_DURATION), NULL, 10);
86         if (mxmlElementGetAttr(node, ATTR_LIVE_RATE)) parameters.live_rate = strtol(mxmlElementGetAttr(node, ATTR_LIVE_RATE), NULL, 10);
87
88         // parse subtags
89         node = mxmlGetFirstChild(node);
90         while (node) {
91                 if (mxmlGetType(node) != MXML_ELEMENT) {
92                         node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
93                         continue;
94                 }
95                 if (strcmp(TAG_IMAGE, mxmlGetElement(node)) == 0) {
96                         sessionImage(node);
97                 }
98                 node = mxmlWalkNext(node, tree, MXML_NO_DESCEND);
99         }
100 }
101
102 void SessionXML::sessionImage(mxml_node_t *node) {
103         int length = strlen(mxmlElementGetAttr(node, ATTR_PATH));
104         struct ImageLinkList *image;
105
106         image = (struct ImageLinkList *)malloc(sizeof(struct ImageLinkList));
107         image->path = (char*)malloc(length + 1);
108         image->path = strdup(mxmlElementGetAttr(node, ATTR_PATH));
109         image->next = parameters.images;
110         parameters.images = image;
111 }