Merge branch 'cloud-interface'
[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 mProvRep = 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
54     public EnrolleeConf(EnrolleeConf enrolleeConf)
55     {
56         mProvRep = enrolleeConf.getProvResRep();
57     }
58
59     /**
60      * Get Device Name property in DevConf resource
61      *
62      * @return deviceName
63      */
64     public String getDeviceName()
65     {
66         List<OcRepresentation> children = mProvRep.getChildren();
67
68         for (OcRepresentation child : children) {
69             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_DEVCONF) != -1)
70             {
71                 try
72                 {
73                     if(child.hasAttribute(ESConstants.OC_RSRVD_ES_DEVNAME)) {
74                         return (String) child.getValue(ESConstants.OC_RSRVD_ES_DEVNAME);
75                     }
76                 } catch (OcException e) {
77                     Log.e(TAG, "getWiFiModes is failed.");
78                 }
79             }
80         }
81
82         return new String("");
83     }
84
85     /**
86      * Get Model Number property in DevConf resource
87      *
88      * @return modelNumber
89      */
90     public String getModelNumber()
91     {
92         List<OcRepresentation> children = mProvRep.getChildren();
93
94         for (OcRepresentation child : children) {
95             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_DEVCONF) != -1)
96             {
97                 try
98                 {
99                     if(child.hasAttribute(ESConstants.OC_RSRVD_ES_MODELNUMBER)) {
100                         return (String) child.getValue(ESConstants.OC_RSRVD_ES_MODELNUMBER);
101                     }
102                 } catch (OcException e) {
103                     Log.e(TAG, "getModelNumber is failed.");
104                 }
105             }
106         }
107
108         return new String("");
109     }
110
111     /**
112      * Get Supported WiFi Modes property in WiFi resource
113      *
114      * @return a list of WiFi modes
115      */
116     public ArrayList<WIFI_MODE> getWiFiModes()
117     {
118         List<OcRepresentation> children = mProvRep.getChildren();
119         ArrayList<WIFI_MODE> modes = new ArrayList<WIFI_MODE>();
120         for (OcRepresentation child : children) {
121             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_WIFI) != -1)
122             {
123                 try {
124                     if (child.hasAttribute(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIMODE)) {
125                         int modes_int[] = child.getValue(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIMODE);
126                         for (int i = 0 ; i < modes_int.length ; ++i) {
127                             modes.add(WIFI_MODE.fromInt(modes_int[i]));
128                         }
129                     }
130                 } catch (OcException e) {
131                     Log.e(TAG, "getWiFiModes is failed.");
132                 }
133             }
134         }
135
136         return modes;
137     }
138
139     /**
140      * Get Supported WiFi frequency property in WiFi resource
141      *
142      * @return WiFi frequency
143      */
144     public WIFI_FREQ getWiFiFreq()
145     {
146         List<OcRepresentation> children = mProvRep.getChildren();
147
148         for (OcRepresentation child : children) {
149             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_WIFI) != -1)
150             {
151                 try{
152                     if(child.hasAttribute(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
153                         return WIFI_FREQ.fromInt(
154                                 (int)child.getValue(ESConstants.OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
155                 } catch (OcException e) {
156                     Log.e(TAG, "getWiFiFreq is failed.");
157                 }
158             }
159         }
160         return WIFI_FREQ.WIFI_FREQ_NONE;
161     }
162
163     /**
164      * To check if Enrollee can access to cloud. To decide its preference, we check if a cloudserver
165      * resource is registered on Enrollee.
166      *
167      * @return boolean
168      */
169     public boolean isCloudAccessible()
170     {
171         List<OcRepresentation> children = mProvRep.getChildren();
172
173         for (OcRepresentation child : children) {
174             if(child.getUri().indexOf(ESConstants.OC_RSRVD_ES_URI_CLOUDSERVER) != -1)
175             {
176                 return true;
177             }
178         }
179         return false;
180     }
181
182     public  OcRepresentation getProvResRep()
183     {
184         return mProvRep;
185     }
186 }
187
188