/* * 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.Generic; using System.Runtime.InteropServices; namespace Tizen.Network.IoTConnectivity { /// /// IoT connectivity client manager consists of client side APIs. /// /// 3 public static class IoTConnectivityClientManager { /// /// The IP Address for multicast. /// /// 3 public const string MulticastAddress = null; private static int s_presenceListenerId = 1; private static Dictionary s_presenceCallbacksMap = new Dictionary(); private static Dictionary s_presenceHandlesMap = new Dictionary(); private static int s_requestId = 1; private static Dictionary s_resourceFoundCallbacksMap = new Dictionary(); private static Dictionary s_deviceInformationCallbacksMap = new Dictionary(); private static Dictionary s_platformInformationCallbacksMap = new Dictionary(); /// /// PresenceReceived event. This event occurs when server starts sending presence of a resource. /// /// 3 public static event EventHandler PresenceReceived; /// /// ResourceFound event. This event occurs when a resource is found from the remote server /// after sending request using API StartFindingResource(). /// /// 3 public static event EventHandler ResourceFound; /// /// PlatformInformationFound event. This event occurs when platform information is found /// after sending request using API StartFindingPlatformInformation(). /// /// 3 public static event EventHandler PlatformInformationFound; /// /// DeviceInformationFound event. This event occurs when device information is found /// after sending request using API StartFindingDeviceInformation(). /// /// 3 public static event EventHandler DeviceInformationFound; /// /// FindingError event. This event occurs when an error is found. /// /// 3 public static event EventHandler FindingErrorOccurred; /// /// Timeout in seconds. /// /// 3 /// /// Value to be set must be in range from 1 to 3600. Default timeout interval value is 30.\n /// Sets/gets the timeout of StartFindingResource(), StartFindingDeviceInformation(), StartFindingPlatformInformation(), /// RemoteResource.GetAsync(), RemoteResource.PutAsync(), RemoteResource.PostAsync() and RemoteResource.DeleteAsync() APIs.\n /// Setter can throw exception. /// ///
        /// Initialize() should be called to initialize.
        /// 
/// /// IoTConnectivityClientManager.Initialize(); /// IoTConnectivityClientManager.TimeOut = 120; /// public static int TimeOut { get { int timeout; int ret = Interop.IoTConnectivity.Client.IoTCon.GetTimeout(out timeout); if (ret != (int)IoTConnectivityError.None) { Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get timeout"); return 0; } return timeout; } set { int ret = Interop.IoTConnectivity.Client.IoTCon.SetTimeout(value); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set timeout"); throw IoTConnectivityErrorFactory.GetException(ret); } } } /// /// Polling interval of IoTConnectivity. /// /// 3 /// /// Sets/Gets the polling inerval(milliseconds) of IoTCon. Default value is 100 milliseconds. /// Value to be set must be in range from 1 to 999. The closer to 0, the faster it operates. /// Setter is invoked immediately for changing the interval. /// If you want the faster operation, we recommend you set 10 milliseconds for polling interval. /// Setter can throw exception. /// ///
        /// Initialize() should be called to initialize.
        /// 
/// /// IoTConnectivityClientManager.Initialize(); /// IoTConnectivityClientManager.PollingInterval = 100; /// public static int PollingInterval { get { int interval; int ret = Interop.IoTConnectivity.Client.IoTCon.GetPollingInterval(out interval); if (ret != (int)IoTConnectivityError.None) { Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get polling interval"); return 0; } return interval; } set { int ret = Interop.IoTConnectivity.Client.IoTCon.SetPollingInterval(value); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set polling interval"); throw IoTConnectivityErrorFactory.GetException(ret); } } } /// /// Initializes IoTCon. /// Call this function to start IoTCon. /// /// 3 /// /// @a filePath point to a file for handling secure virtual resources. /// The file that is CBOR(Concise Binary Object Representation)-format must already exist /// in @a filePath. We recommend to use application-local file for @a filePath. /// /// /// http://tizen.org/privilege/network.get \n /// http://tizen.org/privilege/internet /// /// public /// The file path to point to storage for handling secure virtual resources. /// http://tizen.org/feature/iot.ocf /// /// You must call Deinitialize() if IoTCon API is no longer needed. /// /// /// Thrown when the iotcon is not supported. /// Thrown when there is an invalid parameter. /// Thrown when an application does not have privilege to access. /// /// string filePath = "../../res/iotcon-test-svr-db-client.dat"; /// IoTConnectivityClientManager.Initialize(filePath); /// public static void Initialize(string filePath) { int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize"); throw IoTConnectivityErrorFactory.GetException(ret); } } /// /// Deinitializes IoTCon. /// /// 3 /// /// This API must be called if IoTCon API is no longer needed. /// /// http://tizen.org/feature/iot.ocf ///
        /// Initialize() should be called to initialize.
        /// 
/// /// /// IoTConnectivityClientManager.Deinitialize(); /// public static void Deinitialize() { s_presenceListenerId = 1; s_presenceCallbacksMap.Clear(); s_presenceHandlesMap.Clear(); s_requestId = 1; s_resourceFoundCallbacksMap.Clear(); s_deviceInformationCallbacksMap.Clear(); s_platformInformationCallbacksMap.Clear(); PresenceReceived = delegate{}; ResourceFound = delegate{}; PlatformInformationFound = delegate{}; DeviceInformationFound = delegate{}; FindingErrorOccurred = delegate{}; Interop.IoTConnectivity.Client.IoTCon.Deinitialize(); } /// /// Invokes a next message from a queue for receiving messages from others, immediately. /// /// 3 /// /// This API invokes a next message from a queue for receiving messages from others, immediately. /// After calling the API, it continues the polling with existing interval. /// /// http://tizen.org/feature/iot.ocf ///
        /// Initialize() should be called to initialize.
        /// 
/// Thrown when the iotcon is not supported. /// /// IoTConnectivityClientManager.InvokePolling(); /// public static void InvokePolling() { int ret = Interop.IoTConnectivity.Client.IoTCon.InvokePolling(); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to invoke polling"); throw IoTConnectivityErrorFactory.GetException(ret); } } /// /// Starts receiving presence events. /// /// 3 /// /// Sends request to receive presence to an interested server's resource with resourceType. /// If succeeded, 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.\n /// @a hostAddress could be for IPv4 multicast. /// The length of @ resourceType should be less than or equal to 61. The @ resourceType must start with a lowercase alphabetic character, followed by a sequence /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space. /// /// /// http://tizen.org/privilege/internet /// /// public /// The address or addressable name of the server. /// A resource type that a client is interested in. /// PresenceId - An identifier for this request. /// http://tizen.org/feature/iot.ocf ///
Initialize() should be called to initialize.
/// /// When the resource receive presence, event handler will be invoked.\n /// You must destroy presence by calling StopReceivingPresence() if presence event is no longer needed. /// /// /// /// /// /// Thrown when the iotcon is not supported. /// Thrown when there is an invalid parameter. /// Thrown when the operation is invalid. /// Thrown when an application does not have privilege to access. /// Thrown when there is not enough memory. /// /// EventHandler handler = (sender, e) => { /// Console.Log("PresenceReceived, presence id :" + e.PresenceId); /// } /// EventHandler 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"); /// 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; } /// /// Stops receiving presence events. /// /// 3 /// /// Sends request to not to receive server's presence any more. /// /// /// http://tizen.org/privilege/internet /// /// public /// The start presence request identifier. /// http://tizen.org/feature/iot.ocf ///
        /// Initialize() should be called to initialize.
        /// 
/// /// /// /// /// Thrown when the iotcon is not supported. /// Thrown when there is an invalid parameter. /// Thrown when the operation is invalid. /// Thrown when an application does not have privilege to access. /// Thrown when there is not enough memory. /// /// EventHandler handler = (sender, e) => { /// Console.Log("PresenceReceived, presence id :" + e.PresenceId); /// } /// EventHandler errorHandler = (sender, e) => { /// Console.Log("Found error :" + e.Error.Message); /// } /// IoTConnectivityClientManager.PresenceReceived += handler; /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler; /// int id = IoTConnectivityClientManager.StartReceivingPresence(IoTConnectivityClientManager.MulticastAddress, "oic.iot.door"); /// await Task.Delay(5000); // Do other things here /// // Call StopReceivingPresence() when receiving presence is not required any more /// IoTConnectivityClientManager.PresenceReceived -= handler; /// IoTConnectivityClientManager.FindingErrorOccurred -= errorHandler; /// IoTConnectivityClientManager.StopReceivingPresence(id); /// public static void StopReceivingPresence(int presenceId) { if (!s_presenceHandlesMap.ContainsKey((IntPtr)presenceId)) { Log.Error(IoTConnectivityErrorFactory.LogTag, "this presenceId does not exist"); throw new ArgumentException("this presenceId does not exist"); } if (s_presenceHandlesMap.ContainsKey((IntPtr)presenceId)) { IntPtr presenceHandle = s_presenceHandlesMap[(IntPtr)presenceId]; int ret = Interop.IoTConnectivity.Client.Presence.RemovePresenceCb(presenceHandle); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to deregister presence event handler"); throw IoTConnectivityErrorFactory.GetException(ret); } lock (s_presenceHandlesMap) { s_presenceHandlesMap.Remove((IntPtr)presenceId); } } if (s_presenceCallbacksMap.ContainsKey((IntPtr)presenceId)) { lock (s_presenceCallbacksMap) { s_presenceCallbacksMap.Remove((IntPtr)presenceId); } } } /// /// Starts finding resources. /// /// 3 /// /// Sends request to find a resource of @a hostAddress server with @a resourceType. /// If succeeded, event handler will be triggered with information of the resource.\n /// @a hostAddress could be for the IPv4 multicast. /// The length of @a resourceType should be less than or equal to 61. The @ resourceType must start with a lowercase alphabetic character, followed by a sequence /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space. /// /// /// http://tizen.org/privilege/internet /// /// public /// The address or addressable name of the server. The address includes a protocol like coaps://. /// The query specified as a filter for founding resources. /// RequestId - An identifier for this request. /// http://tizen.org/feature/iot.ocf ///
Initialize() should be called to initialize.
/// /// When the resource is found, event handler will be invoked. /// /// /// /// /// Thrown when the iotcon is not supported. /// Thrown when the operation is invalid. /// Thrown when an application does not have privilege to access. /// Thrown when there is not enough memory. /// /// EventHandler handler = (sender, e) => { /// Console.Log("Found resource at host address :" + e.Resource.HostAddress + ", uri :" + e.Resource.UriPath); /// } /// EventHandler errorHandler = (sender, e) => { /// Console.Log("Found error :" + e.Error.Message); /// } /// IoTConnectivityClientManager.ResourceFound += handler; /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler; /// ResourceQuery query = new ResourceQuery(); /// query.Type = "oic.iot.door"; /// // Do not forget to remove these event handlers when they are not required any more. /// int id = IoTConnectivityClientManager.StartFindingResource(null, query); /// public static int StartFindingResource(string hostAddress, ResourceQuery query = null) { Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ip; IntPtr id = IntPtr.Zero; lock (s_resourceFoundCallbacksMap) { id = (IntPtr)s_requestId++; } s_resourceFoundCallbacksMap[id] = (IntPtr remoteResourceHandle, int result, IntPtr userData) => { if (ResourceFound == null) return false; int requestId = (int)userData; if (result == (int)IoTConnectivityError.None) { if (remoteResourceHandle != IntPtr.Zero) { RemoteResource resource = null; try { resource = new RemoteResource(remoteResourceHandle); } catch (Exception exp) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't clone RemoteResource's handle: " + exp.Message); return true; } ResourceFoundEventArgs e = new ResourceFoundEventArgs() { RequestId = requestId, EventContinue = true, Resource = resource }; ResourceFound?.Invoke(null, e); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.EventContinue : " + e.EventContinue); return e.EventContinue; } else { Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null"); } } else { FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result); FindingErrorOccurred?.Invoke(null, e); lock (s_resourceFoundCallbacksMap) { s_resourceFoundCallbacksMap.Remove(id); } } return true; }; IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle; int errorCode = Interop.IoTConnectivity.Client.ResourceFinder.AddResourceFoundCb(hostAddress, (int)connectivityType, queryHandle, s_resourceFoundCallbacksMap[id], id); if (errorCode != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register resource found event handler"); lock (s_resourceFoundCallbacksMap) { s_resourceFoundCallbacksMap.Remove(id); } throw IoTConnectivityErrorFactory.GetException(errorCode); } return (int)id; } /// /// Starts finding the device information of remote server. /// /// 3 /// /// Requests server for device information. /// If succeeded, event handler will be triggered with information of the device.\n /// @a hostAddress could be for the IPv4 multicast. /// /// /// http://tizen.org/privilege/internet /// /// public /// The host address of the remote server. /// The query specified as a filter for founding resources. /// RequestId - An identifier for this request. /// http://tizen.org/feature/iot.ocf ///
Initialize() should be called to initialize.
/// /// event handler will be invoked. /// /// /// /// /// /// Thrown when the iotcon is not supported. /// Thrown when the operation is invalid. /// Thrown when an application does not have privilege to access. /// Thrown when there is not enough memory. /// /// EventHandler handler = (sender, e) => { /// Console.Log("Device information found, id : " + e.RequestId + ", name : " + e.Name); /// } /// EventHandler errorHandler = (sender, e) => { /// Console.Log("Found error :" + e.Error.Message); /// } /// IoTConnectivityClientManager.DeviceInformationFound += handler; /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler; /// // Do not forget to remove these event handlers when they are not required any more. /// int id = IoTConnectivityClientManager.StartFindingDeviceInformation(IoTConnectivityClientManager.MulticastAddress); /// public static int StartFindingDeviceInformation(string hostAddress, ResourceQuery query = null) { Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ip; IntPtr id = IntPtr.Zero; lock (s_deviceInformationCallbacksMap) { id = (IntPtr)s_requestId++; } s_deviceInformationCallbacksMap[id] = (IntPtr deviceInfoHandle, int result, IntPtr userData) => { if (DeviceInformationFound == null) return false; int requestId = (int)userData; if (result == (int)IoTConnectivityError.None) { if (deviceInfoHandle != IntPtr.Zero) { DeviceInformationFoundEventArgs e = GetDeviceInformationFoundEventArgs(requestId, deviceInfoHandle); if (e == null) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get DeviceInformationFoundEventArgs"); return true; } DeviceInformationFound?.Invoke(null, e); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.EventContinue : " + e.EventContinue); return e.EventContinue; } else { Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null"); } } else { FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result); FindingErrorOccurred?.Invoke(null, e); lock (s_deviceInformationCallbacksMap) { s_deviceInformationCallbacksMap.Remove(id); } } return true; }; IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle; int errorCode = Interop.IoTConnectivity.Client.DeviceInformation.Find(hostAddress, (int)connectivityType, queryHandle, s_deviceInformationCallbacksMap[id], id); if (errorCode != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device information"); lock (s_deviceInformationCallbacksMap) { s_deviceInformationCallbacksMap.Remove(id); } throw IoTConnectivityErrorFactory.GetException(errorCode); } return (int)id; } /// /// Starts finding the platform information of remote server. /// /// 3 /// /// Requests server for platform information. /// If succeeded, event handler will be triggered with information of the platform.\n /// @a hostAddress could be for IPv4 multicast. /// /// /// http://tizen.org/privilege/internet /// /// public /// The host address of remote server. /// The query specified as a filter for founding resources. /// RequestId - An identifier for this request. /// http://tizen.org/feature/iot.ocf ///
Initialize() should be called to initialize.
/// /// event handler will be invoked. /// /// /// /// /// Thrown when the iotcon is not supported. /// Thrown when the operation is invalid. /// Thrown when an application does not have privilege to access. /// Thrown when there is not enough memory. /// /// EventHandler handler = (sender, e) => { /// Console.Log("PlatformInformationFound :" + e.RequestId); /// } /// EventHandler errorHandler = (sender, e) => { /// Console.Log("Found error :" + e.Error.Message); /// } /// IoTConnectivityClientManager.PlatformInformationFound += handler; /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler; /// // Do not forget to remove these event handlers when they are not required any more. /// int id = IoTConnectivityClientManager.StartFindingPlatformInformation(IoTConnectivityClientManager.MulticastAddress); /// public static int StartFindingPlatformInformation(string hostAddress, ResourceQuery query = null) { Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ip; IntPtr id = IntPtr.Zero; lock (s_platformInformationCallbacksMap) { id = (IntPtr)s_requestId++; } s_platformInformationCallbacksMap[id] = (IntPtr platformInfoHandle, int result, IntPtr userData) => { if (PlatformInformationFound == null) return false; int requestId = (int)userData; if (result == (int)IoTConnectivityError.None) { if (platformInfoHandle != IntPtr.Zero) { PlatformInformationFoundEventArgs e = GetPlatformInformationFoundEventArgs(requestId, platformInfoHandle); if (e == null) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get PlatformInformationFoundEventArgs"); return true; } PlatformInformationFound?.Invoke(null, e); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.EventContinue : " + e.EventContinue); return e.EventContinue; } else { Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null"); } } else { FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result); FindingErrorOccurred?.Invoke(null, e); lock (s_platformInformationCallbacksMap) { s_platformInformationCallbacksMap.Remove(id); } } return true; }; IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle; int errorCode = Interop.IoTConnectivity.Client.PlatformInformation.Find(hostAddress, (int)connectivityType, queryHandle, s_platformInformationCallbacksMap[id], id); if (errorCode != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform information"); lock (s_platformInformationCallbacksMap) { s_platformInformationCallbacksMap.Remove(id); } throw IoTConnectivityErrorFactory.GetException(errorCode); } return (int)id; } // Private methods private static PresenceReceivedEventArgs GetPresenceReceivedEventArgs(int presenceId, IntPtr presenceResponseHandle) { int trigger; IntPtr host, type; int ret = Interop.IoTConnectivity.Client.PresenceResponse.GetHostAddress(presenceResponseHandle, out host); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get host address"); return null; } ret = Interop.IoTConnectivity.Client.PresenceResponse.GetResourceType(presenceResponseHandle, out type); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource type"); return null; } ret = Interop.IoTConnectivity.Client.PresenceResponse.GetTrigger(presenceResponseHandle, out trigger); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get event type"); return null; } PresenceReceivedEventArgs e = new PresenceReceivedEventArgs() { PresenceId = presenceId, HostAddress = (host != IntPtr.Zero) ? Marshal.PtrToStringAnsi(host) : string.Empty, Type = (type != IntPtr.Zero) ? Marshal.PtrToStringAnsi(type) : string.Empty, EventType = (PresenceEventType)trigger }; return e; } private static DeviceInformationFoundEventArgs GetDeviceInformationFoundEventArgs(int requestId, IntPtr deviceInfoHandle) { IntPtr name, specVersion, deviceId, dataModelVersion; int ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.Name, out name); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get name"); return null; } ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.SpecVersion, out specVersion); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get spec version"); return null; } ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.Id, out deviceId); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device id"); return null; } ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.DataModelVersion, out dataModelVersion); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get data model version"); return null; } DeviceInformationFoundEventArgs e = new DeviceInformationFoundEventArgs() { RequestId = requestId, EventContinue = true, Name = (name != IntPtr.Zero) ? Marshal.PtrToStringAnsi(name) : string.Empty, SpecVersion = (specVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(specVersion) : string.Empty, DeviceId = (deviceId != IntPtr.Zero) ? Marshal.PtrToStringAnsi(deviceId) : string.Empty, DataModelVersion = (dataModelVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(dataModelVersion) : string.Empty }; return e; } private static PlatformInformationFoundEventArgs GetPlatformInformationFoundEventArgs(int requestId, IntPtr platformInfoHandle) { IntPtr platformId, manufacturerName, manufacturerUrl, modelNumber, dateOfManufacture, platformVersion, osVersion, hardwareVersion, firmwareVersion, supportUrl, systemTime; int ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.Id, out platformId); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform id"); return null; } ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.MfgName, out manufacturerName); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get manufacturer name"); return null; } ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.MfgUrl, out manufacturerUrl); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get manufacturer url"); return null; } ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.ModelNumber, out modelNumber); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get model number"); return null; } ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.DateOfMfg, out dateOfManufacture); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get date of manufacture"); return null; } ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.PlatformVer, out platformVersion); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform version"); return null; } ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.OsVer, out osVersion); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to os version"); return null; } ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.HardwareVer, out hardwareVersion); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to hardware version"); return null; } ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.FirmwareVer, out firmwareVersion); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get firmware version"); return null; } ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.SupportUrl, out supportUrl); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get support url"); return null; } ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.SystemTime, out systemTime); if (ret != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get system time"); return null; } PlatformInformationFoundEventArgs e = new PlatformInformationFoundEventArgs() { RequestId = requestId, EventContinue = true, PlatformId = (platformId != IntPtr.Zero) ? Marshal.PtrToStringAnsi(platformId) : string.Empty, ManufacturerName = (manufacturerName != IntPtr.Zero) ? Marshal.PtrToStringAnsi(manufacturerName) : string.Empty, ManufacturerURL = (manufacturerUrl != IntPtr.Zero) ? Marshal.PtrToStringAnsi(manufacturerUrl) : string.Empty, DateOfManufacture = (dateOfManufacture != IntPtr.Zero) ? Marshal.PtrToStringAnsi(dateOfManufacture) : string.Empty, ModelNumber = (modelNumber != IntPtr.Zero) ? Marshal.PtrToStringAnsi(modelNumber) : string.Empty, PlatformVersion = (platformVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(platformVersion) : string.Empty, OsVersion = (osVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(osVersion) : string.Empty, HardwareVersion = (hardwareVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(hardwareVersion) : string.Empty, FirmwareVersion = (firmwareVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(firmwareVersion) : string.Empty, SupportUrl = (supportUrl != IntPtr.Zero) ? Marshal.PtrToStringAnsi(supportUrl) : string.Empty, SystemTime = (systemTime != IntPtr.Zero) ? Marshal.PtrToStringAnsi(systemTime) : string.Empty }; Log.Info(IoTConnectivityErrorFactory.LogTag, "e.RequestId is " + e.RequestId); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.PlatformId is " + e.PlatformId); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.ManufacturerName is " + e.ManufacturerName); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.ManufacturerURL is " + e.ManufacturerURL); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.DateOfManufacture is " + e.DateOfManufacture); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.ModelNumber is " + e.ModelNumber); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.PlatformVersion is " + e.PlatformVersion); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.OsVersion is " + e.OsVersion); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.HardwareVersion is " + e.HardwareVersion); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.FirmwareVersion is " + e.FirmwareVersion); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.SupportUrl is " + e.SupportUrl); Log.Info(IoTConnectivityErrorFactory.LogTag, "e.SystemTime is " + e.SystemTime); return e; } private static FindingErrorOccurredEventArgs GetFindingErrorOccurredEventArgs(int requestId, int err) { FindingErrorOccurredEventArgs e = new FindingErrorOccurredEventArgs() { RequestId = requestId, Error = IoTConnectivityErrorFactory.GetException(err) }; return e; } } }