API
 
Loading...
Searching...
No Matches
linux.c
Go to the documentation of this file.
1#include <fcntl.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <sys/select.h>
6#include <sys/stat.h>
7#include <sys/types.h>
8#include <termios.h>
9#include <unistd.h>
10
11#include "usbtemp.h"
12
13#define TIMEOUT 1
14
16
17int owReset(int fd)
18{
19 int rv;
20 int wbytes;
21 unsigned char wbuff, rbuff;
22 fd_set readset;
23 struct timeval timeout_tv;
24 struct termios term;
25
26 tcflush(fd, TCIOFLUSH);
27
28 if (tcgetattr(fd, &term) < 0) {
29 ut_errno = 1;
30 return -1;
31 }
32 term.c_cflag &= ~CSIZE | CS8;
33 cfsetispeed(&term, B9600);
34 cfsetospeed(&term, B9600);
35 tcsetattr(fd, TCSANOW, &term);
36
37 /* Send the reset pulse. */
38 wbuff = 0xf0;
39 wbytes = write(fd, &wbuff, 1);
40 if (wbytes != 1) {
41 ut_errno = 9;
42 return -1;
43 }
44
45 timeout_tv.tv_usec = 0;
46 timeout_tv.tv_sec = TIMEOUT;
47
48 FD_ZERO(&readset);
49 FD_SET(fd, &readset);
50
51 if (select(fd + 1, &readset, NULL, NULL, &timeout_tv) > 0) {
52
53 if (FD_ISSET(fd, &readset)) {
54 int rbytes = read(fd, &rbuff, 1);
55 if (rbytes != 1) {
56 return -1;
57 }
58 switch (rbuff) {
59 case 0:
60 /* Ground. */
61 case 0xf0:
62 /* No response. */
63 rv = -1;
64 break;
65 default:
66 /* Got a response */
67 rv = 0;
68 }
69 }
70 else {
71 rv = -1;
72 }
73 }
74 else {
75 rv = -1; /* Timed out or interrupt. */
76 }
77
78 term.c_cflag &= ~CSIZE | CS6;
79 cfsetispeed(&term, B115200);
80 cfsetospeed(&term, B115200);
81
82 tcsetattr(fd, TCSANOW, &term);
83
84 return rv;
85}
86
87static unsigned char owWriteByte(int fd, unsigned char wbuff)
88{
89 char buf[8];
90 int wbytes;
91 unsigned char rbuff, i;
92 size_t remaining, rbytes;
93 fd_set readset;
94 struct timeval timeout_tv;
95
96 tcflush(fd, TCIOFLUSH);
97
98 for (i = 0; i < 8; i++) {
99 buf[i] = (wbuff & (1 << (i & 0x7))) ? 0xff : 0x00;
100 }
101 wbytes = write(fd, buf, 8);
102 if (wbytes != 8) {
103 ut_errno = 9;
104 return -1;
105 }
106
107 timeout_tv.tv_usec = 0;
108 timeout_tv.tv_sec = TIMEOUT;
109
110 FD_ZERO(&readset);
111 FD_SET(fd, &readset);
112
113 rbuff = 0;
114 remaining = 8;
115 while (remaining > 0) {
116
117 if (select(fd + 1, &readset, NULL, NULL, &timeout_tv) > 0) {
118
119 if (FD_ISSET(fd, &readset)) {
120 rbytes = read(fd, &buf, remaining);
121 for (i = 0; i < rbytes; i++) {
122 rbuff >>= 1;
123 rbuff |= (buf[i] & 0x01) ? 0x80 : 0x00;
124 remaining--;
125 }
126 }
127 else {
128 return 0xff;
129 }
130 }
131 else {
132 return 0xff;
133 }
134 }
135 return rbuff;
136}
137
138unsigned char owRead(int fd)
139{
140 return owWriteByte(fd, 0xff);
141}
142
143int owWrite(int fd, unsigned char wbuff)
144{
145 return (owWriteByte(fd, wbuff) == wbuff) ? 0 : -1;
146}
147
148static int file_exists(const char *filename)
149{
150 struct stat st;
151
152 return (stat(filename, &st) == 0);
153}
154
155int owOpen(const char *serial_port)
156{
157 int fd;
158 struct termios term;
159
160 if (!file_exists(serial_port)) {
161 ut_errno = 3;
162 return -1;
163 }
164
165 /*if (access(serial_port, R_OK|W_OK) < 0) {
166 ut_errno = 4;
167 return -1;
168 }*/
169
170 fd = open(serial_port, O_RDWR);
171 if (fd < 0) {
172 ut_errno = 5;
173 return -1;
174 }
175
176 memset(&term, 0, sizeof(term));
177
178 term.c_cc[VMIN] = 1;
179 term.c_cc[VTIME] = 0;
180 term.c_cflag |= CS6 | CREAD | HUPCL | CLOCAL;
181
182 cfsetispeed(&term, B115200);
183 cfsetospeed(&term, B115200);
184
185 if (tcsetattr(fd, TCSANOW, &term) < 0) {
186 close(fd);
187 ut_errno = 2;
188 return -1;
189 }
190
191 tcflush(fd, TCIOFLUSH);
192
193 return fd;
194}
195
196void owClose(int fd)
197{
198 close(fd);
199}
unsigned char owRead(int fd)
Definition linux.c:138
#define TIMEOUT
Definition linux.c:13
int owOpen(const char *serial_port)
Definition linux.c:155
void owClose(int fd)
Definition linux.c:196
int ut_errno
Definition linux.c:15
int owReset(int fd)
Definition linux.c:17
static int file_exists(const char *filename)
Definition linux.c:148
static unsigned char owWriteByte(int fd, unsigned char wbuff)
Definition linux.c:87
int owWrite(int fd, unsigned char wbuff)
Definition linux.c:143