package org.iotivity.service.easysetup.mediator;
+import android.util.Log;
+
+import org.iotivity.base.OcException;
+import org.iotivity.base.OcRepresentation;
+import org.iotivity.service.easysetup.mediator.ESConstants;
import org.iotivity.service.easysetup.mediator.enums.WIFI_FREQ;
import org.iotivity.service.easysetup.mediator.enums.WIFI_MODE;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
/**
* This class stores Enrollee's configuration including WiFi and Device configuration
*/
public class EnrolleeConf
{
- private final DeviceConfig mDevConf;
- private final WiFiConfig mWiFiConf;
- private final boolean mCloudable;
-
+ private static final String TAG = EnrolleeConf.class.getName();
+ protected OcRepresentation mEasySetupRep = null;
/**
* Constructor
*
- * @param devConf
- * device name, language, and country
- * @param wifiConf
- * supported WiFi modes and frequency
- * @param cloudable
- * a preference if Enrollee is supposed to be registered to Cloud
+ * @param rep received properties in a form of OcRepresentation
+ *
*/
- public EnrolleeConf(DeviceConfig devConf, WiFiConfig wifiConf, boolean cloudable)
+ public EnrolleeConf(OcRepresentation rep)
+ {
+ mEasySetupRep = rep;
+ }
+
+ public EnrolleeConf(EnrolleeConf enrolleeConf)
{
- mDevConf = devConf;
- mWiFiConf = wifiConf;
- mCloudable = cloudable;
+ mEasySetupRep = enrolleeConf.getEasySetupRep();
}
/**
- * Get device configuration
+ * Get Device Name property in DevConf resource
*
- * @return DeviceConfig
- * device name, language, and country
+ * @return deviceName
+ */
+ public String getDeviceName()
+ {
+ if(mEasySetupRep == null)
+ {
+ return null;
+ }
+
+ List<OcRepresentation> children = mEasySetupRep.getChildren();
+
+ for (OcRepresentation child : children) {
+ if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_DEVCONF) != -1)
+ {
+ try
+ {
+ OcRepresentation rep;
+ if(child.hasAttribute(ESConstants.OC_RSRVD_REPRESENTATION))
+ {
+ rep = (OcRepresentation)child.getValue(ESConstants.OC_RSRVD_REPRESENTATION);
+ }
+ else
+ {
+ return null;
+ }
+
+ if(rep.hasAttribute(ESConstants.OC_RSRVD_ES_DEVNAME)) {
+ return (String) rep.getValue(ESConstants.OC_RSRVD_ES_DEVNAME);
+ }
+ } catch (OcException e) {
+ Log.e(TAG, "getWiFiModes is failed.");
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Get Model Number property in DevConf resource
*
- * @see DeviceConfig
+ * @return modelNumber
*/
- public DeviceConfig getDeviceConfig()
+ public String getModelNumber()
{
- return mDevConf;
+ if(mEasySetupRep == null)
+ {
+ return null;
+ }
+
+ List<OcRepresentation> children = mEasySetupRep.getChildren();
+
+ for (OcRepresentation child : children) {
+ if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_DEVCONF) != -1)
+ {
+ try
+ {
+ OcRepresentation rep;
+ if(child.hasAttribute(ESConstants.OC_RSRVD_REPRESENTATION))
+ {
+ rep = (OcRepresentation)child.getValue(ESConstants.OC_RSRVD_REPRESENTATION);
+ }
+ else
+ {
+ return null;
+ }
+
+ if(rep.hasAttribute(ESConstants.OC_RSRVD_ES_MODELNUMBER)) {
+ return (String) rep.getValue(ESConstants.OC_RSRVD_ES_MODELNUMBER);
+ }
+ } catch (OcException e) {
+ Log.e(TAG, "getModelNumber is failed.");
+ }
+ }
+ }
+ return null;
}
/**
- * Get WiFi configuration
+ * Get Supported WiFi Modes property in WiFi resource
*
- * @return WiFiConfig
- * supported WiFi modes and frequency
+ * @return a list of WiFi modes
+ */
+ public ArrayList<WIFI_MODE> getWiFiModes()
+ {
+ if(mEasySetupRep == null)
+ {
+ return null;
+ }
+
+ List<OcRepresentation> children = mEasySetupRep.getChildren();
+ ArrayList<WIFI_MODE> modes = new ArrayList<WIFI_MODE>();
+ for (OcRepresentation child : children) {
+ if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_WIFICONF) != -1)
+ {
+ try {
+ OcRepresentation rep;
+ if(child.hasAttribute(ESConstants.OC_RSRVD_REPRESENTATION))
+ {
+ rep = (OcRepresentation)child.getValue(ESConstants.OC_RSRVD_REPRESENTATION);
+ }
+ else
+ {
+ return null;
+ }
+
+ if (rep.hasAttribute(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIMODE)) {
+ int modes_int[] = rep.getValue(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIMODE);
+ for (int i = 0 ; i < modes_int.length ; ++i) {
+ modes.add(WIFI_MODE.fromInt(modes_int[i]));
+ }
+ }
+ } catch (OcException e) {
+ Log.e(TAG, "getWiFiModes is failed.");
+ }
+ }
+ }
+ return modes;
+ }
+
+ /**
+ * Get Supported WiFi frequency property in WiFi resource
*
- * @see WiFiConfig
+ * @return WiFi frequency
*/
- public WiFiConfig getWiFiConfig()
+ public WIFI_FREQ getWiFiFreq()
{
- return mWiFiConf;
+ if(mEasySetupRep == null)
+ {
+ return WIFI_FREQ.WIFI_FREQ_NONE;
+ }
+
+ List<OcRepresentation> children = mEasySetupRep.getChildren();
+
+ for (OcRepresentation child : children) {
+ if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_WIFICONF) != -1)
+ {
+ try{
+ OcRepresentation rep;
+ if(child.hasAttribute(ESConstants.OC_RSRVD_REPRESENTATION))
+ {
+ rep = (OcRepresentation)child.getValue(ESConstants.OC_RSRVD_REPRESENTATION);
+ }
+ else
+ {
+ return null;
+ }
+
+ if(rep.hasAttribute(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
+ return WIFI_FREQ.fromInt(
+ (int)rep.getValue(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
+ } catch (OcException e) {
+ Log.e(TAG, "getWiFiFreq is failed.");
+ }
+ }
+ }
+ return null;
}
/**
- * Get a preference of cloud registration of Enrollee
+ * To check if Enrollee can access to cloud. To decide its preference, we check if a cloudserver
+ * resource is registered on Enrollee.
*
- * @return true if Enrollee is supposed to be registered to Cloud
+ * @return boolean
*/
- public boolean isCloudable()
+ public boolean isCloudAccessible()
+ {
+ if(mEasySetupRep == null)
+ {
+ return false;
+ }
+
+ List<OcRepresentation> children = mEasySetupRep.getChildren();
+
+ for (OcRepresentation child : children) {
+ if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_COAPCLOUDCONF) != -1)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public OcRepresentation getEasySetupRep()
{
- return mCloudable;
+ return mEasySetupRep;
}
}