-
Notifications
You must be signed in to change notification settings - Fork 68
/
rollthecode.c
113 lines (89 loc) · 2.74 KB
/
rollthecode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright 2017 Tom Wimmenhove<[email protected]>
*
* This file is part of Subarufobrob
*
* Subarufobrob is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Subarufobrob is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Subarufobrob. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "hex.h"
#include "protocol.h"
int main(int argc, char** argv)
{
if (argc < 2)
{
fprintf(stderr, "usage, %s <command> [increment]\n\tcommand can be \"lock\", \"unlock\", \"trunk\" or \"panic\"\nincrement increments the rolling code by a certain amount. Defaults to 1\n", argv[0]);
return -1;
}
int inc = 1;
if (argc > 2)
{
inc = atoi(argv[2]);
}
int fd = open("latestcode.txt", O_RDONLY);
if (fd == -1)
{
perror("Could not open latestcode.txt");
return -1;
}
char hexString[21];
if ((read(fd, hexString, 20)) < 20)
{
fprintf(stderr, "Couldn't read a full hex string from latestcode.txt\n");
return -1;
}
hexString[20] = 0; // terminator
int forceCommand;
if (strcmp(argv[1], "lock") == 0) forceCommand = COMMAND_LOCK;
else if (strcmp(argv[1], "unlock") == 0) forceCommand = COMMAND_UNLOCK;
else if (strcmp(argv[1], "trunk") == 0) forceCommand = COMMAND_TRUNK;
else if (strcmp(argv[1], "panic") == 0) forceCommand = COMMAND_PANIC;
else
{
fprintf(stderr, "Dunno what %s means, use \"lock\", \"unlock\" or \"trunk\"\n", argv[2]);
return -1;
}
int n = 0;
if (strlen(hexString) != 20)
{
fprintf(stderr, "hex string must be 20 digits (10 byte) long\n");
return -1;
}
fprintf(stderr, "Current code: %s\n", hexString);
unsigned char decoded[10];
dehexify(decoded, hexString, 10);
unsigned char mycsum = calcCSum(decoded);;
unsigned char csum = getCSum(decoded);
int rollingCode = getCode(decoded);
/* next rolling code */
setCode(decoded, rollingCode + inc);
/* Force command */
setCommand(decoded, forceCommand);
/* Fix CSUM */
setCSum(decoded, calcCSum(decoded));
/* Output the new code */
unsigned char newHexString[21];
hexify(newHexString, decoded, 10);
fprintf(stderr, "New code : %s\n", newHexString);
/* Save to file */
FILE* f = fopen("latestcode.txt", "w");
fprintf(f, "%s\n", newHexString);
fclose(f);
return 0;
}