/*******************************************************************************
  SMTP Client Demonstrations

  Summary:
    SUMMARY

  Description:
    - SMTP Client (short message)
    - SMTP Client (long message, not enabled)
*******************************************************************************/

/*******************************************************************************
FileName:  SMTPDemo.c
Copyright © 2012 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.
*******************************************************************************/

#define __SMTPDEMO_C

#include "tcpip_config.h"

#if defined(TCPIP_STACK_USE_SMTP_CLIENT)

#include "tcpip/tcpip.h"
#include "main_demo.h"
#include <system/system_userio.h>

/*****************************************************************************
  Function:
   void SMTPDemo(void)

  Summary:
   Demonstrates use of the e-mail (SMTP) client.

  Description:
   This function demonstrates the use of the SMTP client.  The function is
   called periodically by the stack, and checks if BUTTON2 and BUTTON3 are
   pressed simultaneously.  If they are, it attempts to send an e-mail
   message using parameters hard coded in the function below.

   While the client is executing, LED1 will be used as a busy indicator.
   LED2 will light when the transmission has been completed successfully.
   If both LEDs extinguish, an error occurred.

   For an example of sending a longer message (one that does not exist in
   RAM all at once), see the commented secondary implementation of this
   function in this file (SMTPDemo.c) below.  For an example of sending
   a message using parameters gathered at run time, and/or a message with
   attachments, see the implementation of HTTPPostEmail in CustomHTTPApp.c.

  Precondition:
   The SMTP client is initialized.

  Parameters:
   None

  Returns:
     None
  ***************************************************************************/
void SMTPDemo(void)
{

   // Send an email once if someone pushes BUTTON2 and BUTTON3 at the same time
   // This is a simple message example, where the message
   // body must already be in RAM.
   // LED1 will be used as a busy indicator
   // LED2 will be used as a mail sent successfully indicator
   static enum
   {
       MAIL_HOME = 0,
       MAIL_BEGIN,
       MAIL_SMTP_FINISHING,
       MAIL_DONE
   } MailState = MAIL_HOME;
   static SYS_TICK WaitTime;
   SMTP_POINTERS mySMTPClient;

   switch(MailState)
   {

      case MAIL_HOME:
         if(SYS_USERIO_ButtonGet((SYS_USERIO_BUTTON_1|SYS_USERIO_BUTTON_2),SYS_USERIO_BUTTON_ASSERTED))
         {
            // Start sending an email
            SYS_USERIO_SetLED(SYS_USERIO_LED_1, SYS_USERIO_LED_ASSERTED);
            MailState++;
            SYS_USERIO_SetLED(SYS_USERIO_LED_2, SYS_USERIO_LED_DEASSERTED);
         }
      break;

      case MAIL_BEGIN:
         if(SMTPBeginUsage())
         {
            // Note that these strings must stay allocated in
            // memory until SMTPIsBusy() returns false.  To
            // guarantee that the C compiler does not reuse this
            // memory, you must allocate the strings as static.

            static uint8_t RAMStringTo[] = "joe@picsaregood.com";
            //static uint8_t RAMStringCC[] = "foo@picsaregood.com, \"Jane Smith\" <jane.smith@picsaregood.com>";
            //static uint8_t RAMStringBCC[] = "";
            static uint8_t RAMStringBody[] = "Message generated by stack " TCPIP_STACK_VERSION " \r\n\r\nButtons: 3210";
            RAMStringBody[sizeof(RAMStringBody)-2] = '0' + BUTTON0_IO;
            RAMStringBody[sizeof(RAMStringBody)-3] = '0' + BUTTON1_IO;
            RAMStringBody[sizeof(RAMStringBody)-4] = '0' + BUTTON2_IO;
            RAMStringBody[sizeof(RAMStringBody)-5] = '0' + BUTTON3_IO;

            memset(&mySMTPClient, 0, sizeof(mySMTPClient));
            mySMTPClient.Server = "mail";   // SMTP server address
            //mySMTPClient.Username = "mchpboard";
            //mySMTPClient.Password = "secretpassword";
            mySMTPClient.To = (char*)RAMStringTo;
            mySMTPClient.From = "\"SMTP Service\" <mchpboard@picsaregood.com>";
            mySMTPClient.Subject = "Hello world!  SMTP Test.";
            mySMTPClient.Body = (char*)RAMStringBody;
            SMTPSendMail(&mySMTPClient);
            MailState++;
         }
         break;

      case MAIL_SMTP_FINISHING:
         if(!SMTPIsBusy())
         {
             // Finished sending mail
             SYS_USERIO_SetLED (SYS_USERIO_LED_1, SYS_USERIO_LED_DEASSERTED);
             MailState++;
             WaitTime = SYS_TICK_Get();
             SYS_USERIO_SetLED(SYS_USERIO_LED_2,(SMTPEndUsage() == SMTP_SUCCESS));
         }
         break;

      case MAIL_DONE:
         // Wait for the user to release BUTTON2 or BUTTON3 and for at
         // least 1 second to pass before allowing another
         // email to be sent.  This is merely to prevent
         // accidental flooding of email boxes while
         // developing code.  Your application may wish to
         // remove this.
         if(SYS_USERIO_ButtonGet((SYS_USERIO_BUTTON_1|SYS_USERIO_BUTTON_2),SYS_USERIO_BUTTON_ASSERTED))
         {
            if(SYS_TICK_Get() - WaitTime > SYS_TICK_TicksPerSecondGet())
               MailState = MAIL_HOME;
         }
         break;
   }
}

#endif //#if defined(TCPIP_STACK_USE_SMTP_CLIENT)
