Search Code
About pktwatcher

Published 14 Aug 2010.

Last change message: N/A

Import this program

pktwatcher

Published 14 Aug 2010, by   user takumi funada   tag No tags
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // mbed packet watcher by musashinodenpa
00002 //
00003 
00004 #include "mbed.h"
00005 #include "HTTPClient.h"
00006 
00007 void analyse(char *buf, int size);
00008 int search(char* hay, int size, char* needle);
00009 unsigned long getTime();
00010 void alert ();
00011 void printtime();
00012 
00013 DigitalOut led_link(LED1);
00014 DigitalOut led_detect(LED2);
00015 DigitalOut led_3(LED3);
00016 DigitalOut led_4(LED4);
00017 DigitalOut led_red(p20);
00018 
00019 PwmOut motor(p21);
00020 
00021 Serial usb(USBTX, USBRX);
00022 Serial com(p13, p14); // for printer
00023 
00024 int count = 0;
00025 #define LIMIT 5
00026 
00027 int main() {
00028     Ethernet ether;
00029     char buffer[0x600];
00030     int size = 0;
00031 
00032     usb.baud(38400);
00033     com.baud(9600);
00034     com.format(8, Serial::Odd, 2); // stopbit=2, parity=odd
00035     led_red = 1;
00036 
00037     set_time(getTime());
00038     printtime();
00039     com.printf(" initialized.\n");
00040 
00041     while (1) {
00042         if ((size = ether.receive()) != 0) {
00043             led_link = 0;
00044             ether.read(buffer, size);
00045             analyse(buffer, size);
00046         }
00047         led_link = ether.link();
00048         if (count == LIMIT) {
00049             alert();
00050             count++; // keep counting
00051         }
00052     }
00053 }
00054 
00055 void analyse(char *buf, int size) {
00056     if (size < 42) return; // too small packet
00057     if (buf[23] != 0x06) return; // not TCP/IP
00058     if ((buf[36]*256 + buf[37]) != 80) return; // not http port
00059 
00060     int r = search(buf, size, "Host:"); // retrieving host name
00061     if (r > 0) {
00062         char hostname[32] = "";
00063         strncpy(hostname, &buf[r+1], strcspn(&buf[r+1], "\r\n"));
00064 
00065         if (NULL != strstr(hostname, "komachi.yomiuri.co.jp")) {
00066             //usb.printf("%s ", hostname);
00067 
00068             r = search(buf, r, "GET");
00069             if (r > 0) {
00070                 char uri[256] = "";
00071                 strncpy(uri, &buf[r+1], strcspn(&buf[r+1], "\r\n"));
00072 
00073                 if (NULL != strstr(uri, ".htm")) {
00074                     led_detect = 1;
00075                     printtime();
00076                     com.printf(" %d/%d\n", ++count, LIMIT);
00077                     //com.printf("%s\n", uri);
00078                 }
00079             }
00080         }
00081     }
00082 }
00083 
00084 int search(char* hay, int size, char* needle) {
00085     int i = 0, pos = 0;
00086     int len = strlen(needle);
00087 
00088     for (i = 0; i < size; i++) {
00089         if (hay[i] == needle[pos]) {
00090             if (++pos > len-1) return(i+1);
00091         } else {
00092             pos = 0;
00093         }
00094     }
00095     return(-1);
00096 }
00097 
00098 unsigned long getTime() {
00099     char t[64];
00100     char r[] = "1234567890.123";
00101 
00102     HTTPClient http;
00103     http.get("http://ntp-a1.nict.go.jp/cgi-bin/jst", t);
00104 
00105     strncpy(r, strpbrk(t,"12"), sizeof(r)-1); // hacking
00106     return(atol(r) + 3600 * 9); // UCT to JST ;-)
00107 }
00108 
00109 void printtime() {
00110     time_t t = time(NULL);
00111     char buf[32];
00112     strftime(buf, 32, "%Y/%m/%d %H:%M:%S", localtime(&t));
00113     com.printf("%s", buf);
00114 }
00115 
00116 void alert () {
00117     motor = 0.1f; // drive slow
00118     wait_ms(3000);
00119     motor = 0;
00120 }