forked from phev-remote/phevctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
281 lines (258 loc) · 7.31 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define VERSION "0.0.1"
#define LOG_LEVEL LOG_NONE
#include "phev.h"
#include "phevargs.h"
#include "msg_utils.h"
#ifdef MQTT_PAHO
#include "msg_mqtt_paho.h"
#endif
char *remaining_args = NULL, num_remaining_args = 0;
bool bool_value;
uint8_t uint_value;
int wait_for_regs = 0;
static void operationCallback(phevCtx_t *ctx, void *value)
{
printf("Operation successful\n");
phev_exit(ctx);
exit(0);
}
static void operationCallbackNoExit(phevCtx_t *ctx, void *value)
{
printf("Operation successful\n");
}
static int main_eventHandler(phevEvent_t *event)
{
phevCtx_t *ctx = event->ctx;
phev_args_opts_t *opts = (phev_args_opts_t *)phev_getUserCtx(ctx);
switch (event->type)
{
case PHEV_REGISTER_UPDATE:
{
printf("Register %02X\n",event->reg);
for (int i = 0; i < event->length; i++)
{
printf("%02X ", event->data[i]);
}
printf("\n");
if (opts->verbose)
{
if (event->reg == KO_WF_DATE_INFO_SYNC_EVR)
{
printf("Date sync 20%d-%d-%d %d:%0d:%0d\n", event->data[0], event->data[1], event->data[2], event->data[3], event->data[4], event->data[5]);
}
}
switch (opts->command)
{
case CMD_BATTERY:
{
if (event->reg == KO_WF_BATT_LEVEL_INFO_REP_EVR)
{
int batt = phev_batteryLevel(ctx);
if (batt < 0)
{
return 0;
}
printf("Battery level %d\n", batt);
exit(0);
}
break;
}
case CMD_DISPLAY_REG:
{
printf("Register : %d Data :", event->reg);
for (int i = 0; i < event->length; i++)
{
printf("%02X ", event->data[i]);
}
printf("\n");
break;
}
case CMD_GET_REG_VAL:
{
phevData_t *reg = phev_getRegister(ctx, opts->reg_operand);
if (reg == NULL)
{
if (wait_for_regs > WAIT_FOR_REG_MAX)
{
printf("REGISTER TIMEOUT\n");
exit(0);
}
wait_for_regs++;
return 0;
}
printf("Get register %d : ", opts->reg_operand);
for (int i = 0; i < reg->length; i++)
{
printf("%02X ", reg->data[i]);
}
printf("\n");
exit(0);
break;
}
}
return 0;
}
case PHEV_REGISTRATION_COMPLETE:
{
printf("Registration Complete\n");
exit(0);
}
case PHEV_CONNECTED:
{
return 0;
}
case PHEV_STARTED:
{
printf("Connected to car successfully\n");
return 0;
}
case PHEV_VIN:
{
if (opts->verbose)
{
printf("VIN number : %s\n", event->data);
}
if (opts->command != CMD_UNSET && opts->command != CMD_INVALID)
{
switch (opts->command)
{
case CMD_HEADLIGHTS:
{
printf("Turning %s headlights\n", opts->operand_on ? "ON" : "OFF");
phev_headLights(event->ctx, opts->operand_on, operationCallback);
break;
}
case CMD_PARKING_LIGHTS:
{
printf("Turning %s parking lights\n", opts->operand_on ? "ON" : "OFF");
phev_parkingLights(event->ctx, opts->operand_on, operationCallback);
break;
}
case CMD_AIRCON:
{
printf("Turning air conditioning %s\n", opts->operand_on ? "ON" : "OFF");
phev_airCon(event->ctx, opts->operand_on, operationCallback);
break;
}
case CMD_AIRCON_MODE:
{
printf("Switching air conditioning mode to %d for %d mins\n", opts->operand_mode, opts->operand_time);
phev_airConMode(event->ctx, opts->operand_mode, opts->operand_time, operationCallback);
break;
}
}
}
return 0;
}
case PHEV_ECU_VERSION:
{
if (opts->verbose)
{
printf("ECU Version : %s\n", event->data);
}
return 0;
}
case PHEV_REGISTER_UPDATE_ACK:
if(opts->verbose)
{
printf("Register %d Acknowledged\n",event->reg);
}
return 0;
}
return 0;
}
void *main_thread(void *ctx)
{
phev_start((phevCtx_t *)ctx);
}
void print_intro()
{
printf("Mitsubishi Outlander PHEV Remote CLI - ");
printf("Designed and coded by Jamie Nuttall 2020\nMIT License\n\n");
printf("Type 'x' then enter to quit.\n");
}
int main(int argc, char *argv[])
{
phevCtx_t *ctx = NULL;
phev_args_opts_t *opts = phev_args_parse(argc, argv);
if (opts == NULL)
{
printf("ERROR Options could not be parsed");
exit(1);
}
if (opts->error)
{
printf("ERROR %s\n", opts->error_message);
exit(1);
}
phevSettings_t settings = {
.host = opts->host,
.mac = opts->mac,
.port = opts->port,
.registerDevice = (opts->command == CMD_REGISTER ? true : false),
.handler = main_eventHandler,
.ctx = (void *)opts,
};
print_intro();
if (opts->command == CMD_REGISTER)
{
printf("Registering device\n");
printf("Host : %s\nPort : %d\nMAC : %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", opts->host, opts->port, opts->mac[0], opts->mac[1], opts->mac[2], opts->mac[3], opts->mac[4], opts->mac[5]);
ctx = phev_registerDevice(settings);
}
else
{
if (opts->verbose)
{
printf("Host : %s\nPort : %d\nMAC : %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", opts->host, opts->port, opts->mac[0], opts->mac[1], opts->mac[2], opts->mac[3], opts->mac[4], opts->mac[5]);
}
ctx = phev_init(settings);
}
pthread_t main;
if (opts->command == CMD_UNSET)
{
printf("No command exiting.\n");
exit(0);
}
if (opts->command == CMD_INVALID)
{
printf("Error %s.\n", opts->error_message);
exit(0);
}
int ret = pthread_create(&main, NULL, main_thread, (void *)ctx);
//main_thread((void *) ctx);
char ch;
do
{
ch = getchar();
switch(ch)
{
case 'r':
{
printf("Disconnecting\n");
phev_disconnect(ctx);
break;
}
case 'l':
{
printf("Lights on\n");
phev_headLights(ctx,true,operationCallbackNoExit);
break;
}
case 'a':
{
printf("Aircon on\n");
phev_airCon(ctx, true, operationCallbackNoExit);
}
}
} while (ch != 'x' && phev_running(ctx));
phev_disconnect(ctx);
printf("Exiting\n");
exit(0);
}