]> rtime.felk.cvut.cz Git - can-usb1.git/blob - ulan/host/ul_drv/utils/ul_lcscan.c
Initializing repo
[can-usb1.git] / ulan / host / ul_drv / utils / ul_lcscan.c
1 /*******************************************************************
2   uLan Communication - simple test client
3
4   ul_lcscan.c   - procceed wavelength scanning with LCD5000
5
6   (C) Copyright 1996,1999 by Pavel Pisa 
7
8   The uLan driver is distributed under the Gnu General Public Licence. 
9   See file COPYING for details.
10  *******************************************************************/
11
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <getopt.h>
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <sys/time.h>
20 #include <sys/stat.h>
21 #include <math.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <ul_lib/ulan.h>
25
26 #ifdef _WIN32
27 #define sleep Sleep
28 #endif
29
30 /*******************************************************************/
31
32 char *ul_dev_name = UL_DEV_NAME;
33 int module= 3;
34 int prt_modules= 0;
35 int debugk     = 0;
36 int debugk_flg = 0;
37 int wl_begin   = 200;
38 int wl_end     = 300;
39 int wl_step    = 1;
40 int wl_pause   = 1000;
41 int zero_fl    = 0;
42 int time_co_fl = 0;
43 int time_co    = 0;
44 int adc_chan_fl= 0;
45 int adc_chan   = 0;
46 int lampctrl_fl= 0;
47 int lampctrl   = 0;
48 int oi_var_fl  = 0;
49 int oi_var_rd  = 0;
50 char *oi_var_aoid=NULL;
51 char *oi_var_val=NULL;
52
53 int oi_var_oiddes_print(int oid, uchar *despack)
54 {
55   char *desname=uloi_oiddespack_strdup(despack,0);
56   char *destype=uloi_oiddespack_strdup(despack,1);
57
58   if(desname){
59     printf(" %5d deslen=%d name=\"%s\" type=\"%s\"\n",
60            oid,despack[0],desname?desname:"",destype?destype:"");
61   }else{
62     printf(" %5d\n",oid);
63   }
64
65   if(desname) free(desname);
66   if(destype) free(destype);
67   return 0;
68 }
69
70 int oi_var_test(char *aoid, char *valin, char **valout, char *type)
71 {
72   int ret;
73   int oid;
74   int val;
75   int vallen=2;
76   int i;
77   uloi_coninfo_t *coninfo;
78
79   coninfo=uloi_open(ul_dev_name,module,0x10,0,10);
80   if(!coninfo) { perror("wl_scan : uLan OI open failed");return -1;};
81
82   if((aoid!=NULL)&&(*aoid))
83   {
84     if(isdigit(*aoid))
85     { oid=strtol(aoid,NULL,0);
86     }
87     else
88     { ret=uloi_get_aoiddes(coninfo,oi_var_rd?ULOI_DOIO:ULOI_DOII,aoid,NULL);
89       if(ret<0) 
90       { fprintf(stderr,"oi_var_test : uloi_get_aoiddes returned %d\n",ret);
91         return -1;
92       };
93       oid=ret;
94     }
95
96     if(valout==NULL&&!oi_var_rd)
97     { if(valin&&*valin)
98       { val=strtol(valin,NULL,0);
99       }
100       else vallen=0;
101       ret=uloi_set_var(coninfo,oid,&val,vallen);
102       if(ret<0) { fprintf(stderr,"oi_var_test : uloi_set_var failed\n");return -1;};
103     }
104     else
105     { ret=uloi_get_var(coninfo,oid,&val,vallen);
106       if(ret<0) {fprintf(stderr,"oi_var_test : uloi_get_var failed\n");return -1;};
107       if(val&0x8000) val|=~0xffff; else val&=0xffff;
108       printf("%s=%d\n",aoid,val);
109     }
110   }
111   else
112   { int *oids;
113     ret=uloi_get_oids(coninfo,ULOI_QOII,&oids);
114     if(ret<0) 
115     { fprintf(stderr,"oi_var_test : uloi_get_oids returned %d\n",ret);
116       return -1;
117     };
118     printf("module recognized commands and set vars:\n");
119     for(i=0;i<ret;i++)
120     { uchar *despack=NULL;
121       if(uloi_get_oiddes(coninfo,ULOI_DOII,oids[i],&despack)>=0){
122         oi_var_oiddes_print(oids[i],despack);
123         if(despack)
124           free(despack);
125       }else{
126         printf(" %5d no reply\n",oids[i]);
127       }
128     }
129     free(oids);
130     ret=uloi_get_oids(coninfo,ULOI_QOIO,&oids);
131     if(ret<0) 
132     { fprintf(stderr,"oi_var_test : uloi_get_oids returned %d\n",ret);
133       return -1;
134     };
135     printf("module recognized get vars:\n");
136     for(i=0;i<ret;i++)
137     { uchar *despack=NULL;
138       if(uloi_get_oiddes(coninfo,ULOI_DOIO,oids[i],&despack)>=0){
139         oi_var_oiddes_print(oids[i],despack);
140         if(despack)
141           free(despack);
142       }else{
143         printf(" %5d no reply\n",oids[i]);
144       }
145     }
146     free(oids);
147   }
148
149   uloi_close(coninfo);
150
151   return 0;
152 }
153
154 int print_modules(int max_addr)
155 { int ret;
156   int i;
157   ul_fd_t ul_fd;
158   uchar *p;
159   void *buf=NULL;
160   int buf_len;
161   int count=0; 
162   ul_fd=ul_open(ul_dev_name, NULL);
163   if(ul_fd==UL_FD_INVALID)
164     { perror("print_modules : uLan open failed");return -1;};
165   for(i=1;i<=max_addr;i++)
166   {
167     ret=ul_send_query_wait(ul_fd,i,UL_CMD_SID,
168                         UL_BFL_NORE|UL_BFL_PRQ,NULL,0,&buf,&buf_len);
169     if(ret>=0)
170     { count++;
171       p=(uchar*)buf;
172       for(ret=0;(ret<buf_len)&&*p;ret++,p++);
173       printf("%2d:  ",i);
174       fwrite(buf,ret,1,stdout);
175       printf("\n");
176     };
177     if(buf) free(buf);
178     buf=NULL;
179   };
180   ul_close(ul_fd);
181   return count;
182 };
183
184 int debug_kernel(int debug_msk)
185 { int ret;
186   ul_fd_t ul_fd;
187   ul_fd=ul_open(ul_dev_name, NULL);
188   if(ul_fd==UL_FD_INVALID) { perror("send_cmd_go : uLan open failed");return -1;};
189   ret=ul_drv_debflg(ul_fd,debug_msk);
190   ul_close(ul_fd);
191   return ret;
192 };
193
194 float IEEE4tofloat(void *p)
195 {
196  uchar *up=p;
197  long mantisa;
198  int exponent;
199  mantisa=up[0]|((unsigned)up[1]<<8)|
200          ((unsigned long)up[2]<<16)|0x800000;
201  if(up[3]&0x80) mantisa=-mantisa;
202  exponent=(up[2]>>7)|(((int)up[3]&0x7f)<<1);
203  return ldexp((float)mantisa,exponent-0x7f-23);
204 };
205
206 uchar test_cmd[]={0,0,0,204,0,234,0,20,0,204,0,0,0};
207
208 int wl_set_wlen(uloi_coninfo_t *coninfo,unsigned wlen)
209 { unsigned u;
210   int ret;
211   ret=uloi_set_var_u2(coninfo,204,wlen);
212   if(ret<0) return ret;
213   while(1)
214   { sleep(1);
215     ret=uloi_get_var_u2(coninfo,204,&u);
216     if(ret<0) return ret;
217     if(u==0x8000) continue;
218     if(u&0x8000) return -1;
219     if(u!=wlen) return -1;
220     return wlen;
221   }
222 }
223
224 int wl_scan(void)
225 {
226   int ret;
227   ul_fd_t ul_fd;
228   ul_msginfo msginfo;
229   uloi_coninfo_t *coninfo;
230   uchar  buf[10];
231   struct timeval timeout;
232   unsigned wlen;
233   fd_set set;
234   float  abs;
235
236   coninfo=uloi_open(ul_dev_name,module,0x10,0,10);
237   if(!coninfo) { perror("wl_scan : uLan OI open failed");return -1;};
238   
239   if(time_co_fl)
240   { uloi_set_var_u2(coninfo,208,time_co);
241   }
242
243   if(adc_chan_fl)
244   { uloi_set_var_u2(coninfo,209,adc_chan);
245   }
246
247   if(lampctrl_fl)
248   { uloi_set_var_u2(coninfo,207,lampctrl);
249   }
250
251   wlen=wl_begin;
252   if(!wl_step) wl_end=wlen;
253   if(((wl_begin<=wl_end)&&(wl_step>=0))
254    ||((wl_begin>=wl_end)&&(wl_step<0))) while(1)
255   { ret=wl_set_wlen(coninfo,wlen);
256     fprintf(stderr,"ul_set_wlen returned %d\n",ret);
257     sleep(wl_pause/1000);
258     if(zero_fl)
259     { ret=uloi_send_cmd(coninfo,255);
260       fprintf(stderr,"uloi_send_cmd returned %d\n",ret);
261     
262     }
263     ret=uloi_get_var(coninfo,220,buf,4);
264     fprintf(stderr,"uloi_get_var returned %d\n",ret);
265     printf("%d %f\n",wlen,IEEE4tofloat(buf));
266     
267     if((wlen==wl_end)||!wl_step) break;
268     wlen+=wl_step;
269     if(((wl_step>0)&&(wlen>wl_end))
270      ||((wl_step<0)&&(wlen<wl_end))) wlen=wl_end;
271   }
272
273   uloi_close(coninfo);
274   return 0;
275
276   ul_fd=ul_open(ul_dev_name, NULL);
277   if(ul_fd==UL_FD_INVALID) { perror("wl_scan : uLan open failed");return -1;};
278   memset(&msginfo,0,sizeof(msginfo));
279   msginfo.sadr=module;
280   msginfo.cmd=UL_CMD_LCDABS;
281   ret=ul_addfilt(ul_fd,&msginfo);
282   if(ret<0) { printf("wl_scan : add filter failed\n");return ret;};
283   msginfo.cmd=UL_CMD_LCDMRK;
284   ret=ul_addfilt(ul_fd,&msginfo);
285   if(ret<0) { printf("wl_scan : add filter failed\n");return ret;};
286
287   for(;;)
288   {
289   #ifndef WITHOUT_SYS_SELECT
290     FD_ZERO (&set);
291     FD_SET (ul_fd2sys_fd(ul_fd), &set);
292     FD_SET (0, &set);
293     timeout.tv_sec = 10;
294     timeout.tv_usec = 0;
295     while ((ret=select(FD_SETSIZE,&set, NULL, NULL,&timeout))==-1
296             &&errno==-EINTR);
297     if(FD_ISSET(0,&set))
298       {printf("\nwl_scan : break by user\n");break;};
299     if(ret<0) return ret;
300     if(!ret) continue;
301   #else /* WITHOUT_SYS_SELECT */
302     while (ul_fd_wait(ul_fd,1)<=0);
303   #endif /* WITHOUT_SYS_SELECT */
304     ret=ul_acceptmsg(ul_fd,&msginfo);
305     if(ret<0) 
306     { printf("wl_scan : problem to accept message\n");
307       return ret;
308     };
309     if(msginfo.flg&UL_BFL_FAIL) 
310       {ul_freemsg(ul_fd);continue;};
311     if(msginfo.cmd==UL_CMD_LCDABS)
312     { while((ret=ul_read(ul_fd,buf,4))==4)
313       {
314         abs=IEEE4tofloat(buf);
315         printf("%9.6f\n",abs);
316       };
317     }else 
318     if(msginfo.cmd==UL_CMD_LCDMRK)
319     {       
320       memset(buf,0,sizeof(buf));
321       ul_read(ul_fd,buf,4);
322       printf("MARK %d %d\n",buf[0],buf[1]);
323     };
324     ul_freemsg(ul_fd);
325   };
326   ul_close(ul_fd);
327   uloi_close(coninfo);
328   return 0;
329 };
330
331 static void
332 usage(void)
333 {
334   printf("usage: ul_lcabsp <parameters> <hex_file>\n");
335   printf("  -d, --uldev  <name>      name of uLan device [/dev/ulan]\n");
336   printf("  -m, --module <num>       messages from/to module\n");
337   printf("  -p, --print  <max>       print modules to max address\n");
338   printf("      --debug-kernel <m>   flags to debug kernel\n");
339   printf("  -b, --begin  <wlen>      initial wavelength\n");
340   printf("  -e, --end    <wlen>      final wavelength\n");
341   printf("  -s, --step   <wlen>      wavelength step\n");
342   printf("  -z, --zero               zero absorbance\n");
343   printf("  -t, --time-const <s>     ADC filter time constant\n");
344   printf("  -c, --adc-chan <c>       imput chanel of ADC\n");
345   printf("  -l, --lamp  <l>          lamp control\n");
346   printf("  -o, --oi-var <oid>=<v>   uLan OI variable set\n");
347   printf("  -o, --oi-var <oid>?      uLan OI variable read\n");
348   printf("  -V, --version            show version\n");
349   printf("  -h, --help               this usage screen\n");
350 }
351
352 int main(int argc,char *argv[])
353 {
354   static struct option long_opts[] = {
355     { "uldev", 1, 0, 'd' },
356     { "module",1, 0, 'm' },
357     { "print", 1, 0, 'p' },
358     { "version",0,0, 'V' },
359     { "help",  0, 0, 'h' },
360     { "debug-kernel", 1, 0, 'D' },
361     { "begin", 1, 0, 'b' },
362     { "end",   1, 0, 'e' },
363     { "step",  1, 0, 's' },
364     { "pause", 1, 0, 'P' },
365     { "zero",  0, 0, 'z' },
366     { "time-const", 1, 0, 't' },
367     { "adc-chan", 1, 0, 'c' },
368     { "lanmp", 1, 0, 'l' },
369     { "oi-var",1, 0, 'o' },
370     { 0, 0, 0, 0}
371   };
372   int  opt;
373   char c;
374
375  #ifndef HAS_GETOPT_LONG
376   while ((opt = getopt(argc, argv, "d:m:p:Vhb:e:s:P:zt:c:l:o:")) != EOF)
377  #else
378   while ((opt = getopt_long(argc, argv, "d:m:p:Vhb:e:s:P:zt:c:l:o:",
379                             &long_opts[0], NULL)) != EOF)
380  #endif
381     switch (opt) {
382     case 'd':
383       ul_dev_name=optarg;
384       break;
385     case 'm':
386       module = strtol(optarg,NULL,0);
387       break;
388     case 'p':
389       prt_modules = strtol(optarg,NULL,0);
390       break;
391     case 'D':
392       debugk = strtol(optarg,NULL,0);
393       debugk_flg=1;
394       break;
395     case 'V':
396       fputs("uLan lcscan v0.7\n", stdout);
397       exit(0);
398     case 'b':
399       wl_begin = strtol(optarg,NULL,0);
400       break;
401     case 'e':
402       wl_end = strtol(optarg,NULL,0);
403       break;
404     case 's':
405       wl_step = strtol(optarg,NULL,0);
406       break;
407     case 'P':
408       wl_pause = strtol(optarg,NULL,0);
409       break;
410     case 'z':
411       zero_fl=1;
412       break;
413     case 't':
414       time_co_fl=1;
415       time_co=strtod(optarg,NULL)*100;
416       break;
417     case 'c':
418       adc_chan_fl=1;
419       adc_chan=strtol(optarg,NULL,0);
420       break;
421     case 'l':
422       lampctrl_fl=1;
423       lampctrl=strtol(optarg,NULL,0);
424       break;
425     case 'o':
426       oi_var_fl=1;
427       oi_var_rd=0;
428       oi_var_aoid=optarg;
429       oi_var_val=strpbrk(optarg,":=?");
430       if(oi_var_val)
431       { c=*oi_var_val;
432         *(oi_var_val++)=0;
433         switch(c)
434         { case '?':
435             oi_var_val=NULL;
436             oi_var_rd=1;
437             break;
438         }
439       }
440       break;
441     case 'h':
442     default:
443       usage();
444       exit(opt == 'h' ? 0 : 1);
445     }
446
447   if(debugk_flg) debug_kernel(debugk);
448
449   if(prt_modules) print_modules(prt_modules);
450   
451   if(oi_var_fl) oi_var_test(oi_var_aoid,oi_var_val,NULL,NULL);
452   
453   if(!debugk_flg&&!prt_modules&&!oi_var_fl)
454     wl_scan();
455
456   return 0;
457 }