codec.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (c) 2015, Nordic Semiconductor
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. #
  7. # * Redistributions of source code must retain the above copyright notice, this
  8. # list of conditions and the following disclaimer.
  9. #
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions and the following disclaimer in the documentation
  12. # and/or other materials provided with the distribution.
  13. #
  14. # * Neither the name of Nordic Semiconductor ASA nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. UART_HEADER_OCTET_COUNT = 4
  29. class ThreeWireUartPacket(object):
  30. """
  31. This class encapsulate a three wire uart packet according to Bluetooth specification
  32. version 4.0 [Vol 4] part D.
  33. """
  34. def __init__(self):
  35. self.ack = None # Acknowledgement number
  36. self.seq = None # Sequence number
  37. self.di = None # Data integrity present
  38. self.rp = None # Reliable packet
  39. self.type = None # Packet type
  40. self.length = None # Payload Length
  41. self.checksum = None # Header checksum
  42. self.payload = None # Payload
  43. @staticmethod
  44. def decode(packet):
  45. """
  46. Decodes a packet from a str encoded array
  47. :param packet_bytes: A str encoded array
  48. :return: TheeWireUartPacket
  49. """
  50. decoded_packet = ThreeWireUartPacket()
  51. packet_bytes = bytearray(packet)
  52. decoded_packet.ack = (packet_bytes[0] & int('38', 16)) >> 3
  53. decoded_packet.seq = (packet_bytes[0] & int('07', 16))
  54. decoded_packet.di = (packet_bytes[0] & int('40', 16)) >> 6
  55. decoded_packet.rp = (packet_bytes[0] & int('80', 16)) >> 7
  56. decoded_packet.type = (packet_bytes[1] & int('0F', 16))
  57. decoded_packet.length = ((packet_bytes[1] & int('F0', 16)) >> 4) + (packet_bytes[2] * 16)
  58. checksum = packet_bytes[0]
  59. checksum = checksum + packet_bytes[1]
  60. checksum = checksum + packet_bytes[2]
  61. checksum &= int('FF', 16)
  62. decoded_packet.checksum = (~checksum + 1) & int('FF', 16)
  63. if decoded_packet.length > 0:
  64. decoded_packet.payload = packet_bytes[UART_HEADER_OCTET_COUNT:-1]
  65. return decoded_packet