Example #1
0
        public void AddResourceType(string resourceTypeName, IDictionary <string, object> properties)
        {
            OCStackResult result = OCStack.OCBindResourceTypeToResource(_handle, resourceTypeName);

            OCStackException.ThrowIfError(result, "Failed to add resource type");
            _resourceProperties.Add(resourceTypeName, new IotivityValueDictionary(properties));
        }
 public void Stop()
 {
     isRunning = false;
     if (handle != IntPtr.Zero)
     {
         var ret = OCStack.OCCancel(handle, OCQualityOfService.OC_LOW_QOS, null, 0);
         OCStackException.ThrowIfError(ret);
         handle = IntPtr.Zero;
     }
 }
Example #3
0
        public static async Task Shutdown()
        {
            if (tcs == null)
            {
                return;
            }
            ct.Cancel();
            await tcs.Task;
            var result = OCStack.OCStop();

            OCStackException.ThrowIfError(result);
        }
Example #4
0
        public DeviceResource(string uri, string resourceTypeName, IDictionary <string, object> properties, string resourceInterfaceName = IotivityDotNet.Interop.Defines.OC_RSRVD_INTERFACE_DEFAULT)
        {
            _resourceCallback = this.OCEntityHandler;
            OCStackResult result = OCStack.OCCreateResource(out _handle, resourceTypeName, resourceInterfaceName, uri, _resourceCallback, IntPtr.Zero, OCResourceProperty.OC_DISCOVERABLE | OCResourceProperty.OC_OBSERVABLE | OCResourceProperty.OC_SECURE);

            OCStackException.ThrowIfError(result, "Failed to create resource");
            _uri = uri;
            _resourceProperties = new Dictionary <string, IotivityValueDictionary>();
            if (properties != null)
            {
                _resourceProperties.Add(resourceTypeName, new IotivityValueDictionary(properties));
            }
        }
Example #5
0
        //public static byte[] DeviceId
        //{
        //    get
        //    {
        //        OCUUIdentity identity = null;
        //        var result = OCStack.OCGetDeviceId(out identity);
        //        if (result != OCStackResult.OC_STACK_OK)
        //        {
        //            throw new Exception(result.ToString());
        //        }
        //        return identity.deviceId;
        //    }
        //    set
        //    {
        //        if (value == null || value.Length > 16)
        //        {
        //            throw new ArgumentException(nameof(value));
        //        }
        //        var result = OCStack.OCSetDeviceId(new OCUUIdentity() { deviceId = value });
        //        if (result != OCStackResult.OC_STACK_OK)
        //        {
        //            throw new Exception(result.ToString());
        //        }
        //    }
        //}

        /// <summary>
        /// This function sets device information.
        /// Upon call to OCInit, the default Device Type (i.e. "rt") has already been set to the default
        /// Device Type "oic.wk.d". You do not have to specify "oic.wk.d" in the OCDeviceInfo.types linked
        /// list. The default Device Type is mandatory and always specified by this Device as the first
        /// Device Type.
        /// </summary>
        public static void SetDeviceInfo(string deviceName, IEnumerable <string> types, string specVersion, IEnumerable <string> dataModelVersions)
        {
            IntPtr octypes             = OCStringLL.Create(types);
            IntPtr ocdataModelVersions = OCStringLL.Create(dataModelVersions);
            var    info = new OCDeviceInfo()
            {
                deviceName        = deviceName,
                types             = octypes,
                specVersion       = specVersion,
                dataModelVersions = ocdataModelVersions
            };
            var result = OCStack.OCSetDeviceInfo(info);

            OCStackException.ThrowIfError(result);
        }
        public async void Start()
        {
            isRunning = true;
            if (handle != IntPtr.Zero)
            {
                return;
            }
            while (isRunning)
            {
                var ret = OCStack.OCDoResource(out handle, OCMethod.OC_REST_DISCOVER, requestUri, null, IntPtr.Zero, OCConnectivityType.CT_DEFAULT, OCQualityOfService.OC_LOW_QOS, cbData, null, 0);
                OCStackException.ThrowIfError(ret);
                await Task.Delay(5000);

                ret    = OCStack.OCCancel(handle, OCQualityOfService.OC_LOW_QOS, null, 0);
                handle = IntPtr.Zero;
            }
        }
        private async Task <ClientResponse <Payload> > SendAsync(string resourceTypeName, Dictionary <string, object> data, OCMethod method)
        {
            var tcs          = new TaskCompletionSource <ClientResponse <Payload> >();
            var callbackData = new OCCallbackData();
            OCClientResponseHandler handler = (context, handle, clientResponse) =>
            {
                GCHandle.FromIntPtr(context).Free();
                if (clientResponse.result > OCStackResult.OC_STACK_RESOURCE_CHANGED)
                {
                    var err = OCStackException.CreateException(clientResponse.result, "Resource returned error");
                    tcs.SetException(err);
                }
                else
                {
                    tcs.SetResult(new ClientResponse <Payload>(clientResponse));
                }
                return(OCStackApplicationResult.OC_STACK_DELETE_TRANSACTION);
            };
            var gcHandle = GCHandle.Alloc(handler);

            callbackData.cb      = handler;
            callbackData.context = GCHandle.ToIntPtr(gcHandle);

            IntPtr payloadHandle = IntPtr.Zero;

            if (resourceTypeName != null)
            {
                RepPayload payload = new RepPayload(data);

                payload.SetUri(_resourceUri);
                payload.Types.Add(resourceTypeName);
                payloadHandle = payload.AsOCRepPayload();
            }

            var result = OCStack.OCDoRequest(out _handle, method, _resourceUri, _address.OCDevAddr, payloadHandle, OCConnectivityType.CT_DEFAULT, OCQualityOfService.OC_LOW_QOS, callbackData, null, 0);

            if (payloadHandle != IntPtr.Zero)
            {
                OCPayloadInterop.OCPayloadDestroy(payloadHandle);
            }
            OCStackException.ThrowIfError(result, "Failed to send to resource");
            var response = await tcs.Task.ConfigureAwait(false);

            return(response);
        }
Example #8
0
        internal static void ThrowIfError(OCStackResult result, string message = null)
        {
            if (result == OCStackResult.OC_STACK_OK)
            {
                return;
            }
            Exception exception = new OCStackException(result, message);

            if (result == OCStackResult.OC_STACK_INVALID_PARAM)
            {
                exception = new ArgumentException(message, exception);
            }
            else if (result == OCStackResult.OC_STACK_INVALID_METHOD)
            {
                exception = new InvalidOperationException(message, exception);
            }
            else if (result == OCStackResult.OC_STACK_UNAUTHORIZED_REQ)
            {
                exception = new UnauthorizedAccessException(message, exception);
            }
            throw exception;
        }
Example #9
0
        public static void Initialize(ServiceMode mode, string dataPath = "", string securityDBDatFilename = null)
        {
            storageHandler = new StorageHandler(dataPath, securityDBDatFilename);
            storageHandle  = GCHandle.Alloc(storageHandler);
            var fileresult = OCStack.OCRegisterPersistentStorageHandler(storageHandler.Handle);

            OCStackException.ThrowIfError(fileresult, "Failed to create storage handler");

            //var result = OCStack.OCInit(null, 0, (OCMode)mode);
            var result = OCStack.OCInit2((OCMode)mode, OCTransportFlags.OC_DEFAULT_FLAGS, OCTransportFlags.OC_DEFAULT_FLAGS, OCTransportAdapter.OC_ADAPTER_IP);

            // result = OCStack.OCInit("0.0.0.0", 0, (OCMode)mode);
            OCStackException.ThrowIfError(result);
            globalHandler = OCDefaultDeviceEntityHandler;
            GC.KeepAlive(storageHandler);
            result = OCStack.OCSetDefaultDeviceEntityHandler(globalHandler, IntPtr.Zero);
            OCStackException.ThrowIfError(result, "Failed to send to resource");


            ct  = new CancellationTokenSource();
            tcs = new TaskCompletionSource <object>();

            Task.Run(async() =>
            {
                while (!ct.IsCancellationRequested)
                {
                    var result2 = OCStack.OCProcess();
                    if (result2 != OCStackResult.OC_STACK_OK)
                    {
                        // tcs.SetException(new Exception("OCStackException on Process: " + result2.ToString()));
                        // break;
                    }
                    await Task.Delay(1);
                }
                tcs.SetResult(true);
                tcs = null;
                ct  = null;
            });
        }
Example #10
0
        protected void BindInterface(string resourceInterfaceName)
        {
            OCStackResult result = OCStack.OCBindResourceInterfaceToResource(_handle, resourceInterfaceName);

            OCStackException.ThrowIfError(result, "Failed to bind interface");
        }