blob: 6d8646fcb9cc2ebd35856e566459b9e0d2277e37 [file] [log] [blame]
David Ngee7c4c52018-03-22 23:49:12 -07001/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29#include <cutils/log.h>
30#include <fcntl.h>
31#include <string.h>
32#include <cutils/properties.h>
33#include <libxml/parser.h>
34#include <libxml/tree.h>
Vinay Verma2fd9c122018-04-29 14:08:30 +053035#include <unistd.h>
David Ngee7c4c52018-03-22 23:49:12 -070036#include "powerhintparser.h"
37#define LOG_TAG "QTI PowerHAL"
38
39int parsePowerhintXML() {
40
41 xmlDocPtr doc;
42 xmlNodePtr currNode;
43 const char *opcode_str, *value_str, *type_str;
44 int opcode = 0, value = 0, type = 0;
45 int numParams = 0;
46 static int hintCount;
47
48 if(access(POWERHINT_XML, F_OK) < 0) {
49 return -1;
50 }
51
52 doc = xmlReadFile(POWERHINT_XML, "UTF-8", XML_PARSE_RECOVER);
53 if(!doc) {
54 ALOGE("Document not parsed successfully");
55 return -1;
56 }
57
58 currNode = xmlDocGetRootElement(doc);
59 if(!currNode) {
60 ALOGE("Empty document");
61 xmlFreeDoc(doc);
62 xmlCleanupParser();
63 return -1;
64 }
65
66 // Confirm the root-element of the tree
67 if(xmlStrcmp(currNode->name, BAD_CAST "Powerhint")) {
68 ALOGE("document of the wrong type, root node != root");
69 xmlFreeDoc(doc);
70 xmlCleanupParser();
71 return -1;
72 }
73
74 currNode = currNode->xmlChildrenNode;
75
76 for(; currNode != NULL; currNode=currNode->next) {
77
78 if(currNode->type != XML_ELEMENT_NODE)
79 continue;
80
81 xmlNodePtr node = currNode;
82
83 if(hintCount == MAX_HINT) {
84 ALOGE("Number of hints exceeded the max count of %d\n",MAX_HINT);
85 break;
86 }
87
88 if(!xmlStrcmp(node->name, BAD_CAST "Hint")) {
89 if(xmlHasProp(node, BAD_CAST "type")) {
90 type_str = (const char*)xmlGetProp(node, BAD_CAST "type");
91 if (type_str == NULL)
92 {
93 ALOGE("xmlGetProp failed on type");
94 xmlFreeDoc(doc);
95 xmlCleanupParser();
96 return -1;
97 }
98 type = strtol(type_str, NULL, 16);
99 }
100
101 node = node->children;
102 while(node != NULL) {
103 if(!xmlStrcmp(node->name, BAD_CAST "Resource")) {
104
105 if(xmlHasProp(node, BAD_CAST "opcode")) {
106 opcode_str = (const char*)xmlGetProp(node, BAD_CAST "opcode");
107 if (opcode_str == NULL)
108 {
109 ALOGE("xmlGetProp failed on opcode");
110 xmlFreeDoc(doc);
111 xmlCleanupParser();
112 return -1;
113 }
114 opcode = strtol(opcode_str, NULL, 16);
115 }
116 if(xmlHasProp(node, BAD_CAST "value")) {
117 value_str = (const char*)xmlGetProp(node, BAD_CAST "value");
118 if (value_str == NULL)
119 {
120 ALOGE("xmlGetProp failed on value");
121 xmlFreeDoc(doc);
122 xmlCleanupParser();
123 return -1;
124 }
125 value = strtol(value_str, NULL, 16);
126 }
127 if(opcode > 0) {
128 if(numParams < (MAX_PARAM-1)) {
129 powerhint[hintCount].paramList[numParams++] = opcode;
130 powerhint[hintCount].paramList[numParams++] = value;
131 } else {
132 ALOGE("Maximum parameters exceeded for Hint ID %x\n",type);
133 opcode = value = 0;
134 break;
135 }
136 }
137
138 opcode = value = 0;
139 }
140 node = node->next;
141 }
142 powerhint[hintCount].type = type;
143 powerhint[hintCount].numParams = numParams;
144 numParams = 0;
145 }
146 hintCount++;
147 }
148
149 xmlFreeDoc(doc);
150 xmlCleanupParser();
151 return 0;
152}
153
154int* getPowerhint(int hint_id, int *params) {
155
156 int *result = NULL;
157
158 if(!hint_id)
159 return result;
160
161 ALOGI("Powerhal hint received=%x\n",hint_id);
162
163 if(!powerhint[0].numParams) {
164 parsePowerhintXML();
165 }
166
167 for(int i = 0; i < MAX_HINT; i++) {
168 if(hint_id == powerhint[i].type) {
169 *params = powerhint[i].numParams;
170 result = powerhint[i].paramList;
171 break;
172 }
173 }
174
175 /*for (int j = 0; j < *params; j++)
176 ALOGI("Powerhal resource again%x = \n", result[j]);*/
177
178 return result;
179}