aboutsummaryrefslogtreecommitdiffstats
path: root/drv_generic.c
blob: fbec1e9d37bf470c231a7b7bcf8eeb70564edbac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* $Id: drv_generic.c,v 1.2 2004/01/20 05:36:59 reinelt Exp $
 *
 * generic driver helper for text- and graphic-based displays
 *
 * Copyright 1999, 2000 Michael Reinelt <reinelt@eunet.at>
 * Copyright 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * LCD4Linux is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * $Log: drv_generic.c,v $
 * Revision 1.2  2004/01/20 05:36:59  reinelt
 * moved text-display-specific stuff to drv_generic_text
 * moved all the bar stuff from drv_generic_bar to generic_text
 *
 * Revision 1.1  2004/01/20 04:51:39  reinelt
 * moved generic stuff from drv_MatrixOrbital to drv_generic
 * implemented new-stylish bars which are nearly finished
 *
 */

/* 
 *
 * exported fuctions:
 *
 * Fixme: document me!
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>

#include "debug.h"
#include "cfg.h"
#include "plugin.h"
#include "lock.h"
#include "widget.h"
#include "widget_text.h"
#include "widget_bar.h"
#include "drv.h"
#include "drv_generic.h"


static char   *Driver;
static char   *Port;
static speed_t Speed;
static int     Device=-1;


// ****************************************
// *** generic serial/USB communication ***
// ****************************************

int drv_generic_serial_open (char *driver, char *port, speed_t speed)
{
  int fd;
  pid_t pid;
  struct termios portset;
  
  Driver = driver;
  Port   = port;
  Speed  = speed;
  
  if ((pid=lock_port(port))!=0) {
    if (pid==-1)
      error ("%s: port %s could not be locked", Driver, Port);
    else
      error ("%s: port %s is locked by process %d", Driver, Port, pid);
    return -1;
  }

  fd = open(Port, O_RDWR | O_NOCTTY | O_NDELAY); 
  if (fd==-1) {
    error ("%s: open(%s) failed: %s", Driver, Port, strerror(errno));
    unlock_port(Port);
    return -1;
  }

  if (tcgetattr(fd, &portset)==-1) {
    error ("%s: tcgetattr(%s) failed: %s", Driver, Port, strerror(errno));
    unlock_port(Port);
    return -1;
  }

  cfmakeraw(&portset);
  cfsetospeed(&portset, Speed);
  if (tcsetattr(fd, TCSANOW, &portset)==-1) {
    error ("%s: tcsetattr(%s) failed: %s", Driver, Port, strerror(errno));
    unlock_port(Port);
    return -1;
  }
  
  Device=fd;
  return Device;
}


int drv_generic_serial_read (char *string, int len)
{
  int run, ret;
  
  if (Device==-1) return -1;
  for (run=0; run<10; run++) {
    ret=read (Device, string, len);
    if (ret>=0 || errno!=EAGAIN) break;
    debug ("read(): EAGAIN");
    usleep(1000);
  }
  
  if (ret<0) {
    error("%s: read(%s, %d) failed: %s", Driver, Port, len, strerror(errno));
  } else if (ret!=len) {
    error ("%s: partial read: len=%d ret=%d", Driver, len, ret);
  }
  
  return ret;
}


void drv_generic_serial_write (char *string, int len)
{
  int run, ret;
  
  if (Device==-1) return;
  for (run=0; run<10; run++) {
    ret=write (Device, string, len);
    if (ret>=0 || errno!=EAGAIN) break;
    debug ("write(): EAGAIN");
    usleep(1000);
  }
  
  if (ret<0) {
    error ("MatrixOrbital: write(%s) failed: %s", Port, strerror(errno));
  } else if (ret!=len) {
    error ("MatrixOrbital: partial write: len=%d ret=%d", len, ret);
  }
  
  return;
}


int drv_generic_serial_close (void)
{
  debug ("%s: closing port %s", Driver, Port);
  close (Device);
  unlock_port(Port);
  return 0;
}