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 header options of the resource
163 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
164 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
165 public ResourceOptions Options
176 int ret = Interop.IoTConnectivity.Client.RemoteResource.SetOptions(_remoteResourceHandle, value._resourceOptionsHandle);
177 if (ret != (int)IoTConnectivityError.None)
179 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set options");
180 throw IoTConnectivityErrorFactory.GetException(ret);
187 /// Indicates the CacheEnabled status of the remote resource.
190 /// Client can start caching only when this is set true. Set it to false to stop caching the resource attributes.
192 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
193 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
194 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
195 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
196 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
197 public bool CacheEnabled
201 return _cacheEnabled;
205 if (_cacheEnabled != value)
207 _cacheEnabled = value;
208 HandleCachePolicyChanged();
214 /// Time interval of monitoring and caching API
217 /// Default time interval is 10 seconds.\n
218 /// Seconds for time interval (must be in range from 1 to 3600)
220 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
221 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
222 public int TimeInterval
227 int ret = Interop.IoTConnectivity.Client.RemoteResource.GetTimeInterval(_remoteResourceHandle, out interval);
228 if (ret != (int)IoTConnectivityError.None)
230 Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get time interval");
237 int ret = (int)IoTConnectivityError.InvalidParameter;
238 if (value <= TimeOutMax && value > 0)
240 ret = Interop.IoTConnectivity.Client.RemoteResource.SetTimeInterval(_remoteResourceHandle, value);
242 if (ret != (int)IoTConnectivityError.None)
244 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set time interval");
245 throw IoTConnectivityErrorFactory.GetException(ret);
251 /// The device id of the resource
253 public string DeviceId { get; private set; }
256 /// Gets cached representation from the remote resource
258 public Representation CachedRepresentation()
261 int ret = Interop.IoTConnectivity.Client.RemoteResource.GetCachedRepresentation(_remoteResourceHandle, out handle);
262 if (ret != (int)IoTConnectivityError.None)
264 Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get CachedRepresentation");
268 Representation representation = new Representation(handle);
269 return representation;
273 /// Starts observing on the resource
276 /// When server sends notification message, <see cref="ObserverNotified"/> will be called.
279 /// http://tizen.org/privilege/internet
281 /// <param name="policy">The type to specify how client wants to observe</param>
282 /// <param name="query">The query to send to server</param>
283 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
284 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
285 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
286 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
287 public void StartObserving(ObservePolicy policy, ResourceQuery query = null)
289 _observeCallback = (IntPtr resource, int err, int sequenceNumber, IntPtr response, IntPtr userData) =>
292 IntPtr representationHandle;
293 int ret = Interop.IoTConnectivity.Server.Response.GetResult(response, out result);
294 if (ret != (int)IoTConnectivityError.None)
296 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get result");
300 ret = Interop.IoTConnectivity.Server.Response.GetRepresentation(response, out representationHandle);
301 if (ret != (int)IoTConnectivityError.None)
303 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get representation");
307 Representation repr = null;
310 repr = new Representation(representationHandle);
312 catch (Exception exp)
314 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new representation: " + exp.Message);
318 ObserverNotifiedEventArgs e = new ObserverNotifiedEventArgs()
320 Representation = repr,
321 Result = (ResponseCode)result
323 ObserverNotified?.Invoke(this, e);
326 IntPtr queryHandle = IntPtr.Zero;
329 queryHandle = query._resourceQueryHandle;
332 int errCode = Interop.IoTConnectivity.Client.RemoteResource.RegisterObserve(_remoteResourceHandle, (int)policy, queryHandle, _observeCallback, IntPtr.Zero);
333 if (errCode != (int)IoTConnectivityError.None)
335 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register observe callbacks");
336 throw IoTConnectivityErrorFactory.GetException(errCode);
341 /// Stops observing on the resource
344 /// http://tizen.org/privilege/internet
346 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
347 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
348 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
349 public void StopObserving()
351 int ret = Interop.IoTConnectivity.Client.RemoteResource.DeregisterObserve(_remoteResourceHandle);
352 if (ret != (int)IoTConnectivityError.None)
354 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to deregister observe callbacks");
355 throw IoTConnectivityErrorFactory.GetException(ret);
360 /// Gets the attributes of a resource, asynchronously
363 /// http://tizen.org/privilege/internet
365 /// <param name="query">The ResourceQuery to send to server</param>
366 /// <returns>Remote response with result and representation</returns>
367 public async Task<RemoteResponse> GetAsync(ResourceQuery query = null)
369 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
371 IntPtr id = IntPtr.Zero;
372 lock (_responseCallbacksMap)
374 id = (IntPtr)_responseCallbackId++;
376 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
378 IntPtr responseCallbackId = userData;
379 lock(_responseCallbacksMap)
381 _responseCallbacksMap.Remove(responseCallbackId);
384 if (responseHandle != IntPtr.Zero)
388 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
392 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
393 tcsRemoteResponse.TrySetException(exp);
398 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
402 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
403 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Get(_remoteResourceHandle, queryHandle, _responseCallbacksMap[id], id);
404 if (errCode != (int)IoTConnectivityError.None)
406 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource attributes");
407 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
409 return await tcsRemoteResponse.Task;
413 /// Puts the representation of a resource, asynchronously.
416 /// http://tizen.org/privilege/internet
418 /// <param name="representation">Resource representation to put</param>
419 /// <param name="query">The ResourceQuery to send to server</param>
420 /// <returns>Remote response with result and representation</returns>
421 public async Task<RemoteResponse> PutAsync(Representation representation, ResourceQuery query = null)
423 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
425 IntPtr id = IntPtr.Zero;
426 lock (_responseCallbacksMap)
428 id = (IntPtr)_responseCallbackId++;
430 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
432 IntPtr responseCallbackId = userData;
433 lock (_responseCallbacksMap)
435 _responseCallbacksMap.Remove(responseCallbackId);
437 if (err == (int)(IoTConnectivityError.Iotivity))
439 RemoteResponse response = new RemoteResponse();
440 response.Result = ResponseCode.Forbidden;
441 response.Representation = null;
442 tcsRemoteResponse.TrySetResult(response);
444 else if (responseHandle != IntPtr.Zero)
448 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
450 catch (Exception exp)
452 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
453 tcsRemoteResponse.TrySetException(exp);
458 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
461 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
462 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Put(_remoteResourceHandle, representation._representationHandle, queryHandle, _responseCallbacksMap[id], id);
463 if (errCode != (int)IoTConnectivityError.None)
465 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to put resource representation");
466 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
468 return await tcsRemoteResponse.Task;
472 /// Post request on a resource, asynchronously
475 /// http://tizen.org/privilege/internet
477 /// <param name="representation">Resource representation of request</param>
478 /// <param name="query">The ResourceQuery to send to server</param>
479 /// <returns>Remote response with result and representation</returns>
480 public async Task<RemoteResponse> PostAsync(Representation representation, ResourceQuery query = null)
482 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
484 IntPtr id = IntPtr.Zero;
485 lock (_responseCallbacksMap)
487 id = (IntPtr)_responseCallbackId++;
489 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
491 IntPtr responseCallbackId = userData;
492 lock (_responseCallbacksMap)
494 _responseCallbacksMap.Remove(responseCallbackId);
496 if (responseHandle != IntPtr.Zero)
500 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
502 catch (Exception exp)
504 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
505 tcsRemoteResponse.TrySetException(exp);
510 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
513 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
514 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Post(_remoteResourceHandle, representation._representationHandle, queryHandle, _responseCallbacksMap[id], id);
515 if (errCode != (int)IoTConnectivityError.None)
517 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to post request");
518 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
520 return await tcsRemoteResponse.Task;
524 /// Deletes the resource, asynchronously
527 /// http://tizen.org/privilege/internet
529 /// <returns>Remote response with result and representation</returns>
530 public async Task<RemoteResponse> DeleteAsync()
532 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
534 IntPtr id = IntPtr.Zero;
535 lock (_responseCallbacksMap)
537 id = (IntPtr)_responseCallbackId++;
539 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
541 IntPtr responseCallbackId = userData;
542 lock (_responseCallbacksMap)
544 _responseCallbacksMap.Remove(responseCallbackId);
546 if (err == (int)(IoTConnectivityError.Iotivity))
548 RemoteResponse response = new RemoteResponse();
549 response.Result = ResponseCode.Forbidden;
550 response.Representation = null;
551 tcsRemoteResponse.TrySetResult(response);
553 else if (responseHandle != IntPtr.Zero)
557 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
559 catch (Exception exp)
561 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
562 tcsRemoteResponse.TrySetException(exp);
567 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
571 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Delete(_remoteResourceHandle, _responseCallbacksMap[id], id);
572 if (errCode != (int)IoTConnectivityError.None)
574 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to delete");
575 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
577 return await tcsRemoteResponse.Task;
581 /// Releases any unmanaged resources used by this object.
583 public void Dispose()
586 GC.SuppressFinalize(this);
589 internal static Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType GetConnectivityType(string hostAddress)
591 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None;
593 Log.Info(IoTConnectivityErrorFactory.LogTag, hostAddress);
595 if (hostAddress == IoTConnectivityClientManager.MulticastAddress)
597 type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv4;
602 string hostName = hostAddress;
603 if (hostAddress.Contains(":"))
605 string[] hostParts = hostAddress.Split(':');
606 if (hostParts.Length == 2)
608 hostName = hostParts[0];
611 if (hostAddress.Contains("%"))
613 string[] hostParts = hostAddress.Split('%');
614 if (hostParts.Length == 2)
616 hostName = hostParts[0];
619 if (hostName.Contains("["))
621 string[] hostParts = hostName.Split('[');
622 if (hostParts.Length == 2)
624 hostName = hostParts[1];
627 Log.Info(IoTConnectivityErrorFactory.LogTag, hostName);
628 if (IPAddress.TryParse(hostName, out address))
630 switch (address.AddressFamily)
632 case System.Net.Sockets.AddressFamily.InterNetwork:
633 type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv4;
635 case System.Net.Sockets.AddressFamily.InterNetworkV6:
636 type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv6;
639 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to parse for Ipv4 or Ipv6");
645 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to parse hostname " + hostName);
652 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
654 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
655 protected virtual void Dispose(bool disposing)
662 // Free managed objects
665 Interop.IoTConnectivity.Client.RemoteResource.Destroy(_remoteResourceHandle);
669 private void HandleCachePolicyChanged()
673 _cacheUpdatedCallback = (IntPtr resource, IntPtr representation, IntPtr userData) =>
677 Representation repr = null;
680 repr = new Representation(representation);
682 catch (Exception exp)
684 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new Representation: " + exp.Message);
688 CacheUpdatedEventArgs e = new CacheUpdatedEventArgs()
690 Representation = repr
692 CacheUpdated?.Invoke(this, e);
696 int ret = Interop.IoTConnectivity.Client.RemoteResource.StartCaching(_remoteResourceHandle, _cacheUpdatedCallback, IntPtr.Zero);
697 if (ret != (int)IoTConnectivityError.None)
699 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add cache updated event handler");
700 throw IoTConnectivityErrorFactory.GetException(ret);
705 int ret = Interop.IoTConnectivity.Client.RemoteResource.StopCaching(_remoteResourceHandle);
706 if (ret != (int)IoTConnectivityError.None)
708 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove cache updated event handler");
709 throw IoTConnectivityErrorFactory.GetException(ret);
714 private void RegisterStateChangedEvent()
716 _stateChangedCallback = (IntPtr resource, int state, IntPtr userData) =>
718 StateChangedEventArgs e = new StateChangedEventArgs()
720 State = (ResourceState)state
722 _stateChangedEventHandler?.Invoke(null, e);
725 int ret = Interop.IoTConnectivity.Client.RemoteResource.StartMonitoring(_remoteResourceHandle, _stateChangedCallback, IntPtr.Zero);
726 if (ret != (int)IoTConnectivityError.None)
728 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state changed event handler");
729 throw IoTConnectivityErrorFactory.GetException(ret);
733 private void UnregisterStateChangedEvent()
735 int ret = Interop.IoTConnectivity.Client.RemoteResource.StopMonitoring(_remoteResourceHandle);
736 if (ret != (int)IoTConnectivityError.None)
738 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove state changed event handler");
739 throw IoTConnectivityErrorFactory.GetException(ret);
743 private void CreateRemoteResource(IntPtr resourceTypeHandle, IntPtr resourceInterfaceHandle)
745 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = GetConnectivityType(HostAddress);
746 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
748 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
749 throw new ArgumentException("Unable to parse host address");
751 int ret = Interop.IoTConnectivity.Client.RemoteResource.Create(HostAddress, (int)connectivityType, UriPath, (int)Policy, resourceTypeHandle, resourceInterfaceHandle, out _remoteResourceHandle);
752 if (ret != (int)IoTConnectivityError.None)
754 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get remote resource");
755 throw IoTConnectivityErrorFactory.GetException(ret);
759 private void SetRemoteResource()
761 IntPtr hostAddressPtr, uriPathPtr;
762 int ret = Interop.IoTConnectivity.Client.RemoteResource.GetHostAddress(_remoteResourceHandle, out hostAddressPtr);
763 if (ret != (int)IoTConnectivityError.None)
765 Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get host address");
766 throw IoTConnectivityErrorFactory.GetException(ret);
769 ret = Interop.IoTConnectivity.Client.RemoteResource.GetUriPath(_remoteResourceHandle, out uriPathPtr);
770 if (ret != (int)IoTConnectivityError.None)
772 Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get uri path");
773 throw IoTConnectivityErrorFactory.GetException(ret);
776 int policy = (int)ResourcePolicy.NoProperty;
777 ret = Interop.IoTConnectivity.Client.RemoteResource.GetPolicies(_remoteResourceHandle, out policy);
778 if (ret != (int)IoTConnectivityError.None)
780 Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get uri path");
781 throw IoTConnectivityErrorFactory.GetException(ret);
784 IntPtr typesHandle, interfacesHandle;
785 ret = Interop.IoTConnectivity.Client.RemoteResource.GetTypes(_remoteResourceHandle, out typesHandle);
786 if (ret != (int)IoTConnectivityError.None)
788 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource types");
789 throw IoTConnectivityErrorFactory.GetException(ret);
792 ret = Interop.IoTConnectivity.Client.RemoteResource.GetInterfaces(_remoteResourceHandle, out interfacesHandle);
793 if (ret != (int)IoTConnectivityError.None)
795 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource interfaces");
796 throw IoTConnectivityErrorFactory.GetException(ret);
800 ret = Interop.IoTConnectivity.Client.RemoteResource.GetDeviceId(_remoteResourceHandle, out deviceIdPtr);
801 if (ret != (int)IoTConnectivityError.None)
803 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device id");
804 throw IoTConnectivityErrorFactory.GetException(ret);
806 DeviceId = (deviceIdPtr != IntPtr.Zero) ? Marshal.PtrToStringAnsi(deviceIdPtr) : string.Empty;
807 HostAddress = (hostAddressPtr != IntPtr.Zero) ? Marshal.PtrToStringAnsi(hostAddressPtr) : string.Empty;
808 UriPath = (uriPathPtr != IntPtr.Zero) ? Marshal.PtrToStringAnsi(uriPathPtr) : string.Empty;
809 Types = new ResourceTypes(typesHandle);
810 Interfaces = new ResourceInterfaces(interfacesHandle);
811 Policy = (ResourcePolicy)policy;
814 private RemoteResponse GetRemoteResponse(IntPtr response)
817 IntPtr representationHandle, optionsHandle;
818 int ret = Interop.IoTConnectivity.Server.Response.GetResult(response, out result);
819 if (ret != (int)IoTConnectivityError.None)
821 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get result");
822 throw IoTConnectivityErrorFactory.GetException(ret);
825 ret = Interop.IoTConnectivity.Server.Response.GetRepresentation(response, out representationHandle);
826 if (ret != (int)IoTConnectivityError.None)
828 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get representation");
829 throw IoTConnectivityErrorFactory.GetException(ret);
832 ret = Interop.IoTConnectivity.Server.Response.GetOptions(response, out optionsHandle);
833 if (ret != (int)IoTConnectivityError.None)
835 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get options");
836 throw IoTConnectivityErrorFactory.GetException(ret);
838 return new RemoteResponse()
840 Result = (ResponseCode)result,
841 Representation = new Representation(representationHandle),
842 Options = (optionsHandle == IntPtr.Zero)? null : new ResourceOptions(optionsHandle)