# 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$ */
    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
    00076 #ifndef MAX_PACKET_BUFFERS
    00077 # define MAX_PACKET_BUFFERS (8)
    00078 #endif
    00079 /* === types =============================================================== */
    00083 typedef enum
    00084 {
    00086     IDLE,
    00088     EDSCAN,
    00090     SCAN,
    00092     SCAN_DONE,
    00094     SNIFF
    00095 } SHORTENUM sniffer_state_t;
    00096
    00100 typedef struct scan_result_tag
    00101 {
    00103     uint16_t framecnt;
    00105     uint16_t crc_ok;
    00106     uint16_t edsum;
    00107     uint16_t lqisum;
    00108     uint16_t ftypes[8];
    00109 } scan_result_t;
    00110
    00115 typedef struct sniffer_context_tag
    00116 {
    00118     volatile sniffer_state_t state;
    00120     channel_t cchan;
    00122     uint8_t cpage;
    00124     uint32_t cmask;
    00125
    00127     bool chkcrc;
    00128
    00130     timer_hdl_t     thdl;
    00132     time_t          scanper;
    00134     scan_result_t   scanres[TRX_NB_CHANNELS];
    00135     uint8_t         scanres_reset;
    00136     uint16_t frames;
    00137     uint16_t irq_ur;
    00138     uint16_t missed_frames;
    00139 } sniffer_context_t;
    00140
    00141 typedef struct pcap_packet_tag
    00142 {
    00144     uint8_t len;
    00146     time_stamp_t ts;
    00148     uint8_t frame[MAX_FRAME_SIZE];
    00149 } pcap_packet_t;
    00150
    00151
    00152 typedef struct pcap_pool_tag
    00153 {
    00154     volatile uint8_t ridx;
    00155     volatile uint8_t widx;
    00156     pcap_packet_t packet[MAX_PACKET_BUFFERS];
    00157 } pcap_pool_t;
    00158
    00159
    00160 /* === externals =========================================================== */
    00161 extern sniffer_context_t ctx;
    00162
    00163 /* === inline functions ==================================================== */
    00164
    00168 static inline void scan_update_frame(uint8_t flen, bool crc_ok, uint8_t lqi, uint8_t ed, uint8_t *rxbuf)
    00169 {
    00170 scan_result_t *scres;
    00171
    00172      scres = &ctx.scanres[(ctx.cchan - TRX_MIN_CHANNEL)];
    00173      scres->framecnt ++;
    00174      scres->edsum +=ed;
    00175      scres->lqisum += lqi;
    00176
    00177      if (flen < 0x80)
    00178      {
    00179          /* process valid frame length */
    00180          if (crc_ok == true)
    00181          {
    00182              scres->crc_ok++;
    00183              scres->ftypes[rxbuf[0]&7] ++;
    00184          }
    00185          /* parse beacon */
    00186      }
    00187 }
    00188
    00189
    00190
    00191 /* === prototypes ========================================================== */
    00192 #ifdef __cplusplus
    00193 extern "C" {
    00194 #endif
    00195
    00202 void sniffer_start(sniffer_state_t state);
    00203 void sniffer_stop(void);
    00204 void ctrl_process_input(void);
    00205 void scan_init(void);
    00206 void scan_continue(void);
    00207 #ifdef __cplusplus
    00208 } /* extern "C" */
    00209 #endif
    00210
    00211 #endif  /* #ifndef SNIFFER_H */