727a097fb5306054be7d67d7d2cd5d711a730b64
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcNdefMessage.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.Collections.Generic;
20
21 namespace Tizen.Network.Nfc
22 {
23     /// <summary>
24     /// A class for Ndef Message information. It allows applications to use Ndef Message information.
25     /// </summary>
26     public class NfcNdefMessage : IDisposable
27     {
28         private bool disposed = false;
29         private IntPtr _messageHandle = IntPtr.Zero;
30         private List<NfcNdefRecord> _recordList = new List<NfcNdefRecord>();
31
32         /// <summary>
33         /// The number of record in NDEF message.
34         /// </summary>
35         public int RecordCount
36         {
37             get
38             {
39                 int recordCount;
40                 int ret = Interop.Nfc.NdefMessage.GetRecordCount(_messageHandle, out recordCount);
41                 if (ret != (int)NfcError.None)
42                 {
43                     Log.Error(Globals.LogTag, "Failed to get record count, Error - " + (NfcError)ret);
44                 }
45                 return recordCount;
46             }
47         }
48
49         /// <summary>
50         /// Creates a object for the access point.
51         /// </summary>
52         public NfcNdefMessage()
53         {
54             int ret = Interop.Nfc.NdefMessage.Create(out _messageHandle);
55
56             if (ret != (int)NfcError.None)
57             {
58                 Log.Error(Globals.LogTag, "Failed to create Ndef message, Error - " + (NfcError)ret);
59                 NfcErrorFactory.ThrowNfcException(ret);
60             }
61         }
62
63         internal NfcNdefMessage(IntPtr messageHandle)
64         {
65             _messageHandle = messageHandle;
66         }
67
68         ~NfcNdefMessage()
69         {
70             int ret = Interop.Nfc.NdefMessage.Destroy(_messageHandle);
71
72             if (ret != (int)NfcError.None)
73             {
74                 Log.Error(Globals.LogTag, "Failed to destroy ndef message, Error - " + (NfcError)ret);
75             }
76
77             Dispose(false);
78         }
79
80         public void Dispose()
81         {
82             Dispose(true);
83             GC.SuppressFinalize(this);
84         }
85
86         private void Dispose(bool disposing)
87         {
88             if (disposed)
89                 return;
90
91             if (disposing)
92             {
93                 // Free managed objects.
94             }
95             //Free unmanaged objects
96             disposed = true;
97         }
98
99         /// <summary>
100         /// Appends a record into NDEF message.
101         /// </summary>
102         /// <returns>Whether appending the record succeeded.</returns>
103         /// <param name="record">The NfcNdefRecord object that will be appended into NDEF message.</param>
104         public bool AppendRecord(NfcNdefRecord record)
105         {
106             bool isSuccess = true;
107
108             int ret = Interop.Nfc.NdefMessage.AppendRecord(_messageHandle, record.GetHandle());
109             if (ret != (int)NfcError.None)
110             {
111                 Log.Error(Globals.LogTag, "Failed to append record, Error - " + (NfcError)ret);
112                 isSuccess = false;
113             }
114             else
115             {
116                 _recordList.Add(record);
117             }
118
119             return isSuccess;
120         }
121
122         /// <summary>
123         /// Inserts a record at index into NDEF message.
124         /// </summary>
125         /// <returns>Whether insterting the record succeeded.</returns>
126         /// <param name="index">The index of record ( starts from 0 ).</param>
127         /// <param name="record">The NfcNdefRecord object that will be appended into NDEF message.</param>
128         public bool InsertRecord(int index, NfcNdefRecord record)
129         {
130             bool isSuccess = true;
131
132             int ret = Interop.Nfc.NdefMessage.InsertRecord(_messageHandle, index, record.GetHandle());
133             if (ret != (int)NfcError.None)
134             {
135                 Log.Error(Globals.LogTag, "Failed to insert record, Error - " + (NfcError)ret);
136                 isSuccess = false;
137             }
138             else
139             {
140                 _recordList.Add(record);
141             }
142
143             return isSuccess;
144         }
145
146         /// <summary>
147         /// Inserts a record at index into NDEF message.
148         /// </summary>
149         /// <returns>Whether removing the record succeeded.</returns>
150         /// <param name="index">The index of record ( starts from 0 ).</param>
151         public bool RemoveRecord(int index)
152         {
153             bool isSuccess = true;
154
155             int ret = Interop.Nfc.NdefMessage.RemoveRecord(_messageHandle, index);
156             if (ret != (int)NfcError.None)
157             {
158                 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
159                 isSuccess = false;
160             }
161
162             return isSuccess;
163         }
164
165         /// <summary>
166         /// Gets record by index.
167         /// </summary>
168         /// <returns>The NfcNdefRecord object.</returns>
169         /// <param name="index">The index of record ( starts from 0 ).</param>
170         public NfcNdefRecord GetRecord(int index)
171         {
172             IntPtr recordHandle;
173             NfcNdefRecord recordObject = null;
174
175             int ret = Interop.Nfc.NdefMessage.GetRecord(_messageHandle, index, out recordHandle);
176             if (ret != (int)NfcError.None)
177             {
178                 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
179             }
180
181             foreach (NfcNdefRecord recordElement in _recordList)
182             {
183                 if(recordElement.GetHandle() == recordHandle)
184                 {
185                     Log.Debug(Globals.LogTag, "Find record handle");
186                     recordObject = recordElement;
187                     break;
188                 }
189             }
190
191             return recordObject;
192         }
193
194         internal IntPtr GetHandle()
195         {
196             return _messageHandle;
197         }
198     }
199 }