[Tapi] Mark API Level as 4
[platform/core/csapi/tizenfx.git] / internal / src / Tizen.Tapi / Tizen.Tapi / Oem.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 using System.Threading.Tasks;
21
22 namespace Tizen.Tapi
23 {
24     /// <summary>
25     /// This class provides functions for sending oem related data.
26     /// </summary>
27     /// <since_tizen> 4 </since_tizen>
28     public class Oem
29     {
30         private IntPtr _handle;
31         private Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback> _response_map = new Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback>();
32         private int _requestId = 0;
33
34         /// <summary>
35         /// A public constructor for Oem class to create a Oem instance for the given tapi handle.
36         /// </summary>
37         /// <since_tizen> 4 </since_tizen>
38         /// <param name="handle">The tapi handle.</param>
39         public Oem(TapiHandle handle)
40         {
41             if (handle == null)
42             {
43                 throw new ArgumentNullException("TapiHandle parameter is null");
44             }
45
46             _handle = handle._handle;
47         }
48
49         /// <summary>
50         /// Sends oem data directly.
51         /// </summary>
52         /// <since_tizen> 4 </since_tizen>
53         /// <param name="oemId">Oem id for user specification.</param>
54         /// <param name="data">Oem data for sending.</param>
55         /// <privilege>http://tizen.org/privilege/telephony.admin</privilege>
56         /// <privlevel>platform</privlevel>
57         /// <feature>http://tizen.org/feature/network.telephony</feature>
58         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
59         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
60         /// <exception cref="System.InvalidOperationException">Thrown when oem instance is invalid or when method failed due to invalid operation.</exception>
61         public void SendOemData(int oemId, byte[] data)
62         {
63             int length = data.Length;
64             IntPtr oemData = Marshal.AllocHGlobal(length);
65             Marshal.Copy(data, 0, oemData, length);
66             int ret = Interop.Tapi.Oem.SendOemData(_handle, oemId, oemData, Convert.ToUInt32(length));
67             if (ret != (int)TapiError.Success)
68             {
69                 Log.Error(TapiUtility.LogTag, "Failed to send oem data directly, Error: " + (TapiError)ret);
70                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
71             }
72         }
73
74         /// <summary>
75         /// Sends oem data directly.
76         /// </summary>
77         /// <since_tizen> 4 </since_tizen>
78         /// <param name="oemId">Oem id for user specification.</param>
79         /// <param name="data">Oem data for sending.</param>
80         /// <returns>The oem data which is sent.</returns>
81         /// <privilege>http://tizen.org/privilege/telephony.admin</privilege>
82         /// <privlevel>platform</privlevel>
83         /// <feature>http://tizen.org/feature/network.telephony</feature>
84         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
85         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
86         /// <exception cref="System.InvalidOperationException">Thrown when call instance is invalid or when method failed due to invalid operation.</exception>
87         public OemData SendOemDataSync(int oemId, byte[] data)
88         {
89             OemDataStruct oemStruct;
90             int length = data.Length;
91             IntPtr oemData = Marshal.AllocHGlobal(length);
92             Marshal.Copy(data, 0, oemData, length);
93             int ret = Interop.Tapi.Oem.SendOemDataSync(_handle, oemId, oemData, Convert.ToUInt32(length), out oemStruct);
94             if (ret != (int)TapiError.Success)
95             {
96                 Log.Error(TapiUtility.LogTag, "Failed to send oem data directly, Error: " + (TapiError)ret);
97                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
98             }
99
100             OemData oemDataSend = OemStructConversions.ConvertOemStruct(oemStruct);
101             return oemDataSend;
102         }
103
104         /// <summary>
105         /// Sends oem data directly.
106         /// </summary>
107         /// <since_tizen> 4 </since_tizen>
108         /// <param name="oemId">Oem id for user specification.</param>
109         /// <param name="data">Oem data for sending.</param>
110         /// <returns>The oem data which is sent.</returns>
111         /// <privilege>http://tizen.org/privilege/telephony.admin</privilege>
112         /// <privlevel>platform</privlevel>
113         /// <feature>http://tizen.org/feature/network.telephony</feature>
114         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
115         /// <exception cref="System.UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
116         /// <exception cref="System.InvalidOperationException">Thrown when call instance is invalid or when method failed due to invalid operation.</exception>
117         public Task<OemData> SendOemDataAsync(int oemId, byte[] data)
118         {
119             TaskCompletionSource<OemData> task = new TaskCompletionSource<OemData>();
120             IntPtr id;
121             id = (IntPtr)_requestId++;
122             _response_map[id] = (IntPtr handle, int result, IntPtr dataResponse, IntPtr key) =>
123             {
124                 Task resultTask = new Task(() =>
125                 {
126                     if (result != (int)NetworkOperationCause.NoError)
127                     {
128                         Log.Error(TapiUtility.LogTag, "Error occurs during sending oem data, " + (NetworkOperationCause)result);
129                         task.SetException(new InvalidOperationException("Error occurs during sending oem data, " + (NetworkOperationCause)result));
130                         return;
131                     }
132
133                     OemDataStruct oemStruct = Marshal.PtrToStructure<OemDataStruct>(dataResponse);
134                     OemData oemClass = OemStructConversions.ConvertOemStruct(oemStruct);
135                     task.SetResult(oemClass);
136                 });
137
138                 resultTask.Start();
139                 resultTask.Wait();
140                 _response_map.Remove(key);
141             };
142
143             int length = data.Length;
144             IntPtr oemData = Marshal.AllocHGlobal(length);
145             Marshal.Copy(data, 0, oemData, length);
146             int ret = Interop.Tapi.Oem.SendOemDataAsync(_handle, oemId, oemData, Convert.ToUInt32(length), _response_map[id], id);
147             if (ret != (int)TapiError.Success)
148             {
149                 Log.Error(TapiUtility.LogTag, "Failed to send oem data, Error: " + (TapiError)ret);
150                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
151             }
152
153             return task.Task;
154         }
155     }
156 }