免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6352 | 回复: 4
打印 上一主题 下一主题

[C] 很难解决的问题 在Fedora9下编译 系统报错:'F_SETSIG'未声明 。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-25 20:19 |只看该作者 |倒序浏览
本帖最后由 yiliyate 于 2010-06-26 14:46 编辑
  1. /*        这是第一个文件 名称为:   cssl.h
  2. Copyright 2003 Marcin Siennicki <m.siennicki@cloos.pl>
  3. * see COPYING file for details */

  4. #ifndef __CSSL_H__
  5. #define __CSSL_H__

  6. #include <stdint.h>
  7. #include <signal.h>
  8. #include <termios.h>


  9. typedef void (*cssl_callback_t)(int id,  /* id passed to callback */
  10.                                 uint8_t *buffer, /* data received */
  11.                                 int len); /* length of data in bytes */

  12. typedef struct __cssl_t {

  13.     uint8_t buffer[255];      /* input buffer */

  14.     int fd;                   /* tty file descriptor */

  15.     struct termios tio;       /* termios structure for the port */
  16.     struct termios oldtio;    /* old termios structure */

  17.     cssl_callback_t callback; /* callback function */

  18.     int id;                   /* id which would be passed to callback */

  19.     struct __cssl_t *next;

  20. } cssl_t;

  21. typedef enum {
  22.     CSSL_OK,                 /* everything is all right */
  23.     CSSL_ERROR_NOSIGNAL,     /* there's no free signal */
  24.     CSSL_ERROR_NOTSTARTED,   /* you should first start cssl */
  25.     CSSL_ERROR_NULLPOINTER,  /* you gave a null pointer to the function */
  26.     CSSL_ERROR_OOPS,         /* internal error, something's erong */
  27.     CSSL_ERROR_MEMORY,       /* there's no memory for cssl_t structure */
  28.     CSSL_ERROR_OPEN          /* file doesnt exist or you aren't good user */
  29. } cssl_error_t;

  30. /* get the error message */
  31. const char *cssl_geterrormsg();

  32. /* get the error code */
  33. int cssl_geterror();

  34. /* start the cssl */
  35. void cssl_start();

  36. /* finish all jobs, clear memory, etc. */
  37. void cssl_stop();

  38. /* alloc new cssl_t struct and open the port */
  39. cssl_t *cssl_open(const char *fname, /* pathname of port file,
  40.                                       * for example "/dev/ttyS0" */
  41.                   cssl_callback_t callback, /* callback function
  42.                                              * If you dont want
  43.                                              * event driven reading - set
  44.                                              * it to NULL */
  45.                   int id,     /* your own id for the port, it can help
  46.                                * to identify the port in callback f.*/
  47.                   int baud,   /* baudrate, integer, for example 19200 */
  48.                   int bits,   /* data bits: 7 or 8 */
  49.                   int parity, /* parity: 0 - none, 1-odd, 2-even */
  50.                   int stop);  /* stop bits: 1 or 2 */

  51. /* closes the port, and frees its cssl_t struct */
  52. void cssl_close(cssl_t *serial);

  53. /* setups the port, look at cssl_open */
  54. void cssl_setup(cssl_t *serial,
  55.                    int baud,
  56.                    int bits,
  57.                    int parity,
  58.                    int stop);

  59. void cssl_setflowcontrol(cssl_t *serial,
  60.                          int rtscts,   /* Boolean:
  61.                                         * 0 - no rts/cts control,
  62.                                         * 1 - rts/cts control
  63.                                         */
  64.                          int xonxoff); /* Boolean:
  65.                                         * 0 - no xon/xoff,
  66.                                         * 1 - xon/xoff
  67.                                         */

  68. /* sends a char via serial port */
  69. void cssl_putchar(cssl_t *serial,
  70.                      char c);

  71. /* sends a null terminated string */
  72. void cssl_putstring(cssl_t *serial,
  73.                        char *str);

  74. /* sends a data of known size */
  75. void cssl_putdata(cssl_t *serial,
  76.                   uint8_t *data, /* data */
  77.                   int datalen);  /* length of data */

  78. /* waits until all data has been transmited */

  79. void cssl_drain(cssl_t *serial);

  80. /*======================================
  81. * Blocking mode
  82. */

  83. /* Sets port timeout (deciseconds) in blocking mode */
  84. void cssl_settimeout(cssl_t *serial, int timeout);

  85. /* reads a char in blocking mode */
  86. int cssl_getchar(cssl_t *serial);

  87. /* reads a data to a buffer in blocking mode*/
  88. int cssl_getdata(cssl_t *serial,
  89.                  uint8_t *buffer,  /* buffer for data */
  90.                  int size);        /* buffer size */


  91. #endif /* __CSSL_H__ */







  92. /*这是第二个文件 名称为: cssl.c
  93. Copyright 2003 Marcin Siennicki <m.siennicki@cloos.pl>
  94. * see COPYING file for details */

  95. #include <stdio.h>

  96. #include <string.h>
  97. #include <termios.h>
  98. #include <sys/types.h>
  99. #include <sys/stat.h>
  100. #include <fcntl.h>
  101. #include <stdlib.h>
  102. #include <signal.h>
  103. #include <unistd.h>

  104. #include "cssl.h"

  105. /*
  106. * Static variables and constants
  107. */

  108. /* signal number for serial i/o read */
  109. static int CSSL_SIGNAL=0;

  110. /* boolean that say if we have started cssl */
  111. static int cssl_started=0;

  112. /* sigactions */
  113. static struct sigaction sa;
  114. static struct sigaction oldsa;

  115. /* head of the cssl_t list */
  116. static cssl_t *head=0;

  117. /* error messages table */
  118. static const char *cssl_errors[]= {
  119.     "cssl: OK",
  120.     "cssl: there's no free signal",
  121.     "cssl: not started",
  122.     "cssl: null pointer",
  123.     "cssl: oops",
  124.     "cssl: out of memory",
  125.     "cssl: cannot open file"
  126. };

  127. /* status of last cssl function */
  128. static cssl_error_t cssl_error=CSSL_OK;

  129. /* prototype of signal handler */
  130. static void cssl_handler(int signo, siginfo_t *info, void *ignored);


  131. /**************************************
  132. * Public functions
  133. **************************************/

  134. /*-------------------------------------
  135. * Error handling
  136. */

  137. /* gets the last operation status message */
  138. const char *cssl_geterrormsg()
  139. {
  140.     return cssl_errors[cssl_error];
  141. }


  142. /* gets the last error code */
  143. int cssl_geterror()
  144. {
  145.     return cssl_error;
  146. }

  147. /*-------------------------------------
  148. * Startig/stoping cssl
  149. */

  150. /* starts cssl */
  151. void cssl_start()
  152. {
  153.     int sig;

  154.     if (cssl_started) {
  155.         return;
  156.     }

  157.     /* Here we scan for unused real time signal */
  158.     sig=SIGRTMIN;

  159.     do {
  160.        
  161.         /* get old sigaction */
  162.         sigaction(sig,0,&oldsa);
  163.        
  164.         /* if signal's handler is empty */
  165.         if (oldsa.sa_handler == 0)
  166.         {
  167.             /* set the signal handler, and others */
  168.             CSSL_SIGNAL=sig;
  169.             sa.sa_sigaction = cssl_handler;
  170.             sa.sa_flags = SA_SIGINFO;
  171.             sa.sa_restorer = NULL;
  172.             sigemptyset(&sa.sa_mask);
  173.             sigaction(CSSL_SIGNAL,&sa,0);

  174.             /* OK, the cssl is started */
  175.             cssl_started=1;
  176.             cssl_error=CSSL_OK;
  177.             return;
  178.         } else {
  179.             /* signal handler was not empty,
  180.                restore original */
  181.             sigaction(CSSL_SIGNAL,&oldsa,0);
  182.         }
  183.         sig++;
  184.     } while(sig<=SIGRTMAX);
  185.    

  186.     /* Sorry, there's no free signal */
  187.     cssl_error=CSSL_ERROR_NOSIGNAL;
  188.    
  189. }

  190. /* stops the cssl */
  191. void cssl_stop()
  192. {
  193.     /* if not started we do nothing */
  194.     if (!cssl_started)
  195.         return;

  196.     /* we close all ports, and free the list */
  197.     while (head)
  198.         cssl_close(head);

  199.     /* then we remove the signal handler */
  200.     sigaction(CSSL_SIGNAL,&oldsa,NULL);

  201.     /* And at least : */
  202.     cssl_started=0;
  203.     cssl_error=CSSL_OK;
  204. }

  205. /*-------------------------------------
  206. * Basic port operation - open/close
  207. */


  208. /* opens the port */
  209. cssl_t *cssl_open(const char *fname,
  210.                   cssl_callback_t callback,
  211.                   int id,
  212.                   int baud,
  213.                   int bits,
  214.                   int parity,
  215.                   int stop)
  216. {
  217.     cssl_t *serial;

  218.     if (!cssl_started) {
  219.         cssl_error=CSSL_ERROR_NOTSTARTED;
  220.         return NULL;
  221.     }
  222.    
  223.     /* create new cssl_t structure */
  224.     serial=calloc(1,sizeof(cssl_t));

  225.     /* oops, no memory */
  226.     if (!serial) {
  227.         cssl_error=CSSL_ERROR_MEMORY;
  228.         return 0;
  229.     }

  230.     /* opening the file */
  231.     if(callback) {
  232.         /* user wants event driven reading */
  233.         serial->fd=open(fname,O_RDWR|O_NOCTTY|O_NONBLOCK);
  234.         fcntl(serial->fd,F_SETSIG,CSSL_SIGNAL);
  235.         fcntl(serial->fd,F_SETOWN,getpid());
  236.         fcntl(serial->fd,F_SETFL,O_ASYNC|O_NONBLOCK);
  237.     } else {
  238.         /* the read/write operations will be bloking */
  239.         serial->fd=open(fname,O_RDWR|O_NOCTTY);
  240.     }

  241.     /* oops, cannot open */
  242.     if (serial->fd == -1) {
  243.         cssl_error=CSSL_ERROR_OPEN;
  244.         free(serial);
  245.         return NULL;
  246.     }

  247.     /* we remember old termios */
  248.     tcgetattr(serial->fd,&(serial->oldtio));
  249.    
  250.     /* now we set new values */
  251.     cssl_setup(serial,baud,parity,bits,stop);

  252.     /* and id */
  253.     serial->id=id;

  254.     /* then set the callback */
  255.     serial->callback=callback;
  256.    
  257.     /* we add the serial to our list */
  258.     serial->next=head;
  259.     head=serial;
  260.        
  261.     cssl_error=CSSL_OK;

  262.     return serial;
  263. }


  264. /* closes file, removes serial from the list and frees it */
  265. void cssl_close(cssl_t *serial)
  266. {
  267.     cssl_t *cur;
  268.    
  269.     if (!cssl_started) {
  270.         cssl_error=CSSL_ERROR_NOTSTARTED;
  271.         return;
  272.     }
  273.    
  274.     if (!serial) {
  275.         cssl_error=CSSL_ERROR_NULLPOINTER;
  276.         return;
  277.     }
  278.    
  279.     /* first we flush the port */
  280.     tcflush(serial->fd,TCOFLUSH);
  281.     tcflush(serial->fd,TCIFLUSH);
  282.    
  283.     /* then we restore old settings */
  284.     tcsetattr(serial->fd,TCSANOW,&(serial->oldtio));
  285.    
  286.     /* and close the file */
  287.     close(serial->fd);
  288.    
  289.     /* now we can remove the serial from the list */

  290.     if (head==serial) {
  291.         head=serial->next;
  292.         free(serial);
  293.         cssl_error=CSSL_OK;
  294.         return;
  295.     }

  296.     for (cur=head;cur;cur=cur->next) {
  297.         if (cur->next==serial) {
  298.             cur->next=serial->next;
  299.             free(serial);
  300.             cssl_error=CSSL_OK;
  301.             return;
  302.         }
  303.     }

  304.     /* we should never reach there,
  305.        it means, that serial was not found in the list */
  306.     cssl_error=CSSL_ERROR_OOPS;
  307. }


  308. /*-------------------------------------
  309. * Port setup
  310. */

  311. /* sets up the port parameters */
  312. void cssl_setup(cssl_t *serial,
  313.                    int baud,
  314.                    int bits,
  315.                    int parity,
  316.                    int stop)
  317. {
  318.     tcflag_t baudrate;
  319.     tcflag_t databits;
  320.     tcflag_t stopbits;
  321.     tcflag_t checkparity;

  322.     if (!cssl_started) {
  323.         cssl_error=CSSL_ERROR_NOTSTARTED;
  324.         return;
  325.     }
  326.    
  327.     if (!serial) {
  328.         cssl_error=CSSL_ERROR_NULLPOINTER;
  329.         return;
  330.     }   

  331.     /* get the propr baudrate */
  332.     switch (baud) {
  333.     case 75:
  334.         baudrate=B75;
  335.         break;
  336.     case 110:
  337.         baudrate=B110;
  338.         break;
  339.     case 150:
  340.         baudrate=B150;
  341.         break;
  342.     case 300:
  343.         baudrate=B300;
  344.         break;
  345.     case 600:
  346.         baudrate=B600;
  347.         break;
  348.     case 1200:
  349.         baudrate=B1200;
  350.         break;
  351.     case 2400:
  352.         baudrate=B2400;
  353.         break;
  354.     case 4800:
  355.         baudrate=B4800;
  356.         break;
  357.     case 9600:
  358.         baudrate=B9600;
  359.         break;
  360.     case 19200:
  361.         baudrate=B19200;
  362.         break;
  363.     case 38400:
  364.         baudrate=B38400;
  365.         break;
  366.     case 57600:
  367.         baudrate=B57600;
  368.         break;
  369.     case 115200:
  370.         baudrate=B115200;
  371.         break;
  372.     default:
  373.         baudrate=B9600;
  374.     }

  375.     /* databits */
  376.     switch (bits) {
  377.     case 7:
  378.         databits=CS7;
  379.         break;
  380.     case 8:
  381.         databits=CS8;
  382.         break;
  383.     default:
  384.         databits=CS8;
  385.     }
  386.    
  387.     /* parity, */
  388.     switch (parity) {
  389.     case 0:
  390.         checkparity=0;
  391.         break;
  392.     case 1:   //odd
  393.         checkparity=PARENB|PARODD;
  394.         break;
  395.     case 2:
  396.         checkparity=PARENB;
  397.         break;
  398.     default:
  399.         checkparity=0;
  400.     }
  401.    
  402.     /* and stop bits */
  403.     switch (stop) {
  404.     case 1:
  405.         stopbits=0;
  406.         break;
  407.     case 2:
  408.         stopbits=CSTOPB;
  409.         break;
  410.     default:
  411.         stopbits=0;
  412.     }
  413.    
  414.     /* now we setup the values in port's termios */
  415.     serial->tio.c_cflag=baudrate|databits|checkparity|stopbits|CLOCAL|CREAD;
  416.     serial->tio.c_iflag=IGNPAR;
  417.     serial->tio.c_oflag=0;
  418.     serial->tio.c_lflag=0;
  419.     serial->tio.c_cc[VMIN]=1;
  420.     serial->tio.c_cc[VTIME]=0;

  421.     /* we flush the port */
  422.     tcflush(serial->fd,TCOFLUSH);
  423.     tcflush(serial->fd,TCIFLUSH);
  424.    
  425.     /* we send new config to the port */
  426.     tcsetattr(serial->fd,TCSANOW,&(serial->tio));

  427.     cssl_error=CSSL_OK;
  428. }

  429. void cssl_setflowcontrol(cssl_t *serial,
  430.                          int rtscts,
  431.                          int xonxoff)
  432. {
  433.     if (!cssl_started) {
  434.         cssl_error=CSSL_ERROR_NOTSTARTED;
  435.         return;
  436.     }
  437.    
  438.     if (!serial) {
  439.         cssl_error=CSSL_ERROR_NULLPOINTER;
  440.         return;
  441.     }   

  442.     /* We setup rts/cts (hardware) flow control */
  443.     if (rtscts) {
  444.         serial->tio.c_cflag |= CRTSCTS;
  445.     } else {
  446.         serial->tio.c_cflag &= ~CRTSCTS;
  447.     }
  448.    
  449.     /* We setup xon/xoff (soft) flow control */
  450.     if (xonxoff) {
  451.         serial->tio.c_iflag |= (IXON|IXOFF);
  452.     } else {
  453.         serial->tio.c_iflag &= ~(IXON|IXOFF);
  454.     }
  455.    
  456.     tcsetattr(serial->fd,TCSANOW,&(serial->tio));

  457.     cssl_error=CSSL_OK;
  458. }



  459. /* Blocking mode: sets the timeout in
  460.    hundreds of miliseconds */
  461. void cssl_settimeout(cssl_t *serial, int timeout)
  462. {
  463.     if (!cssl_started) {
  464.         cssl_error=CSSL_ERROR_NOTSTARTED;
  465.         return;
  466.     }
  467.    
  468.     if (!serial) {
  469.         cssl_error=CSSL_ERROR_NULLPOINTER;
  470.         return;
  471.     }   

  472.     serial->tio.c_cc[VTIME]=timeout;
  473.    
  474.     tcsetattr(serial->fd,TCSANOW,&(serial->tio));

  475.     cssl_error=CSSL_OK;
  476. }


  477. /*-------------------------------------
  478. * Serial communication
  479. */

  480. /* sending a char */
  481. void cssl_putchar(cssl_t *serial,
  482.                      char c)
  483. {
  484.     if (!cssl_started) {
  485.         cssl_error=CSSL_ERROR_NOTSTARTED;
  486.         return;
  487.     }
  488.    
  489.     if (!serial) {
  490.         cssl_error=CSSL_ERROR_NULLPOINTER;
  491.         return;
  492.     }   

  493.     write(serial->fd,&c,1);
  494. }

  495. /* sending a null-terminated string */
  496. void cssl_putstring(cssl_t *serial,
  497.                      char *str)
  498. {
  499.     if (!cssl_started) {
  500.         cssl_error=CSSL_ERROR_NOTSTARTED;
  501.         return;
  502.     }
  503.    
  504.     if (!serial) {
  505.         cssl_error=CSSL_ERROR_NULLPOINTER;
  506.         return;
  507.     }   
  508.     write(serial->fd,str,strlen(str));
  509. }


  510. /* sending a data of known size */
  511. void cssl_putdata(cssl_t *serial,
  512.                   uint8_t *data,
  513.                   int datalen)
  514. {
  515.     if (!cssl_started) {
  516.         cssl_error=CSSL_ERROR_NOTSTARTED;
  517.         return;
  518.     }
  519.    
  520.     if (!serial) {
  521.         cssl_error=CSSL_ERROR_NULLPOINTER;
  522.         return;
  523.     }   

  524.     write(serial->fd,data,datalen);
  525. }

  526. void cssl_drain(cssl_t *serial)
  527. {
  528.     if (!cssl_started) {
  529.         cssl_error=CSSL_ERROR_NOTSTARTED;
  530.         return;
  531.     }
  532.    
  533.     if (!serial) {
  534.         cssl_error=CSSL_ERROR_NULLPOINTER;
  535.         return;
  536.     }   

  537.     tcdrain(serial->fd);
  538. }

  539. /* blocking mode: reading a char */
  540. int cssl_getchar(cssl_t *serial)
  541. {
  542.     int result;
  543.     uint8_t c;
  544.    
  545.     result=read(serial->fd,&c,sizeof(c));
  546.     if (result<=0)
  547.         return -1;
  548.    
  549.     return c;
  550. }

  551. /* blocking mode: reading a data buffer */
  552. int cssl_getdata(cssl_t *serial,
  553.                  uint8_t *buffer,
  554.                  int size)
  555. {
  556.     return read(serial->fd,buffer,size);
  557. }

  558. /*------------------------------------------*/

  559. /* The most important: signal handler */
  560. void cssl_handler(int signo, siginfo_t *info, void *ignored)
  561. {
  562.     cssl_t *cur;
  563.     int n;

  564.     /* is this signal which says about
  565.        incoming of the data? */
  566.     if (info->si_code==POLL_IN) {

  567.         /* Yes, we got some data */
  568.         for(cur=head;cur;cur=cur->next) {

  569.             /* Let's find proper cssl_t */
  570.             if (cur->fd==info->si_fd) {

  571.                 /* Got it */
  572.                 n=read(cur->fd,cur->buffer,255);

  573.                 /* Execute callback */
  574.                 if ((n>0)&&(cur->callback))
  575.                     cur->callback(cur->id,cur->buffer,n);
  576.                 return;
  577.             }
  578.         }
  579.     }
  580. }







  581. /*这是第三个文件 名称为: test.c
  582. Example application of Columbo Simple Serial Library
  583. * Copyright 2003 Marcin Siennicki <m.siennicki@cloos.pl>
  584. * see COPYING file for details */

  585. #include <stdio.h>
  586. #include <unistd.h>

  587. #include "cssl.h"


  588. /* if it is time to finish */
  589. static int finished=0;


  590. /* example callback, it gets its id, buffer, and buffer length */
  591. static void callback(int id,
  592.                      uint8_t *buf,
  593.                      int length)
  594. {
  595.     int i;

  596.     for(i=0;i<length;i++) {

  597.         switch (buf[i]) {

  598.         case 0x04:  /* Ctrl-D */
  599.             finished=1;
  600.             return;

  601.         case '\r': /* replace \r with \n */
  602.             buf[i]='\n';

  603.         }

  604.         putchar(buf[i]);
  605.     }

  606.     fflush(stdout);
  607. }


  608. int main()
  609. {
  610.     cssl_t *serial;

  611.     cssl_start();
  612.    
  613.     serial=cssl_open("/dev/ttyS0",callback,0,
  614.                      19200,8,0,1);

  615.     if (!serial) {
  616.         printf("%s\n",cssl_geterrormsg());
  617.         return -1;
  618.     }

  619.     cssl_putstring(serial,"Type some data, ^D finishes.");

  620.     while (!finished)
  621.         pause();

  622.     printf("\n^D - we exit\n");

  623.     cssl_close(serial);
  624.     cssl_stop();

  625.     return 0;
  626. }
复制代码
在Fedora9环境下编译 : gcc cssl.h cssl.c test.c -o test  
系统报错: 'F_SETSIG'未声明(在此函数内第一次使用)

据分析,出错地方是在cssl.c代码
   if(callback) {
        /* user wants event driven reading */
        serial->fd=open(fname,O_RDWR|O_NOCTTY|O_NONBLOCK);
        fcntl(serial->fd,F_SETSIG,CSSL_SIGNAL);
        fcntl(serial->fd,F_SETOWN,getpid());
        fcntl(serial->fd,F_SETFL,O_ASYNC|O_NONBLOCK);
    } else {
        /* the read/write operations will be bloking */
        serial->fd=open(fname,O_RDWR|O_NOCTTY);
    }

中的    fcntl(serial->fd,F_SETSIG,CSSL_SIGNAL);

但没找到解决方法,请高手指点

论坛徽章:
2
摩羯座
日期:2013-10-10 14:29:04天蝎座
日期:2014-01-03 09:14:49
2 [报告]
发表于 2010-06-26 21:38 |只看该作者
没有找到相应的头文件?

论坛徽章:
0
3 [报告]
发表于 2010-06-27 08:48 |只看该作者
回复 2# EricFisher


    感觉该包含的头文件都包含了,头疼中......

论坛徽章:
0
4 [报告]
发表于 2010-06-27 09:11 |只看该作者
加上编译开关: -D_GNU_SOURCE

论坛徽章:
0
5 [报告]
发表于 2010-06-28 16:00 |只看该作者
回复 4# 没本


    已解决,的确是加这个编译开关,谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP