2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
25 /// This class provides functions for sending oem related data.
27 /// <since_tizen> 4 </since_tizen>
30 private IntPtr _handle;
31 private Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback> _response_map = new Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback>();
32 private int _requestId = 0;
35 /// A public constructor for Oem class to create a Oem instance for the given tapi handle.
37 /// <since_tizen> 4 </since_tizen>
38 /// <param name="handle">The tapi handle.</param>
39 public Oem(TapiHandle handle)
43 throw new ArgumentNullException("TapiHandle parameter is null");
46 _handle = handle._handle;
50 /// Sends oem data directly.
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)
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)
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");
75 /// Sends oem data directly.
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)
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)
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");
100 OemData oemDataSend = OemStructConversions.ConvertOemStruct(oemStruct);
105 /// Sends oem data directly.
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)
119 TaskCompletionSource<OemData> task = new TaskCompletionSource<OemData>();
121 id = (IntPtr)_requestId++;
122 _response_map[id] = (IntPtr handle, int result, IntPtr dataResponse, IntPtr key) =>
124 Task resultTask = new Task(() =>
126 if (result != (int)NetworkOperationCause.NoError)
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));
133 OemDataStruct oemStruct = Marshal.PtrToStructure<OemDataStruct>(dataResponse);
134 OemData oemClass = OemStructConversions.ConvertOemStruct(oemStruct);
135 task.SetResult(oemClass);
140 _response_map.Remove(key);
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)
149 Log.Error(TapiUtility.LogTag, "Failed to send oem data, Error: " + (TapiError)ret);
150 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");