This performs an ethernet sniff test. Copied from an existing ethernet sniff test and updated to work properly with the LISA, supporting output of content to the USB console. See also: http://mbed.org/users/rolf/code/ethsnif/
Fork of ethsnif by
main.cpp@1:a7a52d202638, 2013-12-14 (annotated)
- Committer:
- dixter1
- Date:
- Sat Dec 14 00:53:08 2013 +0000
- Revision:
- 1:a7a52d202638
- Parent:
- 0:29e2df9de9f1
First Version C027 Ethernet Sniff Test to Validate Ethernet Capability.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rolf | 0:29e2df9de9f1 | 1 | #include "mbed.h" |
dixter1 | 1:a7a52d202638 | 2 | #include "C027.h" |
rolf | 0:29e2df9de9f1 | 3 | #include "Ethernet.h" |
rolf | 0:29e2df9de9f1 | 4 | #include "hexview.h" |
rolf | 0:29e2df9de9f1 | 5 | |
rolf | 0:29e2df9de9f1 | 6 | using namespace mbed; |
rolf | 0:29e2df9de9f1 | 7 | |
dixter1 | 1:a7a52d202638 | 8 | DigitalOut led4(LED); |
rolf | 0:29e2df9de9f1 | 9 | |
rolf | 0:29e2df9de9f1 | 10 | Ethernet eth; |
dixter1 | 1:a7a52d202638 | 11 | Serial pc(USBTX, USBRX); |
rolf | 0:29e2df9de9f1 | 12 | |
rolf | 0:29e2df9de9f1 | 13 | unsigned short htons(unsigned short n) { // Host short to network shor |
rolf | 0:29e2df9de9f1 | 14 | return ((n & 0xff) << 8) | ((n & 0xff00) >> 8); // Byte swapping |
rolf | 0:29e2df9de9f1 | 15 | } |
rolf | 0:29e2df9de9f1 | 16 | |
rolf | 0:29e2df9de9f1 | 17 | void show(char *buf, int size) { |
dixter1 | 1:a7a52d202638 | 18 | pc.printf("Destination: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n\r", |
rolf | 0:29e2df9de9f1 | 19 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); |
dixter1 | 1:a7a52d202638 | 20 | pc.printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n\r", |
rolf | 0:29e2df9de9f1 | 21 | buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]); |
rolf | 0:29e2df9de9f1 | 22 | |
dixter1 | 1:a7a52d202638 | 23 | pc.printf("Type %hd\n\r", htons((short)buf[12])); |
rolf | 0:29e2df9de9f1 | 24 | |
rolf | 0:29e2df9de9f1 | 25 | hexview(buf, size); |
rolf | 0:29e2df9de9f1 | 26 | } |
rolf | 0:29e2df9de9f1 | 27 | |
rolf | 0:29e2df9de9f1 | 28 | |
rolf | 0:29e2df9de9f1 | 29 | int main() { |
rolf | 0:29e2df9de9f1 | 30 | char buffer[0x600]; |
rolf | 0:29e2df9de9f1 | 31 | int size = 0; |
dixter1 | 1:a7a52d202638 | 32 | int led_toggle_count = 5; |
dixter1 | 1:a7a52d202638 | 33 | |
dixter1 | 1:a7a52d202638 | 34 | #if 0 |
dixter1 | 1:a7a52d202638 | 35 | while(1) { |
dixter1 | 1:a7a52d202638 | 36 | led4 = !led4; |
dixter1 | 1:a7a52d202638 | 37 | wait(0.2); |
dixter1 | 1:a7a52d202638 | 38 | } |
dixter1 | 1:a7a52d202638 | 39 | #else |
dixter1 | 1:a7a52d202638 | 40 | while( led_toggle_count-- > 0 ) |
dixter1 | 1:a7a52d202638 | 41 | { |
dixter1 | 1:a7a52d202638 | 42 | led4 = !led4; |
dixter1 | 1:a7a52d202638 | 43 | wait(0.2); |
dixter1 | 1:a7a52d202638 | 44 | } |
dixter1 | 1:a7a52d202638 | 45 | #endif |
dixter1 | 1:a7a52d202638 | 46 | |
dixter1 | 1:a7a52d202638 | 47 | // open the PC serial port and (use the same baudrate) |
dixter1 | 1:a7a52d202638 | 48 | pc.baud(MDMBAUD); |
dixter1 | 1:a7a52d202638 | 49 | |
dixter1 | 1:a7a52d202638 | 50 | pc.printf( "Start ETHERNET Sniff Testing\n\r"); |
rolf | 0:29e2df9de9f1 | 51 | |
rolf | 0:29e2df9de9f1 | 52 | while(1) { |
rolf | 0:29e2df9de9f1 | 53 | if((size = eth.receive()) != 0) { |
dixter1 | 1:a7a52d202638 | 54 | led4=!led4; |
rolf | 0:29e2df9de9f1 | 55 | eth.read(buffer, size); |
rolf | 0:29e2df9de9f1 | 56 | show(buffer, size); |
rolf | 0:29e2df9de9f1 | 57 | } |
rolf | 0:29e2df9de9f1 | 58 | } |
rolf | 0:29e2df9de9f1 | 59 | } |