2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
21 namespace Tizen.Network.IoTConnectivity
24 /// IoT connectivity client manager consists of client side APIs.
26 public static class IoTConnectivityClientManager
29 /// The IP Address for multicast
31 public const string MulticastAddress = null;
33 private static int s_presenceListenerId = 1;
34 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.Presence.PresenceCallback> s_presenceCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.Presence.PresenceCallback>();
35 private static Dictionary<IntPtr, IntPtr> s_presenceHandlesMap = new Dictionary<IntPtr, IntPtr>();
37 private static int s_requestId = 1;
38 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.ResourceFinder.FoundResourceCallback> s_resourceFoundCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.ResourceFinder.FoundResourceCallback>();
39 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.DeviceInformation.DeviceInformationCallback> s_deviceInformationCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.DeviceInformation.DeviceInformationCallback>();
40 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.PlatformInformation.PlatformInformationCallback> s_platformInformationCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.PlatformInformation.PlatformInformationCallback>();
43 /// PresenceReceived event. This event is occurred when server starts sending presence of a resource.
45 public static event EventHandler<PresenceReceivedEventArgs> PresenceReceived;
48 /// ResourceFound event. This event is occurred when a resource is found from the remote server
49 /// after sending request using API StartFindingResource().
51 public static event EventHandler<ResourceFoundEventArgs> ResourceFound;
54 /// PlatformInformationFound event. This event is occurred when platform information is found
55 /// after sending request using API StartFindingPlatformInformation().
57 public static event EventHandler<PlatformInformationFoundEventArgs> PlatformInformationFound;
60 /// DeviceInformationFound event. This event is occurred when device information is found
61 /// after sending request using API StartFindingDeviceInformation().
63 public static event EventHandler<DeviceInformationFoundEventArgs> DeviceInformationFound;
66 /// FindingError event. This event is occurred when an error is found.
68 public static event EventHandler<FindingErrorOccurredEventArgs> FindingErrorOccurred;
71 /// Timeout in seconds
74 /// Value to be set must be in range from 1 to 3600. Default timeout interval value is 30.\n
75 /// Sets/gets the timeout of StartFindingResource(), StartFindingDeviceInformation(), StartFindingPlatformInformation(),
76 /// RemoteResource.GetAsync(), RemoteResource.PutAsync(), RemoteResource.PostAsync() and RemoteResource.DeleteAsync() APIs.\n
77 /// Setter can throw exception.
80 /// Initialize() should be called to initialize
83 /// IoTConnectivityClientManager.Initialize();
84 /// IoTConnectivityClientManager.TimeOut = 120;
86 public static int TimeOut
91 int ret = Interop.IoTConnectivity.Client.IoTCon.GetTimeout(out timeout);
92 if (ret != (int)IoTConnectivityError.None)
94 Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get timeout");
101 int ret = Interop.IoTConnectivity.Client.IoTCon.SetTimeout(value);
102 if (ret != (int)IoTConnectivityError.None)
104 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set timeout");
105 throw IoTConnectivityErrorFactory.GetException(ret);
111 /// Polling interval of IoTConnectivity
114 /// Sets/Gets the polling inerval(milliseconds) of IoTCon. Default value is 100 milliseconds.
115 /// Value to be set must be in range from 1 to 999. The closer to 0, the faster it operates.
116 /// Setter is invoked immediately for changing the interval.
117 /// If you want the faster operation, we recommend you set 10 milliseconds for polling interval.
118 /// Setter can throw exception.
121 /// Initialize() should be called to initialize
124 /// IoTConnectivityClientManager.Initialize();
125 /// IoTConnectivityClientManager.PollingInterval = 100;
127 public static int PollingInterval
132 int ret = Interop.IoTConnectivity.Client.IoTCon.GetPollingInterval(out interval);
133 if (ret != (int)IoTConnectivityError.None)
135 Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get polling interval");
142 int ret = Interop.IoTConnectivity.Client.IoTCon.SetPollingInterval(value);
143 if (ret != (int)IoTConnectivityError.None)
145 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set polling interval");
146 throw IoTConnectivityErrorFactory.GetException(ret);
152 /// Initializes IoTCon.
153 /// Call this function to start IoTCon.
156 /// @a filePath point to a file for handling secure virtual resources.
157 /// The file that is CBOR(Concise Binary Object Representation)-format must already exist
158 /// in @a filePath. We recommend to use application-local file for @a filePath.
161 /// http://tizen.org/privilege/network.get \n
162 /// http://tizen.org/privilege/internet
164 /// <param name="filePath">The file path to point to storage for handling secure virtual resources.</param>
166 /// You must call Deinitialize() if IoTCon API is no longer needed.
168 /// <seealso cref="Deinitialize()"/>
170 /// string filePath = "../../res/iotcon-test-svr-db-client.dat";
171 /// IoTConnectivityClientManager.Initialize(filePath);
173 public static void Initialize(string filePath)
175 int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath);
176 if (ret != (int)IoTConnectivityError.None)
178 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
179 throw IoTConnectivityErrorFactory.GetException(ret);
184 /// Deinitializes IoTCon.
187 /// This API must be called if IoTCon API is no longer needed.
190 /// Initialize() should be called to initialize.
192 /// <seealso cref="Initialize()"/>
193 /// <seealso cref="SecureInitialize()"/>
195 /// IoTConnectivityClientManager.Deinitialize();
197 public static void Deinitialize()
199 s_presenceListenerId = 1;
200 s_presenceCallbacksMap.Clear();
201 s_presenceHandlesMap.Clear();
204 s_resourceFoundCallbacksMap.Clear();
205 s_deviceInformationCallbacksMap.Clear();
206 s_platformInformationCallbacksMap.Clear();
208 PresenceReceived = delegate{};
209 ResourceFound = delegate{};
210 PlatformInformationFound = delegate{};
211 DeviceInformationFound = delegate{};
212 FindingErrorOccurred = delegate{};
214 Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
218 /// Invokes a next message from a queue for receiving messages from others, immediately.
221 /// This API invokes a next message from a queue for receiving messages from others, immediately.
222 /// After calling the API, it continues the polling with existing interval.
225 /// Initialize() should be called to initialize.
228 /// IoTConnectivityClientManager.InvokePolling();
230 public static void InvokePolling()
232 int ret = Interop.IoTConnectivity.Client.IoTCon.InvokePolling();
233 if (ret != (int)IoTConnectivityError.None)
235 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to invoke polling");
236 throw IoTConnectivityErrorFactory.GetException(ret);
241 /// Starts receiving presence events
244 /// Sends request to receive presence to an interested server's resource with resourceType.
245 /// If succeeded, <see cref="PresenceReceived"/> event handler will be triggered when the server sends presence.
246 /// A server sends presence events when adds / removes / alters a resource or start / stop presence.\n
247 /// @a hostAddress could be <see cref="MulticastAddress"/> for IPv4 multicast.
248 /// 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
249 /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.
252 /// http://tizen.org/privilege/internet
254 /// <param name="hostAddress">The address or addressable name of the server</param>
255 /// <param name="resourceType">A resource type that a client is interested in</param>
256 /// <returns>PresenceId - An identifier for this request</returns>
257 /// <pre>Initialize() should be called to initialize.</pre>
259 /// When the resource receive presence, <see cref="PresenceReceived"/> event handler will be invoked.\n
260 /// You must destroy presence by calling StopReceivingPresence() if presence event is no longer needed.
262 /// <seealso cref="IoTConnectivityServerManager.StartSendingPresence()"/>
263 /// <seealso cref="IoTConnectivityServerManager.StopSendingPresence()"/>
264 /// <seealso cref="StopReceivingPresence()"/>
265 /// <seealso cref="PresenceReceived"/>
267 /// EventHandler<PresenceReceivedEventArgs> handler = (sender, e) => {
268 /// Console.Log("PresenceReceived, presence id :" + e.PresenceId);
270 /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
271 /// Console.Log("Found error :" + e.Error.Message);
273 /// IoTConnectivityClientManager.PresenceReceived += handler;
274 /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
275 /// // Do not forget to remove these event handlers when they are not required any more.
276 /// int id = IoTConnectivityClientManager.StartReceivingPresence(IoTConnectivityClientManager.MulticastAddress, "oic.iot.door");
278 public static int StartReceivingPresence(string hostAddress, string resourceType)
280 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ip;
282 if (resourceType != null && !ResourceTypes.IsValid(resourceType))
284 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
285 throw new ArgumentException("Invalid type");
288 IntPtr id = IntPtr.Zero;
289 lock (s_presenceCallbacksMap)
291 id = (IntPtr)s_presenceListenerId++;
293 s_presenceCallbacksMap[id] = (IntPtr presence, int result, IntPtr presenceResponseHandle, IntPtr userData) =>
295 int presenceId = (int)userData;
296 if (result == (int)IoTConnectivityError.None)
298 if (presenceResponseHandle != IntPtr.Zero)
300 PresenceReceivedEventArgs e = GetPresenceReceivedEventArgs(presenceId, presenceResponseHandle);
303 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get PresenceReceivedEventArgs");
306 PresenceReceived?.Invoke(null, e);
310 Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null");
316 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(presenceId, result);
317 FindingErrorOccurred?.Invoke(null, e);
321 IntPtr presenceHandle;
322 int errorCode = Interop.IoTConnectivity.Client.Presence.AddPresenceCb(hostAddress, (int)connectivityType, resourceType, s_presenceCallbacksMap[id], id, out presenceHandle);
323 if (errorCode != (int)IoTConnectivityError.None)
325 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register presence event handler");
326 lock (s_presenceCallbacksMap)
328 s_presenceCallbacksMap.Remove(id);
330 throw IoTConnectivityErrorFactory.GetException(errorCode);
333 lock (s_presenceHandlesMap)
335 s_presenceHandlesMap[id] = presenceHandle;
341 /// Stops receiving presence events
344 /// Sends request to not to receive server's presence any more.
347 /// http://tizen.org/privilege/internet
349 /// <param name="presenceId">The start presence request identifier</param>
351 /// Initialize() should be called to initialize.
353 /// <seealso cref="IoTConnectivityServerManager.StartSendingPresence()"/>
354 /// <seealso cref="IoTConnectivityServerManager.StopSendingPresence()"/>
355 /// <seealso cref="StartReceivingPresence()"/>
356 /// <seealso cref="PresenceReceived"/>
358 /// EventHandler<PresenceReceivedEventArgs> handler = (sender, e) => {
359 /// Console.Log("PresenceReceived, presence id :" + e.PresenceId);
361 /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
362 /// Console.Log("Found error :" + e.Error.Message);
364 /// IoTConnectivityClientManager.PresenceReceived += handler;
365 /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
366 /// int id = IoTConnectivityClientManager.StartReceivingPresence(IoTConnectivityClientManager.MulticastAddress, "oic.iot.door");
367 /// await Task.Delay(5000); // Do other things here
368 /// // Call StopReceivingPresence() when receiving presence is not required any more
369 /// IoTConnectivityClientManager.PresenceReceived -= handler;
370 /// IoTConnectivityClientManager.FindingErrorOccurred -= errorHandler;
371 /// IoTConnectivityClientManager.StopReceivingPresence(id);
373 public static void StopReceivingPresence(int presenceId)
375 if (s_presenceHandlesMap.ContainsKey((IntPtr)presenceId))
377 IntPtr presenceHandle = s_presenceHandlesMap[(IntPtr)presenceId];
378 int ret = Interop.IoTConnectivity.Client.Presence.RemovePresenceCb(presenceHandle);
379 if (ret != (int)IoTConnectivityError.None)
381 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to deregister presence event handler");
382 throw IoTConnectivityErrorFactory.GetException(ret);
385 lock (s_presenceHandlesMap)
387 s_presenceHandlesMap.Remove((IntPtr)presenceId);
391 if (s_presenceCallbacksMap.ContainsKey((IntPtr)presenceId))
393 lock (s_presenceCallbacksMap)
395 s_presenceCallbacksMap.Remove((IntPtr)presenceId);
401 /// Starts finding resources.
404 /// Sends request to find a resource of @a hostAddress server with @a resourceType.
405 /// If succeeded, <see cref="ResourceFound"/> event handler will be triggered with information of the resource.\n
406 /// @a hostAddress could be <see cref="MulticastAddress"/> for IPv4 multicast.
407 /// 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
408 /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.
411 /// http://tizen.org/privilege/internet
413 /// <param name="hostAddress">The address or addressable name of the server. The address includes a protocol like coaps://</param>
414 /// <param name="query">The query specified as a filter for founding resources</param>
415 /// <returns>RequestId - An identifier for this request</returns>
416 /// <pre>Initialize() should be called to initialize.</pre>
418 /// When the resource is found, <see cref="ResourceFound"/> event handler will be invoked.
420 /// <seealso cref="ResourceFound"/>
421 /// <seealso cref="ResourceFoundEventArgs"/>
422 /// <seealso cref="TimeOut"/>
424 /// EventHandler<ResourceFoundEventArgs> handler = (sender, e) => {
425 /// Console.Log("Found resource at host address :" + e.Resource.HostAddress + ", uri :" + e.Resource.UriPath);
427 /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
428 /// Console.Log("Found error :" + e.Error.Message);
430 /// IoTConnectivityClientManager.ResourceFound += handler;
431 /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
432 /// ResourceQuery query = new ResourceQuery();
433 /// query.Type = "oic.iot.door";
434 /// // Do not forget to remove these event handlers when they are not required any more.
435 /// int id = IoTConnectivityClientManager.StartFindingResource(null, query);
437 public static int StartFindingResource(string hostAddress, ResourceQuery query = null)
439 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ip;
441 IntPtr id = IntPtr.Zero;
442 lock (s_resourceFoundCallbacksMap)
444 id = (IntPtr)s_requestId++;
446 s_resourceFoundCallbacksMap[id] = (IntPtr remoteResourceHandle, int result, IntPtr userData) =>
448 if (ResourceFound == null)
451 int requestId = (int)userData;
452 if (result == (int)IoTConnectivityError.None)
454 if (remoteResourceHandle != IntPtr.Zero)
456 RemoteResource resource = null;
459 resource = new RemoteResource(remoteResourceHandle);
461 catch (Exception exp)
463 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't clone RemoteResource's handle: " + exp.Message);
466 ResourceFoundEventArgs e = new ResourceFoundEventArgs()
468 RequestId = requestId,
471 ResourceFound?.Invoke(null, e);
475 Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null");
480 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result);
481 FindingErrorOccurred?.Invoke(null, e);
483 lock (s_resourceFoundCallbacksMap)
485 s_resourceFoundCallbacksMap.Remove(id);
490 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
491 int errorCode = Interop.IoTConnectivity.Client.ResourceFinder.AddResourceFoundCb(hostAddress, (int)connectivityType, queryHandle, s_resourceFoundCallbacksMap[id], id);
492 if (errorCode != (int)IoTConnectivityError.None)
494 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register resource found event handler");
495 lock (s_resourceFoundCallbacksMap)
497 s_resourceFoundCallbacksMap.Remove(id);
499 throw IoTConnectivityErrorFactory.GetException(errorCode);
505 /// Starts finding the device information of remote server.
508 /// Requests server for device information.
509 /// If succeeded, <see cref="DeviceInformationFound"/> event handler will be triggered with information of the device.\n
510 /// @a hostAddress could be <see cref="MulticastAddress"/> for IPv4 multicast.
513 /// http://tizen.org/privilege/internet
515 /// <param name="hostAddress">The host address of remote server</param>
516 /// <param name="query">The query specified as a filter for founding resources</param>
517 /// <returns>RequestId - An identifier for this request</returns>
518 /// <pre>Initialize() should be called to initialize.</pre>
520 /// <see cref="DeviceInformationFound" /> event handler will be invoked.
522 /// <seealso cref="IoTConnectivityServerManager.SetDeviceName()"/>
523 /// <seealso cref="DeviceInformationFound"/>
524 /// <seealso cref="DeviceInformationFoundEventArgs"/>
525 /// <seealso cref="TimeOut"/>
527 /// EventHandler<DeviceInformationFoundEventArgs> handler = (sender, e) => {
528 /// Console.Log("Device information found, id : " + e.RequestId + ", name : " + e.Name);
530 /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
531 /// Console.Log("Found error :" + e.Error.Message);
533 /// IoTConnectivityClientManager.DeviceInformationFound += handler;
534 /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
535 /// // Do not forget to remove these event handlers when they are not required any more.
536 /// int id = IoTConnectivityClientManager.StartFindingDeviceInformation(IoTConnectivityClientManager.MulticastAddress);
538 public static int StartFindingDeviceInformation(string hostAddress, ResourceQuery query = null)
540 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ip;
542 IntPtr id = IntPtr.Zero;
543 lock (s_deviceInformationCallbacksMap)
545 id = (IntPtr)s_requestId++;
547 s_deviceInformationCallbacksMap[id] = (IntPtr deviceInfoHandle, int result, IntPtr userData) =>
549 if (DeviceInformationFound == null)
552 int requestId = (int)userData;
553 if (result == (int)IoTConnectivityError.None)
555 if (deviceInfoHandle != IntPtr.Zero)
557 DeviceInformationFoundEventArgs e = GetDeviceInformationFoundEventArgs(requestId, deviceInfoHandle);
560 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get DeviceInformationFoundEventArgs");
563 DeviceInformationFound?.Invoke(null, e);
567 Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null");
572 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result);
573 FindingErrorOccurred?.Invoke(null, e);
575 lock (s_deviceInformationCallbacksMap)
577 s_deviceInformationCallbacksMap.Remove(id);
583 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
584 int errorCode = Interop.IoTConnectivity.Client.DeviceInformation.Find(hostAddress, (int)connectivityType, queryHandle, s_deviceInformationCallbacksMap[id], id);
585 if (errorCode != (int)IoTConnectivityError.None)
587 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device information");
588 lock (s_deviceInformationCallbacksMap)
590 s_deviceInformationCallbacksMap.Remove(id);
592 throw IoTConnectivityErrorFactory.GetException(errorCode);
599 /// Starts finding the platform information of remote server.
602 /// Requests server for platform information.
603 /// If succeeded, <see cref="PlatformInformationFound" /> event handler will be triggered with information of the platform.\n
604 /// @a hostAddress could be <see cref="MulticastAddress"/> for IPv4 multicast.
607 /// http://tizen.org/privilege/internet
609 /// <param name="hostAddress">The host address of remote server</param>
610 /// <param name="query">The query specified as a filter for founding resources</param>
611 /// <returns>RequestId - An identifier for this request</returns>
612 /// <pre>Initialize() should be called to initialize.</pre>
614 /// <see cref="PlatformInformationFound" /> event handler will be invoked.
616 /// <seealso cref="PlatformInformationFound"/>
617 /// <seealso cref="PlatformInformationFoundEventArgs"/>
618 /// <seealso cref="TimeOut"/>
620 /// EventHandler<PlatformInformationFoundEventArgs> handler = (sender, e) => {
621 /// Console.Log("PlatformInformationFound :" + e.RequestId);
623 /// EventHandler<FindingErrorOccurredEventArgs> errorHandler = (sender, e) => {
624 /// Console.Log("Found error :" + e.Error.Message);
626 /// IoTConnectivityClientManager.PlatformInformationFound += handler;
627 /// IoTConnectivityClientManager.FindingErrorOccurred += errorHandler;
628 /// // Do not forget to remove these event handlers when they are not required any more.
629 /// int id = IoTConnectivityClientManager.StartFindingPlatformInformation(IoTConnectivityClientManager.MulticastAddress);
631 public static int StartFindingPlatformInformation(string hostAddress, ResourceQuery query = null)
633 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ip;
635 IntPtr id = IntPtr.Zero;
636 lock (s_platformInformationCallbacksMap)
638 id = (IntPtr)s_requestId++;
640 s_platformInformationCallbacksMap[id] = (IntPtr platformInfoHandle, int result, IntPtr userData) =>
642 if (PlatformInformationFound == null)
645 int requestId = (int)userData;
646 if (result == (int)IoTConnectivityError.None)
648 if (platformInfoHandle != IntPtr.Zero)
650 PlatformInformationFoundEventArgs e = GetPlatformInformationFoundEventArgs(requestId, platformInfoHandle);
653 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get PlatformInformationFoundEventArgs");
656 PlatformInformationFound?.Invoke(null, e);
660 Log.Error(IoTConnectivityErrorFactory.LogTag, "Handle is null");
665 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result);
666 FindingErrorOccurred?.Invoke(null, e);
668 lock (s_platformInformationCallbacksMap)
670 s_platformInformationCallbacksMap.Remove(id);
676 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
677 int errorCode = Interop.IoTConnectivity.Client.PlatformInformation.Find(hostAddress, (int)connectivityType, queryHandle, s_platformInformationCallbacksMap[id], id);
678 if (errorCode != (int)IoTConnectivityError.None)
680 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform information");
681 lock (s_platformInformationCallbacksMap)
683 s_platformInformationCallbacksMap.Remove(id);
685 throw IoTConnectivityErrorFactory.GetException(errorCode);
692 private static PresenceReceivedEventArgs GetPresenceReceivedEventArgs(int presenceId, IntPtr presenceResponseHandle)
697 int ret = Interop.IoTConnectivity.Client.PresenceResponse.GetHostAddress(presenceResponseHandle, out host);
698 if (ret != (int)IoTConnectivityError.None)
700 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get host address");
704 ret = Interop.IoTConnectivity.Client.PresenceResponse.GetResourceType(presenceResponseHandle, out type);
705 if (ret != (int)IoTConnectivityError.None)
707 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource type");
711 ret = Interop.IoTConnectivity.Client.PresenceResponse.GetTrigger(presenceResponseHandle, out trigger);
712 if (ret != (int)IoTConnectivityError.None)
714 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get event type");
718 PresenceReceivedEventArgs e = new PresenceReceivedEventArgs()
720 PresenceId = presenceId,
721 HostAddress = Marshal.PtrToStringAnsi(host),
722 Type = Marshal.PtrToStringAnsi(type),
723 EventType = (PresenceEventType)trigger
729 private static DeviceInformationFoundEventArgs GetDeviceInformationFoundEventArgs(int requestId, IntPtr deviceInfoHandle)
731 IntPtr name, specVersion, deviceId, dataModelVersion;
733 int ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.Name, out name);
734 if (ret != (int)IoTConnectivityError.None)
736 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get name");
740 ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.SpecVersion, out specVersion);
741 if (ret != (int)IoTConnectivityError.None)
743 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get spec version");
747 ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.Id, out deviceId);
748 if (ret != (int)IoTConnectivityError.None)
750 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device id");
754 ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.DataModelVersion, out dataModelVersion);
755 if (ret != (int)IoTConnectivityError.None)
757 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get data model version");
761 DeviceInformationFoundEventArgs e = new DeviceInformationFoundEventArgs()
763 RequestId = requestId,
764 Name = Marshal.PtrToStringAnsi(name),
765 SpecVersion = Marshal.PtrToStringAnsi(specVersion),
766 DeviceId = Marshal.PtrToStringAnsi(deviceId),
767 DataModelVersion = Marshal.PtrToStringAnsi(dataModelVersion)
773 private static PlatformInformationFoundEventArgs GetPlatformInformationFoundEventArgs(int requestId, IntPtr platformInfoHandle)
775 IntPtr platformId, manufacturerName, manufacturerUrl, modelNumber, dateOfManufacture, platformVersion, osVersion, hardwareVersion, firmwareVersion, supportUrl, systemTime;
777 int ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.Id, out platformId);
778 if (ret != (int)IoTConnectivityError.None)
780 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform id");
784 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.MfgName, out manufacturerName);
785 if (ret != (int)IoTConnectivityError.None)
787 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get manufacturer name");
791 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.MfgUrl, out manufacturerUrl);
792 if (ret != (int)IoTConnectivityError.None)
794 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get manufacturer url");
798 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.ModelNumber, out modelNumber);
799 if (ret != (int)IoTConnectivityError.None)
801 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get model number");
805 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.DateOfMfg, out dateOfManufacture);
806 if (ret != (int)IoTConnectivityError.None)
808 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get date of manufacture");
812 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.PlatformVer, out platformVersion);
813 if (ret != (int)IoTConnectivityError.None)
815 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform version");
819 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.OsVer, out osVersion);
820 if (ret != (int)IoTConnectivityError.None)
822 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to os version");
826 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.HardwareVer, out hardwareVersion);
827 if (ret != (int)IoTConnectivityError.None)
829 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to hardware version");
833 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.FirmwareVer, out firmwareVersion);
834 if (ret != (int)IoTConnectivityError.None)
836 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get firmware version");
840 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.SupportUrl, out supportUrl);
841 if (ret != (int)IoTConnectivityError.None)
843 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get support url");
847 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.SystemTime, out systemTime);
848 if (ret != (int)IoTConnectivityError.None)
850 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get system time");
854 PlatformInformationFoundEventArgs e = new PlatformInformationFoundEventArgs()
856 RequestId = requestId,
857 PlatformId = (platformId != IntPtr.Zero) ? Marshal.PtrToStringAnsi(platformId) : string.Empty,
858 ManufacturerName = (manufacturerName != IntPtr.Zero) ? Marshal.PtrToStringAnsi(manufacturerName) : string.Empty,
859 ManufacturerURL = (manufacturerUrl != IntPtr.Zero) ? Marshal.PtrToStringAnsi(manufacturerUrl) : string.Empty,
860 DateOfManufacture = (dateOfManufacture != IntPtr.Zero) ? Marshal.PtrToStringAnsi(dateOfManufacture) : string.Empty,
861 ModelNumber = (modelNumber != IntPtr.Zero) ? Marshal.PtrToStringAnsi(modelNumber) : string.Empty,
862 PlatformVersion = (platformVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(platformVersion) : string.Empty,
863 OsVersion = (osVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(osVersion) : string.Empty,
864 HardwareVersion = (hardwareVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(hardwareVersion) : string.Empty,
865 FirmwareVersion = (firmwareVersion != IntPtr.Zero) ? Marshal.PtrToStringAnsi(firmwareVersion) : string.Empty,
866 SupportUrl = (supportUrl != IntPtr.Zero) ? Marshal.PtrToStringAnsi(supportUrl) : string.Empty,
867 SystemTime = (systemTime != IntPtr.Zero) ? Marshal.PtrToStringAnsi(systemTime) : string.Empty
873 private static FindingErrorOccurredEventArgs GetFindingErrorOccurredEventArgs(int requestId, int err)
875 FindingErrorOccurredEventArgs e = new FindingErrorOccurredEventArgs()
877 RequestId = requestId,
878 Error = IoTConnectivityErrorFactory.GetException(err)