Summary |
#include <net_config.h>
U16 smtp_cbfunc (
U8 code, /* Type of data requested by the SMTP Client. */
U8* buf, /* Location where to write the requested data. */
U16 buflen, /* Number of bytes in the output buffer. */
U32* pvar ); /* Pointer to a storage variable. */
|
Description |
The smtp_cbfunc function provides the email headers and
data, in SMTP format, when requested by the SMTP client running on
the TCPnet system. The SMTP client calls this function several times
to compose the email message.
The argument code specifies the type of email section
(user, header or data) that the SMTP Client requires. This is shown
in the table.
Code |
Email section |
Description |
0 |
Username: |
Username for SMTP authentication |
1 |
Password: |
Password for SMTP authentication |
2 |
From: |
Email address of the sender |
3 |
To: |
Email address of the recipient |
4 |
Subject: |
Subject of the email |
5 |
Data: |
Email body in plain ASCII format |
The argument buf is a pointer to the output buffer where
the smtp_cbfunc function writes the requested data into. The
argument buflen specifies the length of the output buffer in
bytes.
The argument pvar is a pointer to a variable that never
gets altered by the SMTP client. You can use *pvar as a repeat
counter or simply to distinguish between different calls of the
smtp_cbfunc function.
The smtp_cbfunc function is part of RL-TCPnet. The
prototype is defined in net_config.h. You must customize the function
in smtp_uif.c to compose the email message correctly.
note
-
The length of the output buffer, buflen might vary
because buffer length is determined by the TCP socket Maximum
Segment Size (MSS) negotiation. The buffer length is normally
around 1400 bytes for local LAN. But this can be reduced to 500
bytes or even less.
-
If the smtp_cbfunc function writes more bytes than
buflen into the output buffer, then a system crash resulting from
corruption of memory link pointers is highly likely.
-
The argument pcgi is private to each SMTP Session. The
SMTP Client clears the data in the pcgi pointer, to 0,
before the smtp_cbfunc function is called for the first
time.
|
Return Value |
The smtp_cbfunc function returns the number of bytes
written to the output buffer, and it writes the repeat flag value in
the most significant bit of the return value.
If the return value's most significant bit is set to 1, the SMTP
client running on TCPnet calls the smtp_cbfunc function again
with the same value for the argument code, and pcgi,
which holds the same content as previously set. The function
smtp_cbfunc can then enter more data into the buffer
buf.
|
Example |
typedef struct {
U8 id;
U16 idx;
} MY_BUF;
#define MYBUF(p) ((MY_BUF *)p)
U16 smtp_cbfunc (U8 code, U8 *buf, U16 buflen, U32 *pvar) {
U32 i,len = 0;
switch (code) {
case 0:
/* Enter Username for SMTP Server authentication. */
len = str_copy (buf, "user");
break;
case 1:
/* Enter Password for SMTP Server authentication. */
len = str_copy (buf, "password");
break;
case 2:
/* Enter email address of the sender. */
len = str_copy (buf, "mcb@keil.com");
break;
case 3:
/* Enter email address of the recipient. */
len = str_copy (buf, "somebody@keil.com");
break;
case 4:
/* Enter email subject. */
len = str_copy (buf, "Reported measurements");
break;
case 5:
/* Enter email data. */
switch (MYBUF(pvar)->id) {
case 0:
/* First call, enter an email header text. */
len = str_copy (buf, "Here is the log file:\r\n\r\n");
MYBUF(pvar)->id = 1;
MYBUF(pvar)->idx = 1;
goto rep;
case 1:
/* Add email message body. */
for (len = 0; len < buflen-150; ) {
/* Let's use as much of the buffer as possible. */
/* This will produce less packets and speedup the transfer. */
len += sprintf ((char *)(buf+len), "%d. ",MYBUF(pvar)->idx);
for (i = 0; i < 8; i++) {
len += sprintf ((char *)(buf+len), "AD%d= %d ",i,AD_in(i));
}
len += str_copy (buf+len, "\r\n");
if (++MYBUF(pvar)->idx > 500) {
MYBUF(pvar)->id = 2;
break;
}
}
/* Request a repeated call, bit 15 is a repeat flag. */
rep: len |= 0x8000;
break;
case 2:
/* Last one, add a footer text to this email. */
len = str_copy (buf, "OK, that is all. \r\nBye..\r\n");
break;
}
}
return ((U16)len);
}
|