4 * Created on: 2014. 11. 5.
12 //#define __INTERNAL_DEBUG__ 1
14 PROGMEM const char TAG[] = "TrackeeSensor";
15 #define LIMIT_COUNT 100
17 #define DEBUG_SIZE DUMMY_SIZE
19 #define SERIAL_SIZE 100
24 bool isConnected = false;
27 char SelfMaxaddr[19] = {0,};
29 char debugD[DEBUG_SIZE]={0,};
32 char SerialData[SERIAL_SIZE]={0,};
33 char BLEData[SERIAL_SIZE]={0,};
36 // baudRate이 115200이면, 주의해야한다.
37 // Arduino Chip은 16bit Process 이므로 int형의 사이즈가 4byte형 signed int이다.
38 void Cble::init(long baudRate, int ble_mode, char* SelfMaxAddr)
40 debug_printf("BLE shiled is initialed.-\r\n");
42 if(SelfMaxAddr == NULL && ble_mode != BLE_NOCHANGE )
44 debug_printf("Error : Insert Maxaddress of Arduino BLE shiled.\r\n");
47 sprintf(SelfMaxaddr, "%s",SelfMaxAddr);
53 memset(debugD,0,DEBUG_SIZE);
55 if( ble_mode != BLE_NOCHANGE )
57 while( IsSelfArduino() == false )
58 this->pollingDisconnect();
61 if( ble_mode == BLE_MASTER )
63 pollingRequest("AT+ROLE1", debugD, 8);
64 memset(debugD, 0, DEBUG_SIZE);
66 pollingRequest("AT+MODE0", debugD, 8);
67 memset(debugD, 0, DEBUG_SIZE);
69 pollingRequest("AT+NOTI0", debugD, 8);
70 memset(debugD, 0, DEBUG_SIZE);
72 else if ( ble_mode == BLE_SLAVER )
74 pollingRequest("AT+ROLE0", debugD, 8);
75 memset(debugD, 0, DEBUG_SIZE);
77 pollingRequest("AT+MODE2", debugD, 8);
78 memset(debugD, 0, DEBUG_SIZE);
80 pollingRequest("AT+NOTI0", debugD, 8);
81 memset(debugD, 0, DEBUG_SIZE);
84 if( ble_mode != BLE_NOCHANGE )
86 while( IsSelfArduino() == false )
87 this->pollingDisconnect();
93 void Cble::StatusRead( void )
95 debug_printf("StatusRead function called.\r\n");
97 pollingRequest("AT+ROLE?", debugD, 9);
98 memset(debugD, 0, DEBUG_SIZE);
100 pollingRequest("AT+MODE?", debugD, 8);
101 memset(debugD, 0, DEBUG_SIZE);
103 pollingRequest("AT+IMME?", debugD, 9);
104 memset(debugD, 0, DEBUG_SIZE);
106 pollingRequest("AT+TYPE?", debugD, 8);
107 memset(debugD, 0, DEBUG_SIZE);
109 pollingRequest("AT+POWE?", debugD, 8);
110 memset(debugD, 0, DEBUG_SIZE);
112 pollingRequest("AT+NOTI?", debugD, 8);
113 memset(debugD, 0, DEBUG_SIZE);
115 pollingRequest("AT+PIO1?", debugD, 8);
116 memset(debugD, 0, DEBUG_SIZE);
120 bool Cble::IsConnected( void )
125 bool Cble::IsSelfArduino( void )
127 #ifdef __INTERNAL_DEBUG__
128 debug_printf("IsSelfArduino is called.\r\n");
130 int length=mustHaveRequest("AT+ADDR?", debugD, 20);
132 if( strstr(debugD, SelfMaxaddr) )
135 memset(debugD,0,length);
141 memset(debugD,0,length);
146 bool Cble::pollingConnect( const char* maxAddr )
148 #ifdef __INTERNAL_DEBUG__
149 debug_printf("pollingConnect is called.\r\n");
152 #define CONNECT_SIZE 20
154 char cmd[CONNECT_SIZE]= {0,};
156 // is Connected Address ?
157 pollingRequest("AT+ADDR?", debugD, 20);
158 if( strstr(debugD, maxAddr) )
161 memset(debugD,0,DEBUG_SIZE);
164 memset(debugD,0,DEBUG_SIZE);
167 sprintf(cmd, "AT+CON%s",maxAddr);
168 if( pollingRequest(cmd, debugD, 8) == false )
170 debug_printf("Error : %s command is failed.\r\n",cmd );
171 memset(debugD,0,DEBUG_SIZE);
175 else if( strstr(debugD,"OK+CONNA") )
178 memset(debugD,0,DEBUG_SIZE);
182 memset(debugD,0,DEBUG_SIZE);
186 // Confirm Connected Address.
187 pollingRequest("AT+ADDR?", debugD, 20);
189 if( strstr(debugD, maxAddr) == 0 )
192 if ( strstr(debugD, "OK+CONNF") )
194 memset(debugD,0,DEBUG_SIZE);
195 streamDummy(debugD, 8);
198 memset(debugD,0,DEBUG_SIZE);
207 #define CMD_DISCONNECT "(CMD_DISCON)"
208 void Cble::pollingDisconnect( void )
210 #ifdef __INTERNAL_DEBUG__
211 debug_printf("pollingDisconnect is called.\r\n");
215 pollingRequest("\r\n" CMD_DISCONNECT "\r\n", NULL, NULL);
218 length=mustHaveRequest("AT", debugD, 2);
219 if( strstr(debugD,"OK") )
222 memset(debugD,0,length);
224 if( BLE.available() )
226 if( BLE.findUntil(CMD_DISCONNECT, "\r\n") == true )
228 debug_printf("pollingDisconnect : Detected %s command.\r\n", CMD_DISCONNECT);
231 debug_printf("pollingDisconnect : Send AT message.\r\n");
233 memset(debugD, 0, length);
235 length=mustHaveRequest("AT", debugD, 2);
236 }while( strstr(debugD,"OK") == NULL );
238 debug_printf("pollingDisconnect() is Success.\r\n");
239 memset(debugD, 0, length);
247 int Cble::pollingGetRSSI( void )
255 char rssi_res[RSSI_SIZE] = {0,};
257 streamDummy(NULL, NULL);
259 if( isConnected == true )
261 BLE.write("AT+RSSI?");
267 if ( BLE.available() )
268 c = (char)BLE.read();
279 if( index == RSSI_SIZE-1 )
281 rssi_res[index] = '\0';
286 // debug_printf("time_cnt=%d\r\n", time_cnt);
287 if( time_cnt == 0xFFFFF )
289 debug_printf("Error : Time Out GetRSSI().\r\n");
296 #ifdef __INTERNAL_DEBUG__
297 debug_printf("res= %s \r\n",rssi_res);
299 char* Srssi = &(rssi_res[RSSI_POS]);
300 // debug_printf("[S] rssi=%s\r\n", Srssi);
301 int rssi = atoi(Srssi);
302 // debug_printf("rssi=%d\r\n", rssi);
313 int Cble::mustHaveRequest(const char* request, char* data, int dataLen)
317 int required_length = dataLen;
318 char* data_index = data;
325 while( (length=BLE.readBytes(data_index, required_length)) == 0 )
330 if ( cnt >= LIMIT_COUNT/10 )
332 #ifdef __INTERNAL_DEBUG__
333 debug_printf("=====> Retry Request command Send. <=========\r\n");
339 required_length = dataLen;
346 required_length -= length;
347 if( required_length != 0 )
349 data_index = data_index + length;
354 #ifdef __INTERNAL_DEBUG__
355 debug_printf("[ %s ] %s\r\n\r\n", request, data);
360 bool Cble::pollingRequest(const char* request, char* data, int dataLen)
364 int required_length = dataLen;
365 char* data_index = data;
375 while( (length=BLE.readBytes(data_index, required_length)) == 0 )
378 if ( cnt >= LIMIT_COUNT )
380 debug_printf("[ %s ] TimeOut : No data.\r\n\r\n", request);
385 required_length -= length;
386 if( required_length != 0 )
388 data_index = data_index + length;
393 #ifdef __INTERNAL_DEBUG__
394 debug_printf("[ %s ] %s\r\n\r\n", request, data);
400 void Cble::streamDummy( char* data, int dataLen)
404 int required_length = dataLen;
405 char* data_index = data;
407 if( data && required_length )
410 while( (length=BLE.readBytes(data_index, required_length)) == 0 )
413 if ( cnt >= LIMIT_COUNT )
415 debug_printf("[ streamDummy ] TimeOut : No data.\r\n\r\n");
420 required_length -= length;
421 if( required_length != 0 )
423 data_index = data_index + length;
428 debug_printf("[ streamDummy ] %s\r\n\r\n", data);
432 if( BLE.available() )
434 BLE.readBytes(debugD, DEBUG_SIZE);
435 #ifdef __INTERNAL_DEBUG__
436 debug_printf("[ streamDummy ] %s\r\n\r\n", debugD);
438 memset(debugD, 0, DEBUG_SIZE);
446 #define REGARD_DISCON 5000
448 unsigned long global_cnt = 0;
450 void Cble::BLE2Debug( int autoDiscon )
455 if( BLE.available() )
457 while( (length=BLE.readBytes(BLEData, SERIAL_SIZE)) == 0 )
460 if ( cnt >= LIMIT_COUNT )
467 Serial.println(BLEData);
468 memset(BLEData, 0, length);
470 else if ( autoDiscon )
473 // debug_printf("global_cnt=%u , ", global_cnt );
474 // debug_printf("needDiscon=%d\r\n", needDiscon);
475 if( !needDiscon && global_cnt >= REGARD_DISCON )
478 debug_printf("result : global_cnt=%u , ", global_cnt );
479 debug_printf("needDiscon=%d\r\n", needDiscon);
484 debug_printf("Auto Discon : global_cnt=%u , ", global_cnt );
485 debug_printf("needDiscon=%d\r\n", needDiscon);
486 if( pollingRequest("AT", debugD, 2) == true )
495 char* Cble::Debug2BLE( int BLEwrite )
499 if( Serial.available() )
501 char c = (char)Serial.read();
509 debug_printf("\r\n");
510 if(BLEwrite && SerialCnt)
512 BLE.write(SerialData);
516 result = (char*)malloc(sizeof(char)*(SerialCnt+1));
517 memcpy(result, SerialData, SerialCnt );
518 result[SerialCnt] = NULL;
520 memset(SerialData, 0, SerialCnt+1);
524 SerialData[SerialCnt] = c;
534 void Cble::BLE2Debug( int autoDiscon )
539 char* Cble::Debug2BLE( int BLEwrite )
545 void Cble::FactoryReset( void )
547 debug_printf("FactoryReset is called.\r\n");
549 pollingRequest("AT+RENEW", debugD, 8);
550 memset(debugD, 0, DEBUG_SIZE);
554 void Cble::firstUartSetting( void )
556 debug_printf("firstUartSetting is called.\r\n");
563 pollingRequest("AT+BAUD4", debugD, 8);
564 memset(debugD, 0, DEBUG_SIZE);
566 debug_printf("Please Power off and on.\r\n");
570 // BLE.begin(115200);
573 // pollingRequest("AT", debugD, DEBUG_SIZE);
574 // memset(debugD, 0, DEBUG_SIZE);