SMTP.c File Reference

Send mail to SMTP server. More...

#include <BoardSupport/inc/stdtypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
#include <time.h>
#include <string.h>
#include <Libs/NETlib/net.h>
#include <BoardSupport/inc/BoardSpecific.h>
#include <Common/Common.h>
#include <Common/uartio.h>
#include <Common/timer.h>
#include <Common/CPrintf.h>
#include <BoardSupport/config/netconfig.c>

Macros

#define MAX_DATA   512
 
#define SMTPGATEWAY   "deepthought"
 
#define SOURCE_EMAIL_ADDRESS   "dsp@dsignt.de"
 
#define SOURCE_PASS   "testpass"
 
#define DEST_EMAIL_ADDRESS   "claus.hermbusche@dsignt.de"
 
#define SMTP_SERVICE_READY   UINT32_C(220)
 
#define SMTP_SERVICE_CLOSING   UINT32_C(221)
 
#define SMTP_REQUEST_OK   UINT32_C(250)
 
#define SMTP_AUTHENTICATED   UINT32_C(235)
 
#define SMTP_START_MAIL   UINT32_C(354)
 
#define UTC_PRINTF   " Current UTC time: "VT100_BLUE"%s\r"VT100_DEFAULT
 

Enumerations

enum  MAILSTATE_t {
  MS_IDLE_STATE = 0, MS_HELO_STATE, MS_AUTH_STATE, MS_USER_STATE,
  MS_PASS_STATE, MS_MAIL_FROM_STATE, MS_RCPT_TO_STATE, MS_SEND_DATA_STATE,
  MS_SEND_MAIL_STATE, MS_ENDL_STATE
}
 
enum  ML_t { ML_STOP_STATE = 0, ML_WAIT_LINK_STATE, ML_SEND_MAIL_STATE, ML_IDLE_STATE }
 

Functions

void encodeblock (u_int8_t *in, u_int8_t *out, int32_t len)
 
int32_t get_reply_code (SOCKET *mail, char *mail_data, char *text)
 
FUNC_RETURN send_mail (char *text, char *msg)
 
int wait_for_link (void)
 
int main (void)
 

Variables

char * program_name = "SMTP"
 
static char data_msg [] = {"DATA\r\n"}
 
static char auth_msg [] = {"AUTH LOGIN\r\n"}
 
static char auth_name [] = {"dsp@dsignt.de"}
 
static char auth_pass [] = SOURCE_PASS
 
static char mail_from [] = {"MAIL FROM: "SOURCE_EMAIL_ADDRESS"\r\n"}
 
static char rcpt_msg [] = {"RCPT TO: "DEST_EMAIL_ADDRESS"\r\n"}
 
static char quit_msg [] = {"QUIT\r\n"}
 
static char cmd_string [120] = {0}
 
static char from_msg [] = {"From: "SOURCE_EMAIL_ADDRESS"\r\n"}
 
static char to_msg [] = {"To: "DEST_EMAIL_ADDRESS"\r\n"}
 
static char sub_msg [] = {"Subject: Test mail from DSP\r\n"}
 
static char cont_msg [] = {"Content-Type: text/plain\r\n"}
 
static char prio_msg [] = {"Priority: Urgent\r\n\r\n"}
 
static char endmail [] = {"\r\n.\r\n"}
 
const char * eMessages []
 
char * MailCycleMessage []
 

Detailed Description

                          _         _             _
                       __| |    ___(_) ____ _ __ | |_
                      / _` |   / __| |/ _` | '_ \| __|
                     | (_| | _ \__ \ | (_| | | | | |_
                      \__,_|(_) ___/_|\__, |_| |_|\__|
                     Signalprocessing |___/ Technology
Author
D.SignT GmbH & Co. KG, Claus Hermbusche
Date
2019-06-03

More information on the exact specification of SMTP and the meanings of the commands and responses can be found in RFC821 at http://www.ietf.org. and at https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol

Macro Definition Documentation

#define MAX_DATA   512
Examples:
SMTP.c.
#define SMTPGATEWAY   "deepthought"
Examples:
SMTP.c.
#define SOURCE_EMAIL_ADDRESS   "dsp@dsignt.de"
Examples:
SMTP.c.
#define SOURCE_PASS   "testpass"
Examples:
SMTP.c.
#define DEST_EMAIL_ADDRESS   "claus.hermbusche@dsignt.de"
Examples:
SMTP.c.
#define SMTP_SERVICE_READY   UINT32_C(220)
Examples:
SMTP.c.
#define SMTP_SERVICE_CLOSING   UINT32_C(221)
Examples:
SMTP.c.
#define SMTP_REQUEST_OK   UINT32_C(250)
Examples:
SMTP.c.
#define SMTP_AUTHENTICATED   UINT32_C(235)
Examples:
SMTP.c.
#define SMTP_START_MAIL   UINT32_C(354)
Examples:
SMTP.c.
#define UTC_PRINTF   " Current UTC time: "VT100_BLUE"%s\r"VT100_DEFAULT
Examples:
SMTP.c, and SNTPTest.c.

Enumeration Type Documentation

Enumerator
MS_IDLE_STATE 
MS_HELO_STATE 
MS_AUTH_STATE 
MS_USER_STATE 
MS_PASS_STATE 
MS_MAIL_FROM_STATE 
MS_RCPT_TO_STATE 
MS_SEND_DATA_STATE 
MS_SEND_MAIL_STATE 
MS_ENDL_STATE 
Examples:
SMTP.c.
141 {
142  MS_IDLE_STATE = 0,
152 } MAILSTATE_t;
Definition: SMTP.c:150
Definition: SMTP.c:142
Definition: SMTP.c:151
Definition: SMTP.c:147
Definition: SMTP.c:146
Definition: SMTP.c:149
Definition: SMTP.c:143
Definition: SMTP.c:144
Definition: SMTP.c:148
Definition: SMTP.c:145
MAILSTATE_t
Definition: SMTP.c:140
enum ML_t
Enumerator
ML_STOP_STATE 
ML_WAIT_LINK_STATE 
ML_SEND_MAIL_STATE 
ML_IDLE_STATE 
Examples:
SMTP.c.
157  {
158  ML_STOP_STATE = 0, /* (0) stop main loop */
159  ML_WAIT_LINK_STATE, /* (1) wait for link */
160  ML_SEND_MAIL_STATE, /* (2) send emails */
161  ML_IDLE_STATE /* (3) idle network */
162 } ML_t;
Definition: SMTP.c:161
Definition: SMTP.c:159
Definition: SMTP.c:160
Definition: SMTP.c:158
ML_t
Definition: SMTP.c:157

Function Documentation

void encodeblock ( u_int8_t *  in,
u_int8_t *  out,
int32_t  len 
)
Examples:
SMTP.c.
int32_t get_reply_code ( SOCKET *  mail,
char *  mail_data,
char *  text 
)
Examples:
SMTP.c.
249 {
250  /***************************************************************************
251  locals
252  ***************************************************************************/
253  int32_t ret = INT32_C(0);
254  T_TimeOutTimer tot;
255 
256  if ( mail == NULL )
257  {
258  /***********************************************************************
259  invalid socket
260  ***********************************************************************/
261  return (INT32_C(-1));
262  }
263 
264  /***************************************************************************
265  wait for answer or timeout
266  ***************************************************************************/
267  ResetTimeOutTimer (&tot, '.', 10000, text);
268  while ( ret == INT32_C(0) )
269  {
270  /***********************************************************************
271  stop after 5 seconds
272  ***********************************************************************/
273  if (ProcessTimeout (&tot, 5)) break;
274 
275  net_isq ();
276  if ( net_recv (mail, mail_data, MAX_DATA))
277  {
278  /*******************************************************************
279  check events
280  if NULL is passed as pLog parameter, no message is printed to output
281  use net_recv_event_handler() to determine the data length waiting
282  in the receive buffer
283  *******************************************************************/
284  ret = net_recv_event_handler (mail, NULL); /* without messages */
285  if (ret == INT32_C(0))
286  {
287  /***************************************************************
288  receive error
289  ***************************************************************/
290  return (INT32_C(-1));
291  }
292  }
293  } // while
294 
295  /***************************************************************************
296  cut descriptive string; we are interested in return code only
297  ***************************************************************************/
298  mail_data[3] = 0;
299 
300 #if (defined(_TMS320C6X) )
301  return (atoi (mail_data));
302 #else
303  return (atol (mail_data));
304 #endif
305 }
#define MAX_DATA
Definition: SMTP.c:124
#define INT32_C(value)
Definition: stdint.h:209
#define NULL
Definition: net.h:126
int32_t net_recv(SOCKET *so, void *data, uint16_t maxdatasize)
Receive data via the specified socket.
FUNC_RETURN ProcessTimeout(T_TimeOutTimer *tot, time_t timeout)
Definition: timer.c:219
Definition: timer.h:86
FUNC_RETURN ResetTimeOutTimer(T_TimeOutTimer *tot, char symbol, time_t period, char *text)
Definition: timer.c:175
void net_isq(void)
The main polling function for processing sockets, must be periodically called in the main application...
int int32_t
Definition: stdint.h:46
FUNC_RETURN send_mail ( char *  text,
char *  msg 
)
Examples:
SMTP.c.
319 {
320  /***************************************************************************
321  locals
322  ***************************************************************************/
323  SOCKET *mail; /* socket descriptor 1 for transmit */
324  char *mail_data; /* data buffer */
325  MAILSTATE_t ms = MS_IDLE_STATE; /* state */
326  FUNC_RETURN f_result = FR_ERROR; /* result */
327  T_TimeOutTimer tot;
328 
329  u_int8_t in[3], out[4];
330  int i;
331  int32_t len;
332  char *p, *t;
333 
334  /***************************************************************************
335  malloc some space for data
336  ***************************************************************************/
337  mail_data = (char *) malloc (MAX_DATA * sizeof(char));
338 
339  if ( mail_data == NULL )
340  {
341  return (f_result);
342  }
343 
344  /***************************************************************************
345  open socket:
346  ***************************************************************************/
347  mail = socket_open (SMTPGATEWAY,
348  // 25, // traditional SMTP port
349  587, // standard port for SMTP
350  ANY_PORT,
352  TCP_INIT_FUNC);
353  if ( mail == INVALID_SOCKET )
354  {
355  /***********************************************************************
356  couldn't get a free socket, free memory and return
357  ***********************************************************************/
358  free (mail_data);
359  return (f_result);
360  } // if
361 
362  /***************************************************************************
363  set the receive buffer size
364  ***************************************************************************/
365  set_recv_buffer (mail, mail_data, MAX_DATA);
366 
367  /***************************************************************************
368  try to connect to mail server
369  ***************************************************************************/
370  ResetTimeOutTimer (&tot, '.', 10000, " connecting mail server ");
371  for (;;)
372  {
373  /***********************************************************************
374  stop after 6 seconds
375  ***********************************************************************/
376  if (ProcessTimeout (&tot, 6))
377  {
378  /*******************************************************************
379  connect error, free memory and return
380  *******************************************************************/
381  socket_close (mail);
382  free (mail_data);
383  return (FR_TIMEDOUT);
384  }
385  if ( connect (mail, mail_data, UINT32_C(1), SMTP_CONNECT_TIMEOUT) == TRUE ) break;
386  }
387 
388  /***************************************************************************
389  start state machine
390  ***************************************************************************/
391  ms = MS_HELO_STATE;
392 
393  while ( ms > MS_IDLE_STATE ) // while not in idle
394  {
395  net_isq (); // process net ISQ
396 
397  switch ( ms )
398  {
399  case MS_IDLE_STATE: // exit while loop
400  break;
401 
402  case MS_HELO_STATE: // send HELO message
403  if ( get_reply_code (mail, mail_data, text) == SMTP_SERVICE_READY )
404  {
405  /***********************************************************
406  build HELO message
407  ***********************************************************/
408  strncpy (cmd_string, "HELO ", UINT32_C(5));
409 
410  /***********************************************************
411  append IP own address and trailing CRLF
412  ***********************************************************/
414  strcat (cmd_string, "\r");
415  strcat (cmd_string, "\n");
416 
417  /***********************************************************
418  Send: HELO
419  ***********************************************************/
420  net_send_string (mail, cmd_string);
421 
422  /***********************************************************
423  goto next state
424  ***********************************************************/
425  ms = MS_AUTH_STATE;
426  }
427  else
428  {
429  /***********************************************************
430  exit state machine
431  ***********************************************************/
432  ms = MS_IDLE_STATE;
433  }
434  break;
435 
436  case MS_AUTH_STATE :
437  if ( get_reply_code (mail, mail_data, text) == SMTP_REQUEST_OK )
438  {
439  net_send_string (mail, auth_msg);
440 
441  /***********************************************************
442  goto next state
443  ***********************************************************/
444  ms = MS_USER_STATE;
445  }
446  else
447  {
448  /***********************************************************
449  exit state machine
450  ***********************************************************/
451  ms = MS_IDLE_STATE;
452  }
453  break;
454 
455  case MS_USER_STATE :
456  if ( get_reply_code (mail, mail_data, text) == UINT32_C(334) )
457  {
458  /***********************************************************
459  Send: AUTH
460  ***********************************************************/
461  /***********************************************************
462  walk through source string and encode
463  ***********************************************************/
464  p = (char *)&auth_name[0];
465  t = (char *)&cmd_string[0];
466  while ( (*p & 0xff) != 0)
467  {
468  len = UINT32_C(0);
469  for ( i=0;i<3 ;i++ )
470  {
471  if (*p & 0xff)
472  {
473  in[i] = (*p++) & 0xff;
474  len++;
475  }
476  else
477  {
478  in[i] = 0;
479  }
480  }
481  if (len)
482  {
483  encodeblock( in, out, len );
484  *t++ = out[0] & 0xff;
485  *t++ = out[1] & 0xff;
486  *t++ = out[2] & 0xff;
487  *t++ = out[3] & 0xff;
488  }
489  }
490 
491  /***********************************************************
492  terminate encoded string
493  ***********************************************************/
494  *t++ = '\r';
495  *t++ = '\n';
496  *t++ = 0;
497 
498  net_send_string (mail, cmd_string);
499 
500  /***********************************************************
501  goto next state
502  ***********************************************************/
503  ms = MS_PASS_STATE;
504  }
505  else
506  {
507  /***********************************************************
508  exit state machine
509  ***********************************************************/
510  ms = MS_IDLE_STATE;
511  }
512  break;
513 
514  case MS_PASS_STATE :
515  if ( get_reply_code (mail, mail_data, text) == UINT32_C(334) )
516  {
517  /***********************************************************
518  walk through source string and encode
519  ***********************************************************/
520  p = (char *)&auth_pass[0];
521  t = (char *)&cmd_string[0];
522  while ( (*p & 0xff) != 0)
523  {
524  len = UINT32_C(0);
525  for ( i=0;i<3 ;i++ )
526  {
527  if (*p & 0xff)
528  {
529  in[i] = (*p++) & 0xff;
530  len++;
531  }
532  else
533  {
534  in[i] = 0;
535  }
536  }
537  if (len)
538  {
539  encodeblock( in, out, len );
540  *t++ = out[0] & 0xff;
541  *t++ = out[1] & 0xff;
542  *t++ = out[2] & 0xff;
543  *t++ = out[3] & 0xff;
544  }
545  }
546 
547  /***********************************************************
548  terminate encoded string
549  ***********************************************************/
550  *t++ = '\r';
551  *t++ = '\n';
552  *t++ = 0;
553  net_send_string (mail, cmd_string);
554 
555  /***********************************************************
556  goto next state
557  ***********************************************************/
558  ms = MS_MAIL_FROM_STATE;
559  }
560  else
561  {
562  /***********************************************************
563  exit state machine
564  ***********************************************************/
565  ms = MS_IDLE_STATE;
566  }
567  break;
568 
569  case MS_MAIL_FROM_STATE :
570  if ( get_reply_code (mail, mail_data, text) == SMTP_AUTHENTICATED )
571  {
572  /***********************************************************
573  Send: MAIL FROM
574  ***********************************************************/
575  net_send_string (mail, mail_from);
576 
577  /***********************************************************
578  goto next state
579  ***********************************************************/
580  ms = MS_RCPT_TO_STATE;
581  }
582  else
583  {
584  /***********************************************************
585  exit state machine
586  ***********************************************************/
587  ms = MS_IDLE_STATE;
588  }
589  break;
590 
591  case MS_RCPT_TO_STATE :
592  if ( get_reply_code (mail, mail_data, text) == SMTP_REQUEST_OK )
593  {
594  /***********************************************************
595  Send: RCPT TO
596  ***********************************************************/
597  net_send_string (mail, rcpt_msg);
598 
599  /***********************************************************
600  goto next state
601  ***********************************************************/
602  ms = MS_SEND_DATA_STATE;
603  }
604  else
605  {
606  /***********************************************************
607  exit state machine
608  ***********************************************************/
609  ms = MS_IDLE_STATE;
610  }
611  break;
612 
613  case MS_SEND_DATA_STATE :
614  if ( get_reply_code (mail, mail_data, text) == SMTP_REQUEST_OK )
615  {
616  /***********************************************************
617  Send: DATA
618  ***********************************************************/
619  net_send_string (mail, data_msg );
620 
621  /***********************************************************
622  goto next state
623  ***********************************************************/
624  ms = MS_SEND_MAIL_STATE;
625  }
626  else
627  {
628  /***********************************************************
629  exit state machine
630  ***********************************************************/
631  ms = MS_IDLE_STATE;
632  }
633  break;
634 
635  case MS_SEND_MAIL_STATE :
636  if ( get_reply_code (mail, mail_data, text) == SMTP_START_MAIL )
637  {
638  /***********************************************************
639  Send out the header first
640  ***********************************************************/
641  net_send_string (mail, from_msg);
642  net_send_string (mail, to_msg);
643  net_send_string (mail, sub_msg);
644  net_send_string (mail, cont_msg);
645  net_send_string (mail, prio_msg);
646 
647  /***********************************************************
648  send mail
649  ***********************************************************/
650  net_send_string (mail, msg);
651 
652  /***********************************************************
653  Send: mail-end
654  ***********************************************************/
655  net_send_string (mail, endmail);
656 
657  /***********************************************************
658  goto next state
659  ***********************************************************/
660  ms = MS_ENDL_STATE;
661  }
662  else
663  {
664  /***********************************************************
665  exit state machine
666  ***********************************************************/
667  ms = MS_IDLE_STATE;
668  }
669  break;
670 
671  case MS_ENDL_STATE :
672  if ( get_reply_code (mail, mail_data, text) == SMTP_REQUEST_OK )
673  {
674  /***********************************************************
675  Send: QUIT
676  ***********************************************************/
677  net_send_string (mail, quit_msg);
678  if ( get_reply_code (mail, mail_data, text) == SMTP_SERVICE_CLOSING )
679  {
680  /*******************************************************
681  shutdown connection
682  *******************************************************/
683  shutdown (mail, SHUTDOWN_TIMEOUT);
684  }
685 
686  /***********************************************************
687  exit state machine
688  ***********************************************************/
689  ms = MS_IDLE_STATE;
690 
691  /***********************************************************
692  set return value to success
693  ***********************************************************/
694  f_result = FR_OK;
695  }
696  else
697  {
698  /***********************************************************
699  exit state machine
700  ***********************************************************/
701  ms = MS_IDLE_STATE;
702  }
703  break;
704 
705  } // switch
706  } // while
707 
708  /***************************************************************************
709  free memory; close socket
710  ***************************************************************************/
711  shutdown (mail, SHUTDOWN_TIMEOUT);
712  socket_close (mail);
713  free (mail_data);
714 
715  return (f_result);
716 }
static char cmd_string[120]
Definition: SMTP.c:199
int32_t set_recv_buffer(SOCKET *so, void *data, uint16_t maxdatasize)
Define a socket buffer.
#define SMTPGATEWAY
Definition: SMTP.c:130
#define MAX_DATA
Definition: SMTP.c:124
int32_t shutdown(SOCKET *so, int32_t timeout)
Active shut-down of a TCP connection.
Definition: SMTP.c:150
#define SMTP_CONNECT_TIMEOUT
Definition: BoardSpecific.h:241
static char cont_msg[]
Definition: SMTP.c:207
char * inet_ntoa(uint32_t i_addr, char *s)
Convert IP-address from 0xbbaaddcc to "aaa.bbb.ccc.ddd".
Definition: SMTP.c:142
#define ANY_PORT
Definition: net.h:519
#define SMTP_SERVICE_CLOSING
Definition: SMTP.c:169
#define NULL
Definition: net.h:126
int32_t connect(SOCKET *so, void *data, uint32_t send_max, int32_t timeout)
Actively establish a connection.
Definition: SMTP.c:151
#define UINT32_C(value)
Definition: stdint.h:210
#define SHUTDOWN_TIMEOUT
Definition: BoardSpecific.h:246
Definition: SMTP.c:147
Definition: Common.h:282
static char rcpt_msg[]
Definition: SMTP.c:196
FUNC_RETURN ProcessTimeout(T_TimeOutTimer *tot, time_t timeout)
Definition: timer.c:219
#define SMTP_REQUEST_OK
Definition: SMTP.c:170
#define TCP_INIT_FUNC
Definition: net.h:499
int32_t socket_close(SOCKET *so)
Close a socket and free all associated memory.
static char auth_name[]
Definition: SMTP.c:193
Definition: SMTP.c:146
SOCKET * socket_open(char *dest_addr, uint16_t dest_port, uint16_t src_port, uint8_t data_type, int32_t(*init_func)(SOCKET *))
Create a new socket.
Definition: timer.h:86
Definition: SMTP.c:149
Definition: SMTP.c:143
Definition: SMTP.c:144
static char quit_msg[]
Definition: SMTP.c:197
int32_t net_send_string(SOCKET *so, char *data)
Send a zero terminated string via the specified socket.
Definition: Common.h:283
static char auth_msg[]
Definition: SMTP.c:192
Definition: SMTP.c:148
static char prio_msg[]
Definition: SMTP.c:208
FUNC_RETURN
Definition: Common.h:279
static char data_msg[]
Definition: SMTP.c:191
void encodeblock(u_int8_t *in, u_int8_t *out, int32_t len)
static char from_msg[]
Definition: SMTP.c:204
#define SMTP_SERVICE_READY
Definition: SMTP.c:168
Definition: SMTP.c:145
FUNC_RETURN ResetTimeOutTimer(T_TimeOutTimer *tot, char symbol, time_t period, char *text)
Definition: timer.c:175
uint32_t get_ip_address(uint16_t dev_nr)
Get configured IP address.
static char auth_pass[]
Definition: SMTP.c:194
#define DATATYPE_CHAR
Definition: net.h:489
MAILSTATE_t
Definition: SMTP.c:140
int32_t get_reply_code(SOCKET *mail, char *mail_data, char *text)
Definition: SMTP.c:248
void net_isq(void)
The main polling function for processing sockets, must be periodically called in the main application...
int int32_t
Definition: stdint.h:46
#define INVALID_SOCKET
Definition: net.h:540
static char endmail[]
Definition: SMTP.c:209
Definition: Common.h:280
static char to_msg[]
Definition: SMTP.c:205
static char mail_from[]
Definition: SMTP.c:195
#define SMTP_AUTHENTICATED
Definition: SMTP.c:171
#define SMTP_START_MAIL
Definition: SMTP.c:172
static char sub_msg[]
Definition: SMTP.c:206
int wait_for_link ( void  )
Examples:
SMTP.c.
727 {
728  /***************************************************************************
729  locals
730  ***************************************************************************/
731  /* - */
732 
733  /***************************************************************
734  perform progress bar animation each 200 milli second
735  ***************************************************************/
736  CPrintProgressBarText ('.', 200000, 16, " waiting for link ");
737 
738  /***************************************************************
739  monitor link status
740  ***************************************************************/
742 
743  /***************************************************************
744  try to detect IP assignment
745  if DHCP is used, the assigned IP address may change
746  ***************************************************************/
748 
749  /***************************************************************
750  in case of a valid link and IP address advance to next state
751  ***************************************************************/
752  if ((_link != 0) && (get_ip_address (0) != UINT32_C(0)))
753  {
755  return (1);
756  }
757  return (0);
758 }
volatile uint16_t _link
Definition: BoardSpecific.c:143
#define UINT32_C(value)
Definition: stdint.h:210
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
uint32_t get_ip_address(uint16_t dev_nr)
Get configured IP address.
uint16_t monitor_ip_address(tpOutputFunc pLog)
monitor IP assignment
Definition: BoardSpecific.c:545
int32_t CPrintProgressBarText(char c, time_t period, size_t length, char *text)
Definition: cprintf.c:902
uint16_t monitor_link_status(tpOutputFunc pLog)
monitor link status change
Definition: BoardSpecific.c:497
#define CPrintfProgressSuccess()
Definition: cprintf.h:263
int main ( void  )
Examples:
SMTP.c.
769 {
770  /***************************************************************************
771  locals
772  ***************************************************************************/
773  ML_t ml = ML_WAIT_LINK_STATE; /* main loop switch */
774  timeval stamp1, stamp2, delta; /* used to determine startup time */
775  int i=0; /* loop counter */
776  timeval CurrentTime; /* time stamp */
777  struct tm *local_time; /* structure to hold time and date */
778  FUNC_RETURN f_result; /* function result */
779  char *message; /* email message */
780 
781  /***************************************************************************
782  initialize application (e.g. timer clocks, PLL settings, EMIF etc.)
783  (ref. \Common\Common.c)
784  ***************************************************************************/
785  AppInit (GET_CLOCK);
786 
787  /***************************************************************************
788  select output device for CPrintf (ref. \Common\cprintf.c)
789  possible settings:
790  CPRINTF_UART_OUTPUT -> output to UART
791  CPRINTF_CCS_OUTPUT -> output to CCS
792  CPRINTF_UART_OUTPUT | CPRINTF_CCS_OUTPUT -> output to UART and CCS
793  ***************************************************************************/
794  CPrintf_select_output (CPRINTF_DEFAULT_OUTPUT); /* default outputs */
795 
796  /***************************************************************************
797  print a start up message
798  ***************************************************************************/
799  START_UP_MESSAGE (BLANK_REV NETLIB_REV);
800 
801  /**************************************************************************/
802  // CPrintfProgress (" Heap check ");
803  // at least 0x2000 bytes required for this app
804  /**************************************************************************/
805  ASSERT_HEAP (initial_heap_size, 0x2000);
806  // CPrintfProgressSuccess();
807 
808  /**************************************************************************/
809  CPrintfProgress (" Setup system time ");
810  // 10 milli seconds resolution
811  /**************************************************************************/
814  CPrintf (" *** timer %d mapped to CPU int %d ***\r\n",
816 
817  /**************************************************************************/
818  CPrintfProgress (" Enable interrupts ");
819  /**************************************************************************/
822 
823  /**************************************************************************/
824  CPrintfProgress (" Start system timer ");
825  /**************************************************************************/
826  StartSystemTimer ();
828  CPrintf (" *** timer %d running at %"PRId32" Hz ***\r\n", SystemTimerDev, RES_SECONDS/GetSystemTimerRes());
829 
830  /***************************************************************************
831  malloc space for data
832  ***************************************************************************/
833  CPrintfProgress (" Try to allocate data buffer ");
834  message = (char *) malloc (TCP_MAX_PACKET_SIZE * sizeof(char));
835  if ( message == NULL )
836  {
837  prg_exit ("out of memory error");
838  }
840 
841  /***************************************************************************
842  measure network initialization time
843  ***************************************************************************/
844  stamp1 = GetTimeStamp();
845 
846  /**************************************************************************/
847  CPrintfProgress (" Initialize network ");
848  /**************************************************************************/
849  InitializeNetwork ( 64); // 64 bytes for ping
851 
852  stamp2 = GetTimeStamp();
853 
854  tv_interval (&delta, &stamp1, &stamp2);
855  CPuts (" network startup time [sec]: ");
857  "%"PRId32".%03"PRId32"\r\n"
859  delta.tv_sec,
860  delta.tv_usec/1000);
861 
862  local_time = localtime ((const time_t *)&RecentEpoch);
863  CPrintf (UTC_PRINTF, asctime (local_time));
864 
865  while (ml)
866  {
867  net_isq ();
868 
869  switch (ml)
870  {
871  case ML_STOP_STATE:
872  /***************************************************************
873  stop program
874  ***************************************************************/
875  break;
876 
877  case ML_WAIT_LINK_STATE:
878  if ( wait_for_link ())
879  {
880  /*******************************************************
881  if link and IP are valid, go to next state
882  *******************************************************/
883  ml = ML_SEND_MAIL_STATE;
884  }
885  break;
886 
887  case ML_SEND_MAIL_STATE:
888  /***************************************************************
889  build email message
890  ***************************************************************/
891  local_time = localtime ((const time_t *)&RecentEpoch);
892  sprintf ( message, eMessages[i], module_str, asctime (local_time));
893 
894  /***************************************************************
895  send email
896  ***************************************************************/
897  f_result = send_mail (MailCycleMessage[i], message);
898 
899  /***************************************************************
900  print result
901  ***************************************************************/
902  CPuts (CLRLINE);
903  if (f_result == FR_OK)
904  {
906  CPrintf(FRMessages[f_result]);
907  ml = ML_IDLE_STATE;
908  }
909  else
910  {
911  CPrintfProgress (" connecting mail server ");
912  CPrintf(FRMessages[f_result]);
913  ml = ML_STOP_STATE;
914  }
915 
916  /***************************************************************
917  take time
918  ***************************************************************/
919  CurrentTime = GetTimeStamp();
920  break;
921 
922  case ML_IDLE_STATE:
923  if (i == 2)
924  {
925  i++;
926  CPuts (CLRLINE);
928  }
929  if (i < 2)
930  {
931  /***********************************************************
932  restart send state after 5 seconds
933  ***********************************************************/
934  if ( tv_elapsed (&CurrentTime, UINT32_C(5), UINT32_C(0), TRUE) )
935  {
936  ml = ML_SEND_MAIL_STATE;
937  i++;
938  }
939  }
940 
941  /***************************************************************
942  show that the program is running, perform symbol animation
943  ***************************************************************/
945  break;
946  }
947  }
948 
949  /***************************************************************************
950  exit program, shut down peripherals
951  ***************************************************************************/
952  free (message);
953  return (0);
954 }
#define prg_exit(s)
Definition: Common.h:267
void BoardEnableInterrupts(void)
global enable interrupts
Definition: BoardSpecific.c:365
int wait_for_link(void)
Definition: SMTP.c:726
#define ANIMATE_SYMBOLS_COUNT
Definition: BoardSpecific.h:268
#define CLRLINE
Definition: cprintf.h:171
void StartSystemTimer(void)
start system timer
Definition: timer.c:447
uint16_t CPrintf_select_output(uint16_t device)
Definition: cprintf.c:206
#define ASSERT_HEAP(i, h)
Definition: Common.h:262
#define RES_10MSECONDS
Definition: timer.h:68
#define NULL
Definition: net.h:126
time_t GetSystemTimerRes(void)
Definition: timer.c:160
char * MailCycleMessage[]
Definition: SMTP.c:221
#define TCP_MAX_PACKET_SIZE
Definition: net.h:681
#define UINT32_C(value)
Definition: stdint.h:210
#define GET_CLOCK
Definition: BoardSpecific.h:258
int tv_elapsed(timeval *t, time_t s, time_t u, uint16_t retrigger)
check if given time has elapsed
Definition: timer.c:286
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
#define VT100_DEFAULT
Definition: cprintf.h:150
void SetupSystemTime(int32_t cpuint, int port, time_t resolution)
Setup System Time.
Definition: timer.c:392
void AppInit(uint32_t dsp_clock)
Initialize application.
Definition: Common.c:230
uint32_t initial_heap_size
Definition: Common.c:140
timeval RecentEpoch
Definition: timer.c:81
#define UTC_PRINTF
Definition: SMTP.c:174
#define CPRINTF_DEFAULT_OUTPUT
Definition: BoardSpecific.h:129
#define VT100_RED
Definition: cprintf.h:143
uint16_t CPrintAnimatedSymbol(char *c, time_t period, size_t size)
Definition: cprintf.c:816
char module_str[]
Definition: BoardSpecific.c:128
int InitializeNetwork(uint16_t icmp_size)
Initialize MAC, sockets and protocols.
Definition: BoardSpecific.c:597
const char * eMessages[]
Definition: SMTP.c:214
char sym_animate[]
Definition: BoardSpecific.c:129
Definition: SMTP.c:161
Definition: timer.h:75
#define GetTimeStamp()
Definition: timer.h:81
void tv_interval(timeval *e, timeval *t1, timeval *t2)
compute elapsed time
Definition: timer.c:336
FUNC_RETURN
Definition: Common.h:279
#define SYSTEM_TIMER_INT
Definition: BoardSpecific.h:174
#define SYSTEM_TIMER
Definition: BoardSpecific.h:167
Definition: SMTP.c:159
void net_isq(void)
The main polling function for processing sockets, must be periodically called in the main application...
#define BLANK_REV
Definition: BoardSpecific.h:191
Uint16 SystemTimerDev
Definition: SMTP.c:160
Definition: Common.h:280
FUNC_RETURN send_mail(char *text, char *msg)
Definition: SMTP.c:318
Definition: SMTP.c:158
#define START_UP_MESSAGE(rev)
Definition: BoardSpecific.h:192
const char * FRMessages[]
Definition: Common.c:91
ML_t
Definition: SMTP.c:157
#define RES_SECONDS
Definition: timer.h:70
int CPuts(const char *_ptr)
Definition: cprintf.c:399
time_t tv_sec
Definition: timer.h:77
#define CPrintfProgress(s)
Definition: cprintf.h:230
time_t tv_usec
Definition: timer.h:78
#define CPrintfProgressSuccess()
Definition: cprintf.h:263

Variable Documentation

char* program_name = "SMTP"
Examples:
SMTP.c.
char data_msg[] = {"DATA\r\n"}
static
Examples:
SMTP.c.
char auth_msg[] = {"AUTH LOGIN\r\n"}
static
Examples:
SMTP.c.
char auth_name[] = {"dsp@dsignt.de"}
static
Examples:
SMTP.c.
char auth_pass[] = SOURCE_PASS
static
Examples:
SMTP.c.
char mail_from[] = {"MAIL FROM: "SOURCE_EMAIL_ADDRESS"\r\n"}
static
Examples:
SMTP.c.
char rcpt_msg[] = {"RCPT TO: "DEST_EMAIL_ADDRESS"\r\n"}
static
Examples:
SMTP.c.
char quit_msg[] = {"QUIT\r\n"}
static
Examples:
SMTP.c.
char cmd_string[120] = {0}
static
Examples:
SMTP.c.
char from_msg[] = {"From: "SOURCE_EMAIL_ADDRESS"\r\n"}
static
Examples:
SMTP.c.
char to_msg[] = {"To: "DEST_EMAIL_ADDRESS"\r\n"}
static
Examples:
SMTP.c.
char sub_msg[] = {"Subject: Test mail from DSP\r\n"}
static
Examples:
SMTP.c.
char cont_msg[] = {"Content-Type: text/plain\r\n"}
static
Examples:
SMTP.c.
char prio_msg[] = {"Priority: Urgent\r\n\r\n"}
static
Examples:
SMTP.c.
char endmail[] = {"\r\n.\r\n"}
static
Examples:
SMTP.c.
const char* eMessages[]
Initial value:
= {
"%s started work at %s\r\n",
"%s is alive at %s\r\n" ,
"%s stopped work at %s\r\n",
""
}
Examples:
SMTP.c.
char* MailCycleMessage[]
Initial value:
= {
" sending start mail ",
" sending alive mail ",
" sending stop mail ",
" SMTP test finished successfully,\r\n entering idle network processing\r\n"
}
Examples:
SMTP.c.