// (c) Ronald Schmidt  www.ronsc.de

#ifndef _RS485_H_
#define _RS485_H_

#define VERSION(v,p,s) (((v)<<16)+(p<<8)+s)

#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/if_arp.h>
#include <linux/if_slip.h>
#include <linux/init.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/netdevice.h>


#define OPTION_REVERSE_RTS			1
#define OPTION_PING_ALL				2
#define OPTION_WINBOND				4
#define OPTION_DEBUG					8
#define OPTION_COMPRESS				16
#define OPTION_RO                32


#define FRAME_SS        83
#define FRAME_ES        202

#define CHAR_ESC        32
#define CHAR_FSS        33
#define CHAR_FES        34

#define MAX_NODES       15


#define RS485OVERHEAD   3
#define MAX_MTU         1000+RS485OVERHEAD
#define MAX_BUFFER      2000+RS485OVERHEAD

#define DEFAULT_MTU     150


#define STATUS_IDLE     1
#define STATUS_SYN      2 
#define STATUS_READ     3 
#define STATUS_WAIT     99

#define TXSTATUS_IDLE  0   
#define TXSTATUS_SS    1   
#define TXSTATUS_ADDR  2   
#define TXSTATUS_PAY   3   
#define TXSTATUS_ES    4
#define TXSTATUS_END   5

#define FRAME_EMPTY     0
#define FRAME_INIT      1
#define FRAME_FULL      2


#define NODE_INAKTIVE   0
#define NODE_AKTIVE     1

#define WATCHER_INTERVALL  10  //jiffies
#define WATCHDOG_INTERVALL  3  //jiffies

#if (LINUX_VERSION_CODE < VERSION(2,4,0))
#define net_device device

#define create_proc_info_entry(n, m, b, g) \
        { \
                struct proc_dir_entry *r = create_proc_entry(n, m, b); \
                if (r) r->get_info = g; \
        }
#endif


typedef struct {
   int id;
   int status;
   
   unsigned long last_scan;
   unsigned long last;
   
   int rx_size;
   __u8 rx_buf[MAX_BUFFER];
   
   int tx_size;
   int tx_pos;
   __u8 tx_buf[MAX_BUFFER];
   
   // netdev
   char  if_name[8];
   struct net_device dev;
   struct net_device_stats stats;
   int mtu;
 
   int in_use;
   int rx_packetlen;
   u8 *rx_packetdata;
   int tx_packetlen;
   u8 *tx_packetdata;
   struct sk_buff *skb;
   spinlock_t lock;

   // stat

   int on;
   unsigned long int query,resp;

   
} node_t;

typedef struct {
   int rx;           // rx-bytes
   int tx;           // tx-bytes
   
   int prx;          // rx-bytes payload
   int ptx;          // tx-bytes payload

   int ss;           // read start-sentinel
   int es;           // read end-sentinel

   int tss;          // read start-sentinel
   int tes;          // read end-sentinel
   int wss;          // wring SS 

   int fme;          // frames for me

   int mto;          // master-imeout
   int ito;          // irq-timeout
   int rto;          // irq-timeout
   
   int ff;           // finish frame
   int wsend;        // wrong sender

   int br;           // break indikator

} rs485stat_t;

#endif
