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.
19 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using System.Threading.Tasks;
24 namespace Tizen.Network.IoTConnectivity
27 /// This class represents a remote resource.
28 /// It provides APIs to manage remote resource.
30 public class RemoteResource : IDisposable
32 internal const int TimeOutMax = 3600;
33 internal IntPtr _remoteResourceHandle = IntPtr.Zero;
35 private bool _disposed = false;
36 private bool _cacheEnabled = false;
37 private ResourceOptions _options;
39 private int _responseCallbackId = 1;
40 private static Dictionary<IntPtr, Interop.IoTConnectivity.Client.RemoteResource.ResponseCallback> _responseCallbacksMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Client.RemoteResource.ResponseCallback>();
42 private Interop.IoTConnectivity.Client.RemoteResource.CachedRepresentationChangedCallback _cacheUpdatedCallback;
43 private Interop.IoTConnectivity.Client.RemoteResource.StateChangedCallback _stateChangedCallback;
44 private Interop.IoTConnectivity.Client.RemoteResource.ObserveCallback _observeCallback;
46 private EventHandler<StateChangedEventArgs> _stateChangedEventHandler;
49 /// Creates a remote resource instance
52 /// To use this API, you should provide all of the details required to correctly contact and
53 /// observe the object.\n
54 /// If not, you should discover the resource object manually.\n
55 /// The @a policy can contain multiple policies like ResourcePolicy.Discoverable | ResourcePolicy.Observable.
57 /// <param name="hostAddress">The host address of the resource</param>
58 /// <param name="uriPath">The URI path of the resource</param>
59 /// <param name="policy">The policies of the resource</param>
60 /// <param name="resourceTypes">The resource types of the resource</param>
61 /// <param name="resourceInterfaces">The resource interfaces of the resource</param>
62 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
63 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
64 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
65 public RemoteResource(string hostAddress, string uriPath, ResourcePolicy policy, ResourceTypes resourceTypes, ResourceInterfaces resourceInterfaces)
67 if (hostAddress == null || uriPath == null || resourceTypes == null || resourceInterfaces == null)
69 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid parameters");
70 throw new ArgumentException("Invalid parameter");
73 HostAddress = hostAddress;
76 Types = new List<string>(resourceTypes);
77 Interfaces = new List<string>(resourceInterfaces);
80 CreateRemoteResource(resourceTypes._resourceTypeHandle, resourceInterfaces.ResourceInterfacesHandle);
83 internal RemoteResource(IntPtr handleToClone)
85 int ret = Interop.IoTConnectivity.Client.RemoteResource.Clone(handleToClone, out _remoteResourceHandle);
86 if (ret != (int)IoTConnectivityError.None)
88 Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to clone");
89 throw IoTConnectivityErrorFactory.GetException(ret);
95 /// Destructor of the RemoteResource class.
103 /// Event that is invoked with cached resource attributes
105 public event EventHandler<CacheUpdatedEventArgs> CacheUpdated;
108 /// Observe event on the resource sent by the server
110 public event EventHandler<ObserverNotifiedEventArgs> ObserverNotified;
113 /// Event that is called when remote resource's state are changed
115 public event EventHandler<StateChangedEventArgs> StateChanged
119 if (_stateChangedEventHandler == null)
121 RegisterStateChangedEvent();
123 _stateChangedEventHandler += value;
127 _stateChangedEventHandler -= value;
128 if (_stateChangedEventHandler == null)
130 UnregisterStateChangedEvent();
136 /// The host address of the resource
138 public string HostAddress { get; private set; }
141 /// The URI path of the resource
143 public string UriPath { get; private set; }
146 /// The resource types of the remote resource
148 public IEnumerable<string> Types { get; private set; }
151 /// The interfaces of the resource
153 public IEnumerable<string> Interfaces { get; private set; }
156 /// The policy of the resource
158 public ResourcePolicy Policy { get; private set; }
161 /// The device name of the remote resource
163 public string DeviceName { get; private set; }
166 /// The header options of the resource
168 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
169 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
170 public ResourceOptions Options
181 int ret = Interop.IoTConnectivity.Client.RemoteResource.SetOptions(_remoteResourceHandle, value._resourceOptionsHandle);
182 if (ret != (int)IoTConnectivityError.None)
184 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set options");
185 throw IoTConnectivityErrorFactory.GetException(ret);
192 /// Indicates the CacheEnabled status of the remote resource.
195 /// Client can start caching only when this is set true. Set it to false to stop caching the resource attributes.
197 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
198 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
199 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
200 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
201 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
202 public bool CacheEnabled
206 return _cacheEnabled;
210 if (_cacheEnabled != value)
212 _cacheEnabled = value;
213 HandleCachePolicyChanged();
219 /// Time interval of monitoring and caching API
222 /// Default time interval is 10 seconds.\n
223 /// Seconds for time interval (must be in range from 1 to 3600)
225 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
226 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
227 public int TimeInterval
232 int ret = Interop.IoTConnectivity.Client.RemoteResource.GetTimeInterval(_remoteResourceHandle, out interval);
233 if (ret != (int)IoTConnectivityError.None)
235 Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get time interval");
242 int ret = (int)IoTConnectivityError.InvalidParameter;
243 if (value <= TimeOutMax && value > 0)
245 ret = Interop.IoTConnectivity.Client.RemoteResource.SetTimeInterval(_remoteResourceHandle, value);
247 if (ret != (int)IoTConnectivityError.None)
249 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set time interval");
250 throw IoTConnectivityErrorFactory.GetException(ret);
256 /// The device id of the resource
258 public string DeviceId { get; private set; }
261 /// Gets cached representation from the remote resource
263 public Representation CachedRepresentation()
266 int ret = Interop.IoTConnectivity.Client.RemoteResource.GetCachedRepresentation(_remoteResourceHandle, out handle);
267 if (ret != (int)IoTConnectivityError.None)
269 Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get CachedRepresentation");
273 Representation representation = new Representation(handle);
274 return representation;
278 /// Starts observing on the resource
281 /// When server sends notification message, <see cref="ObserverNotified"/> will be called.
284 /// http://tizen.org/privilege/internet
286 /// <param name="policy">The type to specify how client wants to observe</param>
287 /// <param name="query">The query to send to server</param>
288 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
289 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
290 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
291 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
292 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
293 public void StartObserving(ObservePolicy policy, ResourceQuery query = null)
295 _observeCallback = (IntPtr resource, int err, int sequenceNumber, IntPtr response, IntPtr userData) =>
298 IntPtr representationHandle;
299 int ret = Interop.IoTConnectivity.Server.Response.GetResult(response, out result);
300 if (ret != (int)IoTConnectivityError.None)
302 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get result");
306 ret = Interop.IoTConnectivity.Server.Response.GetRepresentation(response, out representationHandle);
307 if (ret != (int)IoTConnectivityError.None)
309 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get representation");
313 Representation repr = null;
316 repr = new Representation(representationHandle);
318 catch (Exception exp)
320 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new representation: " + exp.Message);
324 ObserverNotifiedEventArgs e = new ObserverNotifiedEventArgs()
326 Representation = repr,
327 Result = (ResponseCode)result
329 ObserverNotified?.Invoke(this, e);
332 IntPtr queryHandle = IntPtr.Zero;
335 queryHandle = query._resourceQueryHandle;
338 int errCode = Interop.IoTConnectivity.Client.RemoteResource.RegisterObserve(_remoteResourceHandle, (int)policy, queryHandle, _observeCallback, IntPtr.Zero);
339 if (errCode != (int)IoTConnectivityError.None)
341 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register observe callbacks");
342 throw IoTConnectivityErrorFactory.GetException(errCode);
347 /// Stops observing on the resource
350 /// http://tizen.org/privilege/internet
352 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
353 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
354 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
355 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
356 public void StopObserving()
358 int ret = Interop.IoTConnectivity.Client.RemoteResource.DeregisterObserve(_remoteResourceHandle);
359 if (ret != (int)IoTConnectivityError.None)
361 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to deregister observe callbacks");
362 throw IoTConnectivityErrorFactory.GetException(ret);
367 /// Gets the attributes of a resource, asynchronously
370 /// http://tizen.org/privilege/internet
372 /// <param name="query">The ResourceQuery to send to server</param>
373 /// <returns>Remote response with result and representation</returns>
374 public async Task<RemoteResponse> GetAsync(ResourceQuery query = null)
376 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
378 IntPtr id = IntPtr.Zero;
379 lock (_responseCallbacksMap)
381 id = (IntPtr)_responseCallbackId++;
383 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
385 IntPtr responseCallbackId = userData;
386 lock(_responseCallbacksMap)
388 _responseCallbacksMap.Remove(responseCallbackId);
391 if (responseHandle != IntPtr.Zero)
395 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
399 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
400 tcsRemoteResponse.TrySetException(exp);
405 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
409 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
410 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Get(_remoteResourceHandle, queryHandle, _responseCallbacksMap[id], id);
411 if (errCode != (int)IoTConnectivityError.None)
413 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource attributes");
414 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
416 return await tcsRemoteResponse.Task;
420 /// Puts the representation of a resource, asynchronously.
423 /// http://tizen.org/privilege/internet
425 /// <param name="representation">Resource representation to put</param>
426 /// <param name="query">The ResourceQuery to send to server</param>
427 /// <returns>Remote response with result and representation</returns>
428 public async Task<RemoteResponse> PutAsync(Representation representation, ResourceQuery query = null)
430 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
432 IntPtr id = IntPtr.Zero;
433 lock (_responseCallbacksMap)
435 id = (IntPtr)_responseCallbackId++;
437 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
439 IntPtr responseCallbackId = userData;
440 lock (_responseCallbacksMap)
442 _responseCallbacksMap.Remove(responseCallbackId);
444 if (err == (int)(IoTConnectivityError.Iotivity))
446 RemoteResponse response = new RemoteResponse();
447 response.Result = ResponseCode.Forbidden;
448 response.Representation = null;
449 tcsRemoteResponse.TrySetResult(response);
451 else if (responseHandle != IntPtr.Zero)
455 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
457 catch (Exception exp)
459 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
460 tcsRemoteResponse.TrySetException(exp);
465 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
468 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
469 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Put(_remoteResourceHandle, representation._representationHandle, queryHandle, _responseCallbacksMap[id], id);
470 if (errCode != (int)IoTConnectivityError.None)
472 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to put resource representation");
473 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
475 return await tcsRemoteResponse.Task;
479 /// Post request on a resource, asynchronously
482 /// http://tizen.org/privilege/internet
484 /// <param name="representation">Resource representation of request</param>
485 /// <param name="query">The ResourceQuery to send to server</param>
486 /// <returns>Remote response with result and representation</returns>
487 public async Task<RemoteResponse> PostAsync(Representation representation, ResourceQuery query = null)
489 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
491 IntPtr id = IntPtr.Zero;
492 lock (_responseCallbacksMap)
494 id = (IntPtr)_responseCallbackId++;
496 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
498 IntPtr responseCallbackId = userData;
499 lock (_responseCallbacksMap)
501 _responseCallbacksMap.Remove(responseCallbackId);
503 if (responseHandle != IntPtr.Zero)
507 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
509 catch (Exception exp)
511 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
512 tcsRemoteResponse.TrySetException(exp);
517 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
520 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
521 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Post(_remoteResourceHandle, representation._representationHandle, queryHandle, _responseCallbacksMap[id], id);
522 if (errCode != (int)IoTConnectivityError.None)
524 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to post request");
525 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
527 return await tcsRemoteResponse.Task;
531 /// Deletes the resource, asynchronously
534 /// http://tizen.org/privilege/internet
536 /// <returns>Remote response with result and representation</returns>
537 public async Task<RemoteResponse> DeleteAsync()
539 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
541 IntPtr id = IntPtr.Zero;
542 lock (_responseCallbacksMap)
544 id = (IntPtr)_responseCallbackId++;
546 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
548 IntPtr responseCallbackId = userData;
549 lock (_responseCallbacksMap)
551 _responseCallbacksMap.Remove(responseCallbackId);
553 if (err == (int)(IoTConnectivityError.Iotivity))
555 RemoteResponse response = new RemoteResponse();
556 response.Result = ResponseCode.Forbidden;
557 response.Representation = null;
558 tcsRemoteResponse.TrySetResult(response);
560 else if (responseHandle != IntPtr.Zero)
564 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
566 catch (Exception exp)
568 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
569 tcsRemoteResponse.TrySetException(exp);
574 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
578 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Delete(_remoteResourceHandle, _responseCallbacksMap[id], id);
579 if (errCode != (int)IoTConnectivityError.None)
581 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to delete");
582 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
584 return await tcsRemoteResponse.Task;
588 /// Releases any unmanaged resources used by this object.
590 public void Dispose()
593 GC.SuppressFinalize(this);
596 internal static Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType GetConnectivityType(string hostAddress)
598 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None;
600 Log.Info(IoTConnectivityErrorFactory.LogTag, hostAddress);
602 if (hostAddress == IoTConnectivityClientManager.MulticastAddress)
604 type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv4;
609 string hostName = hostAddress;
610 if (hostAddress.Contains(":"))
612 string[] hostParts = hostAddress.Split(':');
613 if (hostParts.Length == 2)
615 hostName = hostParts[0];
618 if (hostAddress.Contains("%"))
620 string[] hostParts = hostAddress.Split('%');
621 if (hostParts.Length == 2)
623 hostName = hostParts[0];
626 if (hostName.Contains("["))
628 string[] hostParts = hostName.Split('[');
629 if (hostParts.Length == 2)
631 hostName = hostParts[1];
634 Log.Info(IoTConnectivityErrorFactory.LogTag, hostName);
635 if (IPAddress.TryParse(hostName, out address))
637 switch (address.AddressFamily)
639 case System.Net.Sockets.AddressFamily.InterNetwork:
640 type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv4;
642 case System.Net.Sockets.AddressFamily.InterNetworkV6:
643 type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv6;
646 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to parse for Ipv4 or Ipv6");
652 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to parse hostname " + hostName);
659 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
661 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
662 protected virtual void Dispose(bool disposing)
669 // Free managed objects
672 Interop.IoTConnectivity.Client.RemoteResource.Destroy(_remoteResourceHandle);
676 private void HandleCachePolicyChanged()
680 _cacheUpdatedCallback = (IntPtr resource, IntPtr representation, IntPtr userData) =>
684 Representation repr = null;
687 repr = new Representation(representation);
689 catch (Exception exp)
691 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new Representation: " + exp.Message);
695 CacheUpdatedEventArgs e = new CacheUpdatedEventArgs()
697 Representation = repr
699 CacheUpdated?.Invoke(this, e);
703 int ret = Interop.IoTConnectivity.Client.RemoteResource.StartCaching(_remoteResourceHandle, _cacheUpdatedCallback, IntPtr.Zero);
704 if (ret != (int)IoTConnectivityError.None)
706 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add cache updated event handler");
707 throw IoTConnectivityErrorFactory.GetException(ret);
712 int ret = Interop.IoTConnectivity.Client.RemoteResource.StopCaching(_remoteResourceHandle);
713 if (ret != (int)IoTConnectivityError.None)
715 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove cache updated event handler");
716 throw IoTConnectivityErrorFactory.GetException(ret);
721 private void RegisterStateChangedEvent()
723 _stateChangedCallback = (IntPtr resource, int state, IntPtr userData) =>
725 StateChangedEventArgs e = new StateChangedEventArgs()
727 State = (ResourceState)state
729 _stateChangedEventHandler?.Invoke(null, e);
732 int ret = Interop.IoTConnectivity.Client.RemoteResource.StartMonitoring(_remoteResourceHandle, _stateChangedCallback, IntPtr.Zero);
733 if (ret != (int)IoTConnectivityError.None)
735 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state changed event handler");
736 throw IoTConnectivityErrorFactory.GetException(ret);
740 private void UnregisterStateChangedEvent()
742 int ret = Interop.IoTConnectivity.Client.RemoteResource.StopMonitoring(_remoteResourceHandle);
743 if (ret != (int)IoTConnectivityError.None)
745 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove state changed event handler");
746 throw IoTConnectivityErrorFactory.GetException(ret);
750 private void CreateRemoteResource(IntPtr resourceTypeHandle, IntPtr resourceInterfaceHandle)
752 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = GetConnectivityType(HostAddress);
753 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
755 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
756 throw new ArgumentException("Unable to parse host address");
758 int ret = Interop.IoTConnectivity.Client.RemoteResource.Create(HostAddress, (int)connectivityType, UriPath, (int)Policy, resourceTypeHandle, resourceInterfaceHandle, out _remoteResourceHandle);
759 if (ret != (int)IoTConnectivityError.None)
761 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get remote resource");
762 throw IoTConnectivityErrorFactory.GetException(ret);
766 ret = Interop.IoTConnectivity.Client.RemoteResource.GetDeviceName(_remoteResourceHandle, out deviceName);
767 if (ret != (int)IoTConnectivityError.None)
769 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device name of remote resource");
770 throw IoTConnectivityErrorFactory.GetException(ret);
772 DeviceName = Marshal.PtrToStringAnsi(deviceName);*/
775 private void SetRemoteResource()
777 IntPtr hostAddressPtr, uriPathPtr;
778 int ret = Interop.IoTConnectivity.Client.RemoteResource.GetHostAddress(_remoteResourceHandle, out hostAddressPtr);
779 if (ret != (int)IoTConnectivityError.None)
781 Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get host address");
782 throw IoTConnectivityErrorFactory.GetException(ret);
785 ret = Interop.IoTConnectivity.Client.RemoteResource.GetUriPath(_remoteResourceHandle, out uriPathPtr);
786 if (ret != (int)IoTConnectivityError.None)
788 Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get uri path");
789 throw IoTConnectivityErrorFactory.GetException(ret);
792 int policy = (int)ResourcePolicy.NoProperty;
793 ret = Interop.IoTConnectivity.Client.RemoteResource.GetPolicies(_remoteResourceHandle, out policy);
794 if (ret != (int)IoTConnectivityError.None)
796 Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get uri path");
797 throw IoTConnectivityErrorFactory.GetException(ret);
800 IntPtr typesHandle, interfacesHandle;
801 ret = Interop.IoTConnectivity.Client.RemoteResource.GetTypes(_remoteResourceHandle, out typesHandle);
802 if (ret != (int)IoTConnectivityError.None)
804 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource types");
805 throw IoTConnectivityErrorFactory.GetException(ret);
808 ret = Interop.IoTConnectivity.Client.RemoteResource.GetInterfaces(_remoteResourceHandle, out interfacesHandle);
809 if (ret != (int)IoTConnectivityError.None)
811 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource interfaces");
812 throw IoTConnectivityErrorFactory.GetException(ret);
816 ret = Interop.IoTConnectivity.Client.RemoteResource.GetDeviceId(_remoteResourceHandle, out deviceIdPtr);
817 if (ret != (int)IoTConnectivityError.None)
819 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device id");
820 throw IoTConnectivityErrorFactory.GetException(ret);
823 ret = Interop.IoTConnectivity.Client.RemoteResource.GetDeviceName(_remoteResourceHandle, out deviceName);
824 if (ret != (int)IoTConnectivityError.None)
826 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device name of remote resource");
827 throw IoTConnectivityErrorFactory.GetException(ret);
829 DeviceName = Marshal.PtrToStringAnsi(deviceName);
830 DeviceId = Marshal.PtrToStringAnsi(deviceIdPtr);
831 HostAddress = Marshal.PtrToStringAnsi(hostAddressPtr);
832 UriPath = Marshal.PtrToStringAnsi(uriPathPtr);
833 Types = new ResourceTypes(typesHandle);
834 Interfaces = new ResourceInterfaces(interfacesHandle);
835 Policy = (ResourcePolicy)policy;
838 private RemoteResponse GetRemoteResponse(IntPtr response)
841 IntPtr representationHandle, optionsHandle;
842 int ret = Interop.IoTConnectivity.Server.Response.GetResult(response, out result);
843 if (ret != (int)IoTConnectivityError.None)
845 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get result");
846 throw IoTConnectivityErrorFactory.GetException(ret);
849 ret = Interop.IoTConnectivity.Server.Response.GetRepresentation(response, out representationHandle);
850 if (ret != (int)IoTConnectivityError.None)
852 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get representation");
853 throw IoTConnectivityErrorFactory.GetException(ret);
856 ret = Interop.IoTConnectivity.Server.Response.GetOptions(response, out optionsHandle);
857 if (ret != (int)IoTConnectivityError.None)
859 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get options");
860 throw IoTConnectivityErrorFactory.GetException(ret);
862 return new RemoteResponse()
864 Result = (ResponseCode)result,
865 Representation = new Representation(representationHandle),
866 Options = (optionsHandle == IntPtr.Zero)? null : new ResourceOptions(optionsHandle)