/* * * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2000-2001 Qualcomm Incorporated * Copyright (C) 2002-2003 Maxim Krasnyansky * Copyright (C) 2002-2005 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation; * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY * CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS * SOFTWARE IS DISCLAIMED. * * * $Id: l2test.c,v 1.16 2005/01/11 21:42:38 holtmann Exp $ */ /*Bluetooth Receive program*/ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned char *buf; /* Default mtu */ static int imtu = 672; static int omtu = 0; /* Default data size */ static long data_size = 672; /* Default addr and psm */ static bdaddr_t bdaddr; static unsigned short psm = 10; static int master = 0; static int auth = 0; static int encrypt = 0; static int secure = 0; static int socktype = SOCK_SEQPACKET; static int linger = 0; static int reliable = 0; unsigned char packet[256]; unsigned char sbuf[256]={'a','b','c','d','e','f',0x7c,'g',0x7c,'i'}; unsigned char formpacket(unsigned char device, unsigned char len ); void send_data(int); static void hexdump(unsigned char *s, unsigned long l) { unsigned long i; unsigned char j,k; unsigned char temp=0x00; /*for(i=0;i 0) { printf("\nRecevied %d bytes\n", len); static int omtu = 0; hexdump(buf,len); } } } static void do_listen(void) { struct sockaddr_l2 loc_addr, rem_addr; struct l2cap_options opts; int s, s1, opt; char ba[18]; if ((s = socket(PF_BLUETOOTH, socktype, BTPROTO_L2CAP)) < 0) { printf("\nCan't create socket: %s (%d)\n", strerror(errno), errno); exit(1); } loc_addr.l2_family = AF_BLUETOOTH; bacpy(&loc_addr.l2_bdaddr, &bdaddr); loc_addr.l2_psm = htobs(psm); if (bind(s, (struct sockaddr *) &loc_addr, sizeof(loc_addr)) < 0) { printf("\nCan't bind socket: %s (%d)\n", strerror(errno), errno); exit(1); } /* Set link mode */ opt = 0; if (reliable) opt |= L2CAP_LM_RELIABLE; if (master) opt |= L2CAP_LM_MASTER; if (auth) opt |= L2CAP_LM_AUTH; if (encrypt) opt |= L2CAP_LM_ENCRYPT; if (secure) opt |= L2CAP_LM_SECURE; if (setsockopt(s, SOL_L2CAP, L2CAP_LM, &opt, sizeof(opt)) < 0) { printf("\nCan't set L2CAP link mode: %s (%d)\n", strerror(errno), errno); exit(1); } /* Get default options */ opt = sizeof(opts); if (getsockopt(s, SOL_L2CAP, L2CAP_OPTIONS, &opts, &opt) < 0) { printf("\nCan't get default L2CAP options: %s (%d)\n", strerror(errno), errno); exit(1); } /* Set new options */ opts.imtu = imtu; if (setsockopt(s, SOL_L2CAP, L2CAP_OPTIONS, &opts, opt) < 0) { printf("\nCan't set L2CAP options: %s (%d)\n", strerror(errno), errno); exit(1); } /* if (socktype == SOCK_DGRAM) { handler(s); return; }*/ if (listen(s, 10)) { printf("\nCan not listen on the socket: %s (%d)\n", strerror(errno), errno); exit(1); } printf("\nWaiting for connection on psm %d ...\n", psm); while(1) { opt = sizeof(rem_addr); if ((s1 = accept(s, (struct sockaddr *) &rem_addr, &opt)) < 0) { printf("\nAccept failed: %s (%d)\n", strerror(errno), errno); exit(1); } opt = sizeof(opts); if (getsockopt(s1, SOL_L2CAP, L2CAP_OPTIONS, &opts, &opt) < 0) { printf("\nCan't get L2CAP options: %s (%d)\n", strerror(errno), errno); exit(1); } ba2str(&rem_addr.l2_bdaddr, ba); printf("\nConnect from %s [imtu %d, omtu %d, flush_to %d]\n",ba, opts.imtu, opts.omtu, opts.flush_to); /* Enable SO_LINGER */ if (linger) { struct linger l = { .l_onoff = 1, .l_linger = linger }; if (setsockopt(s, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0) { printf("\nCan't enable SO_LINGER: %s (%d)\n", strerror(errno), errno); exit(1); } } dump_mode(s1); close(s1); printf("\nDisconnect: %m\n"); } } // Sending part at the receving end unsigned char formpacket(unsigned char device, unsigned char len ) { unsigned char temp=0x00; unsigned char i,j,pdlen; packet[0]=0x7c; switch(device) { case 1: packet[1]=0x01; break; case 2: packet[1]=0x02; break; case 3: packet[1]=0x03; break; case 4: packet[1]=0x04; break; } pdlen=len; for(i=3,j=0;j