RFCOMM example

Rocker
Posts: 24
Joined: Fri Apr 21, 2017 10:30 pm

RFCOMM example

Postby Rocker » Fri Apr 21, 2017 10:34 pm

I need to connect to a device using RFCOMM - so search for it, connect/pair, start RFCOMM session. I see the rfcomm folder under components/bt/bluedroid, but an example would be wonderful. Has anyone been able to get this to work or even pieces of it to work? Have a code example?

Thanks, Rob

ursfassler
Posts: 3
Joined: Tue May 09, 2017 8:39 pm

Re: RFCOMM example

Postby ursfassler » Tue May 09, 2017 8:42 pm

I am also looking for an rfcomm example.

Meanwhile I try it on my own. I am able to call RFCOMM_CreateConnection on the esp. Unfortunately I can not connect from another device to the ESP32, not sure where the problem is.

Rocker
Posts: 24
Joined: Fri Apr 21, 2017 10:30 pm

Re: RFCOMM example

Postby Rocker » Thu May 18, 2017 9:41 pm

I'm on v2.1 of the esp-idf. I think I can detect and pair (bond) with the device - I'll put code up if it would be helpful.

I tried calling RFCOMM_CreateConnection, but I get a link error. I looked at the component.mk file under components/bt/. It doesn't list the rfcomm files.

How'd you get this to even compile/link?

ursfassler
Posts: 3
Joined: Tue May 09, 2017 8:39 pm

Re: RFCOMM example

Postby ursfassler » Sun May 21, 2017 8:31 am

I added the rfcomm directory in the file component/bt/component.mk and fixed the compile errors.
Have a look here: https://github.com/ursfassler/esp-idf, branch rfcomm

Rocker
Posts: 24
Joined: Fri Apr 21, 2017 10:30 pm

Re: RFCOMM example

Postby Rocker » Fri May 26, 2017 3:32 pm

I also added the directories and fixed the compile errors/warnings. I turned debug logging up. Here's the relevant code for what I'm doing:

Code: Select all

static void esp_inquiry_complete_cb (void *p1)
{
    tBTM_STATUS funcRet;
    uint32_t trustedMask[2] = {BTM_SEC_TRUST_ALL, BTM_SEC_TRUST_ALL};
    switch (state)
    {
        case (0):
            printf("Inquiry complete\n");
            state = 1;
            funcRet = BTM_ReadRemoteDeviceName(bddr, esp_inquiry_complete_cb, BT_TRANSPORT_INVALID);
            printf("BTM_ReadRemoteDeviceName funcRet = %d\n", (int) funcRet);
            break;
        case (1):
        {
            tBTM_REMOTE_DEV_NAME* pDeviceName = (tBTM_REMOTE_DEV_NAME*) p1;
            printf("Device Name = %s, status = %d\n", (char*) pDeviceName->remote_bd_name, pDeviceName->status);
            BTM_SetTraceLevel(BT_TRACE_LEVEL_DEBUG);
            if (!BTM_SetSecurityLevel(TRUE, // originator
                                    "EEG_Rx", // name
                                    BTM_COD_MAJOR_HEALTH, // service ID
                                    BTM_SEC_MODE4_LEVEL4 | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHENTICATE, // security level
                                    BTM_SEC_PROTO_RFCOMM, // psm
                                    0, // mx proto ID
                                    0 // mx channel ID
                                    ))
            {
                printf("BTM_SetSecurityLevel returned false\n");
            }
            funcRet = BTM_SecBond(bddr, 4, devicePIN, trustedMask);
            printf("BTM_SecBond funcRet = %d\n", (int) funcRet);
        }
            break;
        case (3):
            // BTM_SecBond
            break;
        case (4):
            // BTM_AllocateSCN
            break;
    }
}

void rfCommCb (UINT32 code, UINT16 port_handle)
{
    printf("RFCOMM_Callback: code = %d, port_handle = %d\n", code, port_handle);
    if (PORT_EV_RXCHAR == code)
    {
        btDataAvailable = 1;
    }
    else if (PORT_EV_CONNECTED == code)
    {
        btConnected = 1;
    }
}

UINT8 espAuthCompleteCb (BD_ADDR bd_addr, DEV_CLASS dev_class,
        tBTM_BD_NAME bd_name, int result)
{
    int funcRet = 0;
    printf("Authentication complete: Remote Address %02x:%02x:%02x:%02x:%02x:%02x\n  result = %d\n", 
                                        BT_BD_ADDR_HEX(bd_addr),
                                        result);
    RFCOMM_Init();
    PORT_SetTraceLevel(BT_TRACE_LEVEL_DEBUG);
    funcRet = RFCOMM_CreateConnection(UUID_SERVCLASS_SERIAL_PORT, // taken from port_api.c
                                      1, // scn
                                      FALSE, // is_server
                                      600, // max MTU
                                      bddr, // address
                                      &rfCommHandle, // output handle
                                      rfCommCb);
    printf("RFCOMM_CreateConnection returned %d, rfCommHandle = %d\n", funcRet, rfCommHandle);
    funcRet = PORT_SetEventMask(rfCommHandle, PORT_MASK_ALL);
    printf("Port_SetEventMask returned %d\n", funcRet);
    
    return 0; // no documentation on return value, calling code doesn't seem to use
}
with the associated log starting at the set security level log:

Code: Select all

E (15072) BT: btm_sec_set_security_level : sec: 0x70

E (15072) BT: BTM_SEC_REG[3]: id 9, is_orig 1, psm 0x0003, proto_id 0, chan_id 0

E (15072) BT:                : sec: 0x20f0, service name [EEG_Rx] (up to 21 chars saved)

E (15082) BT: BTM_InqDbRead: bd addr [000780aeab65]

E (15082) BT: btm_find_dev_type - device_type = 1 addr_type = 0
E (15092) BT: btm_sec_bond_by_transport BDA: 00:07:80:ae:ab:65

E (15092) BT: btm_sec_bond_by_transport: Transport used 1

E (15102) BT: btm_find_or_alloc_dev

E (15102) BT: before update sec_flags=0x80

E (15112) BT: BTM: BTM_DeleteStoredLinkKey: delete_all_flag: FALSE
E (15112) BT: after update sec_flags=0x80

E (15122) BT:   remote_features page[0] = 00-00-00-00

E (15122) BT:                               00-00-00-00

E (15132) BT:   remote_features page[1] = 00-00-00-00

E (15132) BT:                               00-00-00-00

E (15142) BT:   remote_features page[2] = 00-00-00-00

E (15142) BT:                               00-00-00-00

E (15152) BT: BTM_SecBond: Remote sm4: 0x11  HCI Handle: 0xffff

E (15152) BT: sec mode: 4 sm4:x11

E (15162) BT: BTM_InqDbRead: bd addr [000780aeab65]

E (15162) BT: btm_find_dev_type - device_type = 1 addr_type = 0
E (15172) BT: BTM_InqDbRead: bd addr [000780aeab65]

E (15172) BT: btm_acl_paging discing:0, paging:1 BDA: 000780aeab65

E (15182) BT: connecting_bda: 000780aeab65

E (15182) BT: btm_find_or_alloc_dev

E (15182) BT: btm_acl_update_busy_level

E (15192) BT: BTM_BLI_PAGE_EVT

E (15192) BT: btm_acl_update_busy_level

E (15192) BT: BTM_BLI_PAGE_EVT

E (15202) BT: Security Manager: btm_sec_dd_create_conn [000780aeab65]

BTM_SecBond funcRet = 1
E (15212) BT: btm_sec_rmt_name_request_complete

E (15212) BT: btm_acl_resubmit_page

E (15212) BT: setting BTM_SEC_NAME_KNOWN sec_flags:0x88

PORT_IsOpening returned 0
PORT_CheckConnection returned 9, lcid = 0
E (17632) BT: btm_acl_resubmit_page

E (17632) BT: btm_sec_connected

E (17632) BT: btm_acl_created hci_handle=128 link_role=1  transport=1

E (17632) BT: device_type=0x1

E (17632) BT: btm_read_remote_features() handle: 128

E (17632) BT: btm_handle_to_acl_index

E (17642) BT: is_originator:1 

E (17642) BT: btm_sec_execute_procedure: Required:0x10 Flags:0x88 State:0

E (17652) BT: Security Manager: Start authentication

E (17652) BT: btm_acl_update_busy_level

E (17662) BT: BTM_BLI_PAGE_DONE_EVT

E (17662) BT: btm_acl_created hci_handle=128 link_role=0  transport=1

E (17672) BT: btm_bda_to_acl found

E (17672) BT: Duplicate btm_acl_created: RemBdAddr: 000780aeab65

E (17682) BT: BTM_SetLinkPolicy

E (17682) BT: btm_bda_to_acl found

E (17682) BT: btm_bda_to_acl found

E (17692) BT: BTM_SetLinkSuperTout

E (17692) BT: btm_read_remote_version_complete

E (17692) BT: btm_read_remote_features_complete

E (17702) BT: btm_handle_to_acl_index

E (17702) BT: Start reading remote extended features

E (17712) BT: btm_read_remote_ext_features() handle: 128 page: 1

E (17712) BT: btm_process_clk_off_comp_evt

E (17722) BT: btm_handle_to_acl_index

E (17722) BT: BTM_InqDbRead: bd addr [000780aeab65]

E (17722) BT: btm_find_or_alloc_dev

E (17732) BT: btm_sec_link_key_request()  BDA: 00:07:80:ae:ab:65

E (17742) BT: btm_find_or_alloc_dev

E (17742) BT: btm_io_capabilities_req:Security mode: 4, Num Read Remote Feat pages: 0

E (17792) BT: btm_find_or_alloc_dev

E (18202) BT: btm_proc_sp_req_evt()  just_works:1, io loc:3, rmt:3, auth loc:3, rmt:2

E (18202) BT: calling BTM_ConfirmReqReply with status: 0

PORT_IsOpening returned 0
PORT_CheckConnection returned 9, lcid = 0
E (20692) BT: btm_find_or_alloc_dev

E (20692) BT: btm_sec_link_key_notification()  BDA:000780aeab65, TYPE: 4

Authentication complete: Remote Address 00:07:80:ae:ab:65
  result = 0
E (20702) BT: port_find_mcb: not found, bd_addr:00:07:80:ae:ab:65
E (20702) BT: RFCOMM_CreateConnection()  BDA: 00-07-80-ae-ab-65
E (20702) BT: RFCOMM_CreateConnection(): scn:1, dlci:2, is_server:0 mtu:600, p_mcb:0x0
E (20712) BT: rfc_cb.port.port[1]:0x3ffc83e0 allocated, last_port:1
E (20722) BT: port_allocate_port:bd_addr:00:07:80:ae:ab:65
E (20722) BT: RFCOMM_CreateConnection(): scn:1, dlci:2, is_server:0 mtu:600, p_mcb:0x0, p_port:0x3ffc830
E (20732) BT: RFCOMM_CreateConnection dlci:2 signal state:0xb
E (20742) BT: port_open_continue, p_port:0x3ffc83e0
E (20742) BT: rfc_alloc_multiplexer_channel: bd_addr:00:07:80:ae:ab:65
E (20752) BT: rfc_alloc_multiplexer_channel:is_initiator:1
E (20752) BT: rfc_alloc_multiplexer_channel rfc_cb.port.rfc_mcb[0].state:0
E (20762) BT: (rfc_cb.port.rfc_mcb[i].bd_addr:00:00:00:00:00:00
E (20762) BT: rfc_alloc_multiplexer_channel rfc_cb.port.rfc_mcb[1].state:0
E (20772) BT: (rfc_cb.port.rfc_mcb[i].bd_addr:00:00:00:00:00:00
E (20782) BT: rfc_alloc_multiplexer_channel rfc_cb.port.rfc_mcb[2].state:0
E (20782) BT: (rfc_cb.port.rfc_mcb[i].bd_addr:00:00:00:00:00:00
E (20792) BT: rfc_alloc_multiplexer_channel:is_initiator:1, create new p_mcb:0x3ffc8e6c, index:0
E (20802) BT: rfc_timer_start - timeout:60
E (20802) BT: port_select_mtu application selected 600
E (20812) BT: port_select_mtu credit_rx_max 10, credit_rx_low 4, rx_buf_critical 15
E (20812) BT: rfc_mx_sm_state_idle - evt:6
E (20822) BT: BTM_SetPowerMode: pm_id 128 BDA: 80aeab65 mode:0x0
E (20822) BT: BTM_SetPowerMode: mode:0x0 interval 0 max:0, min:0
E (20832) BT: btm_sec_l2cap_access_req() is_originator:1, 0x3ffc7168

E (20842) BT: btm_find_or_alloc_dev

E (20842) BT: btm_sec_l2cap_access_req: security_required 0x20f0, is_originator 0x01, psm  0x0003

E (20852) BT: btm_sec_l2cap_access_req()  sm4:0x11, sec_flags:0x40ba, security_required:0x20f0 chk:0

E (20862) BT: no next_serv sm4:0x11, chk:0

E (20862) BT: btm_sec_l2cap_access_req: sec_flags:0x4088
E (20872) BT: btm_sec_l2cap_access_req() PSM:3 Handle:128 State:1 Flags: 0x4088 Required: 0x20f0 Servic9

E (20882) BT: btm_sec_execute_procedure: Required:0x20f0 Flags:0x4088 State:1

RFCOMM_CreateConnection returned 0, rfCommHandle = 2
E (20892) BT: PORT_SetEventMask() handle:2 mask:0x3ffff
Port_SetEventMask returned 0
E (20892) BT: TRYING TO DECIDE IF CAN USE SMP_BR_CHNL

E (20902) BT: btm_read_remote_ext_features_complete

E (20902) BT: btm_handle_to_acl_index

E (20912) BT: BTM reached last remote extended features page (1)

E (20912) BT: btm_process_remote_ext_features

E (20922) BT: btm_sec_set_peer_sec_caps: sm4: 0x11, rmt_support_for_secure_connections 0

E (20932) BT: btm_process_remote_ext_features: pend:0

E (20932) BT: btm_establish_continue

E (20932) BT: btm_set_packet_types

E (20942) BT: SetPacketType Mask -> 0xcc18

E (20942) BT: BTM_SetLinkPolicy

E (20942) BT: btm_bda_to_acl found

E (20952) BT: btm_acl_update_busy_level

E (20952) BT: BTM_BLI_ACL_UP_EVT

E (20952) BT: btm_bda_to_acl found

E (20962) BT: BTM_ReadRemoteFeatures

PORT_IsOpening returned 1
E (25432) BT: PORT_CheckConnection() handle:2
PORT_CheckConnection returned 11, lcid = 0
PORT_IsOpening returned 1
E (30432) BT: PORT_CheckConnection() handle:2
PORT_CheckConnection returned 11, lcid = 0
Unfortunately, I have no idea what this is supposed to look like to see where it is going wrong.

eliaskardel
Posts: 3
Joined: Wed May 31, 2017 2:54 pm

Re: RFCOMM example

Postby eliaskardel » Wed Jun 07, 2017 12:57 pm

Did you got this to work?

Rocker
Posts: 24
Joined: Fri Apr 21, 2017 10:30 pm

Re: RFCOMM example

Postby Rocker » Fri Jun 09, 2017 10:37 pm

No. AFAICT, the RFCOMM aspect of the BT library does not work. See this thread:

viewtopic.php?f=18&t=1928&p=9328&hilit=rfcomm#p9328

After reading that, it made sense why the RFCOMM directories weren't included in the library build - they don't work.

I followed up asking when they think it will be implemented. I have not heard a reply. This is critically important in a product we are trying to get ready for later this year.

eliaskardel
Posts: 3
Joined: Wed May 31, 2017 2:54 pm

Re: RFCOMM example

Postby eliaskardel » Tue Jun 13, 2017 9:35 am

could you post/link the complete code of your example? I am working on the same profile. Maybe we could tackle it together?
elias.kardel@u-blox.com

SW_Adnan
Posts: 4
Joined: Wed May 10, 2017 10:55 am

Re: RFCOMM example

Postby SW_Adnan » Wed Aug 02, 2017 3:17 pm

Hi,
We are trying to add SPP support for a product. At this point I have included the RFCOMM directory but can't make it work. Has anyone managed to connect to a device using RFCOMM? Any help would be appreciated.
Cheers

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 139 guests