This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Issues with modularising code

I am developing my first BLE Server application with the nRF52832 and using VSC/nRF Connect/Zephyr ROTS.  I come from a Cypress/PSOC Creator background, and am struggling a bit to get up to speed with the Nordic stuff.

Using the peripheral_dis example, I put together the following code.  Initially everything was stuffed into main.c, but once I had it working OK, I decided to modularise it to make it easier to read, and also so I can reuse some of the code segments more easily.

I'm having issues moving the Advertising Packet and Scan Response Packet definitions, and also the connection and disconnection call back functions and declarations into my ble_config.c and ble_config.h files.  The moment I do that, I get a bunch of compile errors.

I'm sure I'm doing something obviously wrong, but in my inexperience, I'm stuffed if I can work out what!

I've included my project files in the attached .zip  The functions/declarations I'm trying to put into ble_config.c/h are:

// Bluetooth Advertising data
static const struct bt_data ad[] = {
	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),   // Sets up advertising parameters
	BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_LPI_DEVICE),  				// Defines any Service UUIDs to include in advertising packet
};

// Bluetooth Scan Response data (initially empty)
static struct bt_data sd[] = {
    BT_DATA(BT_DATA_MANUFACTURER_DATA, NULL, BT_GAP_ADV_MAX_ADV_DATA_LEN-2),
};

static void connected(struct bt_conn *conn, uint8_t err)
{
	if (err) {
		printk("Connection failed (err 0x%02x)\n", err);
	} else {
		printk("Connected\n");
	}
}

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
	printk("Disconnected (reason 0x%02x)\n", reason);
}

BT_CONN_CB_DEFINE(conn_callbacks) = {
	.connected = connected,
	.disconnected = disconnected,
};

Can anyone point me in the right direction of how to get this to work?
Cheers,
Mike
  • Hello!

    Are you using NCS 1.9, or are you using an older version?

    And could you please share the compile error messages you are getting?

    That would make it much easier to help you.

    Best regards,

    Einar

  • Hi Einar,

    I've done some more investigations into this.  The problem seems to come when I try and move my advertising and scan response packet definitions outside my main.c

    So, with these two definitions inside main.c - everything works OK

    // Bluetooth Advertising data
    static const struct bt_data ad[] = {
    	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),   // Sets up advertising parameters
    	BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_LPI_DEVICE),  				// Defines any Service UUIDs to include in advertising packet
    };
    
    // Bluetooth Scan Response data (initially empty)
    static struct bt_data sd[] = {
        BT_DATA(BT_DATA_MANUFACTURER_DATA, NULL, BT_GAP_ADV_MAX_ADV_DATA_LEN-2),
    };

    But if I attempt to shift these into my bt_config.c and bt_config.h, things fall over.  I think its got something to do with not being allowed to have a variable length array defined in another .c file and attempt to make this visible to other .c files.

    I attempted to get around this by defining those arrays with a fixed length, using BT_GAP_ADV_MAX_ADV_DATA_LEN.  So, in bt_config.c I had:

    // Bluetooth Advertising data
    const struct bt_data ad[BT_GAP_ADV_MAX_ADV_DATA_LEN] = {
    	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),   // Sets up advertising parameters
    	BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_LPI_DEVICE),  				// Defines any Service UUIDs to include in advertising packet
    };
    
    // Bluetooth Scan Response data (initially empty)
    struct bt_data sd[BT_GAP_ADV_MAX_ADV_DATA_LEN] = {
        BT_DATA(BT_DATA_MANUFACTURER_DATA, NULL, BT_GAP_ADV_MAX_ADV_DATA_LEN-2),
    };

    And then in bt_config.h I had:

    extern const struct bt_data ad[BT_GAP_ADV_MAX_ADV_DATA_LEN];
    extern struct bt_data sd[BT_GAP_ADV_MAX_ADV_DATA_LEN];

    But whilst this would compile, it would throw up err = -22 when I tried to initialise the Bluetooth in main.c with a call to 

    err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    Regards,
    Mike
  • Right, I see.

    I believe you'll want to define the arrays in the same file that you use them in, anything else would just make things more difficult for yourself.

    I've been able to define bt_data structs outside main.c before, but then I called bt_le_adv_start in a custom bluetooth init function that was then called in main.

    That way, you don't have to worry about your structs being defined outside your bluetooth file, maybe that could work for your project?

    -Einar

  • Hi Einar,

    Thanks for the clarification.  Gave your suggest a go and it works - thanks

    - Mike.

Related