Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Telephony / Tizen.Telephony / SlotHandle.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.Runtime.InteropServices;
20
21 namespace Tizen.Telephony
22 {
23     /// <summary>
24     /// This Class provides API's that provides functionality related to slot handle.
25     /// </summary>
26     public class SlotHandle
27     {
28         internal IntPtr _handle;
29         private List<Interop.Telephony.NotificationCallback> _changeNotificationList = new List<Interop.Telephony.NotificationCallback>();
30
31         internal SlotHandle(IntPtr handle)
32         {
33             _handle = handle;
34         }
35
36         /// <summary>
37         /// Event Handler for Receiving the Telephony State Changes
38         /// this event will be triggered for the NotificationId's given in the SetNotificationId API
39         /// </summary>
40         public event EventHandler<ChangeNotificationEventArgs> ChangeNotification;
41
42         internal IntPtr Handle
43         {
44             get
45             {
46                 return _handle;
47             }
48         }
49
50         /// <summary>
51         /// The Notification Id's for which the ChangeNotification event will be triggered
52         /// </summary>
53         /// <since_tizen> 3 </since_tizen>
54         /// <param name="list">
55         /// The List of Notification Id's for which the ChangeNotification event will be triggered
56         /// </param>
57         /// <feature>http://tizen.org/feature/network.telephony</feature>
58         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
59         /// <exception cref="InvalidOperationException">
60         /// This Exception can occur due to:
61         /// 1. Operation Not Supported
62         /// 2. Operation Failed
63         /// </exception>
64         public void SetNotificationId(IEnumerable<ChangeNotificationEventArgs.Notification> list)
65         {
66             try
67             {
68                 foreach (ChangeNotificationEventArgs.Notification n in list)
69                 {
70                     SetCallback(n);
71                 }
72             }
73             catch (Exception e)
74             {
75                 Tizen.Log.Error(Interop.Telephony.LogTag, "SetNotificationId Failed with Error " + e.ToString());
76                 throw e;
77             }
78         }
79
80         /// <summary>
81         /// The Notification Id's for which the ChangeNotification event will not be triggered
82         /// </summary>
83         /// <since_tizen> 3 </since_tizen>
84         /// <param name="list">
85         /// The List of Notification Id's for which the ChangeNotification event will be not be triggered
86         /// </param>
87         /// <feature>http://tizen.org/feature/network.telephony</feature>
88         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
89         /// <exception cref="InvalidOperationException">
90         /// This Exception can occur due to:
91         /// 1. Operation Not Supported
92         /// 2. Operation Failed
93         /// </exception>
94         public void RemoveNotificationId(IEnumerable<ChangeNotificationEventArgs.Notification> list)
95         {
96             foreach (ChangeNotificationEventArgs.Notification n in list)
97             {
98                 Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonyUnsetNotiCb(_handle, n);
99                 if (error != Interop.Telephony.TelephonyError.None)
100                 {
101                     throw ExceptionFactory.CreateException(error);
102                 }
103             }
104         }
105
106         private void SetCallback(ChangeNotificationEventArgs.Notification n)
107         {
108             Interop.Telephony.NotificationCallback NotificationDelegate = (IntPtr handle, ChangeNotificationEventArgs.Notification notiId, IntPtr data, IntPtr userData) =>
109             {
110                 SlotHandle simHandle = Manager.FindHandle(handle);
111                 object notiData = null;
112                 switch (notiId)
113                 {
114                     case ChangeNotificationEventArgs.Notification.SimStatus:
115                         {
116                             notiData = (Sim.State)Marshal.ReadInt32(data);
117                             break;
118                         }
119
120                     case ChangeNotificationEventArgs.Notification.SimCallForwardingIndicatorState:
121                         {
122                             notiData = ((Marshal.ReadInt32(data) == 0) ? false : true);
123                             break;
124                         }
125
126                     case ChangeNotificationEventArgs.Notification.NetworkServiceState:
127                         {
128                             notiData = (Network.ServiceState)Marshal.ReadInt32(data);
129                             break;
130                         }
131
132                     case ChangeNotificationEventArgs.Notification.NetworkCellid:
133                         {
134                             notiData = Marshal.ReadInt32(data);
135                             break;
136                         }
137
138                     case ChangeNotificationEventArgs.Notification.NetworkRoamingStatus:
139                         {
140                             notiData = (Marshal.ReadInt32(data) == 0) ? false : true;
141                             break;
142                         }
143
144                     case ChangeNotificationEventArgs.Notification.NetworkSignalstrengthLevel:
145                         {
146                             notiData = (Network.Rssi)Marshal.ReadInt32(data);
147                             break;
148                         }
149
150                     case ChangeNotificationEventArgs.Notification.NetworkNetworkName:
151                         {
152                             notiData = Marshal.PtrToStringAnsi(data);
153                             break;
154                         }
155
156                     case ChangeNotificationEventArgs.Notification.NetworkPsType:
157                         {
158                             notiData = (Network.PsType)Marshal.ReadInt32(data);
159                             break;
160                         }
161
162                     case ChangeNotificationEventArgs.Notification.NetworkDefaultDataSubscription:
163                         {
164                             notiData = (Network.DefaultDataSubscription)Marshal.ReadInt32(data);
165                             break;
166                         }
167
168                     case ChangeNotificationEventArgs.Notification.NetworkDefaultSubscription:
169                         {
170                             notiData = (Network.DefaultSubscription)Marshal.ReadInt32(data);
171                             break;
172                         }
173
174                     case ChangeNotificationEventArgs.Notification.NetworkLac:
175                         {
176                             notiData = Marshal.ReadInt32(data);
177                             break;
178                         }
179
180                     case ChangeNotificationEventArgs.Notification.NetworkTac:
181                         {
182                             notiData = Marshal.ReadInt32(data);
183                             break;
184                         }
185
186                     case ChangeNotificationEventArgs.Notification.NetworkSystemId:
187                         {
188                             notiData = Marshal.ReadInt32(data);
189                             break;
190                         }
191
192                     case ChangeNotificationEventArgs.Notification.NetworkId:
193                         {
194                             notiData = Marshal.ReadInt32(data);
195                             break;
196                         }
197
198                     case ChangeNotificationEventArgs.Notification.NetworkBsId:
199                         {
200                             notiData = Marshal.ReadInt32(data);
201                             break;
202                         }
203
204                     case ChangeNotificationEventArgs.Notification.NetworkBsLatitude:
205                         {
206                             notiData = Marshal.ReadInt32(data);
207                             break;
208                         }
209
210                     case ChangeNotificationEventArgs.Notification.NetworkBsLongitude:
211                         {
212                             notiData = Marshal.ReadInt32(data);
213                             break;
214                         }
215
216                     case ChangeNotificationEventArgs.Notification.VoiceCallStatusIdle:
217                         {
218                             notiData = (uint)Marshal.ReadInt32(data);
219                             break;
220                         }
221
222                     case ChangeNotificationEventArgs.Notification.VoiceCallStatusActive:
223                         {
224                             notiData = (uint)Marshal.ReadInt32(data);
225                             break;
226                         }
227
228                     case ChangeNotificationEventArgs.Notification.VoiceCallStatusHeld:
229                         {
230                             notiData = (uint)Marshal.ReadInt32(data);
231                             break;
232                         }
233
234                     case ChangeNotificationEventArgs.Notification.VoiceCallStatusDialing:
235                         {
236                             notiData = (uint)Marshal.ReadInt32(data);
237                             break;
238                         }
239
240                     case ChangeNotificationEventArgs.Notification.VoiceCallStatusAlerting:
241                         {
242                             notiData = (uint)Marshal.ReadInt32(data);
243                             break;
244                         }
245
246                     case ChangeNotificationEventArgs.Notification.VoiceCallStatusIncoming:
247                         {
248                             notiData = (uint)Marshal.ReadInt32(data);
249                             break;
250                         }
251
252                     case ChangeNotificationEventArgs.Notification.VideoCallStatusIdle:
253                         {
254                             notiData = (uint)Marshal.ReadInt32(data);
255                             break;
256                         }
257
258                     case ChangeNotificationEventArgs.Notification.VideoCallStatusActive:
259                         {
260                             notiData = (uint)Marshal.ReadInt32(data);
261                             break;
262                         }
263
264                     case ChangeNotificationEventArgs.Notification.VideoCallStatusDialing:
265                         {
266                             notiData = (uint)Marshal.ReadInt32(data);
267                             break;
268                         }
269
270                     case ChangeNotificationEventArgs.Notification.VideoCallStatusAlerting:
271                         {
272                             notiData = (uint)Marshal.ReadInt32(data);
273                             break;
274                         }
275
276                     case ChangeNotificationEventArgs.Notification.VideoCallStatusIncoming:
277                         {
278                             notiData = (uint)Marshal.ReadInt32(data);
279                             break;
280                         }
281
282                     case ChangeNotificationEventArgs.Notification.CallPreferredVoiceSubscription:
283                         {
284                             notiData = (CallPreferredVoiceSubscription)Marshal.ReadInt32(data);
285                             break;
286                         }
287
288                 }
289
290                 ChangeNotificationEventArgs args = new ChangeNotificationEventArgs(notiId, notiData);
291                 ChangeNotification?.Invoke(simHandle, args);
292             };
293             _changeNotificationList.Add(NotificationDelegate);
294
295             Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonySetNotiCb(_handle, n, NotificationDelegate, IntPtr.Zero);
296             if (error != Interop.Telephony.TelephonyError.None)
297             {
298                 Exception e = ExceptionFactory.CreateException(error);
299                 // Check if error is Invalid Parameter then hide the error
300                 if (e is ArgumentException)
301                 {
302                     e = new InvalidOperationException("Internal Error Occured");
303                 }
304
305                 throw e;
306             }
307         }
308     }
309 }