Host Communication Protocol  2.0
fpc_com_transport.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Fingerprint Cards AB
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "fpc_com_link.h"
26 #include "fpc_com_transport.h"
27 
29 {
30  fpc_com_packet_link_t link_packet = { 0 };
31  fpc_com_result_t result;
32  uint16_t link_offset;
33  uint16_t offset;
34 
35  if (packet == NULL|| chain == NULL) {
37  goto exit;
38  }
39 
40  /* Construct header */
41  chain->link_overhead_get(&link_offset);
42  link_packet.data = chain->phy_mtu_buffer[FPC_COM_CHAIN_TX] + link_offset;
43 
44  *((uint16_t *)(link_packet.data)) = packet->size;
45  offset = sizeof(packet->size);
46  *((uint16_t *)(link_packet.data + offset)) = packet->seq_nr;
47  offset += sizeof(packet->seq_nr);
48  *((uint16_t *)(link_packet.data + offset)) = packet->seq_len;
49 
50  link_packet.channel = chain->channel;
51  link_packet.size = packet->size + chain->tsp_overhead_get(NULL);
52 
53  /* Send packet */
54  result = fpc_com_link_transmit(&link_packet, chain);
55 
56 exit:
57  return result;
58 }
59 
61 {
62  fpc_com_packet_link_t link_packet = { 0 };
63  fpc_com_result_t result;
64  uint16_t offset;
65 
66  if (packet == NULL|| chain == NULL) {
68  goto exit;
69  }
70 
71  result = fpc_com_link_receive(&link_packet, chain);
72  if (result != FPC_COM_RESULT_OK) {
73  goto exit;
74  }
75 
76  packet->size = *((uint16_t *)link_packet.data);
77  offset = sizeof(packet->size);
78  packet->seq_nr = *((uint16_t *)(link_packet.data + offset));
79  offset += sizeof(packet->seq_nr);
80  packet->seq_len = *((uint16_t *)(link_packet.data + offset));
81  offset += sizeof(packet->seq_len);
82  packet->data = link_packet.data + offset;
83 
84 exit:
85  return result;
86 }
87 
88 uint16_t fpc_com_transport_get_overhead(uint16_t *offset)
89 {
90  fpc_com_packet_tsp_t *packet;
91  static const uint16_t internal_offset = sizeof(packet->size) + sizeof(packet->seq_len)
92  + sizeof(packet->seq_nr);
93 
94  if (offset) {
95  *offset = internal_offset;
96  }
97 
98  return internal_offset;
99 }
fpc_com_result_t fpc_com_transport_transmit(fpc_com_packet_tsp_t *packet, fpc_com_chain_t *chain)
Transmit a transport layer packet.
uint16_t(* tsp_overhead_get)(uint16_t *offset)
Definition: fpc_com_chain.h:95
uint8_t * phy_mtu_buffer[2]
fpc_com_channel_t channel
fpc_com_result_t fpc_com_transport_receive(fpc_com_packet_tsp_t *packet, fpc_com_chain_t *chain)
Receive a transport layer packet.
uint16_t(* link_overhead_get)(uint16_t *offset)
Communication transport interface.
uint8_t fpc_com_result_t
uint16_t fpc_com_transport_get_overhead(uint16_t *offset)
Returns the overhead of the layer.