Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calibike autolock #776

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions comm/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include "password.h"

// Settings
#define PRINT_BUFFER_SIZE 400
Expand Down Expand Up @@ -697,6 +698,7 @@ void commands_process_packet(unsigned char *data, unsigned int len,

case COMM_ALIVE:
SHUTDOWN_RESET();
password_timeout_reset();
timeout_reset();
break;

Expand Down
28 changes: 27 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
#ifdef USE_LISPBM
#include "lispif.h"
#endif

#include "password.h"
/*
* HW resources used:
*
Expand Down Expand Up @@ -205,6 +205,32 @@ static THD_FUNCTION(periodic_thread, arg) {

HW_TRIM_HSI(); // Compensate HSI for temperature

static volatile uint16_t time_to_print_ms = 0;

if( time_to_print_ms++ > 1000 ){
time_to_print_ms = 0;
if(password_get_system_locked_flag()){
commands_printf("system locked by user password");
}

if(password_get_system_connection_alive()){
password_set_system_connection_alive(false);
commands_printf("system alive received");
}
}

static volatile float current_now_filtered = 0.0;
UTILS_LP_FAST(current_now_filtered,mc_interface_get_tot_current(),0.1);

if((!password_get_system_locked_flag()) &&
password_get_system_enable_flag() ){
if( fabsf(current_now_filtered) < 0.2){
password_timeout_increment(10);//argument should match the timer loop time
}else{
password_timeout_reset();//if current is higher than threshold, we reinit timer. this prevents that a mobile disconnect issue can lock the system while the bike is still being used.
}
}

chThdSleepMilliseconds(10);
}
}
Expand Down
1 change: 1 addition & 0 deletions make/fw.mk
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ CSRC = $(STARTUPSRC) \
main.c \
irq_handlers.c \
terminal.c \
password.c \
conf_general.c \
timeout.c \
flash_helper.c \
Expand Down
322 changes: 322 additions & 0 deletions password.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
/*
* password.c
*
* Created on: Nov 11, 2019
* Author: motorcontrol
*/

#include "password.h"
#include "commands.h"
#include "terminal.h"
#include "string.h"
#include "stdio.h"
#include "conf_general.h"

#define EEPROM_ADDR_USER_PASSWORD 1
static volatile uint8_t user_password[40];
static volatile uint8_t user_password_read[10];

#define EEPROM_ADDR_PASSWORD_SYSTEM_ENABLE (EEPROM_ADDR_USER_PASSWORD + 2)
static volatile uint8_t system_enable_argument[40];
static volatile uint8_t pass_system_enable[10];//0 is false, 1 is true

static volatile bool system_locked = true;//this will be true until the user password is entered
static volatile bool password_is_erased = false;//this will be false if there is a valid password saved, or true if a new firmware was loaded, that erase the password
static volatile bool system_connection_alive = false;
static volatile bool password_system_enable = false; //this flag enables / disables the password system. it is set by the "ul <password> <enable/disable> command.
static volatile uint32_t password_timeout_counter = 0;
static volatile uint32_t password_timeout_limit = 0;

static void terminal_cmd_enter_user_password(int argc, const char **argv) {
(void)argc;
(void)argv;

if(password_is_erased){
//initialize password to Calibike
password_is_erased = 0;
strcpy(user_password,"Calibike");
// Store data in eeprom
conf_general_store_eeprom_var_hw((eeprom_var*)user_password, EEPROM_ADDR_USER_PASSWORD);
conf_general_store_eeprom_var_hw((eeprom_var*)(user_password+4), EEPROM_ADDR_USER_PASSWORD+1);

//initialize password system enable to false = 0
uint8_t index;
for( index = 0; index<4 ; index++)
{
pass_system_enable[index] = 0;
}
conf_general_store_eeprom_var_hw((eeprom_var*)(pass_system_enable), EEPROM_ADDR_PASSWORD_SYSTEM_ENABLE);
}

if( argc == 3 ) {
sscanf(argv[1], "%s", user_password);
sscanf(argv[2], "%s", system_enable_argument);

uint8_t pass_length = strlen((const char*)user_password);

if( pass_length != 8){
commands_printf("wrong password, it needs to be 8 characters long");
}else{

if( (strncmp(system_enable_argument, "enable",6) == 0) ||
( strncmp(system_enable_argument, "disable",7) == 0)
)
{
// Read stored password in eeprom
conf_general_read_eeprom_var_hw((eeprom_var*)user_password_read,EEPROM_ADDR_USER_PASSWORD);
conf_general_read_eeprom_var_hw((eeprom_var*)(user_password_read+4),EEPROM_ADDR_USER_PASSWORD+1);

if( strncmp(user_password, user_password_read,8) == 0){
system_locked = false;
commands_printf("good password --> system unlocked");
password_timeout_configure(300000);//5 minutes in msec

if( strncmp(system_enable_argument, "enable",6) == 0){
password_system_enable = true;
uint8_t index;
for( index = 0; index<4 ; index++)
{
pass_system_enable[index] = 1;
}
commands_printf("password system is enabled");
}else{
password_system_enable = false;
uint8_t index;
for( index = 0; index<4 ; index++)
{
pass_system_enable[index] = 0;
}
commands_printf("password system is disabled");
}

conf_general_store_eeprom_var_hw((eeprom_var*)(pass_system_enable), EEPROM_ADDR_PASSWORD_SYSTEM_ENABLE);

}else{
commands_printf("wrong password, it does not match current password");
}
}
else
{
commands_printf("wrong command! second argument should be \"enable\" or \"disable\" ");
}

}

}
else {

if( argc == 2 ) {
sscanf(argv[1], "%s", user_password);

uint8_t pass_length = strlen((const char*)user_password);

if( pass_length != 8){
commands_printf("wrong password, it needs to be 8 characters long");
}else{

// Read stored password in eeprom
conf_general_read_eeprom_var_hw((eeprom_var*)user_password_read,EEPROM_ADDR_USER_PASSWORD);
conf_general_read_eeprom_var_hw((eeprom_var*)(user_password_read+4),EEPROM_ADDR_USER_PASSWORD+1);

if( strncmp(user_password, user_password_read,8) == 0){
system_locked = false;
commands_printf("good password --> system unlocked");
password_timeout_configure(300000);//5 minutes in msec

}else{
commands_printf("wrong password, it does not match current password");
}
}
}
else
{
commands_printf("wrong command, please use 1 argument like \"ul <password>\" or 2 arguments such as \"ul <password> <enable/disable>");
}

}

commands_printf(" ");
return;
}

static void terminal_cmd_new_user_password(int argc, const char **argv) {
(void)argc;
(void)argv;

if( password_system_enable )
{
if( system_locked ){
commands_printf("system is locked, first unlock it, then you will be able to load a new password.");
}else{
if( argc == 2 ) {

sscanf(argv[1], "%s", user_password);

uint8_t pass_length = strlen((const char*)user_password);

if( pass_length != 8){
commands_printf("wrong password, it needs to be 8 characters long");
}else{
commands_printf("good password, User password will change to: %s", user_password);

// Store data in eeprom
conf_general_store_eeprom_var_hw((eeprom_var*)user_password, EEPROM_ADDR_USER_PASSWORD);
conf_general_store_eeprom_var_hw((eeprom_var*)(user_password+4), EEPROM_ADDR_USER_PASSWORD+1);

//read back written data
conf_general_read_eeprom_var_hw((eeprom_var*)user_password_read,EEPROM_ADDR_USER_PASSWORD);
conf_general_read_eeprom_var_hw((eeprom_var*)(user_password_read+4),EEPROM_ADDR_USER_PASSWORD+1);

commands_printf("password saved:%s", user_password_read);
}

}
else {
commands_printf("1 argument required. For example: new_user_password Calibike");
commands_printf(" ");
}
}
}
else
{
commands_printf("password system is disabled, you need first to enable it by the \"ul <password> enable\" command." );
}

commands_printf(" ");
return;
}

static void terminal_cmd_lock_system(int argc, const char **argv)
{
if( password_system_enable )
{
system_locked = true;
commands_printf("system has been locked\r\n");
}
else
{
commands_printf("password system is disabled, you need first to enable it by the \"ul <password> enable\" command." );
}
return;
}

void password_init(void){
// Register terminal callbacks

terminal_register_command_callback(
"ul",
"Unlocks system, and enables or disables password settings , command: unlock <password> <enable/disable>",
0,
terminal_cmd_enter_user_password);

terminal_register_command_callback(
"Ul",
"same as ul",
0,
terminal_cmd_enter_user_password);

terminal_register_command_callback(
"sp",
"Sets a new user password for lock function, that must be 8 characters long, example: sp <password>",
0,
terminal_cmd_new_user_password);

terminal_register_command_callback(
"Sp",
"same as sp",
0,
terminal_cmd_new_user_password);

terminal_register_command_callback(
"lk",
"locks system with password set in memory",
0,
terminal_cmd_lock_system);

terminal_register_command_callback(
"Lk",
"same as lk",
0,
terminal_cmd_lock_system);

// check if flash was erased during a firmware upgrade, then initialize the password to Calibike
conf_general_read_eeprom_var_hw((eeprom_var*)user_password_read,EEPROM_ADDR_USER_PASSWORD);
conf_general_read_eeprom_var_hw((eeprom_var*)(user_password_read+4),EEPROM_ADDR_USER_PASSWORD+1);

conf_general_read_eeprom_var_hw((eeprom_var*)pass_system_enable,EEPROM_ADDR_PASSWORD_SYSTEM_ENABLE);

if( (user_password_read[0] == 0) &&
(user_password_read[1] == 0) &&
(user_password_read[2] == 0) &&
(user_password_read[3] == 0) &&
(user_password_read[4] == 0) &&
(user_password_read[5] == 0) &&
(user_password_read[6] == 0) &&
(user_password_read[7] == 0) )
{
//this means flash memory was just programmed, and we need to set default settings, password erased true, and password enable false and system unlocked
password_is_erased = true;
password_system_enable = false;
system_locked = false;
}else
{
if(pass_system_enable[0] == 0)
{
password_system_enable = false;
system_locked = false;
}
else
{
password_system_enable = true;
system_locked = true;
}
}

}

bool password_get_system_locked_flag(void){
return system_locked;
}

void password_set_system_locked_flag( bool value){
system_locked = value;
}

bool password_get_system_enable_flag(void){
return password_system_enable;
}

void password_set_system_enable_flag( bool value){
password_system_enable = value;
}

bool password_get_system_connection_alive(void){
return system_connection_alive;
}

void password_set_system_connection_alive(bool value){
system_connection_alive = value;
}

void password_timeout_deinit(void){
password_timeout_limit = 0;
password_timeout_counter = 0;
}

void password_timeout_configure( uint32_t timeout_msec ){
password_timeout_limit = timeout_msec;
password_timeout_counter = 0;
}

void password_timeout_reset(void){
password_timeout_counter = 0;
}

void password_timeout_increment(uint16_t delta_msec){
if( password_timeout_limit > 0 ){
password_timeout_counter += delta_msec;//this is called from main timer that has a 10 msec tick
if( password_timeout_counter > password_timeout_limit ){
system_locked = true;
}
}
}
Loading