# sniffer_scan.c

Go to the documentation of this file.

    00001 /* Copyright (c) 2007 Axel Wachtler
    00002    All rights reserved.
    00003
    00004    Redistribution and use in source and binary forms, with or without
    00005    modification, are permitted provided that the following conditions
    00006    are met:
    00007
    00008    * Redistributions of source code must retain the above copyright
    00009      notice, this list of conditions and the following disclaimer.
    00010    * Redistributions in binary form must reproduce the above copyright
    00011      notice, this list of conditions and the following disclaimer in the
    00012      documentation and/or other materials provided with the distribution.
    00013    * Neither the name of the authors nor the names of its contributors
    00014      may be used to endorse or promote products derived from this software
    00015      without specific prior written permission.
    00016
    00017    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    00018    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00019    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00020    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    00021    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    00022    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    00023    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    00024    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    00025    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    00026    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    00027    POSSIBILITY OF SUCH DAMAGE. */
    00028
    00029 /* $Id: sniffer_scan.c,v 1.4 2010/02/22 20:47:43 awachtler Exp $ */
    00037 /* === includes ============================================================ */
    00038 #include "sniffer.h"
    00039
    00040 /* === macros ============================================================== */
    00041
    00042 /* === types =============================================================== */
    00043
    00044 /* === globals ============================================================= */
    00045
    00046 /* === prototypes ========================================================== */
    00047 static void show_status(channel_t choffs);
    00048 time_t timer_scan(timer_arg_t t);
    00049
    00050 /* === functions =========================================================== */
    00051
    00055 void scan_init(void)
    00056 {
    00057
    00058     PRINTF("Scanning channels, scan period %ums"NL,
    00059            SCAN_PERIOD_MS);
    00060     PRINT("             bad"
    00061           "    Avg. "
    00062           "        802.15.4 frames   "
    00063           "    "NL);
    00064     PRINT("chan   frm   crc "
    00065           "  ed lqi "
    00066           "     B     D     A     C "
    00067           " PER"NL);
    00068
    00069     ctx.cchan = TRX_MIN_CHANNEL;
    00070     trx_bit_write(SR_CHANNEL, ctx.cchan);
    00071     trx_reg_write(RG_TRX_STATE, CMD_RX_ON);
    00072
    00073     ctx.thdl = timer_start(timer_scan,MSEC(SCAN_PERIOD_MS),0);
    00074 }
    00075
    00079 void scan_update_status(void)
    00080 {
    00081 scan_result_t *scres;
    00082 uint32_t cmask;
    00083
    00084     cmask = TRX_SUPPORTED_CHANNELS;
    00085     do
    00086     {
    00087         show_status(ctx.cchan);
    00088         ctx.cchan += 1;
    00089         if (ctx.cchan > TRX_MAX_CHANNEL)
    00090         {
    00091             ctx.cchan = TRX_MIN_CHANNEL;
    00092         }
    00093         cmask &= ~(1UL<<ctx.cchan);
    00094         if(((ctx.cmask & (1UL<<ctx.cchan)) != 0) || (cmask == 0))
    00095         {
    00096             /* exit loop if we find a channel that is to be
    00097                scanned or there are no more (supported) channels to scan */
    00098             break;
    00099         }
    00100     }
    00101     while(1);
    00102     if (ctx.scanres_reset)
    00103     {
    00104         scres = &ctx.scanres[CHANNEL_OFFSET(ctx.cchan)];
    00105         memset(scres,0,sizeof(scan_result_t));
    00106         if (ctx.scanres_reset < 32)
    00107         {
    00108             ctx.scanres_reset --;
    00109         }
    00110     }
    00111
    00112     /* set channel next channel and continue scanning */
    00113     trx_bit_write(SR_CHANNEL, ctx.cchan);
    00114     cli();
    00115     ctx.state = SCAN;
    00116     sei();
    00117
    00118     ctx.thdl = timer_start(timer_scan,MSEC(SCAN_PERIOD_MS),0);
    00119
    00120 }
    00121
    00126 static void show_status(channel_t channel)
    00127 {
    00128 static uint16_t updates = 0;
    00129 scan_result_t *scres;
    00130 uint16_t per = 0, lqi, ed;
    00131 uint8_t choffs;
    00132
    00133     updates++;
    00134     choffs = CHANNEL_OFFSET(channel);
    00135     if((ctx.cmask & (1UL<<channel)) == 0)
    00136     {
    00137         /* this is an unscanned channel */
    00138         PRINTF(" %2d  n/a"NL, channel);
    00139     }
    00140     else
    00141     {
    00142         /* this is an scanned channel */
    00143         scres = &ctx.scanres[choffs];
    00144         if (scres->framecnt > 0)
    00145         {
    00146             per = (scres->framecnt - scres->crc_ok) * 100 / scres->framecnt;
    00147         }
    00148
    00149         lqi = (scres->framecnt > 0) ? scres->lqisum / scres->framecnt : 0;
    00150         ed  = (scres->framecnt > 0) ? scres->edsum / scres->framecnt : 0;
    00151
    00152         PRINTF(" %2d  % 5u % 5u "
    00153                " % 3u %3u ",
    00154                 channel, scres->framecnt, scres->framecnt - scres->crc_ok,
    00155                 ed, lqi);
    00156
    00157         PRINTF(" % 5u % 5u % 5u % 5u"
    00158                "  % 3u"NL,
    00159                 scres->ftypes[0], scres->ftypes[1], scres->ftypes[2], scres->ftypes[3],
    00160                 per
    00161                 );
    00162     }
    00163     if (choffs == CHANNEL_MAX_OFFSET)
    00164     {
    00165         PRINTF("=== ur %d frames: %d ==="NL, ctx.irq_ur, ctx.frames);
    00166         /*         0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16 */
    00167         hif_puts("\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\n");
    00168     }
    00169     hif_puts("\r*\r\b");
    00170
    00171
    00172     if (ctx.scanres_reset)
    00173     {
    00174         if (ctx.scanres_reset <= TRX_NB_CHANNELS)
    00175         {
    00176             hif_puts("\rr");
    00177         }
    00178         else
    00179         {
    00180             hif_puts("\rR");
    00181         }
    00182         hif_puts("                                                      \r\b");
    00183     }
    00184     else
    00185     {
    00186         hif_puts("\r*\r\b");
    00187     }
    00188 }
    00189
    00190
    00194 time_t timer_scan(timer_arg_t t)
    00195 {
    00196     ctx.state = SCAN_DONE;
    00197     return 0;
    00198 }
    00199 /* EOF */