Skip to content
Permalink
Browse files

Bluetooth: Use BT_GATT_SERVICE_DEFINE whenever possible

This makes use of BT_GATT_SERVICE_DEFINE to statically define services
for services that are not required to be dynamically registered.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
  • Loading branch information...
Vudentz authored and jhedberg committed Apr 16, 2019
1 parent b3a6da6 commit c42707c2d3fea5b2524b345cc9909263532c0cc8
@@ -557,7 +557,7 @@ static ssize_t write_connectable(struct bt_conn *conn,
}

/* Eddystone Configuration Service Declaration */
static struct bt_gatt_attr eds_attrs[] = {
BT_GATT_SERVICE_DEFINE(eds_svc,
BT_GATT_PRIMARY_SERVICE(&eds_uuid),
/* Capabilities: Readable only when unlocked. Never writable. */
BT_GATT_CHARACTERISTIC(&eds_caps_uuid.uuid, BT_GATT_CHRC_READ,
@@ -615,9 +615,7 @@ static struct bt_gatt_attr eds_attrs[] = {
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
read_connectable, write_connectable, NULL),
};

static struct bt_gatt_service eds_svc = BT_GATT_SERVICE(eds_attrs);
);

static void bt_ready(int err)
{
@@ -628,8 +626,6 @@ static void bt_ready(int err)

printk("Bluetooth initialized\n");

bt_gatt_service_register(&eds_svc);

/* Start advertising */
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
if (err) {
@@ -42,19 +42,16 @@ static ssize_t read_blvl(struct bt_conn *conn, const struct bt_gatt_attr *attr,
}

/* Battery Service Declaration */
static struct bt_gatt_attr attrs[] = {
BT_GATT_SERVICE_DEFINE(bas_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_BAS),
BT_GATT_CHARACTERISTIC(BT_UUID_BAS_BATTERY_LEVEL,
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_READ, read_blvl, NULL, &battery),
BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed),
};

static struct bt_gatt_service bas_svc = BT_GATT_SERVICE(attrs);
);

void bas_init(void)
{
bt_gatt_service_register(&bas_svc);
}

void bas_notify(void)
@@ -69,5 +66,5 @@ void bas_notify(void)
battery = 100U;
}

bt_gatt_notify(NULL, &attrs[1], &battery, sizeof(battery));
bt_gatt_notify(NULL, &bas_svc.attrs[1], &battery, sizeof(battery));
}
@@ -57,16 +57,14 @@ static ssize_t write_ct(struct bt_conn *conn, const struct bt_gatt_attr *attr,
}

/* Current Time Service Declaration */
static struct bt_gatt_attr attrs[] = {
BT_GATT_SERVICE_DEFINE(cts_cvs,
BT_GATT_PRIMARY_SERVICE(BT_UUID_CTS),
BT_GATT_CHARACTERISTIC(BT_UUID_CTS_CURRENT_TIME, BT_GATT_CHRC_READ |
BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
read_ct, write_ct, ct),
BT_GATT_CCC(ct_ccc_cfg, ct_ccc_cfg_changed),
};

static struct bt_gatt_service cts_svc = BT_GATT_SERVICE(attrs);
);

static void generate_current_time(u8_t *buf)
{
@@ -99,8 +97,6 @@ void cts_init(void)
{
/* Simulate current time for Current Time Service */
generate_current_time(ct);

bt_gatt_service_register(&cts_svc);
}

void cts_notify(void)
@@ -110,5 +106,5 @@ void cts_notify(void)
}

ct_update = 0U;
bt_gatt_notify(NULL, &attrs[1], &ct, sizeof(ct));
bt_gatt_notify(NULL, &cts_cvs.attrs[1], &ct, sizeof(ct));
}
@@ -141,7 +141,7 @@ static ssize_t write_ctrl_point(struct bt_conn *conn,
}

/* HID Service Declaration */
static struct bt_gatt_attr attrs[] = {
BT_GATT_SERVICE_DEFINE(hog_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_HIDS),
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_INFO, BT_GATT_CHRC_READ,
BT_GATT_PERM_READ, read_info, NULL, &info),
@@ -158,11 +158,8 @@ static struct bt_gatt_attr attrs[] = {
BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_WRITE,
NULL, write_ctrl_point, &ctrl_point),
};

static struct bt_gatt_service hog_svc = BT_GATT_SERVICE(attrs);
);

void hog_init(void)
{
bt_gatt_service_register(&hog_svc);
}
@@ -41,7 +41,7 @@ static ssize_t read_blsc(struct bt_conn *conn, const struct bt_gatt_attr *attr,
}

/* Heart Rate Service Declaration */
static struct bt_gatt_attr attrs[] = {
BT_GATT_SERVICE_DEFINE(hrs_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_HRS),
BT_GATT_CHARACTERISTIC(BT_UUID_HRS_MEASUREMENT, BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_NONE, NULL, NULL, NULL),
@@ -50,15 +50,11 @@ static struct bt_gatt_attr attrs[] = {
BT_GATT_PERM_READ, read_blsc, NULL, NULL),
BT_GATT_CHARACTERISTIC(BT_UUID_HRS_CONTROL_POINT, BT_GATT_CHRC_WRITE,
BT_GATT_PERM_NONE, NULL, NULL, NULL),
};

static struct bt_gatt_service hrs_svc = BT_GATT_SERVICE(attrs);
);

void hrs_init(u8_t blsc)
{
hrs_blsc = blsc;

bt_gatt_service_register(&hrs_svc);
}

void hrs_notify(void)
@@ -78,5 +74,5 @@ void hrs_notify(void)
hrm[0] = 0x06; /* uint8, sensor contact */
hrm[1] = heartrate;

bt_gatt_notify(NULL, &attrs[1], &hrm, sizeof(hrm));
bt_gatt_notify(NULL, &hrs_svc.attrs[1], &hrm, sizeof(hrm));
}
@@ -24,12 +24,10 @@

#define UNKNOWN_APPEARANCE 0x0000

static struct bt_gatt_attr attrs[] = {
BT_GATT_SERVICE_DEFINE(ipss_svc,
/* IP Support Service Declaration */
BT_GATT_PRIMARY_SERVICE(BT_UUID_IPSS),
};

static struct bt_gatt_service ipss_svc = BT_GATT_SERVICE(attrs);
);

static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
@@ -57,8 +55,6 @@ static struct bt_conn_cb conn_callbacks = {

void ipss_init(void)
{
bt_gatt_service_register(&ipss_svc);

bt_conn_cb_register(&conn_callbacks);
}

@@ -187,8 +187,7 @@ static ssize_t write_without_rsp_vnd(struct bt_conn *conn,
}

/* Vendor Primary Service Declaration */
static struct bt_gatt_attr vnd_attrs[] = {
/* Vendor Primary Service Declaration */
BT_GATT_SERVICE_DEFINE(vnd_svc,
BT_GATT_PRIMARY_SERVICE(&vnd_uuid),
BT_GATT_CHARACTERISTIC(&vnd_enc_uuid.uuid,
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE |
@@ -216,9 +215,7 @@ static struct bt_gatt_attr vnd_attrs[] = {
BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_WRITE, NULL,
write_without_rsp_vnd, &vnd_value),
};

static struct bt_gatt_service vnd_svc = BT_GATT_SERVICE(vnd_attrs);
);

static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
@@ -258,9 +255,7 @@ static void bt_ready(int err)
printk("Bluetooth initialized\n");

hrs_init(0x01);
bas_init();
cts_init();
bt_gatt_service_register(&vnd_svc);

if (IS_ENABLED(CONFIG_SETTINGS)) {
settings_load();
@@ -333,7 +328,7 @@ void main(void)
continue;
}

ind_params.attr = &vnd_attrs[2];
ind_params.attr = &vnd_svc.attrs[2];
ind_params.func = indicate_cb;
ind_params.data = &indicating;
ind_params.len = sizeof(indicating);
@@ -201,7 +201,7 @@ static ssize_t write_ctrl_point(struct bt_conn *conn,
return len;
}

static struct bt_gatt_attr csc_attrs[] = {
BT_GATT_SERVICE_DEFINE(csc_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_CSC),
BT_GATT_CHARACTERISTIC(BT_UUID_CSC_MEASUREMENT, BT_GATT_CHRC_NOTIFY,
0x00, NULL, NULL, NULL),
@@ -216,9 +216,7 @@ static struct bt_gatt_attr csc_attrs[] = {
BT_GATT_PERM_WRITE, NULL, write_ctrl_point,
&sensor_location),
BT_GATT_CCC(ctrl_point_ccc_cfg, ctrl_point_ccc_cfg_changed),
};

static struct bt_gatt_service csc_svc = BT_GATT_SERVICE(csc_attrs);
);

struct sc_ctrl_point_ind {
u8_t op;
@@ -243,7 +241,7 @@ static void ctrl_point_ind(struct bt_conn *conn, u8_t req_op, u8_t status,
memcpy(ind->data, data, data_len);
}

bt_gatt_notify(conn, &csc_attrs[8], buf, sizeof(buf));
bt_gatt_notify(conn, &csc_svc.attrs[8], buf, sizeof(buf));
}

struct csc_measurement_nfy {
@@ -296,7 +294,7 @@ static void measurement_nfy(struct bt_conn *conn, u32_t cwr, u16_t lwet,
memcpy(nfy->data + len, &data, sizeof(data));
}

bt_gatt_notify(NULL, &csc_attrs[1], buf, sizeof(buf));
bt_gatt_notify(NULL, &csc_svc.attrs[1], buf, sizeof(buf));
}

static u16_t lwet; /* Last Wheel Event Time */
@@ -378,7 +376,6 @@ static void bt_ready(int err)
printk("Bluetooth initialized\n");

bas_init();
bt_gatt_service_register(&csc_svc);

err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
if (err) {
@@ -284,7 +284,7 @@ static void update_temperature(struct bt_conn *conn,
}
}

static struct bt_gatt_attr ess_attrs[] = {
BT_GATT_SERVICE_DEFINE(ess_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_ESS),

/* Temperature Sensor 1 */
@@ -324,9 +324,7 @@ static struct bt_gatt_attr ess_attrs[] = {
BT_GATT_CUD(SENSOR_3_NAME, BT_GATT_PERM_READ),
BT_GATT_DESCRIPTOR(BT_UUID_ES_MEASUREMENT, BT_GATT_PERM_READ,
read_es_measurement, NULL, &sensor_3.meas),
};

static struct bt_gatt_service ess_svc = BT_GATT_SERVICE(ess_attrs);
);

static void ess_simulate(void)
{
@@ -335,12 +333,12 @@ static void ess_simulate(void)

if (!(i % SENSOR_1_UPDATE_IVAL)) {
val = 1200 + i;
update_temperature(NULL, &ess_attrs[2], val, &sensor_1);
update_temperature(NULL, &ess_svc.attrs[2], val, &sensor_1);
}

if (!(i % SENSOR_2_UPDATE_IVAL)) {
val = 1800 + i;
update_temperature(NULL, &ess_attrs[9], val, &sensor_2);
update_temperature(NULL, &ess_svc.attrs[9], val, &sensor_2);
}

if (!(i % SENSOR_3_UPDATE_IVAL)) {
@@ -389,7 +387,6 @@ static void bt_ready(int err)

printk("Bluetooth initialized\n");

bt_gatt_service_register(&ess_svc);
bas_init();

err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
@@ -46,7 +46,7 @@ static ssize_t read_u32(struct bt_conn *conn, const struct bt_gatt_attr *attr,
sizeof(value));
}

static struct bt_gatt_attr attrs[] = {
BT_GATT_SERVICE_DEFINE(env_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_ESS),

BT_GATT_CHARACTERISTIC(BT_UUID_TEMPERATURE, BT_GATT_CHRC_READ,
@@ -62,9 +62,7 @@ static struct bt_gatt_attr attrs[] = {
BT_GATT_PERM_READ, read_u32, NULL,
&pressure_value),
BT_GATT_CUD(PRESSURE_CUD, BT_GATT_PERM_READ),
};

static struct bt_gatt_service env_svc = BT_GATT_SERVICE(attrs);
);

static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
@@ -77,8 +75,6 @@ static void bt_ready(int err)
return;
}

bt_gatt_service_register(&env_svc);

err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
if (err) {
printk("Advertising failed to start (err %d)\n", err);
@@ -509,15 +509,13 @@ static void pong_ccc_cfg_changed(const struct bt_gatt_attr *attr, u16_t val)
}
}

static struct bt_gatt_attr pong_attrs[] = {
BT_GATT_SERVICE_DEFINE(pong_svc,
/* Vendor Primary Service Declaration */
BT_GATT_PRIMARY_SERVICE(&pong_svc_uuid.uuid),
BT_GATT_CHARACTERISTIC(&pong_chr_uuid.uuid, BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_NONE, NULL, NULL, NULL),
BT_GATT_CCC(pong_ccc_cfg, pong_ccc_cfg_changed),
};

static struct bt_gatt_service pong_svc = BT_GATT_SERVICE(pong_attrs);
);

void ble_init(void)
{
@@ -533,7 +531,5 @@ void ble_init(void)

bt_conn_cb_register(&conn_callbacks);


local_attr = &pong_attrs[1];
bt_gatt_service_register(&pong_svc);
local_attr = &pong_svc.attrs[1];
}
@@ -74,7 +74,7 @@ static const struct bt_gatt_cpf name_cpf = {
};

/* Vendor Primary Service Declaration */
static struct bt_gatt_attr name_attrs[] = {
BT_GATT_SERVICE_DEFINE(name_svc,
/* Vendor Primary Service Declaration */
BT_GATT_PRIMARY_SERVICE(&name_uuid),
BT_GATT_CHARACTERISTIC(&name_enc_uuid.uuid,
@@ -83,9 +83,7 @@ static struct bt_gatt_attr name_attrs[] = {
read_name, write_name, NULL),
BT_GATT_CUD("Badge Name", BT_GATT_PERM_READ),
BT_GATT_CPF(&name_cpf),
};

static struct bt_gatt_service name_svc = BT_GATT_SERVICE(name_attrs);
);

static void passkey_display(struct bt_conn *conn, unsigned int passkey)
{
@@ -171,8 +169,6 @@ static void bt_ready(int err)
bt_conn_cb_register(&conn_cb);
bt_conn_auth_cb_register(&auth_cb);

bt_gatt_service_register(&name_svc);

if (IS_ENABLED(CONFIG_SETTINGS)) {
settings_load();
}

0 comments on commit c42707c

Please sign in to comment.
You can’t perform that action at this time.