Hi everybody...
Here are a few more sample programs to play with. Ultimately,
Dag could ship them as part of the IrDa utils packages like the other
samples (hint, Dag...).
Jean
/*********************************************************************
*
* Filename: irprintf.c
* Version:
* Description:
* Status: Experimental.
* Authors: Dag Brattli <[EMAIL PROTECTED]>
* Jean Tourrilhes <[EMAIL PROTECTED]>
* Created at: 7/12/99
* Modified at:
* Modified by:
*
* Copyright (c) 1999 Dag Brattli, All Rights Reserved.
*
* This program 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 of
* the License, or (at your option) any later version.
*
* This program 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., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
********************************************************************/
/*
* Read an Ir socket and display it on stdout
* Use stream
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/irda.h>
#ifndef AF_IRDA
#define AF_IRDA 23
#endif /* AF_IRDA */
unsigned char buf[4098];
/*
* Function main (argc, )
*
* Implements IrDA Echo or Discard server
*
*/
int main(int argc, char *argv[])
{
struct sockaddr_irda peer, self;
int addrlen;
int fd, conn_fd;
FILE *stream;
printf("IrDA printf server starting ...\n");
/* Create socket */
fd = socket(AF_IRDA, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
exit(-1);
}
/* Init self */
self.sir_family = AF_IRDA;
strncpy(self.sir_name, "IrPrintf", 25);
self.sir_lsap_sel = LSAP_ANY;
if (bind(fd, (struct sockaddr*) &self, sizeof(struct sockaddr_irda))) {
perror("bind");
return -1;
}
if (listen(fd, 8)) {
perror("listen");
return -1;
}
for (;;) {
addrlen = sizeof(struct sockaddr_irda);
printf("Waiting for connection!\n");
conn_fd = accept(fd, (struct sockaddr *) &peer, &addrlen);
if (conn_fd < 0) {
perror("accept");
return -1;
}
stream = fdopen(conn_fd, "r");
if(stream == NULL) {
perror("fdopen");
return -1;
}
printf("Connected!\n");
do {
if((fgets(buf, sizeof(buf), stream) == NULL) ||
(buf[0] == 0x3))
buf[0] = '\0';
fwrite(buf, 1, strlen(buf), stdout);
} while (buf[0] != '\0');
fflush(stdout);
fclose(stream);
close(conn_fd);
printf("Disconnected!\n");
}
return 0;
}
/*********************************************************************
*
* Filename: irscanf.c
* Version:
* Description:
* Status: Experimental.
* Authors: Dag Brattli <[EMAIL PROTECTED]>
* Jean Tourrilhes <[EMAIL PROTECTED]>
* Created at: 7/12/99
* Modified at:
* Modified by:
*
* Copyright (c) 1999 Dag Brattli, All Rights Reserved.
*
* This program 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 of
* the License, or (at your option) any later version.
*
* This program 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., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
********************************************************************/
/*
* Read stdin and push it on an Ir socket
* Use stream
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/irda.h>
#ifndef AF_IRDA
#define AF_IRDA 23
#endif /* AF_IRDA */
#define MAX_DEVICES 10
unsigned char buf[4096];
/*
* Function echo_discover_devices (fd)
*
* Try to discover some remote device(s) that we can connect to
*
*/
int irscanf_discover_devices(int fd)
{
struct irda_device_list *list;
unsigned char *buf;
int len;
int i;
len = sizeof(struct irda_device_list) +
sizeof(struct irda_device_info) * MAX_DEVICES;
buf = malloc(len);
list = (struct irda_device_list *) buf;
if (getsockopt(fd, SOL_IRLMP, IRLMP_ENUMDEVICES, buf, &len)) {
perror("getsockopt");
exit(-1);
}
if (len > 0) {
printf("Discovered: (list len=%d)\n", list->len);
for (i=0;i<list->len;i++) {
printf(" name: %s\n", list->dev[i].info);
printf(" daddr: %08x\n", list->dev[i].daddr);
printf(" saddr: %08x\n", list->dev[i].saddr);
printf("\n");
if (list->dev[i].hints[0] & HINT_COMPUTER) {
printf("Lets try this one\n");
return list->dev[i].daddr;
}
}
}
printf("Didn't find any devices!\n");
return -1;
}
/*
* Function main (argc, )
*
*
*
*/
int main(int argc, char *argv[])
{
struct sockaddr_irda peer;
int daddr = 0;
int fd;
FILE *stream;
/* Create socket */
fd = socket(AF_IRDA, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
exit(-1);
}
#if 1
/* Find a peer */
daddr = irscanf_discover_devices(fd);
if (daddr == -1)
return -1;
#endif
peer.sir_family = AF_IRDA;
strncpy(peer.sir_name, "IrPrintf", 25);
peer.sir_addr = daddr;
if (connect(fd, (struct sockaddr*) &peer,
sizeof(struct sockaddr_irda))) {
perror("connect");
return -1;
}
stream = fdopen(fd, "w");
if(stream == NULL) {
perror("fdopen");
return -1;
}
printf("Connected!\n");
printf("Type your text with '.' on the last line...\n");
do {
if((fgets(buf, sizeof(buf), stdin) == NULL) ||
(buf[0] == '.')) {
buf[0] = 0x3;
buf[1] = '\0';
}
fwrite(buf, 1, strlen(buf), stream);
fflush(stream);
} while(buf[0] != 0x3);
printf("Closing connection...\n");
fclose(stream);
close (fd);
return 0;
}
/*********************************************************************
*
* Filename: irprintf.c
* Version:
* Description:
* Status: Experimental.
* Authors: Dag Brattli <[EMAIL PROTECTED]>
* Jean Tourrilhes <[EMAIL PROTECTED]>
* Created at: 7/12/99
* Modified at:
* Modified by:
*
* Copyright (c) 1999 Dag Brattli, All Rights Reserved.
*
* This program 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 of
* the License, or (at your option) any later version.
*
* This program 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., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
********************************************************************/
/*
* Read an Ir socket and display it on stdout
* Use file descriptor
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/irda.h>
#ifndef AF_IRDA
#define AF_IRDA 23
#endif /* AF_IRDA */
unsigned char buf[4098];
/*
* Function main (argc, )
*
* Implements IrDA Echo or Discard server
*
*/
int main(int argc, char *argv[])
{
struct sockaddr_irda peer, self;
int addrlen;
int fd, conn_fd;
int actual;
printf("IrDA printf server starting ...\n");
/* Create socket */
fd = socket(AF_IRDA, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
exit(-1);
}
/* Init self */
self.sir_family = AF_IRDA;
strncpy(self.sir_name, "IrPrintf", 25);
self.sir_lsap_sel = LSAP_ANY;
if (bind(fd, (struct sockaddr*) &self, sizeof(struct sockaddr_irda))) {
perror("bind");
return -1;
}
if (listen(fd, 8)) {
perror("listen");
return -1;
}
for (;;) {
addrlen = sizeof(struct sockaddr_irda);
printf("Waiting for connection!\n");
conn_fd = accept(fd, (struct sockaddr *) &peer, &addrlen);
if (conn_fd < 0) {
perror("accept");
return -1;
}
printf("Connected!\n");
do {
actual = read(conn_fd, buf, sizeof(buf));
if((actual <= 0) || (buf[0] == 0x3))
break;
fwrite(buf, 1, actual, stdout);
} while (buf[0] != '\0');
fflush(stdout);
close(conn_fd);
printf("Disconnected!\n");
}
return 0;
}
/*********************************************************************
*
* Filename: irscanfx.c
* Version:
* Description:
* Status: Experimental.
* Authors: Dag Brattli <[EMAIL PROTECTED]>
* Jean Tourrilhes <[EMAIL PROTECTED]>
* Created at: 7/12/99
* Modified at:
* Modified by:
*
* Copyright (c) 1999 Dag Brattli, All Rights Reserved.
*
* This program 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 of
* the License, or (at your option) any later version.
*
* This program 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., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
********************************************************************/
/*
* Read stdin and push it on an Ir socket
* Use file descriptor
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/irda.h>
#ifndef AF_IRDA
#define AF_IRDA 23
#endif /* AF_IRDA */
#define MAX_DEVICES 10
unsigned char buf[4096];
/*
* Function echo_discover_devices (fd)
*
* Try to discover some remote device(s) that we can connect to
*
*/
int irscanf_discover_devices(int fd)
{
struct irda_device_list *list;
unsigned char *buf;
int len;
int i;
len = sizeof(struct irda_device_list) +
sizeof(struct irda_device_info) * MAX_DEVICES;
buf = malloc(len);
list = (struct irda_device_list *) buf;
if (getsockopt(fd, SOL_IRLMP, IRLMP_ENUMDEVICES, buf, &len)) {
perror("getsockopt");
exit(-1);
}
if (len > 0) {
printf("Discovered: (list len=%d)\n", list->len);
for (i=0;i<list->len;i++) {
printf(" name: %s\n", list->dev[i].info);
printf(" daddr: %08x\n", list->dev[i].daddr);
printf(" saddr: %08x\n", list->dev[i].saddr);
printf("\n");
if (list->dev[i].hints[0] & HINT_COMPUTER) {
printf("Lets try this one\n");
return list->dev[i].daddr;
}
}
}
printf("Didn't find any devices!\n");
return -1;
}
/*
* Function main (argc, )
*
*
*
*/
int main(int argc, char *argv[])
{
struct sockaddr_irda peer;
int daddr = 0;
int fd;
/* Create socket */
fd = socket(AF_IRDA, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
exit(-1);
}
#if 1
/* Find a peer */
daddr = irscanf_discover_devices(fd);
if (daddr == -1)
return -1;
#endif
peer.sir_family = AF_IRDA;
strncpy(peer.sir_name, "IrPrintf", 25);
peer.sir_addr = daddr;
if (connect(fd, (struct sockaddr*) &peer,
sizeof(struct sockaddr_irda))) {
perror("connect");
return -1;
}
printf("Connected!\n");
printf("Type your text with '.' on the last line...\n");
do {
if((fgets(buf, sizeof(buf), stdin) == NULL) ||
(buf[0] == '.')) {
buf[0] = 0x3;
buf[1] = '\0';
}
write(fd, buf, strlen(buf));
} while(buf[0] != 0x3);
printf("Closing connection...\n");
close (fd);
return 0;
}