~ubuntu-branches/ubuntu/vivid/thermald/vivid

« back to all changes in this revision

Viewing changes to src/thd_parse.cpp

  • Committer: Package Import Robot
  • Author(s): Colin King
  • Date: 2014-07-28 09:39:34 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20140728093934-xvrqjaw68x6q48ji
Tags: 1.3-1
Sync up with version 1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include "thd_parse.h"
30
30
#include <stdlib.h>
 
31
#include <algorithm>
31
32
#include "thd_sys_fs.h"
 
33
#include "thd_trt_art_reader.h"
32
34
 
33
35
#define DEBUG_PARSER_PRINT(x,...)
34
36
 
35
37
void cthd_parse::string_trim(std::string &str) {
36
 
        std::string::size_type pos = str.find_last_not_of(' ');
37
 
        if (pos != std::string::npos) {
38
 
                str.erase(pos + 1);
39
 
                pos = str.find_first_not_of(' ');
40
 
                if (pos != std::string::npos)
41
 
                        str.erase(0, pos);
42
 
        } else
43
 
                str.erase(str.begin(), str.end());
 
38
        std::string chars = "\n \t\r";
 
39
 
 
40
        for (unsigned int i = 0; i < chars.length(); ++i) {
 
41
                str.erase(std::remove(str.begin(), str.end(), chars[i]), str.end());
 
42
        }
44
43
}
45
44
 
46
45
#ifdef ANDROID
71
70
 
72
71
cthd_parse::cthd_parse() :
73
72
                matched_thermal_info_index(-1), doc(NULL), root_element(NULL) {
74
 
        std::string name = TDCONFDIR;
75
 
        filename = name + "/" "thermal-conf.xml";
 
73
        std::string name_conf = TDCONFDIR;
 
74
        std::string name_run = TDRUNDIR;
 
75
        filename = name_conf + "/" + "thermal-conf.xml";
 
76
        filename_auto = name_run + "/" + "thermal-conf.xml.auto";
76
77
}
77
78
 
78
79
int cthd_parse::parser_init() {
 
80
        cthd_acpi_rel rel;
 
81
        int ret;
79
82
 
80
 
        doc = xmlReadFile(filename.c_str(), NULL, 0);
 
83
        ret = rel.generate_conf(filename_auto);
 
84
        if (!ret) {
 
85
                thd_log_warn("Using generated %s\n", filename_auto.c_str());
 
86
                doc = xmlReadFile(filename_auto.c_str(), NULL, 0);
 
87
        } else {
 
88
                doc = xmlReadFile(filename.c_str(), NULL, 0);
 
89
        }
81
90
        if (doc == NULL) {
82
91
                thd_log_warn("error: could not parse file %s\n", filename.c_str());
83
92
                return THD_ERROR;
111
120
                                        "SamplingPeriod")) {
112
121
                                trip_cdev->sampling_period = atoi(tmp_value);
113
122
                        }
 
123
                        if (tmp_value)
 
124
                                xmlFree(tmp_value);
114
125
                }
115
126
        }
116
127
 
175
186
int cthd_parse::parse_trip_points(xmlNode * a_node, xmlDoc *doc,
176
187
                thermal_zone_t *info_ptr) {
177
188
        xmlNode *cur_node = NULL;
178
 
        trip_point_t trip_pt;
179
189
 
180
190
        for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
181
191
                if (cur_node->type == XML_ELEMENT_NODE) {
182
192
                        DEBUG_PARSER_PRINT("node type: Element, name: %s value: %s\n", cur_node->name, xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1));
183
193
                        if (!strcasecmp((const char*) cur_node->name, "TripPoint")) {
 
194
                                trip_point_t trip_pt;
 
195
 
184
196
                                trip_pt.hyst = trip_pt.temperature = 0;
185
197
                                trip_pt.trip_pt_type = PASSIVE;
186
198
                                trip_pt.control_type = PARALLEL;
551
563
                        for (unsigned int k = 0;
552
564
                                        k < thermal_info_list[i].zones[j].trip_pts.size(); ++k) {
553
565
                                thd_log_info("\t\t Trip Point %u \n", k);
554
 
                                thd_log_info("\t\t  temp id %d \n",
 
566
                                thd_log_info("\t\t  temp %d \n",
555
567
                                                thermal_info_list[i].zones[j].trip_pts[k].temperature);
556
568
                                thd_log_info("\t\t  trip type %d \n",
557
569
                                                thermal_info_list[i].zones[j].trip_pts[k].trip_pt_type);
558
570
                                thd_log_info("\t\t  hyst id %d \n",
559
571
                                                thermal_info_list[i].zones[j].trip_pts[k].hyst);
 
572
                                thd_log_info("\t\t  sensor type %s \n",
 
573
                                                thermal_info_list[i].zones[j].trip_pts[k].sensor_type.c_str());
560
574
 
561
575
                                for (unsigned int l = 0;
562
576
                                                l
563
577
                                                                < thermal_info_list[i].zones[j].trip_pts[k].cdev_trips.size();
564
578
                                                ++l) {
565
 
                                        thd_log_info("\t\t  Trip id %u \n", l);
 
579
                                        thd_log_info("\t\t  cdev index %u \n", l);
566
580
                                        thd_log_info("\t\t\t  type %s \n",
567
581
                                                        thermal_info_list[i].zones[j].trip_pts[k].cdev_trips[l].type.c_str());
568
582
                                        thd_log_info("\t\t\t  influence %d \n",
601
615
}
602
616
 
603
617
bool cthd_parse::platform_matched() {
604
 
        csys_fs thd_sysfs("/sys/class/dmi/id/");
605
 
 
606
 
        if (thd_sysfs.exists(std::string("product_uuid"))) {
607
 
                thd_log_debug("checking UUID\n");
608
 
                std::string str;
609
 
                if (thd_sysfs.read("product_uuid", str) >= 0) {
610
 
                        thd_log_info("UUID is [%s]\n", str.c_str());
611
 
                        for (unsigned int i = 0; i < thermal_info_list.size(); ++i) {
612
 
                                if (thermal_info_list[i].uuid == "*") {
613
 
                                        matched_thermal_info_index = i;
614
 
                                        thd_log_info("UUID matched [wildcard]\n");
615
 
                                        return true;
616
 
                                }
617
 
                                if (thermal_info_list[i].uuid == str) {
618
 
                                        matched_thermal_info_index = i;
619
 
                                        thd_log_info("Product UUID matched \n");
620
 
                                        return true;
621
 
                                }
 
618
 
 
619
        std::string line;
 
620
 
 
621
        std::ifstream product_uuid("/sys/class/dmi/id/product_uuid");
 
622
 
 
623
        if (product_uuid.is_open() && getline(product_uuid, line)) {
 
624
                for (unsigned int i = 0; i < thermal_info_list.size(); ++i) {
 
625
                        if (!thermal_info_list[i].uuid.size())
 
626
                                continue;
 
627
                        string_trim(line);
 
628
                        thd_log_debug("config product uuid [%s] match with [%s]\n",
 
629
                                        thermal_info_list[i].uuid.c_str(), line.c_str());
 
630
                        if (thermal_info_list[i].uuid == "*") {
 
631
                                matched_thermal_info_index = i;
 
632
                                thd_log_info("UUID matched [wildcard]\n");
 
633
                                return true;
 
634
                        }
 
635
                        if (line == thermal_info_list[i].uuid) {
 
636
                                matched_thermal_info_index = i;
 
637
                                thd_log_info("UUID matched \n");
 
638
                                return true;
622
639
                        }
623
640
                }
624
641
        }
625
642
 
626
 
        if (thd_sysfs.exists(std::string("product_name"))) {
627
 
                thd_log_debug("checking product name\n");
628
 
                char product_name[128] = { };
629
 
                // Use different read method as the product name contains spaces
630
 
                if (thd_sysfs.read("product_name", product_name, 127) >= 0) {
631
 
                        product_name[127] = '\0';
632
 
                        int len = strlen(product_name);
633
 
                        if (!len)
634
 
                                return false;
635
 
                        for (int i = 0; i < len; ++i)
636
 
                                if (product_name[i] == '\n')
637
 
                                        product_name[i] = '\0';
638
 
                        thd_log_info("product name is[%s]\n", product_name);
639
 
                        for (unsigned int i = 0; i < thermal_info_list.size(); ++i) {
640
 
                                if (!thermal_info_list[i].product_name.size())
641
 
                                        continue;
642
 
                                thd_log_debug("config product name %s\n",
643
 
                                                thermal_info_list[i].product_name.c_str());
644
 
                                if (thermal_info_list[i].product_name == "*") {
645
 
                                        matched_thermal_info_index = i;
646
 
                                        thd_log_info("Product Name matched [wildcard]\n");
647
 
                                        return true;
648
 
                                }
649
 
                                if (thermal_info_list[i].product_name.compare(0,
650
 
                                                strlen(product_name), product_name) == 0) {
651
 
                                        matched_thermal_info_index = i;
652
 
                                        thd_log_info("Product Name matched \n");
653
 
                                        return true;
654
 
                                }
 
643
        std::ifstream product_name("/sys/class/dmi/id/product_name");
 
644
 
 
645
        if (product_name.is_open() && getline(product_name, line)) {
 
646
                for (unsigned int i = 0; i < thermal_info_list.size(); ++i) {
 
647
                        if (!thermal_info_list[i].product_name.size())
 
648
                                continue;
 
649
                        string_trim(line);
 
650
                        thd_log_debug("config product name [%s] match with [%s]\n",
 
651
                                        thermal_info_list[i].product_name.c_str(), line.c_str());
 
652
                        if (thermal_info_list[i].product_name == "*") {
 
653
                                matched_thermal_info_index = i;
 
654
                                thd_log_info("Product Name matched [wildcard]\n");
 
655
                                return true;
 
656
                        }
 
657
                        if (line == thermal_info_list[i].product_name) {
 
658
                                matched_thermal_info_index = i;
 
659
                                thd_log_info("Product Name matched \n");
 
660
                                return true;
655
661
                        }
656
662
                }
657
663
        }