@@ -31,9 +31,10 @@ static int start_scan(void);
31
31
32
32
static struct bt_conn * default_conn ;
33
33
34
- static struct bt_uuid_128 uuid = BT_UUID_INIT_128 (ZMK_SPLIT_BT_SERVICE_UUID );
34
+ static struct bt_uuid_128 split_service_uuid = BT_UUID_INIT_128 (ZMK_SPLIT_BT_SERVICE_UUID );
35
35
static struct bt_gatt_discover_params discover_params ;
36
36
static struct bt_gatt_subscribe_params subscribe_params ;
37
+ static struct bt_gatt_discover_params sub_discover_params ;
37
38
static uint16_t run_behavior_handle ;
38
39
39
40
static uint8_t split_central_notify_func (struct bt_conn * conn ,
@@ -76,81 +77,85 @@ static uint8_t split_central_notify_func(struct bt_conn *conn,
76
77
return BT_GATT_ITER_CONTINUE ;
77
78
}
78
79
79
- static int split_central_subscribe (struct bt_conn * conn ) {
80
+ static void split_central_subscribe (struct bt_conn * conn ) {
80
81
int err = bt_gatt_subscribe (conn , & subscribe_params );
81
82
switch (err ) {
82
83
case - EALREADY :
83
84
LOG_DBG ("[ALREADY SUBSCRIBED]" );
84
85
break ;
85
- // break;
86
- // bt_gatt_unsubscribe(conn, &subscribe_params);
87
- // return split_central_subscribe(conn);
88
86
case 0 :
89
87
LOG_DBG ("[SUBSCRIBED]" );
90
88
break ;
91
89
default :
92
90
LOG_ERR ("Subscribe failed (err %d)" , err );
93
91
break ;
94
92
}
95
-
96
- return 0 ;
97
93
}
98
94
99
- static uint8_t split_central_discovery_func (struct bt_conn * conn , const struct bt_gatt_attr * attr ,
100
- struct bt_gatt_discover_params * params ) {
101
- int err ;
102
-
95
+ static uint8_t split_central_chrc_discovery_func (struct bt_conn * conn ,
96
+ const struct bt_gatt_attr * attr ,
97
+ struct bt_gatt_discover_params * params ) {
103
98
if (!attr ) {
104
99
LOG_DBG ("Discover complete" );
105
- (void )memset (params , 0 , sizeof (* params ));
106
100
return BT_GATT_ITER_STOP ;
107
101
}
108
102
109
103
LOG_DBG ("[ATTRIBUTE] handle %u" , attr -> handle );
110
104
111
- if (!bt_uuid_cmp (discover_params .uuid , BT_UUID_DECLARE_128 (ZMK_SPLIT_BT_SERVICE_UUID ))) {
112
- memcpy (& uuid , BT_UUID_DECLARE_128 (ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ), sizeof (uuid ));
113
- discover_params .uuid = & uuid .uuid ;
114
- discover_params .start_handle = attr -> handle + 1 ;
105
+ if (attr -> user_data &&
106
+ !bt_uuid_cmp (((struct bt_gatt_chrc * )attr -> user_data )-> uuid ,
107
+ BT_UUID_DECLARE_128 (ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ))) {
108
+ LOG_DBG ("Found position state characteristic" );
109
+ discover_params .uuid = NULL ;
110
+ discover_params .start_handle = attr -> handle + 2 ;
115
111
discover_params .type = BT_GATT_DISCOVER_CHARACTERISTIC ;
116
112
117
- err = bt_gatt_discover (conn , & discover_params );
118
- if (err ) {
119
- LOG_ERR ("Discover failed (err %d)" , err );
120
- }
121
- } else if (!bt_uuid_cmp (discover_params .uuid ,
122
- BT_UUID_DECLARE_128 (ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ))) {
123
- memcpy (& uuid , BT_UUID_GATT_CCC , sizeof (uuid ));
124
- discover_params .uuid = & uuid .uuid ;
125
- discover_params .start_handle = attr -> handle + 2 ;
126
- discover_params .type = BT_GATT_DISCOVER_DESCRIPTOR ;
113
+ subscribe_params .disc_params = & sub_discover_params ;
114
+ subscribe_params .end_handle = discover_params .end_handle ;
127
115
subscribe_params .value_handle = bt_gatt_attr_value_handle (attr );
128
-
129
- err = bt_gatt_discover (conn , & discover_params );
130
- if (err ) {
131
- LOG_ERR ("Discover failed (err %d)" , err );
132
- }
133
- } else if (!bt_uuid_cmp (discover_params .uuid ,
134
- BT_UUID_DECLARE_128 (ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID ))) {
135
- run_behavior_handle = bt_gatt_attr_value_handle (attr );
136
- } else {
137
116
subscribe_params .notify = split_central_notify_func ;
138
117
subscribe_params .value = BT_GATT_CCC_NOTIFY ;
139
- subscribe_params .ccc_handle = attr -> handle ;
140
-
141
118
split_central_subscribe (conn );
119
+ } else if (attr -> user_data &&
120
+ !bt_uuid_cmp (((struct bt_gatt_chrc * )attr -> user_data )-> uuid ,
121
+ BT_UUID_DECLARE_128 (ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID ))) {
122
+ LOG_DBG ("Found run behavior handle" );
123
+ run_behavior_handle = bt_gatt_attr_value_handle (attr );
124
+ }
142
125
143
- memcpy (& uuid , BT_UUID_DECLARE_128 (ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID ), sizeof (uuid ));
144
- discover_params .uuid = & uuid .uuid ;
145
- discover_params .start_handle = attr -> handle + 1 ;
146
- discover_params .type = BT_GATT_DISCOVER_CHARACTERISTIC ;
126
+ bool subscribed = (run_behavior_handle && subscribe_params .value_handle );
147
127
148
- err = bt_gatt_discover (conn , & discover_params );
149
- if (err ) {
150
- LOG_ERR ("Discover failed (err %d)" , err );
151
- }
128
+ return subscribed ? BT_GATT_ITER_STOP : BT_GATT_ITER_CONTINUE ;
129
+ }
130
+
131
+ static uint8_t split_central_service_discovery_func (struct bt_conn * conn ,
132
+ const struct bt_gatt_attr * attr ,
133
+ struct bt_gatt_discover_params * params ) {
134
+ int err ;
135
+
136
+ if (!attr ) {
137
+ LOG_DBG ("Discover complete" );
138
+ (void )memset (params , 0 , sizeof (* params ));
139
+ return BT_GATT_ITER_STOP ;
140
+ }
141
+
142
+ LOG_DBG ("[ATTRIBUTE] handle %u" , attr -> handle );
143
+
144
+ if (bt_uuid_cmp (discover_params .uuid , BT_UUID_DECLARE_128 (ZMK_SPLIT_BT_SERVICE_UUID ))) {
145
+ LOG_DBG ("Found other service" );
146
+ return BT_GATT_ITER_CONTINUE ;
152
147
}
153
148
149
+ LOG_DBG ("Found split service" );
150
+ discover_params .uuid = NULL ;
151
+ discover_params .func = split_central_chrc_discovery_func ;
152
+ discover_params .start_handle = attr -> handle + 1 ;
153
+ discover_params .type = BT_GATT_DISCOVER_CHARACTERISTIC ;
154
+
155
+ err = bt_gatt_discover (conn , & discover_params );
156
+ if (err ) {
157
+ LOG_ERR ("Failed to start discovering split service characteristics (err %d)" , err );
158
+ }
154
159
return BT_GATT_ITER_STOP ;
155
160
}
156
161
@@ -165,9 +170,9 @@ static void split_central_process_connection(struct bt_conn *conn) {
165
170
return ;
166
171
}
167
172
168
- if (conn == default_conn && !subscribe_params .value ) {
169
- discover_params .uuid = & uuid .uuid ;
170
- discover_params .func = split_central_discovery_func ;
173
+ if (conn == default_conn && !subscribe_params .value_handle ) {
174
+ discover_params .uuid = & split_service_uuid .uuid ;
175
+ discover_params .func = split_central_service_discovery_func ;
171
176
discover_params .start_handle = 0x0001 ;
172
177
discover_params .end_handle = 0xffff ;
173
178
discover_params .type = BT_GATT_DISCOVER_PRIMARY ;
@@ -322,6 +327,10 @@ static void split_central_disconnected(struct bt_conn *conn, uint8_t reason) {
322
327
bt_conn_unref (default_conn );
323
328
default_conn = NULL ;
324
329
330
+ // Clean up previously discovered handles;
331
+ subscribe_params .value_handle = 0 ;
332
+ run_behavior_handle = 0 ;
333
+
325
334
start_scan ();
326
335
}
327
336
0 commit comments