replace : iotivity -> iotivity-sec
[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     protected OcRepresentation mEasySetupRep = null;
43     /**
44      * Constructor
45      *
46      * @param rep received properties in a form of OcRepresentation
47      *
48      */
49     public EnrolleeConf(OcRepresentation rep)
50     {
51         mEasySetupRep = rep;
52     }
53
54     public EnrolleeConf(EnrolleeConf enrolleeConf)
55     {
56         mEasySetupRep = enrolleeConf.getEasySetupRep();
57     }
58
59     /**
60      * Get Device Name property in DevConf resource
61      *
62      * @return deviceName
63      */
64     public String getDeviceName()
65     {
66         if(mEasySetupRep == null)
67         {
68             return null;
69         }
70
71         List<OcRepresentation> children = mEasySetupRep.getChildren();
72
73         for (OcRepresentation child : children) {
74             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_DEVCONF) != -1)
75             {
76                 try
77                 {
78                     OcRepresentation rep;
79                     if(child.hasAttribute(ESConstants.OC_RSRVD_REPRESENTATION))
80                     {
81                         rep = (OcRepresentation)child.getValue(ESConstants.OC_RSRVD_REPRESENTATION);
82                     }
83                     else
84                     {
85                         return null;
86                     }
87
88                     if(rep.hasAttribute(ESConstants.OC_RSRVD_ES_DEVNAME)) {
89                         return (String) rep.getValue(ESConstants.OC_RSRVD_ES_DEVNAME);
90                     }
91                 } catch (OcException e) {
92                     Log.e(TAG, "getWiFiModes is failed.");
93                 }
94             }
95         }
96         return null;
97     }
98
99     /**
100      * Get Model Number property in DevConf resource
101      *
102      * @return modelNumber
103      */
104     public String getModelNumber()
105     {
106         if(mEasySetupRep == null)
107         {
108             return null;
109         }
110
111         List<OcRepresentation> children = mEasySetupRep.getChildren();
112
113         for (OcRepresentation child : children) {
114             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_DEVCONF) != -1)
115             {
116                 try
117                 {
118                     OcRepresentation rep;
119                     if(child.hasAttribute(ESConstants.OC_RSRVD_REPRESENTATION))
120                     {
121                         rep = (OcRepresentation)child.getValue(ESConstants.OC_RSRVD_REPRESENTATION);
122                     }
123                     else
124                     {
125                         return null;
126                     }
127
128                     if(rep.hasAttribute(ESConstants.OC_RSRVD_ES_MODELNUMBER)) {
129                         return (String) rep.getValue(ESConstants.OC_RSRVD_ES_MODELNUMBER);
130                     }
131                 } catch (OcException e) {
132                     Log.e(TAG, "getModelNumber is failed.");
133                 }
134             }
135         }
136         return null;
137     }
138
139     /**
140      * Get Supported WiFi Modes property in WiFi resource
141      *
142      * @return a list of WiFi modes
143      */
144     public ArrayList<WIFI_MODE> getWiFiModes()
145     {
146         if(mEasySetupRep == null)
147         {
148             return null;
149         }
150
151         List<OcRepresentation> children = mEasySetupRep.getChildren();
152         ArrayList<WIFI_MODE> modes = new ArrayList<WIFI_MODE>();
153         for (OcRepresentation child : children) {
154             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_WIFICONF) != -1)
155             {
156                 try {
157                     OcRepresentation rep;
158                     if(child.hasAttribute(ESConstants.OC_RSRVD_REPRESENTATION))
159                     {
160                         rep = (OcRepresentation)child.getValue(ESConstants.OC_RSRVD_REPRESENTATION);
161                     }
162                     else
163                     {
164                         return null;
165                     }
166
167                     if (rep.hasAttribute(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIMODE)) {
168                         int modes_int[] = rep.getValue(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIMODE);
169                         for (int i = 0 ; i < modes_int.length ; ++i) {
170                             modes.add(WIFI_MODE.fromInt(modes_int[i]));
171                         }
172                     }
173                 } catch (OcException e) {
174                     Log.e(TAG, "getWiFiModes is failed.");
175                 }
176             }
177         }
178         return modes;
179     }
180
181     /**
182      * Get Supported WiFi frequency property in WiFi resource
183      *
184      * @return WiFi frequency
185      */
186     public WIFI_FREQ getWiFiFreq()
187     {
188         if(mEasySetupRep == null)
189         {
190             return WIFI_FREQ.WIFI_FREQ_NONE;
191         }
192
193         List<OcRepresentation> children = mEasySetupRep.getChildren();
194
195         for (OcRepresentation child : children) {
196             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_WIFICONF) != -1)
197             {
198                 try{
199                     OcRepresentation rep;
200                     if(child.hasAttribute(ESConstants.OC_RSRVD_REPRESENTATION))
201                     {
202                         rep = (OcRepresentation)child.getValue(ESConstants.OC_RSRVD_REPRESENTATION);
203                     }
204                     else
205                     {
206                         return null;
207                     }
208
209                     if(rep.hasAttribute(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
210                         return WIFI_FREQ.fromInt(
211                                 (int)rep.getValue(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
212                 } catch (OcException e) {
213                     Log.e(TAG, "getWiFiFreq is failed.");
214                 }
215             }
216         }
217         return null;
218     }
219
220     /**
221      * To check if Enrollee can access to cloud. To decide its preference, we check if a cloudserver
222      * resource is registered on Enrollee.
223      *
224      * @return boolean
225      */
226     public boolean isCloudAccessible()
227     {
228         if(mEasySetupRep == null)
229         {
230             return false;
231         }
232
233         List<OcRepresentation> children = mEasySetupRep.getChildren();
234
235         for (OcRepresentation child : children) {
236             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_COAPCLOUDCONF) != -1)
237             {
238                 return true;
239             }
240         }
241         return false;
242     }
243
244     public  OcRepresentation getEasySetupRep()
245     {
246         return mEasySetupRep;
247     }
248 }
249
250