aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/google/android/mobly/snippet/bundled/utils/JsonDeserializer.java
blob: c7d5a91ddbcd73bf64ab0fdd36f937aad8ecfb24 (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
/*
 * 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 android.annotation.TargetApi;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;
import android.net.wifi.WifiConfiguration;
import android.os.Build;
import android.os.ParcelUuid;
import android.util.Base64;
import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * A collection of methods used to deserialize JSON strings into data objects defined in Android
 * API.
 */
public class JsonDeserializer {

    private JsonDeserializer() {}

    public static WifiConfiguration jsonToWifiConfig(JSONObject jsonObject) throws JSONException {
        WifiConfiguration config = new WifiConfiguration();
        config.SSID = "\"" + jsonObject.getString("SSID") + "\"";
        config.hiddenSSID = jsonObject.optBoolean("hiddenSSID", false);
        if (jsonObject.has("password")) {
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            config.preSharedKey = "\"" + jsonObject.getString("password") + "\"";
        } else {
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        }
        return config;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static AdvertiseSettings jsonToBleAdvertiseSettings(JSONObject jsonObject)
            throws JSONException {
        AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
        if (jsonObject.has("AdvertiseMode")) {
            int mode = MbsEnums.BLE_ADVERTISE_MODE.getInt(jsonObject.getString("AdvertiseMode"));
            builder.setAdvertiseMode(mode);
        }
        // Timeout in milliseconds.
        if (jsonObject.has("Timeout")) {
            builder.setTimeout(jsonObject.getInt("Timeout"));
        }
        if (jsonObject.has("Connectable")) {
            builder.setConnectable(jsonObject.getBoolean("Connectable"));
        }
        if (jsonObject.has("TxPowerLevel")) {
            int txPowerLevel =
                    MbsEnums.BLE_ADVERTISE_TX_POWER.getInt(jsonObject.getString("TxPowerLevel"));
            builder.setTxPowerLevel(txPowerLevel);
        }
        return builder.build();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static AdvertiseData jsonToBleAdvertiseData(JSONObject jsonObject) throws JSONException {
        AdvertiseData.Builder builder = new AdvertiseData.Builder();
        if (jsonObject.has("IncludeDeviceName")) {
            builder.setIncludeDeviceName(jsonObject.getBoolean("IncludeDeviceName"));
        }
        if (jsonObject.has("IncludeTxPowerLevel")) {
            builder.setIncludeTxPowerLevel(jsonObject.getBoolean("IncludeTxPowerLevel"));
        }
        if (jsonObject.has("ServiceData")) {
            JSONArray serviceData = jsonObject.getJSONArray("ServiceData");
            for (int i = 0; i < serviceData.length(); i++) {
                JSONObject dataSet = serviceData.getJSONObject(i);
                ParcelUuid parcelUuid = ParcelUuid.fromString(dataSet.getString("UUID"));
                builder.addServiceUuid(parcelUuid);
                if (dataSet.has("Data")) {
                    byte[] data = Base64.decode(dataSet.getString("Data"), Base64.DEFAULT);
                    builder.addServiceData(parcelUuid, data);
                }
            }
        }
        if (jsonObject.has("ManufacturerData")) {
            JSONObject manufacturerData = jsonObject.getJSONObject("ManufacturerData");
            int manufacturerId = manufacturerData.getInt("ManufacturerId");
            byte[] manufacturerSpecificData =
                    Base64.decode(
                            manufacturerData.getString("ManufacturerSpecificData"), Base64.DEFAULT);
            builder.addManufacturerData(manufacturerId, manufacturerSpecificData);
        }
        return builder.build();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static BluetoothGattService jsonToBluetoothGattService(
            DataHolder dataHolder, JSONObject jsonObject) throws JSONException {
        BluetoothGattService service =
                new BluetoothGattService(
                        UUID.fromString(jsonObject.getString("UUID")),
                        MbsEnums.BLE_SERVICE_TYPE.getInt(jsonObject.getString("Type")));
        JSONArray characteristics = jsonObject.getJSONArray("Characteristics");
        for (int i = 0; i < characteristics.length(); i++) {
            BluetoothGattCharacteristic characteristic =
                    jsonToBluetoothGattCharacteristic(dataHolder, characteristics.getJSONObject(i));
            service.addCharacteristic(characteristic);
        }
        return service;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static BluetoothGattCharacteristic jsonToBluetoothGattCharacteristic(
            DataHolder dataHolder, JSONObject jsonObject) throws JSONException {
        BluetoothGattCharacteristic characteristic =
                new BluetoothGattCharacteristic(
                        UUID.fromString(jsonObject.getString("UUID")),
                        MbsEnums.BLE_PROPERTY_TYPE.getInt(jsonObject.getString("Property")),
                        MbsEnums.BLE_PERMISSION_TYPE.getInt(jsonObject.getString("Permission")));
        if (jsonObject.has("Data")) {
              dataHolder.insertData(characteristic, jsonObject.getString("Data"));
        }
        return characteristic;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static ScanFilter jsonToScanFilter(JSONObject jsonObject) throws JSONException {
        ScanFilter.Builder builder = new ScanFilter.Builder();
        if (jsonObject.has("ServiceUuid")) {
            builder.setServiceUuid(ParcelUuid.fromString(jsonObject.getString("ServiceUuid")));
        }
        return builder.build();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static ScanSettings jsonToScanSettings(JSONObject jsonObject) throws JSONException {
        ScanSettings.Builder builder = new ScanSettings.Builder();
        if (jsonObject.has("ScanMode")) {
            builder.setScanMode(MbsEnums.BLE_SCAN_MODE.getInt(jsonObject.getString("ScanMode")));
        }
        return builder.build();
    }
}