1 /****************************************************************************** 2 * 3 * Copyright (C) 2018 ST Microelectronics S.A. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 * 18 ******************************************************************************/ 19 20 #ifndef TPDU_H_ 21 #define TPDU_H_ 22 23 //*********************************** Includes ********************************* 24 25 #include <stdbool.h> 26 #include <stdint.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 31 #include "Atp.h" 32 33 //************************************ Defines ********************************* 34 #define NAD_OFFSET_IN_TPDU 0 35 #define PCB_OFFSET_IN_TPDU 1 36 #define LEN_OFFSET_IN_TPDU 2 37 #define DATA_OFFSET_IN_TPDU 3 38 39 #define TPDU_MAX_LENGTH 259 40 #define TPDU_PROLOGUE_LENGTH 3 41 #define TPDU_MAX_DATA_LENGTH 254 42 #define TPDU_CRC_LENGTH 2 43 #define TPDU_LRC_LENGTH 1 44 45 //************************************ Structs ********************************* 46 typedef struct { 47 uint8_t nad; 48 uint8_t pcb; 49 uint8_t len; 50 uint8_t *data; 51 uint16_t checksum; 52 } Tpdu; 53 54 typedef enum { IBlock, RBlock, SBlock } TpduType; 55 56 typedef enum { ErrorFree, ChecksumError, OtherErrors } RBlockType; 57 58 //************************************ Functions ******************************* 59 60 /** 61 * Forms a byte array representing the given TPDU. 62 * 63 * @param structTpdu The TPDU struct to be converted to byte array. 64 * @param baTpdu The memory position where to store the formed byte array. 65 * 66 * @return The length of the formed array 67 */ 68 uint16_t Tpdu_toByteArray(Tpdu *structTpdu, uint8_t *baTpdu); 69 70 /** 71 * Checks that the checksum in the TPDU is as expected. 72 * 73 * @param tpdu The TPDU whose checksum needs to be checked. 74 * 75 * @return true if checksum is ok, false otherwise. 76 */ 77 bool Tpdu_isChecksumOk(Tpdu *tpdu); 78 79 /** 80 * Forms a TPDU with the specified fields. 81 * 82 * @param nad The NAD byte of the TPDU. 83 * @param pac The PCB byte of the TPDU. 84 * @param len The length of the data. 85 * @param data The data of the TPDU. 86 * @pram tpdu The memory position where the formed TPDU will be stored. 87 * 88 * @return 0 if everything went ok, -1 otherwise. 89 */ 90 int Tpdu_formTpdu(uint8_t nad, uint8_t pcb, uint8_t len, uint8_t *data, 91 Tpdu *tpdu); 92 93 /** 94 * Returns the checksum value in the form of a byte array. 95 * 96 * @param tpdu The TPDU struct from where to get the checksum value. 97 * @param checksumBytes The memory position where to store the result. 98 */ 99 void Tpdu_getChecksumBytes(Tpdu *tpdu, uint8_t *checksumBytes); 100 101 /** 102 * Gets the value of the checksum stored in the array. 103 * 104 * @param array The array that contains the checksum. 105 * @param checksumStartPosition The position where the checksum starts in array. 106 * 107 * @return The value of the checksum. 108 */ 109 uint16_t Tpdu_getChecksumValue(uint8_t *array, int checksumStartPosition, 110 ChecksumType checksumType); 111 112 /** 113 * Returns the type of the TPDU. 114 * 115 * @param the tpdu the type has to be get. 116 * 117 * @return The TPDU type of the tpdu. 118 */ 119 TpduType Tpdu_getType(Tpdu *tpdu); 120 121 /** 122 * Copy Tpdu Struct. 123 * 124 * @param dest 125 * src 126 * 127 * @return void 128 */ 129 void Tpdu_copy(Tpdu *dest, Tpdu *src); 130 131 /** 132 * Converts a TPDU into a hex string. 133 * 134 * @param tpdu The TPDU to be converted to a string. 135 * @param hexStringBuffer The output buffer. 136 */ 137 void Tpdu_toHexString(Tpdu *tpdu, uint8_t *hexStringBuffer); 138 139 #endif /* TPDU_H_ */ 140