Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to understand the checksum calculation #18

Closed
TerminalMan opened this issue Apr 5, 2023 · 1 comment
Closed

Trying to understand the checksum calculation #18

TerminalMan opened this issue Apr 5, 2023 · 1 comment

Comments

@TerminalMan
Copy link

TerminalMan commented Apr 5, 2023

Hi, I hope it's ok to ask this question via an 'issue', not sure where else I could ask :)

I'm trying to understand how the checksum calculation works.

I did find the section in your code where i believe it is being calculated. But I can't replicate the checksums given in your api documentation, which must be correct because the commands work.

from your code:

  # add payload and calculate checksum
  set sum 1
  foreach b $bytes {
    lappend full_req $b
    set sum [ expr $sum + [ scan $b %d ] ]
  }
  set sum [ expr $sum & 255 ]
  lappend full_req $sum

Given this example from your documentation
char-write-cmd 0x2b 0f0c170001010203040000000018ffff

My understanding is I have to take the part 0c1700010102030400000000 into account for the checksum calculation

I then loop over the individual bytes and add them to chksm and at the end I have to & 0xFF them, which should net me 0x18, however I'm getting 0x2F, with the following python example

data = bytearray.fromhex('0c1700010102030400000000')
chksm = 1
for d in data:
    chksm += d
print( hex(chksm & 0xff) )

Would be great if you could shine some light on this. I've looked at multiple repos and I can't figure it out :/

edit: After some more attempts: I haven't actually encountered a command that doesn't work when I calculate the checksum excl. the length byte, aside from some examples in your api documentation (e.g. the one above). My guess atm. is that in those cases the checksum is simply not correct in the documentation :?

@Heckie75
Copy link
Owner

Heckie75 commented Jan 14, 2024

@TerminalMan
Sorry for late response.

At least the documentation is wrong. According the code the byte for length is not included

Lines 1459ff

# add payload and calculate checksum
  set sum 1
  foreach b $bytes { # iterates over bytes which is the payload w/o static prefix 0x0f  and w/o length!
    lappend full_req $b
    set sum [ expr $sum + [ scan $b %d ] ]
  }
  set sum [ expr $sum & 255 ]
  lappend full_req $sum

  # static suffix 0xffff
  lappend full_req 255
  lappend full_req 255

  return  [ gatt_char_write_req $HANDLES(command) $full_req ]

}

Your python code seems to be correct.

I've double checked this example from debug mode:

[FC:69:47:06:CB:C6][LE]> char-write-cmd 002b 0f0c01000b0d0a0e0107e8000022ffff
Notification handle = 0x002e value: 0f 04 01 00 00 02 ff ff

Checksum is 0x22

this python code also calculates checksum of 0x22

ba = bytearray.fromhex('01000b0d0a0e0107e80000')
sum = 1
for b in ba:
  sum += b

print(hex(sum & 0xff))

In terms of the notification the length-byte is included.

Heckie75 added a commit that referenced this issue Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants