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.Runtime.InteropServices;
19 using System.ComponentModel;
21 namespace Tizen.Convergence
24 /// Represents a payload used in Tizen D2D convergence
26 [EditorBrowsable(EditorBrowsableState.Never)]
27 public class InternalPayload : IDisposable
29 internal Interop.Internal.ConvPayloadHandle _payloadHandle;
34 /// <feature>http://tizen.org/feature/convergence.d2d</feature>
35 /// <exception cref="NotSupportedException">Thrown if the required feature is not supported.</exception>
36 public InternalPayload()
38 int ret = Interop.Internal.ConvPayload.Create(out _payloadHandle);
39 if (ret != (int)ConvErrorCode.None)
41 Log.Error(ErrorFactory.LogTag, "Failed to create payload");
42 throw ErrorFactory.GetException(ret);
46 internal InternalPayload(IntPtr payloadHandle)
48 _payloadHandle = new Interop.Internal.ConvPayloadHandle(payloadHandle, false);
52 /// Adds a key-value pair to payload
54 /// <param name="key">The key of the attribute</param>
55 /// <param name="value">The value of the attribute</param>
56 /// <feature>http://tizen.org/feature/convergence.d2d</feature>
57 /// <exception cref="NotSupportedException">Thrown if the required feature is not supported.</exception>
58 /// <exception cref="ArgumentNullException">Thrown if the arguments passed are null </exception>
59 public void Set(string key, string value)
61 if(key == null || value == null)
63 throw new ArgumentNullException();
69 ret = Interop.Internal.ConvPayload.SetString(_payloadHandle, key, value);
70 if (ret != (int)ConvErrorCode.None)
72 Log.Error(ErrorFactory.LogTag, "Failed to add string to payload");
73 throw ErrorFactory.GetException(ret);
79 /// Adds a key-value pair to payload
81 /// <param name="key">The key of the attribute</param>
82 /// <param name="value">The value of the attribute</param>
83 /// <feature>http://tizen.org/feature/convergence.d2d</feature>
84 /// <exception cref="NotSupportedException">Thrown if the required feature is not supported.</exception>
85 /// <exception cref="ArgumentNullException">Thrown if the arguments passed are null </exception>
86 public void Set(string key, byte[] value)
88 if (key == null || value == null)
90 throw new ArgumentNullException();
94 ret = Interop.Internal.ConvPayload.SetByte(_payloadHandle, key, value.Length, value);
95 if (ret != (int)ConvErrorCode.None)
97 Log.Error(ErrorFactory.LogTag, "Failed to add string to payload");
98 throw ErrorFactory.GetException(ret);
104 /// Sets binary to payload.
106 /// <param name="value">The binary to set</param>
107 /// <feature>http://tizen.org/feature/convergence.d2d</feature>
108 /// <exception cref="NotSupportedException">Thrown if the required feature is not supported.</exception>
109 /// <exception cref="ArgumentNullException">Thrown if the arguments passed are null </exception>
110 public void Set(byte[] value)
114 throw new ArgumentNullException();
118 ret = Interop.Internal.ConvPayload.SetBinary(_payloadHandle, value.Length, value);
119 if (ret != (int)ConvErrorCode.None)
121 Log.Error(ErrorFactory.LogTag, "Failed to add binary to payload");
122 throw ErrorFactory.GetException(ret);
127 /// Gets the value of the key from payload
129 /// <param name="key">The key of the attribute</param>
130 /// <param name="value">The value of the attribute</param>
131 /// <feature>http://tizen.org/feature/convergence.d2d</feature>
132 /// <exception cref="NotSupportedException">Thrown if the required feature is not supported.</exception>
133 /// <exception cref="ArgumentNullException">Thrown if the arguments passed are null </exception>
134 /// <exception cref="ArgumentException">Thrown if the key is not found </exception>
135 public void Get(string key, out string value)
139 throw new ArgumentNullException();
142 IntPtr stringPtr = IntPtr.Zero;
144 ret = Interop.Internal.ConvPayload.GetString(_payloadHandle, key, out stringPtr);
145 if (ret != (int)ConvErrorCode.None)
147 Log.Error(ErrorFactory.LogTag, "Failed to add string to payload");
148 throw ErrorFactory.GetException(ret);
151 value = Marshal.PtrToStringAnsi(stringPtr);
152 Interop.Libc.Free(stringPtr);
156 /// Gets the value of the key from payload
158 /// <param name="key">The key of the attribute</param>
159 /// <param name="value">The value of the attribute</param>
160 /// <feature>http://tizen.org/feature/convergence.d2d</feature>
161 /// <exception cref="NotSupportedException">Thrown if the required feature is not supported.</exception>
162 /// <exception cref="ArgumentNullException">Thrown if the arguments passed are null </exception>
163 /// <exception cref="ArgumentException">Thrown if the key is not found </exception>
164 public void Get(string key, out byte[] value)
168 throw new ArgumentNullException();
174 ret = Interop.Internal.ConvPayload.GetByte(_payloadHandle, key, out byteArraySize, out byteArrayPtr);
175 if (ret != (int)ConvErrorCode.None)
177 Log.Error(ErrorFactory.LogTag, "Failed to add string to payload");
178 throw ErrorFactory.GetException(ret);
181 byte[] byteArray = new byte[byteArraySize];
182 Marshal.Copy(byteArrayPtr, byteArray, 0, byteArraySize);
184 Interop.Libc.Free(byteArrayPtr);
188 /// Gets binary from payload
190 /// <param name="value">The result value</param>
191 /// <feature>http://tizen.org/feature/convergence.d2d</feature>
192 /// <exception cref="NotSupportedException">Thrown if the required feature is not supported.</exception>
193 public void Get(out byte[] value)
198 ret = Interop.Internal.ConvPayload.GetBinary(_payloadHandle, out byteArraySize, out byteArrayPtr);
199 if (ret != (int)ConvErrorCode.None)
201 Log.Error(ErrorFactory.LogTag, "Failed to get binary from payload");
202 throw ErrorFactory.GetException(ret);
205 byte[] byteArray = new byte[byteArraySize];
206 Marshal.Copy(byteArrayPtr, byteArray, 0, byteArraySize);
208 Interop.Libc.Free(byteArrayPtr);
212 /// The dispose method
214 public void Dispose()
216 _payloadHandle.Dispose();