]>
git.zerfleddert.de Git - hmcfgusb/blob - culfw.c
a4c949baff77fdc6e1325c9a236bec427836f7c8
3 * Copyright (c) 2014 Michael Gernoth <michael@gernoth.net>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 #include <sys/types.h>
39 struct culfw_dev
*culfw_init(char *device
, uint32_t speed
, culfw_cb_fn cb
, void *data
)
41 struct culfw_dev
*dev
= NULL
;
42 struct termios oldtio
, tio
;
62 fprintf(stderr
, "Unsupported baud-rate: %u\n", speed
);
67 dev
= malloc(sizeof(struct culfw_dev
));
69 perror("malloc(struct culfw_dev)");
73 memset(dev
, 0, sizeof(struct culfw_dev
));
75 dev
->fd
= open(device
, O_RDWR
| O_NOCTTY
);
77 perror("open(culfw)");
81 if (tcgetattr(dev
->fd
, &oldtio
) == -1) {
86 memset(&tio
, 0, sizeof(tio
));
88 tio
.c_cflag
= brate
| CS8
| CLOCAL
| CREAD
;
89 tio
.c_iflag
= IGNPAR
| ICRNL
;
95 tcflush(dev
->fd
, TCIFLUSH
);
96 if (tcsetattr(dev
->fd
, TCSANOW
, &tio
) == -1) {
113 int culfw_send(struct culfw_dev
*dev
, char *cmd
, int cmdlen
)
119 ret
= write(dev
->fd
, cmd
+ w
, cmdlen
- w
);
125 } while (w
< cmdlen
);
130 int culfw_poll(struct culfw_dev
*dev
, int timeout
)
132 struct pollfd pfds
[1];
139 memset(pfds
, 0, sizeof(struct pollfd
) * 1);
141 pfds
[0].fd
= dev
->fd
;
142 pfds
[0].events
= POLLIN
;
144 ret
= poll(pfds
, 1, timeout
* 1000);
153 if (!(pfds
[0].revents
& POLLIN
)) {
158 memset(buf
, 0, sizeof(buf
));
159 r
= read(dev
->fd
, buf
, sizeof(buf
));
168 dev
->cb(buf
, r
, dev
->cb_data
);
174 void culfw_close(struct culfw_dev
*dev
)
179 void culfw_flush(struct culfw_dev
*dev
)
181 struct pollfd pfds
[1];
186 tcflush(dev
->fd
, TCIOFLUSH
);
189 memset(pfds
, 0, sizeof(struct pollfd
) * 1);
191 pfds
[0].fd
= dev
->fd
;
192 pfds
[0].events
= POLLIN
;
194 ret
= poll(pfds
, 1, 100);
198 if (!(pfds
[0].revents
& POLLIN
))
201 memset(buf
, 0, sizeof(buf
));
202 r
= read(dev
->fd
, buf
, sizeof(buf
));