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