/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Threading.Tasks; using System.Runtime.InteropServices; using System.Collections.Generic; namespace Tizen.Tapi { /// /// A class which manages Supplementary Services of the SIM. /// /// 4 public class Ss { private IntPtr _handle = IntPtr.Zero; private Dictionary _callbackMap = new Dictionary(); private int _requestId = 0; private Ss() { } /// /// A constructor to instantiate Ss class using the Tapi handle. /// /// 4 /// An instance of TapiHandle obtained from InitTapi in TapiManager API. /// Thrown when handle is passed as null. public Ss(TapiHandle handle) { if (handle == null) { throw new ArgumentNullException("Handle is null"); } _handle = handle._handle; } /// /// Sends a request to activate/deactivate call barring. /// /// 4 /// The information about call barring. /// A task containing an instance of SsBarringResponse which contains information about barring response. /// http://tizen.org/feature/network.telephony /// platform /// http://tizen.org/privilege/telephony.admin /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when barring info is passed as null. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task SsSetBarring(SsBarringInfo info) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)SsCause.Success) { Log.Error(TapiUtility.LogTag, "Error occurs during setting SS barring info: " + (SsCause)result); task.SetException(new InvalidOperationException("Error occurs during setting SS barring info, " + (SsCause)result)); return; } SsBarringResponseStruct response = Marshal.PtrToStructure(data); task.SetResult(SsStructConversions.ConvertBarringRspStruct(response)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; if (info == null) { throw new ArgumentNullException("Ss barring info is null"); } SsBarringInfoStruct infoStruct = SsClassConversions.ConvertSsBarringInfo(info); int ret = Interop.Tapi.Ss.SsSetBarring(_handle, ref infoStruct, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to set barring info, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin"); } return task.Task; } /// /// Gets call barring status. /// /// 4 /// The type of call. /// The barring type. /// A task containing information about barring response. /// http://tizen.org/feature/network.telephony /// http://tizen.org/privilege/telephony /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task SsGetBarringStatus(SsClass ssClass, SsBarringType type) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)SsCause.Success) { Log.Error(TapiUtility.LogTag, "Error occurs in getting barring status: " + (SsCause)result); task.SetException(new InvalidOperationException("Error occurs in getting barring status, " + (SsCause)result)); return; } SsBarringResponseStruct response = Marshal.PtrToStructure(data); task.SetResult(SsStructConversions.ConvertBarringRspStruct(response)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; int ret = Interop.Tapi.Ss.SsGetBarringStatus(_handle, ssClass, type, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to get barring status, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony"); } return task.Task; } /// /// Allows changing of the barring password in the network. /// /// 4 /// The old password set for Barring in the Network. /// The new password set for Barring in the Network. /// The new password again. /// A task indicating whether the change of password is done or not. /// http://tizen.org/feature/network.telephony /// platform /// http://tizen.org/privilege/telephony.admin /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when any of the parameter is passed as null. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task SsChangeBarringPassword(string oldPassword, string newPassword, string newPasswordAgain) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)SsCause.Success) { Log.Error(TapiUtility.LogTag, "Error occurs in changing barring password: " + (SsCause)result); task.SetException(new InvalidOperationException("Error occurs in changing barring password, " + (SsCause)result)); return; } task.SetResult(true); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; if (oldPassword == null || newPassword == null || newPasswordAgain == null) { throw new ArgumentNullException("Old password/new password is null"); } int ret = Interop.Tapi.Ss.SsChangeBarringPassword(_handle, oldPassword, newPassword, newPasswordAgain, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to change barring password, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin"); } return task.Task; } /// /// Allows to set the (register/erase/activate/deactivate) call forwarding option at the network. /// /// 4 /// The Call forward information such as a forward mode, a forward type, and so on. /// A task containing information about SS forward response. /// http://tizen.org/feature/network.telephony /// platform /// http://tizen.org/privilege/telephony.admin /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when forward info is passed as null. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task SsSetForwardInfo(SsForwardInfo info) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)SsCause.Success) { Log.Error(TapiUtility.LogTag, "Error occurs in setting SS forward info: " + (SsCause)result); task.SetException(new InvalidOperationException("Error occurs in setting SS forward info, " + (SsCause)result)); return; } SsForwardResponseStruct response = Marshal.PtrToStructure(data); task.SetResult(SsStructConversions.ConvertForwardRspStruct(response)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; if (info == null) { throw new ArgumentNullException("Ss forward info is null"); } SsForwardInfoStruct infoStruct = SsClassConversions.ConvertSsForwardInfo(info); int ret = Interop.Tapi.Ss.SsSetForward(_handle, ref infoStruct, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to set forward info, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin"); } return task.Task; } /// /// Provides an option to get the call forwarding status of different calls from the Network. /// /// 4 /// The Forward call type. /// The forward condition. /// A task containing SS forward response information. /// http://tizen.org/feature/network.telephony /// http://tizen.org/privilege/telephony /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task SsGetForwardStatus(SsClass ssClass, SsForwardCondition condition) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)SsCause.Success) { Log.Error(TapiUtility.LogTag, "Error occurs in getting SS forward status: " + (SsCause)result); task.SetException(new InvalidOperationException("Error occurs in getting SS forward status, " + (SsCause)result)); return; } SsForwardResponseStruct response = Marshal.PtrToStructure(data); task.SetResult(SsStructConversions.ConvertForwardRspStruct(response)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; int ret = Interop.Tapi.Ss.SsGetForwardStatus(_handle, ssClass, condition, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to get forward status, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony"); } return task.Task; } /// /// Activates/deactivates the call waiting service. /// /// 4 /// The status of call-waiting service. /// A task containing SS waiting response information. /// http://tizen.org/feature/network.telephony /// platform /// http://tizen.org/privilege/telephony.admin /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when waiting info is passed as null. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task SsSetWaitingInfo(SsWaitingInfo info) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)SsCause.Success) { Log.Error(TapiUtility.LogTag, "Error occurs in setting SS waiting info: " + (SsCause)result); task.SetException(new InvalidOperationException("Error occurs in setting SS waiting info, " + (SsCause)result)); return; } SsWaitingResponseStruct response = Marshal.PtrToStructure(data); task.SetResult(SsStructConversions.ConvertWaitingRspStruct(response)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; if (info == null) { throw new ArgumentNullException("Ss waiting info is null"); } SsWaitingInfoStruct infoStruct = SsClassConversions.ConvertSsWaitingInfo(info); int ret = Interop.Tapi.Ss.SsSetWaiting(_handle, ref infoStruct, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to set waiting info, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin"); } return task.Task; } /// /// Gets the status of the call waiting service. /// /// 4 /// The call types. /// A task containing information about SS waiting response. /// http://tizen.org/feature/network.telephony /// http://tizen.org/privilege/telephony /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task SsGetWaitingInfo(SsClass ssClass) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)SsCause.Success) { Log.Error(TapiUtility.LogTag, "Error occurs in getting SS waiting info: " + (SsCause)result); task.SetException(new InvalidOperationException("Error occurs in getting SS waiting info, " + (SsCause)result)); return; } SsWaitingResponseStruct response = Marshal.PtrToStructure(data); task.SetResult(SsStructConversions.ConvertWaitingRspStruct(response)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; int ret = Interop.Tapi.Ss.SsGetWaitingStatus(_handle, ssClass, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to get waiting info, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony"); } return task.Task; } /// /// Activates/deactivates the status of the calling line identity service. /// /// 4 /// The Cli service type. /// The Cli Status. /// A task indicating whether setting of CLI status is done or not. /// http://tizen.org/feature/network.telephony /// platform /// http://tizen.org/privilege/telephony.admin /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task SsSetCliStatus(SsCliType type, SsCliStatus status) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)SsCause.Success) { Log.Error(TapiUtility.LogTag, "Error occurs in setting SS CLI status: " + (SsCause)result); task.SetException(new InvalidOperationException("Error occurs in setting SS CLI status, " + (SsCause)result)); return; } task.SetResult(true); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; int ret = Interop.Tapi.Ss.SsSetCliStatus(_handle, type, status, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to set SS CLI status, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin"); } return task.Task; } /// /// Gets the status of the calling line identity service. /// /// 4 /// The Cli service type. /// A task containing SS CLI response information. /// http://tizen.org/feature/network.telephony /// http://tizen.org/privilege/telephony /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task SsGetCliStatus(SsCliType type) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)SsCause.Success) { Log.Error(TapiUtility.LogTag, "Error occurs in getting SS CLI status: " + (SsCause)result); task.SetException(new InvalidOperationException("Error occurs in getting SS CLI status, " + (SsCause)result)); return; } SsCliResponseStruct response = Marshal.PtrToStructure(data); task.SetResult(SsStructConversions.ConvertSsCliResponseStruct(response)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; int ret = Interop.Tapi.Ss.SsGetCliStatus(_handle, type, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to get CLI status, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony"); } return task.Task; } /// /// Sends a USSD string or User response to the Network. /// /// 4 /// The data coding scheme used /// A task containing SS USSD response information. /// http://tizen.org/feature/network.telephony /// platform /// http://tizen.org/privilege/telephony.admin /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when Ussd message info is passed as null. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task SsSendUssdRequest(SsUssdMsgInfo info) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)SsCause.Success) { Log.Error(TapiUtility.LogTag, "Error occurs in sending USSD request: " + (SsCause)result); task.SetException(new InvalidOperationException("Error occurs in sending USSD request, " + (SsCause)result)); return; } SsUssdResponseStruct response = Marshal.PtrToStructure(data); task.SetResult(SsStructConversions.ConvertSsUssdResponseStruct(response)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; if (info == null) { throw new ArgumentNullException("Ussd message info is null"); } SsUssdMsgInfoStruct msgStruct = SsClassConversions.ConvertSsUssdMsgInfo(info); int ret = Interop.Tapi.Ss.SsSendUssdRequest(_handle, ref msgStruct, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to send USSD request, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin"); } return task.Task; } } }