/* * 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 interfaces and provides APIs to manage, add, or remove those interfaces. /// A resource interface indicates a class or category of resources. /// /// 3 public class ResourceInterfaces : IEnumerable, IDisposable { /// /// Default Interface. /// /// 3 public const string DefaultInterface = "oic.if.baseline"; /// /// List Links Interface, which is used to list the references to other resources contained in a resource. /// /// 3 public const string LinkInterface = "oic.if.ll"; /// /// Batch Interface, which is used to manipulate (GET, PUT, POST, DELETE) on other resource contained in a resource. /// /// 3 public const string BatchInterface = "oic.if.b"; /// /// Group Interface, which is used to manipulate (GET, PUT, POST) a group of remote resources. /// /// 3 public const string GroupInterface = "oic.mi.grp"; /// /// Read-Only Interface, which is used to limit the methods that can be applied to a resource to GET only. /// /// 3 public const string ReadonlyInterface = "oic.if.r"; private readonly IntPtr _resourceInterfacesHandle = IntPtr.Zero; private const int MaxLength = 61; private readonly HashSet _resourceInterfaces = new HashSet(); private bool _disposed = false; /// /// Constructor of ResourceInterfaces. /// /// 3 /// http://tizen.org/feature/iot.ocf /// /// /// Thrown when the iotcon is not supported. /// Thrown when there is not enough memory. /// /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(); /// 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); } } /// /// Constructor of ResourceInterfaces using list of interfaces. /// /// 3 /// List of resource interfaces. /// http://tizen.org/feature/iot.ocf /// Thrown when the iotcon is not supported. /// Thrown when there is not enough memory. /// Thrown when there is an invalid parameter. /// () /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface }); /// ]]> public ResourceInterfaces(IEnumerable 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); } } /// /// Destructor of the ResourceInterfaces class. /// ~ResourceInterfaces() { Dispose(false); } internal IntPtr ResourceInterfacesHandle { get { return _resourceInterfacesHandle; } } /// /// Indicates count of interfaces in the list /// /// 3 /// Count of interfaces in the list. /// () /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface }); /// Console.WriteLine("There are {0} interfaces", resourceInterfaces.Count); /// ]]> public int Count { get { return _resourceInterfaces.Count; } } /// /// Adds a resource interface into the list. /// /// 3 /// /// could be a value, such as . /// /// The string data to insert into the resource interfaces. /// http://tizen.org/feature/iot.ocf /// /// Thrown when the iotcon is not supported. /// Thrown when the operation is invalid. /// Thrown when there is an invalid parameter. /// /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(); /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface); /// 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); } } /// /// Removes a resource interface from the list. /// /// 3 /// The string data to delete from the resource ifaces. /// 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. /// (){ ResourceInterfaces.BatchInterface }); /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface); /// ]]> 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); } /// /// Returns enumerator for the list of interfaces. /// /// 3 /// The enumerator. /// () /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface }); /// foreach(string item in resourceInterfaces) /// { /// Console.WriteLine("Interface : {0}", item); /// } /// ]]> public IEnumerator GetEnumerator() { return _resourceInterfaces.GetEnumerator(); } /// /// Returns enumerator for the list of interfaces. /// /// 3 /// The enumerator. /// () /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface }); /// foreach(string item in resourceInterfaces) /// { /// Console.WriteLine("Interface : {0}", item); /// } /// ]]> IEnumerator IEnumerable.GetEnumerator() { return _resourceInterfaces.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.ResourceInterfaces.Destroy(_resourceInterfacesHandle); _disposed = true; } } }