/* * 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 { /// /// This class contains resource types and provides APIs to manage, add, or remove those types. /// A resource type indicates a class or a category of resources. /// /// 3 public class ResourceTypes : IEnumerable, IDisposable { internal const int MaxLength = 61; internal IntPtr _resourceTypeHandle = IntPtr.Zero; private readonly HashSet _resourceTypes = new HashSet(); private bool _disposed = false; /// /// Constructor of ResourceTypes. /// /// 3 /// http://tizen.org/feature/iot.ocf /// /// /// Thrown when the iotcon is not supported. /// Thrown when there is not enough memory. /// /// ResourceTypes types = new ResourceTypes(); /// public ResourceTypes() { int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type"); throw IoTConnectivityErrorFactory.GetException(ret); } } /// /// Constructor of ResourceTypes using list of types. /// /// 3 /// List of resource types. /// http://tizen.org/feature/iot.ocf /// Thrown when there is an invalid parameter. /// () { "org.tizen.light", "oic.if.room" }); /// ]]> public ResourceTypes(IEnumerable types) { int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type"); throw IoTConnectivityErrorFactory.GetException(ret); } foreach (string type in types) { Add(type); } } internal ResourceTypes(IntPtr typesHandleToClone) { int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type"); throw IoTConnectivityErrorFactory.GetException(ret); } Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) => { _resourceTypes.Add(type); return true; }; ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type"); throw IoTConnectivityErrorFactory.GetException(ret); } } /// /// Destructor of the ResourceTypes class. /// ~ResourceTypes() { Dispose(false); } /// /// Indicates count of types in the list. /// /// 3 /// Count of types in the list. /// () { "org.tizen.light", "oic.if.room" }); /// Console.WriteLine("There are {0} items", types.Count); /// ]]> public int Count { get { return _resourceTypes.Count; } } /// /// Adds a resource type into the list. /// /// 3 /// /// The length of @a item should be less than or equal to 61.\n /// The @a item must start with a lowercase alphabetic character, followed by a sequence /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.\n /// Duplicate strings are not allowed. /// /// The string data to insert into the resource types. /// http://tizen.org/feature/iot.ocf /// /// Thrown when the iotcon is not supported. /// Thrown when there is an invalid parameter. /// Thrown when the operation is invalid. /// /// ResourceTypes resourceTypes = new ResourceTypes(); /// resourceTypes.Add("org.tizen.light"); /// public void Add(string item) { if (IsValid(item)) { int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type"); throw IoTConnectivityErrorFactory.GetException(ret); } _resourceTypes.Add(item); } else { Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type"); throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter); } } /// /// Removes a resource type from the list. /// /// 3 /// The string data to delete from the resource types. /// http://tizen.org/feature/iot.ocf /// /// Thrown when the iotcon is not supported. /// Thrown when there is an invalid parameter. /// Thrown when the operation is invalid. /// () { "org.tizen.light", "oic.if.room" }); /// resourceTypes.Remove("oic.if.room"); /// ]]> public void Remove(string item) { int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type"); throw IoTConnectivityErrorFactory.GetException(ret); } _resourceTypes.Remove(item); } /// /// Returns an enumerator for the list of types. /// /// 3 /// The enumerator. /// () { "org.tizen.light", "oic.if.room" }); /// foreach(string item in resourceTypes) /// { /// Console.WriteLine("Type : {0}", item); /// } /// ]]> public IEnumerator GetEnumerator() { return _resourceTypes.GetEnumerator(); } /// /// Returns an enumerator for the list of types. /// /// 3 /// The enumerator. /// () { "org.tizen.light", "oic.if.room" }); /// foreach(string item in resourceTypes) /// { /// Console.WriteLine("Type : {0}", item); /// } /// ]]> IEnumerator IEnumerable.GetEnumerator() { return _resourceTypes.GetEnumerator(); } /// /// Releases any unmanaged resources used by this object. /// /// 3 /// http://tizen.org/feature/iot.ocf 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)); } /// /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects. /// /// 3 /// If true, disposes any disposable objects. If false, does not dispose disposable objects. /// http://tizen.org/feature/iot.ocf protected virtual void Dispose(bool disposing) { if (_disposed) return; if (disposing) { // Free managed objects } Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle); _disposed = true; } } }