www.pudn.com > IrDA.rar > actisys.c
/*
Copyright (C) 2002-2003 Gerd Rausch, BlauLogic (http://blaulogic.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Except as contained in this notice, neither the name of BlauLogic
nor the name(s) of the author(s) may be used to endorse or promote
products derived from this software without specific prior written
permission.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHOR(S) OR BLAULOGIC BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "irphy.h"
static int rs232_fd=-1;
static void set_dtr_rts(int dtr, int rts)
{
unsigned flags;
if(!dtr || !rts) {
flags=0;
if(!dtr)
flags|=TIOCM_DTR;
if(!rts)
flags|=TIOCM_RTS;
ioctl(rs232_fd, TIOCMBIC, &flags);
}
if(dtr || rts) {
flags=0;
if(dtr)
flags|=TIOCM_DTR;
if(rts)
flags|=TIOCM_RTS;
ioctl(rs232_fd, TIOCMBIS, &flags);
}
}
void irphy_reset(void)
{
char *port;
struct termios ios;
if(rs232_fd>=0)
close(rs232_fd);
if(!(port=getenv("IRDA_PORT")))
port="/dev/ttyS1";
rs232_fd=open(port, O_RDWR);
memset(&ios, 0, sizeof(ios));
ios.c_cflag=B9600|CLOCAL|CREAD|CS8;
ios.c_cc[VMIN]=1;
tcsetattr(rs232_fd, TCSANOW, &ios);
set_dtr_rts(0, 1);
usleep(100);
set_dtr_rts(1, 1);
}
void irphy_send(uint8_t v)
{
int n;
if((n=write(rs232_fd, &v, 1))<0) {
fprintf(stderr, "irphy_write: write error: %s\n", strerror(errno));
exit(1);
}
if(n==0) {
fprintf(stderr, "irphy_write: EOF while writing\n");
exit(1);
}
}
uint8_t irphy_wait(int16_t timeout)
{
fd_set rset;
struct timeval tv;
if(timeout>=0) {
tv.tv_sec=timeout/1000;
tv.tv_usec=(timeout%1000)*1000;
}
FD_ZERO(&rset);
FD_SET(rs232_fd, &rset);
if(select(rs232_fd+1, &rset, NULL, NULL, timeout>=0 ? &tv : NULL)<0) {
fprintf(stderr, "irphy_wait: select error: %s\n", strerror(errno));
exit(1);
}
return FD_ISSET(rs232_fd, &rset);
}
uint8_t irphy_receive(void)
{
uint8_t v;
int n;
if((n=read(rs232_fd, &v, 1))<0) {
fprintf(stderr, "irphy_receive: read error: %s\n", strerror(errno));
exit(1);
}
if(n==0) {
fprintf(stderr, "irphy_receive: EOF while receiving\n");
exit(1);
}
return v;
}