xref: /aosp_15_r20/device/sample/etc/fix_apns_full_conf.py (revision c73d2a9548bdf9b8681aeb4f83c479fa5d880dd4)
1*c73d2a95SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c73d2a95SAndroid Build Coastguard Worker#  Copyright (C) 2021 The Android Open Source Project
3*c73d2a95SAndroid Build Coastguard Worker#
4*c73d2a95SAndroid Build Coastguard Worker#  Licensed under the Apache License, Version 2.0 (the "License");
5*c73d2a95SAndroid Build Coastguard Worker#  you may not use this file except in compliance with the License.
6*c73d2a95SAndroid Build Coastguard Worker#  You may obtain a copy of the License at
7*c73d2a95SAndroid Build Coastguard Worker#
8*c73d2a95SAndroid Build Coastguard Worker#       http://www.apache.org/licenses/LICENSE-2.0
9*c73d2a95SAndroid Build Coastguard Worker#
10*c73d2a95SAndroid Build Coastguard Worker#  Unless required by applicable law or agreed to in writing, software
11*c73d2a95SAndroid Build Coastguard Worker#  distributed under the License is distributed on an "AS IS" BASIS,
12*c73d2a95SAndroid Build Coastguard Worker#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*c73d2a95SAndroid Build Coastguard Worker#  See the License for the specific language governing permissions and
14*c73d2a95SAndroid Build Coastguard Worker#  limitations under the License.
15*c73d2a95SAndroid Build Coastguard Worker
16*c73d2a95SAndroid Build Coastguard Worker# This script helps fix the apns-full-conf.xml
17*c73d2a95SAndroid Build Coastguard Workerimport re
18*c73d2a95SAndroid Build Coastguard Worker
19*c73d2a95SAndroid Build Coastguard Worker# As in TelephonyProvider.java#rilRadioTechnologyToNetworkTypeBitmask(int rat)
20*c73d2a95SAndroid Build Coastguard Workerdef rilRadioTechnologyToNetworkType(rat):
21*c73d2a95SAndroid Build Coastguard Worker    match rat:
22*c73d2a95SAndroid Build Coastguard Worker        case "1":
23*c73d2a95SAndroid Build Coastguard Worker            return 1
24*c73d2a95SAndroid Build Coastguard Worker        case "2":
25*c73d2a95SAndroid Build Coastguard Worker            return 2
26*c73d2a95SAndroid Build Coastguard Worker        case "3":
27*c73d2a95SAndroid Build Coastguard Worker            return 3
28*c73d2a95SAndroid Build Coastguard Worker        case "9":
29*c73d2a95SAndroid Build Coastguard Worker            return 8
30*c73d2a95SAndroid Build Coastguard Worker        case "10":
31*c73d2a95SAndroid Build Coastguard Worker            return 9
32*c73d2a95SAndroid Build Coastguard Worker        case "11":
33*c73d2a95SAndroid Build Coastguard Worker            return 10
34*c73d2a95SAndroid Build Coastguard Worker        case "4":
35*c73d2a95SAndroid Build Coastguard Worker            return 4
36*c73d2a95SAndroid Build Coastguard Worker        case "5":
37*c73d2a95SAndroid Build Coastguard Worker            return 4
38*c73d2a95SAndroid Build Coastguard Worker        case "6":
39*c73d2a95SAndroid Build Coastguard Worker            return 7
40*c73d2a95SAndroid Build Coastguard Worker        case "7":
41*c73d2a95SAndroid Build Coastguard Worker            return 5
42*c73d2a95SAndroid Build Coastguard Worker        case "8":
43*c73d2a95SAndroid Build Coastguard Worker            return 6
44*c73d2a95SAndroid Build Coastguard Worker        case "12":
45*c73d2a95SAndroid Build Coastguard Worker            return 12
46*c73d2a95SAndroid Build Coastguard Worker        case "13":
47*c73d2a95SAndroid Build Coastguard Worker            return 14
48*c73d2a95SAndroid Build Coastguard Worker        case "14":
49*c73d2a95SAndroid Build Coastguard Worker            return 13
50*c73d2a95SAndroid Build Coastguard Worker        case "15":
51*c73d2a95SAndroid Build Coastguard Worker            return 15
52*c73d2a95SAndroid Build Coastguard Worker        case "16":
53*c73d2a95SAndroid Build Coastguard Worker            return 16
54*c73d2a95SAndroid Build Coastguard Worker        case "17":
55*c73d2a95SAndroid Build Coastguard Worker            return 17
56*c73d2a95SAndroid Build Coastguard Worker        case "18":
57*c73d2a95SAndroid Build Coastguard Worker            return 18
58*c73d2a95SAndroid Build Coastguard Worker        case "19":
59*c73d2a95SAndroid Build Coastguard Worker            return 19
60*c73d2a95SAndroid Build Coastguard Worker        case "20":
61*c73d2a95SAndroid Build Coastguard Worker            return 20
62*c73d2a95SAndroid Build Coastguard Worker        case _:
63*c73d2a95SAndroid Build Coastguard Worker            return 0
64*c73d2a95SAndroid Build Coastguard Worker
65*c73d2a95SAndroid Build Coastguard Workerwith open('apns-full-conf.xml', 'r') as ifile, open('new-apns-full-conf.xml', 'w') as ofile:
66*c73d2a95SAndroid Build Coastguard Worker    RE_TYPE = re.compile(r"^\s*type")
67*c73d2a95SAndroid Build Coastguard Worker    RE_IA_DEFAULT = re.compile(r"(?!.*ia)default")
68*c73d2a95SAndroid Build Coastguard Worker
69*c73d2a95SAndroid Build Coastguard Worker    RE_BEAR_BITMASK = re.compile(r"bearer_bitmask=\"[\d|]+\"")
70*c73d2a95SAndroid Build Coastguard Worker    for line in ifile:
71*c73d2a95SAndroid Build Coastguard Worker        if re.match(RE_TYPE, line):
72*c73d2a95SAndroid Build Coastguard Worker        # add the missing "IA" APN type to the APN entry that support "default" APN type
73*c73d2a95SAndroid Build Coastguard Worker            ofile.write(re.sub(RE_IA_DEFAULT, "default,ia", line))
74*c73d2a95SAndroid Build Coastguard Worker        elif re.search(RE_BEAR_BITMASK, line):
75*c73d2a95SAndroid Build Coastguard Worker        # convert bearer_bitmask -> network_type_bitmask
76*c73d2a95SAndroid Build Coastguard Worker            rats = line.split("\"")[1].strip().split("|")
77*c73d2a95SAndroid Build Coastguard Worker            networktypes = map(rilRadioTechnologyToNetworkType, rats)
78*c73d2a95SAndroid Build Coastguard Worker            networktypes = sorted(set(networktypes))
79*c73d2a95SAndroid Build Coastguard Worker            networktypes = map(str, networktypes)
80*c73d2a95SAndroid Build Coastguard Worker            networktypes = "|".join(networktypes)
81*c73d2a95SAndroid Build Coastguard Worker            res = "network_type_bitmask=\"" + networktypes + "\""
82*c73d2a95SAndroid Build Coastguard Worker            ofile.write(re.sub(RE_BEAR_BITMASK, res, line))
83*c73d2a95SAndroid Build Coastguard Worker        else:
84*c73d2a95SAndroid Build Coastguard Worker            ofile.write(line)
85