00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023
00024 #ifndef WIN32
00025 #ifdef linux
00026
00027 #include <linux/rtc.h>
00028 #endif
00029 #include <sys/ioctl.h>
00030 #include <sys/time.h>
00031 #include <sys/select.h>
00032 #include <fcntl.h>
00033 #include <sys/stat.h>
00034 #include <sys/types.h>
00035 #include <dirent.h>
00036 #endif
00037
00038 #include <stdlib.h>
00039 #include <stdarg.h>
00040 #include <time.h>
00041 #include <sys/time.h>
00042 #include <unistd.h>
00043 #include <string.h>
00044
00045
00046 #include <console_ctrl.h>
00047
00048 #include <jutils.h>
00049 #include <config.h>
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 static uint32_t randval;
00102
00103 uint32_t fastrand()
00104 {
00105
00106
00107
00108
00109 return(randval = randval * 1073741789 + 32749 );
00110 }
00111
00112 void fastsrand(uint32_t seed)
00113 {
00114 randval = seed;
00115 }
00116
00117 double dtime() {
00118 struct timeval mytv;
00119 gettimeofday(&mytv,NULL);
00120 return((double)mytv.tv_sec+1.0e-6*(double)mytv.tv_usec);
00121 }
00122
00123 #ifdef linux
00124 #include <sched.h>
00125
00126
00127
00128 bool set_rtpriority(bool max) {
00129 struct sched_param schp;
00130
00131
00132 memset(&schp, 0, sizeof(schp));
00133
00134 if(max)
00135 schp.sched_priority = sched_get_priority_max(SCHED_RR);
00136 else
00137 schp.sched_priority = sched_get_priority_min(SCHED_RR);
00138
00139 if (sched_setscheduler(0, SCHED_RR, &schp) != 0)
00140 return false;
00141 else
00142 return true;
00143 }
00144 #endif
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 void jsleep(int sec, long nsec) {
00157 struct timespec tmp_rem,*rem;
00158 rem = &tmp_rem;
00159 timespec timelap;
00160 timelap.tv_sec = sec;
00161 timelap.tv_nsec = nsec;
00162 while (nanosleep (&timelap, rem) == -1 && (errno == EINTR));
00163 }
00164
00165
00166
00167
00168 #ifdef linux
00169
00170 static int rtcfd = -1;
00171 static fd_set readfds;
00172 static timeval rtctv = { 0,0 };
00173 static unsigned long rtctime;
00174 int rtc_open() {
00175 int res;
00176 rtcfd = open("/dev/rtc",O_RDONLY);
00177 if(!rtcfd) {
00178 perror("/dev/rtc");
00179 return 0;
00180 }
00181
00182 res = ioctl(rtcfd, RTC_UIE_ON, 0);
00183 if(res<0) {
00184 perror("rtc ioctl");
00185 return 0;
00186 }
00187 notice("realtime clock succesfully initialized");
00188 return 1;
00189 }
00190
00191
00192 unsigned long rtc_tick() {
00193 FD_ZERO(&readfds);
00194 FD_SET(rtcfd,&readfds);
00195 if ( ! select(rtcfd+1,&readfds,NULL,NULL,&rtctv) )
00196 return 0;
00197 read(rtcfd,&rtctime,sizeof(unsigned long));
00198 return rtctime;
00199 }
00200 void rtc_freq_set(unsigned long freq) {
00201 int res;
00202
00203 res = ioctl(rtcfd,RTC_IRQP_SET,freq);
00204 if(res<0) { perror("rtc freq set"); }
00205
00206 res = ioctl(rtcfd,RTC_IRQP_READ,&freq);
00207 if(res<0) { perror("rtc freq read"); }
00208
00209 act("realtime clock frequency set to %ld",freq);
00210
00211 res = ioctl(rtcfd,RTC_PIE_ON,0);
00212 if(res<0) { perror("rtc freq on"); return; }
00213
00214 }
00215 void rtc_freq_wait() {
00216 int res;
00217 res = read(rtcfd,&rtctime,sizeof(unsigned long));
00218 if(res < 0) {
00219 perror("read rtc frequency interrupt");
00220 return;
00221 }
00222 }
00223 void rtc_close() {
00224 if(rtcfd<=0) return;
00225 ioctl(rtcfd, RTC_UIE_OFF, 0);
00226
00227 close(rtcfd);
00228 }
00229 #endif
00230
00231 void *(* jmemcpy)(void *to, const void *from, size_t len);
00232
00233
00234 bool filecheck(const char *file) {
00235 bool res = true;
00236 FILE *f = fopen(file,"r");
00237 if(!f) res = false;
00238 else fclose(f);
00239 return(res);
00240 }
00241
00242 bool dircheck(const char *dir) {
00243 bool res = true;
00244 DIR *d = opendir(dir);
00245 if(!d) res = false;
00246 else closedir(d);
00247 return(res);
00248 }