1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef RIL_MCC_H
18 #define RIL_MCC_H
19
20 #include <climits>
21 #include <cstdio>
22 #include <string>
23
24 namespace ril {
25 namespace util {
26 namespace mcc {
27
28 /**
29 * Decode an integer mcc and encode as 3 digit string
30 *
31 * @param an integer mcc, its range should be in 0 to 999.
32 *
33 * @return string representation of an encoded MCC or an empty string
34 * if the MCC is not a valid MCC value.
35 */
decode(int mcc)36 static inline std::string decode(int mcc) {
37 char mccStr[4] = {0};
38 if (mcc > 999 || mcc < 0) return "";
39
40 snprintf(mccStr, sizeof(mccStr), "%03d", mcc);
41 return mccStr;
42 }
43
44 // echo -e "#include \"hardware/ril/include/telephony/ril_mcc.h\"\nint main()"\
45 // "{ return ril::util::mcc::test(); }" > ril_test.cpp \
46 // && g++ -o /tmp/ril_test -DTEST_RIL_MCC ril_test.cpp; \
47 // rm ril_test.cpp; /tmp/ril_test && [ $? ] && echo "passed"
48 #ifdef TEST_RIL_MCC
test()49 static int test() {
50 const struct mcc_ints { const int in; const char * out; } legacy_mccs[] = {
51 {INT_MAX, ""},
52 {1, "001"},
53 {11, "011"},
54 {111, "111"},
55 {0, "000"},
56 {9999, ""},
57 {-12, ""},
58 };
59
60 for (int i=0; i < sizeof(legacy_mccs) / sizeof(struct mcc_ints); i++) {
61 if (decode(legacy_mccs[i].in).compare(legacy_mccs[i].out)) return 1;
62 }
63
64 return 0;
65 }
66 #endif
67
68 }
69 }
70 }
71 #endif /* !defined(RIL_MCC_H) */
72