|
| 1 | +#include "common.h" |
| 2 | + |
| 3 | +unsigned int honda_checksum(unsigned int address, uint64_t d, int l) { |
| 4 | + d >>= ((8-l)*8); // remove padding |
| 5 | + d >>= 4; // remove checksum |
| 6 | + |
| 7 | + int s = 0; |
| 8 | + while (address) { s += (address & 0xF); address >>= 4; } |
| 9 | + while (d) { s += (d & 0xF); d >>= 4; } |
| 10 | + s = 8-s; |
| 11 | + s &= 0xF; |
| 12 | + |
| 13 | + return s; |
| 14 | +} |
| 15 | + |
| 16 | +unsigned int toyota_checksum(unsigned int address, uint64_t d, int l) { |
| 17 | + d >>= ((8-l)*8); // remove padding |
| 18 | + d >>= 8; // remove checksum |
| 19 | + |
| 20 | + unsigned int s = l; |
| 21 | + while (address) { s += address & 0xff; address >>= 8; } |
| 22 | + while (d) { s += d & 0xff; d >>= 8; } |
| 23 | + |
| 24 | + return s & 0xFF; |
| 25 | +} |
| 26 | + |
| 27 | +// Static lookup table for fast computation of CRC8 poly 0x2F, aka 8H2F/AUTOSAR |
| 28 | +uint8_t crc8_lut_8h2f[256]; |
| 29 | + |
| 30 | +void gen_crc_lookup_table(uint8_t poly, uint8_t crc_lut[]) { |
| 31 | + uint8_t crc; |
| 32 | + int i, j; |
| 33 | + |
| 34 | + for (i = 0; i < 256; i++) { |
| 35 | + crc = i; |
| 36 | + for (j = 0; j < 8; j++) { |
| 37 | + if ((crc & 0x80) != 0) |
| 38 | + crc = (uint8_t)((crc << 1) ^ poly); |
| 39 | + else |
| 40 | + crc <<= 1; |
| 41 | + } |
| 42 | + crc_lut[i] = crc; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void init_crc_lookup_tables() { |
| 47 | + // At init time, set up static lookup tables for fast CRC computation. |
| 48 | + |
| 49 | + gen_crc_lookup_table(0x2F, crc8_lut_8h2f); // CRC-8 8H2F/AUTOSAR for Volkswagen |
| 50 | +} |
| 51 | + |
| 52 | +unsigned int volkswagen_crc(unsigned int address, uint64_t d, int l) { |
| 53 | + // Volkswagen uses standard CRC8 8H2F/AUTOSAR, but they compute it with |
| 54 | + // a magic variable padding byte tacked onto the end of the payload. |
| 55 | + // https://www.autosar.org/fileadmin/user_upload/standards/classic/4-3/AUTOSAR_SWS_CRCLibrary.pdf |
| 56 | + |
| 57 | + uint8_t *dat = (uint8_t *)&d; |
| 58 | + uint8_t crc = 0xFF; // Standard init value for CRC8 8H2F/AUTOSAR |
| 59 | + |
| 60 | + // CRC the payload first, skipping over the first byte where the CRC lives. |
| 61 | + for (int i = 1; i < l; i++) { |
| 62 | + crc ^= dat[i]; |
| 63 | + crc = crc8_lut_8h2f[crc]; |
| 64 | + } |
| 65 | + |
| 66 | + // Look up and apply the magic final CRC padding byte, which permutes by CAN |
| 67 | + // address, and additionally (for SOME addresses) by the message counter. |
| 68 | + uint8_t counter = dat[1] & 0x0F; |
| 69 | + switch(address) { |
| 70 | + case 0x86: // LWI_01 Steering Angle |
| 71 | + crc ^= (uint8_t[]){0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86}[counter]; |
| 72 | + break; |
| 73 | + case 0x9F: // EPS_01 Electric Power Steering |
| 74 | + crc ^= (uint8_t[]){0xF5,0xF5,0xF5,0xF5,0xF5,0xF5,0xF5,0xF5,0xF5,0xF5,0xF5,0xF5,0xF5,0xF5,0xF5,0xF5}[counter]; |
| 75 | + break; |
| 76 | + case 0xAD: // Getriebe_11 Automatic Gearbox |
| 77 | + crc ^= (uint8_t[]){0x3F,0x69,0x39,0xDC,0x94,0xF9,0x14,0x64,0xD8,0x6A,0x34,0xCE,0xA2,0x55,0xB5,0x2C}[counter]; |
| 78 | + break; |
| 79 | + case 0xFD: // ESP_21 Electronic Stability Program |
| 80 | + crc ^= (uint8_t[]){0xB4,0xEF,0xF8,0x49,0x1E,0xE5,0xC2,0xC0,0x97,0x19,0x3C,0xC9,0xF1,0x98,0xD6,0x61}[counter]; |
| 81 | + break; |
| 82 | + case 0x106: // ESP_05 Electronic Stability Program |
| 83 | + crc ^= (uint8_t[]){0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07}[counter]; |
| 84 | + break; |
| 85 | + case 0x117: // ACC_10 Automatic Cruise Control |
| 86 | + crc ^= (uint8_t[]){0xAC,0xAC,0xAC,0xAC,0xAC,0xAC,0xAC,0xAC,0xAC,0xAC,0xAC,0xAC,0xAC,0xAC,0xAC,0xAC}[counter]; |
| 87 | + break; |
| 88 | + case 0x122: // ACC_06 Automatic Cruise Control |
| 89 | + crc ^= (uint8_t[]){0x37,0x7D,0xF3,0xA9,0x18,0x46,0x6D,0x4D,0x3D,0x71,0x92,0x9C,0xE5,0x32,0x10,0xB9}[counter]; |
| 90 | + break; |
| 91 | + case 0x126: // HCA_01 Heading Control Assist |
| 92 | + crc ^= (uint8_t[]){0xDA,0xDA,0xDA,0xDA,0xDA,0xDA,0xDA,0xDA,0xDA,0xDA,0xDA,0xDA,0xDA,0xDA,0xDA,0xDA}[counter]; |
| 93 | + break; |
| 94 | + case 0x12B: // GRA_ACC_01 Steering wheel controls for ACC |
| 95 | + crc ^= (uint8_t[]){0x6A,0x38,0xB4,0x27,0x22,0xEF,0xE1,0xBB,0xF8,0x80,0x84,0x49,0xC7,0x9E,0x1E,0x2B}[counter]; |
| 96 | + break; |
| 97 | + case 0x187: // EV_Gearshift "Gear" selection data for EVs with no gearbox |
| 98 | + crc ^= (uint8_t[]){0x7F,0xED,0x17,0xC2,0x7C,0xEB,0x44,0x21,0x01,0xFA,0xDB,0x15,0x4A,0x6B,0x23,0x05}[counter]; |
| 99 | + break; |
| 100 | + case 0x30C: // ACC_02 Automatic Cruise Control |
| 101 | + crc ^= (uint8_t[]){0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F}[counter]; |
| 102 | + break; |
| 103 | + case 0x3C0: // Klemmen_Status_01 ignition and starting status |
| 104 | + crc ^= (uint8_t[]){0xC3,0xC3,0xC3,0xC3,0xC3,0xC3,0xC3,0xC3,0xC3,0xC3,0xC3,0xC3,0xC3,0xC3,0xC3,0xC3}[counter]; |
| 105 | + break; |
| 106 | + case 0x65D: // ESP_20 Electronic Stability Program |
| 107 | + crc ^= (uint8_t[]){0xAC,0xB3,0xAB,0xEB,0x7A,0xE1,0x3B,0xF7,0x73,0xBA,0x7C,0x9E,0x06,0x5F,0x02,0xD9}[counter]; |
| 108 | + break; |
| 109 | + default: // As-yet undefined CAN message, CRC check expected to fail |
| 110 | + printf("Attempt to CRC check undefined Volkswagen message 0x%02X\n", address); |
| 111 | + crc ^= (uint8_t[]){0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}[counter]; |
| 112 | + break; |
| 113 | + } |
| 114 | + crc = crc8_lut_8h2f[crc]; |
| 115 | + |
| 116 | + return crc ^ 0xFF; // Return after standard final XOR for CRC8 8H2F/AUTOSAR |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +unsigned int pedal_checksum(uint64_t d, int l) { |
| 121 | + uint8_t crc = 0xFF; |
| 122 | + uint8_t poly = 0xD5; // standard crc8 |
| 123 | + |
| 124 | + d >>= ((8-l)*8); // remove padding |
| 125 | + d >>= 8; // remove checksum |
| 126 | + |
| 127 | + uint8_t *dat = (uint8_t *)&d; |
| 128 | + |
| 129 | + int i, j; |
| 130 | + for (i = 0; i < l - 1; i++) { |
| 131 | + crc ^= dat[i]; |
| 132 | + for (j = 0; j < 8; j++) { |
| 133 | + if ((crc & 0x80) != 0) { |
| 134 | + crc = (uint8_t)((crc << 1) ^ poly); |
| 135 | + } |
| 136 | + else { |
| 137 | + crc <<= 1; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return crc; |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +uint64_t read_u64_be(const uint8_t* v) { |
| 146 | + return (((uint64_t)v[0] << 56) |
| 147 | + | ((uint64_t)v[1] << 48) |
| 148 | + | ((uint64_t)v[2] << 40) |
| 149 | + | ((uint64_t)v[3] << 32) |
| 150 | + | ((uint64_t)v[4] << 24) |
| 151 | + | ((uint64_t)v[5] << 16) |
| 152 | + | ((uint64_t)v[6] << 8) |
| 153 | + | (uint64_t)v[7]); |
| 154 | +} |
| 155 | + |
| 156 | +uint64_t read_u64_le(const uint8_t* v) { |
| 157 | + return ((uint64_t)v[0] |
| 158 | + | ((uint64_t)v[1] << 8) |
| 159 | + | ((uint64_t)v[2] << 16) |
| 160 | + | ((uint64_t)v[3] << 24) |
| 161 | + | ((uint64_t)v[4] << 32) |
| 162 | + | ((uint64_t)v[5] << 40) |
| 163 | + | ((uint64_t)v[6] << 48) |
| 164 | + | ((uint64_t)v[7] << 56)); |
| 165 | +} |
0 commit comments