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 /// <since_tizen> 4 </since_tizen>
29 public static class TapiManager
31 private static event EventHandler<StateChangedEventArgs> s_stateChanged;
32 private static Interop.Tapi.TapiStateCallback s_stateChangedCb;
35 /// Gets the state value if tapi is ready.
37 /// <since_tizen> 4 </since_tizen>
38 /// <value>The State value in integer format - 0 is False and 1 is True. Returns -1 in case of error.</value>
39 public static int State
44 int ret = Interop.Tapi.GetReadyState(out state);
45 if (ret != (int)TapiError.Success)
47 Log.Error(TapiUtility.LogTag, "Failed to get ready state of tapi, Error: " + (TapiError)ret);
56 /// This event is raised when Tapi ready state changes.
58 /// <since_tizen> 4 </since_tizen>
59 public static event EventHandler<StateChangedEventArgs> StateChanged
63 if (s_stateChanged == null)
65 RegisterStateChangedEvent();
68 s_stateChanged += value;
73 s_stateChanged -= value;
74 if (s_stateChanged == null)
76 UnregisterStateChangedEvent();
81 private static void RegisterStateChangedEvent()
83 s_stateChangedCb = (int state, IntPtr userData) =>
85 s_stateChanged?.Invoke(null, new StateChangedEventArgs(state));
87 int ret = Interop.Tapi.RegisterReadyStateCb(s_stateChangedCb, IntPtr.Zero);
88 if (ret != (int)TapiError.Success)
90 Log.Error(TapiUtility.LogTag, "Failed to register tapi state changed callback, Error: " + (TapiError)ret);
94 private static void UnregisterStateChangedEvent()
96 int ret = Interop.Tapi.DeregisterReadyStateCb(s_stateChangedCb);
97 if (ret != (int)TapiError.Success)
99 Log.Error(TapiUtility.LogTag, "Failed to deregister tapi state changed callback, Error: " + (TapiError)ret);
104 /// Fetches a list of available CPs.
106 /// <since_tizen> 4 </since_tizen>
107 /// <returns>List of available CPs in case of success. Null in case of failure.</returns>
108 /// <feature>http://tizen.org/feature/network.telephony</feature>
109 public static IEnumerable<string> GetCpNames()
111 IntPtr cpNames = Interop.Tapi.GetCpNames();
112 if(cpNames == IntPtr.Zero)
119 List<string> cpList = new List<string>();
121 IntPtr cp = Marshal.ReadIntPtr(cpNames, offset);
122 if(cp != IntPtr.Zero)
126 string cpString = Marshal.PtrToStringAnsi(cp);
127 offset += Marshal.SizeOf(cp);
128 cpList.Add(cpString);
131 while ((cp = Marshal.ReadIntPtr(cpNames, offset)) != IntPtr.Zero);
139 /// Acquires a TAPI Handle for the specified CP name.
141 /// <since_tizen> 4 </since_tizen>
142 /// <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>
143 /// <returns>Instance of TapiHandle on success, null on failure.</returns>
144 /// <feature>http://tizen.org/feature/network.telephony</feature>
145 public static TapiHandle InitTapi(string cpName)
147 IntPtr handle = Interop.Tapi.InitTapi(cpName);
148 if (handle == IntPtr.Zero)
153 return new TapiHandle(handle);
157 /// Deinitializes the TAPI Handle.
159 /// <since_tizen> 4 </since_tizen>
160 /// <feature>http://tizen.org/feature/network.telephony</feature>
161 /// <exception cref="NotSupportedException">Thrown when telephony feature is not supported.</exception>
162 /// <exception cref="ArgumentException">Thrown when it is failed due to invalid parameter.</exception>
163 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation.</exception>
164 public static void DeinitTapi(TapiHandle handle)
166 int ret = Interop.Tapi.DeinitTapi(handle._handle);
167 if (ret != (int)TapiError.Success)
169 Log.Error(TapiUtility.LogTag, "Failed to deinitialize tapi, Error: " + (TapiError)ret);
170 TapiUtility.ThrowTapiException(ret, handle._handle);
173 handle._handle = IntPtr.Zero;