Changed VCSignalProtocol to ModbusProtocol

This is a first draft and it would be lucky if it worked
This commit is contained in:
Jonny007-MKD 2014-05-28 14:27:53 +00:00
parent cdd7f1352a
commit b0c465c341
21 changed files with 1933 additions and 1829 deletions

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,496 @@
/*-------------------------------------------------------------------
ModbusProtocol.cpp
-------------------------------------------------------------------
(c) Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
#include "StdAfx.h"
#include "ModbusProtocol.h"
////////////////////////////////////////////////////////////////////////
//
// Implementation of VExampleSignalProtocol
//
////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------
//! Constructor
/**
*/
ModbusProtocol::ModbusProtocol() :
mProtocolId( NIPB::kNil ),
mTxId(0),
mProtType(0),
mLength(0),
mUnitId(0),
mFuncCode(0)
{
}
////////////////////////////////////////////////////////////////////////
// IProtocol
////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------*/
//! Initialize the protocol
/*!
Register the protocol and its tokens at the IPB Packet Lib.
\param protocolManager The protocol manager for this protocol. The pointer can be stored and is valid
during the whole lifetime of the IProtocol object.
\param networkModel The network model for this protocol. The pointer can be stored and is valid
during the whole lifetime of the IProtocol object.
\param modelCreator Define network model entities with this object. Pointer is only valid during this call.
*/
VDEF ModbusProtocol::Initialize( NIPB::IProtocolManager /*in*/ *protocolManager, NIPB::INetworkModel /*in*/ *networkModel, NIPB::INetworkModelCreator /*in*/ *modelCreator )
{
if (protocolManager == 0) return NIPB::kInvalidArg;
if (networkModel == 0) return NIPB::kInvalidArg;
if (modelCreator == 0) return NIPB::kInvalidArg;
mProtocolManager = protocolManager;
NIPB::ITokenDefinitionCreator *protocol = 0;
if (modelCreator->DefineProtocol( "modbus", &mProtocolId, &protocol ) == NIPB::kOK)
{
modelCreator->DefineProtocolField( protocol, "TxId", &mTxId, 0 );
modelCreator->DefineProtocolField( protocol, "Protocol", &mProtType, 0 );
modelCreator->DefineProtocolField( protocol, "Length", &mLength, 0 );
modelCreator->DefineProtocolField( protocol, "UnitId", &mUnitId, 0 );
modelCreator->DefineProtocolField( protocol, "FuncCode", &mFuncCode, 0 );
}
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Denitialize the protocol
/*!
\param protocolManager The protocol manager for this protocol. The pointer is invalid after Deinitialize.
\param networkModel The network model for this protocol. The pointer is invalid after Deinitialize.
*/
VDEF ModbusProtocol::Deinitialize( NIPB::IProtocolManager /*in*/ * /*protocolManager*/, NIPB::INetworkModel /*in*/ * /*networkModel*/ )
{
mProtocolId = 0;
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Returns the textual designator of the protocol
/*!
The designator is used to access the protocol in CAPL (via Nodelayer) or the GUI of CANoe/CANalyzer.
\param bufferSize Length of 'buffer'. Must not > 0
\param buffer The designator is copied to this buffer. Must not be 0
*/
VDEF ModbusProtocol::GetDesignator( unsigned long /*in*/ bufferSize, char /*out**/ *buffer )
{
if (bufferSize == 0) return NIPB::kInvalidArg;
if (buffer == 0) return NIPB::kInvalidArg;
if (bufferSize < strlen("modbus")) return NIPB::kInvalidArg;
strcpy_s( buffer, bufferSize, "modbus" );
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Unique token identifier of the protocol
/*!
The VTokenId of the protocol is registerd on startup during defintion of the protocol token.
\param identifier A pointer to a VTokenId, which retrieves the identifier. Must not be 0
*/
VDEF ModbusProtocol::GetIdentifier( NIPB::VTokenId /*out*/ *identifier )
{
if (identifier == 0) return NIPB::kInvalidArg;
*identifier = mProtocolId;
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Returns specific encoder for a token ID
/*!
The IEncoder is used to access a token within a IPacket.
\param tokenId Token identifier for which the encoder is requested.
\param encoder Returns a pointer to the encoder. Must not be 0.
*/
VDEF ModbusProtocol::GetEncoder( NIPB::VTokenId /*in*/ tokenId, NIPB::IEncoder /*out*/ **encoder )
{
if (encoder == 0) return NIPB::kInvalidArg;
if ( tokenId == mTxId
|| tokenId == mProtType
|| tokenId == mLength)
{
return mProtocolManager->GetEncoder( NIPB::kEncoderUnsignedLE, encoder );
}
else if ( tokenId == mUnitId
|| tokenId == mFuncCode)
{
return mProtocolManager->GetEncoder( NIPB::kEncoderUnsignedBE, encoder );
}
else
{
return NIPB::kEncodingNotSupported;
}
}
/*--------------------------------------------------------------------*/
//! Initialize a specific protocol during creation of a new packet
/*!
Use this method to setup a protocol within a packet.
\param packet The packet where the protocol should be setup. Must not be 0.
\param protocolTypeId Type ID for a specific type of the protocol, i.e ARP request or ARP response
\param topProtocolToken Points to the top most protocol within the packet.
\param protocolToken This strucuture must be filled in InitProtocol.
*/
VDEF ModbusProtocol::InitProtocol( NIPB::IPacket /*in*/ *packet, NIPB::VTokenId /*in*/ /*protocolTypeId*/, const NIPB::VProtocolToken /*in*/ * /*topProtocolToken*/, NIPB::VProtocolToken /*out*/ *modbus )
{
if (mProtocolManager == 0) return NIPB::kInternalError;
if (packet == 0) return NIPB::kInvalidArg;
NIPB::VResult result = NIPB::kError;
BYTE broadcastMacId[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
//
// Init Ethernet protocol
//
NIPB::VProtocolToken eth;
if ((result = mProtocolManager->GetProtocolByIndex( packet, 0, &eth )) != NIPB::kOK)
{
return result;
}
this->SetTokenData( eth, NIPB::kEthDestination, 6, broadcastMacId ); // Set destination MAC ID to Broadcast
//
// Init IPv4 protocol
//
NIPB::VProtocolToken ipv4;
if ((result = mProtocolManager->InitProtocol( packet, NIPB::kIPv4, 0, &ipv4 )) != NIPB::kOK)
{
return result;
}
this->SetTokenUnsigned( ipv4, NIPB::kIPv4Destination, 4, 0xFFFFFFFF ); // Set destination IP address to Broadcast
//
// Init UDP protocol
//
NIPB::VProtocolToken udp;
if ((result = mProtocolManager->InitProtocol( packet, NIPB::kUDP, 0, &udp )) != NIPB::kOK)
{
return result;
}
this->SetTokenUnsigned( udp, NIPB::kUDPSource , 2, 40002 ); // Set source port
this->SetTokenUnsigned( udp, NIPB::kUDPDestination, 2, 502 ); // Set destination port
//
// Init Modbus protocol
//
if (result == (mProtocolManager->ResizeToken( &udp, kHeaderBitLength, 0 )))
{
modbus->tokenId = mProtocolId;
modbus->protocol = this;
modbus->packet = packet;
modbus->headerBitOffset = udp.bitOffset;
modbus->headerBitLength = kHeaderBitLength;
modbus->bitOffset = udp.bitOffset + kHeaderBitLength;
modbus->bitLength = 0;
this->SetTokenUnsigned( *modbus, mTxId, 2, 0x0000 ); // Set some TxID
this->SetTokenUnsigned( *modbus, mProtType, 2, 0x0000 ); // Set ProtocolID = Modbus
this->SetTokenUnsigned( *modbus, mLength, 2, 0x0000 ); // Set some invalid length
this->SetTokenUnsigned( *modbus, mUnitId, 1, 0xFF ); // Set some UnitID (unused)
this->SetTokenUnsigned( *modbus, mFuncCode, 1, 0x00 ); // Set some invalid FuncCode
}
return result;
}
/*--------------------------------------------------------------------*/
//! Complete the protocol at the end of creation/modification of a packet.
/*!
In this function the checksum or length field of a protocol can be calculated.
\param packet The packet to complete
\param protocol Points to the protocol token for this protocol
*/
VDEF ModbusProtocol::CompleteProtocol( NIPB::IPacket /*in*/ *packet, const NIPB::VProtocolToken /*in*/ *modbus )
{
if (packet == 0) return NIPB::kInvalidArg;
if (modbus == 0) return NIPB::kInvalidArg;
if (modbus->tokenId != mProtocolId) return NIPB::kError;
WORD lengthByte;
lengthByte = (WORD)BI2BY(modbus->bitLength - kHeaderBitLength + 16); // add UnitID and FuncCode to length although they are header
this->SetTokenUnsigned( *(NIPB::VProtocolToken*)modbus, mLength, 2, lengthByte ); // Set the correct length
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Parse a received packet.
/*!
The method is called, when a packet is parsed. During parsing the VProtocols structure is initialzed
with the protocol information, whiche are returned with 'protocolToken'.
\param packet The packet to parse.
\param topProtocolToken Points to the top most, already parsed protocol within the packet.
\param errorWriter Use this interface to return parsing errors, i.e wrong checksum
\param nextProcotolId Return the VTokenId of the next protocol.
\param protocolToken Fill this structure to setup the VProtocols structure.
*/
VDEF ModbusProtocol::ParsePacket( NIPB::IPacket /*in*/ *packet, const NIPB::VProtocolToken /*in*/ *topProtocolToken, NIPB::IFormatter /*in*/ * /*errorWriter*/, NIPB::VTokenId /*out*/ *nextProcotolId, NIPB::VProtocolToken /*out*/ *protocolToken )
{
if (packet == 0 ) return NIPB::kInvalidArg;
if (nextProcotolId == 0 ) return NIPB::kInvalidArg;
if (protocolToken == 0 ) return NIPB::kInvalidArg;
if (topProtocolToken == 0 ) return NIPB::kInvalidArg;
if (topProtocolToken->tokenId != NIPB::kUDP) return NIPB::kInvalidArg;
NIPB::VResult result = NIPB::kError;
const BYTE *packetData = 0;
ULONG packetLength = 0;
NIPB::VProtocols *protocols = 0;
if ( ((result = packet->GetDataSize( &packetLength )) != NIPB::kOK)
|| ((result = packet->GetDataPtr ( (void**)&packetData )) != NIPB::kOK)
|| ((result = packet->GetProtocols( &protocols )) != NIPB::kOK) )
return result;
if (topProtocolToken->bitLength <= kHeaderBitLength)
return NIPB::kPacketParseError;
protocolToken->tokenId = mProtocolId;
protocolToken->protocol = this;
protocolToken->packet = packet;
protocolToken->headerBitOffset = topProtocolToken->bitOffset;
protocolToken->headerBitLength = kHeaderBitLength;
protocolToken->bitOffset = topProtocolToken->bitOffset + kHeaderBitLength;
protocolToken->bitLength = topProtocolToken->bitLength - kHeaderBitLength; // don't add UnitID and FuncCode here
protocols->contentId = this->GetContentId( packetData, packetLength, *protocolToken );
*nextProcotolId = 0;
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Return a token of a protocol.
/*!
The function must fill the 'token' with the information of the request 'tokenId'.
\param protocolToken Points the the VProtocolToken for the protocol.
\param tokenId The identifier of the requested token.
\param token Points to a VToken, which must be fille by this function.
*/
VDEF ModbusProtocol::GetToken( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, NIPB::VToken /*out*/ *token )
{
if (token == 0) return NIPB::kInvalidArg;
if (protocolToken == 0) return NIPB::kInvalidArg;
if (protocolToken->tokenId != mProtocolId) return NIPB::kTokenNotFound;
if (tokenId == mTxId)
return MakeHeaderToken( protocolToken, mTxId, 0, 16, token );
else if (tokenId == mProtType)
return MakeHeaderToken( protocolToken, mProtType, 16, 16, token );
else if (tokenId == mLength)
return MakeHeaderToken( protocolToken, mLength, 32, 16, token );
else if (tokenId == mUnitId)
return MakeHeaderToken( protocolToken, mUnitId, 48, 8, token );
else if (tokenId == mFuncCode)
return MakeHeaderToken( protocolToken, mFuncCode, 56, 8, token );
else if (tokenId == NIPB::kData)
return this->MakePayloadToken( protocolToken, NIPB::kData, this->kHeaderBitLength, protocolToken->bitLength, token );
else if (tokenId == NIPB::kHeader)
return this->MakeHeaderToken( protocolToken, NIPB::kHeader, 0, this->kHeaderBitLength, token );
else
return NIPB::kTokenNotFound;
}
/*--------------------------------------------------------------------*/
//! Build a tree with information of the protocol.
/*!
The function is used in CANoe/CANalyzer in the detail view of the trace window to build
a tree with protocols and fields.
\param protocolToken Protocol token of this protocol
\param type Inspection type
\param inspector IPacketInspectore to create the output.
*/
VDEF ModbusProtocol::InspectProtocol ( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VInspectionType /*in*/ type, NIPB::IPacketInspector /*in*/ *inspector )
{
if (protocolToken == 0) return NIPB::kInvalidArg;
if (protocolToken->packet == 0) return NIPB::kInvalidArg;
if (inspector == 0) return NIPB::kInvalidArg;
NIPB::VResult result = NIPB::kError;
const BYTE *packetData = 0;
ULONG packetLength = 0;
if ( ((result = protocolToken->packet->GetDataSize( &packetLength )) != NIPB::kOK)
|| ((result = protocolToken->packet->GetDataPtr ( (void**)&packetData )) != NIPB::kOK) )
return result;
if (inspector->BeginToken( mProtocolId, 0, 0) != NIPB::kOK)
return NIPB::kError;
NIPB::IFormatter *formatter = 0;
ULONG val;
// TODO: Make InspectProtocol nice and smooth
switch(type)
{
// Protocol Info column
case NIPB::kInspectionInfoColumn:
if (inspector->SelectField( NIPB::kFieldLabel, &formatter ) == NIPB::kOK)
formatter->FormatString( "ModbusInfo" );
// Source port
val = GetContentId( packetData, packetLength, *protocolToken );
FormatUnsigned( inspector, "TxIDInfo", mTxId, val, 2 );
break;
//
// Detail View
//
case NIPB::kInspectionDetailTree:
if (inspector->SelectField( NIPB::kFieldLabel, &formatter ) == NIPB::kOK)
formatter->FormatString( "ModbusDetail" );
// Source port
val = GetContentId( packetData, packetLength, *protocolToken );
FormatUnsigned( inspector, "TxIDDetail", mTxId, val, 2 );
break;
}
inspector->EndToken();
return result;
}
////////////////////////////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------*/
//! Returns the content ID
/*!
*/
ULONG ModbusProtocol::GetContentId( const BYTE *packetData, ULONG packetLength, const NIPB::VProtocolToken &protocol )
{
ULONG offset = BI2BY(protocol.headerBitOffset);
if (offset+4 < packetLength)
{
return *((ULONG*)&packetData[offset]);
}
else
{
return 0;
}
}
/*--------------------------------------------------------------------*/
//! Fill a VToken structure.
/*!
*/
NIPB::VResult ModbusProtocol::MakeHeaderToken( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, ULONG /*in*/ offset, ULONG /*in*/ length, NIPB::VToken /*out*/ *token )
{
if (token == 0) return NIPB::kInvalidArg;
if (protocolToken == 0) return NIPB::kInvalidArg;
token->protocol = protocolToken->protocol;
token->packet = protocolToken->packet;
token->tokenId = tokenId;
token->bitOffset = offset + protocolToken->headerBitOffset;
token->bitLength = length;
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Fill a VToken structure.
/*!
*/
NIPB::VResult ModbusProtocol::MakePayloadToken( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, ULONG /*in*/ offset, ULONG /*in*/ length, NIPB::VToken /*out*/ *token )
{
if (token == 0) return NIPB::kInvalidArg;
if (protocolToken == 0) return NIPB::kInvalidArg;
token->protocol = protocolToken->protocol;
token->packet = protocolToken->packet;
token->tokenId = tokenId;
token->bitOffset = offset + protocolToken->bitOffset;
token->bitLength = length;
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Format a unsigned token.
/*!
*/
void ModbusProtocol::FormatUnsigned( NIPB::IPacketInspector *inspector, LPCTSTR label, NIPB::VTokenId tokenId, ULONG value, ULONG valueLength )
{
if ((inspector) && (inspector->BeginToken( tokenId, 0, 0) == NIPB::kOK))
{
NIPB::IFormatter *formatter = 0;
if (inspector->SelectField( NIPB::kFieldLabel, &formatter ) == NIPB::kOK)
formatter->FormatString( label );
if (inspector->SelectField( NIPB::kFieldValue, &formatter ) == NIPB::kOK)
formatter->FormatUnsigned( value, valueLength, 0 );
inspector->EndToken();
}
}
/*--------------------------------------------------------------------*/
//! Set the value of a unsigned token.
/*!
*/
NIPB::VResult ModbusProtocol::SetTokenUnsigned( NIPB::VProtocolToken &protocolToken, NIPB::VTokenId tokenId, ULONG valueLength, ULONG value )
{
if (protocolToken.protocol == 0) return NIPB::kInvalidArg;
NIPB::VResult result = NIPB::kError;
NIPB::VToken token;
if ((result = protocolToken.protocol->GetToken( &protocolToken, tokenId, &token)) == NIPB::kOK)
{
NIPB::IEncoder *encoder = 0;
if (((result = token.protocol->GetEncoder( tokenId, &encoder )) == NIPB::kOK) && (encoder))
result = encoder->Encode( &token, 0, NIPB::kEncodingUnsigned, valueLength, &value );
}
return result;
}
/*--------------------------------------------------------------------*/
//! Set the data of a data token.
/*!
*/
NIPB::VResult ModbusProtocol::SetTokenData( NIPB::VProtocolToken &protocolToken, NIPB::VTokenId tokenId, ULONG dataLength, const BYTE *data )
{
if (protocolToken.protocol == 0) return NIPB::kInvalidArg;
NIPB::VResult result = NIPB::kError;
NIPB::VToken token;
if ((result = protocolToken.protocol->GetToken( &protocolToken, tokenId, &token)) == NIPB::kOK)
{
NIPB::IEncoder *encoder = 0;
if (((result = token.protocol->GetEncoder( tokenId, &encoder )) == NIPB::kOK) && (encoder))
{
result = encoder->Encode( &token, 0, NIPB::kEncodingData, dataLength, data );
}
}
return result;
}

View file

@ -0,0 +1,162 @@
/*-------------------------------------------------------------------
VCSignalProtocol.h
-------------------------------------------------------------------
(c) Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
#pragma once
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------*/
//! Signal protocol class
/*!
This class implements the NIPB::IProtocol interface. It handles the
Modbus protocol
The protocol is located within a UDP protocol and uses the UDP
ports 40002 and 502. The first 8 Bytes of the UDP payload
contain the header:
[0,2] Transaction ID
[2,2] Protocol ID (Modbus := 0x0000)
[4,2] Length (Number of following bytes)
[6,1] Unit ID (not used)
[7,1] Function Code
*/
class ModbusProtocol :
public NIPB::IProtocol
{
static const ULONG kHeaderLength = 8; // see above
static const ULONG kHeaderBitLength = kHeaderLength*8;
//
// Construction
//
public:
ModbusProtocol();
//
// IProtocol
//
public:
//! Initialize the protocol
/*!
\param protocolManager The protocol manager for this protocol. The pointer can be stored and is valid
during the whole lifetime of the IProtocol object.
\param networkModel The network model for this protocol. The pointer can be stored and is valid
during the whole lifetime of the IProtocol object.
\param modelCreator Define network model entities with this object. Pointer is only valid during this call.
*/
VDEF Initialize ( NIPB::IProtocolManager /*in*/ *protocolManager, NIPB::INetworkModel /*in*/ *networkModel, NIPB::INetworkModelCreator /*in*/ *modelCreator );
//! Deinitialize the protocol
/*!
\param protocolManager The protocol manager for this protocol. The pointer is invalid after Deinitialize.
\param networkModel The network model for this protocol. The pointer is invalid after Deinitialize.
*/
VDEF Deinitialize ( NIPB::IProtocolManager /*in*/ *protocolManager, NIPB::INetworkModel /*in*/ *networkModel );
//! Returns the textual designator of the protocol
/*!
The designator is used to access the protocol in CAPL (via Nodelayer) or the GUI of CANoe/CANalyzer.
\param bufferSize Length of 'buffer'. Must not > 0
\param buffer The designator is copied to this buffer. Must not be 0
*/
VDEF GetDesignator ( unsigned long /*in*/ bufferSize, char /*out**/ *buffer );
//! Unique token identifier of the protocol
/*!
The VTokenId of the protocol is registerd on startup during defintion of the protocol token.
\param identifier A pointer to a VTokenId, which retrieves the identifier. Must not be 0
*/
VDEF GetIdentifier ( NIPB::VTokenId /*out*/ *identifier );
//! Returns specific encoder for a token ID
/*!
The IEncoder is used to access a token within a IPacket.
\param tokenId Token identifier for which the encoder is requested.
\param encoder Returns a pointer to the encoder. Must not be 0.
*/
VDEF GetEncoder ( NIPB::VTokenId /*in*/ tokenId, NIPB::IEncoder /*out*/ **encoder );
//! Initialize a specific protocol during creation of a new packet
/*!
Use this method to setup a protocol within a packet.
\param packet The packet where the protocol should be setup. Must not be 0.
\param protocolTypeId Type ID for a specific type of the protocol, i.e ARP request or ARP response
\param topProtocolToken Points to the top most protocol within the packet.
\param protocolToken This strucuture must be filled in InitProtocol.
*/
VDEF InitProtocol ( NIPB::IPacket /*in*/ *packet, NIPB::VTokenId /*in*/ protocolTypeId, const NIPB::VProtocolToken /*in*/ *topProtocolToken, NIPB::VProtocolToken /*out*/ *protocolToken);
//! Complete the protocol at the end of creation/modification of a packet.
/*!
In this function the checksum or length field of a protocol can be calculated.
\param packet The packet to complete
\param protocol Points to the protocol token for this protocol
*/
VDEF CompleteProtocol( NIPB::IPacket /*in*/ *packet, const NIPB::VProtocolToken /*in*/ *modbus ); // Is *modbus UDP or Modbus?
//! Parse a received packet.
/*!
The method is called, when a packet is parsed. During parsing the VProtocols structure is initialzed
with the protocol information, whiche are returnd with 'protocolToken'.
\param packet The packet to parse.
\param topProtocolToken Points to the top most, already parsed protocol within the packet.
\param errorWriter Use this interface to returne parsing errors, i.e wrong checksum
\param nextProcotolId Return the VTokenId of the next protocol.
\param protocolToken Fill this strucutre to setup the VProtocols structure.
*/
VDEF ParsePacket ( NIPB::IPacket /*in*/ *packet, const NIPB::VProtocolToken /*in*/ *topProtocolToken, NIPB::IFormatter /*in*/ *errorWriter, NIPB::VTokenId /*out*/ *nextProcotolId, NIPB::VProtocolToken /*out*/ *protocolToken );
//! Return a token of a protocol.
/*!
The function must fill the 'token' with the information of the request 'tokenId'.
\param protocolToken Points the the VProtocolToken for the protocol.
\param tokenId The identifier of the requested token.
\param token Points to a VToken, which must be fille by this function.
*/
VDEF GetToken ( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, NIPB::VToken /*out*/ *token );
//! Build a tree with information of the protocol.
/*!
The function is used in CANoe/CANalyzer in the detail view of the trace window to build
a tree with protocols and fields.
\param protocolToken Protocol token of this protocol
\param type Inspection type
\param inspector IPacketInspectore to create the output.
*/
VDEF InspectProtocol ( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VInspectionType /*in*/ type, NIPB::IPacketInspector /*in*/ *inspector );
//
// Methods
//
public:
ULONG GetContentId( const BYTE *packetData, ULONG packetLength, const NIPB::VProtocolToken &protocol );
static NIPB::VResult MakeHeaderToken( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, ULONG /*in*/ offset, ULONG /*in*/ length, NIPB::VToken /*out*/ *token );
static NIPB::VResult MakePayloadToken( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, ULONG /*in*/ offset, ULONG /*in*/ length, NIPB::VToken /*out*/ *token );
static void FormatUnsigned( NIPB::IPacketInspector *inspector, LPCTSTR label, NIPB::VTokenId tokenId, ULONG value, ULONG valueLength );
static NIPB::VResult SetTokenUnsigned( NIPB::VProtocolToken &protocolToken, NIPB::VTokenId tokenId, ULONG valueLength, ULONG value );
static NIPB::VResult SetTokenData( NIPB::VProtocolToken &protocolToken, NIPB::VTokenId tokenId, ULONG dataLength, const BYTE *data );
//
// Attributes
//
private:
NIPB::VTokenId mProtocolId; //!< Token ID of this protocol
NIPB::VTokenId mTxId; //!< Token ID of the 'TxID' field
NIPB::VTokenId mProtType; //!< Token ID of the 'ProtocolID' field
NIPB::VTokenId mLength; //!< Token ID of the 'Length' field
NIPB::VTokenId mUnitId; //!< Token ID of the 'UnitID' field
NIPB::VTokenId mFuncCode; //!< Token ID of the 'FuncCode' field
NIPB::IProtocolManager *mProtocolManager;
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

View file

@ -1,41 +1,41 @@
/*-------------------------------------------------------------------
VCSignalProtocolAddOn.h
-------------------------------------------------------------------
VCSignalProtocolAddOn.h
-------------------------------------------------------------------
(c) Vector Informatik GmbH. All rights reserved.
(c) Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
------------------------------------------------------------------- */
#include "StdAfx.h"
#include "VCSignalProtocolAddOn.h"
#include "VCSignalProtocol.h"
#include "ModbusProtocolAddOn.h"
#include "ModbusProtocol.h"
////////////////////////////////////////////////////////////////////////
//
// Implementaiton of VCSignalProtocolAddOn
// Implementation of ModbusProtocolAddOn
//
////////////////////////////////////////////////////////////////////////
VCSignalProtocolAddOn *VCSignalProtocolAddOn::sInstance = NULL;
ModbusProtocolAddOn *ModbusProtocolAddOn::sInstance = NULL;
//----------------------------------------------------------------------
//!
/**
*/
VCSignalProtocolAddOn::VCSignalProtocolAddOn()
ModbusProtocolAddOn::ModbusProtocolAddOn()
{
sInstance = this;
sInstance = this;
}
//----------------------------------------------------------------------
//!
/**
*/
VCSignalProtocolAddOn& VCSignalProtocolAddOn::Instance()
ModbusProtocolAddOn& ModbusProtocolAddOn::Instance()
{
if (sInstance == 0) throw;
if (sInstance == 0) throw;
return *sInstance;
return *sInstance;
}
////////////////////////////////////////////////////////////////////////
@ -46,26 +46,26 @@ VCSignalProtocolAddOn& VCSignalProtocolAddOn::Instance()
//!
/**
*/
VDEF VCSignalProtocolAddOn::CreateProtocol( const char /*in*/ *protocolSymbol, NIPB::IProtocol /*out*/ **protocol )
VDEF ModbusProtocolAddOn::CreateProtocol( const char /*in*/ *protocolSymbol, NIPB::IProtocol /*out*/ **protocol )
{
if (protocol == 0) return NIPB::kInvalidArg;
if (protocolSymbol == 0) return NIPB::kInvalidArg;
if (protocol == 0) return NIPB::kInvalidArg;
if (protocolSymbol == 0) return NIPB::kInvalidArg;
*protocol = new VCSignalProtocol();
*protocol = new ModbusProtocol();
return NIPB::kOK;
return NIPB::kOK;
}
//----------------------------------------------------------------------
//!
/**
*/
VDEF VCSignalProtocolAddOn::ReleaseProtocol( NIPB::IProtocol /*in*/ *protocol)
VDEF ModbusProtocolAddOn::ReleaseProtocol( NIPB::IProtocol /*in*/ *protocol)
{
VCSignalProtocol *prot = dynamic_cast<VCSignalProtocol*>( protocol );
delete prot;
ModbusProtocol *prot = dynamic_cast<ModbusProtocol*>( protocol );
delete prot;
return NIPB::kOK;
return NIPB::kOK;
}
////////////////////////////////////////////////////////////////////////
@ -76,9 +76,9 @@ VDEF VCSignalProtocolAddOn::ReleaseProtocol( NIPB::IProtocol /*in*/ *protocol)
//!
/**
*/
void VCSignalProtocolAddOn::Release()
void ModbusProtocolAddOn::Release()
{
delete this;
delete this;
}
////////////////////////////////////////////////////////////////////////

View file

@ -0,0 +1,52 @@
/*-------------------------------------------------------------------
ModbusProtocolAddOn.h
-------------------------------------------------------------------
(c) Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
#pragma once
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------*/
//! Factory for the VCSignalProtocol
/*!
This class is registered at the NIPB::IAddOnRegistrar registrar. It
is responsible to create a instance of the ModbusProtocol, when needed.
*/
class ModbusProtocolAddOn :
public NIPB::IProtocolAddOn
{
//
// Construction
//
public:
ModbusProtocolAddOn();
static ModbusProtocolAddOn& Instance();
//
// IProtocolAddOn
//
public:
VDECL CreateProtocol( const char /*in*/ *protocolSymbol, NIPB::IProtocol /*out*/ **protocol );
VDECL ReleaseProtocol( NIPB::IProtocol /*in*/ *protocol);
//
// Methods
//
public:
void Release();
//
// Attributes
//
private:
static ModbusProtocolAddOn *sInstance;
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

View file

@ -0,0 +1,69 @@
/*-------------------------------------------------------------------
ModbusProtocolDLL.cpp
-------------------------------------------------------------------
(c) 2009 Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
#include "StdAfx.h"
#include "ModbusProtocolDLL.h"
#include "ModbusProtocolAddOn.h"
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------*/
//! Reply new ProtocolManager instance
/*!
This function is called on loading of this DLL by the IPB Packet API.
It must register the protocol of this DLL.
*/
STDAPI DllRegisterAddOn( NIPB::IAddOnRegistrar* registrar )
{
if (registrar == NULL)
{
return E_INVALIDARG;
}
//
// register the signal protocol at the IPB Packet Lib. For the protocol
// UDP with source port 40002 is used.
//
if (registrar->RegisterProtocol( "modbus", new ModbusProtocolAddOn(), "udp", 40002, 0xFFFFFFFF ) != NIPB::kOK)
{
ModbusProtocolAddOn::Instance().Release();
return NIPB::kError;
}
return S_OK;
}
/*--------------------------------------------------------------------*/
//! Return interface version
/*!
This DLL must support a proper version of the IBP Packet API.
*/
STDAPI DllGetApiVersion( unsigned long /*out*/*major, unsigned long /*out*/*minor, unsigned long /*out*/*structVersion )
{
if (major)
{
*major = IPB_PACKET_LIB_INTERFACE_VERSION_MAJOR;
}
if (minor)
{
*minor = IPB_PACKET_LIB_INTERFACE_VERSION_MINOR;
}
if (structVersion)
{
*structVersion = IPB_PACKET_LIB_STRUCT_VERSION;
}
return S_OK;
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

View file

@ -0,0 +1,7 @@
; ModbusProtocolDLL.def : Declares the module parameters.
LIBRARY "ModbusProtocolDLL.DLL"
EXPORTS
DllGetApiVersion PRIVATE
DllRegisterAddOn PRIVATE

View file

@ -0,0 +1,10 @@
/*-------------------------------------------------------------------
ModbusProtocolDLL.h
-------------------------------------------------------------------
(c) 2009 Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
#pragma once

View file

@ -2,9 +2,9 @@
<VisualStudioProject
ProjectType="Visual C++"
Version="8,00"
Name="VCSignalProtocolDLL"
Name="ModbusProtocolDLL"
ProjectGUID="{0015F4D0-0830-408B-97FB-D9748FD43623}"
RootNamespace="VCSignalProtocolDLL"
RootNamespace="ModbusProtocolDLL"
Keyword="ManagedCProj"
>
<Platforms>
@ -61,9 +61,9 @@
<Tool
Name="VCLinkerTool"
AdditionalDependencies="$(NoInherit)"
OutputFile="..\VCSignalProtocol.dll"
OutputFile="..\ModbusProtocol.dll"
LinkIncremental="2"
ModuleDefinitionFile="VCSignalProtocolDLL.def"
ModuleDefinitionFile="ModbusProtocolDLL.def"
GenerateDebugInformation="true"
AssemblyDebug="1"
TargetMachine="1"
@ -140,9 +140,9 @@
<Tool
Name="VCLinkerTool"
AdditionalDependencies="$(NoInherit)"
OutputFile="..\VCSignalProtocol.dll"
OutputFile="..\ModbusProtocol.dll"
LinkIncremental="1"
ModuleDefinitionFile="VCSignalProtocolDLL.def"
ModuleDefinitionFile="ModbusProtocolDLL.def"
GenerateDebugInformation="true"
TargetMachine="1"
/>
@ -215,15 +215,15 @@
>
</File>
<File
RelativePath=".\VCSignalProtocolDLL.cpp"
RelativePath=".\ModbusProtocolDLL.cpp"
>
</File>
<File
RelativePath=".\VCSignalProtocolDLL.def"
RelativePath=".\ModbusProtocolDLL.def"
>
</File>
<File
RelativePath=".\VCSignalProtocolDLL.h"
RelativePath=".\ModbusProtocolDLL.h"
>
</File>
</Filter>
@ -231,19 +231,19 @@
Name="Protocol"
>
<File
RelativePath=".\VCSignalProtocol.cpp"
RelativePath=".\ModbusProtocol.cpp"
>
</File>
<File
RelativePath=".\VCSignalProtocol.h"
RelativePath=".\ModbusProtocol.h"
>
</File>
<File
RelativePath=".\VCSignalProtocolAddOn.cpp"
RelativePath=".\ModbusProtocolAddOn.cpp"
>
</File>
<File
RelativePath=".\VCSignalProtocolAddOn.h"
RelativePath=".\ModbusProtocolAddOn.h"
>
</File>
</Filter>

View file

@ -12,8 +12,9 @@
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{0015F4D0-0830-408B-97FB-D9748FD43623}</ProjectGuid>
<RootNamespace>VCSignalProtocolDLL</RootNamespace>
<RootNamespace>ModbusProtocolDLL</RootNamespace>
<Keyword>ManagedCProj</Keyword>
<ProjectName>ModbusProtocol</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
@ -48,7 +49,6 @@
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<AdditionalOptions>/analyze %(AdditionalOptions)</AdditionalOptions>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>.;source;common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
@ -60,8 +60,8 @@
<Link>
<AdditionalDependencies>
</AdditionalDependencies>
<OutputFile>..\VCSignalProtocol.dll</OutputFile>
<ModuleDefinitionFile>VCSignalProtocolDLL.def</ModuleDefinitionFile>
<OutputFile>..\ModbusProtocol.dll</OutputFile>
<ModuleDefinitionFile>ModbusProtocolDLL.def</ModuleDefinitionFile>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AssemblyDebug>true</AssemblyDebug>
<TargetMachine>MachineX86</TargetMachine>
@ -70,6 +70,9 @@
<Command>
</Command>
</PostBuildEvent>
<CustomBuildStep>
<Command>copy $(OutputPath)\$(TargetFileName) $(OutputPath)\..\..\$(TargetFileName)</Command>
</CustomBuildStep>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
@ -84,8 +87,8 @@
<Link>
<AdditionalDependencies>
</AdditionalDependencies>
<OutputFile>..\VCSignalProtocol.dll</OutputFile>
<ModuleDefinitionFile>VCSignalProtocolDLL.def</ModuleDefinitionFile>
<OutputFile>..\ModbusProtocol.dll</OutputFile>
<ModuleDefinitionFile>ModbusProtocolDLL.def</ModuleDefinitionFile>
<GenerateDebugInformation>true</GenerateDebugInformation>
<TargetMachine>MachineX86</TargetMachine>
</Link>
@ -109,19 +112,19 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="VCSignalProtocolDLL.cpp" />
<ClCompile Include="VCSignalProtocol.cpp" />
<ClCompile Include="VCSignalProtocolAddOn.cpp" />
<ClCompile Include="ModbusProtocolDLL.cpp" />
<ClCompile Include="ModbusProtocol.cpp" />
<ClCompile Include="ModbusProtocolAddOn.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Stdafx.h" />
<ClInclude Include="VCSignalProtocolDLL.h" />
<ClInclude Include="VCSignalProtocol.h" />
<ClInclude Include="VCSignalProtocolAddOn.h" />
<ClInclude Include="ModbusProtocolDLL.h" />
<ClInclude Include="ModbusProtocol.h" />
<ClInclude Include="ModbusProtocolAddOn.h" />
<ClInclude Include="Include\IPBPacketAPI.h" />
</ItemGroup>
<ItemGroup>
<None Include="VCSignalProtocolDLL.def" />
<None Include="ModbusProtocolDLL.def" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View file

@ -15,36 +15,36 @@
<ClCompile Include="Stdafx.cpp">
<Filter>DLL</Filter>
</ClCompile>
<ClCompile Include="VCSignalProtocolDLL.cpp">
<ClCompile Include="ModbusProtocolAddOn.cpp">
<Filter>Protocol</Filter>
</ClCompile>
<ClCompile Include="ModbusProtocolDLL.cpp">
<Filter>Protocol</Filter>
</ClCompile>
<ClCompile Include="ModbusProtocol.cpp">
<Filter>DLL</Filter>
</ClCompile>
<ClCompile Include="VCSignalProtocol.cpp">
<Filter>Protocol</Filter>
</ClCompile>
<ClCompile Include="VCSignalProtocolAddOn.cpp">
<Filter>Protocol</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Stdafx.h">
<Filter>DLL</Filter>
</ClInclude>
<ClInclude Include="VCSignalProtocolDLL.h">
<Filter>DLL</Filter>
</ClInclude>
<ClInclude Include="VCSignalProtocol.h">
<Filter>Protocol</Filter>
</ClInclude>
<ClInclude Include="VCSignalProtocolAddOn.h">
<Filter>Protocol</Filter>
</ClInclude>
<ClInclude Include="Include\IPBPacketAPI.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="ModbusProtocolDLL.h">
<Filter>Protocol</Filter>
</ClInclude>
<ClInclude Include="ModbusProtocolAddOn.h">
<Filter>Protocol</Filter>
</ClInclude>
<ClInclude Include="ModbusProtocol.h">
<Filter>DLL</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="VCSignalProtocolDLL.def">
<Filter>DLL</Filter>
<None Include="ModbusProtocolDLL.def">
<Filter>Protocol</Filter>
</None>
</ItemGroup>
</Project>

View file

@ -1,9 +1,9 @@
/*-------------------------------------------------------------------
StdAfx.cpp
-------------------------------------------------------------------
StdAfx.cpp
-------------------------------------------------------------------
(c) 2009 Vector Informatik GmbH. All rights reserved.
(c) 2009 Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
------------------------------------------------------------------- */
#include "StdAfx.h"

View file

@ -1,10 +1,10 @@
/*-------------------------------------------------------------------
StdAfx.h
-------------------------------------------------------------------
StdAfx.h
-------------------------------------------------------------------
(c) 2009 Vector Informatik GmbH. All rights reserved.
(c) 2009 Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
------------------------------------------------------------------- */
#pragma once

View file

@ -1,481 +0,0 @@
/*-------------------------------------------------------------------
VCSignalProtocol.h
-------------------------------------------------------------------
(c) Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
#include "StdAfx.h"
#include "VCSignalProtocol.h"
////////////////////////////////////////////////////////////////////////
//
// Implementaiton of VExampleSignalProtocol
//
////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------
//! Constructor
/**
*/
VCSignalProtocol::VCSignalProtocol() :
mProtocolId( NIPB::kNil ),
mContentId ( 0 )
{
}
////////////////////////////////////////////////////////////////////////
// IProtocol
////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------*/
//! Initialize the protocol
/*!
Register the protocol and its tokens at the IPB Packet Lib.
\param protocolManager The protocol manager for this protocol. The pointer can be stored and is valid
during the whole lifetime of the IProtocol object.
\param networkModel The network model for this protocol. The pointer can be stored and is valid
during the whole lifetime of the IProtocol object.
\param modelCreator Define network model entities with this object. Pointer is only valid during this call.
*/
VDEF VCSignalProtocol::Initialize( NIPB::IProtocolManager /*in*/ *protocolManager, NIPB::INetworkModel /*in*/ *networkModel, NIPB::INetworkModelCreator /*in*/ *modelCreator )
{
if (protocolManager == 0) return NIPB::kInvalidArg;
if (networkModel == 0) return NIPB::kInvalidArg;
if (modelCreator == 0) return NIPB::kInvalidArg;
mProtocolManager = protocolManager;
NIPB::ITokenDefinitionCreator *protocol = 0;
if (modelCreator->DefineProtocol( "vc-sig", &mProtocolId, &protocol ) == NIPB::kOK)
{
modelCreator->DefineProtocolField( protocol, "contentId", &mContentId, 0 );
}
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Denitialize the protocol
/*!
\param protocolManager The protocol manager for this protocol. The pointer is invalid after Deinitialize.
\param networkModel The network model for this protocol. The pointer is invalid after Deinitialize.
*/
VDEF VCSignalProtocol::Deinitialize( NIPB::IProtocolManager /*in*/ * /*protocolManager*/, NIPB::INetworkModel /*in*/ * /*networkModel*/ )
{
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Returns the textual designator of the protocol
/*!
The designator is used to access the protocol in CAPL (via Nodelayer) or the GUI of CANoe/CANalyzer.
\param bufferSize Length of 'buffer'. Must not > 0
\param buffer The designator is copied to this buffer. Must not be 0
*/
VDEF VCSignalProtocol::GetDesignator( unsigned long /*in*/ bufferSize, char /*out**/ *buffer )
{
if (bufferSize == 0) return NIPB::kInvalidArg;
if (buffer == 0) return NIPB::kInvalidArg;
strcpy_s( buffer, bufferSize, "vc-sig" );
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Unique token identifier of the protocol
/*!
The VTokenId of the protocol is registerd on startup during defintion of the protocol token.
\param identifier A pointer to a VTokenId, which retrieves the identifier. Must not be 0
*/
VDEF VCSignalProtocol::GetIdentifier( NIPB::VTokenId /*out*/ *identifier )
{
if (identifier == 0) return NIPB::kInvalidArg;
*identifier = mProtocolId;
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Returns specific encoder for a token ID
/*!
The IEncoder is used to access a token within a IPacket.
\param tokenId Token identifier for which the encoder is requested.
\param encoder Returns a pointer to the encoder. Must not be 0.
*/
VDEF VCSignalProtocol::GetEncoder( NIPB::VTokenId /*in*/ tokenId, NIPB::IEncoder /*out*/ **encoder )
{
if (encoder == 0) return NIPB::kInvalidArg;
if (tokenId == mContentId)
{
return mProtocolManager->GetEncoder( NIPB::kEncoderUnsignedLE, encoder );
}
else
{
return NIPB::kEncodingNotSupported;
}
}
/*--------------------------------------------------------------------*/
//! Initialize a specific protocol during creation of a new packet
/*!
Use this method to setup a protocol within a packet.
\param packet The packet where the protocol should be setup. Must not be 0.
\param protocolTypeId Type ID for a specific type of the protocol, i.e ARP request or ARP response
\param topProtocolToken Points to the top most protocol within the packet.
\param protocolToken This strucuture must be filled in InitProtocol.
*/
VDEF VCSignalProtocol::InitProtocol( NIPB::IPacket /*in*/ *packet, NIPB::VTokenId /*in*/ /*protocolTypeId*/, const NIPB::VProtocolToken /*in*/ * /*topProtocolToken*/, NIPB::VProtocolToken /*out*/ *vc_sig )
{
if (mProtocolManager == 0) return NIPB::kInternalError;
if (packet == 0) return NIPB::kInvalidArg;
NIPB::VResult result = NIPB::kError;
BYTE broadcastMacId[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
//
// Init Ethernet protocol
//
NIPB::VProtocolToken eth;
if ((result = mProtocolManager->GetProtocolByIndex( packet, 0, &eth )) != NIPB::kOK)
{
return result;
}
SetTokenData( eth, NIPB::kEthDestination, 6, broadcastMacId ); // Set destination MAC ID to Broadcast
//
// Init IPv4 protocol
//
NIPB::VProtocolToken ipv4;
if ((result = mProtocolManager->InitProtocol( packet, NIPB::kIPv4, 0, &ipv4 )) != NIPB::kOK)
{
return result;
}
SetTokenUnsigned( ipv4, NIPB::kIPv4Destination, 4, 0xFFFFFFFF ); // Set destination IP address to Broadcast
//
// Init UDP protocol
//
NIPB::VProtocolToken udp;
if ((result = mProtocolManager->InitProtocol( packet, NIPB::kUDP, 0, &udp )) != NIPB::kOK)
{
return result;
}
SetTokenUnsigned( udp, NIPB::kUDPSource , 2, 40001 ); // Set source port
SetTokenUnsigned( udp, NIPB::kUDPDestination, 2, 40001 ); // Set destination port
//
// Init VCSig protocol
//
if (result == (mProtocolManager->ResizeToken( &udp, kHeaderBitLength, 0 )))
{
vc_sig->tokenId = mProtocolId;
vc_sig->protocol = this;
vc_sig->packet = packet;
vc_sig->headerBitOffset = udp.bitOffset;
vc_sig->headerBitLength = kHeaderBitLength;
vc_sig->bitOffset = udp.bitOffset + kHeaderBitLength;
vc_sig->bitLength = 0;
SetTokenUnsigned( *vc_sig, mContentId, 4, 0xFFFFFFFF ); // Set invalid content ID
}
return result;
}
/*--------------------------------------------------------------------*/
//! Complete the protocol at the end of creation/modification of a packet.
/*!
In this function the checksum or length field of a protocol can be calculated.
\param packet The packet to complete
\param protocol Points to the protocol token for this protocol
*/
VDEF VCSignalProtocol::CompleteProtocol( NIPB::IPacket /*in*/ *packet, const NIPB::VProtocolToken /*in*/ *vc_sig )
{
if (packet == 0) return NIPB::kInvalidArg;
if (vc_sig == 0) return NIPB::kInvalidArg;
if (vc_sig->tokenId != mProtocolId) return NIPB::kError;
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Parse a received packet.
/*!
The method is called, when a packet is parsed. During parsing the VProtocols structure is initialzed
with the protocol information, whiche are returnd with 'protocolToken'.
\param packet The packet to parse.
\param topProtocolToken Points to the top most, already parsed protocol within the packet.
\param errorWriter Use this interface to returne parsing errors, i.e wrong checksum
\param nextProcotolId Return the VTokenId of the next protocol.
\param protocolToken Fill this strucutre to setup the VProtocols structure.
*/
VDEF VCSignalProtocol::ParsePacket( NIPB::IPacket /*in*/ *packet, const NIPB::VProtocolToken /*in*/ *topProtocolToken, NIPB::IFormatter /*in*/ * /*errorWriter*/, NIPB::VTokenId /*out*/ *nextProcotolId, NIPB::VProtocolToken /*out*/ *protocolToken )
{
if (packet == 0 ) return NIPB::kInvalidArg;
if (nextProcotolId == 0 ) return NIPB::kInvalidArg;
if (protocolToken == 0 ) return NIPB::kInvalidArg;
if (topProtocolToken == 0 ) return NIPB::kInvalidArg;
if (topProtocolToken->tokenId != NIPB::kUDP) return NIPB::kInvalidArg;
NIPB::VResult result = NIPB::kError;
const BYTE *packetData = 0;
ULONG packetLength = 0;
NIPB::VProtocols *protocols = 0;
if ( ((result = packet->GetDataSize( &packetLength )) == NIPB::kOK)
&& ((result = packet->GetDataPtr ( (void**)&packetData )) == NIPB::kOK)
&& ((result = packet->GetProtocols( &protocols )) == NIPB::kOK)
)
{
result = NIPB::kPacketParseError;
if (BI2BY(topProtocolToken->bitLength) >= kHeaderLength)
{
protocolToken->tokenId = mProtocolId;
protocolToken->protocol = this;
protocolToken->packet = packet;
protocolToken->headerBitOffset = topProtocolToken->bitOffset;
protocolToken->headerBitLength = kHeaderBitLength;
protocolToken->bitOffset = kHeaderBitLength + topProtocolToken->bitOffset;
protocolToken->bitLength = topProtocolToken->bitLength - kHeaderBitLength;
protocols->contentId = GetContentId( packetData, packetLength, *protocolToken );
*nextProcotolId = 0;
result = NIPB::kOK;
}
}
return result;
}
/*--------------------------------------------------------------------*/
//! Return a token of a protocol.
/*!
The function must fill the 'token' with the information of the request 'tokenId'.
\param protocolToken Points the the VProtocolToken for the protocol.
\param tokenId The identifier of the requested token.
\param token Points to a VToken, which must be fille by this function.
*/
VDEF VCSignalProtocol::GetToken( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, NIPB::VToken /*out*/ *token )
{
if (token == 0) return NIPB::kInvalidArg;
if (protocolToken == 0) return NIPB::kInvalidArg;
if (protocolToken->tokenId != mProtocolId) return NIPB::kTokenNotFound;
if (tokenId == mContentId)
{
return MakeHeaderToken( protocolToken, mContentId, 0, 32, token );
}
else if (tokenId == NIPB::kData)
{
return MakePayloadToken( protocolToken, NIPB::kData, 0, protocolToken->bitLength, token );
}
else if (tokenId == NIPB::kHeader)
{
return MakeHeaderToken( protocolToken, NIPB::kHeader, 0, protocolToken->bitLength, token );
}
else
{
return NIPB::kTokenNotFound;
}
}
/*--------------------------------------------------------------------*/
//! Build a tree with information of the protocol.
/*!
The function is used in CANoe/CANalyzer in the detail view of the trace window to build
a tree with protocols and fields.
\param protocolToken Protocol token of this protocol
\param type Inspection type
\param inspector IPacketInspectore to create the output.
*/
VDEF VCSignalProtocol::InspectProtocol ( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VInspectionType /*in*/ type, NIPB::IPacketInspector /*in*/ *inspector )
{
if (protocolToken == 0) return NIPB::kInvalidArg;
if (protocolToken->packet == 0) return NIPB::kInvalidArg;
if (inspector == 0) return NIPB::kInvalidArg;
NIPB::VResult result = NIPB::kError;
const BYTE *packetData = 0;
ULONG packetLength = 0;
if ( ((result = protocolToken->packet->GetDataSize( &packetLength )) == NIPB::kOK)
&& ((result = protocolToken->packet->GetDataPtr ( (void**)&packetData )) == NIPB::kOK) )
{
switch(type)
{
//
// Protocol Info column
//
case NIPB::kInspectionInfoColumn:
break;
//
// Detail View
//
case NIPB::kInspectionDetailTree:
// UDP
if (inspector->BeginToken( mProtocolId, 0, 0) == NIPB::kOK)
{
NIPB::IFormatter *formatter = 0;
if (inspector->SelectField( NIPB::kFieldLabel, &formatter ) == NIPB::kOK)
{
formatter->FormatString( "vc-sig" );
}
// Source port
ULONG val = GetContentId( packetData, packetLength, *protocolToken );
FormatUnsigned( inspector, "contentId", mContentId, val, 4 );
inspector->EndToken();
}
break;
}
}
return result;
}
////////////////////////////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------*/
//! Returns the content ID
/*!
*/
ULONG VCSignalProtocol::GetContentId( const BYTE *packetData, ULONG packetLength, const NIPB::VProtocolToken &protocol )
{
ULONG offset = BI2BY(protocol.headerBitOffset);
if (offset+4 < packetLength)
{
return *((ULONG*)&packetData[offset]);
}
else
{
return 0;
}
}
/*--------------------------------------------------------------------*/
//! Fill a VToken structure.
/*!
*/
NIPB::VResult VCSignalProtocol::MakeHeaderToken( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, ULONG /*in*/ offset, ULONG /*in*/ length, NIPB::VToken /*out*/ *token )
{
if (token == 0) return NIPB::kInvalidArg;
if (protocolToken == 0) return NIPB::kInvalidArg;
token->protocol = protocolToken->protocol;
token->packet = protocolToken->packet;
token->tokenId = tokenId;
token->bitOffset = offset + protocolToken->headerBitOffset;
token->bitLength = length;
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Fill a VToken structure.
/*!
*/
NIPB::VResult VCSignalProtocol::MakePayloadToken( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, ULONG /*in*/ offset, ULONG /*in*/ length, NIPB::VToken /*out*/ *token )
{
if (token == 0) return NIPB::kInvalidArg;
if (protocolToken == 0) return NIPB::kInvalidArg;
token->protocol = protocolToken->protocol;
token->packet = protocolToken->packet;
token->tokenId = tokenId;
token->bitOffset = offset + protocolToken->bitOffset;
token->bitLength = length;
return NIPB::kOK;
}
/*--------------------------------------------------------------------*/
//! Format a unsigned token.
/*!
*/
void VCSignalProtocol::FormatUnsigned( NIPB::IPacketInspector *inspector, LPCTSTR label, NIPB::VTokenId tokenId, ULONG value, ULONG valueLength )
{
if ((inspector) && (inspector->BeginToken( tokenId, 0, 0) == NIPB::kOK))
{
NIPB::IFormatter *formatter = 0;
if (inspector->SelectField( NIPB::kFieldLabel, &formatter ) == NIPB::kOK)
{
formatter->FormatString( label );
}
if (inspector->SelectField( NIPB::kFieldValue, &formatter ) == NIPB::kOK)
{
formatter->FormatUnsigned( value, valueLength, 0 );
}
inspector->EndToken();
}
}
/*--------------------------------------------------------------------*/
//! Set the value of a unsigned token.
/*!
*/
NIPB::VResult VCSignalProtocol::SetTokenUnsigned( NIPB::VProtocolToken &protocolToken, NIPB::VTokenId tokenId, ULONG valueLength, ULONG value )
{
if (protocolToken.protocol == 0) return NIPB::kInvalidArg;
NIPB::VResult result = NIPB::kError;
NIPB::VToken token;
if ((result = protocolToken.protocol->GetToken( &protocolToken, tokenId, &token)) == NIPB::kOK)
{
NIPB::IEncoder *encoder = 0;
if (((result = token.protocol->GetEncoder( tokenId, &encoder )) == NIPB::kOK) && (encoder))
{
result = encoder->Encode( &token, 0, NIPB::kEncodingUnsigned, valueLength, &value );
}
}
return result;
}
/*--------------------------------------------------------------------*/
//! Set the data of a data token.
/*!
*/
NIPB::VResult VCSignalProtocol::SetTokenData( NIPB::VProtocolToken &protocolToken, NIPB::VTokenId tokenId, ULONG dataLength, const BYTE *data )
{
if (protocolToken.protocol == 0) return NIPB::kInvalidArg;
NIPB::VResult result = NIPB::kError;
NIPB::VToken token;
if ((result = protocolToken.protocol->GetToken( &protocolToken, tokenId, &token)) == NIPB::kOK)
{
NIPB::IEncoder *encoder = 0;
if (((result = token.protocol->GetEncoder( tokenId, &encoder )) == NIPB::kOK) && (encoder))
{
result = encoder->Encode( &token, 0, NIPB::kEncodingData, dataLength, data );
}
}
return result;
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

View file

@ -1,76 +0,0 @@
/*-------------------------------------------------------------------
VCSignalProtocol.h
-------------------------------------------------------------------
(c) Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
#pragma once
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------*/
//! Signal protocol class
/*!
This class implements the NIPB::IProtocol interface. It handles a
proprietary signal protocol for the Vector Conecept Car Ethernet
Network.
The protocol is located within a UDP protocol and uses the UDP
ports 40001. The first 32bit of the UDP payload are the content ID.
The 32-bit content ID represents the header of this Signal protocol.
The payload of this protocol is interpreted by the DBC.
*/
class VCSignalProtocol :
public NIPB::IProtocol
{
static const ULONG kHeaderLength = 4;
static const ULONG kHeaderBitLength = 32;
//
// Construction
//
public:
VCSignalProtocol();
//
// IProtocol
//
public:
VDECL Initialize ( NIPB::IProtocolManager /*in*/ *protocolManager, NIPB::INetworkModel /*in*/ *networkModel, NIPB::INetworkModelCreator /*in*/ *modelCreator );
VDECL Deinitialize ( NIPB::IProtocolManager /*in*/ *protocolManager, NIPB::INetworkModel /*in*/ *networkModel );
VDECL GetDesignator ( unsigned long /*in*/ bufferSize, char /*out**/ *buffer );
VDECL GetIdentifier ( NIPB::VTokenId /*out*/ *identifier );
VDECL GetEncoder ( NIPB::VTokenId /*in*/ tokenId, NIPB::IEncoder /*out*/ **encoder );
VDECL InitProtocol ( NIPB::IPacket /*in*/ *packet, NIPB::VTokenId /*in*/ protocolTypeId, const NIPB::VProtocolToken /*in*/ *topProtocolToken, NIPB::VProtocolToken /*out*/ *protocolToken);
VDECL CompleteProtocol( NIPB::IPacket /*in*/ *packet, const NIPB::VProtocolToken /*in*/ *udp );
VDECL ParsePacket ( NIPB::IPacket /*in*/ *packet, const NIPB::VProtocolToken /*in*/ *topProtocolToken, NIPB::IFormatter /*in*/ *errorWriter, NIPB::VTokenId /*out*/ *nextProcotolId, NIPB::VProtocolToken /*out*/ *protocolToken );
VDECL GetToken ( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, NIPB::VToken /*out*/ *token );
VDECL InspectProtocol ( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VInspectionType /*in*/ type, NIPB::IPacketInspector /*in*/ *inspector );
//
// Methods
//
public:
ULONG GetContentId( const BYTE *packetData, ULONG packetLength, const NIPB::VProtocolToken &protocol );
static NIPB::VResult MakeHeaderToken( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, ULONG /*in*/ offset, ULONG /*in*/ length, NIPB::VToken /*out*/ *token );
static NIPB::VResult MakePayloadToken( const NIPB::VProtocolToken /*in*/ *protocolToken, NIPB::VTokenId /*in*/ tokenId, ULONG /*in*/ offset, ULONG /*in*/ length, NIPB::VToken /*out*/ *token );
static void FormatUnsigned( NIPB::IPacketInspector *inspector, LPCTSTR label, NIPB::VTokenId tokenId, ULONG value, ULONG valueLength );
static NIPB::VResult SetTokenUnsigned( NIPB::VProtocolToken &protocolToken, NIPB::VTokenId tokenId, ULONG valueLength, ULONG value );
static NIPB::VResult SetTokenData( NIPB::VProtocolToken &protocolToken, NIPB::VTokenId tokenId, ULONG dataLength, const BYTE *data );
//
// Attributes
//
private:
NIPB::VTokenId mProtocolId; //!< Token ID of this protocol
NIPB::VTokenId mContentId; //!< Token ID of the 'contentID' field
NIPB::IProtocolManager *mProtocolManager;
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

View file

@ -1,52 +0,0 @@
/*-------------------------------------------------------------------
VCSignalProtocolAddOn.h
-------------------------------------------------------------------
(c) Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
#pragma once
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------*/
//! Factory for the VCSignalProtocol
/*!
This class is registered at the NIPB::IAddOnRegistrar registrar. It
is responsible to create a instance of the VCSignalProtocol, when needed.
*/
class VCSignalProtocolAddOn :
public NIPB::IProtocolAddOn
{
//
// Construction
//
public:
VCSignalProtocolAddOn();
static VCSignalProtocolAddOn& Instance();
//
// IProtocolAddOn
//
public:
VDECL CreateProtocol( const char /*in*/ *protocolSymbol, NIPB::IProtocol /*out*/ **protocol );
VDECL ReleaseProtocol( NIPB::IProtocol /*in*/ *protocol);
//
// Methods
//
public:
void Release();
//
// Attributes
//
private:
static VCSignalProtocolAddOn *sInstance;
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

View file

@ -1,69 +0,0 @@
/*-------------------------------------------------------------------
VCSignalProtocolDLL.cpp
-------------------------------------------------------------------
(c) 2009 Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
#include "StdAfx.h"
#include "VCSignalProtocolDLL.h"
#include "VCSignalProtocolAddOn.h"
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------*/
//! Reply new ProtocolManager instance
/*!
This function is called on loading of this DLL by the IPB Packet API.
It must register the protocol of this DLL.
*/
STDAPI DllRegisterAddOn( NIPB::IAddOnRegistrar* registrar )
{
if (registrar == NULL)
{
return E_INVALIDARG;
}
//
// register the signal protocol at the IPB Packet Lib. For the protocol
// UDP with source port 40001 is used.
//
if (registrar->RegisterProtocol( "vc-sig", new VCSignalProtocolAddOn(), "udp", 40001, 0xFFFFFFFF ) != NIPB::kOK)
{
VCSignalProtocolAddOn::Instance().Release();
return NIPB::kError;
}
return S_OK;
}
/*--------------------------------------------------------------------*/
//! Return interface version
/*!
This DLL must support a proper version of the IBP Packet API.
*/
STDAPI DllGetApiVersion( unsigned long /*out*/*major, unsigned long /*out*/*minor, unsigned long /*out*/*structVersion )
{
if (major)
{
*major = IPB_PACKET_LIB_INTERFACE_VERSION_MAJOR;
}
if (minor)
{
*minor = IPB_PACKET_LIB_INTERFACE_VERSION_MINOR;
}
if (structVersion)
{
*structVersion = IPB_PACKET_LIB_STRUCT_VERSION;
}
return S_OK;
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

View file

@ -1,7 +0,0 @@
; VCSignalProtocolDLL.def : Declares the module parameters.
LIBRARY "VCSignalProtocolDLL.DLL"
EXPORTS
DllGetApiVersion PRIVATE
DllRegisterAddOn PRIVATE

View file

@ -1,10 +0,0 @@
/*-------------------------------------------------------------------
VCSignalProtocolDLL.h
-------------------------------------------------------------------
(c) 2009 Vector Informatik GmbH. All rights reserved.
------------------------------------------------------------------- */
#pragma once