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

# sniffer.h

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.h,v 1.6 2009/02/23 22:07:14 awachtler Exp $ */
    00038 /* === includes ============================================================ */
    00039 #include <avr/pgmspace.h>
    00040 #include <string.h>
    00041 #include <stdint.h>
    00042 #include <stdlib.h>
    00043
    00044 #include <stdbool.h>
    00045 #include <util/crc16.h>
    00046 #include <avr/interrupt.h>
    00047 #include "transceiver.h"
    00048 #include "ioutil.h"
    00049 #include "timer.h"
    00050 #ifndef SNIFFER_H
    00051 #define SNIFFER_H
    00052
    00053 /* === macros ============================================================== */
    00054
    00055 #define SCAN_PERIOD_MS  (2000)
    00056 #define NL "\n\r"
    00057 #define CHANNEL_OFFSET(x)  (x > TRX_MAX_CHANNEL ? TRX_MIN_CHANNEL : (x - TRX_MIN_CHANNEL))
    00058 #define CHANNEL_MAX_OFFSET (TRX_NB_CHANNELS-1)
    00059 #define VERSION "0.1"
    00060
    00064 #define CHANNEL_NEXT_CIRCULAR(x) \
    00065     do \
    00066     { \
    00067         (x)++; \
    00068         if ((x) > TRX_MAX_CHANNEL) \
    00069         { \
    00070             (x) = TRX_MIN_CHANNEL; \
    00071         } \
    00072     } \
    00073     while(0)
    00074
    00075 /* === types =============================================================== */
    00079 typedef enum
    00080 {
    00082     IDLE,
    00084     EDSCAN,
    00086     SCAN,
    00088     SCAN_DONE,
    00090     SNIFF
    00091 } SHORTENUM sniffer_state_t;
    00092
    00096 typedef struct scan_result_tag
    00097 {
    00099     uint16_t framecnt;
    00101     uint16_t crc_ok;
    00102     uint16_t edsum;
    00103     uint16_t lqisum;
    00104     uint16_t ftypes[8];
    00105 } scan_result_t;
    00106
    00111 typedef struct sniffer_context_tag
    00112 {
    00114     sniffer_state_t state;
    00116     channel_t cchan;
    00118     uint8_t cpage;
    00120     uint32_t cmask;
    00121
    00123     bool chkcrc;
    00124
    00126     timer_hdl_t     thdl;
    00128     time_t          scanper;
    00130     scan_result_t   scanres[TRX_NB_CHANNELS];
    00131     uint8_t         scanres_reset;
    00132     uint16_t frames;
    00133     uint16_t irq_ur;
    00134     uint16_t missed_frames;
    00135 } sniffer_context_t;
    00136
    00137 typedef struct
    00138 {
    00139     uint8_t irql;
    00140     uint8_t tmrl;
    00141     uint8_t err;
    00142 } dbg_t;
    00143
    00144
    00145 typedef struct sniffer_packet_tag
    00146 {
    00147
    00148     time_stamp_t ts;
    00149     uint8_t flen;
    00150     uint8_t *rxdata;
    00151     uint8_t lqi;
    00152     uint8_t ed;
    00153     bool    crc;
    00154 } sniffer_packet_t;
    00155
    00156 /* === externals =========================================================== */
    00157 extern sniffer_context_t ctx;
    00158
    00159 /* === inline functions ==================================================== */
    00160
    00164 static inline void scan_update_frame(uint8_t flen, bool crc_ok, uint8_t lqi, uint8_t ed, uint8_t *rxbuf)
    00165 {
    00166 scan_result_t *scres;
    00167
    00168      scres = &ctx.scanres[(ctx.cchan - TRX_MIN_CHANNEL)];
    00169      scres->framecnt ++;
    00170      scres->edsum +=ed;
    00171      scres->lqisum += lqi;
    00172
    00173      if (flen < 0x80)
    00174      {
    00175          /* process valid frame length */
    00176          if (crc_ok == true)
    00177          {
    00178              scres->crc_ok++;
    00179              scres->ftypes[rxbuf[0]&7] ++;
    00180          }
    00181          /* parse beacon */
    00182      }
    00183 }
    00184
    00185
    00186
    00187 /* === prototypes ========================================================== */
    00188 #ifdef __cplusplus
    00189 extern "C" {
    00190 #endif
    00191
    00198 void sniffer_start(sniffer_state_t state);
    00199 void sniffer_stop(void);
    00200 void ctrl_process_input(void);
    00201
    00202 void scan_init(void);
    00203 void scan_continue(void);
    00204 #ifdef __cplusplus
    00205 } /* extern "C" */
    00206 #endif
    00207
    00208 #endif  /* #ifndef SNIFFER_H */