[C# Connection] Adding C# Connection code
[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
27     /// </summary>
28     public class CellularProfile : ConnectionProfile
29     {
30         private CellularAuthInformation AuthInfo;
31
32         internal CellularProfile(IntPtr handle): base(handle)
33         {
34             AuthInfo = new CellularAuthInformation(handle);
35         }
36
37         ~CellularProfile()
38         {
39         }
40
41         /// <summary>
42         /// Gets the APN (access point name).
43         /// </summary>
44         public string Apn
45         {
46             get
47             {
48                 IntPtr Value;
49                 int ret = Interop.ConnectionCellularProfile.GetApn(ProfileHandle, out Value);
50                 if ((ConnectionError)ret != ConnectionError.None)
51                 {
52                     Log.Error(Globals.LogTag, "It failed to get apn, " + (ConnectionError)ret);
53                 }
54                 string result = Marshal.PtrToStringAnsi(Value);
55                 Interop.Libc.Free(Value);
56                 return result;
57             }
58             set
59             {
60                 int ret = Interop.ConnectionCellularProfile.SetApn(ProfileHandle, (string)value);
61                 if ((ConnectionError)ret != ConnectionError.None)
62                 {
63                     Log.Error(Globals.LogTag, "It failed to set apn, " + (ConnectionError)ret);
64                     ConnectionErrorFactory.ThrowConnectionException(ret);
65                 }
66             }
67         }
68
69         /// <summary>
70         /// Gets the home URL.
71         /// </summary>
72         public string HomeUri
73         {
74             get
75             {
76                 IntPtr Value;
77                 int ret = Interop.ConnectionCellularProfile.GetHomeUrl(ProfileHandle, out Value);
78                 if ((ConnectionError)ret != ConnectionError.None)
79                 {
80                     Log.Error(Globals.LogTag, "It failed to get home url, " + (ConnectionError)ret);
81                 }
82                 string result = Marshal.PtrToStringAnsi(Value);
83                 Interop.Libc.Free(Value);
84                 return result;
85             }
86             set
87             {
88                 int ret = Interop.ConnectionCellularProfile.SetHomeUrl(ProfileHandle, (string)value);
89                 if ((ConnectionError)ret != ConnectionError.None)
90                 {
91                     Log.Error(Globals.LogTag, "It failed to set home url, " + (ConnectionError)ret);
92                     ConnectionErrorFactory.ThrowConnectionException(ret);
93                 }
94
95             }
96         }
97
98         /// <summary>
99         /// Gets the service type.
100         /// </summary>
101         public CellularServiceType ServiceType
102         {
103             get
104             {
105                 int value;
106                 int ret = Interop.ConnectionCellularProfile.GetServiceType(ProfileHandle, out value);
107                 if ((ConnectionError)ret != ConnectionError.None)
108                 {
109                     Log.Error(Globals.LogTag, "It failed to get service type, " + (ConnectionError)ret);
110                 }
111                 return (CellularServiceType)value;
112             }
113             set
114             {
115                 int ret = Interop.ConnectionCellularProfile.SetServiceType(ProfileHandle, (int)value);
116                 if ((ConnectionError)ret != ConnectionError.None)
117                 {
118                     Log.Error(Globals.LogTag, "It failed to set service type, " + (ConnectionError)ret);
119                     ConnectionErrorFactory.ThrowConnectionException(ret);
120                 }
121             }
122         }
123
124         /// <summary>
125         /// Gets cellular Authentification Information.
126         /// </summary>
127         public CellularAuthInformation CellularAuthInfo
128         {
129             get
130             {
131                 return AuthInfo;
132             }
133         }
134
135         /// <summary>
136         /// Checks whether the profile is hidden.
137         /// </summary>
138         public bool Hidden
139         {
140             get
141             {
142                 bool value;
143                 int ret = Interop.ConnectionCellularProfile.IsHidden(ProfileHandle, out value);
144                 if ((ConnectionError)ret != ConnectionError.None)
145                 {
146                     Log.Error(Globals.LogTag, "It failed to get hidden value, " + (ConnectionError)ret);
147                 }
148                 return value;
149             }
150         }
151
152         /// <summary>
153         /// Checks whether the profile is editable.
154         /// </summary>
155         public bool Editable
156         {
157             get
158             {
159                 bool value;
160                 int ret = Interop.ConnectionCellularProfile.IsEditable(ProfileHandle, out value);
161                 if ((ConnectionError)ret != ConnectionError.None)
162                 {
163                     Log.Error(Globals.LogTag, "It failed to get editable value, " + (ConnectionError)ret);
164                 }
165                 return value;
166             }
167         }
168
169         /// <summary>
170         /// Checks whether the profile is default.
171         /// </summary>
172         public bool IsDefault
173         {
174             get
175             {
176                 bool value;
177                 int ret = Interop.ConnectionCellularProfile.IsDefault(ProfileHandle, out value);
178                 if ((ConnectionError)ret != ConnectionError.None)
179                 {
180                     Log.Error(Globals.LogTag, "It failed to get IsDefault value, " + (ConnectionError)ret);
181                 }
182                 return value;
183             }
184         }
185     }
186
187     /// <summary>
188     /// The authentication information.
189     /// </summary>
190     public class CellularAuthInformation
191     {
192         private IntPtr ProfileHandle;
193
194         private string Name = "";
195         private string Passwd = "";
196         private CellularAuthType AuthenType = CellularAuthType.None;
197
198         internal CellularAuthInformation(IntPtr handle)
199         {
200             ProfileHandle = handle;
201         }
202
203         /// <summary>
204         /// The user name.
205         /// </summary>
206         public string UserName
207         {
208             get
209             {
210                 int type;
211                 IntPtr name;
212                 IntPtr password;
213
214                 int ret = Interop.ConnectionCellularProfile.GetAuthInfo(ProfileHandle, out type, out name, out password);
215                 if ((ConnectionError)ret != ConnectionError.None)
216                 {
217                     Log.Error(Globals.LogTag, "It failed to get auth information, " + (ConnectionError)ret);
218                 }
219
220                 Name = Marshal.PtrToStringAnsi(name);
221                 Passwd = Marshal.PtrToStringAnsi(name);
222                 AuthenType = (CellularAuthType)type;
223
224                 Interop.Libc.Free(name);
225                 Interop.Libc.Free(password);
226
227                 return Name;
228             }
229             set
230             {
231                 Name = value;
232                 int ret = Interop.ConnectionCellularProfile.SetAuthInfo(ProfileHandle, (int)AuthenType, (string)value, Passwd);
233                 Log.Error(Globals.LogTag, "UserName : "+ value);
234                 if ((ConnectionError)ret != ConnectionError.None)
235                 {
236                     Log.Error(Globals.LogTag, "It failed to set auth information, " + (ConnectionError)ret);
237                     ConnectionErrorFactory.ThrowConnectionException(ret);
238                 }
239             }
240         }
241
242         /// <summary>
243         /// The password
244         /// </summary>
245         public string Password
246         {
247             get
248             {
249                 int type;
250                 IntPtr name;
251                 IntPtr password;
252
253                 int ret = Interop.ConnectionCellularProfile.GetAuthInfo(ProfileHandle, out type, out name, out password);
254                 if ((ConnectionError)ret != ConnectionError.None)
255                 {
256                     Log.Error(Globals.LogTag, "It failed to get auth information, " + (ConnectionError)ret);
257                 }
258                 Name = Marshal.PtrToStringAnsi(name);
259                 Passwd = Marshal.PtrToStringAnsi(password);
260                 AuthenType = (CellularAuthType)type;
261
262                 Interop.Libc.Free(name);
263                 Interop.Libc.Free(password);
264
265                 return Passwd;
266             }
267             set
268             {
269                 Passwd = value;
270                 int ret = Interop.ConnectionCellularProfile.SetAuthInfo(ProfileHandle, (int)AuthenType, Name, (string)value);
271                 if ((ConnectionError)ret != ConnectionError.None)
272                 {
273                     Log.Error(Globals.LogTag, "It failed to set auth information, " + (ConnectionError)ret);
274                     ConnectionErrorFactory.ThrowConnectionException(ret);
275                 }
276             }
277         }
278
279         /// <summary>
280         /// The authentication type
281         /// </summary>
282         public CellularAuthType AuthType
283         {
284             get
285             {
286                 int type;
287                 IntPtr name;
288                 IntPtr password;
289
290                 int ret = Interop.ConnectionCellularProfile.GetAuthInfo(ProfileHandle, out type, out name, out password);
291                 if ((ConnectionError)ret != ConnectionError.None)
292                 {
293                     Log.Error(Globals.LogTag, "It failed to get auth information, " + (ConnectionError)ret);
294                 }
295
296                 Name = Marshal.PtrToStringAnsi(name);
297                 Passwd = Marshal.PtrToStringAnsi(name);
298                 AuthenType = (CellularAuthType)type;
299
300                 Interop.Libc.Free(name);
301                 Interop.Libc.Free(password);
302                 return AuthenType;
303             }
304             set
305             {
306                 AuthenType = value;
307                 int ret = Interop.ConnectionCellularProfile.SetAuthInfo(ProfileHandle, (int)value, Name, Passwd);
308                 if ((ConnectionError)ret != ConnectionError.None)
309                 {
310                     Log.Error(Globals.LogTag, "It failed to set auth information, " + (ConnectionError)ret);
311                     ConnectionErrorFactory.ThrowConnectionException(ret);
312                 }
313             }
314         }
315     }
316 }