irobex.c

来自「sparc硬件平台上的红外协议」· C语言 代码 · 共 213 行

C
213
字号
/*************************************<*********************************                * Filename:      irobex.c* Version:       0.6* Description:   IrOBEX, IrDA transport for OBEX* Status:        Experimental.* Author:        Dag Brattli <dagb@cs.uit.no>* Created at:    Fri Apr 23 14:28:13 1999* CVS ID:        $Id: irobex.c,v 1.18 2006/01/03 18:36:15 holtmann Exp $* *     Copyright (c) 1999 Dag Brattli, All Rights Reserved.*     Copyright (c) 2000 Pontus Fuchs, All Rights Reserved.**     This library is free software; you can redistribute it and/or*     modify it under the terms of the GNU Lesser General Public*     License as published by the Free Software Foundation; either*     version 2 of the License, or (at your option) any later version.**     This library 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*     Lesser General Public License for more details.**     You should have received a copy of the GNU Lesser General Public*     License along with this library; if not, write to the Free Software*     Foundation, Inc., 59 Temple Place, Suite 330, Boston, *     MA  02111-1307  USA*     ********************************************************************//* Linux case */#include <string.h>#include <unistd.h>#include <stdio.h>		/* perror */#include "../af_irda.h"#ifdef EMMI_SPARC#include <fsu_pthread.h>#else#include <pthread.h>#endif#ifndef AF_IRDA#define AF_IRDA 23#endif /* AF_IRDA */#include "obex_main.h"#include "irobex.h"/** Function irobex_no_addr (addr)**    Check if the address is not valid for connection**/int irobex_no_addr(struct addr_irda *addr){		return ((addr->sir_addr == 0x0) || (addr->sir_addr == 0xFFFFFFFF));	}/** Function irobex_prepare_connect (self, service)**    Prepare for IR-connect**/void irobex_prepare_connect(obex_t *self, const char *service){	self->trans.peer.irda.sir_family = AF_IRDA;		if (service)		strncpy(self->trans.peer.irda.sir_name, service, 25);	else		strcpy(self->trans.peer.irda.sir_name, "OBEX");}/** Function irobex_prepare_listen (self, service)**    Prepare for IR-listen**/void irobex_prepare_listen(obex_t *self, const char *service){	/* Bind local service */	self->trans.self.irda.sir_family = AF_IRDA;		if (service == NULL)		strncpy(self->trans.self.irda.sir_name, "OBEX", 25);	else		strncpy(self->trans.self.irda.sir_name, service, 25);	self->trans.self.irda.sir_lsap_sel = LSAP_ANY;}/** Function irobex_listen (self)**    Listen for incoming connections.**/int irobex_listen(obex_t *self){	DEBUG(5, "\n");		irda_create();	if (irda_bind(&self->trans.self.irda))	{		DEBUG(0, "Error doing bind\n");		return -1;			}			/* Ask the IrDA stack to advertise the Obex hint bit - Jean II */	/* Under Linux, it's a regular socket option */		unsigned char	hints[4];	/* Hint be we advertise */		/* We want to advertise the OBEX hint bit */	hints[0] = HINT_EXTENSION;	hints[1] = HINT_OBEX;	/* Tell the stack about it */	if (irda_setopt(SOL_IRLMP, IRLMP_HINTS_SET,		hints, sizeof(hints))) {		/* This command is not supported by older kernels,		so ignore any errors! */			}		DEBUG(4, "We are now listening for connections\n");		return 1;	}/* Memory allocation for discovery */#define DISC_BUF_LEN	sizeof(struct irda_device_list) + \sizeof(struct irda_device_info) * (MAX_DEVICES)/** Function irobex_discover_devices (self)**    Try to discover some remote device(s) that we can connect to** Note : we optionally can do a first filtering on the Obex hint bit,* and then we can verify that the device does have the requested service...* Note : in this function, the memory allocation for the discovery log* is done "the right way", so that it's safe and we don't leak memory...* Jean II*/int irobex_discover_devices(obex_t *self){	struct irda_device_list *	list;	unsigned char		buf[DISC_BUF_LEN];	int ret = -1;	DEBUG(1, "\n");		int i;		list = (struct irda_device_list *) buf;	int len = DISC_BUF_LEN;	    		if (irda_getopt( IRLMP_ENUMDEVICES, buf, &len)) {		DEBUG(5, "Didn't find any devices!\n");		return(-1);	}		/* Set the list to point to the correct place */	list = (struct irda_device_list *) buf;	len = DISC_BUF_LEN;			/* Did we get any ? (in some rare cases, this test is true) */	if (list->len <= 0) {		DEBUG(1, "Didn't find any devices!\n");		return(-1);	}		/* List all Obex devices : Linux case */	DEBUG(1, "Discovered %d devices :\n", list->len);	for(i = 0; i < list->len; i++) {		DEBUG(1, "  [%d] name:  %s, daddr: 0x%08x",			i + 1, list->dev[i].info, list->dev[i].daddr);				/* Do we want to filter devices based on IAS ? */				DEBUG(1, "\n");		/* Pick this device */		self->trans.peer.irda.sir_addr = list->dev[i].daddr;		self->trans.self.irda.sir_addr = list->dev[i].saddr;		ret = 0;	}		if(ret <  0)		DEBUG(1, "didn't find any OBEX devices!\n");	return(ret);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?