C++JNI打开/dev/rtc结果的参数无效
C++ JNI open /dev/rtc result invalid argument
我尝试打开/dev/rtc
或/dev/rtc0
。
# ls -l /dev/rtc*
lrwxrwxrwx 1 root root 4 Aug 11 05:59 /dev/rtc -> rtc0
crw------- 1 root root 248, 0 Aug 11 05:59 /dev/rtc0
这是我的JNI代码(C++(:
using namespace std;
void get_system_dt(struct tm tm) {
time_t t = time(NULL);
tm = *localtime(&t) ;
}
int set_hardware_dt() {
struct tm tm ;
get_system_dt(tm);
struct rtc_time rt ;
rt.tm_year = tm.tm_year ;
rt.tm_min = tm.tm_mon ;
rt.tm_yday = tm.tm_yday ;
rt.tm_mday = tm.tm_mday ;
rt.tm_hour = tm.tm_hour ;
rt.tm_wday = tm.tm_wday ;
rt.tm_min = tm.tm_min ;
rt.tm_sec = tm.tm_sec ;
rt.tm_isdst = tm.tm_isdst ;
int fd = open ("/dev/rtc0",O_RDONLY); <-- Problem this line
cout << fd << endl;
if (fd != 3) {
perror("SET_HW_DT");
return 1;
}
int r = ioctl(fd,RTC_SET_TIME,&rt);
if (r != 0) {
perror("ioctl");
return 1;
}
close(fd);
return 0 ;
}
JNIEXPORT void JNICALL Java_ir_moke_jsysbox_time_JDateTime_syncSystemToHardware (JNIEnv *env, jclass clazz) {
int r = set_hardware_dt();
if (r != 0) throwException(env,"Failed to sync system to hardware");
}
这个输出:
6
SET_HW_DT: Invalid argument
我不明白为什么这个代码结果是Invalid argumnt
带有简单main的代码可以毫无问题地工作:
int main(int argc, char *argv[])
{
set_hardware_dt();
return 0;
}
output :
3
SET_HW_DT: Success
什么是问题?
您的代码有几个问题。从上到下:
void get_system_dt(struct tm tm)
将从不修改您在set_hardware_dt
中传入的tm
。您需要传递一个struct tm& tm
,该代码才能工作。
接下来,一个拼写错误:
rt.tm_min = tm.tm_mon ;
最后,您的";问题这行";实际上并不是问题所在。相反,这一行是:
if (fd != 3) {
这假设新分配的文件描述符将是数字3。这通常只适用于在代码运行之前没有打开或关闭任何文件描述符的小程序。
相反,您应该检查fd
是否为负,因为这是来自open
的错误信号。
相关文章:
- 使用具有默认参数的函数模板进行 decltype 会使结果混乱(一个有趣的问题或 gcc 的错误)
- 执行参数未提供预期结果
- 术语的计算结果不是采用0个参数的函数
- 错误 C2064:术语的计算结果不是采用 3 个参数的函数
- C++:通过函数参数传递的值给出不同的结果
- 结果失败或多个参数无效
- 使用函数结果作为参数的 C++
- 添加可变参数函数的错误结果
- 从可调用可变参数元组中的函数结果创建元组
- (WMI)ExecMethod out 参数 - 无论调用的结果如何,ResultingSnapshot 都是 NULL
- C++:术语的计算结果不是采用 1 个参数的函数
- 在线程错误 C2064 中:term 的计算结果不为 0 个参数的函数
- 错误 C2064:term 的计算结果不是采用 1 个参数的函数 - 关于线程的一些东西
- 提升线程:术语的计算结果不为 0 个参数
- 当类的对象更改其参数时,如何获得结果
- 是否可以使用自动占位符来推断非类型模板参数中的函数结果
- 为什么在类构造函数方法中,std::string 参数在调试时显示不同的结果?
- 使用 constexpr 函数的结果作为模板参数(clang vs gcc)
- 在 c++ 代码中具有相同的函数和参数的不同结果
- 默认模板参数结果为"expected type-specifier"