/******************************************************/
/* Copyright (C) 2008 Arch Rock Corporation           */
/* License Agreement: refer to LICENSE.txt            */
/*                                                    */
/******************************************************/
  
#ifndef _IWCONFIG_H
#define _IWCONFIG_H   1

#include <event.h>
#include <netinet/in.h>
#include <lowpan/lpstate.h>

/*!
 * Common configuration options for IEEE 15.4 radio.
 * Multiple options can coexist as flags.
 */
enum iwconfig_options{
  /*! Enable authentication mode if supported */
  O_AUTHEN = 0x1,   
  /*! Enable encryption mode if supported */
  O_ENCRYPT = 0x2,  
  /*! Set 64 bit MAC 15.4 address  */
  O_SET_MAC_ADDRESS = 0x4, 
  /*! Set security key if supported */
  O_SET_KEY = 0x8, 
  /*! Set the radio's transmission power */
  O_SET_TX_POWER = 0x12, 
  /*! Use non-default low power settings. */
  O_NOT_DEFAULT_LP = 0x14,
  /*! Set the radio to not use low power protocol. */
  O_NO_LP = 0x16,
};

/*!
 * Flags reflecting the current status of the 15.4 radio.
 */
enum net_device_status_flags{
  /*! Link is down. */
  IFF_DOWN = 0x1,
  /*! Link is up and running. */
  IFF_UP = 0x2,
  /*! Link loopback is supported */
  IFF_LOOPBACK = 0x4,
  /*! Link local IPv6 multicast is supported */
  IFF_MULTICAST = 0x8,
};

/*!
 * Storage sizes for device data structure abstraction.
 */
enum{
  /*! 64 bit MAC Address */
  MAX_ADDR_LEN = 8,
  /*! 128 bit security key size */
  KEYSIZE = 16,      
};

/*! 
 *  Supported network interfaces.  
 */
enum InterfaceEnum{
  LPAN0=1,
};

/*! \brief
 * IEEE 802.15.4 network link device abstraction.
 */
typedef struct net_device
{
  /*! Name of the link interface.
   *  \note The default 15.4
   *        radio interface name is \em LPAN0.
   *        This is fixed by the kernel library and 
   *        cannot be changed by developers.

   */
  uint8_t name;
  /*! MTU size of link interface.
   *  This cannot be changed.
   */
  uint16_t mtu;
  /*! Link (MAC) address of the device. */
  uint8_t dev_addr[MAX_ADDR_LEN];
  /*! Low power configurations.*/
  lpstate_t lpstate;
  /*! 15.4 channel */
  uint8_t channel;
  /*! 15.4 PAN ID. */
  uint16_t panid;
  /*! Security key. */
  uint8_t key[KEYSIZE];
  /*! 15.4 radio transmission power
   *  in discrete units of dBm. (e.g. 0dBm)
   *  The actual values are provided through proper enumerations.
   *  \note The supported range is manufacturer dependent.
   */
  int16_t tx_pwr; // in dBm
}net_device_t;


/*! \brief
 * IEEE 802.15.4 network link device information.
 */
typedef struct net_device_info
{
  /*! Device data structure */
  net_device_t device;
  /*! Global IPv6 Address of the interface. */
  in6_addr_t v6_gaddr;
  /*! Link Local IPv6 Address of the interface. */
  in6_addr_t v6_laddr;
  /*! IPv6 Prefix of the interface. */
  in6_addr_t v6_prefix;
  /*! Number of 15.4 packets sent. */
  uint16_t packets_sent;
  /*! Number of 15.4 packets received. */
  uint16_t packets_recv;
  /*! Flags reflecting current status of the interface. */
  uint16_t flags;
}net_device_info_t;

/*!
 * iwconfig commands
*/
enum IwConfigCmdsEnum{
  UP=1,
  DOWN,
  CONFIG,
  STATUS,
};


/*! A system call to control and configure the 
 *  wireless link interface.  A continuation
 *  callback will be fired if \em iwconfig_handler
 *  is not NULL. 
 *
 *  @param[in] name  Interface name (e.g. The default 6LowPAN interface 
 *                   is "lpan0")
 *  @param[in] cmd Support [UP, DOWN, CONFIG, STATUS]. See commands #IwConfigCmdsEnum
 *             \li \em UP power up interface
 *             \li \em DOWN power down interface
 *             \li \em CONFIG configure the radio based on \em options
 *                            and \em device_settings
 *             \li \em STATUS fills \em dev_info with
 *                            current interface status
 *  @param[in] options See #iwconfig_options.
 *  @param[in] device_settings Application specifies radio settings.
 *                             Required for UP and CONFIG \em cmd.
 *  @param[in] iwconfig_handler A continuation callback
 *                              for the completion of the 
 *                              system call.  Passing NULL
 *                              here will fail the call.
 *  @param[out] dev_info the results of the calls for the
 *                       continatuion callback are stored here
 *                       NULL cannot be passed.
 *  @param[in] context application context
 *  \return 0 if succeed or -1 if fail and errno is set appropriately.
 *
 *  \par Errors:
 *    EBUSY if the underlying system is busy and cannot service the
 *            call at this time.
 *  \par 
 *     EINVAL if invalid parameters are passed.
 *
 *  \section iwconfig_config Callback Descriptions
 *  \subsection iwconfig_handler iwconfig_handler
 *     @param[in] event  Type is IWCONFIG in #EventsEnum
 *     @param[in] cbargs is \em dev_info in iwconfig()
 *     @param[in] context is \em context in iwconfig()
 *  \return None.
 */
int16_t iwconfig(uint8_t name,
		 uint16_t cmd,
		 uint16_t options, 
		 const net_device_t *device_settings,
		 void (*iwconfig_handler)(event_t event,
			 		  void * cbargs,
					  void * context),
		 net_device_info_t *dev_info,
		 void * context);

#endif // _IWCONFIG_H
