forked from eclipse-threadx/netxduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_netxduo_smtp_client.c
212 lines (155 loc) · 7.1 KB
/
demo_netxduo_smtp_client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
This is a small demo of the NetX SMTP Client on the high-performance NetX TCP/IP stack.
This demo relies on Thread, NetX and SMTP Client API to perform simple SMTP mail
transfers in an SMTP client application to an SMTP mail server.
*/
/* Note: This demo works for IPv4 only. */
#include "nx_api.h"
#include "nx_ip.h"
#include "nxd_smtp_client.h"
#ifndef NX_DISABLE_IPV4
/* Define the host user name and mail box parameters */
#define USERNAME "myusername"
#define PASSWORD "mypassword"
#define FROM_ADDRESS "[email protected]"
#define RECIPIENT_ADDRESS "[email protected]"
#define LOCAL_DOMAIN "mycompany.com"
#define SUBJECT_LINE "NetX SMTP Client Demo"
#define MAIL_BODY "NetX SMTP client is a simple SMTP client implementation \r\n" \
"that allow embedded devices to send email to an SMTP server. \r\n" \
"This feature is intended to allow a device to send simple status\r\n " \
"reports using the most universal Internet application, email.\r\n"
/* See the NetX SMTP Client User Guide for how to set the authentication type.
The most common authentication type is PLAIN. */
#define CLIENT_AUTHENTICATION_TYPE NX_SMTP_CLIENT_AUTH_PLAIN
#define CLIENT_IP_ADDRESS IP_ADDRESS(1,2,3,5)
#define SERVER_IP_ADDRESS IP_ADDRESS(1,2,3,4)
#define SERVER_PORT 25
/* Define the NetX and ThreadX structures for the SMTP client appliciation. */
NX_PACKET_POOL ip_packet_pool;
NX_PACKET_POOL client_packet_pool;
NX_IP client_ip;
TX_THREAD demo_client_thread;
static NX_SMTP_CLIENT demo_client;
void _nx_ram_network_driver(struct NX_IP_DRIVER_STRUCT *driver_req);
void demo_client_thread_entry(ULONG info);
/* Define main entry point. */
int main()
{
/* Enter the ThreadX kernel. */
tx_kernel_enter();
}
/* Define what the initial system looks like. */
void tx_application_define(void *first_unused_memory)
{
UINT status;
CHAR *free_memory_pointer;
/* Setup the pointer to unallocated memory. */
free_memory_pointer = (CHAR *) first_unused_memory;
/* Create IP default packet pool. This packets do not need a very large payload. */
status = nx_packet_pool_create(&ip_packet_pool, "Default IP Packet Pool",
512, free_memory_pointer, 2048);
/* Update pointer to unallocated (free) memory. */
free_memory_pointer = free_memory_pointer + 2048;
/* Create SMTP Client packet pool. This is only for transmitting packets to the server.
It need not be a separate packet pool than the IP default packet pool but for more efficient
resource use, we use two different packet pools because the CLient SMTP messages
generally require more payload than IP control packets.
Packet payload depends on the SMTP Client application requirements. Size of packet payload
must include IP and TCP headers. For IPv6 connections, IP and TCP header data is 60 bytes. For IPv4
IP and TCP header data is 40 bytes (not including TCP options).
*/
status |= nx_packet_pool_create(&client_packet_pool, "SMTP Client Packet Pool",
800, free_memory_pointer, (10*800));
if (status != NX_SUCCESS)
{
return;
}
/* Update pointer to unallocated (free) memory. */
free_memory_pointer = free_memory_pointer + (10*800);
/* Initialize the NetX system. */
nx_system_initialize();
/* Create the client thread */
status = tx_thread_create(&demo_client_thread, "client_thread",
demo_client_thread_entry, 0, free_memory_pointer,
2048, 16, 16,
TX_NO_TIME_SLICE, TX_DONT_START);
if (status != NX_SUCCESS)
{
printf("Error creating Client thread. Status 0x%x\r\n", status);
return;
}
/* Update pointer to unallocated (free) memory. */
free_memory_pointer = free_memory_pointer + 4096;
/* Create Client IP instance. Remember to replace the generic driver
with a real ethernet driver to actually run this demo! */
status = nx_ip_create(&client_ip, "SMTP Client IP Instance", CLIENT_IP_ADDRESS, 0xFFFFFF00UL,
&ip_packet_pool, _nx_ram_network_driver, free_memory_pointer,
2048, 1);
free_memory_pointer = free_memory_pointer + 2048;
/* Enable ARP and supply ARP cache memory. */
status = nx_arp_enable(&client_ip, (void **) free_memory_pointer, 1040);
/* Update pointer to unallocated (free) memory. */
free_memory_pointer = free_memory_pointer + 1040;
/* Enable TCP for client. */
status = nx_tcp_enable(&client_ip);
if (status != NX_SUCCESS)
{
return;
}
/* Enable ICMP for client. */
status = nx_icmp_enable(&client_ip);
if (status != NX_SUCCESS)
{
return;
}
/* Start the client thread. */
tx_thread_resume(&demo_client_thread);
return;
}
/* Define the smtp application thread task. */
void demo_client_thread_entry(ULONG info)
{
UINT status;
UINT error_counter = 0;
NXD_ADDRESS server_ip_address;
NX_PARAMETER_NOT_USED(info);
tx_thread_sleep(NX_IP_PERIODIC_RATE);
/* Set up the server IP address. */
server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
server_ip_address.nxd_ip_address.v4 = SERVER_IP_ADDRESS;
/* The demo client username and password is the authentication
data used when the server attempts to authentication the client. */
status = nxd_smtp_client_create(&demo_client, &client_ip, &client_packet_pool,
USERNAME,
PASSWORD,
FROM_ADDRESS,
LOCAL_DOMAIN, CLIENT_AUTHENTICATION_TYPE,
&server_ip_address, SERVER_PORT);
if (status != NX_SUCCESS)
{
printf("Error creating the client. Status: 0x%x.\n\r", status);
return;
}
/* Create a mail instance with the above text message and recipient info. */
status = nx_smtp_mail_send(&demo_client, RECIPIENT_ADDRESS, NX_SMTP_MAIL_PRIORITY_NORMAL,
SUBJECT_LINE, MAIL_BODY, sizeof(MAIL_BODY) - 1);
/* Check for errors. */
if (status != NX_SUCCESS)
{
/* Mail item was not sent. Note that we need not delete the client. The error status may be a failed
authentication check or a broken connection. We can simply call nx_smtp_mail_send
again. */
error_counter++;
}
/* Release resources used by client. Note that the transmit packet
pool must be deleted by the application if it no longer has use for it.*/
status = nx_smtp_client_delete(&demo_client);
/* Check for errors. */
if (status != NX_SUCCESS)
{
error_counter++;
}
return;
}
#endif /* NX_DISABLE_IPV4 */