doIP.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2007-2011 Siemens AG
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*******************************************************************
  18. *
  19. * @author Sebastian.Kaebisch.EXT@siemens.com
  20. * @version 0.4
  21. * @contact Joerg.Heuer@siemens.com
  22. *
  23. ********************************************************************/
  24. /*
  25. * This file implements the DoIP header writer and reader.
  26. * Note: Not all functions are implemented yet.
  27. *
  28. */
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #include <stdio.h>
  33. #include <math.h>
  34. #include <string.h>
  35. #include "doIP.h"
  36. int write_doIPNack(uint8_t* outStream, uint16_t* outStreamLength, uint8_t nackCode)
  37. {
  38. /* if(outStreamLength<DOIP_HEADER_LENGTH+1)
  39. return -1;*/
  40. /* payload length=1 and nack payload type*/
  41. *outStreamLength = 1;
  42. write_doIPHeader(outStream,outStreamLength, DOIP_NEGATIVE_ACKNOWLEDGE);
  43. outStream[8]=nackCode;
  44. return 0;
  45. }
  46. int write_doIPHeader(uint8_t* outStream, uint16_t* outStreamLength, uint16_t payloadType)
  47. {
  48. /* write DoIP version number 1=byte */
  49. outStream[0]=DOIP_VERSION;
  50. /* write inverse DoIP version */
  51. outStream[1]=DOIP_VERSION_INV;
  52. /* write payload type */
  53. outStream[2] = (uint8_t)(payloadType & 0xFF);
  54. outStream[3] = (uint8_t)(payloadType >> 8 & 0xFF);
  55. /* write payload length */
  56. outStream[4] = (uint8_t)(*outStreamLength & 0xFF);
  57. outStream[5] = (uint8_t)(*outStreamLength>>8 & 0xFF);
  58. outStream[6] = (uint8_t)(*outStreamLength>>16 & 0xFF);
  59. outStream[7] = (uint8_t)(*outStreamLength>>24 & 0xFF);
  60. /* here, the outStream length have to be resized by the DoIP offset*/
  61. *outStreamLength += DOIP_HEADER_LENGTH;
  62. return 0;
  63. }
  64. int read_doIPHeader(uint8_t* inStream, uint16_t inStreamLength, uint16_t* payloadLength)
  65. {
  66. uint16_t payloadType=0;
  67. /* check, if we support this DoIP version */
  68. if(inStream[0]!=DOIP_VERSION && inStream[1]!=DOIP_VERSION_INV)
  69. return DOIP_INCORRECT_PATTERN_FORMAT;
  70. /* check, if we support this payload type*/
  71. payloadType = inStream[3];
  72. payloadType = (payloadType << 8 | inStream[2]);
  73. if(payloadType != DOIP_EXI_TYPE && payloadType != DOIP_NEGATIVE_ACKNOWLEDGE)
  74. return DOIP_UNKNOWN_PAYLOAD_TYPE;
  75. /* determine payload length*/
  76. *payloadLength = inStream[7];
  77. *payloadLength = (*payloadLength << 8 | inStream[6]);
  78. *payloadLength = (*payloadLength << 16 | inStream[5]);
  79. *payloadLength = (*payloadLength << 24 | inStream[4]);
  80. if((*payloadLength+DOIP_HEADER_LENGTH)!=inStreamLength)
  81. return DOIP_INVALID_PAYLOAD_LENGTH;
  82. /* if payload is a NACK read its NACK code and return it*/
  83. /* TODO: should be handled by an extra method */
  84. if(payloadType == DOIP_NEGATIVE_ACKNOWLEDGE)
  85. return inStream[8];
  86. return 0;
  87. }