replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / java / org / iotivity / service / easysetup / mediator / DeviceProp.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.service.easysetup.mediator.enums.WIFI_AUTHTYPE;
26 import org.iotivity.service.easysetup.mediator.enums.WIFI_ENCTYPE;
27
28 import org.iotivity.base.OcException;
29 import org.iotivity.base.OcRepresentation;
30
31 /**
32  * This class contains device properties to be delivered to Enrollee
33  */
34 public class DeviceProp {
35     private static final String TAG = DeviceProp.class.getName();
36     protected OcRepresentation mRep = null;
37
38     /**
39      * Constructor
40      */
41     public DeviceProp() {
42         mRep = new OcRepresentation();
43     }
44
45     public void setWiFiProp(String ssid, String pwd, WIFI_AUTHTYPE authtype, WIFI_ENCTYPE enctype)
46     {
47         if(ssid == null)
48         {
49             ssid = "";
50         }
51         if(pwd == null)
52         {
53             pwd = "";
54         }
55         try
56         {
57             mRep.setValue(ESConstants.OC_RSRVD_ES_SSID, ssid);
58             mRep.setValue(ESConstants.OC_RSRVD_ES_CRED, pwd);
59             mRep.setValue(ESConstants.OC_RSRVD_ES_AUTHTYPE, authtype.getValue());
60             mRep.setValue(ESConstants.OC_RSRVD_ES_ENCTYPE, enctype.getValue());
61         } catch (OcException e) {
62             Log.e(TAG, "setWiFiProp is failed.");
63         }
64     }
65
66     public void setDevConfProp(String language, String country, String location)
67     {
68         if(language == null)
69         {
70             language = "";
71         }
72         if(country == null)
73         {
74             country = "";
75         }
76         if(location == null)
77         {
78             location = "";
79         }
80         try {
81             mRep.setValue(ESConstants.OC_RSRVD_ES_LANGUAGE, language);
82             mRep.setValue(ESConstants.OC_RSRVD_ES_COUNTRY, country);
83             mRep.setValue(ESConstants.OC_RSRVD_ES_LOCATION, location);
84         } catch (OcException e) {
85             Log.e(TAG, "setDevConfProp is failed.");
86         }
87     }
88
89     /**
90      * Get WiFi AP's SSID
91      *
92      * @return String WiFi AP's SSID
93      */
94     public String getSsid()
95     {
96         if(mRep == null)
97         {
98             return null;
99         }
100
101         try
102         {
103             if(mRep.hasAttribute(ESConstants.OC_RSRVD_ES_SSID))
104                 return mRep.getValue(ESConstants.OC_RSRVD_ES_SSID);
105         }
106         catch (OcException e)
107         {
108             Log.e(TAG, "getSsid is failed.");
109         }
110         return null;
111     }
112
113     /**
114      * Get WiFi AP's password
115      *
116      * @return String WiFi AP's password
117      */
118     public String getPassword()
119     {
120         if(mRep == null)
121         {
122             return null;
123         }
124
125         try
126         {
127             if(mRep.hasAttribute(ESConstants.OC_RSRVD_ES_CRED))
128                 return mRep.getValue(ESConstants.OC_RSRVD_ES_CRED);
129         }
130         catch (OcException e)
131         {
132             Log.e(TAG, "getPassword is failed.");
133         }
134         return null;
135     }
136
137     /**
138      * Get WiFi AP's authenticate type
139      * NONE_AUTH(0), WEP(1), WPA_PSK(2), WPA2_PSK(3)
140      *
141      * @return WIFI_AUTHTYPE WiFi AP's authenticate type
142      */
143     public WIFI_AUTHTYPE getAuthType()
144     {
145         if(mRep == null)
146         {
147             return WIFI_AUTHTYPE.NONE_AUTH;
148         }
149
150         try
151         {
152             if (mRep.hasAttribute(ESConstants.OC_RSRVD_ES_AUTHTYPE))
153                 return WIFI_AUTHTYPE.fromInt((int)mRep.getValue(ESConstants.OC_RSRVD_ES_AUTHTYPE));
154         }
155         catch (OcException e)
156         {
157             Log.e(TAG, "getAuthType is failed.");
158         }
159         return WIFI_AUTHTYPE.NONE_AUTH;
160     }
161
162     /**
163      * Get WiFi AP's encryption type
164      * NONE_ENC(0), WEP_64(1), WEP_128(2), TKIP(3), AES(4), TKIP_AES(5)
165      *
166      * @return WIFI_ENCTYPE WiFi AP's encryption type
167      */
168     public WIFI_ENCTYPE getEncType()
169     {
170         if(mRep == null)
171         {
172             return WIFI_ENCTYPE.NONE_ENC;
173         }
174
175         try
176         {
177             if (mRep.hasAttribute(ESConstants.OC_RSRVD_ES_ENCTYPE))
178                 return WIFI_ENCTYPE.fromInt((int)mRep.getValue(ESConstants.OC_RSRVD_ES_ENCTYPE));
179         }
180         catch (OcException e)
181         {
182             Log.e(TAG, "getEncType is failed.");
183         }
184         return WIFI_ENCTYPE.NONE_ENC;
185     }
186
187     /**
188      * Get IETF language tag using ISO 639X
189      *
190      * @return String IETF language tag using ISO 639X
191      */
192     public String getLanguage()
193     {
194         if(mRep == null)
195         {
196             return null;
197         }
198
199         try
200         {
201             if(mRep.hasAttribute(ESConstants.OC_RSRVD_ES_LANGUAGE))
202                 return mRep.getValue(ESConstants.OC_RSRVD_ES_LANGUAGE);
203         }
204         catch (OcException e)
205         {
206             Log.e(TAG, "getLanguage is failed.");
207         }
208         return null;
209     }
210
211     /**
212      * Get ISO Country Code (ISO 3166-1 Alpha-2)
213      *
214      * @return String ISO Country Code (ISO 3166-1 Alpha-2)
215      */
216     public String getCountry()
217     {
218         if(mRep == null)
219         {
220             return null;
221         }
222
223         try
224         {
225             if (mRep.hasAttribute(ESConstants.OC_RSRVD_ES_COUNTRY))
226                 return mRep.getValue(ESConstants.OC_RSRVD_ES_COUNTRY);
227         }
228         catch (OcException e)
229         {
230             Log.e(TAG, "getCountry is failed.");
231         }
232         return null;
233     }
234
235     /**
236      * Get location info
237      *
238      * @return String location info of GPS
239      */
240     public String getLocation()
241     {
242         if(mRep == null)
243         {
244             return null;
245         }
246
247         try
248         {
249             if (mRep.hasAttribute(ESConstants.OC_RSRVD_ES_LOCATION))
250                 return mRep.getValue(ESConstants.OC_RSRVD_ES_LOCATION);
251         }
252         catch (OcException e)
253         {
254             Log.e(TAG, "getLocation is failed.");
255         }
256         return null;
257     }
258
259     public OcRepresentation toOCRepresentation()
260     {
261         return mRep;
262     }
263 }