Download Small Form Factor Pcs

Transcript
Chapter 9, Bluetooth LED Sign
int
open_rfcomm(char *dev)
{
int fd;
for (;;) {
if ((fd = open(dev, O_RDWR)) >= 0)
break;
if (ENODEV != errno)
err(1, dev);
sleep(3);
}
syslog(LOG_DEBUG, "%s opened", dev);
return fd;
}
int
read_rfcomm(int fd, char **line)
{
static char inbuf[INPUT_BUF_LEN];
char c;
int l = 0;
while ((read(fd, &c, 1) > 0) && l < INPUT_BUF_LEN) {
if (c == 0xd || c == 0xa || c == 0x0) {
if (l)
break;
else
continue;
}
inbuf[l++] = c;
}
inbuf[l] = 0;
syslog(LOG_DEBUG, "read {%s}", inbuf);
*line = &inbuf[0];
return l;
}
int
write_sign(int fd, char *buf, int len)
{
int i;
char junk[10];
for (i = 0; i < len; i++) {
if (write(fd, buf+i, 1) < 0) {
syslog(LOG_ERR, "write_sign: %s", strerror(errno));
break;
}
read(fd, junk, 10);
usleep(5000);
}
return i;
}
int
write_sign_str(int fd, char *buf)
{
int len = 0;
syslog(LOG_NOTICE, "writing {%s} to FD %d", buf, fd);
Small Form Factor PCs
Buy the full book online: makezine.com/sff
20