Search Unity

Question CRC8 of the custom HID input

Discussion in 'Input System' started by szamilMST, Jan 25, 2023.

  1. szamilMST

    szamilMST

    Joined:
    Mar 2, 2020
    Posts:
    12
    Where would be the best place to validate a CRC8 sum of the HID response?

    I Have a `HidController` script that reads values from custom HID as well as binds `InputActions` to events. These events should not be fired in case of wrong HID response (wrong CRC8).
     
  2. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    116
    Implement
    IInputStateCallbackReceiver
    . Inspect the event when you get it in
    OnStateEvent
    , and don't commit it via
    InputState.Change
    if the CRC is wrong.
     
    szamilMST likes this.
  3. szamilMST

    szamilMST

    Joined:
    Mar 2, 2020
    Posts:
    12
    Thanks for the reply! It looks like the correct approach, but I still need a help: How to get the bytes of the response from the
    InputEventPtr
    ? Should I use unsafe mode?
     
  4. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    116
    Most likely yes. If you have a struct that defines the state for your devices, you can get it from the event like this

    Code (CSharp):
    1. var state = StateEvent.GetState<MyStateStruct>(eventPtr);
    But for computing a CRC this will likely not be of much use.
    StateEvent.From
    (or
    FromUnchecked
    if you already know it's a state event) gives you a
    StateEvent
    . From there you get to the state via the
    state
    property.

    You can compute a CRC directly from that memory or, if you need a
    byte[]
    array for a managed method, set aside a buffer you use over and over and just copy into.

    Code (CSharp):
    1. private byte[] m_Buffer;
    2.  
    3. public unsafe void OnStateEvent(InputEventPtr eventPtr)
    4. {
    5.     var stateEventPtr = StateEvent.FromUnchecked(eventPtr);
    6.     var stateSize = (int)stateEventPtr->stateSizeInBytes;
    7.     if (m_Buffer == null)
    8.         m_Buffer = new byte[stateSize];
    9.     fixed (byte* bufferPtr = m_Buffer)
    10.         UnsafeUtility.Memcpy(bufferPtr, stateEventPtr->state, stateSize);
    11.     var crc = MyCRCMethod(m_Buffer);
     
  5. szamilMST

    szamilMST

    Joined:
    Mar 2, 2020
    Posts:
    12
    Thanks a lot!

    I did this (no unsafe!):
    Code (CSharp):
    1.  
    2.  public void OnStateEvent(InputEventPtr eventPtr)
    3.     {
    4.         var o = (byte[])this.ReadValueFromEventAsObject(eventPtr);
    5.         var crc = Crc8.Calculate(o);
    6.         if (crc == 0)
    7.             InputState.Change(this, eventPtr);
    8.         else
    9.             Debug.LogWarning("CRC Failed!");
    10.     }
    11.  
    And I am it testing now.

    But
    Code (CSharp):
    1.  
    2. var state = StateEvent.GetState<MyStateStruct>(eventPtr);
    3.  
    is what I'll try in a second as well.
     
  6. szamilMST

    szamilMST

    Joined:
    Mar 2, 2020
    Posts:
    12
    Ok, it seems to work! :)
    Last question: what about the `GetStateOffsetForEvent` ? I'm not sure what should I do here (if anything).
     
  7. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    116
    Simply set offset to 0 and return true.
     
    szamilMST likes this.
  8. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    116
    One thing I just caught, note that you also need to commit the new state yourself via
    InputState.Change
    .
    OnStateEvent
    basically says "if there's a state event for me, don't handle it; I'll handle it myself".
     
  9. szamilMST

    szamilMST

    Joined:
    Mar 2, 2020
    Posts:
    12
    Sorry, you lost me. Is it enough that I call
    InputState.Change
    for valid CRC ?
     
  10. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    116
    Pseuo-logic

    Code (CSharp):
    1. void OnStateEvent(eventPtr)
    2. {
    3.     if (CRCCheckSucceeds(eventPtr))
    4.         InputState.Change(this, eventPtr);
    5. }
     
  11. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    116
    Doh, oversight on my end. Ignore me. You're good.
     
    szamilMST likes this.