Esempio n. 1
0
// -----------------------------------------------------------------------
struct cchar_unit_proto_t * cchar_term_create(struct cfg_arg_t *args)
{
	char *type = NULL;
	int port;
	int res;

	struct cchar_unit_term_t *unit = calloc(1, sizeof(struct cchar_unit_term_t));
	if (!unit) {
		goto fail;
	}

	res = cfg_args_decode(args, "s", &type);
	if (res != E_OK) {
		gerr = res;
		goto fail;
	}

	if (!strcasecmp(type, "tcp")) {
		res = cfg_args_decode(args, "si", &type, &port);
		if (res != E_OK) {
			gerr = res;
			goto fail;
		}
		unit->term = term_open_tcp(port, 100);
		if (!unit->term) {
			gerr = E_TERM;
			goto fail;
		}
	} else if (!strcasecmp(type, "console")) {
		if (em400_console == CONSOLE_DEBUGGER) {
			gerr = E_TERM_CONSOLE_DEBUG;
			goto fail;
		} else if (em400_console == CONSOLE_TERMINAL) {
			gerr = E_TERM_CONSOLE_TERM;
			goto fail;
		} else {
			em400_console = CONSOLE_TERMINAL;
			unit->term = term_open_console();
			if (!unit->term) {
				gerr = E_TERM;
				goto fail;
			}
		}
		printf("Console connected as system terminal.\n");
	} else {
		gerr = E_TERM_UNKNOWN;
		goto fail;
	}

	unit->buf = malloc(TERM_BUF_LEN);
	if (!unit->buf) {
		goto fail;
	}

	eprint("      Terminal (%s), port: %i\n", type, port);

	pthread_mutexattr_t attr;
	pthread_mutexattr_init(&attr);
	pthread_mutex_init(&unit->buf_mutex, &attr);

	res = pthread_create(&unit->worker, NULL, cchar_term_worker, (void *)unit);
	if (res != 0) {
		goto fail;
	}

	return (struct cchar_unit_proto_t *) unit;

fail:
	cchar_term_shutdown((struct cchar_unit_proto_t*) unit);
	return NULL;
}
Esempio n. 2
0
// -----------------------------------------------------------------------
struct cchar_unit_proto_t * cchar_term_create(struct cfg_arg *args)
{
	char *type = NULL;
	int port;
	int res;
	char *device = NULL;
	int speed;

	struct cchar_unit_term_t *unit = (struct cchar_unit_term_t *) calloc(1, sizeof(struct cchar_unit_term_t));
	if (!unit) {
		LOGERR("Failed to allocate memory for unit: %s.", args->text);
		goto fail;
	}

	res = cfg_args_decode(args, "s", &type);

	if (res != E_OK) {
		LOGERR("Failed to parse terminal type: \"%s\".", args->text);
		goto fail;
	}

	if (!strcasecmp(type, "tcp")) {
		res = cfg_args_decode(args->next, "i", &port);
		if (res != E_OK) {
			LOGERR("Failed to parse terminal TCP port: \"%s\".", args->next->text);
			goto fail;
		}
		unit->term = term_open_tcp(port, 100);
		if (!unit->term) {
			LOGERR("Failed to open TCP terminal on port %i.", port);
			goto fail;
		}

	} else if (!strcasecmp(type, "serial")) {
		res = cfg_args_decode(args->next, "s", &device);
		if (res != E_OK) {
			LOGERR("Failed to parse terminal serial device: \"%s\".", args->next->text);
			goto fail;
		}
		res = cfg_args_decode(args->next->next, "i", &speed);
		if (res != E_OK) {
			LOGERR("Failed to parse terminal serial speed: \"%i\".", args->next->next->text);
			goto fail;
		}
		unit->term = term_open_serial(device, speed, 100);
		if (!unit->term) {
			LOGERR("Failed to open serial terminal at %s, speed: %i).", device, speed);
			goto fail;
		}

	} else if (!strcasecmp(type, "console")) {
		if (em400_console == CONSOLE_DEBUGGER) {
			LOGERR("Failed to initialize console terminal; console is being used by the debugger.");
			goto fail;
		} else if (em400_console == CONSOLE_TERMINAL) {
			LOGERR("Failed to initialize console terminal; console is being used by another terminal.");
			goto fail;
		} else {
			em400_console = CONSOLE_TERMINAL;
			unit->term = term_open_console();
			if (!unit->term) {
				LOGERR("Failed to initialize console.");
				goto fail;
			}
		}
		fprintf(stderr, "Console connected as system terminal.\n");

	} else {
		LOGERR("Unknown terminal type: %s.", type);
		goto fail;
	}

	unit->buf = (char *) malloc(TERM_BUF_LEN);
	if (!unit->buf) {
		LOGERR("Failed to allocate memory for terminal buffer.");
		goto fail;
	}

	LOG(L_TERM, "Terminal (%s), port: %i", type, port);

	pthread_mutexattr_t attr;
	pthread_mutexattr_init(&attr);
	pthread_mutex_init(&unit->buf_mutex, &attr);

	res = pthread_create(&unit->worker, NULL, cchar_term_worker, (void *)unit);
	if (res != 0) {
		LOGERR("Failed to spawn terminal thread.");
		goto fail;
	}

	pthread_setname_np(unit->worker, "term");

	free(type);

	return (struct cchar_unit_proto_t *) unit;

fail:
	free(type);
	cchar_term_shutdown((struct cchar_unit_proto_t*) unit);
	return NULL;
}