Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / 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     public static class TapiManager
29     {
30         private static event EventHandler<StateChangedEventArgs> s_stateChanged;
31         private static Interop.Tapi.TapiStateCallback s_stateChangedCb;
32
33         /// <summary>
34         /// Gets the state value if tapi is ready.
35         /// </summary>
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
38         {
39             get
40             {
41                 int state;
42                 int ret = Interop.Tapi.GetReadyState(out state);
43                 if (ret != (int)TapiError.Success)
44                 {
45                     Log.Error(TapiUtility.LogTag, "Failed to get ready state of tapi, Error: " + (TapiError)ret);
46                     return -1;
47                 }
48
49                 return state;
50             }
51         }
52
53         /// <summary>
54         /// This event is raised when Tapi ready state changes.
55         /// </summary>
56         public static event EventHandler<StateChangedEventArgs> StateChanged
57         {
58             add
59             {
60                 if (s_stateChanged == null)
61                 {
62                     RegisterStateChangedEvent();
63                 }
64
65                 s_stateChanged += value;
66             }
67
68             remove
69             {
70                 s_stateChanged -= value;
71                 if (s_stateChanged == null)
72                 {
73                     UnregisterStateChangedEvent();
74                 }
75             }
76         }
77
78         private static void RegisterStateChangedEvent()
79         {
80             s_stateChangedCb = (int state, IntPtr userData) =>
81             {
82                 s_stateChanged?.Invoke(null, new StateChangedEventArgs(state));
83             };
84             int ret = Interop.Tapi.RegisterReadyStateCb(s_stateChangedCb, IntPtr.Zero);
85             if (ret != (int)TapiError.Success)
86             {
87                 Log.Error(TapiUtility.LogTag, "Failed to register tapi state changed callback, Error: " + (TapiError)ret);
88             }
89         }
90
91         private static void UnregisterStateChangedEvent()
92         {
93             int ret = Interop.Tapi.DeregisterReadyStateCb(s_stateChangedCb);
94             if (ret != (int)TapiError.Success)
95             {
96                 Log.Error(TapiUtility.LogTag, "Failed to deregister tapi state changed callback, Error: " + (TapiError)ret);
97             }
98         }
99
100         /// <summary>
101         /// Fetches a list of available CPs.
102         /// </summary>
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()
106         {
107             IntPtr cpNames = Interop.Tapi.GetCpNames();
108             if(cpNames == IntPtr.Zero)
109             {
110                 return null;
111             }
112
113             else
114             {
115                 List<string> cpList = new List<string>();
116                 int offset = 0;
117                 IntPtr cp = Marshal.ReadIntPtr(cpNames, offset);
118                 if(cp != IntPtr.Zero)
119                 {
120                     do
121                     {
122                         string cpString = Marshal.PtrToStringAnsi(cp);
123                         offset += Marshal.SizeOf(cp);
124                         cpList.Add(cpString);
125                     }
126
127                     while ((cp = Marshal.ReadIntPtr(cpNames, offset)) != IntPtr.Zero);
128                 }
129
130                 return cpList;
131             }
132         }
133
134         /// <summary>
135         /// Acquires a TAPI Handle for the specified CP name.
136         /// </summary>
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)
141         {
142             IntPtr handle = Interop.Tapi.InitTapi(cpName);
143             if (handle == IntPtr.Zero)
144             {
145                 return null;
146             }
147
148             return new TapiHandle(handle);
149         }
150
151         /// <summary>
152         /// Deinitializes the TAPI Handle.
153         /// </summary>
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)
159         {
160             int ret = Interop.Tapi.DeinitTapi(handle._handle);
161             if (ret != (int)TapiError.Success)
162             {
163                 Log.Error(TapiUtility.LogTag, "Failed to deinitialize tapi, Error: " + (TapiError)ret);
164                 TapiUtility.ThrowTapiException(ret, handle._handle);
165             }
166
167             handle._handle = IntPtr.Zero;
168         }
169     }
170 }