1#!/usr/bin/env python3
2#  Copyright (C) 2021 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# This script helps fix the apns-full-conf.xml
17import re
18
19# As in TelephonyProvider.java#rilRadioTechnologyToNetworkTypeBitmask(int rat)
20def rilRadioTechnologyToNetworkType(rat):
21    match rat:
22        case "1":
23            return 1
24        case "2":
25            return 2
26        case "3":
27            return 3
28        case "9":
29            return 8
30        case "10":
31            return 9
32        case "11":
33            return 10
34        case "4":
35            return 4
36        case "5":
37            return 4
38        case "6":
39            return 7
40        case "7":
41            return 5
42        case "8":
43            return 6
44        case "12":
45            return 12
46        case "13":
47            return 14
48        case "14":
49            return 13
50        case "15":
51            return 15
52        case "16":
53            return 16
54        case "17":
55            return 17
56        case "18":
57            return 18
58        case "19":
59            return 19
60        case "20":
61            return 20
62        case _:
63            return 0
64
65with open('apns-full-conf.xml', 'r') as ifile, open('new-apns-full-conf.xml', 'w') as ofile:
66    RE_TYPE = re.compile(r"^\s*type")
67    RE_IA_DEFAULT = re.compile(r"(?!.*ia)default")
68
69    RE_BEAR_BITMASK = re.compile(r"bearer_bitmask=\"[\d|]+\"")
70    for line in ifile:
71        if re.match(RE_TYPE, line):
72        # add the missing "IA" APN type to the APN entry that support "default" APN type
73            ofile.write(re.sub(RE_IA_DEFAULT, "default,ia", line))
74        elif re.search(RE_BEAR_BITMASK, line):
75        # convert bearer_bitmask -> network_type_bitmask
76            rats = line.split("\"")[1].strip().split("|")
77            networktypes = map(rilRadioTechnologyToNetworkType, rats)
78            networktypes = sorted(set(networktypes))
79            networktypes = map(str, networktypes)
80            networktypes = "|".join(networktypes)
81            res = "network_type_bitmask=\"" + networktypes + "\""
82            ofile.write(re.sub(RE_BEAR_BITMASK, res, line))
83        else:
84            ofile.write(line)
85