socan 1.2.1
Linux SocketCAN higher level library
Loading...
Searching...
No Matches
socan_util.c
1
23/* socan_util.c: Miscellaneous functions for test programs.
24*/
25
26#include <stdio.h>
27#include <string.h>
28#include <stdlib.h>
29#include <ctype.h>
30#include <stdarg.h>
31#include <stdbool.h>
32
33/* nanosleep: */
34#include <time.h>
35
36#include <socan.h>
37
38#include <socan_util.h>
39
40/* ----------------------------------------------
41 * option parsing
42 * ---------------------------------------------- */
43
44int match(int argc, char *argv[], int *index, option *opts, int opt_no)
45 /* returns match index or -1 */
46 {
47 int i;
48 char *st;
49
50 if (*index >= argc)
51 return -1;
52 st= argv[*index];
53 for(i=0; i<opt_no; i++)
54 {
55 if (0==strcmp(opts[i].opt, st))
56 {
57 (*index)++;
58 return opts[i].index;
59 }
60 }
61 return -1;
62 }
63
64int match_int(int argc, char *argv[], int *index, int *num)
65 {
66 int i;
67
68 if (*index >= argc)
69 return -1;
70 i= sscanf(argv[*index], "%d", num);
71 if (i==1)
72 {
73 (*index)++;
74 return 0; /* ok */
75 }
76 return -2; /* not a number */
77 }
78
79int match_float(int argc, char *argv[], int *index, double *num)
80 {
81 int i;
82
83 if (*index >= argc)
84 return -1;
85 i= sscanf(argv[*index], "%lf", num);
86 if (i==1)
87 {
88 (*index)++;
89 return 0; /* ok */
90 }
91 return -2; /* not a number */
92 }
93
94int match_st(int argc, char *argv[], int *index, char **st)
95 {
96 if (*index >= argc)
97 return -1;
98 *st= argv[*index];
99 (*index)++;
100 return 0; /* ok */
101 }
102
103void my_exit(char *format, ...)
104 {
105 va_list ap;
106
107 va_start(ap, format);
108 vfprintf(stderr, format, ap);
109 va_end(ap);
110 exit(1);
111 }
112
113/* ----------------------------------------------
114 * quiet level setting
115 * ---------------------------------------------- */
116
117void eval_quietlevel(int quietlevel, int *scale, char *ch, int *l)
118 {
119 *scale= 0;
120 *ch= 0;
121 *l= 0;
122 if (quietlevel<=0)
123 return;
124 if (quietlevel>5)
125 quietlevel= 5;
126 switch(quietlevel)
127 {
128 case 5: *scale=10000;
129 *ch='Z';
130 break;
131 case 4: *scale=1000;
132 *ch='T';
133 break;
134 case 3: *scale=100;
135 *ch='O';
136 break;
137 case 2: *scale=10;
138 *ch='o';
139 break;
140 case 1: *scale=1;
141 *ch='.';
142 break;
143 };
144 if (*scale!=0)
145 printf("one '%c' are %d loops!\n",*ch,*scale);
146 }
147
148/* ----------------------------------------------
149 * dump function
150 * ---------------------------------------------- */
151
152void nice_dump(void *ptr, int len, int addr)
153 {
154 unsigned char *p= (unsigned char *)ptr;
155 unsigned char *q;
156 int lcount;
157
158 while (len>0)
159 { printf("%8X: ", addr);
160 for(q=p, lcount=0; (lcount<16) && (lcount<len); lcount++, q++)
161 printf("%2X ",*q);
162 for( ;lcount<16; lcount++)
163 printf(" ");
164 printf(" \"");
165 for(q=p, lcount=0; (lcount<16) && (lcount<len); lcount++, q++)
166 if (isprint(*q))
167 printf("%c",*q);
168 else
169 printf(" ");
170 printf("\"\n");
171 p+= lcount;
172 addr+= lcount;
173 len-= lcount;
174 };
175 }
176
177/* ----------------------------------------------
178 * time module
179 * ---------------------------------------------- */
180
181void mldelay(long milliseconds)
182/* millisecond-delay, works without "dirty tricks"
183 warning: the actual resolution may be smaller, e.g 1/60th of a second*/
184 {
185 struct timespec rq,rt;
186
187 /* POSIX-conform wait */
188 if (milliseconds >= 1000L)
189 { rq.tv_sec = milliseconds / 1000L;
190 rq.tv_nsec= (milliseconds-1000L*rq.tv_sec) * 1000000L;
191 }
192 else
193 { rq.tv_sec = 0;
194 rq.tv_nsec= milliseconds * 1000000L;
195 };
196 nanosleep( &rq, &rt);
197 }
198
199void micdelay(unsigned long microseconds)
200 { struct timespec rq,rt;
201
202 if (microseconds >= 1000000)
203 { rq.tv_sec = microseconds / 1000000;
204 rq.tv_nsec= (microseconds-1000000*rq.tv_sec) * 1000;
205 }
206 else
207 { rq.tv_sec = 0;
208 rq.tv_nsec= microseconds * 1000;
209 };
210 nanosleep( &rq, &rt);
211 }
212
213unsigned long timestamp_ms(void)
214 {
215 struct timespec rq;
216
217 clock_gettime(CLOCK_REALTIME, &rq);
218 return rq.tv_sec *1000 + rq.tv_nsec / 1000000L;
219 }
220
221unsigned long timestamp_us(void)
222 {
223 struct timespec rq;
224
225 clock_gettime(CLOCK_REALTIME, &rq);
226 return rq.tv_sec *1000000 + rq.tv_nsec / 1000L;
227 }
228
229/* ----------------------------------------------
230 * init socan
231 * ---------------------------------------------- */
232
233bool init_socan(char **interfaces,
234 int interface_no,
235 int tracelevel,
236 int errprintlevel,
237 int reader_prio,
238 int writer_prio)
239/* if reader_prio and writer_prio >=0, use real time priorities */
240 {
241 int i;
242 bool use_rt_prio= false;
243
244 if ((reader_prio >=0) && (writer_prio >=0))
245 use_rt_prio= true;
246
247 if (!socan_realtime_setup(use_rt_prio,
248 false, /* require_rt_priority */
249 reader_prio,
250 writer_prio))
251 {
252 printf("%s line %d: socan_realtime_setup failed\n",
253 __FILE__, __LINE__);
254 return false;
255 }
256
257 for(i=0; i<interface_no; i++)
258 {
259 int p= socan_add_port(interfaces[i]);
260 if (p<0)
261 {
262 printf("%s line %d: socan_add_port failed\n",
263 __FILE__, __LINE__);
264 return false;
265 }
266 }
267 if (!socan_init())
268 {
269 printf("%s line %d: socan_init failed\n",
270 __FILE__, __LINE__);
271 return false;
272 }
273 if (errprintlevel>=0)
274 printf("set errprintlevel to %d\n",
275 socan_errprintlevel(errprintlevel));
276 if (tracelevel>=0)
277 printf("set tracelevel to %d\n",
278 socan_tracelevel(tracelevel));
279 return true;
280 }
281
bool socan_realtime_setup(bool use_rt_priority, bool require_rt_priority, int reader_priority, int writer_priority)
Definition socan.c:2472
int socan_errprintlevel(int level)
Definition socan.c:288
int socan_add_port(const char *devicename)
Definition socan.c:1395
int socan_tracelevel(int level)
Definition socan.c:354
bool socan_init(void)
Definition socan.c:2538
c header file for socan object layer library.