fpc_com_transport.c
Go to the documentation of this file.
/*
* Copyright (c) 2020 Fingerprint Cards AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <string.h>
#include "fpc_com_link.h"
#include "fpc_com_transport.h"
fpc_com_result_t fpc_com_transport_transmit(fpc_com_packet_tsp_t *packet, fpc_com_chain_t *chain)
{
fpc_com_packet_link_t link_packet = { 0 };
fpc_com_result_t result;
uint16_t link_offset;
uint16_t offset;
if (packet == NULL|| chain == NULL) {
result = FPC_COM_RESULT_INVALID_ARGUMENT;
goto exit;
}
/* Construct header */
chain->link_overhead_get(&link_offset);
link_packet.data = chain->phy_mtu_buffer[FPC_COM_CHAIN_TX] + link_offset;
*((uint16_t *)(link_packet.data)) = packet->size;
offset = sizeof(packet->size);
*((uint16_t *)(link_packet.data + offset)) = packet->seq_nr;
offset += sizeof(packet->seq_nr);
*((uint16_t *)(link_packet.data + offset)) = packet->seq_len;
link_packet.channel = chain->channel;
link_packet.size = packet->size + chain->tsp_overhead_get(NULL);
/* Send packet */
result = fpc_com_link_transmit(&link_packet, chain);
exit:
return result;
}
fpc_com_result_t fpc_com_transport_receive(fpc_com_packet_tsp_t *packet, fpc_com_chain_t *chain)
{
fpc_com_packet_link_t link_packet = { 0 };
fpc_com_result_t result;
uint16_t offset;
if (packet == NULL|| chain == NULL) {
result = FPC_COM_RESULT_INVALID_ARGUMENT;
goto exit;
}
result = fpc_com_link_receive(&link_packet, chain);
if (result != FPC_COM_RESULT_OK) {
goto exit;
}
packet->size = *((uint16_t *)link_packet.data);
offset = sizeof(packet->size);
packet->seq_nr = *((uint16_t *)(link_packet.data + offset));
offset += sizeof(packet->seq_nr);
packet->seq_len = *((uint16_t *)(link_packet.data + offset));
offset += sizeof(packet->seq_len);
packet->data = link_packet.data + offset;
exit:
return result;
}
uint16_t fpc_com_transport_get_overhead(uint16_t *offset)
{
fpc_com_packet_tsp_t *packet;
static const uint16_t internal_offset = sizeof(packet->size) + sizeof(packet->seq_len)
+ sizeof(packet->seq_nr);
if (offset) {
*offset = internal_offset;
}
return internal_offset;
}
fpc_com_transport_transmit
fpc_com_result_t fpc_com_transport_transmit(fpc_com_packet_tsp_t *packet, fpc_com_chain_t *chain)
Transmit a transport layer packet.
Definition: fpc_com_transport.c:28
fpc_com_chain::tsp_overhead_get
uint16_t(* tsp_overhead_get)(uint16_t *offset)
Definition: fpc_com_chain.h:95
fpc_com_chain::phy_mtu_buffer
uint8_t * phy_mtu_buffer[2]
Definition: fpc_com_chain.h:121
fpc_com_chain
Definition: fpc_com_chain.h:50
FPC_COM_CHAIN_TX
Definition: fpc_com_chain.h:150
fpc_com_packet_transport::data
uint8_t * data
Definition: fpc_com_packets.h:45
fpc_com_packet_link::size
uint16_t size
Definition: fpc_com_packets.h:65
fpc_com_packet_transport
Definition: fpc_com_packets.h:37
fpc_com_link_receive
fpc_com_result_t fpc_com_link_receive(fpc_com_packet_link_t *packet, fpc_com_chain_t *chain)
Receives a packet from the physical link.
Definition: fpc_com_link.c:73
FPC_COM_RESULT_INVALID_ARGUMENT
Definition: fpc_com_result.h:35
fpc_com_chain::channel
fpc_com_channel_t channel
Definition: fpc_com_chain.h:105
fpc_com_link_transmit
fpc_com_result_t fpc_com_link_transmit(fpc_com_packet_link_t *packet, fpc_com_chain_t *chain)
Sends a packet over the physical link in blocking mode.
Definition: fpc_com_link.c:27
fpc_com_transport_receive
fpc_com_result_t fpc_com_transport_receive(fpc_com_packet_tsp_t *packet, fpc_com_chain_t *chain)
Receive a transport layer packet.
Definition: fpc_com_transport.c:60
fpc_com_packet_link::data
uint8_t * data
Definition: fpc_com_packets.h:67
fpc_com_packet_link::channel
fpc_com_channel_t channel
Definition: fpc_com_packets.h:63
fpc_com_packet_transport::seq_len
uint16_t seq_len
Definition: fpc_com_packets.h:41
fpc_com_chain::link_overhead_get
uint16_t(* link_overhead_get)(uint16_t *offset)
Definition: fpc_com_chain.h:103
fpc_com_transport.h
Communication transport interface.
fpc_com_result_t
uint8_t fpc_com_result_t
Definition: fpc_com_result.h:41
fpc_com_link.h
Communication link interface.
fpc_com_packet_transport::seq_nr
uint16_t seq_nr
Definition: fpc_com_packets.h:43
fpc_com_packet_transport::size
uint16_t size
Definition: fpc_com_packets.h:39
FPC_COM_RESULT_OK
Definition: fpc_com_result.h:33
fpc_com_transport_get_overhead
uint16_t fpc_com_transport_get_overhead(uint16_t *offset)
Returns the overhead of the layer.
Definition: fpc_com_transport.c:88
fpc_com_packet_link
Definition: fpc_com_packets.h:61