Ananth Raghavan Subramanian | e432dbf | 2017-03-24 16:30:12 -0700 | [diff] [blame] | 1 | /* 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 | */ |
Rashed Abdel-Tawab | 42eb1ee | 2017-12-26 23:11:03 +0200 | [diff] [blame] | 29 | #include <log/log.h> |
Ananth Raghavan Subramanian | e432dbf | 2017-03-24 16:30:12 -0700 | [diff] [blame] | 30 | #include <fcntl.h> |
| 31 | #include <string.h> |
Rashed Abdel-Tawab | 42eb1ee | 2017-12-26 23:11:03 +0200 | [diff] [blame] | 32 | #include <unistd.h> |
Ananth Raghavan Subramanian | e432dbf | 2017-03-24 16:30:12 -0700 | [diff] [blame] | 33 | #include <cutils/properties.h> |
| 34 | #include <libxml/parser.h> |
Ananth Raghavan Subramanian | e432dbf | 2017-03-24 16:30:12 -0700 | [diff] [blame] | 35 | #include "powerhintparser.h" |
Ananth Raghavan Subramanian | e432dbf | 2017-03-24 16:30:12 -0700 | [diff] [blame] | 36 | |
| 37 | int parsePowerhintXML() { |
| 38 | |
| 39 | xmlDocPtr doc; |
| 40 | xmlNodePtr currNode; |
| 41 | const char *opcode_str, *value_str, *type_str; |
| 42 | int opcode = 0, value = 0, type = 0; |
| 43 | int numParams = 0; |
| 44 | static int hintCount; |
| 45 | |
| 46 | if(access(POWERHINT_XML, F_OK) < 0) { |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | doc = xmlReadFile(POWERHINT_XML, "UTF-8", XML_PARSE_RECOVER); |
| 51 | if(!doc) { |
| 52 | ALOGE("Document not parsed successfully"); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | currNode = xmlDocGetRootElement(doc); |
| 57 | if(!currNode) { |
| 58 | ALOGE("Empty document"); |
| 59 | xmlFreeDoc(doc); |
| 60 | xmlCleanupParser(); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | // Confirm the root-element of the tree |
| 65 | if(xmlStrcmp(currNode->name, BAD_CAST "Powerhint")) { |
| 66 | ALOGE("document of the wrong type, root node != root"); |
| 67 | xmlFreeDoc(doc); |
| 68 | xmlCleanupParser(); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | currNode = currNode->xmlChildrenNode; |
| 73 | |
| 74 | for(; currNode != NULL; currNode=currNode->next) { |
| 75 | |
| 76 | if(currNode->type != XML_ELEMENT_NODE) |
| 77 | continue; |
| 78 | |
| 79 | xmlNodePtr node = currNode; |
| 80 | |
| 81 | if(hintCount == MAX_HINT) { |
| 82 | ALOGE("Number of hints exceeded the max count of %d\n",MAX_HINT); |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | if(!xmlStrcmp(node->name, BAD_CAST "Hint")) { |
| 87 | if(xmlHasProp(node, BAD_CAST "type")) { |
| 88 | type_str = (const char*)xmlGetProp(node, BAD_CAST "type"); |
| 89 | if (type_str == NULL) |
| 90 | { |
| 91 | ALOGE("xmlGetProp failed on type"); |
| 92 | xmlFreeDoc(doc); |
| 93 | xmlCleanupParser(); |
| 94 | return -1; |
| 95 | } |
| 96 | type = strtol(type_str, NULL, 16); |
| 97 | } |
| 98 | |
| 99 | node = node->children; |
| 100 | while(node != NULL) { |
| 101 | if(!xmlStrcmp(node->name, BAD_CAST "Resource")) { |
| 102 | |
| 103 | if(xmlHasProp(node, BAD_CAST "opcode")) { |
| 104 | opcode_str = (const char*)xmlGetProp(node, BAD_CAST "opcode"); |
| 105 | if (opcode_str == NULL) |
| 106 | { |
| 107 | ALOGE("xmlGetProp failed on opcode"); |
| 108 | xmlFreeDoc(doc); |
| 109 | xmlCleanupParser(); |
| 110 | return -1; |
| 111 | } |
| 112 | opcode = strtol(opcode_str, NULL, 16); |
| 113 | } |
| 114 | if(xmlHasProp(node, BAD_CAST "value")) { |
| 115 | value_str = (const char*)xmlGetProp(node, BAD_CAST "value"); |
| 116 | if (value_str == NULL) |
| 117 | { |
| 118 | ALOGE("xmlGetProp failed on value"); |
| 119 | xmlFreeDoc(doc); |
| 120 | xmlCleanupParser(); |
| 121 | return -1; |
| 122 | } |
| 123 | value = strtol(value_str, NULL, 16); |
| 124 | } |
| 125 | if(opcode > 0) { |
| 126 | if(numParams < (MAX_PARAM-1)) { |
| 127 | powerhint[hintCount].paramList[numParams++] = opcode; |
| 128 | powerhint[hintCount].paramList[numParams++] = value; |
| 129 | } else { |
| 130 | ALOGE("Maximum parameters exceeded for Hint ID %x\n",type); |
| 131 | opcode = value = 0; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | opcode = value = 0; |
| 137 | } |
| 138 | node = node->next; |
| 139 | } |
| 140 | powerhint[hintCount].type = type; |
| 141 | powerhint[hintCount].numParams = numParams; |
| 142 | numParams = 0; |
| 143 | } |
| 144 | hintCount++; |
| 145 | } |
| 146 | |
| 147 | xmlFreeDoc(doc); |
| 148 | xmlCleanupParser(); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int* getPowerhint(int hint_id, int *params) { |
| 153 | |
| 154 | int *result = NULL; |
| 155 | |
| 156 | if(!hint_id) |
| 157 | return result; |
| 158 | |
| 159 | ALOGI("Powerhal hint received=%x\n",hint_id); |
| 160 | |
| 161 | if(!powerhint[0].numParams) { |
| 162 | parsePowerhintXML(); |
| 163 | } |
| 164 | |
| 165 | for(int i = 0; i < MAX_HINT; i++) { |
| 166 | if(hint_id == powerhint[i].type) { |
| 167 | *params = powerhint[i].numParams; |
| 168 | result = powerhint[i].paramList; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /*for (int j = 0; j < *params; j++) |
| 174 | ALOGI("Powerhal resource again%x = \n", result[j]);*/ |
| 175 | |
| 176 | return result; |
| 177 | } |