diff --git a/src/transport/doIP.c b/src/transport/doIP.c new file mode 100644 index 0000000..b977519 --- /dev/null +++ b/src/transport/doIP.c @@ -0,0 +1,117 @@ + +/* + * Copyright (C) 2007-2010 Siemens AG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +/******************************************************************* + * + * @author Sebastian.Kaebisch.EXT@siemens.com + * @version 0.3.1 + * @contact Joerg.Heuer@siemens.com + * + ********************************************************************/ + +/* + * This file implements the DoIP header writer and reader. + * Note: Not all functions are implemented yet. + * + */ + +#include +#include +#include +#include +#include +#include + +#include "doIP.h" + +int write_doIPNack(uint8_t* outStream, uint16_t* outStreamLength, uint8_t nackCode) +{ +/* if(outStreamLength> 8 & 0xFF); + + /* write payload length */ + outStream[4] = (uint8_t)(*outStreamLength & 0xFF); + outStream[5] = (uint8_t)(*outStreamLength>>8 & 0xFF); + outStream[6] = (uint8_t)(*outStreamLength>>16 & 0xFF); + outStream[7] = (uint8_t)(*outStreamLength>>24 & 0xFF); + + /* here, the outStream length have to be resized by the DoIP offset*/ + *outStreamLength += DOIP_HEADER_LENGTH; + + return 0; +} + +int read_doIPHeader(uint8_t* inStream, uint16_t inStreamLength, uint16_t* payloadLength) +{ + uint16_t payloadType=0; + + + /* check, if we support this DoIP version */ + if(inStream[0]!=DOIP_VERSION && inStream[1]!=DOIP_VERSION_INV) + return DOIP_INCORRECT_PATTERN_FORMAT; + + + /* check, if we support this payload type*/ + payloadType = inStream[3]; + payloadType = (payloadType << 8 | inStream[2]); + + if(payloadType != DOIP_EXI_TYPE && payloadType != DOIP_NEGATIVE_ACKNOWLEDGE) + return DOIP_UNKNOWN_PAYLOAD_TYPE; + + + /* determine payload length*/ + *payloadLength = inStream[7]; + *payloadLength = (*payloadLength << 8 | inStream[6]); + *payloadLength = (*payloadLength << 16 | inStream[5]); + *payloadLength = (*payloadLength << 24 | inStream[4]); + + if((*payloadLength+DOIP_HEADER_LENGTH)!=inStreamLength) + return DOIP_INVALID_PAYLOAD_LENGTH; + + /* if payload is a NACK read its NACK code and return it*/ + /* TODO: should be handled by an extra method */ + if(payloadType == DOIP_NEGATIVE_ACKNOWLEDGE) + return inStream[8]; + + + return 0; +} + diff --git a/src/transport/doIP.h b/src/transport/doIP.h new file mode 100644 index 0000000..59fb980 --- /dev/null +++ b/src/transport/doIP.h @@ -0,0 +1,68 @@ + +/* + * Copyright (C) 2007-2010 Siemens AG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +/******************************************************************* + * + * @author Sebastian.Kaebisch.EXT@siemens.com + * @version 0.3.1 + * @contact Joerg.Heuer@siemens.com + * + ********************************************************************/ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef DOIP_H_ +#define DOIP_H_ + +/* generic DoIP header length */ +#define DOIP_HEADER_LENGTH 8 + +/* define DoIP Version */ +#define DOIP_VERSION 0x01 +#define DOIP_VERSION_INV 0xFE + +/* define DoIP payload types*/ +#define DOIP_NEGATIVE_ACKNOWLEDGE 0x0000 +#define DOIP_EXI_TYPE 0xC350 /* == 50000 */ + + +/* Generic DoIP Header NACK codes */ +#define DOIP_INCORRECT_PATTERN_FORMAT 0x00 +#define DOIP_UNKNOWN_PAYLOAD_TYPE 0x01 +#define DOIP_MESSAGE_TOO_LARGE 0x02 +#define DOIP_OUT_OF_MEMORY 0x03 +#define DOIP_INVALID_PAYLOAD_LENGTH 0x04 + +/* EXI NACK code */ +#define DOIP_INVALID_EXI_MESSAGE 0x05 + + +/* DoIP methods */ +int write_doIPNack(uint8_t* outStream, uint16_t* outStreamLength, uint8_t nackCode); + +int write_doIPHeader(uint8_t* outStream, uint16_t* outStreamLength, uint16_t payloadType); + +int read_doIPHeader(uint8_t* inStream, uint16_t inStreamLength, uint16_t* payloadLength); + +#endif /* DOIP_H_ */ + +#ifdef __cplusplus +} +#endif