blob: 738d2a20a59b69ddb6288502b8a9c61a7f81192a [file] [log] [blame]
Kévin Redon632502d2019-05-02 15:55:31 +02001/*
2 * Copyright (C) 2019 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17*/
18/* this library provides utilities to handle the ISO-7816 part 3 communication aspects (e.g. related to F and D) */
19#pragma once
20
21#include <stdint.h>
22#include <stdbool.h>
23
24/** default clock rate conversion integer Fd
25 * @implements ISO/IEC 7816-3:2006(E) section 8.1
26 */
27#define ISO7816_3_DEFAULT_FD 372
28/** default baud rate adjustment factor Dd
29 * @implements ISO/IEC 7816-3:2006(E) section 8.1
30 */
31#define ISO7816_3_DEFAULT_DD 1
32/** default clock rate conversion integer Fi
33 * @implements ISO/IEC 7816-3:2006(E) section 8.3
34 * @note non-default value is optionally specified in TA1
35 */
36#define ISO7816_3_DEFAULT_FI 372
37/** default baud rate adjustment factor Di
38 * @implements ISO/IEC 7816-3:2006(E) section 8.3
39 * @note non-default value is optionally specified in TA1
40 */
41#define ISO7816_3_DEFAULT_DI 1
42/** default maximum clock frequency, in Hz
43 * @implements ISO/IEC 7816-3:2006(E) section 8.3
44 * @note non-default value is optionally specified in TA1
45 */
46#define ISO7816_3_DEFAULT_FMAX 5000000UL
47/** default Waiting Integer (WI) value for T=0
48 * @implements ISO/IEC 7816-3:2006(E) section 10.2
49 * @note non-default value is optionally specified in TC2
50 */
51#define ISO7816_3_DEFAULT_WI 10
52/** default Waiting Time (WT) value, in ETU
53 * @implements ISO/IEC 7816-3:2006(E) section 8.1
54 * @note depends on Fi, Di, and WI if protocol T=0 is selected
55 */
56#define ISO7816_3_DEFAULT_WT 9600
57
58/** Table encoding the clock rate conversion integer Fi
59 * @note Fi is indicated in TA1, but the same table is used for F and Fn during PPS
60 * @implements ISO/IEC 7816-3:2006(E) table 7
61 */
62extern const uint16_t iso7816_3_fi_table[];
63
64/** Table encoding the maximum clock frequency f_max in Hz
65 * @implements ISO/IEC 7816-3:2006(E) table 7
66 * @note f_max is indicated in TA1, but the same table is used for F and Fn during PPS
67 */
68extern const uint32_t iso7816_3_fmax_table[];
69
70/** Table encoding the baud rate adjust integer Di
71 * @implements ISO/IEC 7816-3:2006(E) table 8
72 * @note Di is indicated in TA1, but the same table is used for D and Dn during PPS
73 */
74extern const uint8_t iso7816_3_di_table[];
75
76/* verify if the clock rate conversion integer F value is valid
77 * @param[in] f F value to be validated
78 * @return if F value is valid
79 * @note only values in ISO/IEC 7816-3:2006(E) table 7 are valid
80 */
81bool iso7816_3_valid_f(uint16_t f);
82/* verify if the baud rate adjustment factor D value is valid
83 * @param[in] d D value to be validated
84 * @return if D value is valid
85 * @note only values in ISO/IEC 7816-3:2006(E) table 8 are valid
86 */
87bool iso7816_3_valid_d(uint8_t d);
88/** calculate Waiting Time (WT)
89 * @param[in] wi Waiting Integer
90 * @param[in] fi clock rate conversion integer Fi value
91 * @param[in] di baud rate adjustment factor Di value
92 * @param[in] f clock rate conversion integer F value
93 * @param[in] d baud rate adjustment factor D value
94 * @return Waiting Time WT, in ETU, or < 0 on error (see code for return codes)
95 * @note this should happen after reset and T=0 protocol select (through PPS or implicit)
96 * @implements ISO/IEC 7816-3:2006(E) section 8.1 and 10.2
97 */
98int32_t iso7816_3_calculate_wt(uint8_t wi, uint16_t fi, uint8_t di, uint16_t f, uint8_t d);