1
/* Copyright (c) 2003, Erik Rigtorp <erkki@linux.nu>
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
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.
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
31
* * Cleaned up everything.
33
* * Added printing of error messages.
35
* -- Erik Rigtorp <erkki@linux.nu> Thu, 05 Jun 2003 00:57:44 +0200
43
/* local function prototypes */
44
SMALLINT owAcquire(int,char *);
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 */
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)
57
/* Open the serial port */
58
if ((fd[portnum] = open(port_zstr, O_RDWR)) == -1)
60
OWERROR(OWERROR_GET_SYSTEM_RESOURCE_FAILED);
61
perror("owAcquire: failed to open device");
65
/* Get device settings */
66
if(tcgetattr(fd[portnum], &term[portnum] ) < 0 )
68
OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
69
perror("owAcquire: failed to set attributes");
75
term_orig[portnum] = term[portnum];
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;
83
/* 1 byte at a time, no timer */
84
term[portnum].c_cc[VMIN] = 1;
85
term[portnum].c_cc[VTIME] = 0;
87
/* 6 data bits, Receiver enabled, Hangup, Dont change "owner" */
88
term[portnum].c_cflag |= CS6 | CREAD | HUPCL | CLOCAL;
90
/* Set input and output speed to 115.2k */
91
cfsetispeed(&term[portnum], B115200);
92
cfsetospeed(&term[portnum], B115200);
94
/* set the attributes */
95
if(tcsetattr(fd[portnum], TCSANOW, &term[portnum]) < 0 )
97
OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
98
perror("owAcquire: failed to set attributes");
103
/* Flush the input and output buffers */
104
tcflush(fd[portnum], TCIOFLUSH);
109
/* Release port 'portnum' */
110
void owRelease(int portnum)
112
/* Restore original settings */
113
if(tcsetattr(fd[portnum], TCSANOW, &term_orig[portnum]) < 0 )
115
/* We failed doing that */
116
OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
117
perror("owAcquire: failed to set attributes");
122
if (close(fd[portnum]) < 0)
124
/* We failed closing the port */
125
OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
126
perror("owAcquire: failed to close port");
129
/* we should return an error condition here but MAXIMS API is