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.
29 private IntPtr _handle;
30 private Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback> _response_map = new Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback>();
31 private int _requestId = 0;
34 /// A public constructor for Oem class to create a Oem instance for the given tapi handle.
36 /// <param name="handle">The tapi handle.</param>
37 public Oem(TapiHandle handle)
41 throw new ArgumentNullException("TapiHandle parameter is null");
44 _handle = handle._handle;
48 /// Sends oem data directly.
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)
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)
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");
72 /// Sends oem data directly.
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)
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)
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");
96 OemData oemDataSend = OemStructConversions.ConvertOemStruct(oemStruct);
101 /// Sends oem data directly.
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)
114 TaskCompletionSource<OemData> task = new TaskCompletionSource<OemData>();
116 id = (IntPtr)_requestId++;
117 _response_map[id] = (IntPtr handle, int result, IntPtr dataResponse, IntPtr key) =>
119 Task resultTask = new Task(() =>
121 if (result != (int)NetworkOperationCause.NoError)
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));
128 OemDataStruct oemStruct = Marshal.PtrToStructure<OemDataStruct>(dataResponse);
129 OemData oemClass = OemStructConversions.ConvertOemStruct(oemStruct);
130 task.SetResult(oemClass);
135 _response_map.Remove(key);
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)
144 Log.Error(TapiUtility.LogTag, "Failed to send oem data, Error: " + (TapiError)ret);
145 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");