/// <summary>
        /// Starts receiving presence events.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        /// <remarks>
        /// <para>Sends request to receive presence to an interested server's resource with resourceType.
        /// If succeeded, <see cref="PresenceReceived"/> event handler will be triggered when the server sends presence.
        /// A server sends presence events when adds / removes / alters a resource or start / stop presence.</para>
        /// <para><paramref name="hostAddress" /> could be <see cref="MulticastAddress"/> for IPv4 multicast.
        /// The length of <paramref name="resourceType" /> should be less than or equal to 61.
        /// The <paramref name="resourceType" /> must start with a lowercase alphabetic character, followed by a sequence
        /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.</para>
        /// </remarks>
        /// <privilege>http://tizen.org/privilege/internet</privilege>
        /// <privlevel>public</privlevel>
        /// <param name="hostAddress">The address or addressable name of the server.</param>
        /// <param name="resourceType">A resource type that a client is interested in.</param>
        /// <returns>PresenceId - An identifier for this request.</returns>
        /// <feature>http://tizen.org/feature/iot.ocf</feature>
        /// <pre>Initialize() should be called to initialize.</pre>
        /// <post>
        /// When the resource receive presence, <see cref="PresenceReceived"/> event handler will be invoked.<br/>
        /// You must destroy presence by calling StopReceivingPresence() if presence event is no longer needed.
        /// </post>
        /// <seealso cref="IoTConnectivityServerManager.StartSendingPresence(uint)"/>
        /// <seealso cref="IoTConnectivityServerManager.StopSendingPresence()"/>
        /// <seealso cref="StopReceivingPresence(int)"/>
        /// <seealso cref="PresenceReceived"/>
        /// <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>
        /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have privilege to access.</exception>
        /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory.</exception>
        /// <example><code><![CDATA[
        /// EventHandler<PresenceReceivedEventArgs> handler = (sender, e) => {
        ///     Console.Log("PresenceReceived, presence id :" + e.PresenceId);
        /// }
        /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
        ///     Console.Log("Found error :" + e.Error.Message);
        /// }
        /// IoTConnectivityClientManager.PresenceReceived += handler;
        /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
        /// // Do not forget to remove these event handlers when they are not required any more.
        /// int id = IoTConnectivityClientManager.StartReceivingPresence(IoTConnectivityClientManager.MulticastAddress, "oic.iot.door");
        /// ]]></code></example>
        public static int StartReceivingPresence(string hostAddress, string resourceType)
        {
            Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ip;

            if (resourceType != null && !ResourceTypes.IsValid(resourceType))
            {
                Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
                throw new ArgumentException("Invalid type");
            }

            IntPtr id = IntPtr.Zero;

            lock (s_presenceCallbacksMap)
            {
                id = (IntPtr)s_presenceListenerId++;
            }
            s_presenceCallbacksMap[id] = (IntPtr presence, int result, IntPtr presenceResponseHandle, IntPtr userData) =>
            {
                int presenceId = (int)userData;
                if (result == (int)IoTConnectivityError.None)
                {
                    if (presenceResponseHandle != IntPtr.Zero)
                    {
                        PresenceReceivedEventArgs e = GetPresenceReceivedEventArgs(presenceId, presenceResponseHandle);
                        if (e == null)
                        {
                            Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get PresenceReceivedEventArgs");
                            return;
                        }
                        PresenceReceived?.Invoke(null, e);
                    }
                    else
                    {
                        Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null");
                        return;
                    }
                }
                else
                {
                    FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(presenceId, result);
                    FindingErrorOccurred?.Invoke(null, e);
                }
            };

            IntPtr presenceHandle;
            int    errorCode = Interop.IoTConnectivity.Client.Presence.AddPresenceCb(hostAddress, (int)connectivityType, resourceType, s_presenceCallbacksMap[id], id, out presenceHandle);

            if (errorCode != (int)IoTConnectivityError.None)
            {
                Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register presence event handler");
                lock (s_presenceCallbacksMap)
                {
                    s_presenceCallbacksMap.Remove(id);
                }
                throw IoTConnectivityErrorFactory.GetException(errorCode);
            }

            lock (s_presenceHandlesMap)
            {
                s_presenceHandlesMap[id] = presenceHandle;
            }
            return((int)id);
        }
Exemplo n.º 2
0
        /// <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);
        }