aboutsummaryrefslogtreecommitdiff
path: root/operator-properties/operator-properties.cpp
blob: 396ee4f06eec3ff7fcb54a40ab60c27e8c7e5128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (C) 2017, The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "operator-properties"
//#define LOG_NDEBUG 0

#include <stdio.h>
#include <stdlib.h>
#include <cutils/log.h>
#include <cutils/properties.h>
#include <tinyxml.h>

#define MNS_MAP_XML "/system/customize/mns_map.xml"

static const char *find_filename(TiXmlNode *node, unsigned op)
{
    const char *last_match = NULL;

    for (TiXmlNode *c = node->FirstChild(); c != NULL; c = c->NextSibling()) {
        if (c->Type() == TiXmlNode::ELEMENT && c->Value() != NULL) {
            if (strcmp(c->Value(), "item") == 0) {
                const char *filename = NULL;
                bool match = false;
                TiXmlElement *e = (TiXmlElement *) c;

                for (TiXmlAttribute *attrib = e->FirstAttribute(); attrib != NULL; attrib = attrib->Next()) {
                    const char *name = attrib->Name();
                    const char *value = attrib->Value();

                    if (name == NULL) {
                        ALOGE("missing name");
                    } else if (value == NULL) {
                        ALOGE("missing value for attribute %s", name);
                    } else if (strcmp(name, "name") == 0 && atoi(value) == op) {
                        match = true;
                    } else if (strcmp(name, "CNmapfile") == 0) {
                        filename = value;
                    }
                }
                if (match) {
                    last_match = filename;
                }
            } else {
                const char *match = find_filename(c, op);
                if (match) last_match = match;
            }
        }
    }

    return last_match;
}

static char buf[1024*1024];

static void load_properties(const char *filename)
{
    FILE *f;

    if ((f = fopen(filename, "r")) == NULL) {
        ALOGI("Failed to open %s", filename);
        exit(1);
    }

    while (fgets(buf, sizeof(buf), f) != NULL) {
        char *key = buf;

        while (isspace(*key)) key++;
        if (*key == '#') continue;
        char *eol = key+strlen(key)-1;
        while (eol > key && isspace(*eol)) *eol-- = '\0';
        char *value = strchr(key, '=');
        if (value) {
            *value++ = '\0';
            while (isspace(*value)) value++;
            if (property_set(key, value) < 0) {
                ALOGE("failed to set property [%s] = [%s]", key, value);
            }
        }
    }

    fclose(f);
}

int main(int argc, char **argv)
{
    unsigned op;
    const char *filename;
    char full_filename[256];
    char bootcid[PROPERTY_VALUE_MAX];

    property_get("ro.boot.cid", bootcid, "");
    if (strcmp(bootcid, "BS_US001") != 0 && strcmp(bootcid, "BS_US002") != 0) {
        ALOGI("not an unlocked variant");
        exit(0);
    }

    op = property_get_int32("gsm.sim.operator.numeric", 0);

    if (op == 0) {
        ALOGI("no operator defined");
        exit(0);
    }

    ALOGI("operator-properties for operator %d", op);

    TiXmlDocument doc(MNS_MAP_XML);
    if (! doc.LoadFile()) {
        ALOGE("Failed to load %s", MNS_MAP_XML);
        exit(1);
    }

    filename = find_filename(&doc, op);
    if (filename == NULL) filename = "default.prop";

    sprintf(full_filename, "/system/customize/MNSprop/%s", filename);
    ALOGI("Using properties %s", full_filename);

    load_properties(full_filename);

    return 0;
}