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 #define LOG_TAG "Atp"
20 #include "Atp.h"
21 #include "Iso13239CRC.h"
22 #include "android_logmsg.h"
23 
24 Atp ATP = {.bwt = 0x0690, .checksumType = CRC, .ifsc = 0xFE};
25 
26 uint8_t gATP[ATP_MAX_ALLOWED_LENGTH];
27 //************************************ Functions *******************************
28 
29 /*******************************************************************************
30 **
31 ** Function         Atp_getChecksumValue
32 **
33 ** Description      Gets the value of the checksum stored in the array.
34 **
35 ** Parameters       array                 - array that contains the checksum.
36 **                  checksumStartPosition - checksum start position in array.
37 **
38 ** Returns          checksum value
39 **
40 *******************************************************************************/
Atp_getChecksumValue(uint8_t * array,int checksumStartPosition)41 uint16_t Atp_getChecksumValue(uint8_t *array, int checksumStartPosition) {
42   return (uint16_t)((uint8_t)array[checksumStartPosition + 1] << 8) |
43          (uint8_t)array[checksumStartPosition];
44 }
45 
46 /*******************************************************************************
47 **
48 ** Function         Atp_setAtp
49 **
50 ** Description     Sets the ATP struct available for the whole system.
51 **
52 ** Parameters       baAtp - ATP as a byte array.
53 **
54 ** Returns          0 If everything is Ok, -1 otherwise.
55 **
56 *******************************************************************************/
Atp_setAtp(uint8_t * baAtp)57 int Atp_setAtp(uint8_t *baAtp) {
58   uint8_t i;
59   Atp tmpAtp;
60 
61   // Length
62   tmpAtp.len = (uint8_t)baAtp[LEN_OFFSET_IN_ATP];
63 
64   if (tmpAtp.len > ATP_MAX_ALLOWED_LENGTH) {
65     return -1;
66   }
67   memcpy(gATP, baAtp, tmpAtp.len);
68   tmpAtp.checksum = Atp_getChecksumValue(baAtp, CHECKSUM_OFFSET_IN_ATP);
69 
70   // Check CRC
71   if (computeCrc(baAtp, LEN_LENGTH_IN_ATP + tmpAtp.len - CRC_LENGTH_IN_ATP) !=
72       tmpAtp.checksum) {
73     STLOG_HAL_E("Error computing CRC");
74     return -1;
75   }
76 
77   // CHECKSUM TYPE
78   if (baAtp[CHECKSUM_TYPE_OFFSET_IN_ATP] == 0) {
79     tmpAtp.checksumType = LRC;
80   } else if (baAtp[CHECKSUM_TYPE_OFFSET_IN_ATP] == 1) {
81     tmpAtp.checksumType = CRC;
82   } else {
83     // Unexpected value.
84     STLOG_HAL_E("Unexpected value of checksum type.");
85     return -1;
86   }
87 
88   // Vendor ID
89   for (i = 0; i < VENDOR_ID_LENGTH_IN_ATP; i++) {
90     tmpAtp.vendorID[i] = baAtp[VENDOR_ID_OFFSET_IN_ATP + i];
91   }
92 
93   // BWT
94   tmpAtp.bwt = 0;
95   for (i = 0; i < BWT_LENGTH_IN_ATP; i++) {
96     tmpAtp.bwt =
97         (uint16_t)(tmpAtp.bwt << 8) + (uint8_t)baAtp[BWT_OFFSET_IN_ATP + i];
98   }
99 
100   // CWT
101   tmpAtp.cwt = (uint8_t)baAtp[CWT_OFFSET_IN_ATP];
102 
103   // PWT
104   tmpAtp.pwt = (uint8_t)baAtp[PWT_OFFSET_IN_ATP];
105 
106   // MSF
107   STLOG_HAL_V("Configuring ATP %i", tmpAtp.msf);
108   tmpAtp.msf = 0;
109   for (i = 0; i < MSF_LENGTH_IN_ATP; i++) {
110     tmpAtp.msf =
111         (uint16_t)(tmpAtp.msf << 8) + (uint8_t)baAtp[MSF_OFFSET_IN_ATP + i];
112     STLOG_HAL_V("Loop ATP %i", baAtp[MSF_OFFSET_IN_ATP + i]);
113   }
114 
115   // IFSC
116   tmpAtp.ifsc = (uint8_t)baAtp[IFSC_OFFSET_IN_ATP];
117 
118   // HISTORICAL CHARACTER
119   for (i = 0; i < HISTORICAL_CHARACTER_LENGTH_IN_ATP; i++) {
120     tmpAtp.historicalCharacter[i] =
121         baAtp[HISTORICAL_CHARACTER_OFFSET_IN_ATP + i];
122   }
123 
124   // Set the actual ATP and return with no error.
125   ATP = tmpAtp;
126   return 0;
127 }
128 
129 /*******************************************************************************
130 **
131 ** Function         Atp_getAtp
132 **
133 ** Description     Gets the ATP stored.
134 **
135 **
136 ** Returns          pointer to the ATP array
137 **
138 *******************************************************************************/
Atp_getAtp()139 uint8_t *Atp_getAtp() { return &gATP[0]; }
140