#!/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 ERR_FAILED_RM_WIFI_CONF=150 ERR_FAILED_CLEAR_BT_CACHE=151 ERR_FAILED_RESET_SETTINGS_CONF=152 ERR_FAILED_CLEAR_LOGS=153 ERR_FAILED_RM_SSH=154 LOC_SETTINGS_CONF="/home/root/.config/Settings/System.conf" LOC_DEFAULT_SETTINGS_CONF="/home/root/scripts/default_settings.conf" #TODO update to reflect real location LOC_WIFI_CONF="/etc/wpa_supplicant/wpa_supplicant-wlan0.conf" LOC_BLUETOOTH_CACHE_FOLDER="/var/lib/bluetooth/" LOC_SSH_FOLDER="$HOME/.ssh/" LOC_LOG_PARENT_FOLDER="/media/sd-card/" # removes all the files in the directory and subdirectory in a recursive manner # exits with passed error code if either rm command fails or folder is not empty of files # %1 - Directory path # %2 - Error message string/msg # %3 - Error code to return on exit in case of error function clearFolderContent() { if [ -f $1 ]; then rm -rf "$1/*/*" fi rmCommandReturn=$? fileCount=$(find $1 -type f | wc -l) if [ "$fileCount" -ne 0 ] || [ "$rmCommandReturn" -ne 0 ]; then echo "Error : $2 ($fileCount)" exit $3 fi echo "$2 success" } # Removes a single file # exits with passed error code if either rm command fails or if file was not removed # %1 - path to file # %2 - Error message string/msg # %3 - Error code to return on exit in case of error function removeSingleFile() { if [ -f $1 ]; then rm "$1" fi if [ -f $1 ]; then echo "Error: $2" # the file still exists, exit with error message exit $3 fi } # ------------------------------ Remove Wifi Conf removeSingleFile $LOC_WIFI_CONF "Failed to remove WiFi conf file" $ERR_FAILED_RM_WIFI_CONF # ------------------------------ Remove Bluetooth cache clearFolderContent $LOC_BLUETOOTH_CACHE_FOLDER "Failed to remove Bluetooth cache" $ERR_FAILED_CLEAR_BT_CACHE # ------------------------------ Reset Settings Conf storedPassword=$(grep Password $LOC_SETTINGS_CONF) cp $LOC_DEFAULT_SETTINGS_CONF $LOC_SETTINGS_CONF sed -i "s/^.*Password.*$/$storedPassword/" $LOC_SETTINGS_CONF # ------------------------------ Remove Logs #TODO this will always fail due to the fact UI is not pausing on it's writing of log when we attempt a removal, causing the count to always be >0 clearFolderContent $LOC_LOG_PARENT_FOLDER "Failed to remove logs" $ERR_FAILED_CLEAR_LOGS # ------------------------------ Remove ssh clearFolderContent $LOC_SSH_FOLDER "Failed to remove ssh" $ERR_FAILED_RM_SSH exit