Yang Liu | 19159ee | 2016-01-14 11:09:28 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3.4 |
| 2 | # |
| 3 | # Copyright 2016 - Google |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | """ |
| 17 | Basic script for managing a JSON "database" file of SIM cards. |
| 18 | It will look at the list of attached devices, and add their SIMs to a |
| 19 | database. |
| 20 | We expect to add much more functionality in the future. |
| 21 | """ |
| 22 | |
| 23 | import argparse |
| 24 | import json |
| 25 | import acts.controllers.android_device as android_device |
| 26 | import acts.test_utils.tel.tel_defines as tel_defines |
| 27 | import acts.test_utils.tel.tel_lookup_tables as tel_lookup_tables |
| 28 | |
| 29 | def add_sims(sim_card_file=None): |
| 30 | if not sim_card_file: |
| 31 | print("Error: file name is None.") |
| 32 | return False |
| 33 | try: |
| 34 | f = open(sim_card_file, 'r') |
| 35 | simconf = json.load(f) |
| 36 | f.close() |
| 37 | except FileNotFoundError: |
| 38 | simconf = {} |
| 39 | flag = False |
| 40 | |
| 41 | droid_list = android_device.get_all_instances() |
| 42 | for droid_device in droid_list: |
| 43 | droid = droid_device.get_droid(False) |
| 44 | if droid.telephonyGetSimState() != tel_defines.SIM_STATE_READY: |
| 45 | print("No Valid Sim! {} \n".format(droid.telephonyGetSimState())) |
| 46 | continue |
| 47 | serial = droid.telephonyGetSimSerialNumber() |
| 48 | print("add_sims: {}".format(serial)) |
| 49 | |
| 50 | # Add new entry if not exist |
| 51 | if serial in simconf: |
| 52 | print("Declining to add a duplicate entry: {}".format(serial)) |
| 53 | #TODO: Add support for "refreshing" an entry |
| 54 | continue |
| 55 | else: |
| 56 | simconf[serial] = {} |
| 57 | current = simconf[serial] |
| 58 | flag = True |
| 59 | |
| 60 | try: |
| 61 | #TODO: Make this list of hPLMNs a separate data structure |
| 62 | current["operator"] = tel_lookup_tables.operator_name_from_plmn_id( |
| 63 | droid.telephonyGetSimOperator()) |
| 64 | except KeyError: |
| 65 | print("Unknown Operator {}".format(droid.telephonyGetSimOperator())) |
| 66 | current["operator"] = "" |
| 67 | |
| 68 | # This is default capability: we need to try and |
| 69 | # run-time determine in the future based on SIM queries? |
| 70 | current["capability"] = ['voice', 'ims', 'volte', 'vt', 'sms', |
| 71 | 'tethering', 'data'] |
| 72 | |
| 73 | phone_num = droid.telephonyGetLine1Number() |
| 74 | if not phone_num: |
| 75 | print("Please manually add a phone number for {}\n".format(serial)) |
| 76 | current["phone_num"] = "" |
| 77 | else: |
| 78 | # Remove the leading + |
| 79 | if len(phone_num) == 10: |
| 80 | phone_num = "1" + phone_num |
| 81 | current["phone_num"] = ''.join(filter(lambda x: x.isdigit(), phone_num)) |
| 82 | if flag: |
| 83 | f = open(sim_card_file, 'w') |
| 84 | json.dump(simconf, f, indent=4, sort_keys=True) |
| 85 | f.close() |
| 86 | return True |
| 87 | |
| 88 | def prune_sims(sim_card_file=None): |
| 89 | if sim_card_file==None: |
| 90 | sim_card_file = "./simcard_list.json" |
| 91 | add_sims(sim_card_file) |
| 92 | |
| 93 | try: |
| 94 | f = open(sim_card_file, 'r') |
| 95 | simconf = json.load(f) |
| 96 | f.close() |
| 97 | except FileNotFoundError: |
| 98 | print ("File not found.") |
| 99 | return False |
| 100 | |
| 101 | flag = False |
| 102 | |
| 103 | droid_list = android_device.get_all_instances() |
| 104 | |
| 105 | simconf_list = list(simconf.keys()) |
| 106 | delete_list = [] |
| 107 | active_list = [] |
| 108 | |
| 109 | for droid_device in droid_list: |
| 110 | droid = droid_device.get_droid(False) |
| 111 | if droid.telephonyGetSimState() != tel_defines.SIM_STATE_READY: |
| 112 | print("No Valid SIM! {} \n".format(droid.telephonyGetSimState())) |
| 113 | continue |
| 114 | serial = droid.telephonyGetSimSerialNumber() |
| 115 | active_list.append(serial) |
| 116 | |
| 117 | delete_list = list(set(simconf_list).difference(set(active_list))) |
| 118 | |
| 119 | print("active phones: {}".format(active_list)) |
| 120 | |
| 121 | if len(delete_list) > 0: |
| 122 | for sim in delete_list: |
| 123 | # prune |
| 124 | print("Deleting the SIM entry: ", sim) |
| 125 | del simconf[sim] |
| 126 | flag = True |
| 127 | else: |
| 128 | print("nothing to prune") |
| 129 | |
| 130 | if flag: |
| 131 | f = open(sim_card_file, 'w') |
| 132 | json.dump(simconf, f, indent=4, sort_keys=True) |
| 133 | f.close() |
| 134 | return True |
| 135 | |
| 136 | |
| 137 | if __name__ == "__main__": |
| 138 | |
| 139 | parser = argparse.ArgumentParser( |
| 140 | description=("Script to generate, augment and prune" |
| 141 | " SIM list")) |
| 142 | parser.add_argument("-f","--file", |
| 143 | default='./simcard_list.json', |
| 144 | help="json file path", type=str) |
| 145 | group = parser.add_mutually_exclusive_group() |
| 146 | group.add_argument("-a",help="append to the list of SIM entries", |
| 147 | action='store_true') |
| 148 | group.add_argument("-p",help="prune the list of SIM entries", |
| 149 | action='store_true') |
| 150 | |
| 151 | args = parser.parse_args() |
| 152 | |
| 153 | if args.a: |
| 154 | add_sims(args.f) |
| 155 | elif args.p: |
| 156 | prune_sims(args.f) |
| 157 | else: |
| 158 | print ("Error: must select an option: -a or -p") |
| 159 | |
| 160 | """ |
| 161 | Usage Examples |
| 162 | |
| 163 | ---------------------------------------------------------------- |
| 164 | p3 manage_sim.py -h |
| 165 | usage: manage_sim.py [-h] [-f F] [-a | -p] |
| 166 | |
| 167 | Script to generate, augment and prune SIM list |
| 168 | |
| 169 | optional arguments: |
| 170 | -h, --help show this help message and exit |
| 171 | -f F, --file F name for json file |
| 172 | -a append to the list of SIM entries |
| 173 | -p prune the list of SIM entries |
| 174 | |
| 175 | ---------------------------------------------------------------- |
| 176 | p3 manage_sim.py -f ./simcard_list.json -p |
| 177 | OR |
| 178 | p3 manage_sim.py -p |
| 179 | |
| 180 | Namespace(a=False, f='./simcard_list.json', p=True) |
| 181 | add_sims: 8901260222780922759 |
| 182 | Please manually add a phone number for 8901260222780922759 |
| 183 | |
| 184 | active phones: 1 |
| 185 | ['8901260222780922759'] |
| 186 | Deleting the SIM entry: 89148000001280331488 |
| 187 | : |
| 188 | : |
| 189 | Deleting the SIM entry: 89014103277559059196 |
| 190 | |
| 191 | ---------------------------------------------------------------- |
| 192 | p3 manage_sim.py -f ./simcard_list.json -a |
| 193 | OR |
| 194 | p3 manage_sim.py -a |
| 195 | |
| 196 | Namespace(a=True, f='./simcard_list.json', p=False) |
| 197 | add_sims: 8901260222780922759 |
| 198 | Please manually add a phone number for 8901260222780922759 |
| 199 | |
| 200 | ---------------------------------------------------------------- |
| 201 | """ |