runloop: add data source callback type to ds handler

This commit is contained in:
Matthias Ringwald 2016-03-24 21:53:42 +01:00
parent f912b310ca
commit 896424b78e
3 changed files with 50 additions and 27 deletions

View File

@ -92,7 +92,7 @@ struct sockaddr_un {
#define MAX_PENDING_CONNECTIONS 10 #define MAX_PENDING_CONNECTIONS 10
/** prototypes */ /** prototypes */
static int socket_connection_hci_process(btstack_data_source_t *ds); static void socket_connection_hci_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type);
static int socket_connection_dummy_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t length); static int socket_connection_dummy_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t length);
/** globals */ /** globals */
@ -170,8 +170,8 @@ connection_t * socket_connection_register_new_connection(int fd){
// store reference from linked item to base object // store reference from linked item to base object
conn->linked_connection.connection = conn; conn->linked_connection.connection = conn;
conn->ds.fd = fd; btstack_run_loop_set_data_source_handler(&conn->ds, &socket_connection_hci_process);
conn->ds.process = socket_connection_hci_process; btstack_run_loop_set_data_source_fd(&conn->ds, fd);
// prepare state machine and // prepare state machine and
socket_connection_init_statemachine(conn); socket_connection_init_statemachine(conn);
@ -197,7 +197,7 @@ void static socket_connection_emit_connection_closed(connection_t *connection){
(*socket_connection_packet_callback)(connection, DAEMON_EVENT_PACKET, 0, (uint8_t *) &event, 1); (*socket_connection_packet_callback)(connection, DAEMON_EVENT_PACKET, 0, (uint8_t *) &event, 1);
} }
int socket_connection_hci_process(btstack_data_source_t *ds) { void socket_connection_hci_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) {
connection_t *conn = (connection_t *) ds; connection_t *conn = (connection_t *) ds;
int fd = btstack_run_loop_get_data_source_fd(ds); int fd = btstack_run_loop_get_data_source_fd(ds);
int bytes_read = read(fd, &conn->buffer[conn->bytes_read], conn->bytes_to_read); int bytes_read = read(fd, &conn->buffer[conn->bytes_read], conn->bytes_to_read);
@ -208,13 +208,11 @@ int socket_connection_hci_process(btstack_data_source_t *ds) {
// free connection // free connection
socket_connection_free_connection(conn); socket_connection_free_connection(conn);
return 0; return;
} }
conn->bytes_read += bytes_read; conn->bytes_read += bytes_read;
conn->bytes_to_read -= bytes_read; conn->bytes_to_read -= bytes_read;
if (conn->bytes_to_read > 0) { if (conn->bytes_to_read > 0) return;
return 0;
}
int dispatch = 0; int dispatch = 0;
switch (conn->state){ switch (conn->state){
@ -247,7 +245,6 @@ int socket_connection_hci_process(btstack_data_source_t *ds) {
btstack_linked_list_add_tail(&parked, (btstack_linked_item_t *) ds); btstack_linked_list_add_tail(&parked, (btstack_linked_item_t *) ds);
} }
} }
return 0;
} }
/** /**
@ -285,13 +282,13 @@ int socket_connection_has_parked_connections(void){
static void socket_connection_accept(btstack_data_source_t *socket_ds, btstack_data_source_callback_type_t callback_type) { static void socket_connection_accept(btstack_data_source_t *socket_ds, btstack_data_source_callback_type_t callback_type) {
struct sockaddr_storage ss; struct sockaddr_storage ss;
socklen_t slen = sizeof(ss); socklen_t slen = sizeof(ss);
int socket_fd = btstack_run_loop_get_data_source_fd(ds); int socket_fd = btstack_run_loop_get_data_source_fd(socket_ds);
/* New connection coming in! */ /* New connection coming in! */
int fd = accept(socket_fd, (struct sockaddr *)&ss, &slen); int fd = accept(socket_fd, (struct sockaddr *)&ss, &slen);
if (fd < 0) { if (fd < 0) {
perror("accept"); perror("accept");
return 0; return;
} }
// no sigpipe // no sigpipe
@ -301,8 +298,6 @@ static void socket_connection_accept(btstack_data_source_t *socket_ds, btstack_d
connection_t * connection = socket_connection_register_new_connection(fd); connection_t * connection = socket_connection_register_new_connection(fd);
socket_connection_emit_connection_opened(connection); socket_connection_emit_connection_opened(connection);
return 0;
} }
/** /**
@ -461,7 +456,7 @@ int socket_connection_create_unix(char *path){
btstack_run_loop_set_data_source_handler(ds, &socket_connection_accept); btstack_run_loop_set_data_source_handler(ds, &socket_connection_accept);
// create unix socket // create unix socket
int fd = socket (AF_UNIX, SOCK_STREAM, 0)); int fd = socket (AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) { if (fd < 0) {
log_error( "Error creating socket ...(%s)", strerror(errno)); log_error( "Error creating socket ...(%s)", strerror(errno));
free(ds); free(ds);

View File

@ -67,7 +67,7 @@ void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*proce
ts->process = process; ts->process = process;
}; };
void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, int (*process)(btstack_data_source_t *_ds)){ void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, void (*process)(btstack_data_source_t *_ds, btstack_data_source_callback_type_t callback_type)){
ds->process = process; ds->process = process;
}; };
@ -79,6 +79,11 @@ int btstack_run_loop_get_data_source_fd(btstack_data_source_t *ds){
return ds->fd; return ds->fd;
} }
void btstack_run_loop_enable_data_source_callback(btstack_data_source_t *data_source, uint16_t callbacks){
}
void btstack_run_loop_disable_data_source_callback(btstack_data_source_t *data_source, uint16_t callbacks){
}
/** /**
* Add data_source to run_loop * Add data_source to run_loop

View File

@ -118,32 +118,32 @@ void btstack_run_loop_init(const btstack_run_loop_t * run_loop);
/** /**
* @brief Set timer based on current time in milliseconds. * @brief Set timer based on current time in milliseconds.
*/ */
void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms); void btstack_run_loop_set_timer(btstack_timer_source_t * ts, uint32_t timeout_in_ms);
/** /**
* @brief Set callback that will be executed when timer expires. * @brief Set callback that will be executed when timer expires.
*/ */
void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)); void btstack_run_loop_set_timer_handler(btstack_timer_source_t * ts, void (*process)(btstack_timer_source_t *_ts));
/** /**
* @brief Set context for this timer * @brief Set context for this timer
*/ */
void btstack_run_loop_set_timer_context(btstack_timer_source_t *ts, void * context); void btstack_run_loop_set_timer_context(btstack_timer_source_t * ts, void * context);
/** /**
* @brief Get context for this timer * @brief Get context for this timer
*/ */
void * btstack_run_loop_get_timer_context(btstack_timer_source_t *ts); void * btstack_run_loop_get_timer_context(btstack_timer_source_t * ts);
/** /**
* @brief Add timer source. * @brief Add timer source.
*/ */
void btstack_run_loop_add_timer(btstack_timer_source_t *timer); void btstack_run_loop_add_timer(btstack_timer_source_t * timer);
/** /**
* @brief Remove timer source. * @brief Remove timer source.
*/ */
int btstack_run_loop_remove_timer(btstack_timer_source_t *timer); int btstack_run_loop_remove_timer(btstack_timer_source_t * timer);
/** /**
* @brief Get current time in ms * @brief Get current time in ms
@ -154,24 +154,47 @@ uint32_t btstack_run_loop_get_time_ms(void);
/** /**
* @brief Set data source callback. * @brief Set data source callback.
*/ */
void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, void (*process)(btstack_data_source_t *_ds, btstack_data_source_callback_type_t callback_type)); void btstack_run_loop_set_data_source_handler(btstack_data_source_t * data_source, void (*process)(btstack_data_source_t *_ds, btstack_data_source_callback_type_t callback_type));
/** /**
* @brief Set data source file descriptor. * @brief Set data source file descriptor.
* @param data_source
* @param fd file descriptor
* @note No effect if port doensn't have file descriptors * @note No effect if port doensn't have file descriptors
*/ */
void btstack_run_loop_set_data_source_fd(btstack_data_source_t *ds, int fd); void btstack_run_loop_set_data_source_fd(btstack_data_source_t * data_source, int fd);
/** /**
* @brief Get data source file descriptor. * @brief Get data source file descriptor.
* @param data_source
*/ */
int btstack_run_loop_get_data_source_fd(btstack_data_source_t *ds); int btstack_run_loop_get_data_source_fd(btstack_data_source_t * data_source);
/** /**
* @brief Add/Remove data source. * @brief Enable callbacks for a data source
* @param data_source to remove
* @param callback types to enable
*/ */
void btstack_run_loop_add_data_source(btstack_data_source_t *dataSource); void btstack_run_loop_enable_data_source_callback(btstack_data_source_t * data_source, uint16_t callbacks);
int btstack_run_loop_remove_data_source(btstack_data_source_t *dataSource);
/**
* @brief Enable callbacks for a data source
* @param data_source to remove
* @param callback types to disable
*/
void btstack_run_loop_disable_data_source_callback(btstack_data_source_t * data_source, uint16_t callbacks);
/**
* @brief Add data source to run loop
* @param data_source to add
*/
void btstack_run_loop_add_data_source(btstack_data_source_t * data_source);
/**
* @brief Remove data source from run loop
* @param data_source to remove
*/
int btstack_run_loop_remove_data_source(btstack_data_source_t * data_source);
/** /**
* @brief Execute configured run loop. This function does not return. * @brief Execute configured run loop. This function does not return.