mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-13 04:13:54 +00:00
runloop: set data source fd by new setter
This commit is contained in:
parent
0f964fb051
commit
3a5c43eebf
@ -199,8 +199,8 @@ void static socket_connection_emit_connection_closed(connection_t *connection){
|
||||
|
||||
int socket_connection_hci_process(struct btstack_data_source *ds) {
|
||||
connection_t *conn = (connection_t *) ds;
|
||||
|
||||
int bytes_read = read(ds->fd, &conn->buffer[conn->bytes_read], conn->bytes_to_read);
|
||||
int fd = btstack_run_loop_get_data_source_fd(ds);
|
||||
int bytes_read = read(fd, &conn->buffer[conn->bytes_read], conn->bytes_to_read);
|
||||
if (bytes_read <= 0){
|
||||
// connection broken (no particular channel, no date yet)
|
||||
socket_connection_emit_connection_closed(conn);
|
||||
@ -285,9 +285,10 @@ int socket_connection_has_parked_connections(void){
|
||||
static int socket_connection_accept(struct btstack_data_source *socket_ds) {
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t slen = sizeof(ss);
|
||||
int socket_fd = btstack_run_loop_get_data_source_fd(ds);
|
||||
|
||||
/* New connection coming in! */
|
||||
int fd = accept(socket_ds->fd, (struct sockaddr *)&ss, &slen);
|
||||
int fd = accept(socket_fd, (struct sockaddr *)&ss, &slen);
|
||||
if (fd < 0) {
|
||||
perror("accept");
|
||||
return 0;
|
||||
@ -314,7 +315,7 @@ int socket_connection_create_tcp(int port){
|
||||
// create btstack_data_source_t
|
||||
btstack_data_source_t *ds = malloc( sizeof(btstack_data_source_t));
|
||||
if (ds == NULL) return -1;
|
||||
ds->fd = 0;
|
||||
memset(ds, 0, sizeof(btstack_data_source_t));
|
||||
btstack_run_loop_set_data_source_handler(ds, &socket_connection_accept);
|
||||
|
||||
// create tcp socket
|
||||
@ -369,8 +370,9 @@ void socket_connection_launchd_register_fd_array(launch_data_t listening_fd_arra
|
||||
// create btstack_data_source_t for fd
|
||||
btstack_data_source_t *ds = malloc( sizeof(btstack_data_source_t));
|
||||
if (ds == NULL) return;
|
||||
memset(ds, 0, sizeof(btstack_data_source_t));
|
||||
btstack_run_loop_set_data_source_handler(ds, &socket_connection_accept);
|
||||
ds->fd = listening_fd;
|
||||
btstack_run_loop_set_data_source_fd(ds, listening_fd);
|
||||
btstack_run_loop_add_data_source(ds);
|
||||
}
|
||||
}
|
||||
@ -455,16 +457,17 @@ int socket_connection_create_unix(char *path){
|
||||
// create btstack_data_source_t
|
||||
btstack_data_source_t *ds = malloc( sizeof(btstack_data_source_t));
|
||||
if (ds == NULL) return -1;
|
||||
ds->fd = 0;
|
||||
memset(ds, 0, sizeof(btstack_data_source_t));
|
||||
btstack_run_loop_set_data_source_handler(ds, &socket_connection_accept);
|
||||
|
||||
// create unix socket
|
||||
if ((ds->fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
|
||||
int fd = socket (AF_UNIX, SOCK_STREAM, 0));
|
||||
if (fd < 0) {
|
||||
log_error( "Error creating socket ...(%s)", strerror(errno));
|
||||
free(ds);
|
||||
return -1;
|
||||
}
|
||||
|
||||
btstack_run_loop_set_data_source_fd(ds, fd);
|
||||
log_info ("Socket created at %s", path);
|
||||
|
||||
struct sockaddr_un addr;
|
||||
@ -474,9 +477,9 @@ int socket_connection_create_unix(char *path){
|
||||
unlink(path);
|
||||
|
||||
const int y = 1;
|
||||
setsockopt(ds->fd, SOL_SOCKET, SO_REUSEADDR, (void*) &y, sizeof(int));
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*) &y, sizeof(int));
|
||||
|
||||
if (bind ( ds->fd, (struct sockaddr *) &addr, sizeof (addr) ) ) {
|
||||
if (bind (fd, (struct sockaddr *) &addr, sizeof (addr) ) ) {
|
||||
log_error( "Error on bind() ...(%s)", strerror(errno));
|
||||
free(ds);
|
||||
return -1;
|
||||
@ -487,7 +490,7 @@ int socket_connection_create_unix(char *path){
|
||||
chmod(path, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
//
|
||||
|
||||
if (listen (ds->fd, MAX_PENDING_CONNECTIONS)) {
|
||||
if (listen(fd, MAX_PENDING_CONNECTIONS)) {
|
||||
log_error( "Error on listen() ...(%s)", strerror(errno));
|
||||
free(ds);
|
||||
return -1;
|
||||
|
@ -105,7 +105,7 @@ static int h4_set_baudrate(uint32_t baudrate){
|
||||
log_info("h4_set_baudrate %u", baudrate);
|
||||
|
||||
struct termios toptions;
|
||||
int fd = hci_transport_h4->ds->fd;
|
||||
int fd = btstack_run_loop_get_data_source_fd(hci_transport_h4->ds);
|
||||
|
||||
if (tcgetattr(fd, &toptions) < 0) {
|
||||
perror("init_serialport: Couldn't get term attributes");
|
||||
@ -212,7 +212,7 @@ static int h4_open(void){
|
||||
hci_transport_h4->ds = (btstack_data_source_t*) malloc(sizeof(btstack_data_source_t));
|
||||
if (!hci_transport_h4->ds) return -1;
|
||||
hci_transport_h4->uart_fd = fd;
|
||||
hci_transport_h4->ds->fd = fd;
|
||||
btstack_run_loop_set_data_source_fd(hci_transport_h4->ds, fd);
|
||||
btstack_run_loop_set_data_source_handler(hci_transport_h4->ds, &h4_process);
|
||||
btstack_run_loop_add_data_source(hci_transport_h4->ds);
|
||||
|
||||
@ -233,7 +233,8 @@ static int h4_close(void){
|
||||
btstack_run_loop_remove_data_source(hci_transport_h4->ds);
|
||||
|
||||
// close device
|
||||
close(hci_transport_h4->ds->fd);
|
||||
int fd = btstack_run_loop_get_data_source_fd(hci_transport_h4->ds);
|
||||
close(fd);
|
||||
|
||||
// free struct
|
||||
free(hci_transport_h4->ds);
|
||||
|
@ -62,7 +62,7 @@ void btstack_stdin_setup(int (*stdin_process)(btstack_data_source_t *_ds)){
|
||||
}
|
||||
#endif
|
||||
|
||||
stdin_source.fd = 0; // stdin
|
||||
btstack_run_loop_set_data_source_fd(&stdin_source, 0); // stdin
|
||||
btstack_run_loop_set_data_source_handler(&stdin_source, stdin_process);
|
||||
btstack_run_loop_add_data_source(&stdin_source);
|
||||
|
||||
|
@ -744,7 +744,7 @@ void iphone_register_for_power_notifications(void (*cb)(POWER_NOTIFICATION_t eve
|
||||
pipe(power_notification_pipe_fds);
|
||||
|
||||
// set up data source handler
|
||||
power_notification_ds.fd = power_notification_pipe_fds[0];
|
||||
btstack_run_loop_set_data_source_fd(&power_notification_ds, power_notification_pipe_fds[0]); // stdin
|
||||
btstack_run_loop_set_data_source_handler(&power_notification_ds, &power_notification_process);
|
||||
btstack_run_loop_add_data_source(&power_notification_ds);
|
||||
}
|
||||
|
@ -219,13 +219,14 @@ static int h4_open(void)
|
||||
goto err_out4;
|
||||
}
|
||||
|
||||
hci_transport_h4->uart_fd = fd;
|
||||
|
||||
// set up data_source
|
||||
hci_transport_h4->ds = malloc(sizeof(btstack_data_source_t));
|
||||
if (!hci_transport_h4->ds) return -1;
|
||||
hci_transport_h4->uart_fd = fd;
|
||||
|
||||
hci_transport_h4->ds->fd = fd;
|
||||
btstack_run_loop_set_data_source_handler(hci_transport_h4, &h4_process);
|
||||
memset(hci_transport_h4->ds, 0, sizeof(btstack_data_source_t));
|
||||
btstack_run_loop_set_data_source_fd(hci_transport_h4->ds, fd);
|
||||
btstack_run_loop_set_data_source_handler(hci_transport_h4->ds, &h4_process);
|
||||
btstack_run_loop_add_data_source(hci_transport_h4->ds);
|
||||
|
||||
// init state machine
|
||||
|
@ -843,7 +843,7 @@ static int usb_open(void){
|
||||
}
|
||||
for (r = 0 ; r < num_pollfds ; r++) {
|
||||
btstack_data_source_t *ds = &pollfd_data_sources[r];
|
||||
ds->fd = pollfd[r]->fd;
|
||||
btstack_run_loop_set_data_source_fd(ds, pollfd[r]->fd);
|
||||
btstack_run_loop_set_data_source_handler(ds, &usb_process_ds);
|
||||
btstack_run_loop_add_data_source(ds);
|
||||
log_info("%u: %p fd: %u, events %x", r, pollfd[r], pollfd[r]->fd, pollfd[r]->events);
|
||||
|
@ -84,7 +84,8 @@ static int h4_open(void){
|
||||
// set up data_source
|
||||
hci_transport_h4->ds = (btstack_data_source_t*) malloc(sizeof(btstack_data_source_t));
|
||||
if (!hci_transport_h4->ds) return -1;
|
||||
hci_transport_h4->ds->fd = fd;
|
||||
memset(hci_transport_h4->ds, 0, sizeof(btstack_data_source_t));
|
||||
btstack_run_loop_set_data_source_fd(hci_transport_h4->ds, fd);
|
||||
btstack_run_loop_set_data_source_handler(hci_transport_h4->ds, &h4_process);
|
||||
btstack_run_loop_add_data_source(hci_transport_h4->ds);
|
||||
return 0;
|
||||
|
@ -71,6 +71,14 @@ void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, int (*p
|
||||
ds->process = process;
|
||||
};
|
||||
|
||||
void btstack_run_loop_set_data_source_fd(btstack_data_source_t *ds, int fd){
|
||||
ds->fd = fd;
|
||||
}
|
||||
|
||||
int btstack_run_loop_get_data_source_fd(btstack_data_source_t *ds){
|
||||
return ds->fd;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add data_source to run_loop
|
||||
|
@ -156,6 +156,17 @@ uint32_t btstack_run_loop_get_time_ms(void);
|
||||
*/
|
||||
void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, int (*process)(btstack_data_source_t *_ds));
|
||||
|
||||
/**
|
||||
* @brief Set data source file descriptor.
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Get data source file descriptor.
|
||||
*/
|
||||
int btstack_run_loop_get_data_source_fd(btstack_data_source_t *ds);
|
||||
|
||||
/**
|
||||
* @brief Add/Remove data source.
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user