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;
12 namespace Tizen.Network.IoTConnectivity
14 public static class IoTConnectivityClientManager
16 public const string MulticastAddress = null;
18 private static int s_presenceListenerId = 1;
19 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.Presence.PresenceCallback> s_presenceCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.Presence.PresenceCallback>();
20 private static Dictionary<IntPtr, IntPtr> s_presenceHandlesMap = new Dictionary<IntPtr, IntPtr>();
22 private static int s_requestId = 1;
23 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.ResourceFinder.FoundResourceCallback> s_resourceFoundCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.ResourceFinder.FoundResourceCallback>();
24 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.DeviceInformation.DeviceInformationCallback> s_deviceInformationCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.DeviceInformation.DeviceInformationCallback>();
25 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.PlatformInformation.PlatformInformationCallback> s_platformInformationCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.PlatformInformation.PlatformInformationCallback>();
28 /// presence event on the resource
30 public static event EventHandler<PresenceReceivedEventArgs> PresenceReceived;
33 /// Resource found event handler
35 public static event EventHandler<ResourceFoundEventArgs> ResourceFound;
38 /// PlatformInformationFound event handler
40 public static event EventHandler<PlatformInformationFoundEventArgs> PlatformInformationFound;
43 /// DeviceInformationFound event handler
45 public static event EventHandler<DeviceInformationFoundEventArgs> DeviceInformationFound;
48 /// FoundError event handler
50 public static event EventHandler<FindingErrorOccurredEventArgs> FindingErrorOccurred;
55 public static int TimeOut
60 int ret = Interop.IoTConnectivity.Client.IoTCon.GetTimeout(out timeout);
61 if (ret != (int)IoTConnectivityError.None)
63 Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get timeout");
70 int ret = Interop.IoTConnectivity.Client.IoTCon.SetTimeout(value);
71 if (ret != (int)IoTConnectivityError.None)
73 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set timeout");
74 throw IoTConnectivityErrorFactory.GetException(ret);
80 /// Connects to the iotcon service
82 public static void Initialize()
84 int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize();
85 if (ret != (int)IoTConnectivityError.None)
87 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
88 throw IoTConnectivityErrorFactory.GetException(ret);
93 /// Disconnects from the iotcon service
95 public static void Deinitialize()
97 Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
101 /// Starts receiving presence events
106 public static int StartReceivingPresence(string hostAddress, string resourceType)
108 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress);
109 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
111 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
112 throw new ArgumentException("Unable to parse host address");
115 if (resourceType != null && !ResourceTypes.IsValid(resourceType))
117 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
118 throw new ArgumentException("Invalid type");
121 IntPtr id = IntPtr.Zero;
122 lock (s_presenceCallbacksMap)
124 id = (IntPtr)s_presenceListenerId++;
126 s_presenceCallbacksMap[id] = (IntPtr presence, int result, IntPtr presenceResponseHandle, IntPtr userData) =>
128 if (presenceResponseHandle != IntPtr.Zero)
130 int presenceId = (int)userData;
133 PresenceReceivedEventArgs e = GetPresenceReceivedEventArgs(presenceId, presenceResponseHandle);
136 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get PresenceReceivedEventArgs");
139 PresenceReceived?.Invoke(null, e);
143 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(presenceId, result);
144 FindingErrorOccurred?.Invoke(null, e);
149 IntPtr presenceHandle;
150 int errorCode = Interop.IoTConnectivity.Client.Presence.AddPresenceCb(hostAddress, (int)connectivityType, resourceType, s_presenceCallbacksMap[id], id, out presenceHandle);
151 if (errorCode != (int)IoTConnectivityError.None)
153 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register presence event handler");
154 lock (s_presenceCallbacksMap)
156 s_presenceCallbacksMap.Remove(id);
158 throw IoTConnectivityErrorFactory.GetException(errorCode);
161 lock (s_presenceHandlesMap)
163 s_presenceHandlesMap[id] = presenceHandle;
169 /// Stops receiving presence events
171 public static void StopReceivingPresence(int presenceId)
173 if (s_presenceHandlesMap.ContainsKey((IntPtr)presenceId))
175 IntPtr presenceHandle = s_presenceHandlesMap[(IntPtr)presenceId];
176 int ret = Interop.IoTConnectivity.Client.Presence.RemovePresenceCb(presenceHandle);
177 if (ret != (int)IoTConnectivityError.None)
179 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to deregister presence event handler");
180 throw IoTConnectivityErrorFactory.GetException(ret);
183 lock (s_presenceHandlesMap)
185 s_presenceHandlesMap.Remove((IntPtr)presenceId);
189 if (s_presenceCallbacksMap.ContainsKey((IntPtr)presenceId))
191 lock (s_presenceCallbacksMap)
193 s_presenceCallbacksMap.Remove((IntPtr)presenceId);
199 /// Starts finding resource events
204 public static int StartFindingResource(string hostAddress, string resourceType, bool isSecure = false)
206 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress);
207 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
209 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
210 throw new ArgumentException("Unable to parse host address");
213 if (resourceType != null && !ResourceTypes.IsValid(resourceType))
215 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
216 throw new ArgumentException("Invalid type");
219 IntPtr id = IntPtr.Zero;
220 lock (s_resourceFoundCallbacksMap)
222 id = (IntPtr)s_requestId++;
224 s_resourceFoundCallbacksMap[id] = (IntPtr remoteResourceHandle, int result, IntPtr userData) =>
226 if (remoteResourceHandle != IntPtr.Zero)
228 int requestId = (int)userData;
229 if (result == (int)IoTConnectivityError.None)
231 RemoteResource resource = null;
234 resource = new RemoteResource(remoteResourceHandle);
236 catch (Exception exp)
238 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't clone RemoteResource's handle: " + exp.Message);
241 ResourceFoundEventArgs e = new ResourceFoundEventArgs()
243 RequestId = requestId,
246 ResourceFound?.Invoke(null, e);
250 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result);
251 FindingErrorOccurred?.Invoke(null, e);
253 lock (s_resourceFoundCallbacksMap)
255 s_resourceFoundCallbacksMap.Remove(id);
261 int errorCode = Interop.IoTConnectivity.Client.ResourceFinder.AddResourceFoundCb(hostAddress, (int)connectivityType, resourceType, isSecure, s_resourceFoundCallbacksMap[id], id);
262 if (errorCode != (int)IoTConnectivityError.None)
264 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register resource found event handler");
265 lock (s_resourceFoundCallbacksMap)
267 s_resourceFoundCallbacksMap.Remove(id);
269 throw IoTConnectivityErrorFactory.GetException(errorCode);
275 /// Starts finding device information events
280 public static int StartFindingDeviceInformation(string hostAddress)
282 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress);
283 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
285 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
286 throw new ArgumentException("Unable to parse host address");
289 IntPtr id = IntPtr.Zero;
290 lock (s_deviceInformationCallbacksMap)
292 id = (IntPtr)s_requestId++;
294 s_deviceInformationCallbacksMap[id] = (IntPtr deviceInfoHandle, int result, IntPtr userData) =>
296 if (deviceInfoHandle != IntPtr.Zero)
298 int requestId = (int)userData;
301 DeviceInformationFoundEventArgs e = GetDeviceInformationFoundEventArgs(requestId, deviceInfoHandle);
304 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get DeviceInformationFoundEventArgs");
307 DeviceInformationFound?.Invoke(null, e);
311 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result);
312 FindingErrorOccurred?.Invoke(null, e);
314 lock (s_deviceInformationCallbacksMap)
316 s_deviceInformationCallbacksMap.Remove(id);
322 int errorCode = Interop.IoTConnectivity.Client.DeviceInformation.Get(hostAddress, (int)connectivityType, s_deviceInformationCallbacksMap[id], id);
323 if (errorCode != (int)IoTConnectivityError.None)
325 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device information");
326 lock (s_deviceInformationCallbacksMap)
328 s_deviceInformationCallbacksMap.Remove(id);
330 throw IoTConnectivityErrorFactory.GetException(errorCode);
337 /// Starts finding platform information events
342 public static int StartFindingPlatformInformation(string hostAddress)
344 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress);
345 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
347 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
348 throw new ArgumentException("Unable to parse host address");
351 IntPtr id = IntPtr.Zero;
352 lock (s_platformInformationCallbacksMap)
354 id = (IntPtr)s_requestId++;
356 s_platformInformationCallbacksMap[id] = (IntPtr platformInfoHandle, int result, IntPtr userData) =>
358 if (platformInfoHandle != IntPtr.Zero)
360 int requestId = (int)userData;
363 PlatformInformationFoundEventArgs e = GetPlatformInformationFoundEventArgs(requestId, platformInfoHandle);
366 Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get PlatformInformationFoundEventArgs");
369 PlatformInformationFound?.Invoke(null, e);
373 FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result);
374 FindingErrorOccurred?.Invoke(null, e);
376 lock (s_platformInformationCallbacksMap)
378 s_platformInformationCallbacksMap.Remove(id);
384 int errorCode = Interop.IoTConnectivity.Client.PlatformInformation.Get(hostAddress, (int)connectivityType, s_platformInformationCallbacksMap[id], id);
385 if (errorCode != (int)IoTConnectivityError.None)
387 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform information");
388 lock (s_platformInformationCallbacksMap)
390 s_platformInformationCallbacksMap.Remove(id);
392 throw IoTConnectivityErrorFactory.GetException(errorCode);
398 private static PresenceReceivedEventArgs GetPresenceReceivedEventArgs(int presenceId, IntPtr presenceResponseHandle)
403 int ret = Interop.IoTConnectivity.Client.PresenceResponse.GetHostAddress(presenceResponseHandle, out host);
404 if (ret != (int)IoTConnectivityError.None)
406 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get host address");
410 ret = Interop.IoTConnectivity.Client.PresenceResponse.GetResourceType(presenceResponseHandle, out type);
411 if (ret != (int)IoTConnectivityError.None)
413 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource type");
417 ret = Interop.IoTConnectivity.Client.PresenceResponse.GetTrigger(presenceResponseHandle, out trigger);
418 if (ret != (int)IoTConnectivityError.None)
420 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get event type");
424 PresenceReceivedEventArgs e = new PresenceReceivedEventArgs()
426 PresenceId = presenceId,
429 EventType = (PresenceEventType)trigger
435 private static DeviceInformationFoundEventArgs GetDeviceInformationFoundEventArgs(int requestId, IntPtr deviceInfoHandle)
437 string name, specVersion, deviceId, dataModelVersion;
439 int ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.Name, out name);
440 if (ret != (int)IoTConnectivityError.None)
442 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get name");
446 ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.SpecVersion, out specVersion);
447 if (ret != (int)IoTConnectivityError.None)
449 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get spec version");
453 ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.Id, out deviceId);
454 if (ret != (int)IoTConnectivityError.None)
456 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device id");
460 ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.DataModelVersion, out dataModelVersion);
461 if (ret != (int)IoTConnectivityError.None)
463 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get data model version");
467 DeviceInformationFoundEventArgs e = new DeviceInformationFoundEventArgs()
469 RequestId = requestId,
471 SpecVersion = specVersion,
473 DataModelVersion = dataModelVersion
479 private static PlatformInformationFoundEventArgs GetPlatformInformationFoundEventArgs(int requestId, IntPtr platformInfoHandle)
481 string platformId, manufacturerName, manufacturerUrl, modelNumber, dateOfManufacture, platformVersion, osVersion, hardwareVersion, firmwareVersion, supportUrl, systemTime;
483 int ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.Id, out platformId);
484 if (ret != (int)IoTConnectivityError.None)
486 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform id");
490 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.MfgName, out manufacturerName);
491 if (ret != (int)IoTConnectivityError.None)
493 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get manufacturer name");
497 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.MfgUrl, out manufacturerUrl);
498 if (ret != (int)IoTConnectivityError.None)
500 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get manufacturer url");
504 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.ModelNumber, out modelNumber);
505 if (ret != (int)IoTConnectivityError.None)
507 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get model number");
511 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.DateOfMfg, out dateOfManufacture);
512 if (ret != (int)IoTConnectivityError.None)
514 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get date of manufacture");
518 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.PlatformVer, out platformVersion);
519 if (ret != (int)IoTConnectivityError.None)
521 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform version");
525 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.OsVer, out osVersion);
526 if (ret != (int)IoTConnectivityError.None)
528 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to os version");
532 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.HardwareVer, out hardwareVersion);
533 if (ret != (int)IoTConnectivityError.None)
535 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to hardware version");
539 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.FirmwareVer, out firmwareVersion);
540 if (ret != (int)IoTConnectivityError.None)
542 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get firmware version");
546 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.SupportUrl, out supportUrl);
547 if (ret != (int)IoTConnectivityError.None)
549 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get support url");
553 ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.SystemTime, out systemTime);
554 if (ret != (int)IoTConnectivityError.None)
556 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get system time");
560 PlatformInformationFoundEventArgs e = new PlatformInformationFoundEventArgs()
562 RequestId = requestId,
563 PlatformId = platformId,
564 ManufacturerName = manufacturerName,
565 ManufacturerURL = manufacturerUrl,
566 DateOfManufacture = dateOfManufacture,
567 ModelNumber = modelNumber,
568 PlatformVersion = platformVersion,
569 OsVersion = osVersion,
570 HardwareVersion = hardwareVersion,
571 FirmwareVersion = firmwareVersion,
572 SupportUrl = supportUrl,
573 SystemTime = systemTime
579 private static FindingErrorOccurredEventArgs GetFindingErrorOccurredEventArgs(int requestId, int err)
581 FindingErrorOccurredEventArgs e = new FindingErrorOccurredEventArgs()
583 RequestId = requestId,
584 Error = IoTConnectivityErrorFactory.GetException(err)