Bugfixes
[rtl-433.git] / src / devices / schraeder.c
1 /* Schraeder TPMS protocol
2  *
3  * Copyright © 2016 Benjamin Larsson
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10
11 /**
12  * Packet payload: 8,5 bytes, 17 nibbles
13  *
14  *           01 23 45 67 89 AB CD EF 0
15  * [00] {68} 7f 67 03 a3 8b 20 04 94 9
16  *           SP UU UI II II IU UU UC C
17  *
18  * S = sync
19  * P = preamble (0xf)
20  * U = unknown
21  * I = id
22  * C = CRC8 from nibble 1 to E
23  */
24
25 #include "rtl_433.h"
26 #include "pulse_demod.h"
27 #include "util.h"
28
29
30 static int schraeder_callback(bitbuffer_t *bitbuffer) {
31         char time_str[LOCAL_TIME_BUFLEN];
32         bitrow_t *bb = bitbuffer->bb;
33         uint32_t serial_id = 0;
34         data_t *data;
35         char hexid[20] = {0};
36         uint8_t work_buffer[9];
37         int i;
38
39         /* Reject wrong amount of bits */
40         if ( bitbuffer->bits_per_row[0] != 68)
41                 return 0;
42
43         /* shift the buffer 4 bits for the crc8 calculation */
44         for (i=0 ; i<8 ; i++)
45                 work_buffer[i] = (bb[0][i]&0x0F)<<4 | (bb[0][i+1]&0xF0) >> 4;
46
47         /* Calculate the crc */
48         if (work_buffer[7] != crc8(work_buffer, 7, 0x07, 0xf0)) {
49                 return 0;
50         }
51
52         local_time_str(0, time_str);
53
54         /* Get serial number id */
55         serial_id = (bb[0][2]&0x0F) << 20 | bb[0][3] << 12 | bb[0][4] << 4 | (bb[0][5]&0xF0) >> 4;
56         sprintf(hexid, "%X", serial_id);
57
58         if (debug_output >= 1) {
59                 fprintf(stderr, "Schraeder TPMS decoder\n");
60                 bitbuffer_print(bitbuffer);
61                 fprintf(stderr, "id = 0x%X\n", serial_id);
62                 fprintf(stderr, "CRC = %x\n", crc8(work_buffer, 7, 0x07, 0xf0));
63         }
64
65         data = data_make("time", "", DATA_STRING, time_str,
66                                         "model", "", DATA_STRING, "Schraeder",
67                                         "type", "", DATA_STRING, "TPMS",
68                                         "id", "ID", DATA_STRING, hexid,
69                                         "crc", "", DATA_STRING, "OK",
70                                         NULL);
71
72         data_acquired_handler(data);
73         return 0;
74 }
75
76 static char *output_fields[] = {
77         "time",
78         "model",
79         "id",
80         "flags",
81         "pressure",
82         "temperature_C",
83         "depth",
84         NULL
85 };
86
87 r_device schraeder = {
88         .name                   = "Schraeder TPMS",
89         .modulation             = OOK_PULSE_MANCHESTER_ZEROBIT,
90         .short_limit    = 30*4,
91         .long_limit     = 0,
92         .reset_limit    = 120*4,
93         .json_callback  = &schraeder_callback,
94         .disabled               = 0,
95         .fields                 = output_fields,
96 };