WiFi Tracker, scan WiFi probe packer and publish message to broker

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WiFiTracker.cpp Source File

WiFiTracker.cpp

00001 #include "mbed.h"
00002 #include "WiFiTracker.h"
00003 
00004 
00005 
00006 WiFiTracker::WiFiTracker() : led(LED3), wifi(P23, P24)
00007 {
00008     wifi.baud(115200);
00009 //    dbg.baud(115200);
00010     led = 0;
00011 //    dbg.printf("make wifi tracker!\n"); 
00012     g_store_cnt = 0;
00013     isPrinted = false;
00014     //mkdir("/sd/mydir", 0777);
00015     
00016     LedBlink(5, 20);
00017     
00018 }
00019 
00020 
00021 WiFiTracker::~WiFiTracker()
00022 {
00023     printf("destroy\n");
00024 
00025 }
00026 
00027 
00028 void WiFiTracker::init()
00029 {
00030 
00031     //char cmd[] = "AT+MWPACKET=3,1,1,0,4000,,11010111";
00032     char cmd[] = "AT+MWPACKET=3,1,3,0,4000,,10010001";
00033     int8_t ret = -1;
00034     
00035     ret = send_command(cmd);
00036     printf("init ret: %d",ret);    
00037 }
00038 
00039 // 0x4000/04:1b:ba:21:6d:a1/-66
00040 int8_t WiFiTracker::tracking(int method)
00041 {
00042     char buf[30] = {0,};
00043     struct WiFiProbe probe;
00044     bool ret;
00045     
00046     if(wifi.readable())
00047     {
00048         wifi.scanf("%s",&buf);
00049         
00050         ret = isValidData(&probe, buf);
00051         
00052         if( ret == true )
00053         {
00054             if( !strcmp(PreProbe.mac, probe.mac) )
00055             {   
00056                 isPrinted = true;
00057                 //dbg.printf("non print mac (%s)\n",probe.mac);
00058                 return -1;
00059             }
00060             else
00061             {
00062                 memcpy(&PreProbe, &probe, sizeof(WiFiProbe));
00063                 isPrinted = false;
00064                 return 0;
00065             }
00066             
00067             //switch(method)
00068 //            {
00069 //                case MQTT_PUBLISH:
00070 //                    
00071 //                case JUST_PRINT:
00072 //                    dbg.printf("(%s, %d)\n",probe.mac, probe.rssi);
00073 //                    break;
00074 //            }
00075         }
00076         else return -1;   
00077     }
00078     return 0;
00079 }
00080 bool WiFiTracker::isValidData(WiFiProbe* probe,char* str)
00081 {
00082     char *p, *p1;
00083     char char_rssi[3] = {0,};
00084     
00085     p = strstr(str, probe_filter);
00086     
00087     if( p == NULL )
00088     {
00089         return false;
00090     }
00091     else
00092     {    
00093         memset(probe,'\0',sizeof(WiFiProbe));
00094         
00095         p = strstr(str, delimiter);
00096         p1 = strstr(p+1, delimiter);
00097         
00098         strncpy(probe->mac,p+1,(p1-p-1));
00099         
00100         delete_char(probe->mac,':',strlen(probe->mac));
00101         
00102         strcpy(probe->rssi, p1+2);
00103         
00104 //        strcpy(char_rssi, p1+2);
00105 //        probe->rssi = atoi(char_rssi);
00106         //dbg.printf("(%s, %d)\n",probe->mac, probe->rssi);
00107         return true;
00108     }
00109 }
00110 
00111 int8_t WiFiTracker::getProbe(char* mac, char* rssi)
00112 {
00113     
00114     
00115     memcpy(mac, PreProbe.mac, sizeof(PreProbe.mac));   
00116     memcpy(rssi, PreProbe.rssi, sizeof(PreProbe.rssi));
00117     
00118     if( isPrinted )
00119         return -1;
00120     else
00121     {
00122         isPrinted = true;
00123         return 0;
00124     }
00125 }
00126 
00127 //uint8_t WiFiTracker::store_probe_request(WiFiProbe wp)
00128 //uint8_t WiFiTracker::store_probe_request(char c)
00129 uint8_t WiFiTracker::write_sdcard(char c)
00130 {
00131 //    dbg.printf("store\n"); 
00132     
00133     return 0;
00134 }
00135 
00136 uint8_t WiFiTracker::write_sdcard(char* str, int len)
00137 {    
00138     char temp_str[30] = {0,};
00139     
00140     //dbg.printf("%s, (%d)", str, len);
00141     
00142     if(char_cnt(str,'/') && len == 30 )
00143     {
00144         strncpy(temp_str, str, len);
00145         char_change(temp_str, '/', ',');
00146 //        dbg.printf("(%d), %s", len, temp_str);
00147         g_store_cnt++;  
00148     }
00149     
00150     
00151     if(g_store_cnt % 100 == 0)
00152     {
00153 //        dbg.printf("\r\n[100]\r\n");
00154         
00155         LedBlink(2,50);
00156     }
00157     
00158     //dbg.printf("\r\nwrite sd card\r\n"); 
00159     return 0;
00160 }
00161 
00162 
00163 // count the number of c in str
00164 int WiFiTracker::char_cnt(char* str, char c)
00165 {
00166     int i = 0;
00167     char* tmp = str;
00168     
00169     while( *tmp )
00170     {
00171         if( *tmp++ == c )
00172             i++;
00173     }
00174     
00175     return i;
00176 }
00177 
00178 // change dst_char to new_char in str
00179 int WiFiTracker::char_change(char* str, char dst_char, char new_char)
00180 {
00181     int i = 0;
00182     char* tmp = str;
00183     
00184     while( *tmp )
00185     {
00186         if( *tmp == dst_char )
00187             *tmp = new_char;
00188         tmp++;
00189     }
00190     
00191     return i;    
00192     
00193 } 
00194 
00195 
00196 void WiFiTracker::delete_char(char *src, char c, int len)
00197 {
00198 
00199     char *dst;
00200 
00201     int i;
00202     // Do not remove NULL characters.
00203     if ( c == 0 )
00204         return;
00205         
00206     // Small attempt to control a buffer overflow if the
00207     // the string is not null-terminated and a proper length
00208     // is not specified.
00209 
00210     if ( len <= 0 )
00211         len = 17;
00212 
00213     dst = src;
00214 
00215     for ( i = 0; i < len && *src != 0; i++, src++ )
00216     {
00217         if ( *src != c )
00218             *dst++ = *src;
00219 
00220     }
00221     // Ensure the string is null-terminated.
00222 
00223     *dst = 0;
00224 
00225     return;
00226 
00227 }
00228 
00229 // delete_char()
00230 
00231 void WiFiTracker::LedBlink(int cnt, int interval)
00232 {
00233     int i;
00234     
00235     for(i=0; i<cnt; i++)
00236     {
00237         led = 1;
00238         wait_ms(interval);
00239         led = 0;
00240     }
00241 }
00242 
00243 
00244 int8_t WiFiTracker::send_command(char* cmd)
00245 {
00246     wifi.printf("%s\r",cmd);    
00247     return 0;
00248 }
00249 
00250 //int8_t WiFiTracker::wait_response(char* rsp)
00251 //{
00252 //}
00253 
00254 
00255