Wake/Sleep Function for Vehicle

Which Khadas SBC do you use?

VIM3

Which system do you use? Android, Ubuntu, OOWOW or others?

Android

Which version of system do you use? Khadas official images, self built images, or others?

https://dl.khadas.com/products/vim3/firmware/android/vim3-android-9-64bit-v230130.img.xz

Please describe your issue below:

I am building a digital dashboard to replace the instrument cluster in a old vehicle using a VIM3 running android and the Realdash app from the play store.

I need a solution to reliably wake and put the VIM to sleep according to the vehicles ignition key position, as the VIM will be inaccessible once mounted. I know this has been discussed before in other posts but I have been unable to implement a solution.

The main problem is that even though the power button on the device can be toggled to wake and sleep the SBC, 99.999% of the time when its put to sleep, it will wake itself up within a few minutes and drain the vehicle battery. I have built relays that short the POGO pads on the SBC according to the ignition state, but this is even more unreliable as the SBC still wakes itself, and the switch logic easily fall out of sync.

I want to follow the solution as described here: Command to Trigger Android Sleep? - RealDash Forum which seems to be a similar approach discussed by @3id_L here : VIM3 Android Power Suspend by Voltage Level / GPIO? - #6 by goenjoy

A ESP is powered when the vehicle is turned on, it checks the sleep state of the VIM via a GPIO pin, and if it’s asleep, it shorts the pogo pins to wake it up.

Once the VIM is awake, the VIM then monitors the ESP’s power state, and if the ESP is turned off when the ignition is turned off, the VIM runs a script to put it into a sleep state. (apparently it like to stay asleep when commanded this way?)

I am familiar with how to wire this up, and I am familiar with how to put the code on the ESP using something like Arduino IDE.

What I don’t know is how to put the code on the Android VIM3? Hoping someone can help, where does it need to go, and how do I put it there. Preferably explained in a way a child would understand, because that’s my level of knowledge with this SBC and android.

Many thanks

@Duke3D

Hello, after reading your description, I roughly understand what you want to do. Do you want to refer to the method mentioned by 3id_L in the above example? We have not verified the case he mentioned. Sorry, you You can try to contact him for advice, or you can ask us if you are stuck

Thank you, I appreciate the assistance.

Yes, I think best to start with the method mentioned by 3id_L as the code is already written for us. All I need to know, is how to put his code below, onto the VIM3?

Do I paste the code into a file somewhere? Do I access the files by connecting the VIM to PC over USB? Or do I need to install some kind of terminal application in Android? I really have no idea how its done…

#!/system/bin/sh
restart_usb=false
echo 'Starting Powermanagement Service...'
while true
do
	ignition=$( cat /sys/class/gpio/gpio431/value )
	enabled=$( cat /sys/class/gpio/gpio432/value )
	logcat > /logs/logcat_$(date +"%Y%m%d%H%M").txt
	if [ "$enabled" = 1 ]; then
		if [ "$ignition" = 0 ]; then
			shutdown_timer_run=true
			if [ -z $(dumpsys display | grep mScreenState=OFF) ]; then
				echo 'Going to sleep'
				echo 'mem' > /sys/power/state
				restart_usb=true
			fi
		else
			if [ "$restart_usb" = true ]; then
				echo 0 > /sys/bus/usb/devices/1-1.1/authorized
				sleep 1
				echo 1 > /sys/bus/usb/devices/1-1.1/authorized
				restart_usb=false
			fi
		fi
	fi
	sleep 1
done

@Duke3D

I understand what you mean, you want to put this script into the system and execute it when needed, right? So the link mentioned that root permissions are required, I probably got through the information, I will try to see if it works It cannot be put into the system file, wait for my message

Yes, I just need to know how to put this script into the system. Do I need to use a tool on my PC like Android Debug Bridge (ABD) to put it on there? I am not sure… Sounds like a simple task for someone who knows what they are doing, but I have never done this before so I don’t know where to start.

This script below may be better. If I understand correctly, I should be able to connect a jumper wire from pin on the VIM which has 3.3v (like pin 20 or 27), to pin 37 (gpio431), power on the VIM, and if i disconnect the jumper wire, the VIM should then go to sleep.

#!/system/bin/sh
echo 'Start Power Service...'
echo 431 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio431/direction
while true
do
    pwrctrl=$( cat /sys/class/gpio/gpio431/value )
	if [ "$pwrctrl" = 0 ]; then		
		echo 'mem' > /sys/power/state
		echo ' Set sleep...'
		
	fi
	sleep 1
done

@Duke3D

Sorry to reply you so late, you can add your script to the system as follows, and you can execute it in adb

  • The path is /device/khadas
From e972bdb3c0364ffebecf582d91e0e56012bb2a9b Mon Sep 17 00:00:00 2001
From: Xiong Zhang <xiong.zhang@wesion.com>
Date: Thu, 23 Mar 2023 14:01:46 +0800
Subject: [PATCH] test1

Signed-off-by: Xiong Zhang <xiong.zhang@wesion.com>
---
 common/sepolicy/file_contexts            |  1 +
 common/sepolicy/powermanagement.te       | 11 +++++++++++
 kvim3/init.amlogic.board.rc              | 14 ++++++++++++++
 kvim3/kvim3.mk                           |  6 ++++++
 kvim3/powermanagement/powermanagement.sh |  3 +++
 5 files changed, 35 insertions(+)
 create mode 100644 common/sepolicy/powermanagement.te
 create mode 100644 kvim3/powermanagement/powermanagement.sh

diff --git a/common/sepolicy/file_contexts b/common/sepolicy/file_contexts
index b8577c5..c379033 100644
--- a/common/sepolicy/file_contexts
+++ b/common/sepolicy/file_contexts
@@ -296,6 +296,7 @@
 /vendor/bin/tee-supplicant    u:object_r:tee_exec:s0
 /vendor/bin/tee_hdcp          u:object_r:tee_exec:s0
 /vendor/bin/tee_preload_fw    u:object_r:firmload_exec:s0
+/vendor/bin/powermanagement.sh    u:object_r:powermanagement_exec:s0
 
 /vendor/bin/tvserver          u:object_r:tvserver_exec:s0
 /vendor/bin/dtvkitserver      u:object_r:dtvkitserver_exec:s0
diff --git a/common/sepolicy/powermanagement.te b/common/sepolicy/powermanagement.te
new file mode 100644
index 0000000..82d7dee
--- /dev/null
+++ b/common/sepolicy/powermanagement.te
@@ -0,0 +1,11 @@
+########################################
+# sepolicy rules for powermanagement
+########################################
+
+type powermanagement, domain, vendor_executes_system_violators, data_between_core_and_vendor_violators;
+
+type powermanagement_exec, exec_type, vendor_file_type, file_type;
+
+permissive powermanagement;
+
+init_daemon_domain(powermanagement)
diff --git a/kvim3/init.amlogic.board.rc b/kvim3/init.amlogic.board.rc
index e36b9f6..64a267b 100755
--- a/kvim3/init.amlogic.board.rc
+++ b/kvim3/init.amlogic.board.rc
@@ -81,6 +81,8 @@ on property:sys.boot_completed=1
 #cmdline
     chmod 666 /proc/cmdline
 
+chmod 777 /vendor/bin/powermanagement.sh
+
 service preinstall /system/bin/preinstall.sh
     user root
     group root
@@ -89,3 +91,15 @@ service preinstall /system/bin/preinstall.sh
 
 on property:sys.boot_completed=1
     start preinstall
+
+service powermanagement /vendor/bin/powermanagement.sh
+    class main
+    user root
+    group root
+    oneshot
+
+on property:persist.powermanagement.service=1
+    start powermanagement
+
+on property:persist.powermanagement.service=0
+    stop powermanagement
diff --git a/kvim3/kvim3.mk b/kvim3/kvim3.mk
index 6fe7484..cf225a2 100644
--- a/kvim3/kvim3.mk
+++ b/kvim3/kvim3.mk
@@ -44,6 +44,9 @@ $(shell python device/khadas/kvim3/auto_generator.py preinstall)
 PRODUCT_COPY_FILES += \
     device/khadas/kvim3/preinstall/preinstall.sh:system/bin/preinstall.sh
 
+
+PRODUCT_COPY_FILES += \
+    device/khadas/kvim3/powermanagement/powermanagement.sh:/vendor/bin/powermanagement.sh
 #########################################################################
 #
 #                     media ext
@@ -84,6 +87,9 @@ PRODUCT_PROPERTY_OVERRIDES += \
 PRODUCT_PROPERTY_OVERRIDES += \
         persist.sys.func.key.action=102
 
+PRODUCT_PROPERTY_OVERRIDES += \
+        persist.powermanagement.service=0
+
 #wifi hotpot
 PRODUCT_PROPERTY_OVERRIDES += \
 	persist.sys.softap.band=0
diff --git a/kvim3/powermanagement/powermanagement.sh b/kvim3/powermanagement/powermanagement.sh
new file mode 100644
index 0000000..23b114c
--- /dev/null
+++ b/kvim3/powermanagement/powermanagement.sh
@@ -0,0 +1,3 @@
+#!/system/bin/sh
+#
+echo "just test" >> /sdcard/shelltest
\ No newline at end of file
-- 
2.17.1
  • The path is /system/core
From 58bb3dbc8234c954400954fd5032e9a606fdc5f5 Mon Sep 17 00:00:00 2001
From: Xiong Zhang <xiong.zhang@wesion.com>
Date: Thu, 23 Mar 2023 14:02:03 +0800
Subject: [PATCH] test2

Signed-off-by: Xiong Zhang <xiong.zhang@wesion.com>
---
 init/stable_properties.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/init/stable_properties.h b/init/stable_properties.h
index 4972d101f..ca3552c64 100644
--- a/init/stable_properties.h
+++ b/init/stable_properties.h
@@ -25,7 +25,7 @@ namespace init {
 
 static constexpr const char* kPartnerPrefixes[] = {
     "init.svc.vendor.", "ro.vendor.", "persist.vendor.", "vendor.", "init.svc.odm.", "ro.odm.",
-    "persist.odm.",     "odm.",       "ro.boot.",
+    "persist.odm.",     "odm.",       "ro.boot.",       "persist.powermanagement.",
 };
 
 static const std::set<std::string> kExportedActionableProperties = {
-- 
2.17.1


This file is the script file you want to execute, and the content inside is DIY according to your actual situation

Hi @xiong.zhang ,

I followed this procedure line by line, except using on property:persist.powermanagement.service thing

+service powermanagement /vendor/bin/powermanagement.sh
+    class main
+    user root
+    group root
+    oneshot
+
+on property:persist.powermanagement.service=1
+    start powermanagement
+
+on property:persist.powermanagement.service=0
+    stop powermanagement

I used this

service staticaddr /vendor/bin/staticaddr.sh
    class main
    user root
    group root
    oneshot

on property:sys.boot_completed=1
    start staticaddr

but I got this following errors in build

make[1]: Leaving directory '/home/expleo/aosp9/out/target/product/kvim3/obj/KERNEL_OBJ'
make: Leaving directory '/home/expleo/aosp9/common'
[  8% 21/252] build out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy
FAILED: out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy 
/bin/bash -c "(ASAN_OPTIONS=detect_leaks=0 out/host/linux-x86/bin/checkpolicy -M -c 		30 -o out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy.tmp out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy.recovery.conf ) && (out/host/linux-x86/bin/sepolicy-analyze out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy.tmp permissive > out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy.permissivedomains ) && (if [ \"user\" = \"user\" -a -s out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy.permissivedomains ]; then 		echo \"==========\" 1>&2; 		echo \"ERROR: permissive domains not allowed in user builds\" 1>&2; 		echo \"List of invalid domains:\" 1>&2; 		cat out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy.permissivedomains 1>&2; 		exit 1; 		fi ) && (mv out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy.tmp out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy )"
out/host/linux-x86/bin/checkpolicy:  loading policy configuration from out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy.recovery.conf
out/host/linux-x86/bin/checkpolicy:  policy configuration loaded
out/host/linux-x86/bin/checkpolicy:  writing binary representation (version 30) to out/target/product/kvim3/obj/ETC/sepolicy.recovery_intermediates/sepolicy.tmp
==========
ERROR: permissive domains not allowed in user builds
List of invalid domains:
staticaddr
[  8% 22/252] build out/target/product/kvim3/obj/ETC/sepolicy_neverallows_intermediates/sepolicy_neverallows
out/host/linux-x86/bin/checkpolicy:  loading policy configuration from out/target/product/kvim3/obj/ETC/sepolicy_neverallows_intermediates/policy.conf
out/host/linux-x86/bin/checkpolicy:  policy configuration loaded
out/host/linux-x86/bin/checkpolicy:  writing binary representation (version 30) to out/target/product/kvim3/obj/ETC/sepolicy_neverallows_intermediates/sepolicy_neverallows
[ 10% 26/252] make Image
make: Entering directory '/home/expleo/aosp9/common'

it showing error permissive domains not allowed in user builds and list of invalid domain as my staticaddr domain. what I should do