struct queue *notify_list; /* List of registered callbacks */
struct queue *disconn_list; /* List of disconnect handlers */
+ struct queue *exchange_list; /* List of MTU changed handlers */
unsigned int next_send_id; /* IDs for "send" ops */
unsigned int next_reg_id; /* IDs for registered callbacks */
void *user_data;
};
+struct att_exchange {
+ unsigned int id;
+ bool removed;
+ bt_att_exchange_func_t callback;
+ bt_att_destroy_func_t destroy;
+ void *user_data;
+};
+
static void destroy_att_disconn(void *data)
{
struct att_disconn *disconn = data;
free(disconn);
}
+static void destroy_att_exchange(void *data)
+{
+ struct att_exchange *exchange = data;
+
+ if (exchange->destroy)
+ exchange->destroy(exchange->user_data);
+
+ free(exchange);
+}
+
static bool match_disconn_id(const void *a, const void *b)
{
const struct att_disconn *disconn = a;
queue_destroy(att->write_queue, NULL);
queue_destroy(att->notify_list, NULL);
queue_destroy(att->disconn_list, NULL);
+ queue_destroy(att->exchange_list, NULL);
queue_destroy(att->chans, bt_att_chan_free);
free(att);
att->write_queue = queue_new();
att->notify_list = queue_new();
att->disconn_list = queue_new();
+ att->exchange_list = queue_new();
bt_att_attach_chan(att, chan);
return att->mtu;
}
+static void exchange_handler(void *data, void *user_data)
+{
+ struct att_exchange *exchange = data;
+ uint16_t mtu = PTR_TO_INT(user_data);
+
+ if (exchange->removed)
+ return;
+
+ if (exchange->callback)
+ exchange->callback(mtu, exchange->user_data);
+}
+
bool bt_att_set_mtu(struct bt_att *att, uint16_t mtu)
{
struct bt_att_chan *chan;
chan->mtu = mtu;
chan->buf = buf;
- if (chan->mtu > att->mtu)
+ if (chan->mtu > att->mtu) {
att->mtu = chan->mtu;
+ queue_foreach(att->exchange_list, exchange_handler,
+ INT_TO_PTR(att->mtu));
+ }
return true;
}
return true;
}
+unsigned int bt_att_register_exchange(struct bt_att *att,
+ bt_att_exchange_func_t callback,
+ void *user_data,
+ bt_att_destroy_func_t destroy)
+{
+ struct att_exchange *mtu;
+
+ if (!att || queue_isempty(att->chans))
+ return 0;
+
+ mtu = new0(struct att_exchange, 1);
+ mtu->callback = callback;
+ mtu->destroy = destroy;
+ mtu->user_data = user_data;
+
+ if (att->next_reg_id < 1)
+ att->next_reg_id = 1;
+
+ mtu->id = att->next_reg_id++;
+
+ if (!queue_push_tail(att->exchange_list, mtu)) {
+ free(att);
+ return 0;
+ }
+
+ return mtu->id;
+}
+
+bool bt_att_unregister_exchange(struct bt_att *att, unsigned int id)
+{
+ struct att_exchange *mtu;
+
+ if (!att || !id)
+ return false;
+
+ /* Check if disconnect is running */
+ if (queue_isempty(att->chans)) {
+ mtu = queue_find(att->exchange_list, match_disconn_id,
+ UINT_TO_PTR(id));
+ if (!mtu)
+ return false;
+
+ mtu->removed = true;
+ return true;
+ }
+
+ mtu = queue_remove_if(att->exchange_list, match_disconn_id,
+ UINT_TO_PTR(id));
+ if (!mtu)
+ return false;
+
+ destroy_att_exchange(mtu);
+ return true;
+}
+
unsigned int bt_att_send(struct bt_att *att, uint8_t opcode,
const void *pdu, uint16_t length,
bt_att_response_func_t callback, void *user_data,
queue_remove_all(att->notify_list, NULL, NULL, destroy_att_notify);
queue_remove_all(att->disconn_list, NULL, NULL, destroy_att_disconn);
+ queue_remove_all(att->exchange_list, NULL, NULL, destroy_att_exchange);
return true;
}
typedef void (*bt_att_timeout_func_t)(unsigned int id, uint8_t opcode,
void *user_data);
typedef void (*bt_att_disconnect_func_t)(int err, void *user_data);
+typedef void (*bt_att_exchange_func_t)(uint16_t mtu, void *user_data);
typedef bool (*bt_att_counter_func_t)(uint32_t *sign_cnt, void *user_data);
bool bt_att_set_debug(struct bt_att *att, uint8_t level,
bt_att_destroy_func_t destroy);
bool bt_att_unregister_disconnect(struct bt_att *att, unsigned int id);
+unsigned int bt_att_register_exchange(struct bt_att *att,
+ bt_att_exchange_func_t callback,
+ void *user_data,
+ bt_att_destroy_func_t destroy);
+bool bt_att_unregister_exchange(struct bt_att *att, unsigned int id);
bool bt_att_unregister_all(struct bt_att *att);
int bt_att_get_security(struct bt_att *att, uint8_t *enc_size);