00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __WPROGRAM_H__
00025 #include <WaspClasses.h>
00026 #endif
00027
00028
00029
00030
00031 uint16_t WaspBT::waitForData(uint8_t* data, char* expectedAnswer)
00032 {
00033 uint16_t i=0;
00034 for (i = 0; i < 100; i++) received[i] = ' ';
00035
00036 int theLength = 0;
00037 int it=0;
00038 bool theSame=false;
00039
00040 uint8_t first=1;
00041 uint8_t match=0;
00042 i=0;
00043
00044 while( expectedAnswer[theLength]!='\0' ) theLength++;
00045
00046 while( !match && data[i]!='\0' )
00047 {
00048 if( first )
00049 {
00050 for(it=0;it<theLength;it++)
00051 {
00052 received[it]=data[i];
00053 i++;
00054 }
00055 first=0;
00056 }
00057 it=0;
00058 theSame=true;
00059 for(it=0; it<theLength ; it++)
00060 {
00061 if(received[it]!=expectedAnswer[it]){
00062 theSame= false;
00063 break;
00064 }
00065 }
00066 if( theSame ) match=1;
00067 else
00068 {
00069 for(it=0; it<theLength-1 ; it++)
00070 {
00071 received[it]=received[it+1];
00072 }
00073 received[it]=data[i];
00074 i++;
00075 }
00076 }
00077
00078 if( !match ) i=0;
00079 return i;
00080 }
00081
00082
00083 uint8_t WaspBT::parse_data()
00084 {
00085 parse_data("");
00086 }
00087
00088
00089
00090 uint8_t WaspBT::parse_data(char* answer)
00091 {
00092 bool withAnswer = false;
00093 bool match = false;
00094 uint8_t pin_en = 0;
00095 uint8_t lengthAnswer = 0;
00096 long previous=millis();
00097 long previous2=millis();
00098 int16_t interval=50;
00099 int16_t intervalMAX=40000;
00100 uint16_t MAX_BT_DATA=450;
00101 uint8_t* memory = (uint8_t*) calloc(MAX_PARSE,sizeof(uint8_t));
00102 if( memory==NULL ) return -1;
00103
00104
00105 i=0;
00106 while(answer[i]!='\0') i++;
00107 lengthAnswer=i;
00108 if( lengthAnswer ) withAnswer = true;
00109
00110 if( !(strcmp(answer,"+RDDSCNF=0")) ) interval=30000;
00111 if( !(strcmp(answer,"+RSDSCNF=0")) ){
00112 interval=10000;
00113 pin_en=1;
00114 }
00115 if( !(strcmp(answer,"ROK")) ) interval=5000;
00116 if( !(strcmp(answer,"+RCCRCNF")) ){
00117 interval=10000;
00118 pin_en=2;
00119 }
00120
00121
00122 i=0;
00123 previous2=millis();
00124 previous=millis();
00125 while( ((millis()-previous)<interval) && ((millis()-previous2)<intervalMAX) && i<MAX_BT_DATA && !match)
00126 {
00127 if(serialAvailable(0))
00128 {
00129 memory[i]=serialRead(0);
00130 i++;
00131 previous=millis();
00132 }
00133 if( withAnswer ){
00134 if( waitForData(memory,answer) ) match=true;
00135 }
00136 if( millis()-previous < 0 ) previous=millis();
00137 if( millis()-previous2 < 0 ) previous2=millis();
00138 }
00139 printNewline(1);
00140
00141
00142 if( waitForData(memory,"+RPCI") ){
00143 if(!pin_request(memory,pin_en)) match = true;
00144 }
00145 if( waitForData(memory,"+RCOI") && (auto_accept==BT_MANUAL_ACC) ) accept_connection(memory);
00146 if( waitForData(memory,"+RDAI") ){
00147 if(!data_request(memory)) match = true;
00148 }
00149 if( waitForData(memory,"+RDII") ) ending_connection(memory);
00150 if( waitForData(memory,"+RDDSCNF=0") ) parse_brothers(memory,i);
00151 if( waitForData(memory,"+RSDSCNF=0") ) parse_device(memory);
00152 if( waitForData(memory,"+RCCRCNF") ) get_MTU(memory);
00153
00154 if( match ){
00155 free(memory);
00156 memory=NULL;
00157 return 0;
00158 }
00159
00160 if( withAnswer )
00161 {
00162 if( i>0 ){
00163 free(memory);
00164 memory=NULL;
00165 return 1;
00166 }
00167 else{
00168 free(memory);
00169 memory=NULL;
00170 return 2;
00171 }
00172 }
00173 else
00174 {
00175 if( i>0 ){
00176 free(memory);
00177 memory=NULL;
00178 return 3;
00179 }
00180 else{
00181 free(memory);
00182 memory=NULL;
00183 return 4;
00184 }
00185 }
00186
00187 }
00188
00189
00190 uint8_t WaspBT::pin_request(uint8_t* data, uint8_t pin_en)
00191 {
00192 char command[50];
00193 uint8_t length=0;
00194
00195 i=0;
00196 while(bt_pin[i]!='\0') i++;
00197 length=i;
00198
00199 if(length<10) sprintf(command,"%s0%d,%s",BT_AT_PIN_REQUEST,length,bt_pin);
00200 else if(length>=10) sprintf(command,"%s%d,%s",BT_AT_PIN_REQUEST,length,bt_pin);
00201
00202 printData(command);
00203
00204 if(pin_en==1) return parse_data("+RSDSCNF=0");
00205 if(pin_en==2) return parse_data("+RCCRCNF");
00206 else if(!pin_en) return parse_data("+RSLE");
00207 }
00208
00209
00210 uint8_t WaspBT::accept_connection(uint8_t* data)
00211 {
00212 char command[50];
00213
00214 sprintf(command,"%s1",BT_AT_ACCEPT_CONN);
00215
00216 printData(command);
00217
00218 return parse_data("OK");
00219 }
00220
00221
00222 uint8_t WaspBT::data_request(uint8_t* data)
00223 {
00224 uint16_t i=0;
00225 uint16_t j=0;
00226 uint8_t k=0;
00227 uint16_t init=0;
00228
00229 j=waitForData(data,"+RDAI");
00230
00231 while(data[j]!=',') j++;
00232 j++;
00233
00234 while(data[j]!='\r' && i<BT_MAX_DATA){
00235 data_received[i]=data[j];
00236 i++;
00237 j++;
00238 }
00239 printNewline(1);
00240 if(i<BT_MAX_DATA) data_received[i]='\0';
00241 else data_received[BT_MAX_DATA-1]='\0';
00242
00243 return 0;
00244 }
00245
00246
00247 void WaspBT::ending_connection(uint8_t* data)
00248 {
00249 active_connection=0;
00250 }
00251
00252
00253 void WaspBT::parse_brothers(uint8_t* data, uint16_t number_of_data)
00254 {
00255 uint8_t i=0;
00256 uint16_t j=0;
00257 uint8_t k=0;
00258 uint8_t end_line=0;
00259
00260 i=0;
00261 while(i<number_of_data){
00262 if(data[i]=='\n') end_line++;
00263 i++;
00264 }
00265
00266 devices_found=end_line-1;
00267 if(devices_found>BT_MAX_DEVICES) devices_found=BT_MAX_DEVICES;
00268
00269 i=0;
00270 while( i<(end_line-1) && i<BT_MAX_DEVICES )
00271 {
00272 while(data[j]!='=' && j<number_of_data) j++;
00273 j++;
00274 for(k=0;k<12;k++)
00275 {
00276 discovered_devices[i].mac_address[k]=data[j];
00277 j++;
00278 }
00279 j++;
00280 k=0;
00281 while(data[j]!=',' && k<18)
00282 {
00283 discovered_devices[i].name[k]=data[j];
00284 j++;
00285 k++;
00286 }
00287 discovered_devices[i].name[k]='\0';
00288 while(data[j]!=',' && j<number_of_data) j++;
00289 j++;
00290 k=0;
00291 while(data[j]!='\r' && k<6)
00292 {
00293 discovered_devices[i].CoD[k]=data[j];
00294 j++;
00295 k++;
00296 }
00297 discovered_devices[i].CoD[k]='\0';
00298 i++;
00299 k=0;
00300 }
00301 }
00302
00303
00304 void WaspBT::parse_device(uint8_t* data)
00305 {
00306 uint16_t j=0;
00307 uint8_t k=0;
00308
00309 if( waitForData(data,"+RSDSRES=") ){
00310 while(data[j]!='=') j++;
00311 j++;
00312 while(data[j]!=',' && k<16)
00313 {
00314 device_service_name[k]=data[j];
00315 j++;
00316 k++;
00317 }
00318 j++;
00319 device_service_name[k]='\0';
00320 device_service_channel[0]=data[j];
00321 device_service_channel[1]=data[j+1];
00322 }
00323 }
00324
00325
00326 void WaspBT::get_MTU(uint8_t* data)
00327 {
00328 uint8_t i=0;
00329 uint16_t j=0;
00330 uint8_t k=0;
00331
00332 while(data[j]!='=') j++;
00333 j++;
00334
00335 connection_mtu[0]=serialRead(0);
00336 connection_mtu[1]=serialRead(0);
00337 connection_mtu[2]=serialRead(0);
00338 serialFlush(1);
00339 }
00340
00341
00342 void WaspBT::printData(char* data)
00343 {
00344 uint8_t uart=0;
00345
00346 printString(data,uart);
00347 printByte('\r',uart);
00348 printByte('\n',uart);
00349 }
00350
00351
00352
00353
00354 WaspBT::WaspBT()
00355 {
00356 i=0;
00357 while(BT_PIN[i]!='\0'){
00358 bt_pin[i]=BT_PIN[i];
00359 i++;
00360 }
00361
00362 auto_accept=BT_AUTO_ACC;
00363
00364 active_connection=0;
00365
00366 devices_found=0;
00367 }
00368
00369
00370
00371 void WaspBT::ON()
00372 {
00373 uint8_t answer=0;
00374
00375 beginSerial(XBEE_RATE, 0);
00376 pinMode(XBEE_PW,OUTPUT);
00377 digitalWrite(XBEE_PW,HIGH);
00378
00379
00380 answer=parse_data("ROK");
00381
00382 if(answer) flag |= BT_ERROR_ON;
00383 }
00384
00385
00386 void WaspBT::OFF()
00387 {
00388 closeSerial(0);
00389 pinMode(XBEE_PW,OUTPUT);
00390 digitalWrite(XBEE_PW,LOW);
00391 }
00392
00393
00394
00395 uint8_t WaspBT::init()
00396 {
00397 uint8_t error = 0;
00398
00399 first=1;
00400
00401 if(setSecurity(BT_SECURITY_PREDEFINED,bt_pin)) error++;
00402
00403 if(setServiceParameters(BT_SERVICE_NAME,BT_SERVICE_CHANNEL,BT_SERVICE_COD)) error++;
00404
00405 if(setPublicName(BT_PUBLIC_NAME)) error++;
00406
00407 if(setAutoAccept(BT_AUTO_ACC)) error++;
00408
00409 if(setDiscoveryOptions(BT_DISCOVERABLE)) error++;
00410
00411 return error;
00412 }
00413
00414
00415
00416 uint8_t WaspBT::setSecurity(uint8_t mode, char* pin)
00417 {
00418 char command[50];
00419 uint8_t length=0;
00420 uint8_t error=0;
00421
00422 i=0;
00423 while(pin[i]!='\0'){
00424 bt_pin[i]=pin[i];
00425 i++;
00426 }
00427 length=i;
00428
00429 if(!first) reset();
00430
00431 if(length<10) sprintf(command,"%s%d,1,1,0%d,%s",BT_AT_SECURITY,mode,length,pin);
00432 else if(length>=10) sprintf(command,"%s%d,1,1,%d,%s",BT_AT_SECURITY,mode,length,pin);
00433
00434 printData(command);
00435
00436 first=0;
00437
00438 error=parse_data("OK");
00439 if(error) flag |= BT_ERROR_SECURITY;
00440 return error;
00441 }
00442
00443
00444
00445 uint8_t WaspBT::setPublicName(char* name)
00446 {
00447 char command[50];
00448 uint8_t length=0;
00449 uint8_t error=0;
00450
00451 i=0;
00452 while(name[i]!='\0') i++;
00453 length=i;
00454
00455 if(length<10) sprintf(command,"%s0%d,%s",BT_AT_PUBLIC_NAME,length,name);
00456 else if(length>=10) sprintf(command,"%s%d,%s",BT_AT_PUBLIC_NAME,length,name);
00457
00458 printData(command);
00459
00460 error=parse_data("OK");
00461 if(error) flag |= BT_ERROR_NAME;
00462 return error;
00463 }
00464
00465
00466
00467 uint8_t WaspBT::setAutoAccept(uint8_t mode)
00468 {
00469 char command[50];
00470 uint8_t error=0;
00471
00472 auto_accept=mode;
00473
00474 sprintf(command,"%s%d",BT_AT_AUTO_ACCEPT,mode);
00475
00476 printData(command);
00477
00478 error=parse_data("OK");
00479 if(error) flag |= BT_ERROR_ACCEPT;
00480 return error;
00481 }
00482
00483
00484
00485 uint8_t WaspBT::setDiscoveryOptions(uint8_t option)
00486 {
00487 char command[50];
00488 uint8_t length=0;
00489 uint8_t error=0;
00490
00491 sprintf(command,"%s%d",BT_AT_DISCOV_OPT,option);
00492
00493 printData(command);
00494
00495 error=parse_data("OK");
00496 if(error) flag |= BT_ERROR_DISC_OPT;
00497 return error;
00498 }
00499
00500
00501 uint8_t WaspBT::readData()
00502 {
00503 return parse_data();
00504 }
00505
00506
00507 uint8_t WaspBT::setServiceParameters(char* name, uint8_t channel, char* cod)
00508 {
00509 char command[50];
00510 uint8_t length=0;
00511 uint8_t error=0;
00512
00513 i=0;
00514 while(name[i]!='\0') i++;
00515 length=i;
00516
00517 if(length<10 && channel<10) sprintf(command,"%s1101,0%d,%s,0%d,%s",BT_AT_SERVICE_PAR,length,name,channel,cod);
00518 else if(length>=10 && channel<10) sprintf(command,"%s1101,%d,%s,0%d,%s",BT_AT_SERVICE_PAR,length,name,channel,cod);
00519 else if(length<10 && channel>=10) sprintf(command,"%s1101,0%d,%s,%d,%s",BT_AT_SERVICE_PAR,length,name,channel,cod);
00520 else if(length>=10 && channel>=10) sprintf(command,"%s1101,%d,%s,%d,%s",BT_AT_SERVICE_PAR,length,name,channel,cod);
00521
00522 printData(command);
00523
00524 error=parse_data("OK");
00525 if(error) flag |= BT_ERROR_SERVICE;
00526 return error;
00527 }
00528
00529
00530
00531 uint8_t WaspBT::reset()
00532 {
00533 uint8_t error=0;
00534
00535 printData(BT_AT_RESET);
00536
00537 error=parse_data("ROK");
00538 if(error) flag |= BT_ERROR_RESET;
00539 return error;
00540 }
00541
00542
00543
00544 uint8_t WaspBT::scanNetwork()
00545 {
00546 char command[50];
00547 uint8_t error=0;
00548
00549 sprintf(command,"%s%d",BT_AT_DISCOVER,0);
00550
00551 printData(command);
00552
00553 if(parse_data("OK")) return 1;
00554
00555 error=parse_data("+RDDSCNF=0");
00556 if(error) flag |= BT_ERROR_SCAN;
00557 return error;
00558 }
00559
00560
00561 uint8_t WaspBT::discoverDevice(char* mac, char* profile)
00562 {
00563 char command[50];
00564 uint8_t error=0;
00565
00566 sprintf(command,"%s%s,%s",BT_AT_DISCOVER_DEV,mac,profile);
00567
00568 printData(command);
00569
00570 if(parse_data("OK")) return 1;
00571
00572 error=parse_data("+RSDSCNF=0");
00573 if(error) flag |= BT_ERROR_DISC;
00574 return error;
00575 }
00576
00577
00578 uint8_t WaspBT::createConnection(char* mac,char* channel)
00579 {
00580 char command[50];
00581 uint8_t error=0;
00582
00583 sprintf(command,"%s%s,%s",BT_AT_CONNECT,mac,channel);
00584
00585 printData(command);
00586
00587 if(parse_data("OK")){
00588 flag |= BT_ERROR_CONNECTING;
00589 return 1;
00590 }
00591
00592 error=parse_data("+RCCRCNF");
00593 if(error) flag |= BT_ERROR_CONNECTING;
00594 else active_connection=1;
00595
00596 return error;
00597 }
00598
00599
00600 uint8_t WaspBT::getOwnMac()
00601 {
00602 printData(BT_AT_OWN_MAC);
00603
00604 if(!parse_data("+RRBDRES")){
00605 i=0;
00606 while(i<12){
00607 own_mac[i]=serialRead(0);
00608 i++;
00609 }
00610 serialFlush(1);
00611 return 0;
00612 }
00613 flag |= BT_ERROR_MAC;
00614 return 1;
00615 }
00616
00617
00618 uint8_t WaspBT::removeTrustedDevice(char* mac)
00619 {
00620 char command[50];
00621 uint8_t error=0;
00622
00623 sprintf(command,"%s%s",BT_AT_DELETE_TD,mac);
00624
00625 printData(command);
00626
00627 error=parse_data("OK");
00628 if(error) flag |= BT_ERROR_REMOVING_TD;
00629 return error;
00630 }
00631
00632
00633 uint8_t WaspBT::sendData(char* data)
00634 {
00635 uint16_t length=0;
00636 uint8_t error=0;
00637
00638 i=0;
00639 while(data[i]!='\0') i++;
00640 length=i;
00641
00642 char command[20+length];
00643
00644 if(length<10) sprintf(command,"%s00%d,%s",BT_AT_SEND_DATA,length,data);
00645 else if(length<100 && length>10) sprintf(command,"%s0%d,%s",BT_AT_SEND_DATA,length,data);
00646 else sprintf(command,"%s%d,%s",BT_AT_SEND_DATA,length,data);
00647
00648 printData(command);
00649
00650 error=parse_data("OK");
00651 delay(300);
00652 if(error) flag |= BT_ERROR_SENDING_DATA;
00653 return error;
00654 }
00655
00656
00657 uint8_t WaspBT::removeConnection()
00658 {
00659 printData(BT_AT_DISCONNECT);
00660
00661 if(!parse_data("OK"))
00662 {
00663 getOwnMac();
00664 return 0;
00665 }
00666 flag |= BT_ERROR_REMOVING_CONNECTION;
00667 return 1;
00668 }
00669
00670 uint8_t WaspBT::createStreamConnection()
00671 {
00672 uint8_t error=0;
00673
00674 printData(BT_AT_STREAM_CONN);
00675
00676 error=parse_data("OK");
00677 if(error) flag |= BT_ERROR_STREAM;
00678 return error;
00679 }
00680
00681
00682 WaspBT BT=WaspBT();