Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Tapi / Tizen.Tapi / Modem.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Runtime.InteropServices;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace Tizen.Tapi
8 {
9     /// <summary>
10     /// This class provides functions for modem services.
11     /// </summary>
12     public class Modem
13     {
14         private IntPtr _handle;
15         private Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback> _response_map = new Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback>();
16         private int _requestId = 0;
17
18         /// <summary>
19         /// A public constructor for Modem class to create a Modem instance for the given tapi handle.
20         /// </summary>
21         /// <param name="handle">The tapi handle.</param>
22         public Modem(TapiHandle handle)
23         {
24             if (handle == null)
25             {
26                 throw new ArgumentNullException("TapiHandle parameter is null");
27             }
28
29             _handle = handle._handle;
30         }
31
32         /// <summary>
33         /// Turn the modem on/off asynchronously.
34         /// </summary>
35         /// <param name="cmd">Power command value.</param>
36         /// <returns>A task indicating whether the ProcessPowerCommand method is done or not.</returns>
37         /// <privilege>http://tizen.org/privilege/telephony.admin</privilege>
38         /// <privlevel>platform</privlevel>
39         /// <feature>http://tizen.org/feature/network.telephony</feature>
40         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
41         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
42         /// <exception cref="System.InvalidOperationException">Thrown when modem instance is invalid or when method failed due to invalid operation.</exception>
43         public Task ProcessPowerCommand(PhonePowerCommand cmd)
44         {
45             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
46             IntPtr id;
47             id = (IntPtr)_requestId++;
48             _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
49             {
50                 Task resultTask = new Task(() =>
51                 {
52                     if (result != (int)TapiError.Success)
53                     {
54                         Log.Error(TapiUtility.LogTag, "Error occurs during turning modem on/off, " + (TapiError)result);
55                         task.SetException(new InvalidOperationException("Error occurs during turning modem on/off, " + (TapiError)result));
56                         return;
57                     }
58
59                     task.SetResult(true);
60                 });
61
62                 resultTask.Start();
63                 resultTask.Wait();
64                 _response_map.Remove(key);
65             };
66
67             int ret = Interop.Tapi.Modem.ProcessPowerCommand(_handle, cmd, _response_map[id], id);
68             if (ret != (int)TapiError.Success)
69             {
70                 Log.Error(TapiUtility.LogTag, "Failed to turn the modem on/off, Error: " + (TapiError)ret);
71                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
72             }
73
74             return task.Task;
75         }
76
77         /// <summary>
78         /// Switch the flight mode on/off asynchronously.
79         /// </summary>
80         /// <param name="mode">Flight mode request value.</param>
81         /// <returns>A task indicating whether the SetFlightMode method is done or not.</returns>
82         /// <privilege>http://tizen.org/privilege/telephony.admin</privilege>
83         /// <privlevel>platform</privlevel>
84         /// <feature>http://tizen.org/feature/network.telephony</feature>
85         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
86         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
87         /// <exception cref="System.InvalidOperationException">Thrown when modem instance is invalid or when method failed due to invalid operation.</exception>
88         public Task SetFlightMode(PowerFlightModeRequest mode)
89         {
90             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
91             IntPtr id;
92             id = (IntPtr)_requestId++;
93             _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
94             {
95                 Task resultTask = new Task(() =>
96                 {
97                     if ((mode == PowerFlightModeRequest.Leave && result != (int)PowerFlightModeResponse.Off) ||
98                         (mode == PowerFlightModeRequest.Enter && result != (int)PowerFlightModeResponse.On))
99                     {
100                         Log.Error(TapiUtility.LogTag, "Error occurs during switching flight mode on/off, " + (PowerFlightModeResponse)result);
101                         task.SetException(new InvalidOperationException("Error occurs during switching flight mode on/off, " + (PowerFlightModeResponse)result));
102                         return;
103                     }
104
105                     task.SetResult(true);
106                 });
107
108                 resultTask.Start();
109                 resultTask.Wait();
110                 _response_map.Remove(key);
111             };
112
113             int ret = Interop.Tapi.Modem.SetFlightMode(_handle, mode, _response_map[id], id);
114             if (ret != (int)TapiError.Success)
115             {
116                 Log.Error(TapiUtility.LogTag, "Failed to switch the flight mode on/off, Error: " + (TapiError)ret);
117                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
118             }
119
120             return task.Task;
121         }
122
123         /// <summary>
124         /// Get the flight mode asynchronously.
125         /// </summary>
126         /// <returns>If flight mode is On, it returns true else it returns false.</returns>
127         /// <privilege>http://tizen.org/privilege/telephony</privilege>
128         /// <feature>http://tizen.org/feature/network.telephony</feature>
129         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
130         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
131         /// <exception cref="System.InvalidOperationException">Thrown when modem instance is invalid or when method failed due to invalid operation.</exception>
132         public Task<bool> GetFlightMode()
133         {
134             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
135             IntPtr id;
136             id = (IntPtr)_requestId++;
137             _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
138             {
139                 Task resultTask = new Task(() =>
140                 {
141                     if (result != (int)TapiError.Success)
142                     {
143                         Log.Error(TapiUtility.LogTag, "Error occurs during getting the flight mode, " + (TapiError)result);
144                         task.SetException(new InvalidOperationException("Error occurs during getting the flight mode, " + (TapiError)result));
145                         return;
146                     }
147
148                     int mode = Marshal.ReadInt32(data);
149                     if (mode == 1)
150                     {
151                         task.SetResult(true);
152                     }
153
154                     else
155                     {
156                         task.SetResult(false);
157                     }
158                 });
159
160                 resultTask.Start();
161                 resultTask.Wait();
162                 _response_map.Remove(key);
163             };
164
165             int ret = Interop.Tapi.Modem.GetFlightMode(_handle, _response_map[id], id);
166             if (ret != (int)TapiError.Success)
167             {
168                 Log.Error(TapiUtility.LogTag, "Failed to get the flight mode, Error: " + (TapiError)ret);
169                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
170             }
171
172             return task.Task;
173         }
174
175         /// <summary>
176         /// Get Me version information asynchronously.
177         /// </summary>
178         /// <returns>Instance of MiscVersionInformation.</returns>
179         /// <privilege>http://tizen.org/privilege/telephony</privilege>
180         /// <feature>http://tizen.org/feature/network.telephony</feature>
181         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
182         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
183         /// <exception cref="System.InvalidOperationException">Thrown when modem instance is invalid or when method failed due to invalid operation.</exception>
184         public Task<MiscVersionInformation> GetMiscMeVersion()
185         {
186             TaskCompletionSource<MiscVersionInformation> task = new TaskCompletionSource<MiscVersionInformation>();
187             IntPtr id;
188             id = (IntPtr)_requestId++;
189             _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
190             {
191                 Task resultTask = new Task(() =>
192                 {
193                     if (result != (int)TapiError.Success)
194                     {
195                         Log.Error(TapiUtility.LogTag, "Error occurs during getting the Me version, " + (TapiError)result);
196                         task.SetException(new InvalidOperationException("Error occurs during getting the Me version, " + (TapiError)result));
197                         return;
198                     }
199
200                     MiscVersionInfoStruct infoStruct = Marshal.PtrToStructure<MiscVersionInfoStruct>(data);
201                     MiscVersionInformation versionInfoClass = ModemStructConversions.ConvertVersionStruct(infoStruct);
202                     task.SetResult(versionInfoClass);
203                 });
204
205                 resultTask.Start();
206                 resultTask.Wait();
207                 _response_map.Remove(key);
208             };
209
210             int ret = Interop.Tapi.Modem.GetMiscMeVersion(_handle, _response_map[id], id);
211             if (ret != (int)TapiError.Success)
212             {
213                 Log.Error(TapiUtility.LogTag, "Failed to get the Me version information, Error: " + (TapiError)ret);
214                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
215             }
216
217             return task.Task;
218         }
219
220         /// <summary>
221         /// Misc me version information.
222         /// </summary>
223         /// <privilege>http://tizen.org/privilege/telephony</privilege>
224         /// <feature>http://tizen.org/feature/network.telephony</feature>
225         /// <remarks>Returns null in case of failure.</remarks>
226         public MiscVersionInformation MiscMeVersionSync
227         {
228             get
229             {
230                 IntPtr info = Interop.Tapi.Modem.GetMiscMeVersionSync(_handle);
231                 MiscVersionInfoStruct infoStruct = Marshal.PtrToStructure<MiscVersionInfoStruct>(info);
232                 if (infoStruct.Equals(null))
233                 {
234                     return null;
235                 }
236
237                 MiscVersionInformation versionInfoClass = ModemStructConversions.ConvertVersionStruct(infoStruct);
238                 return versionInfoClass;
239             }
240         }
241
242         /// <summary>
243         /// Get the Me Esn/Meid for each phone type asynchronously.
244         /// </summary>
245         /// <returns>Instance of MiscSerialNumberInformation.</returns>
246         /// <privilege>http://tizen.org/privilege/telephony</privilege>
247         /// <feature>http://tizen.org/feature/network.telephony</feature>
248         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
249         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
250         /// <exception cref="System.InvalidOperationException">Thrown when modem instance is invalid or when method failed due to invalid operation.</exception>
251         public Task<MiscSerialNumberInformation> GetMiscMeSn()
252         {
253             TaskCompletionSource<MiscSerialNumberInformation> task = new TaskCompletionSource<MiscSerialNumberInformation>();
254             IntPtr id;
255             id = (IntPtr)_requestId++;
256             _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
257             {
258                 Task resultTask = new Task(() =>
259                 {
260                     if (result != (int)TapiError.Success)
261                     {
262                         Log.Error(TapiUtility.LogTag, "Error occurs during getting the Me Esn/Meid, " + (TapiError)result);
263                         task.SetException(new InvalidOperationException("Error occurs during getting the Me Esn/Meid, " + (TapiError)result));
264                         return;
265                     }
266
267                     MiscSerialNumInfoStruct infoStruct = Marshal.PtrToStructure<MiscSerialNumInfoStruct>(data);
268                     MiscSerialNumberInformation serialNumberClass = ModemStructConversions.ConvertSerialNumberStruct(infoStruct);
269                     task.SetResult(serialNumberClass);
270                 });
271
272                 resultTask.Start();
273                 resultTask.Wait();
274                 _response_map.Remove(key);
275             };
276
277             int ret = Interop.Tapi.Modem.GetMiscMeSn(_handle, _response_map[id], id);
278             if (ret != (int)TapiError.Success)
279             {
280                 Log.Error(TapiUtility.LogTag, "Failed to get the Me Esn/Meid information for each phone type, Error: " + (TapiError)ret);
281                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
282             }
283
284             return task.Task;
285         }
286
287         /// <summary>
288         /// Misc me serial number information.
289         /// </summary>
290         /// <privilege>http://tizen.org/privilege/telephony</privilege>
291         /// <feature>http://tizen.org/feature/network.telephony</feature>
292         /// <remarks>Returns null in case of failure.</remarks>
293         public MiscSerialNumberInformation MiscMeSnSync
294         {
295             get
296             {
297                 IntPtr info = Interop.Tapi.Modem.GetMiscMeSnSync(_handle);
298                 MiscSerialNumInfoStruct infoStruct = Marshal.PtrToStructure<MiscSerialNumInfoStruct>(info);
299                 if (infoStruct.Equals(null))
300                 {
301                     return null;
302                 }
303
304                 MiscSerialNumberInformation versionInfoClass = ModemStructConversions.ConvertSerialNumberStruct(infoStruct);
305                 return versionInfoClass;
306             }
307         }
308
309         /// <summary>
310         /// Get the Misc Me Imei asynchronously.
311         /// </summary>
312         /// <returns>The imei string.</returns>
313         /// <privilege>http://tizen.org/privilege/telephony</privilege>
314         /// <feature>http://tizen.org/feature/network.telephony</feature>
315         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
316         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
317         /// <exception cref="System.InvalidOperationException">Thrown when modem instance is invalid or when method failed due to invalid operation.</exception>
318         public Task<string> GetMiscMeImei()
319         {
320             TaskCompletionSource<string> task = new TaskCompletionSource<string>();
321             IntPtr id;
322             id = (IntPtr)_requestId++;
323             _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
324             {
325                 Task resultTask = new Task(() =>
326                 {
327                     if (result != (int)TapiError.Success)
328                     {
329                         Log.Error(TapiUtility.LogTag, "Error occurs during getting the Misc Me Imei, " + (TapiError)result);
330                         task.SetException(new InvalidOperationException("Error occurs during getting the Misc Me Imei, " + (TapiError)result));
331                         return;
332                     }
333
334                     task.SetResult(Marshal.PtrToStringAnsi(data));
335                 });
336
337                 resultTask.Start();
338                 resultTask.Wait();
339                 _response_map.Remove(key);
340             };
341
342             int ret = Interop.Tapi.Modem.GetMiscMeImei(_handle, _response_map[id], id);
343             if (ret != (int)TapiError.Success)
344             {
345                 Log.Error(TapiUtility.LogTag, "Failed to get the Misc Me Imei information, Error: " + (TapiError)ret);
346                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
347             }
348
349             return task.Task;
350         }
351
352         /// <summary>
353         /// Misc me Imei information.
354         /// </summary>
355         /// <privilege>http://tizen.org/privilege/telephony</privilege>
356         /// <feature>http://tizen.org/feature/network.telephony</feature>
357         /// <remarks>Returns null in case of failure.</remarks>
358         public string MiscMeImeiSync
359         {
360             get
361             {
362                 string imei = Interop.Tapi.Modem.GetMiscMeImeiSync(_handle);
363                 if (imei == null)
364                 {
365                     return null;
366                 }
367
368                 return imei;
369             }
370         }
371
372         /// <summary>
373         /// Check the modem power status.
374         /// </summary>
375         /// <returns>Phone power status value.</returns>
376         /// <privilege>http://tizen.org/privilege/telephony</privilege>
377         /// <feature>http://tizen.org/feature/network.telephony</feature>
378         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
379         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
380         /// <exception cref="System.InvalidOperationException">Thrown when modem instance is invalid or when method failed due to invalid operation.</exception>
381         public PhonePowerStatus CheckPowerStatus()
382         {
383             int result;
384             int ret = Interop.Tapi.Modem.CheckPowerStatus(_handle, out result);
385             if (ret != (int)TapiError.Success)
386             {
387                 Log.Error(TapiUtility.LogTag, "Failed to check the modem power status, Error: " + (TapiError)ret);
388                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
389             }
390
391             return (PhonePowerStatus)result;
392         }
393
394         /// <summary>
395         /// Get device vendor name and device name of cellular dongle.
396         /// </summary>
397         /// <returns>Instance of MiscDeviceInfo.</returns>
398         /// <privilege>http://tizen.org/privilege/telephony</privilege>
399         /// <feature>http://tizen.org/feature/network.telephony</feature>
400         /// <remarks>
401         /// Result can be delivered with only cellular dongle insertion.
402         /// </remarks>
403         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
404         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
405         /// <exception cref="System.InvalidOperationException">Thrown when modem instance is invalid or when method failed due to invalid operation.</exception>
406         public Task<MiscDeviceInfo> GetDeviceInfo()
407         {
408             TaskCompletionSource<MiscDeviceInfo> task = new TaskCompletionSource<MiscDeviceInfo>();
409             IntPtr id;
410             id = (IntPtr)_requestId++;
411             _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
412             {
413                 Task resultTask = new Task(() =>
414                 {
415                     if (result != (int)TapiError.Success)
416                     {
417                         Log.Error(TapiUtility.LogTag, "Error occurs during getting the device name and vendor name, " + (TapiError)result);
418                         task.SetException(new InvalidOperationException("Error occurs during getting the device name and vendor name, " + (TapiError)result));
419                         return;
420                     }
421
422                     MiscDeviceInfoStruct infoStruct = Marshal.PtrToStructure<MiscDeviceInfoStruct>(data);
423                     MiscDeviceInfo deviceInfo = ModemStructConversions.ConvertMiscInfoStruct(infoStruct);
424                     task.SetResult(deviceInfo);
425                 });
426
427                 resultTask.Start();
428                 resultTask.Wait();
429                 _response_map.Remove(key);
430             };
431
432             int ret = Interop.Tapi.Modem.GetDeviceInfo(_handle, _response_map[id], id);
433             if (ret != (int)TapiError.Success)
434             {
435                 Log.Error(TapiUtility.LogTag, "Failed to get the device vendor name and device name, Error: " + (TapiError)ret);
436                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
437             }
438
439             return task.Task;
440         }
441     }
442 }