1 /// Copyright 2016 by Samsung Electronics, Inc.,
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
10 using System.Collections.Generic;
11 using System.Runtime.InteropServices;
13 namespace Tizen.Network.IoTConnectivity
16 /// IoT connectivity client manager consists of client side APIs.
18 public static class IoTConnectivityClientManager
21 /// The IP Address for multicast
23 public const string MulticastAddress = null;
25 private static int s_presenceListenerId = 1;
26 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.Presence.PresenceCallback> s_presenceCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.Presence.PresenceCallback>();
27 private static Dictionary<IntPtr, IntPtr> s_presenceHandlesMap = new Dictionary<IntPtr, IntPtr>();
29 private static int s_requestId = 1;
30 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.ResourceFinder.FoundResourceCallback> s_resourceFoundCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.ResourceFinder.FoundResourceCallback>();
31 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.DeviceInformation.DeviceInformationCallback> s_deviceInformationCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.DeviceInformation.DeviceInformationCallback>();
32 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.PlatformInformation.PlatformInformationCallback> s_platformInformationCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.PlatformInformation.PlatformInformationCallback>();
35 /// PresenceReceived event. This event is occurred when server starts sending presence of a resource.
37 public static event EventHandler<PresenceReceivedEventArgs> PresenceReceived;
40 /// ResourceFound event. This event is occurred when a resource is found from the remote server
41 /// after sending request using API StartFindingResource().
43 public static event EventHandler<ResourceFoundEventArgs> ResourceFound;
46 /// PlatformInformationFound event. This event is occurred when platform information is found
47 /// after sending request using API StartFindingPlatformInformation().
49 public static event EventHandler<PlatformInformationFoundEventArgs> PlatformInformationFound;
52 /// DeviceInformationFound event. This event is occurred when device information is found
53 /// after sending request using API StartFindingDeviceInformation().
55 public static event EventHandler<DeviceInformationFoundEventArgs> DeviceInformationFound;
58 /// FindingError event. This event is occurred when an error is found.
60 public static event EventHandler<FindingErrorOccurredEventArgs> FindingErrorOccurred;
63 /// Timeout in seconds
66 /// Value to be set must be in range from 1 to 3600. Default timeout interval value is 30.\n
67 /// Sets/gets the timeout of StartFindingResource(), StartFindingDeviceInformation(), StartFindingPlatformInformation(),
68 /// RemoteResource.GetAsync(), RemoteResource.PutAsync(), RemoteResource.PostAsync() and RemoteResource.DeleteAsync() APIs.\n
69 /// Setter can throw exception.
72 /// Initialize() should be called to initialize
75 /// IoTConnectivityClientManager.Initialize();
76 /// IoTConnectivityClientManager.TimeOut = 120;
78 public static int TimeOut
83 int ret = Interop.IoTConnectivity.Client.IoTCon.GetTimeout(out timeout);
84 if (ret != (int)IoTConnectivityError.None)
86 Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get timeout");
93 int ret = Interop.IoTConnectivity.Client.IoTCon.SetTimeout(value);
94 if (ret != (int)IoTConnectivityError.None)
96 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set timeout");
97 throw IoTConnectivityErrorFactory.GetException(ret);
103 /// Polling interval of IoTConnectivity
106 /// Sets/Gets the polling inerval(milliseconds) of IoTCon. Default value is 100 milliseconds.
107 /// Value to be set must be in range from 1 to 999. The closer to 0, the faster it operates.
108 /// Setter is invoked immediately for changing the interval.
109 /// If you want the faster operation, we recommend you set 10 milliseconds for polling interval.
110 /// Setter can throw exception.
113 /// Initialize() should be called to initialize
116 /// IoTConnectivityClientManager.Initialize();
117 /// IoTConnectivityClientManager.PollingInterval = 100;
119 public static int PollingInterval
124 int ret = Interop.IoTConnectivity.Client.IoTCon.GetPollingInterval(out interval);
125 if (ret != (int)IoTConnectivityError.None)
127 Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get polling interval");
134 int ret = Interop.IoTConnectivity.Client.IoTCon.SetPollingInterval(value);
135 if (ret != (int)IoTConnectivityError.None)
137 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set polling interval");
138 throw IoTConnectivityErrorFactory.GetException(ret);
144 /// Initializes IoTCon.
145 /// Call this function to start IoTCon.
148 /// @a filePath point to a file for handling secure virtual resources.
149 /// The file that is CBOR(Concise Binary Object Representation)-format must already exist
150 /// in @a filePath. We recommend to use application-local file for @a filePath.
153 /// http://tizen.org/privilege/network.get \n
154 /// http://tizen.org/privilege/internet
156 /// <param name="filePath">The file path to point to storage for handling secure virtual resources.</param>
158 /// You must call Deinitialize() if IoTCon API is no longer needed.
160 /// <seealso cref="Deinitialize()"/>
162 /// string filePath = "../../res/iotcon-test-svr-db-client.dat";
163 /// IoTConnectivityClientManager.Initialize(filePath);
165 public static void Initialize(string filePath)
167 int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath);
168 if (ret != (int)IoTConnectivityError.None)
170 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
171 throw IoTConnectivityErrorFactory.GetException(ret);
176 /// Deinitializes IoTCon.
179 /// This API must be called if IoTCon API is no longer needed.
182 /// Initialize() should be called to initialize.
184 /// <seealso cref="Initialize()"/>
185 /// <seealso cref="SecureInitialize()"/>
187 /// IoTConnectivityClientManager.Deinitialize();
189 public static void Deinitialize()
191 Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
195 /// Invokes a next message from a queue for receiving messages from others, immediately.
198 /// This API invokes a next message from a queue for receiving messages from others, immediately.
199 /// After calling the API, it continues the polling with existing interval.
202 /// Initialize() should be called to initialize.
205 /// IoTConnectivityClientManager.InvokePolling();
207 public static void InvokePolling()
209 int ret = Interop.IoTConnectivity.Client.IoTCon.InvokePolling();
210 if (ret != (int)IoTConnectivityError.None)
212 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to invoke polling");
213 throw IoTConnectivityErrorFactory.GetException(ret);
218 /// Starts receiving presence events
221 /// Sends request to receive presence to an interested server's resource with resourceType.
222 /// If succeeded, <see cref="PresenceReceived"/> event handler will be triggered when the server sends presence.
223 /// A server sends presence events when adds / removes / alters a resource or start / stop presence.\n
224 /// @a hostAddress could be <see cref="MulticastAddress"/> for IPv4 multicast.
225 /// 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
226 /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.
229 /// http://tizen.org/privilege/internet
231 /// <param name="hostAddress">The address or addressable name of the server</param>
232 /// <param name="resourceType">A resource type that a client is interested in</param>
233 /// <returns>PresenceId - An identifier for this request</returns>
234 /// <pre>Initialize() should be called to initialize.</pre>
236 /// When the resource receive presence, <see cref="PresenceReceived"/> event handler will be invoked.\n
237 /// You must destroy presence by calling StopReceivingPresence() if presence event is no longer needed.
239 /// <seealso cref="IoTConnectivityServerManager.StartSendingPresence()"/>
240 /// <seealso cref="IoTConnectivityServerManager.StopSendingPresence()"/>
241 /// <seealso cref="StopReceivingPresence()"/>
242 /// <seealso cref="PresenceReceived"/>
244 /// EventHandler<PresenceReceivedEventArgs> handler = (sender, e) => {
245 /// Console.Log("PresenceReceived, presence id :" + e.PresenceId);
247 /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
248 /// Console.Log("Found error :" + e.Error.Message);
250 /// IoTConnectivityClientManager.PresenceReceived += handler;
251 /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
252 /// // Do not forget to remove these event handlers when they are not required any more.
253 /// int id = IoTConnectivityClientManager.StartReceivingPresence(IoTConnectivityClientManager.MulticastAddress, "oic.iot.door");
255 public static int StartReceivingPresence(string hostAddress, string resourceType)
257 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress);
258 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
260 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
261 throw new ArgumentException("Unable to parse host address");
264 if (resourceType != null && !ResourceTypes.IsValid(resourceType))
266 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
267 throw new ArgumentException("Invalid type");
270 IntPtr id = IntPtr.Zero;
271 lock (s_presenceCallbacksMap)
273 id = (IntPtr)s_presenceListenerId++;
275 s_presenceCallbacksMap[id] = (IntPtr presence, int result, IntPtr presenceResponseHandle, IntPtr userData) =>
277 int presenceId = (int)userData;
278 if (result == (int)IoTConnectivityError.None)
280 if (presenceResponseHandle != IntPtr.Zero)
282 PresenceReceivedEventArgs e = GetPresenceReceivedEventArgs(presenceId, presenceResponseHandle);
285 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get PresenceReceivedEventArgs");
288 PresenceReceived?.Invoke(null, e);
292 Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null");
298 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(presenceId, result);
299 FindingErrorOccurred?.Invoke(null, e);
303 IntPtr presenceHandle;
304 int errorCode = Interop.IoTConnectivity.Client.Presence.AddPresenceCb(hostAddress, (int)connectivityType, resourceType, s_presenceCallbacksMap[id], id, out presenceHandle);
305 if (errorCode != (int)IoTConnectivityError.None)
307 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register presence event handler");
308 lock (s_presenceCallbacksMap)
310 s_presenceCallbacksMap.Remove(id);
312 throw IoTConnectivityErrorFactory.GetException(errorCode);
315 lock (s_presenceHandlesMap)
317 s_presenceHandlesMap[id] = presenceHandle;
323 /// Stops receiving presence events
326 /// Sends request to not to receive server's presence any more.
329 /// http://tizen.org/privilege/internet
331 /// <param name="presenceId">The start presence request identifier</param>
333 /// Initialize() should be called to initialize.
335 /// <seealso cref="IoTConnectivityServerManager.StartSendingPresence()"/>
336 /// <seealso cref="IoTConnectivityServerManager.StopSendingPresence()"/>
337 /// <seealso cref="StartReceivingPresence()"/>
338 /// <seealso cref="PresenceReceived"/>
340 /// EventHandler<PresenceReceivedEventArgs> handler = (sender, e) => {
341 /// Console.Log("PresenceReceived, presence id :" + e.PresenceId);
343 /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
344 /// Console.Log("Found error :" + e.Error.Message);
346 /// IoTConnectivityClientManager.PresenceReceived += handler;
347 /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
348 /// int id = IoTConnectivityClientManager.StartReceivingPresence(IoTConnectivityClientManager.MulticastAddress, "oic.iot.door");
349 /// await Task.Delay(5000); // Do other things here
350 /// // Call StopReceivingPresence() when receiving presence is not required any more
351 /// IoTConnectivityClientManager.PresenceReceived -= handler;
352 /// IoTConnectivityClientManager.FindingErrorOccurred -= errorHandler;
353 /// IoTConnectivityClientManager.StopReceivingPresence(id);
355 public static void StopReceivingPresence(int presenceId)
357 if (s_presenceHandlesMap.ContainsKey((IntPtr)presenceId))
359 IntPtr presenceHandle = s_presenceHandlesMap[(IntPtr)presenceId];
360 int ret = Interop.IoTConnectivity.Client.Presence.RemovePresenceCb(presenceHandle);
361 if (ret != (int)IoTConnectivityError.None)
363 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to deregister presence event handler");
364 throw IoTConnectivityErrorFactory.GetException(ret);
367 lock (s_presenceHandlesMap)
369 s_presenceHandlesMap.Remove((IntPtr)presenceId);
373 if (s_presenceCallbacksMap.ContainsKey((IntPtr)presenceId))
375 lock (s_presenceCallbacksMap)
377 s_presenceCallbacksMap.Remove((IntPtr)presenceId);
383 /// Starts finding resources.
386 /// Sends request to find a resource of @a hostAddress server with @a resourceType.
387 /// If succeeded, <see cref="ResourceFound"/> event handler will be triggered with information of the resource.\n
388 /// @a hostAddress could be <see cref="MulticastAddress"/> for IPv4 multicast.
389 /// 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
390 /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.
393 /// http://tizen.org/privilege/internet
395 /// <param name="hostAddress">The address or addressable name of the server. The address includes a protocol like coaps://</param>
396 /// <param name="query">The query specified as a filter for founding resources</param>
397 /// <returns>RequestId - An identifier for this request</returns>
398 /// <pre>Initialize() should be called to initialize.</pre>
400 /// When the resource is found, <see cref="ResourceFound"/> event handler will be invoked.
402 /// <seealso cref="ResourceFound"/>
403 /// <seealso cref="ResourceFoundEventArgs"/>
404 /// <seealso cref="TimeOut"/>
406 /// EventHandler<ResourceFoundEventArgs> handler = (sender, e) => {
407 /// Console.Log("Found resource at host address :" + e.Resource.HostAddress + ", uri :" + e.Resource.UriPath);
409 /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
410 /// Console.Log("Found error :" + e.Error.Message);
412 /// IoTConnectivityClientManager.ResourceFound += handler;
413 /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
414 /// ResourceQuery query = new ResourceQuery();
415 /// query.Type = "oic.iot.door";
416 /// // Do not forget to remove these event handlers when they are not required any more.
417 /// int id = IoTConnectivityClientManager.StartFindingResource(null, query);
419 public static int StartFindingResource(string hostAddress, ResourceQuery query = null)
421 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress);
422 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
424 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
425 throw new ArgumentException("Unable to parse host address");
428 IntPtr id = IntPtr.Zero;
429 lock (s_resourceFoundCallbacksMap)
431 id = (IntPtr)s_requestId++;
433 s_resourceFoundCallbacksMap[id] = (IntPtr remoteResourceHandle, int result, IntPtr userData) =>
435 if (ResourceFound == null)
438 int requestId = (int)userData;
439 if (result == (int)IoTConnectivityError.None)
441 if (remoteResourceHandle != IntPtr.Zero)
443 RemoteResource resource = null;
446 resource = new RemoteResource(remoteResourceHandle);
448 catch (Exception exp)
450 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't clone RemoteResource's handle: " + exp.Message);
453 ResourceFoundEventArgs e = new ResourceFoundEventArgs()
455 RequestId = requestId,
458 ResourceFound?.Invoke(null, e);
462 Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null");
467 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result);
468 FindingErrorOccurred?.Invoke(null, e);
470 lock (s_resourceFoundCallbacksMap)
472 s_resourceFoundCallbacksMap.Remove(id);
477 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
478 int errorCode = Interop.IoTConnectivity.Client.ResourceFinder.AddResourceFoundCb(hostAddress, (int)connectivityType, queryHandle, s_resourceFoundCallbacksMap[id], id);
479 if (errorCode != (int)IoTConnectivityError.None)
481 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register resource found event handler");
482 lock (s_resourceFoundCallbacksMap)
484 s_resourceFoundCallbacksMap.Remove(id);
486 throw IoTConnectivityErrorFactory.GetException(errorCode);
492 /// Starts finding the device information of remote server.
495 /// Requests server for device information.
496 /// If succeeded, <see cref="DeviceInformationFound"/> event handler will be triggered with information of the device.\n
497 /// @a hostAddress could be <see cref="MulticastAddress"/> for IPv4 multicast.
500 /// http://tizen.org/privilege/internet
502 /// <param name="hostAddress">The host address of remote server</param>
503 /// <param name="query">The query specified as a filter for founding resources</param>
504 /// <returns>RequestId - An identifier for this request</returns>
505 /// <pre>Initialize() should be called to initialize.</pre>
507 /// <see cref="DeviceInformationFound" /> event handler will be invoked.
509 /// <seealso cref="IoTConnectivityServerManager.SetDeviceName()"/>
510 /// <seealso cref="DeviceInformationFound"/>
511 /// <seealso cref="DeviceInformationFoundEventArgs"/>
512 /// <seealso cref="TimeOut"/>
514 /// EventHandler<DeviceInformationFoundEventArgs> handler = (sender, e) => {
515 /// Console.Log("Device information found, id : " + e.RequestId + ", name : " + e.Name);
517 /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
518 /// Console.Log("Found error :" + e.Error.Message);
520 /// IoTConnectivityClientManager.DeviceInformationFound += handler;
521 /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
522 /// // Do not forget to remove these event handlers when they are not required any more.
523 /// int id = IoTConnectivityClientManager.StartFindingDeviceInformation(IoTConnectivityClientManager.MulticastAddress);
525 public static int StartFindingDeviceInformation(string hostAddress, ResourceQuery query = null)
527 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress);
528 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
530 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
531 throw new ArgumentException("Unable to parse host address");
534 IntPtr id = IntPtr.Zero;
535 lock (s_deviceInformationCallbacksMap)
537 id = (IntPtr)s_requestId++;
539 s_deviceInformationCallbacksMap[id] = (IntPtr deviceInfoHandle, int result, IntPtr userData) =>
541 if (DeviceInformationFound == null)
544 int requestId = (int)userData;
545 if (result == (int)IoTConnectivityError.None)
547 if (deviceInfoHandle != IntPtr.Zero)
549 DeviceInformationFoundEventArgs e = GetDeviceInformationFoundEventArgs(requestId, deviceInfoHandle);
552 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get DeviceInformationFoundEventArgs");
555 DeviceInformationFound?.Invoke(null, e);
559 Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null");
564 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result);
565 FindingErrorOccurred?.Invoke(null, e);
567 lock (s_deviceInformationCallbacksMap)
569 s_deviceInformationCallbacksMap.Remove(id);
575 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
576 int errorCode = Interop.IoTConnectivity.Client.DeviceInformation.Find(hostAddress, (int)connectivityType, queryHandle, s_deviceInformationCallbacksMap[id], id);
577 if (errorCode != (int)IoTConnectivityError.None)
579 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device information");
580 lock (s_deviceInformationCallbacksMap)
582 s_deviceInformationCallbacksMap.Remove(id);
584 throw IoTConnectivityErrorFactory.GetException(errorCode);
591 /// Starts finding the platform information of remote server.
594 /// Requests server for platform information.
595 /// If succeeded, <see cref="PlatformInformationFound" /> event handler will be triggered with information of the platform.\n
596 /// @a hostAddress could be <see cref="MulticastAddress"/> for IPv4 multicast.
599 /// http://tizen.org/privilege/internet
601 /// <param name="hostAddress">The host address of remote server</param>
602 /// <param name="query">The query specified as a filter for founding resources</param>
603 /// <returns>RequestId - An identifier for this request</returns>
604 /// <pre>Initialize() should be called to initialize.</pre>
606 /// <see cref="PlatformInformationFound" /> event handler will be invoked.
608 /// <seealso cref="PlatformInformationFound"/>
609 /// <seealso cref="PlatformInformationFoundEventArgs"/>
610 /// <seealso cref="TimeOut"/>
612 /// EventHandler<PlatformInformationFoundEventArgs> handler = (sender, e) => {
613 /// Console.Log("PlatformInformationFound :" + e.RequestId);
615 /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
616 /// Console.Log("Found error :" + e.Error.Message);
618 /// IoTConnectivityClientManager.PlatformInformationFound += handler;
619 /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
620 /// // Do not forget to remove these event handlers when they are not required any more.
621 /// int id = IoTConnectivityClientManager.StartFindingPlatformInformation(IoTConnectivityClientManager.MulticastAddress);
623 public static int StartFindingPlatformInformation(string hostAddress, ResourceQuery query = null)
625 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress);
626 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
628 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
629 throw new ArgumentException("Unable to parse host address");
632 IntPtr id = IntPtr.Zero;
633 lock (s_platformInformationCallbacksMap)
635 id = (IntPtr)s_requestId++;
637 s_platformInformationCallbacksMap[id] = (IntPtr platformInfoHandle, int result, IntPtr userData) =>
639 if (PlatformInformationFound == null)
642 int requestId = (int)userData;
643 if (result == (int)IoTConnectivityError.None)
645 if (platformInfoHandle != IntPtr.Zero)
647 PlatformInformationFoundEventArgs e = GetPlatformInformationFoundEventArgs(requestId, platformInfoHandle);
650 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get PlatformInformationFoundEventArgs");
653 PlatformInformationFound?.Invoke(null, e);
657 Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null");
662 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result);
663 FindingErrorOccurred?.Invoke(null, e);
665 lock (s_platformInformationCallbacksMap)
667 s_platformInformationCallbacksMap.Remove(id);
673 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
674 int errorCode = Interop.IoTConnectivity.Client.PlatformInformation.Find(hostAddress, (int)connectivityType, queryHandle, s_platformInformationCallbacksMap[id], id);
675 if (errorCode != (int)IoTConnectivityError.None)
677 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform information");
678 lock (s_platformInformationCallbacksMap)
680 s_platformInformationCallbacksMap.Remove(id);
682 throw IoTConnectivityErrorFactory.GetException(errorCode);
689 private static PresenceReceivedEventArgs GetPresenceReceivedEventArgs(int presenceId, IntPtr presenceResponseHandle)
694 int ret = Interop.IoTConnectivity.Client.PresenceResponse.GetHostAddress(presenceResponseHandle, out host);
695 if (ret != (int)IoTConnectivityError.None)
697 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get host address");
701 ret = Interop.IoTConnectivity.Client.PresenceResponse.GetResourceType(presenceResponseHandle, out type);
702 if (ret != (int)IoTConnectivityError.None)
704 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource type");
708 ret = Interop.IoTConnectivity.Client.PresenceResponse.GetTrigger(presenceResponseHandle, out trigger);
709 if (ret != (int)IoTConnectivityError.None)
711 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get event type");
715 PresenceReceivedEventArgs e = new PresenceReceivedEventArgs()
717 PresenceId = presenceId,
718 HostAddress = Marshal.PtrToStringAnsi(host),
719 Type = Marshal.PtrToStringAnsi(type),
720 EventType = (PresenceEventType)trigger
726 private static DeviceInformationFoundEventArgs GetDeviceInformationFoundEventArgs(int requestId, IntPtr deviceInfoHandle)
728 IntPtr name, specVersion, deviceId, dataModelVersion;
730 int ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.Name, out name);
731 if (ret != (int)IoTConnectivityError.None)
733 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get name");
737 ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.SpecVersion, out specVersion);
738 if (ret != (int)IoTConnectivityError.None)
740 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get spec version");
744 ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.Id, out deviceId);
745 if (ret != (int)IoTConnectivityError.None)
747 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device id");
751 ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.DataModelVersion, out dataModelVersion);
752 if (ret != (int)IoTConnectivityError.None)
754 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get data model version");
758 DeviceInformationFoundEventArgs e = new DeviceInformationFoundEventArgs()
760 RequestId = requestId,
761 Name = Marshal.PtrToStringAnsi(name),
762 SpecVersion = Marshal.PtrToStringAnsi(specVersion),
763 DeviceId = Marshal.PtrToStringAnsi(deviceId),
764 DataModelVersion = Marshal.PtrToStringAnsi(dataModelVersion)
770 private static PlatformInformationFoundEventArgs GetPlatformInformationFoundEventArgs(int requestId, IntPtr platformInfoHandle)
772 IntPtr platformId, manufacturerName, manufacturerUrl, modelNumber, dateOfManufacture, platformVersion, osVersion, hardwareVersion, firmwareVersion, supportUrl, systemTime;
774 int ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.Id, out platformId);
775 if (ret != (int)IoTConnectivityError.None)
777 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform id");
781 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.MfgName, out manufacturerName);
782 if (ret != (int)IoTConnectivityError.None)
784 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get manufacturer name");
788 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.MfgUrl, out manufacturerUrl);
789 if (ret != (int)IoTConnectivityError.None)
791 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get manufacturer url");
795 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.ModelNumber, out modelNumber);
796 if (ret != (int)IoTConnectivityError.None)
798 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get model number");
802 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.DateOfMfg, out dateOfManufacture);
803 if (ret != (int)IoTConnectivityError.None)
805 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get date of manufacture");
809 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.PlatformVer, out platformVersion);
810 if (ret != (int)IoTConnectivityError.None)
812 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform version");
816 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.OsVer, out osVersion);
817 if (ret != (int)IoTConnectivityError.None)
819 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to os version");
823 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.HardwareVer, out hardwareVersion);
824 if (ret != (int)IoTConnectivityError.None)
826 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to hardware version");
830 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.FirmwareVer, out firmwareVersion);
831 if (ret != (int)IoTConnectivityError.None)
833 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get firmware version");
837 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.SupportUrl, out supportUrl);
838 if (ret != (int)IoTConnectivityError.None)
840 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get support url");
844 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.SystemTime, out systemTime);
845 if (ret != (int)IoTConnectivityError.None)
847 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get system time");
851 PlatformInformationFoundEventArgs e = new PlatformInformationFoundEventArgs()
853 RequestId = requestId,
854 PlatformId = (platformId != IntPtr.Zero) ? Marshal.PtrToStringAnsi(platformId) : string.Empty,
855 ManufacturerName = (manufacturerName != IntPtr.Zero) ? Marshal.PtrToStringAnsi(manufacturerName) : string.Empty,
856 ManufacturerURL = (manufacturerUrl != IntPtr.Zero) ? Marshal.PtrToStringAnsi(manufacturerUrl) : string.Empty,
857 DateOfManufacture = (dateOfManufacture != IntPtr.Zero) ? Marshal.PtrToStringAnsi(dateOfManufacture) : string.Empty,
858 ModelNumber = (modelNumber != IntPtr.Zero) ? Marshal.PtrToStringAnsi(modelNumber) : string.Empty,
859 PlatformVersion = (platformVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(platformVersion) : string.Empty,
860 OsVersion = (osVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(osVersion) : string.Empty,
861 HardwareVersion = (hardwareVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(hardwareVersion) : string.Empty,
862 FirmwareVersion = (firmwareVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(firmwareVersion) : string.Empty,
863 SupportUrl = (supportUrl != IntPtr.Zero) ? Marshal.PtrToStringAnsi(supportUrl) : string.Empty,
864 SystemTime = (systemTime != IntPtr.Zero) ? Marshal.PtrToStringAnsi(systemTime) : string.Empty
870 private static FindingErrorOccurredEventArgs GetFindingErrorOccurredEventArgs(int requestId, int err)
872 FindingErrorOccurredEventArgs e = new FindingErrorOccurredEventArgs()
874 RequestId = requestId,
875 Error = IoTConnectivityErrorFactory.GetException(err)