aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/google/android/mobly/snippet/bundled/utils/JsonSerializer.java
blob: 64875014ca9b43ed40806e4f503ee0464825cb84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (C) 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.mobly.snippet.bundled.utils;

import static java.nio.charset.StandardCharsets.UTF_8;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.ScanRecord;
import android.net.DhcpInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelUuid;
import android.util.Base64;
import android.util.SparseArray;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * A collection of methods used to serialize data types defined in Android API into JSON strings.
 */
public class JsonSerializer {
    private static final Gson gson =
        new GsonBuilder()
            .serializeNulls()
            .excludeFieldsWithModifiers(Modifier.STATIC)
            .enableComplexMapKeySerialization()
            .disableInnerClassSerialization()
            .create();

    /**
     * Remove the extra quotation marks from the beginning and the end of a string.
     *
     * <p>This is useful for strings like the SSID field of Android's Wi-Fi configuration.
     *
     * @param originalString
     */
    public static String trimQuotationMarks(String originalString) {
        String result = originalString;
        if (originalString.length() > 2
                && originalString.charAt(0) == '"'
                && originalString.charAt(originalString.length() - 1) == '"') {
            result = originalString.substring(1, originalString.length() - 1);
        }
        return result;
    }

    public JSONObject toJson(Object object) throws JSONException {
        if (object instanceof DhcpInfo) {
            return serializeDhcpInfo((DhcpInfo) object);
        } else if (object instanceof WifiConfiguration) {
            return serializeWifiConfiguration((WifiConfiguration) object);
        } else if (object instanceof WifiInfo) {
            return serializeWifiInfo((WifiInfo) object);
        }
        return defaultSerialization(object);
    }

    /**
     * By default, we rely on Gson to do the right job.
     *
     * @param data An object to serialize
     * @return A JSONObject that has the info of the serialized data object.
     * @throws JSONException
     */
    private JSONObject defaultSerialization(Object data) throws JSONException {
        return new JSONObject(gson.toJson(data));
    }

    private JSONObject serializeDhcpInfo(DhcpInfo data) throws JSONException {
        JSONObject result = new JSONObject(gson.toJson(data));
        int ipAddress = data.ipAddress;
        byte[] addressBytes = {
            (byte) (0xff & ipAddress),
            (byte) (0xff & (ipAddress >> 8)),
            (byte) (0xff & (ipAddress >> 16)),
            (byte) (0xff & (ipAddress >> 24))
        };
        try {
            String addressString = InetAddress.getByAddress(addressBytes).toString();
            result.put("IpAddress", addressString);
        } catch (UnknownHostException e) {
            result.put("IpAddress", ipAddress);
        }
        return result;
    }

    private JSONObject serializeWifiConfiguration(WifiConfiguration data) throws JSONException {
        JSONObject result = new JSONObject(gson.toJson(data));
        result.put("Status", WifiConfiguration.Status.strings[data.status]);
        result.put("SSID", trimQuotationMarks(data.SSID));
        return result;
    }

    private JSONObject serializeWifiInfo(WifiInfo data) throws JSONException {
        JSONObject result = new JSONObject(gson.toJson(data));
        result.put("SSID", trimQuotationMarks(data.getSSID()));
        for (SupplicantState state : SupplicantState.values()) {
            if (data.getSupplicantState().equals(state)) {
                result.put("SupplicantState", state.name());
            }
        }
        return result;
    }

    public static Bundle serializeBluetoothDevice(BluetoothDevice data) {
        Bundle result = new Bundle();
        result.putString("Address", data.getAddress());
        final String bondState =
                MbsEnums.BLUETOOTH_DEVICE_BOND_STATE.getString(data.getBondState());
        result.putString("BondState", bondState);
        result.putString("Name", data.getName());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            String deviceType = MbsEnums.BLUETOOTH_DEVICE_TYPE.getString(data.getType());
            result.putString("DeviceType", deviceType);
            ParcelUuid[] parcelUuids = data.getUuids();
            if (parcelUuids != null) {
                ArrayList<String> uuidStrings = new ArrayList<>(parcelUuids.length);
                for (ParcelUuid parcelUuid : parcelUuids) {
                    uuidStrings.add(parcelUuid.getUuid().toString());
                }
                result.putStringArrayList("UUIDs", uuidStrings);
            }
        }
        return result;
    }

    public ArrayList<Bundle> serializeBluetoothDeviceList(
            Collection<BluetoothDevice> bluetoothDevices) {
        ArrayList<Bundle> results = new ArrayList<>();
        for (BluetoothDevice device : bluetoothDevices) {
            results.add(serializeBluetoothDevice(device));
        }
        return results;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public Bundle serializeBleScanResult(android.bluetooth.le.ScanResult scanResult) {
        Bundle result = new Bundle();
        result.putBundle("Device", serializeBluetoothDevice(scanResult.getDevice()));
        result.putInt("Rssi", scanResult.getRssi());
        result.putBundle("ScanRecord", serializeBleScanRecord(scanResult.getScanRecord()));
        result.putLong("TimestampNanos", scanResult.getTimestampNanos());
        return result;
    }

    /**
     * Serialize ScanRecord for Bluetooth LE.
     *
     * <p>Not all fields are serialized here. Will add more as we need.
     *
     * <pre>The returned {@link Bundle} has the following info:
     *          "DeviceName", String
     *          "TxPowerLevel", String
     * </pre>
     *
     * @param record A {@link ScanRecord} object.
     * @return A {@link Bundle} object.
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private Bundle serializeBleScanRecord(ScanRecord record) {
        Bundle result = new Bundle();
        result.putString("DeviceName", record.getDeviceName());
        result.putInt("TxPowerLevel", record.getTxPowerLevel());
        result.putParcelableArrayList("Services", serializeBleScanServices(record));
        result.putBundle(
            "manufacturerSpecificData", serializeBleScanManufacturerSpecificData(record));
        return result;
    }

    /** Serialize manufacturer specific data from ScanRecord for Bluetooth LE. */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private ArrayList<Bundle> serializeBleScanServices(ScanRecord record) {
        ArrayList<Bundle> result = new ArrayList<>();
        if (record.getServiceUuids() != null) {
            for (ParcelUuid uuid : record.getServiceUuids()) {
                Bundle service = new Bundle();
                service.putString("UUID", uuid.getUuid().toString());
                if (record.getServiceData(uuid) != null) {
                    service.putString(
                            "Data",
                            new String(Base64.encode(record.getServiceData(uuid), Base64.NO_WRAP),
                                      UTF_8));
                } else {
                    service.putString("Data", "");
                }
                result.add(service);
            }
        }
        return result;
    }

    /** Serialize manufacturer specific data from ScanRecord for Bluetooth LE. */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private Bundle serializeBleScanManufacturerSpecificData(ScanRecord record) {
        Bundle result = new Bundle();
        SparseArray<byte[]> sparseArray = record.getManufacturerSpecificData();
        for (int i = 0; i < sparseArray.size(); i++) {
            int key = sparseArray.keyAt(i);
            result.putByteArray(String.valueOf(key), sparseArray.get(key));
        }
        return result;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static Bundle serializeBleAdvertisingSettings(AdvertiseSettings advertiseSettings) {
        Bundle result = new Bundle();
        result.putString(
                "TxPowerLevel",
                MbsEnums.BLE_ADVERTISE_TX_POWER.getString(advertiseSettings.getTxPowerLevel()));
        result.putString(
                "Mode", MbsEnums.BLE_ADVERTISE_MODE.getString(advertiseSettings.getMode()));
        result.putInt("Timeout", advertiseSettings.getTimeout());
        result.putBoolean("IsConnectable", advertiseSettings.isConnectable());
        return result;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static Bundle serializeBluetoothGatt(BluetoothGatt gatt) {
        Bundle result = new Bundle();
        ArrayList<Bundle> services = new ArrayList<>();
        for (BluetoothGattService service : gatt.getServices()) {
            services.add(JsonSerializer.serializeBluetoothGattService(service));
        }
        result.putParcelableArrayList("Services", services);
        result.putBundle("Device", JsonSerializer.serializeBluetoothDevice(gatt.getDevice()));
        return result;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static Bundle serializeBluetoothGattService(BluetoothGattService service) {
        Bundle result = new Bundle();
        result.putString("UUID", service.getUuid().toString());
        result.putString("Type", MbsEnums.BLE_SERVICE_TYPE.getString(service.getType()));
        ArrayList<Bundle> characteristics = new ArrayList<>();
        for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
            characteristics.add(serializeBluetoothGattCharacteristic(characteristic));
        }
        result.putParcelableArrayList("Characteristics", characteristics);
        return result;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static Bundle serializeBluetoothGattCharacteristic(
            BluetoothGattCharacteristic characteristic) {
        Bundle result = new Bundle();
        result.putString("UUID", characteristic.getUuid().toString());
        result.putString(
                "Property", MbsEnums.BLE_PROPERTY_TYPE.getString(characteristic.getProperties()));
        result.putString(
                "Permission",
                MbsEnums.BLE_PERMISSION_TYPE.getString(characteristic.getPermissions()));
        return result;
    }
}