- Main Page
- Related Pages
- Modules
- Data Structures
- Files

# 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.3 2008/10/05 16:58:57 awachtler Exp $ */
    00037 /* === includes ============================================================ */
    00038 #include "sniffer.h"
    00039
    00040 /* === macros ============================================================== */
    00041
    00042 /* === types =============================================================== */
    00043
    00044 /* === globals ============================================================= */
    00045 extern volatile dbg_t dbg;
    00046
    00047 /* === prototypes ========================================================== */
    00048 static void show_status(channel_t choffs);
    00049 time_t timer_scan(timer_arg_t t);
    00050
    00051 /* === functions =========================================================== */
    00052
    00056 void scan_init(void)
    00057 {
    00058
    00059     PRINTF("Scanning channels, scan period %ums"NL,
    00060            SCAN_PERIOD_MS);
    00061     PRINT("             bad"
    00062           "    Avg. "
    00063           "        802.15.4 frames   "
    00064           "    "NL);
    00065     PRINT("chan   frm   crc "
    00066           "  ed lqi "
    00067           "     B     D     A     C "
    00068           " PER"NL);
    00069
    00070     ctx.cchan = TRX_MIN_CHANNEL;
    00071     trx_bit_write(SR_CHANNEL, ctx.cchan);
    00072     trx_reg_write(RG_TRX_STATE, CMD_RX_ON);
    00073
    00074     ctx.thdl = timer_start(timer_scan,MSEC(SCAN_PERIOD_MS),0);
    00075 }
    00076
    00080 void scan_update_status(void)
    00081 {
    00082 scan_result_t *scres;
    00083 uint32_t cmask;
    00084
    00085     cmask = TRX_SUPPORTED_CHANNELS;
    00086     do
    00087     {
    00088         show_status(ctx.cchan);
    00089         ctx.cchan += 1;
    00090         if (ctx.cchan > TRX_MAX_CHANNEL)
    00091         {
    00092             ctx.cchan = TRX_MIN_CHANNEL;
    00093         }
    00094         cmask &= ~(1UL<<ctx.cchan);
    00095         if(((ctx.cmask & (1UL<<ctx.cchan)) != 0) || (cmask == 0))
    00096         {
    00097             /* exit loop if we find a channel that is to be
    00098                scanned or there are no more (supported) channels to scan */
    00099             break;
    00100         }
    00101     }
    00102     while(1);
    00103     if (ctx.scanres_reset)
    00104     {
    00105         scres = &ctx.scanres[CHANNEL_OFFSET(ctx.cchan)];
    00106         memset(scres,0,sizeof(scan_result_t));
    00107         if (ctx.scanres_reset < 32)
    00108         {
    00109             ctx.scanres_reset --;
    00110         }
    00111     }
    00112
    00113     /* set channel next channel and continue scanning */
    00114     trx_bit_write(SR_CHANNEL, ctx.cchan);
    00115     cli();
    00116     ctx.state = SCAN;
    00117     sei();
    00118
    00119     ctx.thdl = timer_start(timer_scan,MSEC(SCAN_PERIOD_MS),0);
    00120
    00121 }
    00122
    00127 static void show_status(channel_t channel)
    00128 {
    00129 static uint16_t updates = 0;
    00130 scan_result_t *scres;
    00131 uint16_t per = 0, lqi, ed;
    00132 uint8_t choffs;
    00133
    00134     updates++;
    00135     choffs = CHANNEL_OFFSET(channel);
    00136     if((ctx.cmask & (1UL<<channel)) == 0)
    00137     {
    00138         /* this is an unscanned channel */
    00139         PRINTF(" %2d  n/a"NL, channel);
    00140     }
    00141     else
    00142     {
    00143         /* this is an scanned channel */
    00144         scres = &ctx.scanres[choffs];
    00145         if (scres->framecnt > 0)
    00146         {
    00147             per = (scres->framecnt - scres->crc_ok) * 100 / scres->framecnt;
    00148         }
    00149
    00150         lqi = (scres->framecnt > 0) ? scres->lqisum / scres->framecnt : 0;
    00151         ed  = (scres->framecnt > 0) ? scres->edsum / scres->framecnt : 0;
    00152
    00153         PRINTF(" %2d  % 5u % 5u "
    00154                " % 3u %3u ",
    00155                 channel, scres->framecnt, scres->framecnt - scres->crc_ok,
    00156                 ed, lqi);
    00157
    00158         PRINTF(" % 5u % 5u % 5u % 5u"
    00159                "  % 3u"NL,
    00160                 scres->ftypes[0], scres->ftypes[1], scres->ftypes[2], scres->ftypes[3],
    00161                 per
    00162                 );
    00163     }
    00164     if (choffs == CHANNEL_MAX_OFFSET)
    00165     {
    00166         PRINTF("=== ur %d err %d frames: %d ==="NL, ctx.irq_ur, dbg.err, ctx.frames);
    00167         /*         0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16 */
    00168         hif_puts("\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\n");
    00169     }
    00170     hif_puts("\r*\r\b");
    00171
    00172
    00173     if (ctx.scanres_reset)
    00174     {
    00175         if (ctx.scanres_reset <= TRX_NB_CHANNELS)
    00176         {
    00177             hif_puts("\rr");
    00178         }
    00179         else
    00180         {
    00181             hif_puts("\rR");
    00182         }
    00183         hif_puts("                                                      \r\b");
    00184     }
    00185     else
    00186     {
    00187         hif_puts("\r*\r\b");
    00188     }
    00189 }
    00190
    00191
    00195 time_t timer_scan(timer_arg_t t)
    00196 {
    00197     if (dbg.tmrl) dbg.err = 1;
    00198     dbg.tmrl ++;
    00199     //LED_TOGGLE(1);
    00200     ctx.state = SCAN_DONE;
    00201     dbg.tmrl --;
    00202     return 0;
    00203 }
    00204 /* EOF */