• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.google.android.mobly.snippet.bundled.utils;
18 
19 import android.annotation.TargetApi;
20 import android.bluetooth.BluetoothGattCharacteristic;
21 import android.bluetooth.BluetoothGattService;
22 import android.bluetooth.le.AdvertiseData;
23 import android.bluetooth.le.AdvertiseSettings;
24 import android.bluetooth.le.ScanFilter;
25 import android.bluetooth.le.ScanSettings;
26 import android.net.wifi.WifiConfiguration;
27 import android.os.Build;
28 import android.os.ParcelUuid;
29 import android.util.Base64;
30 import java.util.UUID;
31 import org.json.JSONArray;
32 import org.json.JSONException;
33 import org.json.JSONObject;
34 
35 /**
36  * A collection of methods used to deserialize JSON strings into data objects defined in Android
37  * API.
38  */
39 public class JsonDeserializer {
40 
JsonDeserializer()41     private JsonDeserializer() {}
42 
jsonToWifiConfig(JSONObject jsonObject)43     public static WifiConfiguration jsonToWifiConfig(JSONObject jsonObject) throws JSONException {
44         WifiConfiguration config = new WifiConfiguration();
45         config.SSID = "\"" + jsonObject.getString("SSID") + "\"";
46         config.hiddenSSID = jsonObject.optBoolean("hiddenSSID", false);
47         if (jsonObject.has("password")) {
48             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
49             config.preSharedKey = "\"" + jsonObject.getString("password") + "\"";
50         } else {
51             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
52         }
53         return config;
54     }
55 
56     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
jsonToBleAdvertiseSettings(JSONObject jsonObject)57     public static AdvertiseSettings jsonToBleAdvertiseSettings(JSONObject jsonObject)
58             throws JSONException {
59         AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
60         if (jsonObject.has("AdvertiseMode")) {
61             int mode = MbsEnums.BLE_ADVERTISE_MODE.getInt(jsonObject.getString("AdvertiseMode"));
62             builder.setAdvertiseMode(mode);
63         }
64         // Timeout in milliseconds.
65         if (jsonObject.has("Timeout")) {
66             builder.setTimeout(jsonObject.getInt("Timeout"));
67         }
68         if (jsonObject.has("Connectable")) {
69             builder.setConnectable(jsonObject.getBoolean("Connectable"));
70         }
71         if (jsonObject.has("TxPowerLevel")) {
72             int txPowerLevel =
73                     MbsEnums.BLE_ADVERTISE_TX_POWER.getInt(jsonObject.getString("TxPowerLevel"));
74             builder.setTxPowerLevel(txPowerLevel);
75         }
76         return builder.build();
77     }
78 
79     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
jsonToBleAdvertiseData(JSONObject jsonObject)80     public static AdvertiseData jsonToBleAdvertiseData(JSONObject jsonObject) throws JSONException {
81         AdvertiseData.Builder builder = new AdvertiseData.Builder();
82         if (jsonObject.has("IncludeDeviceName")) {
83             builder.setIncludeDeviceName(jsonObject.getBoolean("IncludeDeviceName"));
84         }
85         if (jsonObject.has("IncludeTxPowerLevel")) {
86             builder.setIncludeTxPowerLevel(jsonObject.getBoolean("IncludeTxPowerLevel"));
87         }
88         if (jsonObject.has("ServiceData")) {
89             JSONArray serviceData = jsonObject.getJSONArray("ServiceData");
90             for (int i = 0; i < serviceData.length(); i++) {
91                 JSONObject dataSet = serviceData.getJSONObject(i);
92                 ParcelUuid parcelUuid = ParcelUuid.fromString(dataSet.getString("UUID"));
93                 builder.addServiceUuid(parcelUuid);
94                 if (dataSet.has("Data")) {
95                     byte[] data = Base64.decode(dataSet.getString("Data"), Base64.DEFAULT);
96                     builder.addServiceData(parcelUuid, data);
97                 }
98             }
99         }
100         if (jsonObject.has("ManufacturerData")) {
101             JSONObject manufacturerData = jsonObject.getJSONObject("ManufacturerData");
102             int manufacturerId = manufacturerData.getInt("ManufacturerId");
103             byte[] manufacturerSpecificData =
104                     Base64.decode(jsonObject.getString("ManufacturerSpecificData"), Base64.DEFAULT);
105             builder.addManufacturerData(manufacturerId, manufacturerSpecificData);
106         }
107         return builder.build();
108     }
109 
110     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
jsonToBluetoothGattService( DataHolder dataHolder, JSONObject jsonObject)111     public static BluetoothGattService jsonToBluetoothGattService(
112             DataHolder dataHolder, JSONObject jsonObject) throws JSONException {
113         BluetoothGattService service =
114                 new BluetoothGattService(
115                         UUID.fromString(jsonObject.getString("UUID")),
116                         MbsEnums.BLE_SERVICE_TYPE.getInt(jsonObject.getString("Type")));
117         JSONArray characteristics = jsonObject.getJSONArray("Characteristics");
118         for (int i = 0; i < characteristics.length(); i++) {
119             BluetoothGattCharacteristic characteristic =
120                     jsonToBluetoothGattCharacteristic(dataHolder, characteristics.getJSONObject(i));
121             service.addCharacteristic(characteristic);
122         }
123         return service;
124     }
125 
126     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
jsonToBluetoothGattCharacteristic( DataHolder dataHolder, JSONObject jsonObject)127     public static BluetoothGattCharacteristic jsonToBluetoothGattCharacteristic(
128             DataHolder dataHolder, JSONObject jsonObject) throws JSONException {
129         BluetoothGattCharacteristic characteristic =
130                 new BluetoothGattCharacteristic(
131                         UUID.fromString(jsonObject.getString("UUID")),
132                         MbsEnums.BLE_PROPERTY_TYPE.getInt(jsonObject.getString("Property")),
133                         MbsEnums.BLE_PERMISSION_TYPE.getInt(jsonObject.getString("Permission")));
134         if (jsonObject.has("Data")) {
135               dataHolder.insertData(characteristic, jsonObject.getString("Data"));
136         }
137         return characteristic;
138     }
139 
140     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
jsonToScanFilter(JSONObject jsonObject)141     public static ScanFilter jsonToScanFilter(JSONObject jsonObject) throws JSONException {
142         ScanFilter.Builder builder = new ScanFilter.Builder();
143         if (jsonObject.has("ServiceUuid")) {
144             builder.setServiceUuid(ParcelUuid.fromString(jsonObject.getString("ServiceUuid")));
145         }
146         return builder.build();
147     }
148 
149     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
jsonToScanSettings(JSONObject jsonObject)150     public static ScanSettings jsonToScanSettings(JSONObject jsonObject) throws JSONException {
151         ScanSettings.Builder builder = new ScanSettings.Builder();
152         if (jsonObject.has("ScanMode")) {
153             builder.setScanMode(MbsEnums.BLE_SCAN_MODE.getInt(jsonObject.getString("ScanMode")));
154         }
155         return builder.build();
156     }
157 }
158