summaryrefslogtreecommitdiff
path: root/src/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceInterfaces.cs
blob: 8d0f7899adc9057c1194be5f2b31426d6a725dd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
 /*
 * 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;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Tizen.Network.IoTConnectivity
{
    /// <summary>
    /// This class contains resource interfaces and provides APIs to manage, add, remove those interfaces.
    /// A resource interface indicates a class or category of resources.
    /// </summary>
    /// <since_tizen>3</since_tizen>
    public class ResourceInterfaces : IEnumerable<string>, IDisposable
    {
        /// <summary>
        /// Default Interface
        /// </summary>
        /// <since_tizen>3</since_tizen>
        public const string DefaultInterface = "oic.if.baseline";

        /// <summary>
        /// List Links Interface which is used to list the references to other resources contained in a resource.
        /// </summary>
        /// <since_tizen>3</since_tizen>
        public const string LinkInterface = "oic.if.ll";

        /// <summary>
        /// Batch Interface which is used to manipulate (GET, PUT, POST, DELETE) on other resource contained in a resource.
        /// </summary>
        /// <since_tizen>3</since_tizen>
        public const string BatchInterface = "oic.if.b";

        /// <summary>
        /// Group Interface which is used to manipulate (GET, PUT, POST) a group of remote resources.
        /// </summary>
        /// <since_tizen>3</since_tizen>
        public const string GroupInterface = "oic.mi.grp";

        /// <summary>
        /// Read-Only Interface which is used to limit the methods that can be applied to a resource to GET only.
        /// </summary>
        /// <since_tizen>3</since_tizen>
        public const string ReadonlyInterface = "oic.if.r";

        private readonly IntPtr _resourceInterfacesHandle = IntPtr.Zero;
        private const int MaxLength = 61;
        private readonly HashSet<string> _resourceInterfaces = new HashSet<string>();
        private bool _disposed = false;

        /// <summary>
        /// Constructor of ResourceInterfaces
        /// </summary>
        /// <since_tizen>3</since_tizen>
        /// <feature>http://tizen.org/feature/iot.ocf</feature>
        /// <seealso cref="Add()"/>
        /// <seealso cref="Remove()"/>
        /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
        /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
        /// <code>
        /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
        /// </code>
        public ResourceInterfaces()
        {
            int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
            if (ret != (int)IoTConnectivityError.None)
            {
                Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
                throw IoTConnectivityErrorFactory.GetException(ret);
            }
        }

        /// <summary>
        /// Constructor of ResourceInterfaces using list of interfaces
        /// </summary>
        /// <since_tizen>3</since_tizen>
        /// <param name="ifaces">List of resource interfaces</param>
        /// <feature>http://tizen.org/feature/iot.ocf</feature>
        /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
        /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
        /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
        /// <code>
        /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
        ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
        /// </code>
        public ResourceInterfaces(IEnumerable<string> ifaces)
        {
            int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
            if (ret != (int)IoTConnectivityError.None)
            {
                Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
                throw IoTConnectivityErrorFactory.GetException(ret);
            }
            foreach (string iface in ifaces)
            {
                Add(iface);
            }
        }

        internal ResourceInterfaces(IntPtr ifacesHandleToClone)
        {
            int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Clone(ifacesHandleToClone, out _resourceInterfacesHandle);
            if (ret != (int)IoTConnectivityError.None)
            {
                Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
                throw IoTConnectivityErrorFactory.GetException(ret);
            }

            Interop.IoTConnectivity.Common.ResourceInterfaces.ForeachCallback cb = (string iface, IntPtr data) =>
            {
                _resourceInterfaces.Add(iface);
                return true;
            };

            ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Foreach(ifacesHandleToClone, cb, IntPtr.Zero);
            if (ret != (int)IoTConnectivityError.None)
            {
                Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
                throw IoTConnectivityErrorFactory.GetException(ret);
            }
        }

        /// <summary>
        /// Destructor of the ResourceInterfaces class.
        /// </summary>
        ~ResourceInterfaces()
        {
            Dispose(false);
        }

        internal IntPtr ResourceInterfacesHandle
        {
            get
            {
                return _resourceInterfacesHandle;
            }
        }

        /// <summary>
        /// Indicates count of interfaces in the list
        /// </summary>
        /// <since_tizen>3</since_tizen>
        /// <value>Count of interfaces in the list.</value>
        /// <code>
        /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
        ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
        /// Console.WriteLine("There are {0} interfaces", resourceInterfaces.Count);
        /// </code>
        public int Count
        {
            get
            {
                return _resourceInterfaces.Count;
            }
        }

        /// <summary>
        /// Adds a resource interface into the list.
        /// </summary>
        /// <since_tizen>3</since_tizen>
        /// <remarks>
        /// @a item could be a value such as <see cref="DefaultInterface"/>
        /// </remarks>
        /// <param name="item">The string data to insert into the resource interfaces</param>
        /// <feature>http://tizen.org/feature/iot.ocf</feature>
        /// <seealso cref="Remove()"/>
        /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
        /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
        /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
        /// <code>
        /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
        /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
        /// </code>
        public void Add(string item)
        {
            if (IsValid(item))
            {
                int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Add(_resourceInterfacesHandle, item);
                if (ret != (int)IoTConnectivityError.None)
                {
                    Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
                    throw IoTConnectivityErrorFactory.GetException(ret);
                }
                _resourceInterfaces.Add(item);
            }
            else
            {
                Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid interface");
                throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
            }
        }

        /// <summary>
        /// Removes a resource interface from the list
        /// </summary>
        /// <since_tizen>3</since_tizen>
        /// <param name="item">The string data to delete from the resource ifaces</param>
        /// <feature>http://tizen.org/feature/iot.ocf</feature>
        /// <seealso cref="Add()"/>
        /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
        /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
        /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
        /// <code>
        /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.BatchInterface });
        /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
        /// </code>
        public void Remove(string item)
        {
            bool isRemoved = _resourceInterfaces.Remove(item);
            if (isRemoved)
            {
                int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Remove(_resourceInterfacesHandle, item);
                if (ret != (int)IoTConnectivityError.None)
                {
                    Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove interface");
                    throw IoTConnectivityErrorFactory.GetException(ret);
                }
            }
            else
                throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
        }

        /// <summary>
        /// Return enumerator for the list of interfaces
        /// </summary>
        /// <since_tizen>3</since_tizen>
        /// <returns>The enumerator</returns>
        /// <code>
        /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
        ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
        /// foreach(string item in resourceInterfaces)
        /// {
        ///     Console.WriteLine("Interface : {0}", item);
        /// }
        /// </code>
        public IEnumerator<string> GetEnumerator()
        {
            return _resourceInterfaces.GetEnumerator();
        }

        /// <summary>
        /// Return enumerator for the list of interfaces
        /// </summary>
        /// <since_tizen>3</since_tizen>
        /// <returns>The enumerator</returns>
        /// <code>
        /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
        ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
        /// foreach(string item in resourceInterfaces)
        /// {
        ///     Console.WriteLine("Interface : {0}", item);
        /// }
        /// </code>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return _resourceInterfaces.GetEnumerator();
        }

        /// <summary>
        /// Releases any unmanaged resources used by this object.
        /// </summary>
        /// <since_tizen>3</since_tizen>
        /// <feature>http://tizen.org/feature/iot.ocf</feature>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        internal static bool IsValid(string type)
        {
            Regex r = new Regex("^[a-zA-Z0-9.-]+$");
            return (type.Length <= MaxLength && type.Length > 0 && char.IsLower(type[0]) && r.IsMatch(type));
        }

        /// <summary>
        /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
        /// </summary>
        /// <since_tizen>3</since_tizen>
        /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
        /// <feature>http://tizen.org/feature/iot.ocf</feature>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
                return;

            if (disposing)
            {
                // Free managed objects
            }

            Interop.IoTConnectivity.Common.ResourceInterfaces.Destroy(_resourceInterfacesHandle);
            _disposed = true;
        }
    }
}