/*******************************************************************************
    TCP6 Demo Application Source File

    Company:
    Microchip Technology Inc.

    File Name:
    tcp6_demo_app.h

    Summary:
    Contains declaration of "tcp6_demo_app_xxx() APIs"
    Implements a custom HTTP client-based application.

    Description:

*******************************************************************************/

// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013 released Microchip Technology Inc.  All rights reserved.

Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).

You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.

SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
// DOM-IGNORE-END

#ifndef _TCP6_DEMO_APP_H    // Guards against multiple inclusion
#define _TCP6_DEMO_APP_H


// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************


#ifdef __cplusplus  // Provide C++ Compatability

    extern "C" {

#endif


// *****************************************************************************
// *****************************************************************************
// Section: Constants
// *****************************************************************************
// *****************************************************************************

/* HTTP constants */

#define HTTP_SERVER_PORT_NUMBER  80
#define HTTPS_SERVER_PORT_NUMBER  443

// *****************************************************************************
// *****************************************************************************
// Section: Data Types
// *****************************************************************************
// *****************************************************************************

// Data type to store static (compile-time) parameters for
// each http transaction to be executed
typedef struct
{
    const char* id;         // http transaction identifier (from Web API documentation)
    const char* method;     // http method to use (ex. GET, PUT, DELETE etc)
    const char* scheme;     // scheme (http or https)
    const char* host;       // domain name of host (ex. www.microchip.com)
    const char* port;       // port number for the scheme
    const char* path;       // target path
    const char* version;    // http version
    const char* username;   // http "basic" authentication credential "username"
    const char* password;   // http "basic" authentication credential "password"
    const char *headers[8]; // number of static headers (max. of 8/transaction supported in the application)
    int entity_body;        // flag to indicate if the http transaction requires a body
    unsigned int dispatch_delay;    // how long between transactions (seconds)
} HTTP_TRANSACTION_STATIC;

// Dynamic HTTP Transaction Data Structure 
// filled in per transaction scheduled at run-time
typedef struct
{
    char id[8];                     // http transaction identifier
    char method[8];                 // http method ("GET", "POST" etc..
    char scheme[8];                 // scheme ("http", "https")
    char host[64];                  // target host ("www.microchip.com")
    char port[8];                   // target port ("80")
    char path[64];                  // target path ("/index.html")
    char version[16];               // http version ("HTTP/1.1")
    char username[64];              // http "basic" authentication credential "username"
    char password[64];              // http "basic" authentication credential "password"
    char headers[10][64];           // number of dynamic request headers (#static + 2)
    char entity_body[256];          // the body of the message
} HTTP_TRANSACTION_DYNAMIC;

/* TCP6 Web API Resource Data Structure */
/* Resource = "the record of a specific board"*/
/* See the WEB API Spec document "17052 TCP6 Web API v016" */
/* MONITOR variables: macaddr, pot, leds, buttons */
/* CONTROL variables: ledtoggle, status */
typedef struct
{
    char collection[16];    // collection name
    char board[16];         // board name
    char username[20];      // used to hold resource username credential (defined at commissioning)
    char password[16];      // used to hold resource password credential (defined at commissioning)
                            // the password credential is never transmitted in the body of the http messages
                            // it is only used for http 'basic' authentication
    char macaddr[16];       // used to represent the board's mac address
    char pot[80];           //
    unsigned int leds;      // state of 8 leds on explorer16 board
    unsigned int buttons;   // state of push buttons
    unsigned int ledtoggle; // LED control variable (controlled by web user)
    unsigned int status;    // COMET control variable (reserved for future use)
} TCP6_WEBAPI_RESOURCE_BOARD;

// tcp6_deno_app state machine states
// ref: "TCP6_Demo_App_SM_006.pptx"
typedef enum
{
    SM_IDLE = 0,
    SM_INIT,                            // one-time application initialization
    SM_HTTPC_TRANSACTION_DISPATCH,      // dispatch the next http transaction
    SM_RESOURCE_UPDATE,                 // update data to send to server on PUT
    SM_HTTPC_TRANSACTION_CONFIG,        // configure the http transaction
    SM_CONNECT_SERVER_WAIT_ASSOCIATE,   // wait for wifi association (skipped if no WiFi interface)
    SM_CONNECT_SERVER_WAIT_DHCP,        // make sure dhcp process finished
    SM_CONNECT_SERVER_BEGIN_DNS,        // start DNS process
    SM_CONNECT_SERVER_WAIT_DNS,         // wait for DNS resolution
    SM_CONNECT_SERVER_OPEN_SOCKET,      // create a client socket, and
    SM_CONNECT_SERVER_WAIT_CONNECT,     // wait for the server to accept the connection request
    SM_HTTPC_TRANSACTION_SEND,          // send the http request
    SM_HTTPC_TRANSACTION_RECEIVE,       // receive the response
    SM_HTTPC_TRANSACTION_CLEANUP,       // disconnect from the server, cleanup variables
    SM_DISPATCH_RESPONSE_HANDLER        // application to process the reply data

} TCP6_DEMO_APP_SM;

// *****************************************************************************
// *****************************************************************************
// Section: Helper Macros
// *****************************************************************************
// *****************************************************************************


// *****************************************************************************
// *****************************************************************************
// Section: Interface Routines
// *****************************************************************************
// *****************************************************************************

// TCP6 Demo App State Machine
void tcp6_demo_app_init(void);
void tcp6_demo_app_tasks(TCPIP_NET_HANDLE netH);

// Base64 Encode Routines
void encodeblock(unsigned char in[], char b64str[], int len);
void b64_encode(char *clrstr, char *b64dst);

void WFStateNotify(uint8_t state);

// *****************************************************************************
// *****************************************************************************
// Section: Interface Routines Group
// *****************************************************************************
// *****************************************************************************
/*  An optional brief description of the group of routines is helpful, but will
    not be picked up by the document tool.
*/


#ifdef __cplusplus  // Provide C++ Compatibility

    }

#endif

#endif // _TCP6_DEMO_APP_H

/*******************************************************************************
 End of File
*/
