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