/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace Tizen.Tapi
{
///
/// This class provides functions for sending oem related data.
///
public class Oem
{
private IntPtr _handle;
private Dictionary _response_map = new Dictionary();
private int _requestId = 0;
///
/// A public constructor for Oem class to create a Oem instance for the given tapi handle.
///
/// The tapi handle.
public Oem(TapiHandle handle)
{
if (handle == null)
{
throw new ArgumentNullException("TapiHandle parameter is null");
}
_handle = handle._handle;
}
///
/// Sends oem data directly.
///
/// Oem id for user specification.
/// Oem data for sending.
/// http://tizen.org/privilege/telephony.admin
/// platform
/// http://tizen.org/feature/network.telephony
/// Thrown when feature is not supported.
/// Thrown when privilege access is denied.
/// Thrown when oem instance is invalid or when method failed due to invalid operation.
public void SendOemData(int oemId, byte[] data)
{
int length = data.Length;
IntPtr oemData = Marshal.AllocHGlobal(length);
Marshal.Copy(data, 0, oemData, length);
int ret = Interop.Tapi.Oem.SendOemData(_handle, oemId, oemData, Convert.ToUInt32(length));
if (ret != (int)TapiError.Success)
{
Log.Error(TapiUtility.LogTag, "Failed to send oem data directly, Error: " + (TapiError)ret);
TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
}
}
///
/// Sends oem data directly.
///
/// Oem id for user specification.
/// Oem data for sending.
/// The oem data which is sent.
/// http://tizen.org/privilege/telephony.admin
/// platform
/// http://tizen.org/feature/network.telephony
/// Thrown when feature is not supported.
/// Thrown when privilege access is denied.
/// Thrown when call instance is invalid or when method failed due to invalid operation.
public OemData SendOemDataSync(int oemId, byte[] data)
{
OemDataStruct oemStruct;
int length = data.Length;
IntPtr oemData = Marshal.AllocHGlobal(length);
Marshal.Copy(data, 0, oemData, length);
int ret = Interop.Tapi.Oem.SendOemDataSync(_handle, oemId, oemData, Convert.ToUInt32(length), out oemStruct);
if (ret != (int)TapiError.Success)
{
Log.Error(TapiUtility.LogTag, "Failed to send oem data directly, Error: " + (TapiError)ret);
TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
}
OemData oemDataSend = OemStructConversions.ConvertOemStruct(oemStruct);
return oemDataSend;
}
///
/// Sends oem data directly.
///
/// Oem id for user specification.
/// Oem data for sending.
/// The oem data which is sent.
/// http://tizen.org/privilege/telephony.admin
/// platform
/// http://tizen.org/feature/network.telephony
/// Thrown when feature is not supported.
/// Thrown when privilege access is denied.
/// Thrown when call instance is invalid or when method failed due to invalid operation.
public Task SendOemDataAsync(int oemId, byte[] data)
{
TaskCompletionSource task = new TaskCompletionSource();
IntPtr id;
id = (IntPtr)_requestId++;
_response_map[id] = (IntPtr handle, int result, IntPtr dataResponse, IntPtr key) =>
{
Task resultTask = new Task(() =>
{
if (result != (int)NetworkOperationCause.NoError)
{
Log.Error(TapiUtility.LogTag, "Error occurs during sending oem data, " + (NetworkOperationCause)result);
task.SetException(new InvalidOperationException("Error occurs during sending oem data, " + (NetworkOperationCause)result));
return;
}
OemDataStruct oemStruct = Marshal.PtrToStructure(dataResponse);
OemData oemClass = OemStructConversions.ConvertOemStruct(oemStruct);
task.SetResult(oemClass);
});
resultTask.Start();
resultTask.Wait();
_response_map.Remove(key);
};
int length = data.Length;
IntPtr oemData = Marshal.AllocHGlobal(length);
Marshal.Copy(data, 0, oemData, length);
int ret = Interop.Tapi.Oem.SendOemDataAsync(_handle, oemId, oemData, Convert.ToUInt32(length), _response_map[id], id);
if (ret != (int)TapiError.Success)
{
Log.Error(TapiUtility.LogTag, "Failed to send oem data, Error: " + (TapiError)ret);
TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
}
return task.Task;
}
}
}