C++JNI打开/dev/rtc结果的参数无效

C++ JNI open /dev/rtc result invalid argument

本文关键字:参数 结果 无效 rtc 打开 dev C++JNI      更新时间:2024-09-21

我尝试打开/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的错误信号。