Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / java / org / iotivity / service / easysetup / mediator / EnrolleeConf.java
1 /**
2  * ***************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ****************************************************************
19  */
20
21 package org.iotivity.service.easysetup.mediator;
22
23 import android.util.Log;
24
25 import org.iotivity.base.OcException;
26 import org.iotivity.base.OcRepresentation;
27 import org.iotivity.service.easysetup.mediator.ESConstants;
28 import org.iotivity.service.easysetup.mediator.enums.WIFI_FREQ;
29 import org.iotivity.service.easysetup.mediator.enums.WIFI_MODE;
30
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 /**
36  * This class stores Enrollee's configuration including WiFi and Device configuration
37  * including supported WiFi frequency and device name
38  */
39 public class EnrolleeConf
40 {
41     private static final String TAG = EnrolleeConf.class.getName();
42     private OcRepresentation mProvRep = null, mWiFiRep = null, mDevConfRep = null, mCloudRep = null;
43     /**
44      * Constructor
45      *
46      * @param rep received properties in a form of OcRepresentation
47      *
48      */
49     public EnrolleeConf(OcRepresentation rep)
50     {
51         mProvRep = rep;
52
53         List<OcRepresentation> children = rep.getChildren();
54
55         for (OcRepresentation child : children) {
56             List<String> rts = child.getResourceTypes();
57
58             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_WIFI) != -1)
59             {
60                 mWiFiRep = child;
61             }
62             else if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_DEVCONF) != -1)
63             {
64                 mDevConfRep = child;
65             }
66             else if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_CLOUDSERVER) != -1)
67             {
68                 mCloudRep = child;
69             }
70         }
71     }
72
73     /**
74      * Get Device Name property in DevConf resource
75      *
76      * @return deviceName
77      */
78     public String getDeviceName()
79     {
80         try
81         {
82             if(mDevConfRep != null && mDevConfRep.hasAttribute(ESConstants.OC_RSRVD_ES_DEVNAME)) {
83                 return (String) mDevConfRep.getValue(ESConstants.OC_RSRVD_ES_DEVNAME);
84             }
85         } catch (OcException e) {
86                 Log.e(TAG, "getWiFiModes is failed.");
87         }
88         return new String("");
89     }
90
91     /**
92      * Get Model Number property in DevConf resource
93      *
94      * @return modelNumber
95      */
96     public String getModelNumber()
97     {
98         try
99         {
100             if(mDevConfRep != null && mDevConfRep.hasAttribute(ESConstants.OC_RSRVD_ES_MODELNUMBER)) {
101                 return (String) mDevConfRep.getValue(ESConstants.OC_RSRVD_ES_MODELNUMBER);
102             }
103         } catch (OcException e) {
104                 Log.e(TAG, "getModelNumber is failed.");
105         }
106         return new String("");
107     }
108
109     /**
110      * Get Supported WiFi Modes property in WiFi resource
111      *
112      * @return a list of WiFi modes
113      */
114     public ArrayList<WIFI_MODE> getWiFiModes()
115     {
116         ArrayList<WIFI_MODE> modes = new ArrayList<WIFI_MODE>();
117         try {
118             if (mWiFiRep != null && mWiFiRep.hasAttribute(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIMODE)) {
119                 int modes_int[] = mWiFiRep.getValue(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIMODE);
120                 for (int i = 0 ; i < modes_int.length ; ++i) {
121                     modes.add(WIFI_MODE.fromInt(modes_int[i]));
122                 }
123             }
124         } catch (OcException e) {
125             Log.e(TAG, "getWiFiModes is failed.");
126         }
127         return modes;
128     }
129
130     /**
131      * Get Supported WiFi frequency property in WiFi resource
132      *
133      * @return WiFi frequency
134      */
135     public WIFI_FREQ getWiFiFreq()
136     {
137         try{
138             if(mWiFiRep != null && mWiFiRep.hasAttribute(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
139                 return WIFI_FREQ.fromInt(
140                         (int)mWiFiRep.getValue(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
141         } catch (OcException e) {
142             Log.e(TAG, "getWiFiFreq is failed.");
143         }
144         return WIFI_FREQ.WIFI_FREQ_NONE;
145     }
146
147     /**
148      * To check if Enrollee can access to cloud. To decide its preference, we check if a cloudserver
149      * resource is registered on Enrollee.
150      *
151      * @return boolean
152      */
153     public boolean isCloudAccessible()
154     {
155         if(mCloudRep != null && mCloudRep.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_CLOUDSERVER) != -1)
156             return true;
157
158         return false;
159     }
160 }
161
162