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 package com.android.server.wifi; 18 19 import com.android.server.wifi.util.XmlUtil; 20 21 import org.xmlpull.v1.XmlPullParser; 22 import org.xmlpull.v1.XmlPullParserException; 23 import org.xmlpull.v1.XmlSerializer; 24 25 import java.io.IOException; 26 import java.util.HashMap; 27 import java.util.Map; 28 29 /** 30 * This class performs serialization and parsing of XML data block that contain the mapping 31 * from configKey to randomized MAC address 32 * (XML block data inside <MacAddressMappingList> tag). 33 */ 34 public class RandomizedMacStoreData implements WifiConfigStore.StoreData { 35 private static final String XML_TAG_SECTION_HEADER_MAC_ADDRESS_MAP = "MacAddressMap"; 36 private static final String XML_TAG_MAC_MAP = "MacMapEntry"; 37 38 private Map<String, String> mMacMapping; 39 RandomizedMacStoreData()40 RandomizedMacStoreData() {} 41 42 @Override serializeData(XmlSerializer out)43 public void serializeData(XmlSerializer out) 44 throws XmlPullParserException, IOException { 45 if (mMacMapping != null) { 46 XmlUtil.writeNextValue(out, XML_TAG_MAC_MAP, mMacMapping); 47 } 48 } 49 50 @Override deserializeData(XmlPullParser in, int outerTagDepth)51 public void deserializeData(XmlPullParser in, int outerTagDepth) 52 throws XmlPullParserException, IOException { 53 // Ignore empty reads. 54 if (in == null) { 55 return; 56 } 57 while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) { 58 String[] valueName = new String[1]; 59 Object value = XmlUtil.readCurrentValue(in, valueName); 60 if (valueName[0] == null) { 61 throw new XmlPullParserException("Missing value name"); 62 } 63 switch (valueName[0]) { 64 case XML_TAG_MAC_MAP: 65 mMacMapping = (Map<String, String>) value; 66 break; 67 default: 68 throw new XmlPullParserException("Unknown tag under " 69 + XML_TAG_SECTION_HEADER_MAC_ADDRESS_MAP 70 + ": " + valueName[0]); 71 } 72 } 73 } 74 75 @Override resetData()76 public void resetData() { 77 mMacMapping = null; 78 } 79 80 @Override hasNewDataToSerialize()81 public boolean hasNewDataToSerialize() { 82 // always persist. 83 return true; 84 } 85 86 @Override getName()87 public String getName() { 88 return XML_TAG_SECTION_HEADER_MAC_ADDRESS_MAP; 89 } 90 91 @Override getStoreFileId()92 public @WifiConfigStore.StoreFileId int getStoreFileId() { 93 // Shared general store. 94 return WifiConfigStore.STORE_FILE_SHARED_GENERAL; 95 } 96 97 /** 98 * An empty Map will be returned for null MAC address map. 99 * 100 * @return Map of mapping from configKey to the randomized MAC address. 101 */ getMacMapping()102 public Map<String, String> getMacMapping() { 103 if (mMacMapping == null) { 104 return new HashMap<String, String>(); 105 } 106 return mMacMapping; 107 } 108 109 /** 110 * Sets the data to be stored to file. 111 * @param macMapping 112 */ setMacMapping(Map<String, String> macMapping)113 public void setMacMapping(Map<String, String> macMapping) { 114 mMacMapping = macMapping; 115 } 116 } 117 118