~ubuntu-branches/debian/jessie/digitemp/jessie

« back to all changes in this revision

Viewing changes to userial/ds9097/linuxses.c

  • Committer: Bazaar Package Importer
  • Author(s): Jesus Roncero
  • Date: 2004-09-01 01:34:37 UTC
  • Revision ID: james.westby@ubuntu.com-20040901013437-eicsrrd40dr371u0
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2003, Erik Rigtorp <erkki@linux.nu>
 
2
 * All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 * 3. Neither the name of Erik Rigtorp nor the names of his contributors
 
13
 *    may be used to endorse or promote products derived from this software
 
14
 *    without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
26
 * SUCH DAMAGE.
 
27
 * 
 
28
 * 
 
29
 * linuxses.c (0.20)
 
30
 * 
 
31
 *   * Cleaned up everything.
 
32
 * 
 
33
 *   * Added printing of error messages.
 
34
 * 
 
35
 *  -- Erik Rigtorp <erkki@linux.nu>  Thu, 05 Jun 2003 00:57:44 +0200
 
36
 */
 
37
 
 
38
#include <unistd.h>
 
39
#include <termios.h>
 
40
#include <fcntl.h>
 
41
#include <ownet.h>
 
42
 
 
43
/* local function prototypes */
 
44
SMALLINT owAcquire(int,char *);
 
45
void     owRelease(int);
 
46
 
 
47
 
 
48
int fd[MAX_PORTNUM]; /* a list of filedescriptors for serial ports */
 
49
struct termios term[MAX_PORTNUM]; /* Current termios settings */
 
50
struct termios term_orig[MAX_PORTNUM]; /* backup termios settings */
 
51
 
 
52
 
 
53
/* Attempt to acquire a 1-Wire net. Associate 'portnum' with the serial port
 
54
 * with name 'port_zstr'. Returns TRUE on success. */
 
55
SMALLINT owAcquire(int portnum, char *port_zstr)
 
56
{  
 
57
   /* Open the serial port */
 
58
   if ((fd[portnum] = open(port_zstr, O_RDWR)) == -1)
 
59
     {
 
60
        OWERROR(OWERROR_GET_SYSTEM_RESOURCE_FAILED);
 
61
        perror("owAcquire: failed to open device");
 
62
        return FALSE;
 
63
     }
 
64
   
 
65
   /* Get device settings */
 
66
   if(tcgetattr(fd[portnum], &term[portnum] ) < 0 )
 
67
     {
 
68
        OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
 
69
        perror("owAcquire: failed to set attributes");
 
70
        close(fd[portnum]);
 
71
        return FALSE;
 
72
     }
 
73
   
 
74
   /* Save a backup */
 
75
   term_orig[portnum] = term[portnum];
 
76
   
 
77
   /* Reset all settings */
 
78
   term[portnum].c_iflag = 0;
 
79
   term[portnum].c_oflag = 0;
 
80
   term[portnum].c_lflag = 0;   
 
81
   term[portnum].c_cflag = 0;   
 
82
 
 
83
   /* 1 byte at a time, no timer */
 
84
   term[portnum].c_cc[VMIN] = 1;  
 
85
   term[portnum].c_cc[VTIME] = 0;
 
86
      
 
87
   /* 6 data bits, Receiver enabled, Hangup, Dont change "owner" */
 
88
   term[portnum].c_cflag |= CS6 | CREAD | HUPCL | CLOCAL;
 
89
   
 
90
   /* Set input and output speed to 115.2k */
 
91
   cfsetispeed(&term[portnum], B115200);
 
92
   cfsetospeed(&term[portnum], B115200);
 
93
   
 
94
   /* set the attributes */
 
95
   if(tcsetattr(fd[portnum], TCSANOW, &term[portnum]) < 0 )
 
96
     {
 
97
        OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
 
98
        perror("owAcquire: failed to set attributes");  
 
99
        close(fd[portnum]);
 
100
        return FALSE;
 
101
     }
 
102
      
 
103
   /* Flush the input and output buffers */
 
104
   tcflush(fd[portnum], TCIOFLUSH);
 
105
   
 
106
   return TRUE;
 
107
}
 
108
 
 
109
/* Release port 'portnum' */
 
110
void owRelease(int portnum)
 
111
{
 
112
   /* Restore original settings */
 
113
   if(tcsetattr(fd[portnum], TCSANOW, &term_orig[portnum]) < 0 )
 
114
     {
 
115
        /* We failed doing that */
 
116
        OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
 
117
        perror("owAcquire: failed to set attributes");
 
118
        close(fd[portnum]);
 
119
     }
 
120
   
 
121
   /* Close the port */
 
122
   if (close(fd[portnum]) < 0)
 
123
     {
 
124
        /* We failed closing the port */
 
125
        OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
 
126
        perror("owAcquire: failed to close port");
 
127
     }
 
128
 
 
129
   /* we should return an error condition here but MAXIMS API is 
 
130
    * badly designed */
 
131
}
 
132
 
 
133