From e86328a55b0bc94c27616de36ee038c4a92040a0 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Wed, 23 Sep 2020 22:47:52 +0200 Subject: [PATCH] Add parking lights support --- include/phevargs.h | 21 +++++++++++++++++---- main.c | 32 ++++++++++++-------------------- src/phevargs.c | 18 ++++++++++-------- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/include/phevargs.h b/include/phevargs.h index 9b9a38e..03ec7e6 100644 --- a/include/phevargs.h +++ b/include/phevargs.h @@ -11,6 +11,7 @@ #include #define HEADLIGHTS "headlights" +#define PARKING_LIGHTS "parkinglights" #define BATTERY "battery" #define AIRCON "aircon" #define AIRCON_MODE "acmode" @@ -24,7 +25,20 @@ #define WINDSCREEN "windscreen" #define WAIT_FOR_REG_MAX 40 -typedef enum phev_args_commands_t { CMD_UNSET, CMD_MONITOR, CMD_INVALID, CMD_STATUS, CMD_REGISTER, CMD_HEADLIGHTS, CMD_BATTERY, CMD_AIRCON, CMD_AIRCON_MODE, CMD_GET_REG_VAL, CMD_DISPLAY_REG } phev_args_commands_t; +typedef enum phev_args_commands_t { + CMD_UNSET, + CMD_MONITOR, + CMD_INVALID, + CMD_STATUS, + CMD_REGISTER, + CMD_HEADLIGHTS, + CMD_PARKING_LIGHTS, + CMD_BATTERY, + CMD_AIRCON, + CMD_AIRCON_MODE, + CMD_GET_REG_VAL, + CMD_DISPLAY_REG, +} phev_args_commands_t; typedef struct phev_args_opts_t { bool init; @@ -50,7 +64,7 @@ static uint8_t PHEV_ARGS_DEFAULT_MAC[] = {0,0,0,0,0,0}; static const char * phev_args_argp_program_version = "Version\t" VERSION; static const char * phev_args_argp_program_bug_address = "jamie@wattu.com"; static char phev_args_doc[] = "\n\nProgram to control the car via the remote WiFi interface. Requires this device to be connected to the REMOTE**** access point with a valid IP address, which is on the 192.168.8.x subnet.\n\nTHIS PROGRAM COMES WITH NO WARRANTY ANY DAMAGE TO THE CAR OR ANY OTHER EQUIPMENT IS AT THE USERS OWN RISK."; -static char phev_args_args_doc[] = "register\nbattery\naircon [on|off]\nacmode [heat|cool|windscreen] [10|20|30]\nheadlights [on|off]\nmonitor\nget "; +static char phev_args_args_doc[] = "register\nbattery\naircon [on|off]\nacmode [heat|cool|windscreen] [10|20|30]\nheadlights [on|off]\nparkinglights [on|off]\nmonitor\nget "; static struct argp_option phev_args_options[] = { { "mac", 'm', "",0, "MAC address."}, { "host", 'h', "",OPTION_HIDDEN, "IP address of car - defaults to 192.168.8.46."}, @@ -60,5 +74,4 @@ static struct argp_option phev_args_options[] = { }; phev_args_opts_t * phev_args_parse(int argc, char *argv[]); - -#endif \ No newline at end of file +#endif diff --git a/main.c b/main.c index 57f0b2d..42acfe0 100644 --- a/main.c +++ b/main.c @@ -32,22 +32,9 @@ static void operationCallbackNoExit(phevCtx_t *ctx, void *value) { printf("Operation successful\n"); } -static void batteryLevelCallback(phevCtx_t *ctx, void *level) -{ - printf("Battery %d\n", *((int *)level)); - phev_exit(ctx); - exit(0); -} -static void turnonlights(phevCtx_t *ctx) -{ - printf("\n\nTURN ON LIGHTS\n\n"); - ctx->serviceCtx->pipe->currentXOR = 0xd0; - phev_headLights(ctx,true,NULL); -} - static bool d = true; + static int main_eventHandler(phevEvent_t *event) { - phevCtx_t *ctx = event->ctx; phev_args_opts_t *opts = (phev_args_opts_t *)phev_getUserCtx(ctx); @@ -147,20 +134,26 @@ static int main_eventHandler(phevEvent_t *event) { case CMD_HEADLIGHTS: { - printf("Turning %s headlights\n",(opts->operand_on ? "ON" : "OFF")); - phev_headLights(event->ctx,opts->operand_on,operationCallback); + 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")); + 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); + phev_airConMode(event->ctx, opts->operand_mode, opts->operand_time, operationCallback); break; } } @@ -196,9 +189,9 @@ void print_intro() 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); @@ -254,7 +247,6 @@ int main(int argc, char *argv[]) int ret = pthread_create(&main, NULL, main_thread, (void *)ctx); //main_thread((void *) ctx); char ch; - static bool d = true; do { ch = getchar(); diff --git a/src/phevargs.c b/src/phevargs.c index 6b16f7d..afcef43 100644 --- a/src/phevargs.c +++ b/src/phevargs.c @@ -1,12 +1,12 @@ #include #include "phevargs.h" - int phev_args_validate(int arg_num,phev_args_opts_t * opts) { switch (opts->command) { case CMD_HEADLIGHTS: + case CMD_PARKING_LIGHTS: case CMD_AIRCON: { if(arg_num == 2) @@ -60,6 +60,7 @@ int phev_args_validate(int arg_num,phev_args_opts_t * opts) opts->error = true; return 1; } + int phev_args_process_operands(char * arg, int arg_num, phev_args_opts_t * opts) { switch (opts->command) @@ -70,6 +71,7 @@ int phev_args_process_operands(char * arg, int arg_num, phev_args_opts_t * opts) break; } case CMD_HEADLIGHTS: + case CMD_PARKING_LIGHTS: case CMD_AIRCON: { if(arg_num == 1) { @@ -157,13 +159,9 @@ int phev_args_process_operands(char * arg, int arg_num, phev_args_opts_t * opts) break; } } - if(!opts->error) - { - return 0; - } else { - return 1; - } + return opts->error ? 1 : 0; } + int phev_args_process_command(char * arg, int arg_num, phev_args_opts_t * opts) { if(strcmp(arg,REGISTER) == 0 && arg_num ==0) @@ -174,6 +172,10 @@ int phev_args_process_command(char * arg, int arg_num, phev_args_opts_t * opts) { opts->command = CMD_HEADLIGHTS; } + if(strcmp(arg,PARKING_LIGHTS) == 0 && arg_num == 0) + { + opts->command = CMD_PARKING_LIGHTS; + } if(strcmp(arg,BATTERY) == 0 && arg_num == 0) { opts->command = CMD_BATTERY; @@ -196,6 +198,7 @@ int phev_args_process_command(char * arg, int arg_num, phev_args_opts_t * opts) } return 0; } + static error_t phev_args_parse_opt(int key, char *arg, struct argp_state *state) { phev_args_opts_t * opts = state->input; @@ -262,7 +265,6 @@ static error_t phev_args_parse_opt(int key, char *arg, struct argp_state *state) } case ARGP_KEY_END: { - if(opts->error) { opts->command = CMD_INVALID;