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