shellmatta_transport.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2019 - 2024 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file shellmatta_transport.h
  10. * @brief transport layer functions of shellmatta
  11. * @authors Simon Fischer <dev@s-fischer.net> Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. /**
  14. * @addtogroup shellmatta_transport
  15. * @{
  16. */
  17. #ifndef _SHELLMATTA_TRANSPORT_H_
  18. #define _SHELLMATTA_TRANSPORT_H_
  19. #include "shellmatta.h"
  20. /** @brief value of start-of-header character */
  21. #define SHELLMATTA_TRANSPORT_START_OF_HEADER 0x01u
  22. /** @brief currently supported protocol version */
  23. #define SHELLMATTA_TRANSPORT_PROTOCOL_VERSION 0x01u
  24. /** @brief broadcast address */
  25. #define SHELLMATTA_TRANSPORT_BROADCAST_ADDRESS 0u
  26. /** @brief master address */
  27. #define SHELLMATTA_TRANSPORT_MASTER_ADDRESS 1u
  28. /* header field length defines */
  29. /** @brief length of header */
  30. #define SHELLMATTA_TRANSPORT_LENGTH_HEADER ((uint8_t)(8))
  31. /** @brief maximum length of shellmatta internal packages (e.g. sequence counter packets) */
  32. #define SHELLMATTA_TRANSPORT_LENGTH_PAYLOAD_INT ((uint8_t)(32))
  33. /** @brief length of crc32 */
  34. #define SHELLMATTA_TRANSPORT_LENGTH_CRC ((uint8_t)(4))
  35. /** @brief length of crc32 */
  36. #define SHELLMATTA_TRANSPORT_LENGTH_STATIC (SHELLMATTA_TRANSPORT_LENGTH_HEADER + SHELLMATTA_TRANSPORT_LENGTH_CRC)
  37. /** @brief helper macro for CRC calculation */
  38. #define SHELLMATTA_TRANSPORT_CALC_CRC(transporLayer, data, size) NULL != (transportLayer)->customCrcFct ? \
  39. (transportLayer)->customCrcFct((data), (size)) : \
  40. crc32Calc((data), (size));
  41. /** @brief packet definition with reduced payload (for internal packets) */
  42. typedef struct __attribute__((__packed__))
  43. {
  44. shellmatta_transport_header_t header; /**< header of the packet */
  45. char payload[SHELLMATTA_TRANSPORT_LENGTH_PAYLOAD_INT]; /**< payload of the packet */
  46. uint32_t crc; /**< checksum of the packet */
  47. } shellmatta_transport_packet_int_t;
  48. /**
  49. * @brief definitions of shellmatta transport layer packet types
  50. */
  51. #define SHELLMATTA_TRANSPORT_PACKET_DATA 0x00u /**< packet type to send plain data */
  52. #define SHELLMATTA_TRANSPORT_PACKET_SEQ_CNT_REQUEST 0x01u /**< packet type to request sequence counters */
  53. #define SHELLMATTA_TRANSPORT_PACKET_SEQ_CNT_RESPOND 0x81u /**< packet type to respond with sequence counters */
  54. #define SHELLMATTA_TRANSPORT_PACKET_MAX_BUFFERSIZE_REQUEST 0x02u /**< packet type to set and request max buffersize */
  55. #define SHELLMATTA_TRANSPORT_PACKET_MAX_BUFFERSIZE_RESPOND 0x82u /**< packet type to respond with max buffersize */
  56. #define SHELLMATTA_TRANSPORT_PACKET_SEARCH_DEVICE_REQUEST 0x03u /**< UNUSED: packet type to request search for a device by unique id */
  57. #define SHELLMATTA_TRANSPORT_PACKET_SEARCH_DEVICE_RESPOND 0x83u /**< UNUSED: packet type to respond with search results */
  58. #define SHELLMATTA_TRANSPORT_PACKET_SET_ADDRESS_REQUEST 0x04u /**< UNUSED: packet type to set and request an address */
  59. #define SHELLMATTA_TRANSPORT_PACKET_SET_ADDRESS_RESPOND 0x84u /**< UNUSED: packet type to respond with a set address */
  60. shellmatta_retCode_t shellmatta_transport_init( shellmatta_transport_layer_t *transportLayer,
  61. shellmatta_write_t writeFct);
  62. shellmatta_retCode_t shellmatta_transport_process(shellmatta_transport_layer_t *transportLayer,
  63. char byte,
  64. char **data,
  65. uint32_t *length);
  66. shellmatta_retCode_t shellmatta_transport_write(shellmatta_transport_layer_t *transportLayer,
  67. const char* data,
  68. uint32_t length);
  69. #endif /* _SHELLMATTA_TRANSPORT_H_ */
  70. /** @} */