summaryrefslogtreecommitdiff
path: root/comm/IrdaAddress.cc
blob: 98965642f9cb485ace1afa2b7c1ba4c84ac3eb07 (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
/*
 *	Ivy League
 *
 *	IRDA Addresses,
 *
 *	Copyright 2000
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	by Stephane Chatty
 *
 *	$Id$
 *
 */

#include "cplus_bugs.h"
#include "IrdaAddress.h"
#include "ivl/List.h"
#include <stdio.h>
#include <memory.h>
#include <linux/irda.h>
#include <sys/socket.h>

#ifndef AF_IRDA
#define AF_IRDA 23
#endif /* AF_IRDA */

IvlIrdaAddress :: IvlIrdaAddress (const char* service)
: IvlAddress ()
{
	Addr.sir_family = AF_IRDA;
	strncpy (Addr.sir_name, service ? service : "OBEX", 25);
	Addr.sir_lsap_sel = LSAP_ANY;
	Valid = true;
}


IvlIrdaAddress :: IvlIrdaAddress (lword addr, const char* service)
: IvlAddress ()
{
	Addr.sir_family = AF_IRDA;
	strncpy (Addr.sir_name, service ? service : "OBEX", 25);
	Addr.sir_lsap_sel = LSAP_ANY;
	Addr.sir_addr = addr;
	Valid = true;
}

/*?nodoc?*/
IvlIrdaAddress :: ~IvlIrdaAddress ()
{
}


int
IvlIrdaAddress :: Family ()
{
	return AF_IRDA;
}


int
IvlIrdaAddress :: Length ()
{
	return sizeof (Addr);
}


SockAddr*
IvlIrdaAddress :: GetSockAddr ()
{
	return (SockAddr*) &Addr;
}


char*
IvlIrdaAddress :: StrRepr (char* buf)
{
	if (! buf)
		buf = new char [ strlen (Addr.sir_name) + 1 + 5];
	sprintf (buf, "IRDA:%s:0x%08x", Addr.sir_name, Addr.sir_addr);
	return buf;
}

int
IvlIrdaAddress :: DiscoverPeers (int fd, IvlListOf <IvlIrdaAddress>& al)
{
	al.Clear ();

	socklen_t len = 1024;
	byte buf[1024];
	struct irda_device_list *l = (struct irda_device_list *) buf;
	getsockopt (fd, SOL_IRLMP, IRLMP_ENUMDEVICES, buf, &len);


	if (len <= 0) {
		fprintf (stderr, "no device found\n");
		return 0;
	}

	struct irda_device_info* d = l->dev;
	for (int i = 0; i < l->len ; ++i, ++d) {
		fprintf (stderr, "found %s at [0x%08x, 0x%08x]\n", d->info, d->saddr, d->daddr);
		al.Append (new IvlIrdaAddress (d->daddr));
	}
	return l->len;
}