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="ArgumentException">Thrown when there is an invalid parameter</exception>
285 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
286 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
287 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
288 public void StartObserving(ObservePolicy policy, ResourceQuery query = null)
290 _observeCallback = (IntPtr resource, int err, int sequenceNumber, IntPtr response, IntPtr userData) =>
293 IntPtr representationHandle;
294 int ret = Interop.IoTConnectivity.Server.Response.GetResult(response, out result);
295 if (ret != (int)IoTConnectivityError.None)
297 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get result");
301 ret = Interop.IoTConnectivity.Server.Response.GetRepresentation(response, out representationHandle);
302 if (ret != (int)IoTConnectivityError.None)
304 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get representation");
308 Representation repr = null;
311 repr = new Representation(representationHandle);
313 catch (Exception exp)
315 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new representation: " + exp.Message);
319 ObserverNotifiedEventArgs e = new ObserverNotifiedEventArgs()
321 Representation = repr,
322 Result = (ResponseCode)result
324 ObserverNotified?.Invoke(this, e);
327 IntPtr queryHandle = IntPtr.Zero;
330 queryHandle = query._resourceQueryHandle;
333 int errCode = Interop.IoTConnectivity.Client.RemoteResource.RegisterObserve(_remoteResourceHandle, (int)policy, queryHandle, _observeCallback, IntPtr.Zero);
334 if (errCode != (int)IoTConnectivityError.None)
336 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register observe callbacks");
337 throw IoTConnectivityErrorFactory.GetException(errCode);
342 /// Stops observing on the resource
345 /// http://tizen.org/privilege/internet
347 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
348 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
349 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
350 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
351 public void StopObserving()
353 int ret = Interop.IoTConnectivity.Client.RemoteResource.DeregisterObserve(_remoteResourceHandle);
354 if (ret != (int)IoTConnectivityError.None)
356 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to deregister observe callbacks");
357 throw IoTConnectivityErrorFactory.GetException(ret);
362 /// Gets the attributes of a resource, asynchronously
365 /// http://tizen.org/privilege/internet
367 /// <param name="query">The ResourceQuery to send to server</param>
368 /// <returns>Remote response with result and representation</returns>
369 public async Task<RemoteResponse> GetAsync(ResourceQuery query = null)
371 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
373 IntPtr id = IntPtr.Zero;
374 lock (_responseCallbacksMap)
376 id = (IntPtr)_responseCallbackId++;
378 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
380 IntPtr responseCallbackId = userData;
381 lock(_responseCallbacksMap)
383 _responseCallbacksMap.Remove(responseCallbackId);
386 if (responseHandle != IntPtr.Zero)
390 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
394 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
395 tcsRemoteResponse.TrySetException(exp);
400 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
404 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
405 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Get(_remoteResourceHandle, queryHandle, _responseCallbacksMap[id], id);
406 if (errCode != (int)IoTConnectivityError.None)
408 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource attributes");
409 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
411 return await tcsRemoteResponse.Task;
415 /// Puts the representation of a resource, asynchronously.
418 /// http://tizen.org/privilege/internet
420 /// <param name="representation">Resource representation to put</param>
421 /// <param name="query">The ResourceQuery to send to server</param>
422 /// <returns>Remote response with result and representation</returns>
423 public async Task<RemoteResponse> PutAsync(Representation representation, ResourceQuery query = null)
425 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
427 IntPtr id = IntPtr.Zero;
428 lock (_responseCallbacksMap)
430 id = (IntPtr)_responseCallbackId++;
432 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
434 IntPtr responseCallbackId = userData;
435 lock (_responseCallbacksMap)
437 _responseCallbacksMap.Remove(responseCallbackId);
439 if (err == (int)(IoTConnectivityError.Iotivity))
441 RemoteResponse response = new RemoteResponse();
442 response.Result = ResponseCode.Forbidden;
443 response.Representation = null;
444 tcsRemoteResponse.TrySetResult(response);
446 else if (responseHandle != IntPtr.Zero)
450 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
452 catch (Exception exp)
454 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
455 tcsRemoteResponse.TrySetException(exp);
460 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
463 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
464 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Put(_remoteResourceHandle, representation._representationHandle, queryHandle, _responseCallbacksMap[id], id);
465 if (errCode != (int)IoTConnectivityError.None)
467 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to put resource representation");
468 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
470 return await tcsRemoteResponse.Task;
474 /// Post request on a resource, asynchronously
477 /// http://tizen.org/privilege/internet
479 /// <param name="representation">Resource representation of request</param>
480 /// <param name="query">The ResourceQuery to send to server</param>
481 /// <returns>Remote response with result and representation</returns>
482 public async Task<RemoteResponse> PostAsync(Representation representation, ResourceQuery query = null)
484 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
486 IntPtr id = IntPtr.Zero;
487 lock (_responseCallbacksMap)
489 id = (IntPtr)_responseCallbackId++;
491 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
493 IntPtr responseCallbackId = userData;
494 lock (_responseCallbacksMap)
496 _responseCallbacksMap.Remove(responseCallbackId);
498 if (responseHandle != IntPtr.Zero)
502 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
504 catch (Exception exp)
506 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
507 tcsRemoteResponse.TrySetException(exp);
512 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
515 IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle;
516 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Post(_remoteResourceHandle, representation._representationHandle, queryHandle, _responseCallbacksMap[id], id);
517 if (errCode != (int)IoTConnectivityError.None)
519 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to post request");
520 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
522 return await tcsRemoteResponse.Task;
526 /// Deletes the resource, asynchronously
529 /// http://tizen.org/privilege/internet
531 /// <returns>Remote response with result and representation</returns>
532 public async Task<RemoteResponse> DeleteAsync()
534 TaskCompletionSource<RemoteResponse> tcsRemoteResponse = new TaskCompletionSource<RemoteResponse>();
536 IntPtr id = IntPtr.Zero;
537 lock (_responseCallbacksMap)
539 id = (IntPtr)_responseCallbackId++;
541 _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) =>
543 IntPtr responseCallbackId = userData;
544 lock (_responseCallbacksMap)
546 _responseCallbacksMap.Remove(responseCallbackId);
548 if (err == (int)(IoTConnectivityError.Iotivity))
550 RemoteResponse response = new RemoteResponse();
551 response.Result = ResponseCode.Forbidden;
552 response.Representation = null;
553 tcsRemoteResponse.TrySetResult(response);
555 else if (responseHandle != IntPtr.Zero)
559 tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle));
561 catch (Exception exp)
563 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message);
564 tcsRemoteResponse.TrySetException(exp);
569 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System));
573 int errCode = Interop.IoTConnectivity.Client.RemoteResource.Delete(_remoteResourceHandle, _responseCallbacksMap[id], id);
574 if (errCode != (int)IoTConnectivityError.None)
576 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to delete");
577 tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode));
579 return await tcsRemoteResponse.Task;
583 /// Releases any unmanaged resources used by this object.
585 public void Dispose()
588 GC.SuppressFinalize(this);
591 internal static Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType GetConnectivityType(string hostAddress)
593 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None;
595 Log.Info(IoTConnectivityErrorFactory.LogTag, hostAddress);
597 if (hostAddress == IoTConnectivityClientManager.MulticastAddress)
599 type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv4;
604 string hostName = hostAddress;
605 if (hostAddress.Contains(":"))
607 string[] hostParts = hostAddress.Split(':');
608 if (hostParts.Length == 2)
610 hostName = hostParts[0];
613 if (hostAddress.Contains("%"))
615 string[] hostParts = hostAddress.Split('%');
616 if (hostParts.Length == 2)
618 hostName = hostParts[0];
621 if (hostName.Contains("["))
623 string[] hostParts = hostName.Split('[');
624 if (hostParts.Length == 2)
626 hostName = hostParts[1];
629 Log.Info(IoTConnectivityErrorFactory.LogTag, hostName);
630 if (IPAddress.TryParse(hostName, out address))
632 switch (address.AddressFamily)
634 case System.Net.Sockets.AddressFamily.InterNetwork:
635 type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv4;
637 case System.Net.Sockets.AddressFamily.InterNetworkV6:
638 type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv6;
641 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to parse for Ipv4 or Ipv6");
647 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to parse hostname " + hostName);
654 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
656 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
657 protected virtual void Dispose(bool disposing)
664 // Free managed objects
667 Interop.IoTConnectivity.Client.RemoteResource.Destroy(_remoteResourceHandle);
671 private void HandleCachePolicyChanged()
675 _cacheUpdatedCallback = (IntPtr resource, IntPtr representation, IntPtr userData) =>
679 Representation repr = null;
682 repr = new Representation(representation);
684 catch (Exception exp)
686 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new Representation: " + exp.Message);
690 CacheUpdatedEventArgs e = new CacheUpdatedEventArgs()
692 Representation = repr
694 CacheUpdated?.Invoke(this, e);
698 int ret = Interop.IoTConnectivity.Client.RemoteResource.StartCaching(_remoteResourceHandle, _cacheUpdatedCallback, IntPtr.Zero);
699 if (ret != (int)IoTConnectivityError.None)
701 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add cache updated event handler");
702 throw IoTConnectivityErrorFactory.GetException(ret);
707 int ret = Interop.IoTConnectivity.Client.RemoteResource.StopCaching(_remoteResourceHandle);
708 if (ret != (int)IoTConnectivityError.None)
710 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove cache updated event handler");
711 throw IoTConnectivityErrorFactory.GetException(ret);
716 private void RegisterStateChangedEvent()
718 _stateChangedCallback = (IntPtr resource, int state, IntPtr userData) =>
720 StateChangedEventArgs e = new StateChangedEventArgs()
722 State = (ResourceState)state
724 _stateChangedEventHandler?.Invoke(null, e);
727 int ret = Interop.IoTConnectivity.Client.RemoteResource.StartMonitoring(_remoteResourceHandle, _stateChangedCallback, IntPtr.Zero);
728 if (ret != (int)IoTConnectivityError.None)
730 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state changed event handler");
731 throw IoTConnectivityErrorFactory.GetException(ret);
735 private void UnregisterStateChangedEvent()
737 int ret = Interop.IoTConnectivity.Client.RemoteResource.StopMonitoring(_remoteResourceHandle);
738 if (ret != (int)IoTConnectivityError.None)
740 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove state changed event handler");
741 throw IoTConnectivityErrorFactory.GetException(ret);
745 private void CreateRemoteResource(IntPtr resourceTypeHandle, IntPtr resourceInterfaceHandle)
747 Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = GetConnectivityType(HostAddress);
748 if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None)
750 Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address");
751 throw new ArgumentException("Unable to parse host address");
753 int ret = Interop.IoTConnectivity.Client.RemoteResource.Create(HostAddress, (int)connectivityType, UriPath, (int)Policy, resourceTypeHandle, resourceInterfaceHandle, out _remoteResourceHandle);
754 if (ret != (int)IoTConnectivityError.None)
756 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get remote resource");
757 throw IoTConnectivityErrorFactory.GetException(ret);
761 private void SetRemoteResource()
763 IntPtr hostAddressPtr, uriPathPtr;
764 int ret = Interop.IoTConnectivity.Client.RemoteResource.GetHostAddress(_remoteResourceHandle, out hostAddressPtr);
765 if (ret != (int)IoTConnectivityError.None)
767 Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get host address");
768 throw IoTConnectivityErrorFactory.GetException(ret);
771 ret = Interop.IoTConnectivity.Client.RemoteResource.GetUriPath(_remoteResourceHandle, out uriPathPtr);
772 if (ret != (int)IoTConnectivityError.None)
774 Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get uri path");
775 throw IoTConnectivityErrorFactory.GetException(ret);
778 int policy = (int)ResourcePolicy.NoProperty;
779 ret = Interop.IoTConnectivity.Client.RemoteResource.GetPolicies(_remoteResourceHandle, out policy);
780 if (ret != (int)IoTConnectivityError.None)
782 Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get uri path");
783 throw IoTConnectivityErrorFactory.GetException(ret);
786 IntPtr typesHandle, interfacesHandle;
787 ret = Interop.IoTConnectivity.Client.RemoteResource.GetTypes(_remoteResourceHandle, out typesHandle);
788 if (ret != (int)IoTConnectivityError.None)
790 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource types");
791 throw IoTConnectivityErrorFactory.GetException(ret);
794 ret = Interop.IoTConnectivity.Client.RemoteResource.GetInterfaces(_remoteResourceHandle, out interfacesHandle);
795 if (ret != (int)IoTConnectivityError.None)
797 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource interfaces");
798 throw IoTConnectivityErrorFactory.GetException(ret);
802 ret = Interop.IoTConnectivity.Client.RemoteResource.GetDeviceId(_remoteResourceHandle, out deviceIdPtr);
803 if (ret != (int)IoTConnectivityError.None)
805 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device id");
806 throw IoTConnectivityErrorFactory.GetException(ret);
808 DeviceId = Marshal.PtrToStringAnsi(deviceIdPtr);
809 HostAddress = Marshal.PtrToStringAnsi(hostAddressPtr);
810 UriPath = Marshal.PtrToStringAnsi(uriPathPtr);
811 Types = new ResourceTypes(typesHandle);
812 Interfaces = new ResourceInterfaces(interfacesHandle);
813 Policy = (ResourcePolicy)policy;
816 private RemoteResponse GetRemoteResponse(IntPtr response)
819 IntPtr representationHandle, optionsHandle;
820 int ret = Interop.IoTConnectivity.Server.Response.GetResult(response, out result);
821 if (ret != (int)IoTConnectivityError.None)
823 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get result");
824 throw IoTConnectivityErrorFactory.GetException(ret);
827 ret = Interop.IoTConnectivity.Server.Response.GetRepresentation(response, out representationHandle);
828 if (ret != (int)IoTConnectivityError.None)
830 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get representation");
831 throw IoTConnectivityErrorFactory.GetException(ret);
834 ret = Interop.IoTConnectivity.Server.Response.GetOptions(response, out optionsHandle);
835 if (ret != (int)IoTConnectivityError.None)
837 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get options");
838 throw IoTConnectivityErrorFactory.GetException(ret);
840 return new RemoteResponse()
842 Result = (ResponseCode)result,
843 Representation = new Representation(representationHandle),
844 Options = (optionsHandle == IntPtr.Zero)? null : new ResourceOptions(optionsHandle)