Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Convergence / Tizen.Convergence / InternalPayload.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.Runtime.InteropServices;
19 using System.ComponentModel;
20
21 namespace Tizen.Convergence
22 {
23     /// <summary>
24     /// Represents a payload used in Tizen D2D convergence
25     /// </summary>
26     [EditorBrowsable(EditorBrowsableState.Never)]
27     public class InternalPayload : IDisposable
28     {
29         internal Interop.Internal.ConvPayloadHandle _payloadHandle;
30
31         /// <summary>
32         /// The constructor
33         /// </summary>
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()
37         {
38             int ret = Interop.Internal.ConvPayload.Create(out _payloadHandle);
39             if (ret != (int)ConvErrorCode.None)
40             {
41                 Log.Error(ErrorFactory.LogTag, "Failed to create payload");
42                 throw ErrorFactory.GetException(ret);
43             }
44         }
45
46         internal InternalPayload(IntPtr payloadHandle)
47         {
48             _payloadHandle = new Interop.Internal.ConvPayloadHandle(payloadHandle, false);
49         }
50
51         /// <summary>
52         /// Adds a key-value pair to payload
53         /// </summary>
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)
60         {
61             if(key == null || value == null)
62             {
63                 throw new ArgumentNullException();
64             }
65
66             int ret = 0;
67             if (value is string)
68             {
69                 ret = Interop.Internal.ConvPayload.SetString(_payloadHandle, key, value);
70                 if (ret != (int)ConvErrorCode.None)
71                 {
72                     Log.Error(ErrorFactory.LogTag, "Failed to add string to payload");
73                     throw ErrorFactory.GetException(ret);
74                 }
75             }
76         }
77
78         /// <summary>
79         /// Adds a key-value pair to payload
80         /// </summary>
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)
87         {
88             if (key == null || value == null)
89             {
90                 throw new ArgumentNullException();
91             }
92
93             int ret = 0;
94             ret = Interop.Internal.ConvPayload.SetByte(_payloadHandle, key, value.Length, value);
95             if (ret != (int)ConvErrorCode.None)
96             {
97                 Log.Error(ErrorFactory.LogTag, "Failed to add string to payload");
98                 throw ErrorFactory.GetException(ret);
99             }
100         }
101
102
103         /// <summary>
104         /// Sets binary to payload.
105         /// </summary>
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)
111         {
112             if (value == null)
113             {
114                 throw new ArgumentNullException();
115             }
116
117             int ret = 0;
118             ret = Interop.Internal.ConvPayload.SetBinary(_payloadHandle, value.Length, value);
119             if (ret != (int)ConvErrorCode.None)
120             {
121                 Log.Error(ErrorFactory.LogTag, "Failed to add binary to payload");
122                 throw ErrorFactory.GetException(ret);
123             }
124         }
125
126         /// <summary>
127         /// Gets the value of the key from payload
128         /// </summary>
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)
136         {
137             if (key == null)
138             {
139                 throw new ArgumentNullException();
140             }
141
142             IntPtr stringPtr = IntPtr.Zero;
143             int ret = 0;
144             ret = Interop.Internal.ConvPayload.GetString(_payloadHandle, key, out stringPtr);
145             if (ret != (int)ConvErrorCode.None)
146             {
147                 Log.Error(ErrorFactory.LogTag, "Failed to add string to payload");
148                 throw ErrorFactory.GetException(ret);
149             }
150
151             value = Marshal.PtrToStringAnsi(stringPtr);
152             Interop.Libc.Free(stringPtr);
153         }
154
155         /// <summary>
156         /// Gets the value of the key from payload
157         /// </summary>
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)
165         {
166             if (key == null)
167             {
168                 throw new ArgumentNullException();
169             }
170
171             int ret = 0;
172             IntPtr byteArrayPtr;
173             int byteArraySize;
174             ret = Interop.Internal.ConvPayload.GetByte(_payloadHandle, key, out byteArraySize, out byteArrayPtr);
175             if (ret != (int)ConvErrorCode.None)
176             {
177                 Log.Error(ErrorFactory.LogTag, "Failed to add string to payload");
178                 throw ErrorFactory.GetException(ret);
179             }
180
181             byte[] byteArray = new byte[byteArraySize];
182             Marshal.Copy(byteArrayPtr, byteArray, 0, byteArraySize);
183             value = byteArray;
184             Interop.Libc.Free(byteArrayPtr);
185         }
186
187         /// <summary>
188         /// Gets binary from payload
189         /// </summary>
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)
194         {
195             int ret = 0;
196             IntPtr byteArrayPtr;
197             int byteArraySize;
198             ret = Interop.Internal.ConvPayload.GetBinary(_payloadHandle, out byteArraySize, out byteArrayPtr);
199             if (ret != (int)ConvErrorCode.None)
200             {
201                 Log.Error(ErrorFactory.LogTag, "Failed to get binary from payload");
202                 throw ErrorFactory.GetException(ret);
203             }
204
205             byte[] byteArray = new byte[byteArraySize];
206             Marshal.Copy(byteArrayPtr, byteArray, 0, byteArraySize);
207             value = byteArray;
208             Interop.Libc.Free(byteArrayPtr);
209         }
210
211         /// <summary>
212         /// The dispose method
213         /// </summary>
214         public void Dispose()
215         {
216             _payloadHandle.Dispose();
217         }
218     }
219 }