1 /* 2 * Copyright (C) 2024 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.google.snippet.wifi.aware; 18 19 import android.net.NetworkRequest; 20 import android.net.wifi.aware.PublishConfig; 21 import android.net.wifi.aware.SubscribeConfig; 22 import android.net.wifi.ScanResult; 23 import android.net.wifi.aware.WifiAwareNetworkSpecifier; 24 25 import com.google.android.mobly.snippet.SnippetObjectConverter; 26 import com.google.android.mobly.snippet.util.Log; 27 28 import org.json.JSONException; 29 import org.json.JSONObject; 30 31 import java.lang.reflect.Type; 32 import java.util.List; 33 34 /** 35 * The converter class that allows users to use custom type as snippet RPC arguments and return 36 * values. 37 */ 38 public class WifiAwareSnippetConverter implements SnippetObjectConverter { 39 40 trimQuotationMarks(String originalString)41 public static String trimQuotationMarks(String originalString) { 42 String result = originalString; 43 if (originalString == null) 44 return result; 45 if (originalString.length() > 2 46 && originalString.charAt(0) == '"' 47 && originalString.charAt(originalString.length() - 1) == '"') { 48 result = originalString.substring(1, originalString.length() - 1); 49 } 50 return result; 51 } 52 53 @Override serialize(Object object)54 public JSONObject serialize(Object object) throws JSONException { 55 // If the RPC method requires a custom return type, e.g. SubscribeConfig, PublishConfig, we 56 // need to define it here. 57 // If the object type is not recognized, you can throw an exception or return null 58 // depending on your application's needs. 59 JSONObject result = new JSONObject(); 60 if (object instanceof WifiAwareNetworkSpecifier) { 61 WifiAwareNetworkSpecifier frame = (WifiAwareNetworkSpecifier) object; 62 result.put("result", SerializationUtil.parcelableToString(frame)); 63 return result; 64 } 65 return null; 66 } 67 serializeScanResult(ScanResult data)68 public static JSONObject serializeScanResult(ScanResult data) throws JSONException { 69 JSONObject result = new JSONObject(); 70 result.put("BSSID", data.BSSID); 71 result.put("SSID", trimQuotationMarks(data.getWifiSsid().toString())); 72 result.put("capabilities", data.capabilities); 73 result.put("centerFreq0", data.centerFreq0); 74 result.put("centerFreq1", data.centerFreq1); 75 result.put("channelWidth", data.channelWidth); 76 result.put("frequency", data.frequency); 77 result.put("level", data.level); 78 result.put("operatorFriendlyName", 79 (data.operatorFriendlyName != null) ? data.operatorFriendlyName.toString() : ""); 80 result.put("timestamp", data.timestamp); 81 result.put("venueName", (data.venueName != null) ? data.venueName.toString() : ""); 82 result.put("scan_result_parcel", SerializationUtil.parcelableToString(data)); 83 return result; 84 } 85 86 @Override deserialize(JSONObject jsonObject, Type type)87 public Object deserialize(JSONObject jsonObject, Type type) throws JSONException { 88 // The parameters of Mobly RPC directly reference the Object type. 89 // Here, we need to convert JSONObjects back into specific types. 90 if (type == SubscribeConfig.class) { 91 return WifiAwareJsonDeserializer.jsonToSubscribeConfig(jsonObject); 92 } else if (type == PublishConfig.class) { 93 return WifiAwareJsonDeserializer.jsonToPublishConfig(jsonObject); 94 } else if (type == NetworkRequest.class) { 95 return WifiAwareJsonDeserializer.jsonToNetworkRequest(jsonObject); 96 } 97 // If the type is not recognized, you can throw an exception or return null 98 // depending on your application's needs. 99 return null; 100 } 101 } 102