• Alright, i've made some little progress :

    I know understand parts of the out of bounds error that i got from my last attempt. In this file, in the function Hci.prototype.writeAclDataPkt, we allocate a buffer named first. The size of this buffer is 5 + aclLength, aclLength is the minimum between l2capLength and this._aclBuffers.length. For some reason, the later is 0 in my use case, which means that aclLength is set with the value 0 and finally the buffer is allocated with a size of 5.

    Few lines under that, line 487 and 488, we try to set some data on the buffer at indexes 5 & 7, outside of the buffer range.

    Hci.prototype.writeAclDataPkt = function (handle, cid, data) {
      const l2capLength = 4 + data.length; // data length is 3 therefore l2capLength is 7
    
      const aclLength = Math.min(l2capLength, this._aclBuffers.length); // this._aclBuffers.length is 0, which means that aclLength is 0 as well 
    
      const first = Buffer.alloc(aclLength + 5); // buffer is allocated with a length of 5
    
      // acl header
      first.writeUInt8(HCI_ACLDATA_PKT, 0);
      first.writeUInt16LE(handle | ACL_START_NO_FLUSH << 12, 1);
      first.writeUInt16LE(aclLength, 3);
    
      // l2cap header
      first.writeUInt16LE(data.length, 5); // OUT_OF_BOUNDS error
      first.writeUInt16LE(cid, 7); // OUT_OF_BOUNDS error
    
      data.copy(first, 9);
      data = data.slice(first.length - 9);
    ...
    

    I compared this code snippet with the "old" repository of Noble (this file) and changed the code a little bit to reflect the old version.

    Hci.prototype.writeAclDataPkt = function (handle, cid, data) {
      var pkt = new Buffer(9 + data.length);
    
      pkt.writeUInt8(HCI_ACLDATA_PKT, 0);
      pkt.writeUInt16LE(handle | ACL_START_NO_FLUSH << 12, 1);
      pkt.writeUInt16LE(data.length + 4, 3);
      pkt.writeUInt16LE(data.length, 5);
      pkt.writeUInt16LE(cid, 7);
    
      data.copy(first, 9);
      data = data.slice(first.length - 9);
    ...
    

    I also changed the line 497 :

    // from 
    const fragAclLength = Math.min(data.length, this._aclBuffers.length);
    // to
    const fragAclLength = data.length;
    

    Somehow it "worked" a little bit : the watch received the connection (the screen lights up and the BT logo turns blue) and the EspruinoTools logs says :

    ...
    Noble: Stopping scan (openSerial)
    BT> Connecting
    BT> Connected
    

    Then, after a timeout of a few seconds, i got an error message saying :

    Unable to connect!
    

    This message comes from the espruino-cli.js, line 530.

    I have the feeling that the code i've changed is not enough and that the acl packet is not correctly sent to the watch. Also, it looks like this._aclBuffers.length is not supposed to be 0.

    Does anyone has any idea ? Should i ask on the abandonware/noble github instead ?

About

Avatar for Gouwi @Gouwi started