C# Class Subaru.SSM2.Ssm2Packet

Subaru SSM2 packet base class. Designed for object reusability which means immutable. Properties can be changed and will only affect those packet bytes. I.e. once header info has been set, one can modify payload data, header bytes won't be rewritten. Also avoids many backing fields since most properties directly read/write packet bytes.
Inheritance: ISsm2Packet
Datei anzeigen Open project: src0x/LibSSM2 Class Usage Examples

Protected Properties

Property Type Description
buffer byte[]
propsSet SetProperties

Public Methods

Method Description
Check ( ) : bool

Validate packet completely including checksum. Checks common and packet type specific characteristics.

Equals ( Ssm2Packet p ) : bool
Equals ( object obj ) : bool
Finish ( ) : void

Finish the packet. (Checksum and inline length byte.) Also checks if all properties have been set.

FromBytes ( byte buffer ) : void

Parses packet by taking packet bytes, does not use a copy! Assumes complete packet, does not validate. May want to call Check () afterwards. (Packet size is being calculated based on packet length byte.)

GetHashCode ( ) : int
NewFromBytes ( byte bytes ) : Ssm2Packet

Returns a specific Ssm2Packet type based on given content. Does not validate the packet, so manually call the Check method afterwards!

Ssm2Packet ( ) : System

Creates a SSM2 packet. Afterwards you have to set all properties and call Finish() to complete the packet. (This constructor allocates maximum possible packet size of 260 bytes.)

Ssm2Packet ( byte buffer ) : System

Takes given (empty) storage buffer and sets first byte. Afterwards you have to set all properties and call Construct() to finish the packet. This constructor is not meant for parsing an existing packet, use static method NewFromBytes instead or instance method FromBytes instead!

Ssm2Packet ( int capacity ) : System

Creates a new packet with specified capacity. Afterwards you have to set all properties and call Construct() to finish the packet.

ToBytesCopy ( ) : byte[]

Returns a copy of the actual packet bytes. May return empty array or less bytes if packet is incomplete. Guaranteed to not return null. Use method Check () before to assure validity.

Protected Methods

Method Description
AssertAddress ( int address ) : void

Throw ArgumentOutOfRangeException if outside valid range: 0 ≤ address ≤ 0xFFFFFF. (SSM2 addresses consist of 3 bytes allowing 16 MiB address space.)

GetAddress ( int index ) : int
SetAddress ( int address, int index ) : void
SetConstBytes ( ) : void

Sets the const bytes except first packet byte. (Must be overriden by derived classes to set command and possible padding bytes.)

Ssm2Packet ( byte buffer, int capacityMin ) : System

Assures minimum capacity of given buffer. Useful when specific minimum or fixed packet size is known. Also sets packet size to given capacity.

TakeBuffer ( byte buffer, int minLength ) : void

Takes the buffer and checks for minimum length.

UpdateFlags ( SetProperties toSet ) : void

Set flag to indicate that this part of the packet has been set. Also unsets checksum flag as it needs recalculation in method Finish().

Private Methods

Method Description
SetFirstByte ( ) : void

Method Details

AssertAddress() protected static method

Throw ArgumentOutOfRangeException if outside valid range: 0 ≤ address ≤ 0xFFFFFF. (SSM2 addresses consist of 3 bytes allowing 16 MiB address space.)
protected static AssertAddress ( int address ) : void
address int /// A ///
return void

Check() public method

Validate packet completely including checksum. Checks common and packet type specific characteristics.
public Check ( ) : bool
return bool

Equals() public method

public Equals ( Ssm2Packet p ) : bool
p Ssm2Packet
return bool

Equals() public method

public Equals ( object obj ) : bool
obj object
return bool

Finish() public method

Finish the packet. (Checksum and inline length byte.) Also checks if all properties have been set.
/// Not all required properties have been set.
public Finish ( ) : void
return void

FromBytes() public method

Parses packet by taking packet bytes, does not use a copy! Assumes complete packet, does not validate. May want to call Check () afterwards. (Packet size is being calculated based on packet length byte.)
/// /// Length ≥ 6 required. ///
public FromBytes ( byte buffer ) : void
buffer byte /// A . Length can be larger than needed. ///
return void

GetAddress() protected method

protected GetAddress ( int index ) : int
index int
return int

GetHashCode() public method

public GetHashCode ( ) : int
return int

NewFromBytes() public static method

Returns a specific Ssm2Packet type based on given content. Does not validate the packet, so manually call the Check method afterwards!
public static NewFromBytes ( byte bytes ) : Ssm2Packet
bytes byte /// A containing the packet. /// The packet must start at index 0 though the array may be larger than needed. ///
return Ssm2Packet

SetAddress() protected method

protected SetAddress ( int address, int index ) : void
address int
index int
return void

SetConstBytes() protected method

Sets the const bytes except first packet byte. (Must be overriden by derived classes to set command and possible padding bytes.)
protected SetConstBytes ( ) : void
return void

Ssm2Packet() public method

Creates a SSM2 packet. Afterwards you have to set all properties and call Finish() to complete the packet. (This constructor allocates maximum possible packet size of 260 bytes.)
public Ssm2Packet ( ) : System
return System

Ssm2Packet() public method

Takes given (empty) storage buffer and sets first byte. Afterwards you have to set all properties and call Construct() to finish the packet. This constructor is not meant for parsing an existing packet, use static method NewFromBytes instead or instance method FromBytes instead!
/// If buffer.Length < minimum (6). ///
public Ssm2Packet ( byte buffer ) : System
buffer byte /// A . Minimum ///
return System

Ssm2Packet() protected method

Assures minimum capacity of given buffer. Useful when specific minimum or fixed packet size is known. Also sets packet size to given capacity.
protected Ssm2Packet ( byte buffer, int capacityMin ) : System
buffer byte
capacityMin int
return System

Ssm2Packet() public method

Creates a new packet with specified capacity. Afterwards you have to set all properties and call Construct() to finish the packet.
/// Capacity ≥ 6 required. ///
public Ssm2Packet ( int capacity ) : System
capacity int /// Maximum packet size. ///
return System

TakeBuffer() protected method

Takes the buffer and checks for minimum length.
/// Is thrown when an argument passed to a method is invalid because it is . /// /// If buffer.Length < minimum. ///
protected TakeBuffer ( byte buffer, int minLength ) : void
buffer byte /// Buffer. ///
minLength int /// Minimum buffer length. ///
return void

ToBytesCopy() public method

Returns a copy of the actual packet bytes. May return empty array or less bytes if packet is incomplete. Guaranteed to not return null. Use method Check () before to assure validity.
public ToBytesCopy ( ) : byte[]
return byte[]

UpdateFlags() protected method

Set flag to indicate that this part of the packet has been set. Also unsets checksum flag as it needs recalculation in method Finish().
protected UpdateFlags ( SetProperties toSet ) : void
toSet SetProperties /// The flag to set. ///
return void

Property Details

buffer protected_oe property

Storage to use, make sure it is big enough. Use constant MaxBytesPacket.
protected byte[] buffer
return byte[]

propsSet protected_oe property

protected SetProperties propsSet
return SetProperties