From ee177181f87c4ce601e05512e31140f2cfd30162 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 20 Feb 2017 21:22:42 +0100 Subject: [PATCH 1/2] [Done] Deploy management + updated configuration files (fetched by init/sync) --- lib/api/source/deploy.php | 109 +++++++++++++++++++++++++++++++++--- lib/api/source/init.php | 17 ++++-- lib/api/source/sync.php | 18 ++++-- lib/include/bash/const | 12 +++- lib/include/php/const | 8 ++- lib/mfrc522/source/loop.php | 3 + 6 files changed, 143 insertions(+), 24 deletions(-) diff --git a/lib/api/source/deploy.php b/lib/api/source/deploy.php index f2f4a6e..eb12000 100755 --- a/lib/api/source/deploy.php +++ b/lib/api/source/deploy.php @@ -21,20 +21,111 @@ 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; } - /* [1] Check $1 argument - =========================================================*/ - if( $argc < 2 || !in_array($argv[1], 'init', 'sync' ){ - echo 127; - die(); - } + $exec = api_deploy(); - - echo api_deploy($argv[1]); - + echo $exec; + die($exec); ?> diff --git a/lib/api/source/init.php b/lib/api/source/init.php index b3b6e35..17116f6 100755 --- a/lib/api/source/init.php +++ b/lib/api/source/init.php @@ -128,12 +128,16 @@ $response_arr = json_decode($response, true); // if cannot, abort - if( is_null($response_arr) ) + if( is_null($response_arr) ){ + slog('Cannot parse HTTP response', 'api:init', 'update'); return 127; + } /* (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; + } /* (3) Writes request to temporary pipe */ @@ -150,13 +154,13 @@ return 127; } - return 0; + slog('HTTP Response succesfully received', 'api:init', 'update'); /* [8] Deploy received data =========================================================*/ /* (1) Manage response deployement */ - $deploy = syscall(SOURCE_DIR.'/lib/api/deploy init'); + $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); /* (2) Return state */ return ($deploy === true) ? 0 : 127; @@ -164,7 +168,10 @@ } - die(api_init()); + + $exec = api_init(); + echo $exec; + die($exec); diff --git a/lib/api/source/sync.php b/lib/api/source/sync.php index 70b5ac3..ba7ab32 100755 --- a/lib/api/source/sync.php +++ b/lib/api/source/sync.php @@ -114,12 +114,16 @@ $response_arr = json_decode($response, true); // if cannot, abort - if( is_null($response_arr) ) + if( is_null($response_arr) ){ + slog('Cannot parse HTTP response', 'api:sync', 'update'); return 127; + } /* (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; + } /* (3) Writes request to temporary pipe */ file_put_contents(TMP_DIR.'/api.response', $response); @@ -131,16 +135,16 @@ $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); 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 0; + slog('HTTP Response succesfully received', 'api:sync', 'update'); /* [8] Deploy received data =========================================================*/ /* (1) Manage response deployement */ - $deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync'); + $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); /* (2) Return state */ return ($deploy === true) ? 0 : 127; @@ -148,7 +152,9 @@ } - die(api_request()); + $exec = api_request(); + echo $exec; + die($exec); diff --git a/lib/include/bash/const b/lib/include/bash/const index 42d7fc6..ae4caa5 100755 --- a/lib/include/bash/const +++ b/lib/include/bash/const @@ -12,24 +12,32 @@ export CONF_DIR="$ROOT_DIR/conf"; export SOURCE_DIR="/home/xdrm-brackets/Desktop/git.xdr/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats"; export TMP_DIR="$ROOT_DIR/tmp"; + # CONFIGURATION FILES export SECRET_CONF="$CONF_DIR/machine.secret"; export STATE_CONF="$CONF_DIR/machine.state"; export BRANCH_CONF="$CONF_DIR/machine.branch"; export ID_CONF="$CONF_DIR/machine.id"; export URL_CONF="$CONF_DIR/api.url"; -export AUTH_CONF="$CONF_DIR/auth.list"; export WAREHOUSE_TOK="$CONF_DIR/warehouse.token"; 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 export BOOT_LOG="$LOG_DIR/boot.log"; export DAEMON_LOG="$LOG_DIR/daemon.log"; export INSTALL_LOG="$LOG_DIR/install-source.log"; export UPDATE_LOG="$LOG_DIR/update.log"; + # DATA FILES -export ACCESS_DATA="$DATA_DIR/access.data"; +export DEFAULT_DATA="$DATA_DIR/mfrc522"; + # SETTINGS export SECRET_SIZE=250; diff --git a/lib/include/php/const b/lib/include/php/const index 189f15d..ee9f8f7 100755 --- a/lib/include/php/const +++ b/lib/include/php/const @@ -18,10 +18,14 @@ define('BRANCH_CONF', CONF_DIR.'/machine.branch'); define('ID_CONF', CONF_DIR.'/machine.id'); define('URL_CONF', CONF_DIR.'/api.url'); - define('AUTH_CONF', CONF_DIR.'/auth.list'); define('WAREHOUSE_TOK', CONF_DIR.'/warehouse.token'); 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 define('BOOT_LOG', LOG_DIR.'/boot.log'); define('DAEMON_LOG', LOG_DIR.'/daemon.log'); @@ -29,7 +33,7 @@ define('UPDATE_LOG', LOG_DIR.'/update.log'); # DATA FILES - define('ACCESS_DATA', DATA_DIR.'/access.data'); + define('DEFAULT_DATA', DATA_DIR.'/mfrc522'); # SETTINGS define('SECRET_SIZE', 250); diff --git a/lib/mfrc522/source/loop.php b/lib/mfrc522/source/loop.php index 3252947..74315ed 100755 --- a/lib/mfrc522/source/loop.php +++ b/lib/mfrc522/source/loop.php @@ -50,6 +50,9 @@ =========================================================*/ + /* [3] Manage action (states) + =========================================================*/ + /* [n] Wait for 0.5 s before reading again =========================================================*/ From 291190475d33ab162eb644ee299a84c4a3b57137 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 20 Feb 2017 21:58:29 +0100 Subject: [PATCH 2/2] [Done] Implemented chip state management --- lib/chip/source/state.php | 101 ++++++++++++++++++++++++++++++++++++++ lib/chip/state | 3 ++ 2 files changed, 104 insertions(+) create mode 100755 lib/chip/source/state.php create mode 100755 lib/chip/state diff --git a/lib/chip/source/state.php b/lib/chip/source/state.php new file mode 100755 index 0000000..a94556a --- /dev/null +++ b/lib/chip/source/state.php @@ -0,0 +1,101 @@ +#!/usr/bin/php + +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); +?> + diff --git a/lib/chip/state b/lib/chip/state new file mode 100755 index 0000000..7457f40 --- /dev/null +++ b/lib/chip/state @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/bin/env php $(realpath $(dirname $0))/source/state.php $*;