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