Exemplo n.º 1
0
bool hci_inject_open(const hci_t *hci_interface) {
  assert(listen_socket == NULL);
  assert(thread == NULL);
  assert(clients == NULL);
  assert(hci_interface != NULL);

  hci = hci_interface;

  thread = thread_new("hci_inject");
  if (!thread)
    goto error;

  clients = list_new(client_free);
  if (!clients)
    goto error;

  listen_socket = socket_new();
  if (!listen_socket)
    goto error;

  if (!socket_listen(listen_socket, LISTEN_PORT))
    goto error;

  socket_register(listen_socket, thread_get_reactor(thread), NULL, accept_ready, NULL);
  return true;

error:;
  interface.close();
  return false;
}
Exemplo n.º 2
0
static void accept_ready(socket_t *socket, UNUSED_ATTR void *context) {
  assert(socket != NULL);
  assert(socket == listen_socket);

  socket = socket_accept(socket);
  if (!socket)
    return;

  client_t *client = (client_t *)osi_calloc(sizeof(client_t));
  if (!client) {
    LOG_ERROR(LOG_TAG, "%s unable to allocate memory for client.", __func__);
    socket_free(socket);
    return;
  }

  client->socket = socket;

  if (!list_append(clients, client)) {
    LOG_ERROR(LOG_TAG, "%s unable to add client to list.", __func__);
    client_free(client);
    return;
  }

  socket_register(socket, thread_get_reactor(thread), client, read_ready, NULL);
}
Exemplo n.º 3
0
// Must be called with |lock| held.
static sco_socket_t *sco_socket_establish_locked(bool is_listening, const bt_bdaddr_t *bd_addr, int *sock_fd) {
  int pair[2] = { INVALID_FD, INVALID_FD };
  sco_socket_t *sco_socket = NULL;

  if (socketpair(AF_LOCAL, SOCK_STREAM, 0, pair) == -1) {
    LOG_ERROR(LOG_TAG, "%s unable to allocate socket pair: %s", __func__, strerror(errno));
    goto error;
  }

  sco_socket = sco_socket_new();
  if (!sco_socket) {
    LOG_ERROR(LOG_TAG, "%s unable to allocate new SCO socket.", __func__);
    goto error;
  }

  tBTM_STATUS status = BTM_CreateSco((uint8_t *)bd_addr, !is_listening, sco_parameters.packet_types, &sco_socket->sco_handle, connect_completed_cb, disconnect_completed_cb);
  if (status != BTM_CMD_STARTED) {
    LOG_ERROR(LOG_TAG, "%s unable to create SCO socket: %d", __func__, status);
    goto error;
  }

  socket_t *socket = socket_new_from_fd(pair[1]);
  if (!socket) {
    LOG_ERROR(LOG_TAG, "%s unable to allocate socket from file descriptor %d.", __func__, pair[1]);
    goto error;
  }

  *sock_fd = pair[0];           // Transfer ownership of one end to caller.
  sco_socket->socket = socket;  // Hang on to the other end.
  list_append(sco_sockets, sco_socket);

  socket_register(socket, thread_get_reactor(thread), sco_socket, socket_read_ready_cb, NULL);
  return sco_socket;

error:;
  if (pair[0] != INVALID_FD)
    close(pair[0]);
  if (pair[1] != INVALID_FD)
    close(pair[1]);

  sco_socket_free_locked(sco_socket);
  return NULL;
}
Exemplo n.º 4
0
int main(int argc, char *argv[]) {
	duk_context *ctx = NULL;
	int retval = 0;
	const char *filename = NULL;
	int i;

#ifndef NO_SIGNAL
	set_sigint_handler();

	/* This is useful at the global level; libraries should avoid SIGPIPE though */
	/*signal(SIGPIPE, SIG_IGN);*/
#endif

	for (i = 1; i < argc; i++) {
		char *arg = argv[i];
		if (!arg) {
			goto usage;
		}
		if (strcmp(arg, "-c") == 0) {
			c_evloop = 1;
		} else if (strlen(arg) > 1 && arg[0] == '-') {
			goto usage;
		} else {
			if (filename) {
				goto usage;
			}
			filename = arg;
		}
	}
	if (!filename) {
		goto usage;
	}

	ctx = duk_create_heap_default();

	poll_register(ctx);
	ncurses_register(ctx);
	socket_register(ctx);
	fileio_register(ctx);

	if (c_evloop) {
		fprintf(stderr, "Using C based eventloop (omit -c to use Ecmascript based eventloop)\n");
		fflush(stderr);

		eventloop_register(ctx);
		duk_eval_file(ctx, "c_eventloop.js");
	} else {
		fprintf(stderr, "Using Ecmascript based eventloop (give -c to use C based eventloop)\n");
		fflush(stderr);

		duk_eval_file(ctx, "ecma_eventloop.js");
	}

	fprintf(stderr, "Executing code from: '%s'\n", filename);
	fflush(stderr);

	if (strcmp(filename, "-") == 0) {
		if (handle_stdin(ctx) != 0) {
			retval = 1;
			goto cleanup;
		}
	} else {
		if (handle_file(ctx, filename) != 0) {
			retval = 1;
			goto cleanup;
		}
	}

 cleanup:
	if (ctx) {
		duk_destroy_heap(ctx);
	}

	return retval;

 usage:
	fprintf(stderr, "Usage: evloop [-c] <filename>\n");
	fprintf(stderr, "\n");
	fprintf(stderr, "Uses an Ecmascript based eventloop (ecma_eventloop.js) by default.\n");
	fprintf(stderr, "If -c option given, uses a C based eventloop (c_eventloop.{c,js}).\n");
	fprintf(stderr, "If <filename> is '-', the entire STDIN executed.\n");
	fflush(stderr);
	exit(1);
}