From 331571f69c3d94d69a6d20d68991ee827fe7361a Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Tue, 1 Sep 2015 18:21:48 +0200 Subject: [PATCH] add getchan routine support beacon scan on single channel --- ieee802154/common/Makefile | 3 +- ieee802154/common/ieee802154_getcca.c | 2 +- ieee802154/common/ieee802154_getchan.c | 57 ++++++++++++++++ ieee802154/i8sak/Makefile | 2 +- ieee802154/i8sak/i8sak_main.c | 92 ++++++++++++++++---------- include/ieee802154/ieee802154.h | 2 + 6 files changed, 121 insertions(+), 37 deletions(-) create mode 100644 ieee802154/common/ieee802154_getchan.c diff --git a/ieee802154/common/Makefile b/ieee802154/common/Makefile index 52079408f..e2f441c2c 100644 --- a/ieee802154/common/Makefile +++ b/ieee802154/common/Makefile @@ -40,7 +40,8 @@ include $(APPDIR)/Make.defs # NSH Library ASRCS = -CSRCS = ieee802154_setchan.c ieee802154_setpanid.c +CSRCS = ieee802154_setchan.c ieee802154_getchan.c +CSRCS += ieee802154_setpanid.c CSRCS += ieee802154_seteaddr.c ieee802154_setsaddr.c CSRCS += ieee802154_setpromisc.c ieee802154_setdevmode.c CSRCS += ieee802154_setcca.c ieee802154_getcca.c diff --git a/ieee802154/common/ieee802154_getcca.c b/ieee802154/common/ieee802154_getcca.c index 5cdcb6ba4..5df88e617 100644 --- a/ieee802154/common/ieee802154_getcca.c +++ b/ieee802154/common/ieee802154_getcca.c @@ -1,5 +1,5 @@ /**************************************************************************** - * ieee802154/common/ieee802154_setchan.c + * ieee802154/common/ieee802154_getcca.c * * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. * Author: Sebastien Lorquet diff --git a/ieee802154/common/ieee802154_getchan.c b/ieee802154/common/ieee802154_getchan.c new file mode 100644 index 000000000..93bede8a2 --- /dev/null +++ b/ieee802154/common/ieee802154_getchan.c @@ -0,0 +1,57 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_getchan.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +int ieee802154_getchan(int fd, FAR uint8_t *chan) +{ + int ret = ioctl(fd, MAC854IOCGCHAN, (unsigned long)chan ); + if (ret<0) + { + printf("MAC854IOCGCHAN failed\n"); + } + return ret; +} + diff --git a/ieee802154/i8sak/Makefile b/ieee802154/i8sak/Makefile index 7dc7d266d..37fcd8dd0 100644 --- a/ieee802154/i8sak/Makefile +++ b/ieee802154/i8sak/Makefile @@ -42,7 +42,7 @@ include $(APPDIR)/Make.defs APPNAME = i8 PRIORITY = SCHED_PRIORITY_DEFAULT -STACKSIZE = 2048 +STACKSIZE = 4096 # IEEE 802.15.4 SAK (Swiss Army Knife) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index 387d43916..e56103bb0 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -73,12 +73,11 @@ static struct ieee802154_packet_s gRxPacket; static struct ieee802154_packet_s gTxPacket; -static uint8_t gChan; uint8_t levels[16]; uint8_t disp[16]; -int scan(int fd) +int energy_scan(int fd) { int ret = OK; uint8_t chan, energy; @@ -160,10 +159,10 @@ static int status(int fd) printf("MAC854IOCGPANID failed\n"); return ret; } - ret = ioctl(fd, MAC854IOCGCHAN, (unsigned long)&chan); + + ret = ieee802154_getchan(fd, &chan); if (ret) { - printf("MAC854IOCGCHAN failed\n"); return ret; } @@ -201,7 +200,7 @@ static int status(int fd) /* Display */ printf("PANID %02X%02X CHAN %2d (%d MHz)\nSADDR %02X%02X EADDR ", - panid>>8, panid&0xFF, chan, 2350+(5*chan), saddr>>8, saddr&0xFF); + panid>>8, panid&0xFF, (int)chan, 2350+(5*chan), saddr>>8, saddr&0xFF); for (i=0; i<8; i++) { printf("%02X", eaddr[i]); @@ -226,7 +225,7 @@ static int status(int fd) * Display a single packet ****************************************************************************/ -static int display(FAR struct ieee802154_packet_s *pack, bool verbose) +static int display(uint8_t chan, FAR struct ieee802154_packet_s *pack, bool verbose) { int i; int hlen=0, dhlen=0; @@ -238,7 +237,7 @@ static int display(FAR struct ieee802154_packet_s *pack, bool verbose) dhlen = hlen; if(hlen>pack->len) dhlen = 0; - printf("chan=%2d rssi=%3u lqi=%3u len=%3u ", gChan, pack->rssi, pack->lqi, pack->len - dhlen); + printf("chan=%2d rssi=%3u lqi=%3u len=%3u ", chan, pack->rssi, pack->lqi, pack->len - dhlen); if(hlen<0) { @@ -278,6 +277,7 @@ static void* sniff(void *arg) int ret; struct sniffargs *sa = (struct sniffargs*)arg; int fd = sa->fd; + uint8_t chan; printf("Listening...\n"); while (1) @@ -287,12 +287,12 @@ static void* sniff(void *arg) { if (errno == EAGAIN) { - continue; + continue; } if (errno == EINTR) { - printf("read: interrupted\n"); - break; + printf("read: interrupted\n"); + break; } else { @@ -302,7 +302,9 @@ static void* sniff(void *arg) } /* Display packet */ - display(&gRxPacket, sa->verbose); + chan=0; + ieee802154_getchan(fd, &chan); + display(chan, &gRxPacket, sa->verbose); } @@ -350,16 +352,17 @@ static int tx(int fd, FAR struct ieee802154_packet_s *pack, int verbose) int usage(void) { - printf("i8 \n" - " scan\n" - " dump\n" - " chan \n" - " snif\n" - " stat\n" - " pa \n" - " edth \n" - " csth \n" - " tx \n" + printf("i8 []\n" + " scan Energy scan\n" + " beacons [single] Aactive scan [on cur chan]\n" + " dump Show device registers\n" + " chan Select current channel\n" + " snif Listen for packets\n" + " stat Device status\n" + " pa Enable/Disable external RF amplifiers\n" + " edth Select Energy detection rx threshold\n" + " csth Select preamble correlation rx threshold\n" + " tx Transmit a raw packet\n" ); return ERROR; } @@ -402,7 +405,7 @@ int i8_main(int argc, char *argv[]) /* get mode */ if (!strcmp(argv[2], "scan")) { - ret = scan(fd); + ret = energy_scan(fd); } else if (!strcmp(argv[2], "dump")) { @@ -438,7 +441,6 @@ int i8_main(int argc, char *argv[]) goto usage; } ret = ieee802154_setchan(fd, arg); - gChan = arg; } else if (!strcmp(argv[2], "edth")) { @@ -528,8 +530,14 @@ data_error: struct sniffargs args; struct ieee802154_addr_s dest; pthread_t pth; - int i; - + int i,single = FALSE; + uint8_t ch; + + if(argc==4 && !strcmp(argv[3], "single") ) + { + single = TRUE; + } + args.fd = fd; args.verbose = FALSE; @@ -543,26 +551,42 @@ data_error: dest.ia_len = 2; dest.ia_panid = 0xFFFF; dest.ia_saddr = 0xFFFF; + + gTxPacket.len = ieee802154_addrstore(&gTxPacket, &dest, NULL); + ieee802154_addrstore(&gTxPacket, &dest, NULL); gTxPacket.data[gTxPacket.len++] = 0xFF; //panid gTxPacket.data[gTxPacket.len++] = 0xFF; gTxPacket.data[gTxPacket.len++] = 0xFF; //saddr gTxPacket.data[gTxPacket.len++] = 0xFF; + gTxPacket.data[gTxPacket.len++] = IEEE802154_CMD_BEACON_REQ; -// for(i=1;i<3;i++) + if(!single) + { + ch = 11; + } + i=0; while(1) { - int ch; - for(ch=11; ch<27; ch++) + if(!single) { - printf("chan=%2d...\r", ch); fflush(stdout); ieee802154_setchan(fd, ch); - gChan = ch; - gTxPacket.data[2] = i; //seq - tx(fd, &gTxPacket, FALSE); - sleep(1); } - i++; if(i==256) i=0; + ieee802154_getchan(fd, &ch); + printf("chan=%2d seq=%2X ...\r", (int)ch, i); fflush(stdout); + gTxPacket.data[2] = i; //seq + tx(fd, &gTxPacket, FALSE); + sleep(1); + i++; + if(i==256) + { + i=0; + } + if(!single) + { + ch++; + if(ch==27) ch=11; + } } pthread_kill(pth, SIGUSR1); } diff --git a/include/ieee802154/ieee802154.h b/include/ieee802154/ieee802154.h index c7b41db57..d506ded7a 100644 --- a/include/ieee802154/ieee802154.h +++ b/include/ieee802154/ieee802154.h @@ -53,6 +53,8 @@ struct ieee802154_addr_s #define IEEE802154_ADDRSTRLEN 22 /* (4+1+8*2) */ int ieee802154_setchan (int fd, uint8_t chan); +int ieee802154_getchan (int fd, FAR uint8_t *chan); + int ieee802154_setpanid (int fd, uint16_t panid); int ieee802154_setsaddr (int fd, uint16_t saddr); int ieee802154_seteaddr (int fd, FAR uint8_t *chan);