Skip to content

Commit

Permalink
Merge pull request phev-remote#19 from bphermansson/numerical_output
Browse files Browse the repository at this point in the history
Numerical output
  • Loading branch information
papawattu authored Aug 29, 2022
2 parents 2653ac8 + 38093b5 commit b3d49f2
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ build
.vscode
*.txt
CMakeLists.txt
BUILD.md

2 changes: 2 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Usage: phevctl [OPTION...] register
or: phevctl [OPTION...] chargestatus
or: phevctl [OPTION...] lockstatus
or: phevctl [OPTION...] hvac
or: phevctl [OPTION...] hvacoperating
or: phevctl [OPTION...] hvacmode
or: phevctl [OPTION...] remaningchargestatus
or: phevctl [OPTION...] update
or: phevctl [OPTION...] aircon [on|off]
Expand Down
8 changes: 7 additions & 1 deletion include/phevargs.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#define ISLOCKED "lockstatus"
#define CHARGING_STATUS "chargestatus"
#define HVAC_STATUS "hvac"
#define HVAC_STATUS_OPERATING "hvacoperating"
#define HVAC_STATUS_MODE "hvacmode"
#define UPDATE "update"
#define REMAINING_CHARGING_STATUS "remaningchargestatus"
#define AIRCON "aircon"
Expand Down Expand Up @@ -43,6 +45,8 @@ typedef enum phev_args_commands_t {
CMD_ISLOCKED,
CMD_CHARGING_STATUS,
CMD_HVAC_STATUS,
CMD_HVAC_STATUS_OPERATING,
CMD_HVAC_STATUS_MODE,
CMD_REMAINING_CHARGING_STATUS,
CMD_AIRCON,
CMD_PANIC,
Expand All @@ -62,6 +66,7 @@ typedef struct phev_args_opts_t {
char * topic;
char * command_topic;
bool verbose;
bool numerical;
int carModel;
bool operand_on;
uint8_t operand_mode;
Expand All @@ -78,13 +83,14 @@ 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 = "[email protected]";
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\nchargestatus\nlockstatus\nhvac\nremaningchargestatus\nupdate\naircon [on|off]\nacmode [heat|cool|windscreen] [10|20|30]\nheadlights [on|off]\nparkinglights [on|off]\nmonitor\nget <register>";
static char phev_args_args_doc[] = "register\nbattery\nchargestatus\nlockstatus\nhvac\nhvacoperating\nhvacmode\nremaningchargestatus\nupdate\naircon [on|off]\nacmode [heat|cool|windscreen] [10|20|30]\nheadlights [on|off]\nparkinglights [on|off]\nmonitor\nget <register>";
static struct argp_option phev_args_options[] = {
{ "mac", 'm', "<MAC ADDRESS>",0, "MAC address."},
{ "car-model", 'c', "<YEAR>",0, "Model Year."},
{ "host", 'h', "<HOST NAME>",OPTION_HIDDEN, "IP address of car - defaults to 192.168.8.46."},
{ "port", 'p', "<PORT NUMBER>",OPTION_HIDDEN, "Port to use - defaults to 8080"},
{ "verbose",'v',0,0,"Verbose output"},
{ "numerical",'n',0,0,"Numerical output"},
{ 0 }
};
phev_args_opts_t * phev_args_parse(int argc, char *argv[]);
Expand Down
164 changes: 142 additions & 22 deletions main.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,29 @@ int wait_for_regs = 0;

static void operationCallback(phevCtx_t *ctx, void *value)
{
printf("Operation successful\n");
phev_args_opts_t *opts = (phev_args_opts_t *)phev_getUserCtx(ctx);
if (!opts->numerical)
{
printf("Operation successful\n");
}
else
{
printf("0");
}
phev_exit(ctx);
exit(0);
}
static void operationCallbackNoExit(phevCtx_t *ctx, void *value)
{
printf("Operation successful\n");
{
phev_args_opts_t *opts = (phev_args_opts_t *)phev_getUserCtx(ctx);
if (!opts->numerical)
{
printf("Operation successful\n");
}
else
{
printf("0");
}
}

static int main_eventHandler(phevEvent_t *event)
Expand Down Expand Up @@ -70,7 +86,14 @@ static int main_eventHandler(phevEvent_t *event)
{
return 0;
}
printf("Battery level %d\n", batt);
if (opts->numerical)
{
printf("%d", batt);
}
else {
printf("Battery level %d\n", batt);
}

exit(0);
}
break;
Expand All @@ -80,13 +103,29 @@ static int main_eventHandler(phevEvent_t *event)
if (event->reg == KO_WF_DOOR_STATUS_INFO_REP_EVR)
{
int islocked = phev_isLocked(ctx);
if (islocked < 0)
if (islocked < 0 && !opts->numerical)
{
printf("Doors in UNKNOWN STATE\n");
}
else if (islocked < 0 && opts->numerical)
{
printf("-1");
}
else if (islocked == 1 && !opts->numerical)
{
printf("Doors in UNKNOWN STATE\n");
} else if (islocked == 1){
printf("Doors are Locked\n");
} else{
printf("Doors are UnLocked\n");
printf("Doors are Locked\n");
}
else if (islocked == 1 && opts->numerical)
{
printf("1");
}
else if (islocked == 0 && !opts->numerical)
{
printf("Doors are UnLocked\n");
}
else
{
printf("0");
}
exit(0);
}
Expand All @@ -101,10 +140,19 @@ static int main_eventHandler(phevEvent_t *event)
{
return 0;
}
if (chargeStatus == 1)
{
printf("Charging...\n");
} else {
if (chargeStatus == 1 && !opts->numerical)
{
printf("Charging\n");
}
else if (chargeStatus == 1 && opts->numerical)
{
printf("1");
}
else if (chargeStatus == 0 && opts->numerical)
{
printf("0");
}
else {
printf("Not charging\n");
}
exit(0);
Expand All @@ -115,13 +163,50 @@ static int main_eventHandler(phevEvent_t *event)
{
if (event->reg == KO_WF_TM_AC_STAT_INFO_REP_EVR)
{
printf("HVAC Status\n");
phevServiceHVAC_t * ph = phev_HVACStatus(ctx);
printf("Operating:%d\n", ph->operating);
printf("mode:%d\n", ph->mode);
exit(0);
}
break;
}
case CMD_HVAC_STATUS_OPERATING:
{
if (event->reg == KO_WF_TM_AC_STAT_INFO_REP_EVR)
{
phevServiceHVAC_t * ph = phev_HVACStatus(ctx);
if (!opts->numerical)
{
printf("HVAC Status Operating\n");
printf("Operating:%d\n", ph->operating);
}
else
{
printf("%d", ph->operating);
}
exit(0);
}
break;
}
case CMD_HVAC_STATUS_MODE:
{
if (event->reg == KO_WF_TM_AC_STAT_INFO_REP_EVR)
{
phevServiceHVAC_t * ph = phev_HVACStatus(ctx);
if (!opts->numerical)
{
printf("HVAC Status Mode\n");
printf("mode:%d\n", ph->mode);
}
else
{
printf("%d", ph->mode);
}
exit(0);
}
break;
}
case CMD_REMAINING_CHARGING_STATUS:
{
if (event->reg == KO_WF_OBCHG_OK_ON_INFO_REP_EVR)
Expand All @@ -131,7 +216,14 @@ static int main_eventHandler(phevEvent_t *event)
{
return 0;
}
printf("Remaining %d\n", remainingChargeStatus);
if (!opts->numerical)
{
printf("Remaining %d\n", remainingChargeStatus);
}
else
{
printf("%d", remainingChargeStatus);
}
exit(0);
}
break;
Expand Down Expand Up @@ -159,7 +251,11 @@ static int main_eventHandler(phevEvent_t *event)
wait_for_regs++;
return 0;
}
printf("Get register %d : ", opts->reg_operand);
if (!opts->numerical)
{
printf("Get register %d : ", opts->reg_operand);
}

for (int i = 0; i < reg->length; i++)
{
printf("%02X ", reg->data[i]);
Expand Down Expand Up @@ -198,31 +294,46 @@ static int main_eventHandler(phevEvent_t *event)
{
case CMD_HEADLIGHTS:
{
printf("Turning %s headlights\n", opts->operand_on ? "ON" : "OFF");
if (!opts->numerical)
{
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");
if (!opts->numerical)
{
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");
if (!opts->numerical)
{
printf("Turning air conditioning %s\n", opts->operand_on ? "ON" : "OFF");
}
phev_airCon(event->ctx, opts->operand_on, operationCallback);
break;
}
case CMD_UPDATE:
{
printf("Update All\n");
if (!opts->numerical)
{
printf("Update All\n");
}
phev_updateAll(event->ctx, operationCallback);
break;
}
case CMD_AIRCON_MODE:
{
printf("Switching air conditioning mode to %d for %d mins\n", opts->operand_mode, opts->operand_time);
if (!opts->numerical)
{
printf("Switching air conditioning mode to %d for %d mins\n", opts->operand_mode, opts->operand_time);
}
if (opts->verbose)
{
printf("Car Model: %d\n", opts->carModel);
Expand All @@ -243,6 +354,10 @@ static int main_eventHandler(phevEvent_t *event)
}
case PHEV_ECU_VERSION:
{
if (!opts->numerical)
{

}
if (opts->verbose)
{
printf("ECU Version : %s\n", event->data);
Expand Down Expand Up @@ -291,16 +406,21 @@ int main(int argc, char *argv[])
printf("ERROR %s\n", opts->error_message);
exit(1);
}

phevSettings_t settings = {
.host = opts->host,
.mac = opts->mac,
.port = opts->port,
.numerical = opts->numerical,
.registerDevice = (opts->command == CMD_REGISTER ? true : false),
.handler = main_eventHandler,
.ctx = (void *)opts,
};

print_intro();
if (!opts->numerical)
{
print_intro();
}

if (opts->command == CMD_REGISTER)
{
Expand Down
31 changes: 30 additions & 1 deletion src/phevargs.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ int phev_args_validate(int arg_num,phev_args_opts_t * opts)
}
break;
}
case CMD_HVAC_STATUS_OPERATING:
{
if(arg_num == 1)
{
return 0;
}
break;
}
case CMD_HVAC_STATUS_MODE:
{
if(arg_num == 1)
{
return 0;
}
break;
}
case CMD_REMAINING_CHARGING_STATUS:
{
if(arg_num == 1)
Expand Down Expand Up @@ -185,6 +201,8 @@ int phev_args_process_operands(char * arg, int arg_num, phev_args_opts_t * opts)
case CMD_MONITOR:
case CMD_CHARGING_STATUS:
case CMD_HVAC_STATUS:
case CMD_HVAC_STATUS_OPERATING:
case CMD_HVAC_STATUS_MODE:
case CMD_ISLOCKED:
case CMD_REMAINING_CHARGING_STATUS:
case CMD_BATTERY:
Expand Down Expand Up @@ -229,6 +247,14 @@ int phev_args_process_command(char * arg, int arg_num, phev_args_opts_t * opts)
{
opts->command = CMD_HVAC_STATUS;
}
if(strcmp(arg,HVAC_STATUS_OPERATING) == 0 && arg_num == 0)
{
opts->command = CMD_HVAC_STATUS_OPERATING;
}
if(strcmp(arg,HVAC_STATUS_MODE) == 0 && arg_num == 0)
{
opts->command = CMD_HVAC_STATUS_MODE;
}
if(strcmp(arg,AIRCON) == 0 && arg_num == 0)
{
opts->command = CMD_AIRCON;
Expand Down Expand Up @@ -316,6 +342,10 @@ static error_t phev_args_parse_opt(int key, char *arg, struct argp_state *state)
opts->verbose = true;
break;
}
case 'n': {
opts->numerical = true;
break;
}
case 'c': {
opts->carModel = atoi(arg);
break;
Expand Down Expand Up @@ -363,7 +393,6 @@ static struct argp phev_args_argp = { phev_args_options, phev_args_parse_opt, ph
phev_args_opts_t * phev_args_parse(int argc, char *argv[])
{
phev_args_opts_t * arguments = malloc(sizeof(phev_args_opts_t));

arguments->host = strdup("192.168.8.46");
arguments->uri = strdup("tcp://localhost:1883");
arguments->mac = PHEV_ARGS_DEFAULT_MAC;
Expand Down

0 comments on commit b3d49f2

Please sign in to comment.