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( 105 manufacturerData.getString("ManufacturerSpecificData"), Base64.DEFAULT); 106 builder.addManufacturerData(manufacturerId, manufacturerSpecificData); 107 } 108 return builder.build(); 109 } 110 111 @TargetApi(Build.VERSION_CODES.LOLLIPOP) jsonToBluetoothGattService( DataHolder dataHolder, JSONObject jsonObject)112 public static BluetoothGattService jsonToBluetoothGattService( 113 DataHolder dataHolder, JSONObject jsonObject) throws JSONException { 114 BluetoothGattService service = 115 new BluetoothGattService( 116 UUID.fromString(jsonObject.getString("UUID")), 117 MbsEnums.BLE_SERVICE_TYPE.getInt(jsonObject.getString("Type"))); 118 JSONArray characteristics = jsonObject.getJSONArray("Characteristics"); 119 for (int i = 0; i < characteristics.length(); i++) { 120 BluetoothGattCharacteristic characteristic = 121 jsonToBluetoothGattCharacteristic(dataHolder, characteristics.getJSONObject(i)); 122 service.addCharacteristic(characteristic); 123 } 124 return service; 125 } 126 127 @TargetApi(Build.VERSION_CODES.LOLLIPOP) jsonToBluetoothGattCharacteristic( DataHolder dataHolder, JSONObject jsonObject)128 public static BluetoothGattCharacteristic jsonToBluetoothGattCharacteristic( 129 DataHolder dataHolder, JSONObject jsonObject) throws JSONException { 130 BluetoothGattCharacteristic characteristic = 131 new BluetoothGattCharacteristic( 132 UUID.fromString(jsonObject.getString("UUID")), 133 MbsEnums.BLE_PROPERTY_TYPE.getInt(jsonObject.getString("Property")), 134 MbsEnums.BLE_PERMISSION_TYPE.getInt(jsonObject.getString("Permission"))); 135 if (jsonObject.has("Data")) { 136 dataHolder.insertData(characteristic, jsonObject.getString("Data")); 137 } 138 return characteristic; 139 } 140 141 @TargetApi(Build.VERSION_CODES.LOLLIPOP) jsonToScanFilter(JSONObject jsonObject)142 public static ScanFilter jsonToScanFilter(JSONObject jsonObject) throws JSONException { 143 ScanFilter.Builder builder = new ScanFilter.Builder(); 144 if (jsonObject.has("ServiceUuid")) { 145 builder.setServiceUuid(ParcelUuid.fromString(jsonObject.getString("ServiceUuid"))); 146 } 147 return builder.build(); 148 } 149 150 @TargetApi(Build.VERSION_CODES.LOLLIPOP) jsonToScanSettings(JSONObject jsonObject)151 public static ScanSettings jsonToScanSettings(JSONObject jsonObject) throws JSONException { 152 ScanSettings.Builder builder = new ScanSettings.Builder(); 153 if (jsonObject.has("ScanMode")) { 154 builder.setScanMode(MbsEnums.BLE_SCAN_MODE.getInt(jsonObject.getString("ScanMode"))); 155 } 156 return builder.build(); 157 } 158 } 159