Exemple #1
0
        private OCEntityHandlerResult OCEntityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest entityHandlerRequest, IntPtr callbackParam)
        {
            OCEntityHandlerResult result         = OCEntityHandlerResult.OC_EH_OK;
            RepPayload            payload        = null;
            RepPayload            requestPayload = entityHandlerRequest.payload == IntPtr.Zero ? null : new RepPayload(entityHandlerRequest.payload);

            if (entityHandlerRequest != null && (flag.HasFlag(OCEntityHandlerFlag.OC_REQUEST_FLAG)))
            {
                switch (entityHandlerRequest.method)
                {
                case OCMethod.OC_REST_GET:
                {
                    var rpayload = payload = new RepPayload();
                    rpayload.SetUri(_uri);
                    foreach (var resource in _resourceProperties)
                    {
                        if (requestPayload != null && !requestPayload.Types.Contains(resource.Key))
                        {
                            continue;
                        }
                        var repayload = new RepPayload(resource.Value);
                        repayload.Types.Add(resource.Key);
                        rpayload.Next = repayload;
                        rpayload      = repayload;
                    }
                    payload = rpayload;
                }
                break;

                case OCMethod.OC_REST_POST:
                case OCMethod.OC_REST_PUT:
                {
                    var p = new RepPayload(entityHandlerRequest.payload);
                    result = OnPropertyUpdated(p);
                    PropertyUpdated?.Invoke(this, p);
                }
                break;

                default:
                    result = OCEntityHandlerResult.OC_EH_METHOD_NOT_ALLOWED;
                    break;
                }
                var response = new OCEntityHandlerResponse();
                response.requestHandle  = entityHandlerRequest.requestHandle;
                response.resourceHandle = entityHandlerRequest.resource;
                response.ehResult       = result;
                response.payload        = payload == null ? IntPtr.Zero : payload.AsOCRepPayload();
                response.numSendVendorSpecificHeaderOptions = 0;
                response.sendVendorSpecificHeaderOptions    = IntPtr.Zero;
                response.resourceUri          = string.Empty;
                response.persistentBufferFlag = 0;
                OCStack.OCDoResponse(response);
            }
            return(result);
        }
        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);
        }
        internal RepPayload(IntPtr handle) : this()
        {
            var ocpayload = Marshal.PtrToStructure <OCRepPayload>(handle);

            if (ocpayload.interfaces != IntPtr.Zero)
            {
                var resource = Marshal.PtrToStructure <OCStringLL>(ocpayload.interfaces);
                Interfaces = resource.Values.ToArray();
            }
            if (ocpayload.types != IntPtr.Zero)
            {
                var resource = Marshal.PtrToStructure <OCStringLL>(ocpayload.types);
                Types = resource.Values.ToArray();
            }
            if (ocpayload.next != IntPtr.Zero)
            {
                Next = new RepPayload(ocpayload.next);
            }
            Values = new IotivityValueDictionary(ocpayload);
        }
Exemple #4
0
 protected virtual OCEntityHandlerResult OnPropertyUpdated(RepPayload payload)
 {
     return(OCEntityHandlerResult.OC_EH_OK);
 }
 internal ResourceObservationEventArgs(DeviceAddress deviceAddress, string resourceUri, RepPayload payload)
 {
     DeviceAddress = deviceAddress;
     ResourceUri   = resourceUri;
     Payload       = payload;
 }