2121#include <zephyr/logging/log.h>
2222LOG_MODULE_REGISTER (uplink_gatt );
2323
24- /* Callback for handling BLE GATT Uplink Response */
25- static uint8_t tf_uplink_read_cb (struct bt_conn * conn ,
26- uint8_t err ,
27- struct bt_gatt_read_params * params ,
28- const void * data ,
29- uint16_t length )
24+ static uint8_t handle_uplink_payload (struct bt_conn * conn , const void * data , uint16_t length )
3025{
31- if (err )
32- {
33- LOG_ERR ("Failed to read BLE GATT %s (err %d)" , "Uplink" , err );
34- return BT_GATT_ITER_STOP ;
35- }
36-
3726 bool is_first = false;
3827 bool is_last = false;
3928 const void * payload = NULL ;
@@ -56,7 +45,7 @@ static uint8_t tf_uplink_read_cb(struct bt_conn *conn,
5645 int ret = pouch_uplink_write (node -> uplink , payload , payload_len , is_last );
5746 if (ret )
5847 {
59- LOG_ERR ("Failed to write to pouch (err %d)" , err );
48+ LOG_ERR ("Failed to write to pouch (err %d)" , ret );
6049 bt_conn_disconnect (conn , BT_HCI_ERR_REMOTE_USER_TERM_CONN );
6150 return BT_GATT_ITER_STOP ;
6251 }
@@ -69,6 +58,29 @@ static uint8_t tf_uplink_read_cb(struct bt_conn *conn,
6958 return BT_GATT_ITER_STOP ;
7059 }
7160
61+ return BT_GATT_ITER_CONTINUE ;
62+ }
63+
64+ /* Callback for handling BLE GATT Uplink Response */
65+ static uint8_t tf_uplink_read_cb (struct bt_conn * conn ,
66+ uint8_t err ,
67+ struct bt_gatt_read_params * params ,
68+ const void * data ,
69+ uint16_t length )
70+ {
71+ if (err )
72+ {
73+ LOG_ERR ("Failed to read BLE GATT %s (err %d)" , "Uplink" , err );
74+ return BT_GATT_ITER_STOP ;
75+ }
76+
77+ err = handle_uplink_payload (conn , data , length );
78+
79+ if (BT_GATT_ITER_STOP == err )
80+ {
81+ return err ;
82+ }
83+
7284 err = bt_gatt_read (conn , params );
7385 if (err )
7486 {
@@ -79,6 +91,20 @@ static uint8_t tf_uplink_read_cb(struct bt_conn *conn,
7991 return BT_GATT_ITER_STOP ;
8092}
8193
94+ static uint8_t tf_uplink_indicate_cb (struct bt_conn * conn ,
95+ struct bt_gatt_subscribe_params * params ,
96+ const void * data ,
97+ uint16_t length )
98+ {
99+ if (NULL == data )
100+ {
101+ LOG_DBG ("Subscription terminated" );
102+ return BT_GATT_ITER_STOP ;
103+ }
104+
105+ return handle_uplink_payload (conn , data , length );
106+ }
107+
82108void gateway_uplink_start (struct bt_conn * conn )
83109{
84110 struct golioth_node_info * node = get_node_info (conn );
@@ -89,21 +115,40 @@ void gateway_uplink_start(struct bt_conn *conn)
89115 if (node -> uplink == NULL )
90116 {
91117 LOG_ERR ("Failed to open pouch uplink" );
92- bt_conn_disconnect (conn , BT_HCI_ERR_REMOTE_USER_TERM_CONN );
118+ ( void ) bt_conn_disconnect (conn , BT_HCI_ERR_REMOTE_USER_TERM_CONN );
93119 return ;
94120 }
95121
96- struct bt_gatt_read_params * read_params = & node -> read_params ;
97- memset (read_params , 0 , sizeof (* read_params ));
98-
99- read_params -> func = tf_uplink_read_cb ;
100- read_params -> handle_count = 1 ;
101- read_params -> single .handle = node -> attr_handles [GOLIOTH_GATT_ATTR_UPLINK ].value ;
102- int err = bt_gatt_read (conn , read_params );
103- if (err )
122+ if (node -> attr_handles [GOLIOTH_GATT_ATTR_UPLINK ].ccc )
104123 {
105- LOG_ERR ("BT read request failed: %d" , err );
106- bt_conn_disconnect (conn , BT_HCI_ERR_REMOTE_USER_TERM_CONN );
124+ struct bt_gatt_subscribe_params * subscribe_params = & node -> subscribe_params ;
125+ memset (subscribe_params , 0 , sizeof (* subscribe_params ));
126+
127+ subscribe_params -> notify = tf_uplink_indicate_cb ;
128+ subscribe_params -> value = BT_GATT_CCC_INDICATE ;
129+ subscribe_params -> value_handle = node -> attr_handles [GOLIOTH_GATT_ATTR_UPLINK ].value ;
130+ subscribe_params -> ccc_handle = node -> attr_handles [GOLIOTH_GATT_ATTR_UPLINK ].ccc ;
131+ int err = bt_gatt_subscribe (conn , subscribe_params );
132+ if (err )
133+ {
134+ LOG_ERR ("BT subscribe request failed: %d" , err );
135+ (void ) bt_conn_disconnect (conn , BT_HCI_ERR_REMOTE_USER_TERM_CONN );
136+ }
137+ }
138+ else
139+ {
140+ struct bt_gatt_read_params * read_params = & node -> read_params ;
141+ memset (read_params , 0 , sizeof (* read_params ));
142+
143+ read_params -> func = tf_uplink_read_cb ;
144+ read_params -> handle_count = 1 ;
145+ read_params -> single .handle = node -> attr_handles [GOLIOTH_GATT_ATTR_UPLINK ].value ;
146+ int err = bt_gatt_read (conn , read_params );
147+ if (err )
148+ {
149+ LOG_ERR ("BT read request failed: %d" , err );
150+ (void ) bt_conn_disconnect (conn , BT_HCI_ERR_REMOTE_USER_TERM_CONN );
151+ }
107152 }
108153}
109154
0 commit comments