[C# Connection] Adding C# Connection code
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Connection / Tizen.Network.Connection / RequestCellularProfile.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.Threading.Tasks;
22 using System.Runtime.InteropServices;
23
24
25 namespace Tizen.Network.Connection
26 {
27     /// <summary>
28     /// This Class is RequestCellularProfile.
29     /// CellularServiceType should be set before AddProfile method of ConnectionProfileManager is called.
30     /// </summary>
31     public class RequestCellularProfile : RequestProfile
32     {
33         internal IntPtr ProfileHandle = IntPtr.Zero;
34         private IAddressInformation Ipv4;
35         private IAddressInformation Ipv6;
36         private bool disposed = false;
37
38         private CellularAuthInformation AuthInfo;
39         /// <summary>
40         /// The constructor of CellularProfile class with profile type and keyword.
41         /// </summary>
42         /// <privilege>http://tizen.org/privilege/network.get</privilege>
43         public RequestCellularProfile(string keyword)
44         {
45             Log.Debug(Globals.LogTag, "RequestCellularProfile : " + keyword);
46             ProfileHandle = ConnectionInternalManager.CreateRequestProfile(ConnectionProfileType.Cellular, keyword);
47             Log.Debug(Globals.LogTag, "RequestCellularProfile is created : " + ProfileHandle);
48
49             Ipv4 = AddressFactory.CreateAddressInformation(ProfileHandle, AddressFamily.Ipv4, AddressInformationType.Connection);
50             Ipv6 = AddressFactory.CreateAddressInformation(ProfileHandle, AddressFamily.Ipv6, AddressInformationType.Connection);
51
52             AuthInfo = new CellularAuthInformation(ProfileHandle);
53         }
54
55         /// <summary>
56         /// The destructor of CellularProfile class
57         /// </summary>
58         ~RequestCellularProfile()
59         {
60             Dispose(false);
61         }
62
63         public void Dispose()
64         {
65             Dispose(true);
66             GC.SuppressFinalize(this);
67         }
68
69         private void Dispose(bool disposing)
70         {
71             if (disposed)
72                 return;
73
74             if (disposing)
75             {
76                 // Free managed objects.
77             }
78             Interop.ConnectionProfile.Destroy(ProfileHandle);
79             disposed = true;
80         }
81
82         /// <summary>
83         /// Gets the network type.
84         /// </summary>
85         public ConnectionProfileType Type
86         {
87             get
88             {
89                 int Value;
90                 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
91                 if ((ConnectionError)ret != ConnectionError.None)
92                 {
93                     Log.Error(Globals.LogTag, "It failed to get profile type, " + (ConnectionError)ret);
94                 }
95                 return (ConnectionProfileType)Value;
96             }
97         }
98
99         /// <summary>
100         /// Gets the Proxy type.
101         /// </summary>
102         public ProxyType ProxyType
103         {
104             get
105             {
106                 int Value;
107                 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
108                 if ((ConnectionError)ret != ConnectionError.None)
109                 {
110                     Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
111                 }
112                 return (ProxyType)Value;
113
114             }
115             set
116             {
117                 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
118                 if ((ConnectionError)ret != ConnectionError.None)
119                 {
120                     Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
121                     ConnectionErrorFactory.ThrowConnectionException(ret);
122                 }
123             }
124         }
125
126         /// <summary>
127         /// The proxy address.
128         /// </summary>
129         public System.Net.IPAddress ProxyAddress
130         {
131             get
132             {
133                 IntPtr Value;
134                 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, out Value);
135                 if ((ConnectionError)ret != ConnectionError.None)
136                 {
137                     Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
138                 }
139                 string result = Marshal.PtrToStringAnsi(Value);
140                 Interop.Libc.Free(Value);
141                 if (result == null)
142                     return System.Net.IPAddress.Parse("0.0.0.0");
143                 return System.Net.IPAddress.Parse(result);
144
145             }
146             set
147             {
148                 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, value.ToString());
149                 if ((ConnectionError)ret != ConnectionError.None)
150                 {
151                     Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
152                     ConnectionErrorFactory.ThrowConnectionException(ret);
153                 }
154             }
155
156         }
157
158         /// <summary>
159         /// The subnet mask address(Ipv4).
160         /// </summary>
161         public IAddressInformation Ipv4Settings
162         {
163             get
164             {
165                 return Ipv4;
166             }
167         }
168
169
170         /// <summary>
171         /// The subnet mask address(Ipv4).
172         /// </summary>
173         public IAddressInformation Ipv6Settings
174         {
175             get
176             {
177                 return Ipv6;
178             }
179         }
180
181         /// <summary>
182         /// Gets the APN (access point name).
183         /// </summary>
184         public string Apn
185         {
186             get
187             {
188                 IntPtr Value;
189                 int ret = Interop.ConnectionCellularProfile.GetApn(ProfileHandle, out Value);
190                 if ((ConnectionError)ret != ConnectionError.None)
191                 {
192                     Log.Error(Globals.LogTag, "It failed to get apn, " + (ConnectionError)ret);
193                 }
194                 string result = Marshal.PtrToStringAnsi(Value);
195                 Interop.Libc.Free(Value);
196                 return result;
197             }
198             set
199             {
200                 int ret = Interop.ConnectionCellularProfile.SetApn(ProfileHandle, (string)value);
201                 if ((ConnectionError)ret != ConnectionError.None)
202                 {
203                     Log.Error(Globals.LogTag, "It failed to set apn, " + (ConnectionError)ret);
204                     ConnectionErrorFactory.ThrowConnectionException(ret);
205                 }
206             }
207         }
208
209         /// <summary>
210         /// Gets the home URL.
211         /// </summary>
212         public string HomeUri
213         {
214             get
215             {
216                 IntPtr Value;
217                 int ret = Interop.ConnectionCellularProfile.GetHomeUrl(ProfileHandle, out Value);
218                 if ((ConnectionError)ret != ConnectionError.None)
219                 {
220                     Log.Error(Globals.LogTag, "It failed to get home uri, " + (ConnectionError)ret);
221                 }
222                 string result = Marshal.PtrToStringAnsi(Value);
223                 Interop.Libc.Free(Value);
224                 return result;
225             }
226             set
227             {
228                 int ret = Interop.ConnectionCellularProfile.SetHomeUrl(ProfileHandle, value);
229                 Log.Error(Globals.LogTag, "home uri  " + value);
230                 if ((ConnectionError)ret != ConnectionError.None)
231                 {
232                     Log.Error(Globals.LogTag, "It failed to set home uri, " + (ConnectionError)ret);
233                     ConnectionErrorFactory.ThrowConnectionException(ret);
234                 }
235             }
236         }
237
238         /// <summary>
239         /// Gets the service type.
240         /// </summary>
241         public CellularServiceType ServiceType
242         {
243             get
244             {
245                 int value;
246                 int ret = Interop.ConnectionCellularProfile.GetServiceType(ProfileHandle, out value);
247                 if ((ConnectionError)ret != ConnectionError.None)
248                 {
249                     Log.Error(Globals.LogTag, "It failed to get service type, " + (ConnectionError)ret);
250                 }
251                 return (CellularServiceType)value;
252             }
253             set
254             {
255                 int ret = Interop.ConnectionCellularProfile.SetServiceType(ProfileHandle, (int)value);
256                 if ((ConnectionError)ret != ConnectionError.None)
257                 {
258                     Log.Error(Globals.LogTag, "It failed to set service type, " + (ConnectionError)ret);
259                     ConnectionErrorFactory.ThrowConnectionException(ret);
260                 }
261             }
262         }
263
264         /// <summary>
265         /// Gets cellular Authentification Information.
266         /// </summary>
267         public CellularAuthInformation CellularAuthInfo
268         {
269             get
270             {
271                 return AuthInfo;
272             }
273         }
274     }
275 }