private void SetRemoteResource() { IntPtr hostAddressPtr, uriPathPtr; int ret = Interop.IoTConnectivity.Client.RemoteResource.GetHostAddress(_remoteResourceHandle, out hostAddressPtr); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get host address"); throw IoTConnectivityErrorFactory.GetException(ret); } ret = Interop.IoTConnectivity.Client.RemoteResource.GetUriPath(_remoteResourceHandle, out uriPathPtr); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get uri path"); throw IoTConnectivityErrorFactory.GetException(ret); } int policy = (int)ResourcePolicy.NoProperty; ret = Interop.IoTConnectivity.Client.RemoteResource.GetPolicies(_remoteResourceHandle, out policy); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get uri path"); throw IoTConnectivityErrorFactory.GetException(ret); } IntPtr typesHandle, interfacesHandle; ret = Interop.IoTConnectivity.Client.RemoteResource.GetTypes(_remoteResourceHandle, out typesHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource types"); throw IoTConnectivityErrorFactory.GetException(ret); } ret = Interop.IoTConnectivity.Client.RemoteResource.GetInterfaces(_remoteResourceHandle, out interfacesHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource interfaces"); throw IoTConnectivityErrorFactory.GetException(ret); } IntPtr deviceIdPtr; ret = Interop.IoTConnectivity.Client.RemoteResource.GetDeviceId(_remoteResourceHandle, out deviceIdPtr); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device id"); throw IoTConnectivityErrorFactory.GetException(ret); } DeviceId = (deviceIdPtr != IntPtr.Zero) ? Marshal.PtrToStringAnsi(deviceIdPtr) : string.Empty; HostAddress = (hostAddressPtr != IntPtr.Zero) ? Marshal.PtrToStringAnsi(hostAddressPtr) : string.Empty; UriPath = (uriPathPtr != IntPtr.Zero) ? Marshal.PtrToStringAnsi(uriPathPtr) : string.Empty; Types = new ResourceTypes(typesHandle); Interfaces = new ResourceInterfaces(interfacesHandle); Policy = (ResourcePolicy)policy; }
/// <summary> /// The constructor. /// </summary> /// <since_tizen> 3 </since_tizen> /// <remarks> /// <paramref name="uri" /> format would be relative URI path like '/a/light' /// and its length must be less than 128. /// </remarks> /// <privilege>http://tizen.org/privilege/internet</privilege> /// <privlevel>public</privlevel> /// <param name="uri">The URI path of the resource.</param> /// <param name="types">Resource types.</param> /// <param name="interfaces">Resource interfaces.</param> /// <param name="policy">The policies of the resoruce.</param> /// <feature>http://tizen.org/feature/iot.ocf</feature> /// <pre> /// IoTConnectivityServerManager.Initialize() should be called to initialize. /// </pre> /// <seealso cref="ResourceTypes"/> /// <seealso cref="ResourceInterfaces"/> /// <seealso cref="ResourcePolicy"/> /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception> /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory.</exception> /// <example><code><![CDATA[ /// // Create a class which inherits from Resource /// public class DoorResource : Resource /// { /// public DoorResource(string uri, ResourceTypes types, ResourceInterfaces interfaces, ResourcePolicy policy) /// : base(uri, types, interfaces, policy) { /// } /// protected override Response OnDelete(Request request) { /// // Do something /// } /// protected override Response OnGet(Request request) { /// // Do something /// } /// // Override other abstract methods of Resource class /// } /// /// // Use it like below /// ResourceInterfaces ifaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.DefaultInterface }); /// ResourceTypes types = new ResourceTypes(new List<string>(){ "oic.iot.door.new" }); /// Resource resource = new DoorResource("/door/uri1", types, ifaces, ResourcePolicy.Discoverable | ResourcePolicy.Observable); /// ]]></code></example> protected Resource(string uri, ResourceTypes types, ResourceInterfaces interfaces, ResourcePolicy policy) { UriPath = uri; Types = types; Interfaces = interfaces; Policy = policy; _children.CollectionChanged += ChildrenCollectionChanged; int ret = Interop.IoTConnectivity.Server.Observers.Create(out _observerHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create obsever handle"); throw IoTConnectivityErrorFactory.GetException(ret); } }
/// <summary> /// Creates a remote resource instance. /// </summary> /// <since_tizen> 3 </since_tizen> /// <remarks> /// <para>To use this API, you should provide all the details required to correctly contact and /// observe the object.</para> /// <para>If not, you should discover the resource object manually.</para> /// <para>The <paramref name="policy" /> can contain multiple policies like <c>ResourcePolicy.Discoverable | ResourcePolicy.Observable</c>.</para> /// </remarks> /// <param name="hostAddress">The host address of the resource.</param> /// <param name="uriPath">The URI path of the resource.</param> /// <param name="policy">The policies of the resource.</param> /// <param name="resourceTypes">The resource types of the resource.</param> /// <param name="resourceInterfaces">The resource interfaces of the resource.</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> public RemoteResource(string hostAddress, string uriPath, ResourcePolicy policy, ResourceTypes resourceTypes, ResourceInterfaces resourceInterfaces) { if (hostAddress == null || uriPath == null || resourceTypes == null || resourceInterfaces == null) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid parameters"); throw new ArgumentException("Invalid parameter"); } HostAddress = hostAddress; UriPath = uriPath; Policy = policy; Types = new List <string>(resourceTypes); Interfaces = new List <string>(resourceInterfaces); DeviceId = null; CreateRemoteResource(resourceTypes._resourceTypeHandle, resourceInterfaces.ResourceInterfacesHandle); }