1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
25 //#define __INTERNAL_DEBUG__ 1
27 PROGMEM const char TAG[] = "TrackeeSensor";
28 #define LIMIT_COUNT 100
30 #define DEBUG_SIZE DUMMY_SIZE
32 #define SERIAL_SIZE 100
37 bool isConnected = false;
40 char SelfMaxaddr[19] = {0,};
42 char debugD[DEBUG_SIZE] = {0,};
45 char SerialData[SERIAL_SIZE] = {0,};
46 char BLEData[SERIAL_SIZE] = {0,};
49 void Cble::init(long baudRate, int ble_mode, char *SelfMaxAddr)
51 debug_printf("BLE shiled is initialed.-\r\n");
53 if (SelfMaxAddr == NULL && ble_mode != BLE_NOCHANGE )
55 debug_printf("Error : Insert Maxaddress of Arduino BLE shiled.\r\n");
58 sprintf(SelfMaxaddr, "%s", SelfMaxAddr);
64 memset(debugD, 0, DEBUG_SIZE);
66 if ( ble_mode != BLE_NOCHANGE )
68 while ( IsSelfArduino() == false )
69 this->pollingDisconnect();
72 if ( ble_mode == BLE_MASTER )
74 pollingRequest("AT+ROLE1", debugD, 8);
75 memset(debugD, 0, DEBUG_SIZE);
77 pollingRequest("AT+MODE0", debugD, 8);
78 memset(debugD, 0, DEBUG_SIZE);
80 pollingRequest("AT+NOTI0", debugD, 8);
81 memset(debugD, 0, DEBUG_SIZE);
83 else if ( ble_mode == BLE_SLAVER )
85 pollingRequest("AT+ROLE0", debugD, 8);
86 memset(debugD, 0, DEBUG_SIZE);
88 pollingRequest("AT+MODE2", debugD, 8);
89 memset(debugD, 0, DEBUG_SIZE);
91 pollingRequest("AT+NOTI0", debugD, 8);
92 memset(debugD, 0, DEBUG_SIZE);
95 if ( ble_mode != BLE_NOCHANGE )
97 while ( IsSelfArduino() == false )
98 this->pollingDisconnect();
104 void Cble::StatusRead( void )
106 debug_printf("StatusRead function called.\r\n");
108 pollingRequest("AT+ROLE?", debugD, 9);
109 memset(debugD, 0, DEBUG_SIZE);
111 pollingRequest("AT+MODE?", debugD, 8);
112 memset(debugD, 0, DEBUG_SIZE);
114 pollingRequest("AT+IMME?", debugD, 9);
115 memset(debugD, 0, DEBUG_SIZE);
117 pollingRequest("AT+TYPE?", debugD, 8);
118 memset(debugD, 0, DEBUG_SIZE);
120 pollingRequest("AT+POWE?", debugD, 8);
121 memset(debugD, 0, DEBUG_SIZE);
123 pollingRequest("AT+NOTI?", debugD, 8);
124 memset(debugD, 0, DEBUG_SIZE);
126 pollingRequest("AT+PIO1?", debugD, 8);
127 memset(debugD, 0, DEBUG_SIZE);
131 bool Cble::IsConnected( void )
136 bool Cble::IsSelfArduino( void )
138 #ifdef __INTERNAL_DEBUG__
139 debug_printf("IsSelfArduino is called.\r\n");
141 int length = mustHaveRequest("AT+ADDR?", debugD, 20);
143 if ( strstr(debugD, SelfMaxaddr) )
146 memset(debugD, 0, length);
152 memset(debugD, 0, length);
157 bool Cble::pollingConnect( const char *maxAddr )
159 #ifdef __INTERNAL_DEBUG__
160 debug_printf("pollingConnect is called.\r\n");
163 #define CONNECT_SIZE 20
165 char cmd[CONNECT_SIZE] = {0,};
167 // is Connected Address ?
168 pollingRequest("AT+ADDR?", debugD, 20);
169 if ( strstr(debugD, maxAddr) )
172 memset(debugD, 0, DEBUG_SIZE);
175 memset(debugD, 0, DEBUG_SIZE);
178 sprintf(cmd, "AT+CON%s", maxAddr);
179 if ( pollingRequest(cmd, debugD, 8) == false )
181 debug_printf("Error : %s command is failed.\r\n", cmd );
182 memset(debugD, 0, DEBUG_SIZE);
186 else if ( strstr(debugD, "OK+CONNA") )
189 memset(debugD, 0, DEBUG_SIZE);
193 memset(debugD, 0, DEBUG_SIZE);
197 // Confirm Connected Address.
198 pollingRequest("AT+ADDR?", debugD, 20);
200 if ( strstr(debugD, maxAddr) == 0 )
203 if ( strstr(debugD, "OK+CONNF") )
205 memset(debugD, 0, DEBUG_SIZE);
206 streamDummy(debugD, 8);
209 memset(debugD, 0, DEBUG_SIZE);
218 #define CMD_DISCONNECT "(CMD_DISCON)"
219 void Cble::pollingDisconnect( void )
221 #ifdef __INTERNAL_DEBUG__
222 debug_printf("pollingDisconnect is called.\r\n");
226 pollingRequest("\r\n" CMD_DISCONNECT "\r\n", NULL, NULL);
229 length = mustHaveRequest("AT", debugD, 2);
230 if ( strstr(debugD, "OK") )
233 memset(debugD, 0, length);
235 if ( BLE.available() )
237 if ( BLE.findUntil(CMD_DISCONNECT, "\r\n") == true )
239 debug_printf("pollingDisconnect : Detected %s command.\r\n", CMD_DISCONNECT);
243 debug_printf("pollingDisconnect : Send AT message.\r\n");
245 memset(debugD, 0, length);
247 length = mustHaveRequest("AT", debugD, 2);
249 while ( strstr(debugD, "OK") == NULL );
251 debug_printf("pollingDisconnect() is Success.\r\n");
252 memset(debugD, 0, length);
260 int Cble::pollingGetRSSI( void )
268 char rssi_res[RSSI_SIZE] = {0,};
270 streamDummy(NULL, NULL);
272 if ( isConnected == true )
274 BLE.write("AT+RSSI?");
280 if ( BLE.available() )
281 c = (char)BLE.read();
292 if ( index == RSSI_SIZE - 1 )
294 rssi_res[index] = '\0';
299 // debug_printf("time_cnt=%d\r\n", time_cnt);
300 if ( time_cnt == 0xFFFFF )
302 debug_printf("Error : Time Out GetRSSI().\r\n");
309 #ifdef __INTERNAL_DEBUG__
310 debug_printf("res= %s \r\n", rssi_res);
312 char *Srssi = &(rssi_res[RSSI_POS]);
313 // debug_printf("[S] rssi=%s\r\n", Srssi);
314 int rssi = atoi(Srssi);
315 // debug_printf("rssi=%d\r\n", rssi);
326 int Cble::mustHaveRequest(const char *request, char *data, int dataLen)
330 int required_length = dataLen;
331 char *data_index = data;
338 while ( (length = BLE.readBytes(data_index, required_length)) == 0 )
343 if ( cnt >= LIMIT_COUNT / 10 )
345 #ifdef __INTERNAL_DEBUG__
346 debug_printf("=====> Retry Request command Send. <=========\r\n");
352 required_length = dataLen;
359 required_length -= length;
360 if ( required_length != 0 )
362 data_index = data_index + length;
367 #ifdef __INTERNAL_DEBUG__
368 debug_printf("[ %s ] %s\r\n\r\n", request, data);
373 bool Cble::pollingRequest(const char *request, char *data, int dataLen)
377 int required_length = dataLen;
378 char *data_index = data;
388 while ( (length = BLE.readBytes(data_index, required_length)) == 0 )
391 if ( cnt >= LIMIT_COUNT )
393 debug_printf("[ %s ] TimeOut : No data.\r\n\r\n", request);
398 required_length -= length;
399 if ( required_length != 0 )
401 data_index = data_index + length;
406 #ifdef __INTERNAL_DEBUG__
407 debug_printf("[ %s ] %s\r\n\r\n", request, data);
413 void Cble::streamDummy( char *data, int dataLen)
417 int required_length = dataLen;
418 char *data_index = data;
420 if ( data && required_length )
423 while ( (length = BLE.readBytes(data_index, required_length)) == 0 )
426 if ( cnt >= LIMIT_COUNT )
428 debug_printf("[ streamDummy ] TimeOut : No data.\r\n\r\n");
433 required_length -= length;
434 if ( required_length != 0 )
436 data_index = data_index + length;
441 debug_printf("[ streamDummy ] %s\r\n\r\n", data);
445 if ( BLE.available() )
447 BLE.readBytes(debugD, DEBUG_SIZE);
448 #ifdef __INTERNAL_DEBUG__
449 debug_printf("[ streamDummy ] %s\r\n\r\n", debugD);
451 memset(debugD, 0, DEBUG_SIZE);
459 #define REGARD_DISCON 5000
461 unsigned long global_cnt = 0;
463 void Cble::BLE2Debug( int autoDiscon )
468 if ( BLE.available() )
470 while ( (length = BLE.readBytes(BLEData, SERIAL_SIZE)) == 0 )
473 if ( cnt >= LIMIT_COUNT )
480 Serial.println(BLEData);
481 memset(BLEData, 0, length);
483 else if ( autoDiscon )
486 // debug_printf("global_cnt=%u , ", global_cnt );
487 // debug_printf("needDiscon=%d\r\n", needDiscon);
488 if ( !needDiscon && global_cnt >= REGARD_DISCON )
491 debug_printf("result : global_cnt=%u , ", global_cnt );
492 debug_printf("needDiscon=%d\r\n", needDiscon);
497 debug_printf("Auto Discon : global_cnt=%u , ", global_cnt );
498 debug_printf("needDiscon=%d\r\n", needDiscon);
499 if ( pollingRequest("AT", debugD, 2) == true )
508 char *Cble::Debug2BLE( int BLEwrite )
512 if ( Serial.available() )
514 char c = (char)Serial.read();
522 debug_printf("\r\n");
523 if (BLEwrite && SerialCnt)
525 BLE.write(SerialData);
529 result = (char *)malloc(sizeof(char) * (SerialCnt + 1));
530 memcpy(result, SerialData, SerialCnt );
531 result[SerialCnt] = NULL;
533 memset(SerialData, 0, SerialCnt + 1);
537 SerialData[SerialCnt] = c;
547 void Cble::BLE2Debug( int autoDiscon )
552 char *Cble::Debug2BLE( int BLEwrite )
558 void Cble::FactoryReset( void )
560 debug_printf("FactoryReset is called.\r\n");
562 pollingRequest("AT+RENEW", debugD, 8);
563 memset(debugD, 0, DEBUG_SIZE);
567 void Cble::firstUartSetting( void )
569 debug_printf("firstUartSetting is called.\r\n");
576 pollingRequest("AT+BAUD4", debugD, 8);
577 memset(debugD, 0, DEBUG_SIZE);
579 debug_printf("Please Power off and on.\r\n");
583 // BLE.begin(115200);
586 // pollingRequest("AT", debugD, DEBUG_SIZE);
587 // memset(debugD, 0, DEBUG_SIZE);