adafruit / Adafruit_BluefruitLE_nRF51

Arduino library for nRF51822-based Adafruit Bluefruit LE modules
197 stars 122 forks source link

ble.echo(false) disrupts AT+FACTORYRESET and readline_parseInt() #53

Open jennydaman opened 4 years ago

jennydaman commented 4 years ago

readline.parseInt()

gatt.addService and gatt.addChraracteristic always returns 0

sketch

ble.begin(true);
ble.factoryReset();
ble.info();

uint8_t SERVICE_UUID[16] = {0xAC, 0xE6, 0xE3, 0xE3, 0x45, 0x49, 0x4B, 0x5C,
                            0x86, 0x11, 0x6C, 0xA6, 0x26, 0x5E, 0x34, 0x0A};
uint8_t service = gatt.addService(SERVICE_UUID);
Serial.print("the service I created is: ");
Serial.println(service);

output

ATZ

<- ATZ
OK
AT+FACTORYRESET

<- AT+FACTORYRESET
OK
----------------
⸮ATI
BLESPIFRIEND
nRF51822 QFACA00
B59E5C9C8B926082
0.8.1
0.8.1
Apr 10 2019
S110 8.0.0, 0.2
----------------
AT+GATTADDSERVICE=UUID128=AC-E6-E3-E3-45-49-4B-5C-86-11-6C-A6-26-5E-34-0A

<- AT+GATTADDSERVICE=UUID128=AC-E6-E3-E3-45-49-4B-5C-86-11-6C-A6-26
<- -5E-34-0A
1
OK
the service I created is: 0

expected

service is 0, even though the command is successful which I know to be true because OK is printed out, also I can find the service with the Bluefruit LE Connect for Android and even react-native-ble-plx.

Same thing for addCharacteristic. While both addService and addCharacteristic are able to execute the command successfully, neither are able to read and return the ID.

trace

https://github.com/adafruit/Adafruit_BluefruitLE_nRF51/blob/d7069a3aa160de74d2b7f32afa78f27d7f5f2891/Adafruit_ATParser.cpp#L203

To readline_parseInt() I added some print statements:

int32_t Adafruit_ATParser::readline_parseInt(void)
{
  uint16_t len = readline();
  if (len == 0) return 0;
  Serial.println("I am here");
  Serial.print("len=");
  Serial.println(len);
  Serial.print("buffer=");
  Serial.println(buffer);

  // also parsed hex number e.g 0xADAF
  int32_t val = strtol(buffer, NULL, 0);
  Serial.print("val=");
  Serial.println(val);

  return val;
}

output:

I am here
len=64
buffer=AT+GATTADDSERVICE=UUID128=AC-E6-E3-E3-45-49-4B-5C-86-11-6C-A6-26
val=0
the service I created is: 0

The content of buffer is wrong. I think it is the next line below where the Bluefruit is echoing the ID (what we want) of the newly created service/characteristic).

readline_parseInt() is being called from send_arg_get_resp.

https://github.com/adafruit/Adafruit_BluefruitLE_nRF51/blob/d7069a3aa160de74d2b7f32afa78f27d7f5f2891/Adafruit_ATParser.cpp#L140

Here I am repeating readline_parseInt() a couple of times to move the buffer to the correct line.

  // parse integer response if required
  if (reply)
  {
    if (_verbose) SerialDebug.print( F("\n<- ") );
    readline_parseInt();
    readline_parseInt();
    (*reply) = readline_parseInt();
  }

Two repeats of readline_parseInt() are needed because the command echo is broken over two lines. Output:

I am here
len=64
buffer=AT+GATTADDSERVICE=UUID128=AC-E6-E3-E3-45-49-4B-5C-86-11-6C-A6-26
val=0
I am here
len=9
buffer=-5E-34-0A
val=-5
I am here
len=1
buffer=1
val=1
the service I created is: 1

ble.echo(false)

readline_parseInt() works as expected if the function is preceeded by ble.echo(false). However, ble.echo(false) is involved in a second bug I found.

ble.factoryReset()

ble.echo(false);
if (!ble.factoryReset()) {
  Serial.println("!!!failed!!!");
}

AT+FACTORYRESET produces unusual output (but still includes "OK") if it is preceeded with ble.echo(false). output:

<- ⸮OK
⸮

ble.factoryReset() will return false.

Workaround

Order is important.

ble.begin(true);
ble.echo(true);
ble.factoryReset();
ble.echo(false);

After those four commands, we did a factory reset and the functions addService and addCharacteristic will work as expected.