2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
20 using System.Runtime.InteropServices;
26 /// This class is used for initializing/deinitializing Tapi and manages the state of it.
28 public static class TapiManager
30 private static event EventHandler<StateChangedEventArgs> s_stateChanged;
31 private static Interop.Tapi.TapiStateCallback s_stateChangedCb;
34 /// Gets the state value if tapi is ready.
36 /// <value>The State value in integer format - 0 is False and 1 is True. Returns -1 in case of error.</value>
37 public static int State
42 int ret = Interop.Tapi.GetReadyState(out state);
43 if (ret != (int)TapiError.Success)
45 Log.Error(TapiUtility.LogTag, "Failed to get ready state of tapi, Error: " + (TapiError)ret);
54 /// This event is raised when Tapi ready state changes.
56 public static event EventHandler<StateChangedEventArgs> StateChanged
60 if (s_stateChanged == null)
62 RegisterStateChangedEvent();
65 s_stateChanged += value;
70 s_stateChanged -= value;
71 if (s_stateChanged == null)
73 UnregisterStateChangedEvent();
78 private static void RegisterStateChangedEvent()
80 s_stateChangedCb = (int state, IntPtr userData) =>
82 s_stateChanged?.Invoke(null, new StateChangedEventArgs(state));
84 int ret = Interop.Tapi.RegisterReadyStateCb(s_stateChangedCb, IntPtr.Zero);
85 if (ret != (int)TapiError.Success)
87 Log.Error(TapiUtility.LogTag, "Failed to register tapi state changed callback, Error: " + (TapiError)ret);
91 private static void UnregisterStateChangedEvent()
93 int ret = Interop.Tapi.DeregisterReadyStateCb(s_stateChangedCb);
94 if (ret != (int)TapiError.Success)
96 Log.Error(TapiUtility.LogTag, "Failed to deregister tapi state changed callback, Error: " + (TapiError)ret);
101 /// Fetches a list of available CPs.
103 /// <returns>List of available CPs in case of success. Null in case of failure.</returns>
104 /// <feature>http://tizen.org/feature/network.telephony</feature>
105 public static IEnumerable<string> GetCpNames()
107 IntPtr cpNames = Interop.Tapi.GetCpNames();
108 if(cpNames == IntPtr.Zero)
115 List<string> cpList = new List<string>();
117 IntPtr cp = Marshal.ReadIntPtr(cpNames, offset);
118 if(cp != IntPtr.Zero)
122 string cpString = Marshal.PtrToStringAnsi(cp);
123 offset += Marshal.SizeOf(cp);
124 cpList.Add(cpString);
127 while ((cp = Marshal.ReadIntPtr(cpNames, offset)) != IntPtr.Zero);
135 /// Acquires a TAPI Handle for the specified CP name.
137 /// <param name="cpName">The CP Name against which a TAPI handle is required (A NULL CP Name will return a #TapiHandle bound to the first CP in the list of available CPs).</param>
138 /// <returns>Instance of TapiHandle on success, null on failure.</returns>
139 /// <feature>http://tizen.org/feature/network.telephony</feature>
140 public static TapiHandle InitTapi(string cpName)
142 IntPtr handle = Interop.Tapi.InitTapi(cpName);
143 if (handle == IntPtr.Zero)
148 return new TapiHandle(handle);
152 /// Deinitializes the TAPI Handle.
154 /// <feature>http://tizen.org/feature/network.telephony</feature>
155 /// <exception cref="NotSupportedException">Thrown when telephony feature is not supported.</exception>
156 /// <exception cref="ArgumentException">Thrown when it is failed due to invalid parameter.</exception>
157 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation.</exception>
158 public static void DeinitTapi(TapiHandle handle)
160 int ret = Interop.Tapi.DeinitTapi(handle._handle);
161 if (ret != (int)TapiError.Success)
163 Log.Error(TapiUtility.LogTag, "Failed to deinitialize tapi, Error: " + (TapiError)ret);
164 TapiUtility.ThrowTapiException(ret, handle._handle);
167 handle._handle = IntPtr.Zero;