Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
JeHu_3414236
Level 5
Level 5
10 likes received First like received

When I receive the event CY_BLE_EVT_GAP_DEVICE_DISCONNECTED, I want to do another connection right away using Cy_BLE_GAPC_ConnectDevice() but it returns CY_BLE_ERROR_INVALID_OPERATION probably because I have max connections set to 1.  How long do I have to wait for the connection resources to be released before starting the next connection?  I don't receive any other BLE events after CY_BLE_EVT_GAP_DEVICE_DISCONNECTED so I can't wait for an event.  I set a timer and sometimes I have to wait 2s+ before I can try connection again.  I tried shutting off/on BLE stack and it has no effect on how long I need to wait before Cy_BLE_GAPC_ConnectDevice() will return success.

0 Likes
1 Solution

Still not clear about the purpose of power off on peripheral.

Bad RF (out of range / interfrence) may cause disconnection. Similarly, it can also cause failure of re-connection.

The current situation of this thread may similar to BLE scan response not being reported​, all visible issues only occurred under bad RF circumstances. Digging into stack may not be helpful as the stack mechanism has also been verified under good RF.

View solution in original post

0 Likes
11 Replies
ShipingW_81
Moderator
Moderator
Moderator
500 replies posted 250 solutions authored 250 replies posted

Cy_BLE_GAPC_ConnectDevice() returns CY_BLE_ERROR_INVALID_OPERATION only if the current number of active connection equals or exceeds the pre-defined max connection number.

In the code, the current connection state would be updated once the event CY_BLE_EVT_GATT_DISCONNECT_IND is received, in Cy_BLE_EventHandler(), which is always executed ahead of Cy_BLE_ApplCallback() for a same stack event.

As you put the API Cy_BLE_GAPC_ConnectDevice following event CY_BLE_EVT_GAP_DEVICE_DISCONNECTED, theoretically the connection state has already updated to disconnected before it.

I believe if you could possibly debug a little to check if the objective connection state updating process in your code?

0 Likes

I checked and Cy_BLE_GetNumOfActiveConn() returns 0 when I call Cy_BLE_GAPC_ConnectDevice().  It passes all these error checks:

    if(Cy_BLE_IsPeerConnected((uint8_t *)address->bdAddr) == true)

    {

        apiResult = CY_BLE_ERROR_INVALID_STATE;

    }

    else if(address == NULL)

    {

        apiResult = CY_BLE_ERROR_INVALID_PARAMETER;

    }

    else if(Cy_BLE_GetNumOfActiveConn() == CY_BLE_CONN_COUNT)

    {

        apiResult = CY_BLE_ERROR_INVALID_OPERATION;

    }

#if (CY_BLE_GAP_ROLE_PERIPHERAL)

    else if(((CY_BLE_CONN_COUNT - Cy_BLE_GetNumOfActiveConn()) == 1u) && (advState == CY_BLE_ADV_STATE_ADVERTISING) &&

            ((cy_ble_configPtr->discoveryModeInfo[cy_ble_advIndex].advParam->advType <

                CY_BLE_GAPP_SCANNABLE_UNDIRECTED_ADV) ||

             (cy_ble_configPtr->discoveryModeInfo[cy_ble_advIndex].advParam->advType >

                CY_BLE_GAPP_NON_CONNECTABLE_UNDIRECTED_ADV)))

    {

        apiResult = CY_BLE_ERROR_INVALID_OPERATION;

    }

#endif /* CY_BLE_GAP_ROLE_PERIPHERAL */

It is one of these 2 calls to library functions that returns the error:

        apiResult = Cy_BLE_StartTimer(&cy_ble_connectingTimeout);

        if(apiResult == CY_BLE_SUCCESS)

        {

            apiResult = Cy_BLE_GAPC_InitConnection(&cy_ble_connectionParameters);

            if(apiResult == CY_BLE_SUCCESS)

            {

                Cy_BLE_SetState(CY_BLE_STATE_CONNECTING);

            }

        }

        else

        {

            (void)Cy_BLE_StopTimer(&cy_ble_connectingTimeout);

        }

My project has CY_BLE_CONN_COUNT = 2 and I have central and peripheral modes enabled but I am only doing central operations when I see this problem.

0 Likes

According to your description, it may be not hard to locate which library function returns the error.

If it is Cy_BLE_GAPC_InitConnection(), please make sure the connection parameters you are using follows the restrictions listed in API comments.

Besides, not sure if the GAP peripheral device is ready in advertising state at the moment of calling Cy_BLE_GAPC_ConnectDevice() on central side.

Any event generated on both sides during / after calling Cy_BLE_GAPC_ConnectDevice()?

0 Likes

I am using the same parameters for Cy_BLE_GAPC_ConnectDevice() which is just the same BT address that disconnected and index.  The parameters for Cy_BLE_GAPC_InitConnection() reads the values from BLE_config which I do not change.  I do not see any events on either side after calling Cy_BLE_GAPC_ConnectDevice() which returns the error.

0 Likes
lock attach
Attachments are accessible only for community members.

I created a demo on CY8CKIT062-BLE kit board to reproduce this issue, however there is no any problem to reconnect a same device immediately after receiving event CY_BLE_EVT_GAP_DEVICE_DISCONNECTED.

Please find the attached demo code. The demo is modified on CE215118(GTAA Client), and the code for GATT Server is CE215119(no any change).

I only add a single line of calling  Cy_BLE_GAPC_ConnectDevice() under case CY_BLE_EVT_GAP_DEVICE_DISCONNECTED. You will find the re-connection process is successful once the CY_BLE_EVT_GAP_DEVICE_DISCONNECTED is received, by observing through printing UART log.

0 Likes

I used this project and there is no problem when I send the disconnect command from UART.  When it is connected and I cut power on the peripheral, then I see this problem.  If I am running in the debugger, the debugger also gets an error and quits debugging for some reason.  This is the debug log:

----------------------------------------------------------

Connected devices list:

1. address: d36453a0deda bdHandle:5 attId:1 CLIENT

----------------------------------------------------------

select device for disconnect:  (1..1):

1

Cy_BLE_GAP_Disconnect param: bdHandle:5, reason:13

Cy_BLE_GAP_Disconnect API Success

CY_BLE_EVT_GATT_DISCONNECT_IND: attId=1, bdHandle=5

CY_BLE_EVT_GAP_DEVICE_DISCONNECTED: bdHandle=5, reason=16, status=0

result of re-connect = 0

CY_BLE_EVT_GATT_CONNECT_IND: attId=1, bdHandle=5

CY_BLE_EVT_GAP_DEVICE_CONNECTED: status = 0,  connIntv = 35 ms

connected as Central connHandle 5

Send an authorization request: bdHandler 0x5

CY_BLE_EVT_GATTS_XCNHG_MTU_REQ 1, 5, final mtu= 23

CY_BLE_EVT_GAP_SMP_NEGOTIATED_AUTH_INFO: bdHandle=5, security=1, bonding=1, ekeySize=10, err=0

CY_BLE_EVT_GAP_AUTH_COMPLETE: bdHandle=5, security=1, bonding=1, ekeySize=10, err=0

CY_BLE_EVT_PENDING_FLASH_WRITE

CY_BLE_EVT_GAP_ENCRYPT_CHANGE: 0

///////////////////CUT POWER ON PERIPHERAL////////////////////////////

CY_BLE_EVT_GATT_DISCONNECT_IND: attId=1, bdHandle=5

CY_BLE_EVT_GAP_DEVICE_DISCONNECTED: bdHandle=5, reason=8, status=0

result of re-connect = 1441794

0 Likes

Did you test on DVK board, or custom board?

Did you re-power the peripheral device after disconnect event received on GATT Server side?

Attach log on GATT Client side during power off and re-power on GATT Server device:

...connected state...

CY_BLE_EVT_BASC_NOTIFICATION: Battery Level: 3 [attId:1, bdHandle:5]

///////////////////CUT POWER ON PERIPHERAL////////////////////////////

[11:11:20.703] CY_BLE_EVT_GATT_DISCONNECT_IND: attId=1, bdHandle=5

CY_BLE_EVT_GAP_DEVICE_DISCONNECTED: bdHandle=5, reason=8, status=0

result of re-connect = 0

Connecting to the device 00a050000002

CY_BLE_EVT_GAP_DEVICE_CONNECTED: status = c, connIntv = 5 ms

[11:11:26.474] CY_BLE_EVT_TIMEOUT Error

/////////////////// POWER ON ON PERIPHERAL////////////////////////////

[11:11:36.553] CY_BLE_EVT_GATT_CONNECT_IND: attId=1, bdHandle=5

CY_BLE_EVT_GAP_DEVICE_CONNECTED: status = 0, connIntv = 35 ms

connected as Central connHandle 5

Send an authorization request: bdHandler 0x5

[11:11:36.603] CY_BLE_EVT_GAP_SMP_NEGOTIATED_AUTH_INFO: bdHandle=5, security=1, bonding=0, ekeySize=10, err=0

[11:11:38.143] Discovery complete: attId=1, bdHandle=5

  service with UUID 0x1800 has range from 0x1 to 0x7

  service with UUID 0x1801 has range from 0x8 to 0xb

  service with UUID 0x180f has range from 0x13 to 0x17

  service with UUID 0x180f has range from 0x0 to 0x0

  service with UUID 0x180a has range from 0xc to 0x12

Enable notifications for BAS service [0]:

Cy_BLE_BASC_SetCharacteristicDescriptor() successful.

[11:11:38.214] BAS event: 10037,

Write Descriptor Confirm

[11:11:38.423] BAS event: 10034,

CY_BLE_EVT_BASC_NOTIFICATION: Battery Level: 3 [attId:1, bdHandle:5]

Once GATT Server is powered off, GATT Client tries reconnecting to GATT Server after receiving disconnect events, but then CY_BLE_EVT_TIMEOUT is received as no ack from GATT Server. Then GATT Client is still trying connecting. Once GATT Server is re-powered, connection is successful.

0 Likes

I am using my own board on the peripheral side which is also a PSoC 63.  I tried both keeping peripheral off and just toggling reset line and the results are the same.

0 Likes

I'd like to know this is just a corner case on your application, right?

May be we need more info over why you keep the peripheral off or toggling reset line while connected state, for power saving consideration?

0 Likes

This happens when RF packets are lost causing disconnection. I see it

easily on bad boards but can happen due to normal reasons like out of range

or interference.

0 Likes

Still not clear about the purpose of power off on peripheral.

Bad RF (out of range / interfrence) may cause disconnection. Similarly, it can also cause failure of re-connection.

The current situation of this thread may similar to BLE scan response not being reported​, all visible issues only occurred under bad RF circumstances. Digging into stack may not be helpful as the stack mechanism has also been verified under good RF.

0 Likes