Merge branch '1.0-local' into 1.0

This commit is contained in:
xdrm-brackets 2017-02-20 21:58:52 +01:00
commit 03f28390ac
8 changed files with 247 additions and 24 deletions

View File

@ -21,20 +21,111 @@
return 127; return 127;
/* [2] Get system states (global_state)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['states']) || !is_array($arr_r['states']) )
return 127;
/* (2) Reset file */
file_put_contents(STATES_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(STATES_CONF, 'w');
foreach($arr_r['states'] as $state)
$f->fwrite( json_encode([
$state['global_state'],
$state['chips']
]).PHP_EOL );
$f = null;
/* [3] Get chip list (chips)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['chips']) || !is_array($arr_r['chips']) )
return 127;
/* (2) Reset file */
file_put_contents(CHIPS_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(CHIPS_CONF, 'w');
foreach($arr_r['chips'] as $chip)
$f->fwrite( json_encode([
(int) $chip['position'],
$chip['pins'],
$chip['states']
]).PHP_EOL );
$f = null;
/* [4] Get auth list (permissions)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['permissions']) || !is_array($arr_r['permissions']) )
return 127;
/* (2) Reset file */
file_put_contents(AUTH_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(AUTH_CONF, 'w');
foreach($arr_r['permissions'] as $code=>$perm)
$f->fwrite( json_encode([
$code,
(int) $perm['id_user'],
$perm['actions']
]).PHP_EOL );
$f = null;
/* [5] Get action list (with timeout)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['actions']) || !is_array($arr_r['actions']) )
return 127;
/* (2) Reset file */
file_put_contents(ACTIONS_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(ACTIONS_CONF, 'w');
foreach($arr_r['actions'] as $timeout=>$actions)
foreach($actions as $action)
$f->fwrite( json_encode([
(int) $timeout,
(int) $action['id_action'],
$action['previous'],
$action['action']
]).PHP_EOL );
$f = null;
return 0; return 0;
} }
/* [1] Check $1 argument $exec = api_deploy();
=========================================================*/
if( $argc < 2 || !in_array($argv[1], 'init', 'sync' ){
echo 127;
die();
}
echo $exec;
echo api_deploy($argv[1]); die($exec);
?> ?>

View File

@ -128,12 +128,16 @@
$response_arr = json_decode($response, true); $response_arr = json_decode($response, true);
// if cannot, abort // if cannot, abort
if( is_null($response_arr) ) if( is_null($response_arr) ){
slog('Cannot parse HTTP response', 'api:init', 'update');
return 127; return 127;
}
/* (2) Check response error */ /* (2) Check response error */
if( !isset($response_arr['error']) || $response_arr['error'] != 0 ) if( !isset($response_arr['error']) || $response_arr['error'] != 0 ){
slog('API error not on \'Success\'', 'api:init', 'update');
return 127; return 127;
}
/* (3) Writes request to temporary pipe */ /* (3) Writes request to temporary pipe */
@ -150,13 +154,13 @@
return 127; return 127;
} }
return 0; slog('HTTP Response succesfully received', 'api:init', 'update');
/* [8] Deploy received data /* [8] Deploy received data
=========================================================*/ =========================================================*/
/* (1) Manage response deployement */ /* (1) Manage response deployement */
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy init'); $deploy = syscall(SOURCE_DIR.'/lib/api/deploy');
/* (2) Return state */ /* (2) Return state */
return ($deploy === true) ? 0 : 127; return ($deploy === true) ? 0 : 127;
@ -164,7 +168,10 @@
} }
die(api_init());
$exec = api_init();
echo $exec;
die($exec);

View File

@ -114,12 +114,16 @@
$response_arr = json_decode($response, true); $response_arr = json_decode($response, true);
// if cannot, abort // if cannot, abort
if( is_null($response_arr) ) if( is_null($response_arr) ){
slog('Cannot parse HTTP response', 'api:sync', 'update');
return 127; return 127;
}
/* (2) Check response error */ /* (2) Check response error */
if( !isset($response_arr['error']) || $response_arr['error'] != 0 ) if( !isset($response_arr['error']) || $response_arr['error'] != 0 ){
slog('API error not on \'Success\'', 'api:sync', 'update');
return 127; return 127;
}
/* (3) Writes request to temporary pipe */ /* (3) Writes request to temporary pipe */
file_put_contents(TMP_DIR.'/api.response', $response); file_put_contents(TMP_DIR.'/api.response', $response);
@ -131,16 +135,16 @@
$decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr');
if( $decr === false ){ if( $decr === false ){
slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:init','update'); slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:sync','update');
return 127; return 127;
} }
return 0; slog('HTTP Response succesfully received', 'api:sync', 'update');
/* [8] Deploy received data /* [8] Deploy received data
=========================================================*/ =========================================================*/
/* (1) Manage response deployement */ /* (1) Manage response deployement */
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync'); $deploy = syscall(SOURCE_DIR.'/lib/api/deploy');
/* (2) Return state */ /* (2) Return state */
return ($deploy === true) ? 0 : 127; return ($deploy === true) ? 0 : 127;
@ -148,7 +152,9 @@
} }
die(api_request()); $exec = api_request();
echo $exec;
die($exec);

101
lib/chip/source/state.php Executable file
View File

@ -0,0 +1,101 @@
#!/usr/bin/php
<?php
require_once __DIR__.'/../../include/php/const';
function chip_state($p_id_chip, $p_state){
$g_pins = null;
$g_state = null;
/* [1] Cache chips
=========================================================*/
/* (1) Get file handler */
$f_chips = new SplFileObject(CHIPS_CONF, 'r');
/* (2) Parse line by line */
while( !$f_chips->eof() ){
// {1} Try to parse current line //
$parsed = json_decode($f_chips->fgets(), true);
// {2} If cannot parse, go to next //
if( is_null($parsed) )
continue;
// {3} Check if choosen pin //
if( $parsed[0] == $p_id_chip ){
// store pin list
$g_pins = $parsed[1];
/* (3) Check if choosen state */
foreach($parsed[2] as $state=>$pinValues){
if( "$state" === "$p_state" ){
// store state values
$g_state = $pinValues;
break;
}
}
break;
}
}
/* (3) If chip not found -> abort */
if( is_null($g_pins) || is_null($g_state) )
return 127;
/* [2] Set chip to choosen state
=========================================================*/
/* (1) Activate pin + set OUT mode */
foreach($g_pins as $pinNumber)
// if failed -> propagate error
if( syscall(SOURCE_DIR."/gpio/out {$pinNumber}") == false )
return 127;
/* (2) For each pin, set the associated value */
foreach($g_state as $pinIndex=>$pinValue){
// {1} set value to LOW if 0 //
if( $pinValue == 0 )
if( syscall(SOURCE_DIR."/gpio/low ".$g_pins[$pinIndex]) == false )
return 127;
// {2} set value to HIGH if not 0 //
else
if( syscall(SOURCE_DIR."/gpio/high ".$g_pins[$pinIndex]) == false )
return 127;
}
return 0;
}
/* [1] Check argument (chip position, chip state)
=========================================================*/
if( $argc < 3 || !preg_match('@^\d+$@', $argv[1]) || !preg_match('@^[A-Za-z0-9]{1}$@', $argv[2]) ){
echo 127;
die(127);
}
/* [2] Launch main script
=========================================================*/
$exec = chip_state($argv[1], $argv[2]);
echo $exec;
die($exec);
?>

3
lib/chip/state Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/state.php $*;

View File

@ -12,24 +12,32 @@ export SOURCE_DIR="$ROOT_DIR/source";
#export SOURCE_DIR="/home/xdrm-brackets/Desktop/git.xdr/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats"; #export SOURCE_DIR="/home/xdrm-brackets/Desktop/git.xdr/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats";
export TMP_DIR="$ROOT_DIR/tmp"; export TMP_DIR="$ROOT_DIR/tmp";
# CONFIGURATION FILES # CONFIGURATION FILES
export SECRET_CONF="$CONF_DIR/machine.secret"; export SECRET_CONF="$CONF_DIR/machine.secret";
export STATE_CONF="$CONF_DIR/machine.state"; export STATE_CONF="$CONF_DIR/machine.state";
export BRANCH_CONF="$CONF_DIR/machine.branch"; export BRANCH_CONF="$CONF_DIR/machine.branch";
export ID_CONF="$CONF_DIR/machine.id"; export ID_CONF="$CONF_DIR/machine.id";
export URL_CONF="$CONF_DIR/api.url"; export URL_CONF="$CONF_DIR/api.url";
export AUTH_CONF="$CONF_DIR/auth.list";
export WAREHOUSE_TOK="$CONF_DIR/warehouse.token"; export WAREHOUSE_TOK="$CONF_DIR/warehouse.token";
export UNLOCK_CODE="$CONF_DIR/machine.unlock"; export UNLOCK_CODE="$CONF_DIR/machine.unlock";
export AUTH_CONF="$CONF_DIR/auth.list";
export STATES_CONF="$CONF_DIR/states.list";
export CHIPS_CONF="$CONF_DIR/chips.list";
export ACTIONS_CONF="$CONF_DIR/actions.list";
# LOG FILES # LOG FILES
export BOOT_LOG="$LOG_DIR/boot.log"; export BOOT_LOG="$LOG_DIR/boot.log";
export DAEMON_LOG="$LOG_DIR/daemon.log"; export DAEMON_LOG="$LOG_DIR/daemon.log";
export INSTALL_LOG="$LOG_DIR/install-source.log"; export INSTALL_LOG="$LOG_DIR/install-source.log";
export UPDATE_LOG="$LOG_DIR/update.log"; export UPDATE_LOG="$LOG_DIR/update.log";
# DATA FILES # DATA FILES
export ACCESS_DATA="$DATA_DIR/access.data"; export DEFAULT_DATA="$DATA_DIR/mfrc522";
# SETTINGS # SETTINGS
export SECRET_SIZE=250; export SECRET_SIZE=250;

View File

@ -18,10 +18,14 @@
define('BRANCH_CONF', CONF_DIR.'/machine.branch'); define('BRANCH_CONF', CONF_DIR.'/machine.branch');
define('ID_CONF', CONF_DIR.'/machine.id'); define('ID_CONF', CONF_DIR.'/machine.id');
define('URL_CONF', CONF_DIR.'/api.url'); define('URL_CONF', CONF_DIR.'/api.url');
define('AUTH_CONF', CONF_DIR.'/auth.list');
define('WAREHOUSE_TOK', CONF_DIR.'/warehouse.token'); define('WAREHOUSE_TOK', CONF_DIR.'/warehouse.token');
define('UNLOCK_CODE', CONF_DIR.'/machine.unlock'); define('UNLOCK_CODE', CONF_DIR.'/machine.unlock');
define('AUTH_CONF', CONF_DIR.'/auth.list');
define('STATES_CONF', CONF_DIR.'/states.list');
define('CHIPS_CONF', CONF_DIR.'/chips.list');
define('ACTIONS_CONF', CONF_DIR.'/actions.list');
# LOG FILES # LOG FILES
define('BOOT_LOG', LOG_DIR.'/boot.log'); define('BOOT_LOG', LOG_DIR.'/boot.log');
define('DAEMON_LOG', LOG_DIR.'/daemon.log'); define('DAEMON_LOG', LOG_DIR.'/daemon.log');
@ -29,7 +33,7 @@
define('UPDATE_LOG', LOG_DIR.'/update.log'); define('UPDATE_LOG', LOG_DIR.'/update.log');
# DATA FILES # DATA FILES
define('ACCESS_DATA', DATA_DIR.'/access.data'); define('DEFAULT_DATA', DATA_DIR.'/mfrc522');
# SETTINGS # SETTINGS
define('SECRET_SIZE', 250); define('SECRET_SIZE', 250);

View File

@ -50,6 +50,9 @@
=========================================================*/ =========================================================*/
/* [3] Manage action (states)
=========================================================*/
/* [n] Wait for 0.5 s before reading again /* [n] Wait for 0.5 s before reading again
=========================================================*/ =========================================================*/