[Tapi] Mark API Level as 4
[platform/core/csapi/tizenfx.git] / internal / src / Tizen.Tapi / Tizen.Tapi / TapiManager.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Runtime.InteropServices;
21 using Tizen;
22
23 namespace Tizen.Tapi
24 {
25     /// <summary>
26     /// This class is used for initializing/deinitializing Tapi and manages the state of it.
27     /// </summary>
28     /// <since_tizen> 4 </since_tizen>
29     public static class TapiManager
30     {
31         private static event EventHandler<StateChangedEventArgs> s_stateChanged;
32         private static Interop.Tapi.TapiStateCallback s_stateChangedCb;
33
34         /// <summary>
35         /// Gets the state value if tapi is ready.
36         /// </summary>
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
40         {
41             get
42             {
43                 int state;
44                 int ret = Interop.Tapi.GetReadyState(out state);
45                 if (ret != (int)TapiError.Success)
46                 {
47                     Log.Error(TapiUtility.LogTag, "Failed to get ready state of tapi, Error: " + (TapiError)ret);
48                     return -1;
49                 }
50
51                 return state;
52             }
53         }
54
55         /// <summary>
56         /// This event is raised when Tapi ready state changes.
57         /// </summary>
58         /// <since_tizen> 4 </since_tizen>
59         public static event EventHandler<StateChangedEventArgs> StateChanged
60         {
61             add
62             {
63                 if (s_stateChanged == null)
64                 {
65                     RegisterStateChangedEvent();
66                 }
67
68                 s_stateChanged += value;
69             }
70
71             remove
72             {
73                 s_stateChanged -= value;
74                 if (s_stateChanged == null)
75                 {
76                     UnregisterStateChangedEvent();
77                 }
78             }
79         }
80
81         private static void RegisterStateChangedEvent()
82         {
83             s_stateChangedCb = (int state, IntPtr userData) =>
84             {
85                 s_stateChanged?.Invoke(null, new StateChangedEventArgs(state));
86             };
87             int ret = Interop.Tapi.RegisterReadyStateCb(s_stateChangedCb, IntPtr.Zero);
88             if (ret != (int)TapiError.Success)
89             {
90                 Log.Error(TapiUtility.LogTag, "Failed to register tapi state changed callback, Error: " + (TapiError)ret);
91             }
92         }
93
94         private static void UnregisterStateChangedEvent()
95         {
96             int ret = Interop.Tapi.DeregisterReadyStateCb(s_stateChangedCb);
97             if (ret != (int)TapiError.Success)
98             {
99                 Log.Error(TapiUtility.LogTag, "Failed to deregister tapi state changed callback, Error: " + (TapiError)ret);
100             }
101         }
102
103         /// <summary>
104         /// Fetches a list of available CPs.
105         /// </summary>
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()
110         {
111             IntPtr cpNames = Interop.Tapi.GetCpNames();
112             if(cpNames == IntPtr.Zero)
113             {
114                 return null;
115             }
116
117             else
118             {
119                 List<string> cpList = new List<string>();
120                 int offset = 0;
121                 IntPtr cp = Marshal.ReadIntPtr(cpNames, offset);
122                 if(cp != IntPtr.Zero)
123                 {
124                     do
125                     {
126                         string cpString = Marshal.PtrToStringAnsi(cp);
127                         offset += Marshal.SizeOf(cp);
128                         cpList.Add(cpString);
129                     }
130
131                     while ((cp = Marshal.ReadIntPtr(cpNames, offset)) != IntPtr.Zero);
132                 }
133
134                 return cpList;
135             }
136         }
137
138         /// <summary>
139         /// Acquires a TAPI Handle for the specified CP name.
140         /// </summary>
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)
146         {
147             IntPtr handle = Interop.Tapi.InitTapi(cpName);
148             if (handle == IntPtr.Zero)
149             {
150                 return null;
151             }
152
153             return new TapiHandle(handle);
154         }
155
156         /// <summary>
157         /// Deinitializes the TAPI Handle.
158         /// </summary>
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)
165         {
166             int ret = Interop.Tapi.DeinitTapi(handle._handle);
167             if (ret != (int)TapiError.Success)
168             {
169                 Log.Error(TapiUtility.LogTag, "Failed to deinitialize tapi, Error: " + (TapiError)ret);
170                 TapiUtility.ThrowTapiException(ret, handle._handle);
171             }
172
173             handle._handle = IntPtr.Zero;
174         }
175     }
176 }