[Peripheral] Gpio callback return pin value from structure. (#2798)
authorxerrni <e.borowski@samsung.com>
Wed, 28 Apr 2021 05:46:17 +0000 (07:46 +0200)
committerGitHub <noreply@github.com>
Wed, 28 Apr 2021 05:46:17 +0000 (14:46 +0900)
Change implementation so interrupted callback reads pin value from
structure rather than invoking Read() method.

Signed-off-by: Ernest Borowski <e.borowski@samsung.com>
Co-authored-by: WonYoung Choi <wy80.choi@samsung.com>
internals/src/Tizen.Peripheral/Interop/Gpio.cs
internals/src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs

index 5c7d688..f63e447 100644 (file)
@@ -38,6 +38,20 @@ internal static partial class Interop
                 Falling,
                 Both
             }
+            [StructLayout(LayoutKind.Sequential)]
+            public struct callbackInfo
+            {
+                public int vermagic;
+                public int gpioPinValue;
+                // skip the rest fields in structure as we do not need it
+            }
+            [StructLayout(LayoutKind.Sequential)]
+            public struct PeripherialGpio
+            {
+                public int vermagic;
+                public callbackInfo cbInfo;
+                // skip the rest fields in structure as we do not need it
+            }
 
             [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
             internal delegate void InterruptedEventCallback(IntPtr handle, ErrorCode error, IntPtr data);
@@ -67,4 +81,4 @@ internal static partial class Interop
             internal static extern ErrorCode UnsetInterruptedCb(IntPtr handle);
         }
     }
-}
\ No newline at end of file
+}
index 32b33ec..239769f 100644 (file)
@@ -168,7 +168,18 @@ namespace Tizen.Peripheral.Gpio
 
         private void OnInterrupted(IntPtr handle, ErrorCode error, IntPtr data)
         {
-            ValueChanged?.Invoke(this, new PinUpdatedEventArgs(PinNumber, Read()));
+            NativeGpio.PeripherialGpio pio = (NativeGpio.PeripherialGpio)
+                                            System.Runtime.InteropServices.Marshal.PtrToStructure(handle,
+                                                typeof(NativeGpio.PeripherialGpio));
+            // check magic values to verify capi structures integrity
+            if (pio.vermagic != 13712 || pio.cbInfo.vermagic != 14469)
+            {
+                Log.Error("Peripheral",
+                        "Unable to parse gpio structure in callback - vermagic is wrong");
+                return;
+            }
+            ValueChanged?.Invoke(this, new PinUpdatedEventArgs(PinNumber, pio.cbInfo.gpioPinValue == 1 ?
+                                                                GpioPinValue.High : GpioPinValue.Low));
         }
 
         /// <summary>