shellmatta_transport.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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.c
  10. * @brief transport layer functions of shellmatta
  11. * @author Simon Fischer <fischer.simon.1991@gmail.com>
  12. */
  13. #ifdef SHELLMATTA_TRANSPORT
  14. #include "shellmatta_transport.h"
  15. #include "shellmatta.h"
  16. #include "shellmatta_crc.h"
  17. #include "shellmatta_utils.h"
  18. #include <string.h>
  19. /**
  20. * @brief create a secured packet and pass it to the shellmatta write function
  21. *
  22. * @note The packetType, payloadLength and payload have to be preset!
  23. *
  24. * @param[in, out] transportLayer transport layer instance to work on
  25. * @param[in] packetType type of packet to send
  26. * @param[in] packet packet to send
  27. * @return errorcode
  28. */
  29. static shellmatta_retCode_t shellmatta_transport_send(shellmatta_transport_layer_t *transportLayer,
  30. shellmatta_transport_packet_t *packet)
  31. {
  32. uint32_t crc;
  33. char *rawPacket = (char*)packet;
  34. shellmatta_transport_header_t *header = &packet->header;
  35. /** -# fill header */
  36. header->startOfHeader = SHELLMATTA_TRANSPORT_START_OF_HEADER;
  37. header->protocolVersion = SHELLMATTA_TRANSPORT_PROTOCOL_VERSION;
  38. header->source = 0;
  39. header->destination = 0;
  40. header->sequenceH2S = transportLayer->sequenceH2S;
  41. header->sequenceS2H = ++transportLayer->sequenceS2H;
  42. crc = crc32Calc((char*) packet, SHELLMATTA_TRANSPORT_LENGTH_HEADER + header->payloadLength);
  43. /** -# append crc to end of payload */
  44. rawPacket[SHELLMATTA_TRANSPORT_LENGTH_HEADER + header->payloadLength] = (uint8_t)(crc >> 24u);
  45. rawPacket[SHELLMATTA_TRANSPORT_LENGTH_HEADER + header->payloadLength + 1u] = (uint8_t)(crc >> 16u);
  46. rawPacket[SHELLMATTA_TRANSPORT_LENGTH_HEADER + header->payloadLength + 2u] = (uint8_t)(crc >> 8u);
  47. rawPacket[SHELLMATTA_TRANSPORT_LENGTH_HEADER + header->payloadLength + 3u] = (uint8_t)(crc);
  48. /** -# use original write function to send full buffer */
  49. return transportLayer->writeFct((char*) rawPacket,
  50. SHELLMATTA_TRANSPORT_LENGTH_STATIC +
  51. header->payloadLength);
  52. }
  53. /**
  54. * @brief Initializes the transportLayerInst
  55. * @param[in, out] transportLayer transport layer instance to work on
  56. * @param[in] writeFct function pointer to output function
  57. * @return errorcode #SHELLMATTA_OK
  58. */
  59. shellmatta_retCode_t shellmatta_transport_init( shellmatta_transport_layer_t *transportLayer,
  60. shellmatta_write_t writeFct)
  61. {
  62. /** -# clear instance and store write function */
  63. memset(transportLayer, 0u, sizeof(shellmatta_transport_layer_t));
  64. transportLayer->writeFct = writeFct;
  65. return SHELLMATTA_OK;
  66. }
  67. /**
  68. * @brief Configures the transport layer
  69. * @param[in, out] handle shellmatta handle of the instance
  70. * @param[in] mandatory enforce using the transport layer
  71. * @param[in] disableAutoFlush enforce manual flushing of the output packet
  72. * @param[in] customCrcFct use a custom crc generation (default NULL)
  73. * @return errorcode
  74. */
  75. shellmatta_retCode_t shellmatta_transport_configure(shellmatta_handle_t handle,
  76. bool mandatory,
  77. bool disableAutoFlush,
  78. shellmatta_transport_crc_t customCrcFct)
  79. {
  80. shellmatta_retCode_t ret = SHELLMATTA_OK;
  81. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  82. /** -# check parameters for plausibility */
  83. if( (NULL != inst)
  84. && (SHELLMATTA_MAGIC == inst->magic))
  85. {
  86. shellmatta_transport_layer_t *transportLayer = &inst->transportLayer;
  87. transportLayer->mandatory = mandatory;
  88. transportLayer->disableAutoFlush = disableAutoFlush;
  89. transportLayer->customCrcFct = customCrcFct;
  90. /** -# set the transport layer active when configured as mandatory */
  91. if(true == mandatory)
  92. {
  93. transportLayer->active = true;
  94. }
  95. }
  96. else
  97. {
  98. ret = SHELLMATTA_USE_FAULT;
  99. }
  100. return ret;
  101. }
  102. /**
  103. * @brief Resets all values of tranportLayerInst except for sequence counters
  104. * @param[in, out] handle shellmatta handle of the instance
  105. * @return errorcode
  106. */
  107. shellmatta_retCode_t shellmatta_transport_reset(shellmatta_handle_t handle)
  108. {
  109. shellmatta_retCode_t ret = SHELLMATTA_OK;
  110. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  111. /** -# check parameters for plausibility */
  112. if( (NULL != inst)
  113. && (SHELLMATTA_MAGIC == inst->magic))
  114. {
  115. /** -# set transport layer back to initial state */
  116. shellmatta_transport_layer_t *transportLayer = &inst->transportLayer;
  117. transportLayer->state = SHELLMATTA_TRANSPORT_STATE_WAIT;
  118. transportLayer->active = transportLayer->mandatory;
  119. transportLayer->headerIndex = 0u;
  120. transportLayer->payloadIndex = 0u;
  121. transportLayer->crcIndex = 0u;
  122. memset(&transportLayer->inPacket, 0, sizeof(transportLayer->inPacket));
  123. memset(&transportLayer->outPacket, 0, sizeof(transportLayer->outPacket));
  124. }
  125. else
  126. {
  127. ret = SHELLMATTA_USE_FAULT;
  128. }
  129. return ret;
  130. }
  131. /**
  132. * @brief processes the passed amount of data
  133. * @param[in, out] transportLayer transport layer instance to work on
  134. * @param[in] byte byte to process
  135. * @param[out] data payload of processed packet - or manual mode
  136. * @param[out] length length of processed packet
  137. * @return errorcode #SHELLMATTA_OK data is returned\n
  138. * #SHELLMATTA_BUSY packet reception ongoing\n
  139. * #SHELLMATTA_ERROR in case of crc error
  140. */
  141. shellmatta_retCode_t shellmatta_transport_process(shellmatta_transport_layer_t *transportLayer,
  142. char byte,
  143. char **data,
  144. uint32_t *length)
  145. {
  146. shellmatta_retCode_t ret = SHELLMATTA_BUSY;
  147. char *rawPacket = (char*)&transportLayer->inPacket;
  148. shellmatta_transport_header_t *header = &transportLayer->inPacket.header;
  149. shellmatta_transport_packet_int_t intPacket;
  150. uint32_t refCrc;
  151. switch (transportLayer->state)
  152. {
  153. /** -# look for start of header */
  154. case SHELLMATTA_TRANSPORT_STATE_WAIT:
  155. /** -# if start of header is found, continue transport layer fsm */
  156. if (SHELLMATTA_TRANSPORT_START_OF_HEADER == byte)
  157. {
  158. memset(&transportLayer->inPacket, 0, sizeof(transportLayer->inPacket));
  159. transportLayer->headerIndex = 1u;
  160. transportLayer->payloadIndex = 0u;
  161. transportLayer->crcIndex = 0u;
  162. header->startOfHeader = byte;
  163. transportLayer->state = SHELLMATTA_TRANSPORT_STATE_GET_HEADER;
  164. }
  165. else if((true == transportLayer->mandatory))
  166. {
  167. /** -# if transport layer is mandatory - throw away unrecognized bytes */
  168. *data = transportLayer->inPacket.payload;
  169. *length = 0;
  170. transportLayer->active = true;
  171. ret = SHELLMATTA_OK;
  172. }
  173. else
  174. {
  175. /** -# otherwise just pass the payload as is */
  176. transportLayer->inPacket.payload[0] = byte;
  177. *data = transportLayer->inPacket.payload;
  178. *length = 1;
  179. transportLayer->active = false;
  180. ret = SHELLMATTA_OK;
  181. }
  182. break;
  183. case SHELLMATTA_TRANSPORT_STATE_GET_HEADER:
  184. rawPacket[transportLayer->headerIndex] = byte;
  185. transportLayer->headerIndex ++;
  186. if (transportLayer->headerIndex == SHELLMATTA_TRANSPORT_LENGTH_HEADER)
  187. {
  188. //TODO check for errors in header data
  189. if (0u != header->payloadLength)
  190. {
  191. transportLayer->state = SHELLMATTA_TRANSPORT_STATE_GET_PAYLOAD;
  192. }
  193. else
  194. {
  195. transportLayer->state = SHELLMATTA_TRANSPORT_STATE_GET_CRC;
  196. }
  197. }
  198. break;
  199. /** -# read payload for previously read length of payload */
  200. case SHELLMATTA_TRANSPORT_STATE_GET_PAYLOAD:
  201. transportLayer->inPacket.payload[transportLayer->payloadIndex] = byte;
  202. transportLayer->payloadIndex ++;
  203. if (transportLayer->payloadIndex >= header->payloadLength)
  204. {
  205. transportLayer->state = SHELLMATTA_TRANSPORT_STATE_GET_CRC;
  206. }
  207. break;
  208. /** -# read crc32 for four bytes */
  209. case SHELLMATTA_TRANSPORT_STATE_GET_CRC:
  210. transportLayer->crcIndex ++;
  211. transportLayer->inPacket.crc |= ((uint8_t)byte << ((SHELLMATTA_TRANSPORT_LENGTH_CRC - transportLayer->crcIndex) * 8u));
  212. if (transportLayer->crcIndex >= SHELLMATTA_TRANSPORT_LENGTH_CRC)
  213. {
  214. refCrc = SHELLMATTA_TRANSPORT_CALC_CRC(transportLayer,
  215. rawPacket,
  216. SHELLMATTA_TRANSPORT_LENGTH_HEADER + header->payloadLength);
  217. /** -# if crc is correct, further handling of data depends on type of packet */
  218. if (transportLayer->inPacket.crc == refCrc)
  219. {
  220. transportLayer->active = true;
  221. /* crc is correct */
  222. transportLayer->sequenceH2S++;
  223. switch (header->packetType)
  224. {
  225. case SHELLMATTA_TRANSPORT_PACKET_DATA:
  226. /** -# return pointer to payload and length */
  227. *data = (char*)&transportLayer->inPacket.payload;
  228. *length = header->payloadLength;
  229. ret = SHELLMATTA_OK;
  230. break;
  231. case SHELLMATTA_TRANSPORT_PACKET_SEQ_CNT_REQUEST:
  232. /** -# send out packet with no payload */
  233. intPacket.header.packetType = SHELLMATTA_TRANSPORT_PACKET_SEQ_CNT_RESPOND;
  234. intPacket.header.payloadLength = 0u;
  235. (void)shellmatta_transport_send(transportLayer, (shellmatta_transport_packet_t *)&intPacket);
  236. break;
  237. case SHELLMATTA_TRANSPORT_PACKET_SEQ_CNT_RESPOND:
  238. /** -# ignore #SHELLMATTA_TRANSPORT_PACKET_SEQ_CNT_RESPOND - nothing to do */
  239. break;
  240. case SHELLMATTA_TRANSPORT_PACKET_MAX_BUFFERSIZE_REQUEST:
  241. /** @todo implement */
  242. break;
  243. case SHELLMATTA_TRANSPORT_PACKET_MAX_BUFFERSIZE_RESPOND:
  244. /** -# ignore #SHELLMATTA_TRANSPORT_PACKET_MAX_BUFFERSIZE_RESPOND - nothing to do */
  245. break;
  246. case SHELLMATTA_TRANSPORT_PACKET_SEARCH_DEVICE_REQUEST:
  247. /** @todo implement */
  248. break;
  249. case SHELLMATTA_TRANSPORT_PACKET_SEARCH_DEVICE_RESPOND:
  250. /** -# ignore #SHELLMATTA_TRANSPORT_PACKET_SEARCH_DEVICE_RESPOND - nothing to do */
  251. break;
  252. case SHELLMATTA_TRANSPORT_PACKET_SET_ADDRESS_REQUEST:
  253. /** @todo implement */
  254. break;
  255. case SHELLMATTA_TRANSPORT_PACKET_SET_ADDRESS_RESPOND:
  256. /** -# ignore #SHELLMATTA_TRANSPORT_PACKET_SET_ADDRESS_RESPOND - nothing to do */
  257. break;
  258. default:
  259. /** -# undo sequence counter increment on wrong packet type */
  260. transportLayer->sequenceH2S--;
  261. break;
  262. }
  263. }
  264. else
  265. {
  266. /** -# return error if crc is incorrect */
  267. ret = SHELLMATTA_ERROR;
  268. }
  269. /** -# reset state machine */
  270. transportLayer->state = SHELLMATTA_TRANSPORT_STATE_WAIT;
  271. }
  272. break;
  273. default:
  274. break;
  275. }
  276. return ret;
  277. }
  278. /**
  279. * @brief Wrapper function for the write-function of shellmatta handle
  280. *
  281. * This function is used to transmit data with the tranport layer protocol.\n
  282. * The input data is copied into a new array along with the header and crc32.\n
  283. * The resulting buffer is the forwarded to the original write function.\n
  284. *
  285. * @note If length of data exceeds #UINT8_MAX, data is sent in multiple packets.\n
  286. *
  287. * @param[in, out] transportLayer transport layer instance to work on
  288. * @param[in] data pointer to input data to process
  289. * @param[in] length length of data to process
  290. * @return errorcode #SHELLMATTA_OK
  291. */
  292. shellmatta_retCode_t shellmatta_transport_write(shellmatta_transport_layer_t *transportLayer,
  293. const char* data,
  294. uint32_t length)
  295. {
  296. shellmatta_retCode_t ret = SHELLMATTA_OK;
  297. uint32_t outPayloadLength = length;
  298. shellmatta_transport_packet_t *packet = &transportLayer->outPacket;
  299. shellmatta_transport_header_t *header = &transportLayer->outPacket.header;
  300. /** -# handle data with length bigger than max length */
  301. uint32_t processedLength = 0u;
  302. uint32_t piledUpPayload;
  303. uint32_t splitLength;
  304. uint32_t restOfPayload;
  305. /** -# foot-controlled loop to send packets without payload length */
  306. do
  307. {
  308. piledUpPayload = header->payloadLength;
  309. /* compute length of next payload split */
  310. restOfPayload = (outPayloadLength - processedLength);
  311. if (restOfPayload > (SHELLMATTA_TRANPORT_PAYLOAD_MAXLENGTH - piledUpPayload))
  312. {
  313. splitLength = SHELLMATTA_TRANPORT_PAYLOAD_MAXLENGTH - piledUpPayload;
  314. }
  315. else
  316. {
  317. splitLength = restOfPayload;
  318. }
  319. (void)memcpy(&packet->payload[piledUpPayload], &data[processedLength], splitLength);
  320. header->payloadLength += splitLength;
  321. processedLength += splitLength;
  322. if(header->payloadLength >= SHELLMATTA_TRANPORT_PAYLOAD_MAXLENGTH)
  323. {
  324. /** -# packet is full - send */
  325. header->packetType = SHELLMATTA_TRANSPORT_PACKET_DATA;
  326. ret = shellmatta_transport_send(transportLayer, packet);
  327. header->payloadLength = 0u;
  328. }
  329. }
  330. while (processedLength < outPayloadLength);
  331. return ret;
  332. }
  333. /**
  334. * @brief Send out piled up payload
  335. *
  336. * @param[in, out] handle shellmatta handle of the instance
  337. * @return errorcode
  338. */
  339. shellmatta_retCode_t shellmatta_transport_flush(shellmatta_handle_t handle)
  340. {
  341. shellmatta_retCode_t ret = SHELLMATTA_OK;
  342. shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
  343. /** -# check parameters for plausibility */
  344. if( (NULL != inst)
  345. && (SHELLMATTA_MAGIC == inst->magic))
  346. {
  347. shellmatta_transport_packet_t *packet = &inst->transportLayer.outPacket;
  348. if(0 != packet->header.payloadLength)
  349. {
  350. packet->header.packetType = SHELLMATTA_TRANSPORT_PACKET_DATA;
  351. ret = shellmatta_transport_send(&inst->transportLayer, packet);
  352. packet->header.payloadLength = 0u;
  353. }
  354. }
  355. else
  356. {
  357. ret = SHELLMATTA_USE_FAULT;
  358. }
  359. return ret;
  360. }
  361. #endif