Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add commandline tool to check rtc battery status #438

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions recipes-app/read-rtc-battery/files/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright (c) Siemens AG, 2019-2023
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

#
# Authors:
# Torsten Hahn <[email protected]>
#
# This file is subject to the terms and conditions of the MIT License. See
# COPYING.MIT file in the top-level directory.
#
cmake_minimum_required(VERSION 3.2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing copyright header.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected

project(read-rtc-battery)
include_directories(.)
add_executable(readrtcbattery readrtcbattery.c)
set(CMAKE_INSTALL_PREFIX "/usr")
install(TARGETS readrtcbattery RUNTIME DESTINATION bin)
54 changes: 54 additions & 0 deletions recipes-app/read-rtc-battery/files/src/readrtcbattery.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) Siemens AG, 2019-2023
Copy link
Collaborator

@BaochengSu BaochengSu May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use 2023 only

*
* Authors:
* Torsten Hahn <[email protected]>
*
* This file is subject to the terms and conditions of the MIT License. See
* COPYING.MIT file in the top-level directory.
*
**/
#include <sys/ioctl.h>
#include <stdio.h>
#include <linux/rtc.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
int fd; // Device file path
int batteryStatus; // Status of battery
int retval = 0; // Return value of ioctl function (-1 = Error)

// 1st step: Open Driver
fd = open("/dev/rtc", O_RDONLY);

// 2nd step: Display error in command line if the device
// file wasn't found and close application
if (fd < 0) {
printf("Device file not found\n");
return EXIT_FAILURE;
}

// 3rd step: Read out
retval = ioctl(fd, RTC_VL_DATA_INVALID, &batteryStatus);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if (retval == -1) {
// 4th step: Display error in command line if the ioctl function isn't able to access
// the specific function
printf("Error access ioctl function \n");
return EXIT_FAILURE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close(fd) before return error.

} else {
// 5th step: Print battery status
printf("Battery status (1 = Error) %d\n", batteryStatus);
}
Comment on lines +47 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to wrap this print to the else branch, since the return in if.

close(fd);

return EXIT_SUCCESS;
} // main
Comment on lines +25 to +54
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is simple and straight-forward enough to be self-explained, so most of the comments are unnecessary.

30 changes: 30 additions & 0 deletions recipes-app/read-rtc-battery/read-rtc-battery_0.1.bb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright (c) Siemens AG, 2019-2023
#
# Authors:
# Torsten Hahn <[email protected]>
#
# This file is subject to the terms and conditions of the MIT License. See
# COPYING.MIT file in the top-level directory.
#

inherit dpkg

DESCRIPTION = "Commandline tool to read the state of the rtc battery"
MAINTAINER = "[email protected]"

SRC_URI = "file://src"

S = "${WORKDIR}/src"

DEBIAN_BUILD_DEPENDS = "cmake"
DEBIAN_DEPENDS = "\${shlibs:Depends}"

do_prepare_build[cleandirs] += "${S}/debian"

do_prepare_build() {
deb_debianize
}

# propably correct permissions

1 change: 1 addition & 0 deletions recipes-core/images/iot2050-image-example.bb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ IMAGE_INSTALL += " \
libteec1 \
optee-client-dev \
tee-supplicant \
read-rtc-battery \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to test it?

"

IOT2050_CORAL_SUPPORT ?= "1"
Expand Down