Merge remote-tracking branch 'lsk/v3.10/topic/aosp' into linux-linaro-lsk-android
[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 static const char *ATTR_CAPTURE_WORKING_DIR  = "capture_working_dir";
30 static const char *ATTR_CAPTURE_COMMAND      = "capture_command";
31 static const char *ATTR_CAPTURE_USER         = "capture_user";
32
33 SessionXML::SessionXML(const char *str) {
34         parameters.buffer_mode[0] = 0;
35         parameters.sample_rate[0] = 0;
36         parameters.call_stack_unwinding = false;
37         parameters.live_rate = 0;
38         mSessionXML = str;
39         logg->logMessage(mSessionXML);
40 }
41
42 SessionXML::~SessionXML() {
43 }
44
45 void SessionXML::parse() {
46         mxml_node_t *tree;
47         mxml_node_t *node;
48
49         tree = mxmlLoadString(NULL, mSessionXML, MXML_NO_CALLBACK);
50         node = mxmlFindElement(tree, tree, TAG_SESSION, NULL, NULL, MXML_DESCEND);
51
52         if (node) {
53                 sessionTag(tree, node);
54                 mxmlDelete(tree);
55                 return;
56         }
57
58         logg->logError(__FILE__, __LINE__, "No session tag found in the session.xml file");
59         handleException();
60 }
61
62 void SessionXML::sessionTag(mxml_node_t *tree, mxml_node_t *node) {
63         int version = 0;
64         if (mxmlElementGetAttr(node, ATTR_VERSION)) version = strtol(mxmlElementGetAttr(node, ATTR_VERSION), NULL, 10);
65         if (version != 1) {
66                 logg->logError(__FILE__, __LINE__, "Invalid session.xml version: %d", version);
67                 handleException();
68         }
69
70         // copy to pre-allocated strings
71         if (mxmlElementGetAttr(node, ATTR_BUFFER_MODE)) {
72                 strncpy(parameters.buffer_mode, mxmlElementGetAttr(node, ATTR_BUFFER_MODE), sizeof(parameters.buffer_mode));
73                 parameters.buffer_mode[sizeof(parameters.buffer_mode) - 1] = 0; // strncpy does not guarantee a null-terminated string
74         }
75         if (mxmlElementGetAttr(node, ATTR_SAMPLE_RATE)) {
76                 strncpy(parameters.sample_rate, mxmlElementGetAttr(node, ATTR_SAMPLE_RATE), sizeof(parameters.sample_rate));
77                 parameters.sample_rate[sizeof(parameters.sample_rate) - 1] = 0; // strncpy does not guarantee a null-terminated string
78         }
79         if (mxmlElementGetAttr(node, ATTR_CAPTURE_WORKING_DIR)) gSessionData->mCaptureWorkingDir = strdup(mxmlElementGetAttr(node, ATTR_CAPTURE_WORKING_DIR));
80         if (mxmlElementGetAttr(node, ATTR_CAPTURE_COMMAND)) gSessionData->mCaptureCommand = strdup(mxmlElementGetAttr(node, ATTR_CAPTURE_COMMAND));
81         if (mxmlElementGetAttr(node, ATTR_CAPTURE_USER)) gSessionData->mCaptureUser = strdup(mxmlElementGetAttr(node, ATTR_CAPTURE_USER));
82
83         // integers/bools
84         parameters.call_stack_unwinding = util->stringToBool(mxmlElementGetAttr(node, ATTR_CALL_STACK_UNWINDING), false);
85         if (mxmlElementGetAttr(node, ATTR_DURATION)) gSessionData->mDuration = 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 = gSessionData->mImages;
110         gSessionData->mImages = image;
111 }