Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Connection / Tizen.Network.Connection / CellularProfile.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21 using System.Runtime.InteropServices;
22
23 namespace Tizen.Network.Connection
24 {
25     /// <summary>
26     /// This Class is CellularProfile. It provides functions to manage the cellular profile.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class CellularProfile : ConnectionProfile
30     {
31         internal CellularProfile(IntPtr handle): base(handle)
32         {
33         }
34
35         private CellularAuthInformation _cellularAuthInfo = null;
36
37         ~CellularProfile()
38         {
39         }
40
41         /// <summary>
42         /// The APN (access point name).
43         /// </summary>
44         /// <since_tizen> 3 </since_tizen>
45         /// <value>Cellular access point name.</value>
46         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
47         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
48         /// <exception cref="System.ArgumentNullException">Thrown during set when value is null.</exception>
49         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
50         /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
51         public string Apn
52         {
53             get
54             {
55                 Log.Debug(Globals.LogTag, "Get Apn");
56                 IntPtr Value;
57                 int ret = Interop.ConnectionCellularProfile.GetApn(ProfileHandle, out Value);
58                 if ((ConnectionError)ret != ConnectionError.None)
59                 {
60                     Log.Error(Globals.LogTag, "It failed to get apn, " + (ConnectionError)ret);
61                 }
62                 string result = Marshal.PtrToStringAnsi(Value);
63                 Interop.Libc.Free(Value);
64                 return result;
65             }
66
67             set
68             {
69                 Log.Debug(Globals.LogTag, "Set Apn");
70                 CheckDisposed();
71                 if (value != null)
72                 {
73                     int ret = Interop.ConnectionCellularProfile.SetApn(ProfileHandle, value);
74                     if ((ConnectionError)ret != ConnectionError.None)
75                     {
76                         Log.Error(Globals.LogTag, "It failed to set apn, " + (ConnectionError)ret);
77                         ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
78                         ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
79                         ConnectionErrorFactory.ThrowConnectionException(ret);
80                     }
81                 }
82
83                 else
84                 {
85                     throw new ArgumentNullException("Value of Apn is null");
86                 }
87             }
88         }
89
90         /// <summary>
91         /// The home URL.
92         /// </summary>
93         /// <since_tizen> 3 </since_tizen>
94         /// <value>Cellular home URL.</value>
95         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
96         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
97         /// <exception cref="System.ArgumentNullException">Thrown during set when value is null.</exception>
98         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
99         /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
100         public string HomeUri
101         {
102             get
103             {
104                 Log.Debug(Globals.LogTag, "Get HomeUri");
105                 IntPtr Value;
106                 int ret = Interop.ConnectionCellularProfile.GetHomeUrl(ProfileHandle, out Value);
107                 if ((ConnectionError)ret != ConnectionError.None)
108                 {
109                     Log.Error(Globals.LogTag, "It failed to get home url, " + (ConnectionError)ret);
110                 }
111                 string result = Marshal.PtrToStringAnsi(Value);
112                 Interop.Libc.Free(Value);
113                 return result;
114             }
115
116             set
117             {
118                 Log.Debug(Globals.LogTag, "Set HomeUri");
119                 CheckDisposed();
120                 if (value != null)
121                 {
122                     int ret = Interop.ConnectionCellularProfile.SetHomeUrl(ProfileHandle, value);
123                     if ((ConnectionError)ret != ConnectionError.None)
124                     {
125                         Log.Error(Globals.LogTag, "It failed to set home url, " + (ConnectionError)ret);
126                         ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
127                         ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
128                         ConnectionErrorFactory.ThrowConnectionException(ret);
129                     }
130                 }
131
132                 else
133                 {
134                     throw new ArgumentNullException("Value of HomeUri is null");
135                 }
136             }
137         }
138
139         /// <summary>
140         /// The service type.
141         /// </summary>
142         /// <since_tizen> 3 </since_tizen>
143         /// <value>Cellular service type.</value>
144         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
145         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
146         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
147         /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
148         public CellularServiceType ServiceType
149         {
150             get
151             {
152                 Log.Debug(Globals.LogTag, "Get ServiceType");
153                 int value;
154                 int ret = Interop.ConnectionCellularProfile.GetServiceType(ProfileHandle, out value);
155                 if ((ConnectionError)ret != ConnectionError.None)
156                 {
157                     Log.Error(Globals.LogTag, "It failed to get service type, " + (ConnectionError)ret);
158                 }
159                 return (CellularServiceType)value;
160             }
161
162             set
163             {
164                 Log.Debug(Globals.LogTag, "Set ServiceType");
165                 CheckDisposed();
166                 int ret = Interop.ConnectionCellularProfile.SetServiceType(ProfileHandle, (int)value);
167                 if ((ConnectionError)ret != ConnectionError.None)
168                 {
169                     Log.Error(Globals.LogTag, "It failed to set service type, " + (ConnectionError)ret);
170                     ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
171                     ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
172                     ConnectionErrorFactory.ThrowConnectionException(ret);
173                 }
174             }
175         }
176
177         /// <summary>
178         /// The cellular pdn type.
179         /// </summary>
180         /// <since_tizen> 3 </since_tizen>
181         /// <value>Cellular pdn type.</value>
182         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
183         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
184         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
185         /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
186         public CellularPdnType PdnType
187         {
188             get
189             {
190                 Log.Debug(Globals.LogTag, "Get PdnType");
191                 int value;
192                 int ret = Interop.ConnectionCellularProfile.GetPdnType(ProfileHandle, out value);
193                 if ((ConnectionError)ret != ConnectionError.None)
194                 {
195                     Log.Error(Globals.LogTag, "It failed to get pdn type, " + (ConnectionError)ret);
196                 }
197                 return (CellularPdnType)value;
198             }
199
200             set
201             {
202                 Log.Debug(Globals.LogTag, "Set PdnType");
203                 CheckDisposed();
204                 int ret = Interop.ConnectionCellularProfile.SetPdnType(ProfileHandle, (int)value);
205                 if ((ConnectionError)ret != ConnectionError.None)
206                 {
207                     Log.Error(Globals.LogTag, "It failed to set pdn type, " + (ConnectionError)ret);
208                     ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
209                     ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
210                     ConnectionErrorFactory.ThrowConnectionException(ret);
211                 }
212             }
213         }
214
215         /// <summary>
216         /// The cellular roaming pdn type.
217         /// </summary>
218         /// <since_tizen> 3 </since_tizen>
219         /// <value>Cellular roaming pdn type.</value>
220         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
221         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
222         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
223         /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
224         public CellularPdnType RoamingPdnType
225         {
226             get
227             {
228                 Log.Debug(Globals.LogTag, "Get RoamingPdnType");
229                 int value;
230                 int ret = Interop.ConnectionCellularProfile.GetRoamingPdnType(ProfileHandle, out value);
231                 if ((ConnectionError)ret != ConnectionError.None)
232                 {
233                     Log.Error(Globals.LogTag, "It failed to get roam pdn type, " + (ConnectionError)ret);
234                 }
235                 return (CellularPdnType)value;
236             }
237
238             set
239             {
240                 Log.Debug(Globals.LogTag, "Set RoamingPdnType");
241                 CheckDisposed();
242                 int ret = Interop.ConnectionCellularProfile.SetRoamingPdnType(ProfileHandle, (int)value);
243                 if ((ConnectionError)ret != ConnectionError.None)
244                 {
245                     Log.Error(Globals.LogTag, "It failed to set roam pdn type, " + (ConnectionError)ret);
246                     ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
247                     ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
248                     ConnectionErrorFactory.ThrowConnectionException(ret);
249                 }
250             }
251         }
252
253         /// <summary>
254         /// Cellular Authentication Information.
255         /// </summary>
256         /// <since_tizen> 3 </since_tizen>
257         /// <value>Instance of CellularAuthInformation.</value>
258         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
259         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
260         /// <exception cref="System.ArgumentNullException">Thrown during set when value is null.</exception>
261         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
262         /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
263         public CellularAuthInformation CellularAuthInfo
264         {
265             get
266             {
267                 int type;
268                 string name;
269                 string password;
270                 int ret = Interop.ConnectionCellularProfile.GetAuthInfo(ProfileHandle, out type, out name, out password);
271                 if ((ConnectionError)ret != ConnectionError.None)
272                 {
273                     Log.Error(Globals.LogTag, "It failed to get cellular authentication information, " + (ConnectionError)ret);
274                     return null;
275                 }
276
277                 if (_cellularAuthInfo == null)
278                     _cellularAuthInfo = new CellularAuthInformation();
279                 _cellularAuthInfo.AuthType = (CellularAuthType)type;
280                 _cellularAuthInfo.UserName = name;
281                 _cellularAuthInfo.Password = password;
282                 return _cellularAuthInfo;
283             }
284
285             set
286             {
287                 CheckDisposed();
288                 if (value != null)
289                 {
290                     _cellularAuthInfo = value;
291                     int type = (int)_cellularAuthInfo.AuthType;
292                     string name = _cellularAuthInfo.UserName;
293                     string password = _cellularAuthInfo.Password;
294                     int ret = Interop.ConnectionCellularProfile.SetAuthInfo(ProfileHandle, type, name, password);
295                     if ((ConnectionError)ret != ConnectionError.None)
296                     {
297                         Log.Error(Globals.LogTag, "It failed to set auth information, " + (ConnectionError)ret);
298                         ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
299                         ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
300                         ConnectionErrorFactory.ThrowConnectionException(ret);
301                     }
302                 }
303
304                 else
305                 {
306                     throw new ArgumentNullException("CellularAuthInformation value is null");
307                 }
308             }
309         }
310
311         /// <summary>
312         /// Checks whether the profile is hidden.
313         /// </summary>
314         /// <since_tizen> 3 </since_tizen>
315         /// <value>True if the cellular profile is hidden, otherwise false.</value>
316         public bool Hidden
317         {
318             get
319             {
320                 bool value;
321                 int ret = Interop.ConnectionCellularProfile.IsHidden(ProfileHandle, out value);
322                 if ((ConnectionError)ret != ConnectionError.None)
323                 {
324                     Log.Error(Globals.LogTag, "It failed to get hidden value, " + (ConnectionError)ret);
325                 }
326                 return value;
327             }
328         }
329
330         /// <summary>
331         /// Checks whether the profile is editable.
332         /// </summary>
333         /// <since_tizen> 3 </since_tizen>
334         /// <value>True if the cellular profile is editable, otherwise false.</value>
335         public bool Editable
336         {
337             get
338             {
339                 bool value;
340                 int ret = Interop.ConnectionCellularProfile.IsEditable(ProfileHandle, out value);
341                 if ((ConnectionError)ret != ConnectionError.None)
342                 {
343                     Log.Error(Globals.LogTag, "It failed to get editable value, " + (ConnectionError)ret);
344                 }
345                 return value;
346             }
347         }
348
349         /// <summary>
350         /// Checks whether the profile is default.
351         /// </summary>
352         /// <since_tizen> 3 </since_tizen>
353         /// <value>True if the cellular profile is default, otherwise false.</value>
354         public bool IsDefault
355         {
356             get
357             {
358                 bool value;
359                 int ret = Interop.ConnectionCellularProfile.IsDefault(ProfileHandle, out value);
360                 if ((ConnectionError)ret != ConnectionError.None)
361                 {
362                     Log.Error(Globals.LogTag, "It failed to get IsDefault value, " + (ConnectionError)ret);
363                 }
364                 return value;
365             }
366         }
367     }
368
369     /// <summary>
370     /// This Class is CellularAuthInformation. It provides the properties to get and set the cellular authentication information.
371     /// </summary>
372     /// <since_tizen> 3 </since_tizen>
373     public class CellularAuthInformation
374     {
375         /// <summary>
376         /// Default Constructor.Initializes an object of CellularAuthInformation.
377         /// </summary>
378         public CellularAuthInformation()
379         {
380         }
381
382         /// <summary>
383         /// The user name.
384         /// <since_tizen> 3 </since_tizen>
385         /// </summary>
386         /// <value>Cellular user name.</value>
387         public string UserName { get; set;}
388         /// <summary>
389         /// The password
390         /// </summary>
391         /// <since_tizen> 3 </since_tizen>
392         /// <value>Cellular password.</value>
393         public string Password { get; set; }
394
395         /// <summary>
396         /// The authentication type
397         /// </summary>
398         /// <since_tizen> 3 </since_tizen>
399         /// <value>Cellular authentication type.</value>
400         public CellularAuthType AuthType { get; set; }
401     }
402 }