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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief Basic SMTP Client for AVR32 UC3.
*
* - Compiler: GNU GCC for AVR32
* - Supported devices: All AVR32 devices can be used.
* - AppNote:
*
* \author Atmel Corporation: http://www.atmel.com \n
* Support and FAQ: http://support.atmel.no/
*
*****************************************************************************/
/* Copyright (c) 2007, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Implements a simplistic SMTP client. First time the task is started, connection is made and
email is sent. Mail flag is then reset. Each time you press the Push Button 0, a new mail will be sent.
*/
#if (SMTP_USED == 1)
#include <string.h>
// Scheduler includes.
#include "FreeRTOS.h"
#include "task.h"
#include "BasicSMTP.h"
// Demo includes.
#include "portmacro.h"
#include "partest.h"
#include "intc.h"
#include "gpio.h"
// lwIP includes.
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "lwip/opt.h"
#include "lwip/api.h"
#include "lwip/arch.h"
#include "lwip/sys.h"
#include "lwip/sockets.h"
#include "netif/loopif.h"
//! SMTP default port
#define SMTP_PORT 25
//! SMTP EHLO code answer
#define SMTP_EHLO_STRING "220"
//! SMTP end of transmission code answer
#define SMTP_END_OF_TRANSMISSION_STRING "221"
//! SMTP OK code answer
#define SMTP_OK_STRING "250"
//! SMTP start of transmission code answer
#define SMTP_START_OF_TRANSMISSION_STRING "354"
//! SMTP DATA<CRLF>
#define SMTP_DATA_STRING "DATA\r\n"
//! SMTP <CRLF>.<CRLF>
#define SMTP_MAIL_END_STRING "\r\n.\r\n"
//! SMTP QUIT<CRLFCRLF>
#define SMTP_QUIT_STRING "QUIT\r\n"
//! Server address
#error configure SMTP server address
portCHAR cServer[] = "192.168.0.1";
//! Fill here the mailfrom with your mail address
#error configure SMTP mail sender
char cMailfrom[] = "MAIL FROM: <sender@domain.com>\r\n";
//! Fill here the mailto with your contact mail address
#error configure SMTP mail recipient
char cMailto[] = "RCPT TO: <recipient@domain.com>\r\n";
//! Fill here the mailcontent with the mail you want to send
#error configure SMTP mail content
char cMailcontent[] ="Subject: *** SPAM ***\r\nFROM: \"Your Name here\" <sender@domain.com>\r\nTO: \"Your Contact here\" <recipient@domain.com>\r\n\r\nSay what you want here.";
//! flag to send mail
Bool bSendMail = pdFALSE;
//! buffer for SMTP response
portCHAR cTempBuffer[200];
//_____ D E C L A R A T I O N S ____________________________________________
//! interrupt handler.
#if __GNUC__
__attribute__((naked))
#elif __ICCAVR32__
#pragma shadow_registers = full // Naked.
#endif
void vpushb_ISR( void );
//! soft interrupt handler. where treatment should be done
#if __GNUC__
__attribute__((__noinline__))
#endif
static portBASE_TYPE prvpushb_ISR_NonNakedBehaviour( void );
//! Basic SMTP client task definition
portTASK_FUNCTION( vBasicSMTPClient, pvParameters )
{
struct sockaddr_in stServeurSockAddr;
portLONG lRetval;
portLONG lSocket = -1;
// configure push button 0 to produce IT on falling edge
gpio_enable_pin_interrupt(GPIO_PUSH_BUTTON_0 , GPIO_FALLING_EDGE);
// Disable all interrupts
vPortEnterCritical();
// register push button 0 handler on level 3
INTC_register_interrupt( (__int_handler)&vpushb_ISR, AVR32_GPIO_IRQ_0 + (GPIO_PUSH_BUTTON_0/8), INT3);
// Enable all interrupts
vPortExitCritical();
for (;;)
{
// wait for a signal to send a mail
while (bSendMail != pdTRUE) vTaskDelay(200);
// Disable all interrupts
vPortEnterCritical();
// clear the flag
bSendMail = pdFALSE;
// Enable all interrupts
vPortExitCritical();
// clear the LED
vParTestSetLED( 3 , pdFALSE );
// Set up port
memset(&stServeurSockAddr, 0, sizeof(stServeurSockAddr));
stServeurSockAddr.sin_len = sizeof(stServeurSockAddr);
stServeurSockAddr.sin_addr.s_addr = inet_addr(cServer);
stServeurSockAddr.sin_port = htons(SMTP_PORT);
stServeurSockAddr.sin_family = AF_INET;
// socket as a stream
if ( (lSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
// socket failed, blink a LED and stay here
for (;;) {
vParTestToggleLED( 4 );
vTaskDelay( 200 );
}
}
// connect to the server
if(connect(lSocket,(struct sockaddr *)&stServeurSockAddr, sizeof(stServeurSockAddr)) < 0)
{
// connect failed, blink a LED and stay here
for (;;) {
vParTestToggleLED( 6 );
vTaskDelay( 200 );
}
}
else
{
//Server: 220 SMTP Ready
// wait for SMTP Server answer
do
{
lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
}while (lRetval <= 0);
if (strncmp(cTempBuffer, SMTP_EHLO_STRING, sizeof(cTempBuffer)) >= 0)
{
//Client: EHLO smtp.domain.com
// send ehlo
send(lSocket, "HELO ", 5, 0);
send(lSocket, cServer, strlen(cServer), 0);
send(lSocket, "\r\n", 2, 0);
//Server: 250
// wait for SMTP Server answer
do
{
lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
}while (lRetval <= 0);
if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0)
{
//Client: MAIL FROM:<sender@domain.com>
// send MAIL FROM
send(lSocket, cMailfrom, strlen(cMailfrom), 0);
//Server: 250 OK
// wait for SMTP Server answer
do
{
lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
}while (lRetval <= 0);
if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0)
{
//Client: RCPT TO:<receiver@domain.com>
// send RCPT TO
send(lSocket, cMailto, strlen(cMailto), 0);
//Server: 250 OK
// wait for SMTP Server answer
do
{
lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
}while (lRetval <= 0);
if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0)
{
//Client: DATA<CRLF>
// send DATA
send(lSocket, SMTP_DATA_STRING, 6, 0);
//Server: 354 Start mail input; end with <CRLF>.<CRLF>
// wait for SMTP Server answer
do
{
lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
}while (lRetval <= 0);
if (strncmp(cTempBuffer, SMTP_START_OF_TRANSMISSION_STRING, sizeof(cTempBuffer)) >= 0)
{
// send content
send(lSocket, cMailcontent, strlen(cMailcontent), 0);
//Client: <CRLF>.<CRLF>
// send "<CRLF>.<CRLF>"
send(lSocket, SMTP_MAIL_END_STRING, 5, 0);
//Server: 250 OK
// wait for SMTP Server answer
do
{
lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
}while (lRetval <= 0);
if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0)
{
//Client: QUIT<CRLFCRLF>
// send QUIT
send(lSocket, SMTP_QUIT_STRING, 8, 0);
//Server: 221 smtp.domain.com closing transmission
do
{
lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
}while (lRetval <= 0);
if (strncmp(cTempBuffer, SMTP_END_OF_TRANSMISSION_STRING, sizeof(cTempBuffer)) >= 0)
{
vParTestSetLED( 3 , pdTRUE );
}
}
}
}
}
}
// close socket
close(lSocket);
}
}
}
}
/*! \brief push button naked interrupt handler.
*
*/
#if __GNUC__
__attribute__((naked))
#elif __ICCAVR32__
#pragma shadow_registers = full // Naked.
#endif
void vpushb_ISR( void )
{
/* This ISR can cause a context switch, so the first statement must be a
call to the portENTER_SWITCHING_ISR() macro. This must be BEFORE any
variable declarations. */
portENTER_SWITCHING_ISR();
prvpushb_ISR_NonNakedBehaviour();
portEXIT_SWITCHING_ISR();
}
/*! \brief push button interrupt handler. Here, declarations should be done
*
*/
#if __GNUC__
__attribute__((__noinline__))
#elif __ICCAVR32__
#pragma optimize = no_inline
#endif
static portBASE_TYPE prvpushb_ISR_NonNakedBehaviour( void )
{
if (gpio_get_pin_interrupt_flag(GPIO_PUSH_BUTTON_0))
{
// set the flag
bSendMail = pdTRUE;
// allow new interrupt : clear the IFR flag
gpio_clear_pin_interrupt_flag(GPIO_PUSH_BUTTON_0);
}
// no context switch required, task is polling the flag
return( pdFALSE );
}
#endif
|