SerialPacketizer.h
/*
* Copyright (c) 2013-14, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _serial_packetizer_h_
#define _serial_packetizer_h_
#include "Packetizer.h"
#include "UartPeripheral.h"
#include "bootloader_common.h"
#include "packet/serial_packet.h"
namespace blfwk
{
// Forward declarations.
class Peripheral;
class SerialPacketizer : public Packetizer
{
public:
SerialPacketizer(UartPeripheral *peripheral, uint32_t packetTimeoutMs);
virtual ~SerialPacketizer();
virtual UartPeripheral *getPeripheral() { return dynamic_cast< UartPeripheral *>(m_peripheral); }
virtual status_t readPacket(uint8_t **packet, uint32_t *packetLength, packet_type_t packetType);
virtual status_t writePacket(const uint8_t *packet, uint32_t byteCount, packet_type_t packetType);
virtual void abortPacket();
virtual void sync();
virtual void finalize();
virtual void enableSimulatorPump() {}
virtual void pumpSimulator() {}
virtual void setAborted(bool aborted) {}
virtual uint32_t getMaxPacketSize();
void host_delay(uint32_t milliseconds);
virtual status_t ping(int retries, unsigned int delay, ping_response_t *response, int comSpeed);
protected:
status_t send_deferred_ack();
status_t serial_packet_read(uint8_t **packet, uint32_t *packetLength, packet_type_t packetType);
status_t serial_packet_write(const uint8_t *packet, uint32_t byteCount, packet_type_t packetType);
void serial_packet_abort();
uint32_t serial_packet_get_max_packet_size();
status_t serial_packet_send_sync(uint8_t framingPacketType);
status_t serial_send_ping_response();
status_t wait_for_ack_packet();
status_t read_data_packet(framing_data_packet_t *packet, uint8_t *data, packet_type_t packetType);
status_t read_start_byte(framing_header_t *header);
status_t read_header(framing_header_t *header);
status_t read_length(framing_data_packet_t *packet);
status_t read_crc16(framing_data_packet_t *packet);
uint16_t calculate_framing_crc16(framing_data_packet_t *packet, const uint8_t *data);
serial_data_t m_serialContext;
};
} // namespace blfwk
#endif // _serial_packetizer_h_
// EOF
blfwk::SerialPacketizer::sync
virtual void sync()
Send framing packet ack.
Definition: SerialPacketizer.cpp:111
blfwk::Packetizer
Interface class for packetization of commands and data.
Definition: Packetizer.h:59
blfwk::SerialPacketizer::serial_send_ping_response
status_t serial_send_ping_response()
Send a ping message back in response to a ping.
Definition: SerialPacketizer.cpp:470
blfwk::SerialPacketizer::abortPacket
virtual void abortPacket()
Abort data phase.
Definition: SerialPacketizer.cpp:105
blfwk::SerialPacketizer::calculate_framing_crc16
uint16_t calculate_framing_crc16(framing_data_packet_t *packet, const uint8_t *data)
Calculate crc over framing data packet.
Definition: SerialPacketizer.cpp:650
blfwk
Definition: BlfwkErrors.h:16
blfwk::UartPeripheral
Peripheral that talks to the target device over COM port hardware.
Definition: UartPeripheral.h:45
blfwk::SerialPacketizer::ping
virtual status_t ping(int retries, unsigned int delay, ping_response_t *response, int comSpeed)
Send a ping packet and receive an ack.
Definition: SerialPacketizer.cpp:134
blfwk::SerialPacketizer::serial_packet_read
status_t serial_packet_read(uint8_t **packet, uint32_t *packetLength, packet_type_t packetType)
Read packet using serial framing.
Definition: SerialPacketizer.cpp:267
blfwk::SerialPacketizer::serial_packet_abort
void serial_packet_abort()
Abort data phase.
Definition: SerialPacketizer.cpp:387
blfwk::SerialPacketizer::serial_packet_write
status_t serial_packet_write(const uint8_t *packet, uint32_t byteCount, packet_type_t packetType)
Write packet using serial framing.
Definition: SerialPacketizer.cpp:330
blfwk::SerialPacketizer::readPacket
virtual status_t readPacket(uint8_t **packet, uint32_t *packetLength, packet_type_t packetType)
Read a packet.
Definition: SerialPacketizer.cpp:99
blfwk::SerialPacketizer::finalize
virtual void finalize()
Finalize.
Definition: SerialPacketizer.cpp:87
blfwk::SerialPacketizer::writePacket
virtual status_t writePacket(const uint8_t *packet, uint32_t byteCount, packet_type_t packetType)
Write a packet.
Definition: SerialPacketizer.cpp:93
blfwk::SerialPacketizer::read_length
status_t read_length(framing_data_packet_t *packet)
Read from peripheral until packet length found.
Definition: SerialPacketizer.cpp:618
blfwk::SerialPacketizer::getMaxPacketSize
virtual uint32_t getMaxPacketSize()
Return the max packet size.
Definition: SerialPacketizer.cpp:117
blfwk::SerialPacketizer::read_crc16
status_t read_crc16(framing_data_packet_t *packet)
Read from peripheral until crc16 is found.
Definition: SerialPacketizer.cpp:634
blfwk::SerialPacketizer::host_delay
void host_delay(uint32_t milliseconds)
Delay milliseconds.
Definition: SerialPacketizer.cpp:123
blfwk::SerialPacketizer::enableSimulatorPump
virtual void enableSimulatorPump()
Enable simulator command processor pump.
Definition: SerialPacketizer.h:85
blfwk::SerialPacketizer::~SerialPacketizer
virtual ~SerialPacketizer()
Destructor.
Definition: SerialPacketizer.cpp:81
blfwk::SerialPacketizer::setAborted
virtual void setAborted(bool aborted)
Set aborted flag.
Definition: SerialPacketizer.h:89
blfwk::SerialPacketizer::SerialPacketizer
SerialPacketizer(UartPeripheral *peripheral, uint32_t packetTimeoutMs)
Constructor.
Definition: SerialPacketizer.cpp:73
blfwk::SerialPacketizer::read_data_packet
status_t read_data_packet(framing_data_packet_t *packet, uint8_t *data, packet_type_t packetType)
Read from peripheral until entire data framing packet read.
Definition: SerialPacketizer.cpp:507
blfwk::SerialPacketizer::wait_for_ack_packet
status_t wait_for_ack_packet()
Wait for an ACK, handling NAKs as needed.
Definition: SerialPacketizer.cpp:420
blfwk::SerialPacketizer::pumpSimulator
virtual void pumpSimulator()
Pump simulator command processor.
Definition: SerialPacketizer.h:87
blfwk::SerialPacketizer::getPeripheral
virtual UartPeripheral * getPeripheral()
Peripheral accessor.
Definition: SerialPacketizer.h:60
blfwk::SerialPacketizer::serial_packet_send_sync
status_t serial_packet_send_sync(uint8_t framingPacketType)
Send a sync packet of the specified type.
Definition: SerialPacketizer.cpp:401
blfwk::SerialPacketizer::read_start_byte
status_t read_start_byte(framing_header_t *header)
Read from peripheral until start byte found.
Definition: SerialPacketizer.cpp:563
blfwk::SerialPacketizer
Provides source and sink for packets that go over the serial peripherals.
Definition: SerialPacketizer.h:50
blfwk::SerialPacketizer::send_deferred_ack
status_t send_deferred_ack()
Send ACK if needed.
Definition: SerialPacketizer.cpp:486
blfwk::Packetizer::m_peripheral
Peripheral * m_peripheral
Peripheral to send/receive bytes on.
Definition: Packetizer.h:113
blfwk::SerialPacketizer::serial_packet_get_max_packet_size
uint32_t serial_packet_get_max_packet_size()
Get max packet size.
Definition: SerialPacketizer.cpp:395
blfwk::SerialPacketizer::read_header
status_t read_header(framing_header_t *header)
Read from peripheral until packet header found.
Definition: SerialPacketizer.cpp:604