#!/bin/sh ########################################################################### # # Copyright (c) 2021-2023 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file factory_reset.sh # # @author (last) Vy Duong # @date (last) 26-May-2023 # @author (original) Vy Duong # @date (original) 26-May-2023 # ############################################################################ # @details # This file is called by UI Software to remove and reset the device #TODO This script needs to be integrated with the globals.sh ERR_REMOVE_PRM_DIR=150 ERR_REMOVE_PRM_FIL=151 ERR_REMOVE_CMD_DIR=152 ERR_REMOVE_CMD_FIL=153 ERR_REMOVE_DEL_DIR=154 ERR_REMOVE_DEL_FIL=155 ERR_CLEAN_SETTINGS_CONF_PASSNOTFOUND=159 ERR_CLEAN_SETTINGS_CONF_OVERWRITE=160 ERR_CLEAN_SETTINGS_DFLT_RESTORE=161 ERR_CLEAN_LOGS=162 LOC_SETTINGS_CONF="/opt/leahi/configurations/Settings/System.conf" LOC_SETTINGS_DFLT="/opt/leahi/configurations/Settings/System.dflt" LOC_LOG_BASE_FOLDER="/media/sd-card/" LOC_TX_LOG_BASE_FOLDER="/var/configurations/treatment/" LOC_SCRIPTS=/opt/leahi/scripts LOC_SCRIPTS_BRIGHTNESS=$LOC_SCRIPTS/brightness.sh DEFAULT_BRIGHTNESS_LEVEL=5 TRUE=1 FALSE=0 # check if the passed argument is a non-zero number function isNonZero () # $1 - mutant: the argument to be detected { local _ok_=$FALSE if [[ $1 =~ ^[0-9]+$ ]]; then if (( $1 )); then _ok_=$TRUE fi fi echo $_ok_ } # exits with the exit code if the condition ($1) is non-zero function exitError () # $1 - number: boolean result to exit if non-zero, with exit code # $1 - number: exit code # $2 - string: echo message [optional] { if (( $2 )); then echo "$3" fi if (( $(isNonZero $1) )); then exit $2 fi } function deleteWiFi() { $LOC_SCRIPTS/wifi_disconnect_network.sh wlan0 } function deleteBluetooth() { $LOC_SCRIPTS/bluetooth_paired_clear.sh } function defaultSettings() { local _has_error_=$FALSE servicePasswordLine=$(grep -A1 "\[Service\]" "$LOC_SETTINGS_CONF" | grep Password) # store the service password line _has_error_= [[ -z "$servicePasswordLine" ]] exitError $_has_error_ $ERR_CLEAN_SETTINGS_CONF_PASSNOTFOUND "Password not found in '$LOC_SETTINGS_CONF'" cp $LOC_SETTINGS_DFLT $LOC_SETTINGS_CONF # reset the settings to default settings _has_error_=$? exitError $_has_error_ $ERR_CLEAN_SETTINGS_CONF_OVERWRITE "Service settings overwrite failed" sed -i "s/^.*Password.*$/$servicePasswordLine/" $LOC_SETTINGS_CONF # keep the service password _has_error_=$? exitError $_has_error_ $ERR_CLEAN_SETTINGS_DFLT_RESTORE "Service password restore failed" } function defaultBrightness() { $LOC_SCRIPTS_BRIGHTNESS $DEFAULT_BRIGHTNESS_LEVEL } function deleteLogFiles() { # handling log deletion in a special manner: # - Log files generated today # - are not deleted # - not part of the checking whether deletion was successful # - setting maxDepth to 2 due to path used is root level with sd-card/ folders of logs # NOTE: find command always return true / non-zero! when using with exec # TODO: Checking the file count should be fixed later # Remove the contents of the log folder rm "$LOC_LOG_BASE_FOLDER"/log/* _has_error_=!$? exitError $_has_error_ $ERR_REMOVE_CMD_FIL "Log file deletion" # Remove the contents of the service folder rm "$LOC_LOG_BASE_FOLDER"/service/* _has_error_=!$? exitError $_has_error_ $ERR_REMOVE_CMD_FIL "Service file deletion" fileCount=$(find "$LOC_LOG_BASE_FOLDER" -maxdepth 2 -type f -daystart -mtime +0 | wc -l) _has_error_=$fileCount exitError $_has_error_ $ERR_REMOVE_DEL_FIL "Remained '$fileCount' file undeleted" # Deleting treatment logs in encrypted partition: find "$LOC_TX_LOG_BASE_FOLDER" -maxdepth 2 -type f | xargs rm _has_error_=!$? exitError $_has_error_ $ERR_REMOVE_CMD_FIL "Tx Log file deletion" fileCount=$(find "$LOC_TX_LOG_BASE_FOLDER" -maxdepth 2 -type f | wc -l) _has_error_=$fileCount exitError $_has_error_ $ERR_REMOVE_DEL_FIL "Remained '$fileCount' file undeleted" } # delete WiFi settings # delete Bluetooth settings # settings.conf to default # brightness to default (10) # delete logs # keep partition password # keep service password # keep cloudsync tokens function main() { deleteWiFi deleteBluetooth defaultSettings defaultBrightness deleteLogFiles } main echo "" exit 0