version_2.0

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

cc3000.h

Committer:
Kojto
Date:
2013-10-08
Revision:
33:9e23b24fb4f3
Parent:
31:7b6e85b68b01
Child:
34:1ad18123bf11

File content as of revision 33:9e23b24fb4f3:

/*****************************************************************************
*
*  C++ interface/implementation created by Martin Kojtal (0xc0170). Thanks to
*  Jim Carver and Frank Vannieuwkerke for their inital cc3000 mbed port and
*  provided help.
*
*  This version of "host driver" uses CC3000 Host Driver Implementation. Thus
*  read the following copyright:
*
*  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*    Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
*    Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the
*    distribution.
*
*    Neither the name of Texas Instruments Incorporated nor the names of
*    its contributors may be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
#ifndef CC3000_H
#define CC3000_H

#include "mbed.h"
#include "cc3000_common.h"
#include "cc3000_spi.h"
#include "cc3000_simplelink.h"
#include "cc3000_netapp.h"
#include "cc3000_nvmem.h"
#include "cc3000_socket.h"

#define MAX_SOCKETS 4

/** Enable debug messages, comment out the ones you don't want
 */

// Debug - Socket interface messages
#define CC3000_DEBUG_SOCKET

// Debug - HCI TX messages
#define CC3000_DEBUG_HCI_TX

// Debug - HCI Rx messages
#define CC3000_DEBUG_HCI_RX

// Debug - General Debug
#define CC3000_DEBUG

// Add colour to the debug messages, requires a VT100 terminal like putty, comment out to remove
#define VT100_COLOUR

#ifdef CC3000_DEBUG_SOCKET

    #ifdef VT100_COLOUR
        #define DBG_SOCKET(x, ...) std::printf("\x1b[2;32;40m[CC3000 : SOCKET] "x"\x1b[0;37;40m\r\n", ##__VA_ARGS__);
    #else
        #define DBG_SOCKET(x, ...) std::printf("[CC3000 : SOCKET] "x"\r\n", ##__VA_ARGS__);
    #endif

#else
    #define DBG_SOCKET(x, ...)
#endif

#ifdef CC3000_DEBUG_HCI_TX

    #ifdef VT100_COLOUR
        #define DBG_HCI(x, ...) std::printf("\x1b[2;35;40m[CC3000 : HCI RX] "x"\x1b[0;37;40m\r\n", ##__VA_ARGS__);
    #else
        #define DBG_HCI(x, ...) std::printf("[CC3000 : HCI RX] "x"\r\n", ##__VA_ARGS__);
    #endif

#else
    #define DBG_HCI(x, ...)
#endif

#ifdef CC3000_DEBUG_HCI_RX

    #ifdef VT100_COLOUR
        #define DBG_HCI_CMD(x, ...) std::printf("\x1b[2;36;40m[CC3000 : HCI TX] "x"\x1b[0;37;40m\r\n", ##__VA_ARGS__);
    #else
        #define DBG_HCI_CMD(x, ...) std::printf("[CC3000 : HCI TX] "x"\r\n", ##__VA_ARGS__);
    #endif

#else
    #define DBG_HCI_CMD(x, ...)
#endif


#ifdef CC3000_DEBUG

    #ifdef VT100_COLOUR
        #define DBG_CC(x, ...) std::printf("\x1b[2;32;40m[CC3000] "x"\x1b[0;37;40m\r\n", ##__VA_ARGS__);
    #else
        #define DBG_CC(x, ...) std::printf("[CC3000] "x"\r\n", ##__VA_ARGS__);
    #endif

#else
    #define DBG_CC(x, ...)
#endif

namespace mbed_cc3000 {

/** User info structure
 */
typedef struct {
    uint8_t FTC;           // First time config performed
    uint8_t PP_version[2]; // Patch Programmer version
    uint8_t SERV_PACK[2];  // Service Pack Version
    uint8_t DRV_VER[3];    // Driver Version
    uint8_t FW_VER[3];     // Firmware Version
    uint8_t validCIK;      // CIK[] is valid (Client Interface Key)
    uint8_t CIK[40];
} tUserFS;

/** Function pointers which are not yet implemented
 */
enum FunctionNumber {
    FW_PATCHES          = 0,
    DRIVER_PATCHES      = 1,
    BOOTLOADER_PATCHES  = 2,
};

/** CC3000 Simple Link class which contains status of cc3000.
 */
class cc3000_simple_link {
public:
    /**
     *  \brief ctor - sets magic number in the buffers (overflow mark).
     *  \param none
     *  \return none
     */
    cc3000_simple_link();
    /**
     *  \brief dtor
     *  \param none
     *  \return none
     */
    ~cc3000_simple_link();
    /**
     *  \brief Returns data received flag.
     *  \return Data received flag.
     */
    uint8_t get_data_received_flag();
    /**
     *  \brief Set data received flag.
     *  \param value The value to be set.
     */
    void set_data_received_flag(uint8_t value);
    /** Returns if tx was completed.
     *  \return
     *    true if tx was completed,
     *    false otherwise.
     */
    bool get_tx_complete_signal();
    /**
     *  \brief Sets flag that tx was completed.
     *  \param value Value to be set
     *  \return none
     */
    void set_tx_complete_signal(bool value);
    /**
     *  \brief Get receive buffer.
     *  \param none
     *  \return Pointer to the receive buffer.
     */
    uint8_t *get_received_buffer();
    /**
     *  \brief Get transmit buffer.
     *  \param none
     *  \return Pointer to the transmit buffer.
     */
    uint8_t *get_transmit_buffer();
    /**
     *  \brief Get number of free buffers.
     *  \param none
     *  \return
     *      Number of free buffers.
     */
    uint16_t get_number_free_buffers();
    /**
     *  \brief Set number of free buffers.
     *  \param value Number of free buffers.
     *  \return none
     */
    void set_number_free_buffers(uint16_t value);
    /**
     *  \brief Retrieve buffer length.
     *  \param none
     *  \return Buffer length
     */
    uint16_t get_buffer_length();
    /**
     *  \brief Set buffer length
     *  \param value The length
     *  \return none
     */
    void set_buffer_length(uint16_t value);
    /**
     *  \brief Retrieve pending data flag.
     *  \param none
     *  \return Pending data flag
     */
    uint16_t get_pending_data();
    /**
     *  \brief Set pending data flag.
     *  \param value Pending data value.
     *  \return none
     */
    void set_pending_data(uint16_t value);
    /**
     *  \brief Retreive op code.
     *  \param none
     *  \return Op code
     */
    uint16_t get_op_code();
    /**
     *  \brief Set op code.
     *  \param code op code.
     *  \return none
     */
    void set_op_code(uint16_t code);
    /**
     *  \brief Get number of released packets.
     *  \param none
     *  \return Number of released packets.
     */
    uint16_t get_released_packets();
    /**
     *  \brief Set number of released packets.
     *  \param value Number of released packets.
     *  \return none
     */
    void set_number_of_released_packets(uint16_t value);
    /**
     *  \brief Get number of sent packats
     *  \param none
     *  \return Number of sent packets.
     */
    uint16_t get_sent_packets();
    /**
     *  \brief Set number of sent packets
     *  \param value Number of sent packets.
     *  \return none
     */
    void set_sent_packets(uint16_t value);
    /**
     *  \brief Retrieve transmit error
     *  \param none
     *  \return Transmit error
     */
    int32_t get_transmit_error();
    /**
     *  \brief Set transmit error.
     *  \param value Error to be set.
     *  \return none
     */
    void set_transmit_error(int32_t value);
    /**
     *  \brief Get buffer size.
     *  \param none
     *  \return Size of buffer.
     */
    uint16_t get_buffer_size();
    /**
     *  \brief Set buffer size.
     *  \param value Buffer size.
     *  \return none
     */
    void set_buffer_size(uint16_t value);
    /**
     *  \brief Not used currently.
     *  \param function Number of desired function.
     *  \return void pointer to the function (need to recast).
     */
    void *get_func_pointer(FunctionNumber function);
    /**
     *  \brief Retreive pointer to the received data.
     *  \param none
     *  \return Pointer to the received data buffer.
     */
    uint8_t *get_received_data();
    /**
     *  \brief Set received data pointer.
     *  \param pointer Pointer to the buffer.
     *  \return none
     */
    void set_received_data(uint8_t *pointer);
private:
    uint8_t  _data_received_flag;
    bool     _tx_complete_signal;
    uint16_t _rx_event_opcode;
    uint16_t _free_buffers;
    uint16_t _buffer_length;
    uint16_t _buffer_size;
    uint16_t _rx_data_pending;
    uint16_t _sent_packets;
    uint16_t _released_packets;
    int32_t  _transmit_data_error;
    uint8_t  *_received_data;
    uint8_t  _rx_buffer[CC3000_RX_BUFFER_SIZE];
    uint8_t  _tx_buffer[CC3000_TX_BUFFER_SIZE];
private:
    /* Not used currently */
    int8_t *(* _fFWPatches)(uint32_t *length);
    int8_t *(* _fDriverPatches)(uint32_t *length);
    int8_t *(* _fBootLoaderPatches)(uint32_t *length);
};

/** Forward declaration classes
 */
class cc3000_hci;
class cc3000_nvmem;
class cc3000_spi;
class cc3000;

/** Event layer
 */
class cc3000_event {
public:
    /**
     *  \brief
     *  \param
     *  \return
     */
    cc3000_event(cc3000_simple_link &simplelink, cc3000_hci &hci, cc3000_spi &spi, cc3000 &cc3000);
    /**
     *  \brief
     *  \param
     *  \return
     */
     ~cc3000_event();
     /**
     *  \brief
     *  \param
     *  \return
     */
    void hci_unsol_handle_patch_request(uint8_t *event_hdr);
    /**
     *  \brief
     *  \param
     *  \return
     */
    uint8_t *hci_event_handler(void *ret_param, uint8_t *from, uint8_t *fromlen);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t hci_unsol_event_handler(uint8_t *event_hdr);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t hci_unsolicited_event_handler(void);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t get_socket_active_status(int32_t sd);
    /**
     *  \brief
     *  \param
     *  \return
     */
    void set_socket_active_status(int32_t sd, int32_t status);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t hci_event_unsol_flowcontrol_handler(uint8_t *event);
    /**
     *  \brief
     *  \param
     *  \return
     */
    void update_socket_active_status(uint8_t *resp_params);
    /**
     *  \brief
     *  \param
     *  \return
     */
    void simplelink_wait_event(uint16_t op_code, void *ret_param);
    /**
     *  \brief
     *  \param
     *  \return
     */
    void simplelink_wait_data(uint8_t *buffer, uint8_t *from, uint8_t *fromlen);
    /**
     *  \brief
     *  \param
     *  \return
     */
    void received_handler(uint8_t *buffer);
 private:
    uint32_t            socket_active_status;
    cc3000_simple_link  &_simple_link;
    cc3000_hci          &_hci;
    cc3000_spi          &_spi;
    cc3000              &_cc3000;
};

/** Netapp layer
 */
class cc3000_netapp {
public:
    /**
     *  \brief
     *  \param
     *  \return
     */
    cc3000_netapp(cc3000_simple_link &simple_link, cc3000_nvmem &nvmem, cc3000_hci &hci, cc3000_event &event);
    /**
     *  \brief
     *  \param
     *  \return
     */
    ~cc3000_netapp();
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t config_mac_adrress(uint8_t *mac);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t dhcp(uint32_t *ip, uint32_t *subnet_mask,uint32_t *default_gateway, uint32_t *dns_server);
#ifndef CC3000_TINY_DRIVER
    /**
     *  \brief
     *  \param
     *  \return
     */
    void ipconfig(tNetappIpconfigRetArgs *ipconfig);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t timeout_values(uint32_t *dhcp, uint32_t *arp,uint32_t *keep_alive, uint32_t *inactivity);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t ping_send(uint32_t *ip, uint32_t ping_attempts, uint32_t ping_size, uint32_t ping_timeout);
    /**
     *  \brief
     *  \param
     *  \return
     */
    void ping_report();
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t ping_stop();
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t arp_flush();
#endif
private:
    cc3000_simple_link  &_simple_link;
    cc3000_nvmem        &_nvmem;
    cc3000_hci          &_hci;
    cc3000_event        &_event;
};

#ifndef CC3000_UNENCRYPTED_SMART_CONFIG
/** Security class used only if encrypted smart config is set
 */
class cc3000_security {
public:
    /**
     *  \brief
     *  \param
     *  \return
     */
    void expandKey(uint8_t *expanded_key, uint8_t *key);
    /**
     *  \brief
     *  \param
     *  \return
     */
    uint8_t galois_mul2(uint8_t value);
    /**
     *  \brief
     *  \param
     *  \return
     */
    void aes_encr(uint8_t *state, uint8_t *expanded_key);
    /**
     *  \brief
     *  \param
     *  \return
     */
    void aes_decr(uint8_t *state, uint8_t *expanded_key);
    /**
     *  \brief
     *  \param
     *  \return
     */
    void aes_encrypt(uint8_t *state, uint8_t *key);
    /**
     *  \brief
     *  \param
     *  \return
     */
    void aes_decrypt(uint8_t *state, uint8_t *key);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t aes_read_key(uint8_t *key);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t aes_write_key(uint8_t *key);
private:
    uint8_t _expanded_key[176];
};
#endif

/** Socket layer
 */
class cc3000_socket {
public:
    /**
     *  \brief
     *  \param
     *  \return
     */
    cc3000_socket(cc3000_simple_link &simplelink, cc3000_hci &hci, cc3000_event &event);
    /**
     *  \brief
     *  \param
     *  \return
     */
    ~cc3000_socket();
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t socket(int32_t domain, int32_t type, int32_t protocol);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t accept(int32_t sd, sockaddr *addr, socklen_t *addrlen);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t bind(int32_t sd, const sockaddr *addr, int32_t addrlen);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t HostFlowControlConsumeBuff(int32_t sd);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t closesocket(int32_t sd);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t listen(int32_t sd, int32_t backlog);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t connect(int32_t sd, const sockaddr *addr, int32_t addrlen);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t select(int32_t nfds, fd_set *readsds, fd_set *writesds, fd_set *exceptsds, struct timeval *timeout);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t getsockopt (int32_t sd, int32_t level, int32_t optname, void *optval, socklen_t *optlen);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t simple_link_recv(int32_t sd, void *buf, int32_t len, int32_t flags, sockaddr *from, socklen_t *fromlen, int32_t opcode);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t simple_link_send(int32_t sd, const void *buf, int32_t len, int32_t flags, const sockaddr *to, int32_t tolen, int32_t opcode);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t recv(int32_t sd, void *buf, int32_t len, int32_t flags);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t recvfrom(int32_t sd, void *buf, int32_t len, int32_t flags, sockaddr *from, socklen_t *fromlen);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t send(int32_t sd, const void *buf, int32_t len, int32_t flags);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t sendto(int32_t sd, const void *buf, int32_t len, int32_t flags, const sockaddr *to, socklen_t tolen);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t mdns_advertiser(uint16_t mdns_enabled, uint8_t * device_service_name, uint16_t device_service_name_length);
#ifndef CC3000_TINY_DRIVER
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t gethostbyname(uint8_t *hostname, uint16_t name_length, uint32_t *out_ip_addr);
    /**
     *  \brief
     *  \param
     *  \return
     */
    int32_t setsockopt(int32_t sd, int32_t level, int32_t optname, const void *optval, socklen_t optlen);
#endif
private:
    cc3000_simple_link  &_simple_link;
    cc3000_hci          &_hci;
    cc3000_event        &_event;
};

/** SPI communication layer
 */
class cc3000_spi {
public:
    /**
     *  \brief Ctor
     *  \param none
     *  \return none
     */
     cc3000_spi(PinName cc3000_irq, PinName cc3000_en, PinName cc3000_cs, SPI cc3000_spi, IRQn_Type irq_port, cc3000_event &event, cc3000_simple_link &simple_link);
    /**
     *  \brief Dtor
     *  \param none
     *  \return none
     */
     ~cc3000_spi();
     /**
      *  \brief Close SPI - disables IRQ and set received buffer to 0
      *  \param none
      *  \return none
     */
     void close();
     /**
      *  \param none
      *  \return none
     */
     void open();
     /**
      *  \param buffer
      *  \param length
      *  \return none
     */
     uint32_t first_write(uint8_t *buffer, uint16_t length);
     /**
     *  \brief
     *  \param
     *  \return
     */
     uint32_t write(uint8_t *buffer, uint16_t length);
     /**
     *  \brief
     *  \param
     *  \return
     */
     void write_synchronous(uint8_t *data, uint16_t size);
     /**
     *  \brief
     *  \param
     *  \return
     */
     void read_synchronous(uint8_t *data, uint16_t size);
     /**
     *  \brief
     *  \param
     *  \return
     */
     uint32_t read_data_cont();
     /**
     *  \brief
     *  \param
     *  \return
     */
    void wlan_irq_enable();
    /**
     *  \brief
     *  \param
     *  \return
     */
    void wlan_irq_disable();
    /**
     *  \brief
     *  \param
     *  \return
     */
    void wlan_irq_set(uint8_t value);
    /**
     *  \brief
     *  \param
     *  \return
     */
    uint32_t wlan_irq_read();
    /**
     *  \brief
     *  \param
     *  \return
     */
    void WLAN_IRQHandler();
    /**
     *  \brief
     *  \param
     *  \return
     */
    void write_wlan_en(uint8_t value);
private:
    tSpiInfo            _spi_info;
    InterruptIn         _wlan_irq;
    DigitalOut          _wlan_en;
    DigitalOut          _wlan_cs;
    SPI                 _wlan_spi;
    IRQn_Type           _irq_port;
    pFunctionPointer_t  _function_pointer;
    cc3000_event        &_event;
    cc3000_simple_link  &_simple_link;
};

/** HCI layer
 */
class cc3000_hci {
public:
    /**
     *  \brief Ctor
     *  \param spi Reference to spi object.
     *  \return none
     */
    cc3000_hci(cc3000_spi &spi);
    /**
     *  \brief Dtor
     *  \param none
     *  \return none
     */
    ~cc3000_hci();
    /**
     *  \brief Initiate an HCI command.
     *  \param op_code command operation code
     *  \param buffer  pointer to the command's arguments buffer
     *  \param length  length of the arguments
     *  \return ???
     */
    uint16_t command_send(uint16_t op_code, uint8_t *buffer, uint8_t length);
    /**
     *  \brief Initiate an HCI data write operation
     *  \param op_code     command operation code
     *  \param args        pointer to the command's arguments buffer
     *  \param arg_length  length of the arguments
     *  \param data_length length od data
     *  \param tail        pointer to the data buffer
     *  \param tail_length buffer length
     *  \return ???
     */
    uint32_t data_send(uint8_t op_code, uint8_t *args, uint16_t arg_length,
                        uint16_t data_length, const uint8_t *tail, uint16_t tail_length);
    /**
     *  \brief Prepare HCI header and initiate an HCI data write operation.
     *  \param op_code     command operation code
     *  \param buffer      pointer to the data buffer
     *  \param arg_length  arguments length
     *  \param data_length data length
     *  \return none
     */
    void data_command_send(uint16_t op_code, uint8_t *buffer, uint8_t arg_length,
                            uint16_t data_length);
    /**
     *  \brief Prepare HCI header and initiate an HCI patch write operation.
     *  \param op_code     command operation code
     *  \param buffer      pointer to the command's arguments buffer
     *  \param patch       pointer to patch content buffer
     *  \param data_length data length
     *  \return none
     */
    void patch_send(uint8_t op_code, uint8_t *buffer, uint8_t *patch, uint16_t data_length);
private:
    cc3000_spi &_spi;
};

/** NVMEM layer
 */
class cc3000_nvmem {
public:
    /**
     *  \brief Ctor
     *  \param hci          Reference to hci object.
     *  \param event        Reference to event object.
     *  \param simple_link  Reference to simple link object.
     *  \return none
     */
    cc3000_nvmem(cc3000_hci &hci, cc3000_event &event, cc3000_simple_link &simple_link);
    /**
     *  \brief Dtor
     *  \param none
     *  \return none
     */
    ~cc3000_nvmem();
    /**
     *  \brief Reads data from the file referred by the file_id parameter.
     *      Reads data from file offset till length. Err if the file can't be used,
     *      is invalid, or if the read is out of bounds.
     *  \param file_id nvmem file id.
     *  \param length  number of bytes to read.
     *  \param offset  offset in file from where to read.
     *  \param buff    output buffer pointer.
     *  \return
     *      Number of bytes read, otherwise error.
     */
    int32_t read(uint32_t file_id, uint32_t length, uint32_t offset, uint8_t *buff);
    /**
     *  \brief Write data to nvmem.
     *  \param file_id      Nvmem file id
     *  \param length       number of bytes to write
     *  \param entry_offset offset in file to start write operation from
     *  \param buff         data to write
     *  \return
     *      On success 0, error otherwise.
     */
    int32_t write(uint32_t file_id, uint32_t length, uint32_t entry_offset, uint8_t *buff);
    /**
     *  \brief Write MAC address to EEPROM.
     *  \param mac Mac address to be set
     *  \return
     *      On success 0, error otherwise.
     */
    uint8_t set_mac_address(uint8_t *mac);
    /**
     *  \brief Read MAC address from EEPROM.
     *  \param mac Mac address
     *  \return
     *      On success 0, error otherwise.
     */
    uint8_t get_mac_address(uint8_t *mac);
    /**
     *  \brief Program a patch to a specific file ID. The SP data is assumed to be organized in 2-dimensional.
     *      Each line is SP_PORTION_SIZE bytes long.
     *  \param file_id nvmem file id/
     *  \param length  number of bytes to write
     *  \param data    SP data to write
     *  \return
     *      On success 0, error otherwise.
     */
    uint8_t write_patch(uint32_t file_id, uint32_t length, const uint8_t *data);
    /**
     *  \brief Create new file entry and allocate space on the NVMEM. Applies only to user files.
     *  \param file_id nvmem file Id
     *  \param new_len entry ulLength
     *  \return
     */
    int32_t create_entry(uint32_t file_id, uint32_t new_len);
#ifndef CC3000_TINY_DRIVER
    /**
     *  \brief Read patch version. read package version (WiFi FW patch, river-supplicant-NS patch,
     *      bootloader patch)
     *  \param patch_ver First number indicates package ID and the second number indicates
     *      package build number
     *  \return
     *      On success 0, error otherwise.
     */
    uint8_t read_sp_version(uint8_t* patch_ver);
#endif
private:
    cc3000_hci          &_hci;
    cc3000_event        &_event;
    cc3000_simple_link  &_simple_link;
};

/** WLAN layer
 */
class cc3000_wlan {
public:
    /**
     *  \brief Ctor
     *  \param simple_link Reference to simple link object.
     *  \param event       Reference to event object.
     *  \param spi         Reference to spi object.
     *  \param hci         Reference to hci object.
     *  \return none
     */
    cc3000_wlan(cc3000_simple_link &simple_link, cc3000_event &event, cc3000_spi &spi, cc3000_hci &hci);
    /**
     *  \brief Dtor
     *  \param none
     *  \return none
     */
    ~cc3000_wlan();
    /**
     *  \brief Send SIMPLE LINK START to cc3000.
     *  \param patches_available_host Flag to indicate if patches are available.
     *  \return none
     */
    void simpleLink_init_start(uint16_t patches_available_host);
    /**
     *  \brief Start wlan device. Blocking call until init is completed.
     *  \param patches_available_host Flag to indicate if patches are available.
     *  \return none
     */
    void start(uint16_t patches_available_host);
    /**
     *  \brief Stop wlan device
     *  \param none
     *  \return none
     */
    void stop(void);
#ifndef CC3000_TINY_DRIVER
    /**
     *  \brief Connect to AP.
     *  \param sec_type    Security option.
     *  \param ssid        up to 32 bytes, ASCII SSID
     *  \param ssid_length length of SSID
     *  \param b_ssid      6 bytes specified the AP bssid
     *  \param key         up to 16 bytes specified the AP security key
     *  \param key_len     key length
     *  \return
     *      On success, zero is returned. On error, negative is returned.
     */
    int32_t connect(uint32_t sec_type, const uint8_t *ssid, int32_t ssid_length, uint8_t *b_ssid, uint8_t *key, int32_t key_len);
    /**
     *  \brief Add profile. Up to 7 profiles are supported.
     *  \param sec_type                      Security option.
     *  \param ssid                          Up to 32 bytes, ASCII SSID
     *  \param ssid_length                   Length of SSID
     *  \param b_ssid                        6 bytes specified the AP bssid
     *  \param priority                      Up to 16 bytes specified the AP security key
     *  \param pairwise_cipher_or_tx_key_len Key length
     *  \param group_cipher_tx_key_index     Key length for WEP security
     *  \param key_mgmt                      KEY management
     *  \param pf_or_key                     Security key
     *  \param pass_phrase_length            Security key length for WPA\WPA2
     *  \return
     *      On success, zero is returned. On error, negative is returned.
     */
    int32_t add_profile(uint32_t sec_type, uint8_t* ssid, uint32_t ssid_length, uint8_t *b_ssid, uint32_t priority, uint32_t pairwise_cipher_or_tx_key_len, uint32_t group_cipher_tx_key_index,
                          uint32_t key_mgmt, uint8_t* pf_or_key, uint32_t pass_phrase_length);
    /**
     *  \brief Gets entry from scan result table. The scan results are returned
     *      one by one, and each entry represents a single AP found in the area.
     *  \param scan_timeout Not supported yet
     *  \param results      Scan result
     *  \return
     *      On success, zero is returned. On error, -1 is returned
     */
    int32_t ioctl_get_scan_results(uint32_t scan_timeout, uint8_t *results);
    /**
     *  \brief Start and stop scan procedure. Set scan parameters.
     *  \param enable             Start/stop application scan
     *  \param min_dwell_time     Minimum dwell time value to be used for each channel, in ms. (Default: 20)
     *  \param max_dwell_time     Maximum dwell time value to be used for each channel, in ms. (Default: 30)
     *  \param num_probe_requests Max probe request between dwell time. (Default:2)
     *  \param channel_mask       Bitwise, up to 13 channels (0x1fff).
     *  \param rssi_threshold     RSSI threshold. Saved: yes (Default: -80)
     *  \param snr_threshold      NSR threshold. Saved: yes (Default: 0)
     *  \param default_tx_power   probe Tx power. Saved: yes (Default: 205)
     *  \param interval_list      Pointer to array with 16 entries (16 channels)
     *  \return
     *      On success, zero is returned. On error, -1 is returned.
     */
    int32_t ioctl_set_scan_params(uint32_t enable, uint32_t min_dwell_time, uint32_t max_dwell_time, uint32_t num_probe_requests,
                                uint32_t channel_mask, int32_t rssi_threshold, uint32_t snr_threshold, uint32_t default_tx_power, uint32_t *interval_list);
    /**
     *  \brief Get wlan status: disconnected, scanning, connecting or connected
     *  \param none
     *  \return
     *      WLAN_STATUS_DISCONNECTED, WLAN_STATUS_SCANING, STATUS_CONNECTING or WLAN_STATUS_CONNECTED
     */
    int32_t ioctl_statusget(void);
#else
    /**
     *  \brief Connect to AP
     *  \param ssid        Up to 32 bytes and is ASCII SSID of the AP
     *  \param ssid_length Length of the SSID
     *  \return
     *      On success, zero is returned. On error, negative is returned.
     */
    int32_t connect(const uint8_t *ssid, int32_t ssid_length);
    /**
     *  \brief When auto start is enabled, the device connects to station from the profiles table.
     *      If several profiles configured the device choose the highest priority profile.
     *  \param sec_type                      WLAN_SEC_UNSEC,WLAN_SEC_WEP,WLAN_SEC_WPA,WLAN_SEC_WPA2
     *  \param ssid                          SSID up to 32 bytes
     *  \param ssid_length                   SSID length
     *  \param b_ssid                        bssid 6 bytes
     *  \param priority                      Profile priority. Lowest priority:0.
     *  \param pairwise_cipher_or_tx_key_len Key length for WEP security
     *  \param group_cipher_tx_key_index     Key index
     *  \param key_mgmt                      KEY management
     *  \param pf_or_key                     Security key
     *  \param pass_phrase_length            Security key length for WPA\WPA2
     *  \return
     *      On success, zero is returned. On error, -1 is returned
     */
    int32_t add_profile(uint32_t sec_type, uint8_t *ssid, uint32_t ssid_length, uint8_t *b_ssid, uint32_t priority,
                      uint32_t pairwise_cipher_or_tx_key_len, uint32_t group_cipher_tx_key_index, uint32_t key_mgmt,
                      uint8_t* pf_or_key, uint32_t pass_phrase_length);
#endif
#ifndef CC3000_UNENCRYPTED_SMART_CONFIG
    /**
     *  \brief Process the acquired data and store it as a profile.
     *  \param none
     *  \return
     *      On success, zero is returned. On error, -1 is returned.
     */
    int32_t smart_config_process(void);
#endif
    /**
     *  \brief Disconnect connection from AP.
     *  \param none
     *  \return
     *      0 if disconnected done, other CC3000 already disconnected.
     */
    int32_t disconnect();
    /**
     *  \brief When auto is enabled, the device tries to connect according the following policy:
     *      1) If fast connect is enabled and last connection is valid, the device will try to
     *      connect to it without the scanning procedure (fast). The last connection will be
     *      marked as invalid, due to adding/removing profile.
     *      2) If profile exists, the device will try to connect it (Up to seven profiles).
     *      3) If fast and profiles are not found, and open mode is enabled, the device
     *      will try to connect to any AP.
     *      Note that the policy settings are stored in the CC3000 NVMEM.
     *  \param should_connect_to_open_ap Enable(1), disable(0) connect to any available AP.
     *  \param use_fast_connect          Enable(1), disable(0). if enabled, tries to
     *                                   connect to the last connected AP.
     *  \param use_profiles              Enable(1), disable(0) auto connect after reset.
     *                                   and periodically reconnect if needed.
     *  \return
     *      On success, zero is returned. On error, -1 is returned
     */
    int32_t ioctl_set_connection_policy(uint32_t should_connect_to_open_ap, uint32_t use_fast_connect, uint32_t use_profiles);
    /**
     *  \brief Delete WLAN profile
     *  \param index Number of profile to delete
     *  \return
     *      On success, zero is returned. On error, -1 is returned
     */
    int32_t ioctl_del_profile(uint32_t index);
    /**
     *  \brief Mask event according to bit mask. In case that event is
     *      masked (1), the device will not send the masked event to host.
     *  \param mask event mask
     *  \return
     *      On success, zero is returned. On error, -1 is returned
     */
    int32_t set_event_mask(uint32_t mask);
    /**
     *  \brief Start to acquire device profile. The device acquire its own
     *      profile, if profile message is found.
     *  \param encrypted_flag Indicates whether the information is encrypted
     *  \return
     *      On success, zero is returned. On error, -1 is returned.
     */
    int32_t smart_config_start(uint32_t encrypted_flag);
    /**
     *  \brief Stop the acquire profile procedure.
     *  \param none
     *  \return
     *      On success, zero is returned. On error, -1 is returned
     */
    int32_t smart_config_stop(void);
    /**
     *  \brief Configure station ssid prefix.
     *  \param new_prefix 3 bytes identify the SSID prefix for the Smart Config.
     *  \return
     *      On success, zero is returned. On error, -1 is returned.
     */
    int32_t smart_config_set_prefix(uint8_t *new_prefix);
private:
    cc3000_simple_link  &_simple_link;
    cc3000_event        &_event;
    cc3000_spi          &_spi;
    cc3000_hci          &_hci;
};

/** The main object of cc3000 implementation
 */
class cc3000 {
public:
    /** status structure */
    typedef struct {
        bool    dhcp;
        bool    connected;
        uint8_t socket;
        bool    smart_config_complete;
        bool    stop_smart_config;
        bool    dhcp_configured;
        bool    ok_to_shut_down;
    } tStatus;
    /**
     *  \brief Ctor.
     *  \param cc3000_irq IRQ pin
     *  \param cc3000_en  Enable pin
     *  \param cc3000_cs  Chip select pin
     *  \param cc3000_spi SPI interface
     *  \param irq_port   IRQ pin's port
     */
    cc3000(PinName cc3000_irq, PinName cc3000_en, PinName cc3000_cs, SPI cc3000_spi, IRQn_Type irq_port);
    /**
     *  \brief Dtor.
     */
    ~cc3000();
    /**
     *  \brief Initiate cc3000. It starts the wlan communication and deletes profiles.
     *  \param patch Patch
     */
    void start(uint8_t patch);
    /**
     *  \brief Stops the wlan communication.
     */
    void stop();
    /**
     *  \brief Restarts the wlan communication.
     */
    void restart(uint8_t patch);
    /**
     *  \brief Disconnect wlan device
     *
     */
    bool disconnect(void);
    /**
     *  \brief Callback which is called from the event class. This updates status of cc3000.
     *  \param event_type Type of the event
     *  \param data       Pointer to data
     *  \param length     Length of data
     *  \return none
     */
    void usync_callback(int32_t event_type, uint8_t *data, uint8_t length);
    /**
     *  \brief Connect to SSID (open/secured) with timeout (10ms).
     *  \param ssid          SSID name
     *  \param key           Security key (if key = 0, open connection)
     *  \param security_mode Security mode
     *  \return true if connection was established, false otherwise.
     */
    bool connect_to_AP(const uint8_t *ssid, const uint8_t *key, int32_t security_mode);
    /**
     *  \brief Connect to SSID which is secured
     *  \param ssid          SSID name
     *  \param key           Security key
     *  \param security_mode Security mode
     *  \return true if connection was established, false otherwise.
     */
    bool connect_secure(const uint8_t *ssid, const uint8_t *key, int32_t security_mode);
    /**
     *  \brief Connect to SSID which is open (no security)
     *  \param ssid          SSID name
     *  \return true if connection was established, false otherwise.
     */
    bool connect_open(const uint8_t *ssid);
    /**
     *  \brief Status of the cc3000 connection.
     *  \return true if it's connected, false otherwise.
     */
    bool is_connected();
    /**
     *  \brief Status of DHCP.
     *  \param none
     *  \return true if DCHP is configured, false otherwise.
     */
    bool is_dhcp_configured();
    /**
     *  \brief Status of smart confing completation.
     *  \param none
     *  \return smart config was set, false otherwise.
     */
    bool is_smart_confing_completed();
    /**
     *  \brief Return the cc3000's mac address.
     *  \param address Retreived mac address.
     *  \return
     */
    uint8_t get_mac_address(uint8_t address[6]);
    /**
     *  \brief Set the cc3000's mac address.
     *  \param address Mac address to be set.
     *  \return
     */
    uint8_t set_mac_address(uint8_t address[6]);
    /**
     *  \brief Get user file info.
     *  \param  info_file Pointer where info will be stored.
     *  \param  size      Available size.
     *  \return none
     */
    void get_user_file_info(uint8_t *info_file, size_t size);
    /**
     *  \brief Set user filo info.
     *  \param info_file Pointer to user's info.
     *  \return none
     */
    void set_user_file_info(uint8_t *info_file, size_t size);
    /**
     *  \brief Start smart config.
     *  \param smart_config_key Pointer to smart config key.
     *  \return none
     */
    void start_smart_config(const uint8_t *smart_config_key);  /* TODO enable AES ? */
#ifndef CC3000_TINY_DRIVER
    /**
     *  \brief Return ip configuration.
     *  \param ip_config Pointer to ipconfig data.
     *  \return true if it's connected and info was retrieved, false otherwise.
     */
    bool get_ip_config(tNetappIpconfigRetArgs *ip_config);
#endif
    /**
     *  \brief Delete all stored profiles.
     *  \param none
     *  \return none
     */
    void delete_profiles(void);
    /**
     *  \brief Ping an ip address.
     *  \param ip       Destination IP address
     *  \param attempts Number of attempts
     *  \param timeout  Time to wait for a response,in milliseconds.
     *  \param size     Send buffer size which may be up to 1400 bytes
     */
    uint32_t ping(uint32_t ip, uint8_t attempts, uint16_t timeout, uint8_t size);
    /**
     *  \brief Returns cc3000 instance. Used in Socket interface.
     *  \param none
     *  \return Pointer to cc3000 object
     */
    static cc3000 *get_instance() {
        return _inst;
    }
public:
    cc3000_simple_link  _simple_link;
    cc3000_event        _event;
#ifndef CC3000_UNENCRYPTED_SMART_CONFIG
    cc3000_security     _security;
#endif
    cc3000_socket       _socket;
    cc3000_spi          _spi;
    cc3000_hci          _hci;
    cc3000_nvmem        _nvmem;
    cc3000_netapp       _netapp;
    cc3000_wlan         _wlan;
protected:
    static cc3000       *_inst;
private:
    tStatus                  _status;
    netapp_pingreport_args_t _ping_report;
    bool                     _closed_sockets[MAX_SOCKETS];
};

/**
 * Copy 32 bit to stream while converting to little endian format.
 * @param  p       pointer to the new stream
 * @param  u32     pointer to the 32 bit
 * @return         pointer to the new stream
 */
uint8_t *UINT32_TO_STREAM_f (uint8_t *p, uint32_t u32);

/**
 * Copy 16 bit to stream while converting to little endian format.
 * @param  p       pointer to the new stream
 * @param  u32     pointer to the 16 bit
 * @return         pointer to the new stream
 */
uint8_t *UINT16_TO_STREAM_f (uint8_t *p, uint16_t u16);

/**
 * Copy received stream to 16 bit in little endian format.
 * @param  p          pointer to the stream
 * @param  offset     offset in the stream
 * @return            pointer to the new 16 bit
 */
uint16_t STREAM_TO_UINT16_f(uint8_t* p, uint16_t offset);

/**
 * Copy received stream to 32 bit in little endian format.
 * @param  p          pointer to the stream
 * @param  offset     offset in the stream
 * @return            pointer to the new 32 bit
 */
uint32_t STREAM_TO_UINT32_f(uint8_t* p, uint16_t offset);

} /* end of mbed_cc3000 namespace */


#endif