2 using System.Collections.Generic;
3 using System.Runtime.InteropServices;
5 using System.Threading.Tasks;
10 /// This class provides functions for modem services.
14 private IntPtr _handle;
15 private Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback> _response_map = new Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback>();
16 private int _requestId = 0;
19 /// A public constructor for Modem class to create a Modem instance for the given tapi handle.
21 /// <param name="handle">The tapi handle.</param>
22 public Modem(TapiHandle handle)
26 throw new ArgumentNullException("TapiHandle parameter is null");
29 _handle = handle._handle;
33 /// Turn the modem on/off asynchronously.
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)
45 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
47 id = (IntPtr)_requestId++;
48 _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
50 Task resultTask = new Task(() =>
52 if (result != (int)TapiError.Success)
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));
64 _response_map.Remove(key);
67 int ret = Interop.Tapi.Modem.ProcessPowerCommand(_handle, cmd, _response_map[id], id);
68 if (ret != (int)TapiError.Success)
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");
78 /// Switch the flight mode on/off asynchronously.
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)
90 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
92 id = (IntPtr)_requestId++;
93 _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
95 Task resultTask = new Task(() =>
97 if ((mode == PowerFlightModeRequest.Leave && result != (int)PowerFlightModeResponse.Off) ||
98 (mode == PowerFlightModeRequest.Enter && result != (int)PowerFlightModeResponse.On))
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));
105 task.SetResult(true);
110 _response_map.Remove(key);
113 int ret = Interop.Tapi.Modem.SetFlightMode(_handle, mode, _response_map[id], id);
114 if (ret != (int)TapiError.Success)
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");
124 /// Get the flight mode asynchronously.
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()
134 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
136 id = (IntPtr)_requestId++;
137 _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
139 Task resultTask = new Task(() =>
141 if (result != (int)TapiError.Success)
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));
148 int mode = Marshal.ReadInt32(data);
151 task.SetResult(true);
156 task.SetResult(false);
162 _response_map.Remove(key);
165 int ret = Interop.Tapi.Modem.GetFlightMode(_handle, _response_map[id], id);
166 if (ret != (int)TapiError.Success)
168 Log.Error(TapiUtility.LogTag, "Failed to get the flight mode, Error: " + (TapiError)ret);
169 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
176 /// Get Me version information asynchronously.
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()
186 TaskCompletionSource<MiscVersionInformation> task = new TaskCompletionSource<MiscVersionInformation>();
188 id = (IntPtr)_requestId++;
189 _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
191 Task resultTask = new Task(() =>
193 if (result != (int)TapiError.Success)
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));
200 MiscVersionInfoStruct infoStruct = Marshal.PtrToStructure<MiscVersionInfoStruct>(data);
201 MiscVersionInformation versionInfoClass = ModemStructConversions.ConvertVersionStruct(infoStruct);
202 task.SetResult(versionInfoClass);
207 _response_map.Remove(key);
210 int ret = Interop.Tapi.Modem.GetMiscMeVersion(_handle, _response_map[id], id);
211 if (ret != (int)TapiError.Success)
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");
221 /// Misc me version information.
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
230 IntPtr info = Interop.Tapi.Modem.GetMiscMeVersionSync(_handle);
231 MiscVersionInfoStruct infoStruct = Marshal.PtrToStructure<MiscVersionInfoStruct>(info);
232 if (infoStruct.Equals(null))
237 MiscVersionInformation versionInfoClass = ModemStructConversions.ConvertVersionStruct(infoStruct);
238 return versionInfoClass;
243 /// Get the Me Esn/Meid for each phone type asynchronously.
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()
253 TaskCompletionSource<MiscSerialNumberInformation> task = new TaskCompletionSource<MiscSerialNumberInformation>();
255 id = (IntPtr)_requestId++;
256 _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
258 Task resultTask = new Task(() =>
260 if (result != (int)TapiError.Success)
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));
267 MiscSerialNumInfoStruct infoStruct = Marshal.PtrToStructure<MiscSerialNumInfoStruct>(data);
268 MiscSerialNumberInformation serialNumberClass = ModemStructConversions.ConvertSerialNumberStruct(infoStruct);
269 task.SetResult(serialNumberClass);
274 _response_map.Remove(key);
277 int ret = Interop.Tapi.Modem.GetMiscMeSn(_handle, _response_map[id], id);
278 if (ret != (int)TapiError.Success)
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");
288 /// Misc me serial number information.
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
297 IntPtr info = Interop.Tapi.Modem.GetMiscMeSnSync(_handle);
298 MiscSerialNumInfoStruct infoStruct = Marshal.PtrToStructure<MiscSerialNumInfoStruct>(info);
299 if (infoStruct.Equals(null))
304 MiscSerialNumberInformation versionInfoClass = ModemStructConversions.ConvertSerialNumberStruct(infoStruct);
305 return versionInfoClass;
310 /// Get the Misc Me Imei asynchronously.
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()
320 TaskCompletionSource<string> task = new TaskCompletionSource<string>();
322 id = (IntPtr)_requestId++;
323 _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
325 Task resultTask = new Task(() =>
327 if (result != (int)TapiError.Success)
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));
334 task.SetResult(Marshal.PtrToStringAnsi(data));
339 _response_map.Remove(key);
342 int ret = Interop.Tapi.Modem.GetMiscMeImei(_handle, _response_map[id], id);
343 if (ret != (int)TapiError.Success)
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");
353 /// Misc me Imei information.
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
362 string imei = Interop.Tapi.Modem.GetMiscMeImeiSync(_handle);
373 /// Check the modem power status.
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()
384 int ret = Interop.Tapi.Modem.CheckPowerStatus(_handle, out result);
385 if (ret != (int)TapiError.Success)
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");
391 return (PhonePowerStatus)result;
395 /// Get device vendor name and device name of cellular dongle.
397 /// <returns>Instance of MiscDeviceInfo.</returns>
398 /// <privilege>http://tizen.org/privilege/telephony</privilege>
399 /// <feature>http://tizen.org/feature/network.telephony</feature>
401 /// Result can be delivered with only cellular dongle insertion.
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()
408 TaskCompletionSource<MiscDeviceInfo> task = new TaskCompletionSource<MiscDeviceInfo>();
410 id = (IntPtr)_requestId++;
411 _response_map[id] = (IntPtr handle, int result, IntPtr data, IntPtr key) =>
413 Task resultTask = new Task(() =>
415 if (result != (int)TapiError.Success)
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));
422 MiscDeviceInfoStruct infoStruct = Marshal.PtrToStructure<MiscDeviceInfoStruct>(data);
423 MiscDeviceInfo deviceInfo = ModemStructConversions.ConvertMiscInfoStruct(infoStruct);
424 task.SetResult(deviceInfo);
429 _response_map.Remove(key);
432 int ret = Interop.Tapi.Modem.GetDeviceInfo(_handle, _response_map[id], id);
433 if (ret != (int)TapiError.Success)
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");