Skip to content

Commit

Permalink
sample: extract filtration of GATT to a function
Browse files Browse the repository at this point in the history
simplify gatt authorize function

Signed-off-by: Robert Gałat <[email protected]>
  • Loading branch information
RobertGalatNordic committed Nov 20, 2024
1 parent 71f2b0f commit 0438cd7
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 146 deletions.
4 changes: 0 additions & 4 deletions Kconfig.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ config BT_ID_MAX
default 3 if SIDEWALK_DFU
default 2

config BT_APP_IFC
bool "Enable BT app wrapper"
default y if SIDEWALK_BLE

config SIDEWALK_ASSERT
bool
default SIDEWALK
Expand Down
46 changes: 2 additions & 44 deletions samples/sid_end_device/src/cli/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
#include <json_printer/sidTypes2Json.h>
#include <zephyr/logging/log.h>

#include <sid_ble_uuid.h>
#include <bt_app_callbacks.h>
#include <zephyr/bluetooth/gatt.h>
#if defined(CONFIG_SIDEWALK_DFU)
#include <zephyr/mgmt/mcumgr/transport/smp_bt.h>
#endif //defined(CONFIG_SIDEWALK_DFU)

LOG_MODULE_REGISTER(app, CONFIG_SIDEWALK_LOG_LEVEL);

Expand Down Expand Up @@ -124,53 +119,16 @@ static bool gatt_authorize(struct bt_conn *conn, const struct bt_gatt_attr *attr
LOG_ERR("Failed to get id of connection err %d", ret);
return false;
}
char uuid_s[50] = "";
bt_uuid_to_str(attr->uuid, uuid_s, sizeof(uuid_s));
LOG_DBG("GATT authorize : conn_id = %d, attr %s", cinfo.id, uuid_s);

if (cinfo.id == BT_ID_SIDEWALK) {
if (bt_uuid_cmp(attr->uuid, BT_UUID_DECLARE_128(SMP_BT_CHR_UUID_VAL)) == 0) {
LOG_WRN("Block SMP_BT_CHR_UUID_VAL in Sidewalk connection");
if (sid_ble_bt_attr_is_SMP(attr)) {
return false;
}
}

#if defined(CONFIG_SIDEWALK_DFU)
if (cinfo.id == BT_ID_SMP_DFU) {
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(AMA_CHARACTERISTIC_UUID_VAL_WRITE)) == 0) {
LOG_WRN("block AMA_CHARACTERISTIC_UUID_VAL_WRITE in DFU connection");
return false;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(AMA_CHARACTERISTIC_UUID_VAL_NOTIFY)) == 0) {
LOG_WRN("block AMA_CHARACTERISTIC_UUID_VAL_NOTIFY in DFU connection");
return false;
}

if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE)) ==
0) {
LOG_WRN("block VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE in DFU connection");
return false;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY)) ==
0) {
LOG_WRN("block VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY in DFU connection");
return false;
}

if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE)) ==
0) {
LOG_WRN("block LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE in DFU connection");
return false;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY)) ==
0) {
LOG_WRN("block LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY in DFU connection");
if (sid_ble_bt_attr_is_SIDEWALK(attr)) {
return false;
}
}
Expand Down
46 changes: 2 additions & 44 deletions samples/sid_end_device/src/hello/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
#include <sbdt/dfu_file_transfer.h>
#endif

#include <sid_ble_uuid.h>
#include <bt_app_callbacks.h>
#include <zephyr/bluetooth/gatt.h>
#if defined(CONFIG_SIDEWALK_DFU)
#include <zephyr/mgmt/mcumgr/transport/smp_bt.h>
#endif //defined(CONFIG_SIDEWALK_DFU)

#if defined(CONFIG_GPIO)
#include <state_notifier/notifier_gpio.h>
Expand Down Expand Up @@ -332,53 +327,16 @@ static bool gatt_authorize(struct bt_conn *conn, const struct bt_gatt_attr *attr
LOG_ERR("Failed to get id of connection err %d", ret);
return false;
}
char uuid_s[50] = "";
bt_uuid_to_str(attr->uuid, uuid_s, sizeof(uuid_s));
LOG_DBG("GATT authorize : conn_id = %d, attr %s", cinfo.id, uuid_s);

if (cinfo.id == BT_ID_SIDEWALK) {
if (bt_uuid_cmp(attr->uuid, BT_UUID_DECLARE_128(SMP_BT_CHR_UUID_VAL)) == 0) {
LOG_WRN("Block SMP_BT_CHR_UUID_VAL in Sidewalk connection");
if (sid_ble_bt_attr_is_SMP(attr)) {
return false;
}
}

#if defined(CONFIG_SIDEWALK_DFU)
if (cinfo.id == BT_ID_SMP_DFU) {
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(AMA_CHARACTERISTIC_UUID_VAL_WRITE)) == 0) {
LOG_WRN("block AMA_CHARACTERISTIC_UUID_VAL_WRITE in DFU connection");
return false;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(AMA_CHARACTERISTIC_UUID_VAL_NOTIFY)) == 0) {
LOG_WRN("block AMA_CHARACTERISTIC_UUID_VAL_NOTIFY in DFU connection");
return false;
}

if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE)) ==
0) {
LOG_WRN("block VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE in DFU connection");
return false;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY)) ==
0) {
LOG_WRN("block VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY in DFU connection");
return false;
}

if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE)) ==
0) {
LOG_WRN("block LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE in DFU connection");
return false;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY)) ==
0) {
LOG_WRN("block LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY in DFU connection");
if (sid_ble_bt_attr_is_SIDEWALK(attr)) {
return false;
}
}
Expand Down
46 changes: 2 additions & 44 deletions samples/sid_end_device/src/sensor_monitoring/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@
#include <sbdt/dfu_file_transfer.h>
#endif

#include <sid_ble_uuid.h>
#include <bt_app_callbacks.h>
#include <zephyr/bluetooth/gatt.h>
#if defined(CONFIG_SIDEWALK_DFU)
#include <zephyr/mgmt/mcumgr/transport/smp_bt.h>
#endif //defined(CONFIG_SIDEWALK_DFU)

LOG_MODULE_REGISTER(app, CONFIG_SIDEWALK_LOG_LEVEL);

Expand Down Expand Up @@ -254,53 +249,16 @@ static bool gatt_authorize(struct bt_conn *conn, const struct bt_gatt_attr *attr
LOG_ERR("Failed to get id of connection err %d", ret);
return false;
}
char uuid_s[50] = "";
bt_uuid_to_str(attr->uuid, uuid_s, sizeof(uuid_s));
LOG_DBG("GATT authorize : conn_id = %d, attr %s", cinfo.id, uuid_s);

if (cinfo.id == BT_ID_SIDEWALK) {
if (bt_uuid_cmp(attr->uuid, BT_UUID_DECLARE_128(SMP_BT_CHR_UUID_VAL)) == 0) {
LOG_WRN("Block SMP_BT_CHR_UUID_VAL in Sidewalk connection");
if (sid_ble_bt_attr_is_SMP(attr)) {
return false;
}
}

#if defined(CONFIG_SIDEWALK_DFU)
if (cinfo.id == BT_ID_SMP_DFU) {
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(AMA_CHARACTERISTIC_UUID_VAL_WRITE)) == 0) {
LOG_WRN("block AMA_CHARACTERISTIC_UUID_VAL_WRITE in DFU connection");
return false;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(AMA_CHARACTERISTIC_UUID_VAL_NOTIFY)) == 0) {
LOG_WRN("block AMA_CHARACTERISTIC_UUID_VAL_NOTIFY in DFU connection");
return false;
}

if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE)) ==
0) {
LOG_WRN("block VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE in DFU connection");
return false;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY)) ==
0) {
LOG_WRN("block VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY in DFU connection");
return false;
}

if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE)) ==
0) {
LOG_WRN("block LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE in DFU connection");
return false;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY)) ==
0) {
LOG_WRN("block LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY in DFU connection");
if (sid_ble_bt_attr_is_SIDEWALK(attr)) {
return false;
}
}
Expand Down
20 changes: 19 additions & 1 deletion subsys/sal/sid_pal/include/bt_app_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,25 @@

#include <stdbool.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/gatt.h>

#if defined(CONFIG_BT_APP_IFC)
/**
* @brief check if attr is for SMP service
*
* @param attr
* @return true if attr is for SMP
* @return false in othre cases
*/
bool sid_ble_bt_attr_is_SMP(const struct bt_gatt_attr *attr);

/**
* @brief check if attr is for one of Sidewlak services
*
* @param attr
* @return true if attr is
* @return false in othre cases
*/
bool sid_ble_bt_attr_is_SIDEWALK(const struct bt_gatt_attr *attr);

/**
* @brief Wrapper for @bt_enable, with reference tracking.
Expand Down Expand Up @@ -43,6 +60,7 @@ enum sid_ble_id_values {
_BT_ID_MAX
};

#if defined(CONFIG_BT_ID_MAX)
BUILD_ASSERT(_BT_ID_MAX <= CONFIG_BT_ID_MAX,
"Too many BT Ids! increase CONFIG_BT_ID_MAX, to match _BT_ID_MAX");
#endif
Expand Down
41 changes: 41 additions & 0 deletions subsys/sal/sid_pal/src/bt_app_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <zephyr/bluetooth/bluetooth.h>
#include <stdbool.h>
#include <zephyr/kernel.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/uuid.h>
#include <sid_ble_uuid.h>
#include <zephyr/mgmt/mcumgr/transport/smp_bt.h>

static uint32_t bt_enable_count = 0;

Expand Down Expand Up @@ -41,3 +45,40 @@ int sid_ble_bt_disable()
return 0;
}
}

bool sid_ble_bt_attr_is_SMP(const struct bt_gatt_attr *attr)
{
if (bt_uuid_cmp(attr->uuid, BT_UUID_DECLARE_128(SMP_BT_CHR_UUID_VAL)) == 0) {
return true;
}
return false;
}

bool sid_ble_bt_attr_is_SIDEWALK(const struct bt_gatt_attr *attr)
{
if (bt_uuid_cmp(attr->uuid, BT_UUID_DECLARE_128(AMA_CHARACTERISTIC_UUID_VAL_WRITE)) == 0) {
return true;
}
if (bt_uuid_cmp(attr->uuid, BT_UUID_DECLARE_128(AMA_CHARACTERISTIC_UUID_VAL_NOTIFY)) == 0) {
return true;
}

if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE)) == 0) {
return true;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(VND_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY)) == 0) {
return true;
}

if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_WRITE)) == 0) {
return true;
}
if (bt_uuid_cmp(attr->uuid,
BT_UUID_DECLARE_128(LOG_EXAMPLE_CHARACTERISTIC_UUID_VAL_NOTIFY)) == 0) {
return true;
}
return false;
}
3 changes: 0 additions & 3 deletions tests/unit_tests/pal_ble_adapter/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ config SIDEWALK_LOG_LEVEL
config SIDEWALK_BLE_ADAPTER_LOG_LEVEL
default 0

config BT_APP_IFC
default y

config BT_ID_MAX
default 2

Expand Down
3 changes: 0 additions & 3 deletions tests/unit_tests/sid_ble_advert/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ config SIDEWALK_LOG_LEVEL
config BT_ID_MAX
default 2

config BT_APP_IFC
default y

module = SIDEWALK_BLE_ADAPTER
module-str = Sidewalk BLE interface
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"
Expand Down
3 changes: 0 additions & 3 deletions tests/unit_tests/sid_ble_connection/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@ config SIDEWALK_BLE_ADAPTER_LOG_LEVEL
config BT_ID_MAX
default 2

config BT_APP_IFC
default y

source "Kconfig.zephyr"

0 comments on commit 0438cd7

Please sign in to comment.