Пример #1
0
int cfg_init(cfg_s *cfg, int comm_port)
{
	int i;
	char buf[BUFSIZ];

	sprintf(buf, "%d", comm_port);
		
	for (i = 0; i < CFG_IEND; i++)
	{
		cfg->ints[i].key = NULL;
		str_assign(&cfg->ints[i].key, cfg_int_defs[i].key);

		if (comm_port > 0)
			str_cat(&cfg->ints[i].key, buf);
		
		cfg->ints[i].val = cfg_int_defs[i].val;
	}

	for (i = 0; i < CFG_SEND; i++)
	{
		cfg->strs[i].key = NULL;
		str_assign(&cfg->strs[i].key, cfg_str_defs[i].key);

		if (comm_port > 0)
			str_cat(&cfg->strs[i].key, buf);
		
		cfg->strs[i].val = NULL;;
		str_assign(&cfg->strs[i].val, cfg_str_defs[i].val);		
	}
	
	return 0;
}
Пример #2
0
/* setup client id from database, otherwise intialize from source */
char *get_client_id(sqlite3 *database, pSource source) {
	char *json_string;
	char url_string[4096];
	char *c_id = NULL;
	prepare_db_statement("SELECT client_id from client_info limit 1",
						 database,
						 &client_id_statement);
	sqlite3_step(client_id_statement);
	c_id = str_assign((char *)sqlite3_column_text(client_id_statement, 0));
	if (c_id != NULL) {
		printf("Using client_id %s from database...\n", c_id);
	} else {
		sqlite3_reset(client_id_statement);
		sprintf(url_string, "%s/clientcreate%s", source->_source_url, SYNC_SOURCE_FORMAT);
		json_string = fetch_remote_data(url_string);
		if(json_string && strlen(json_string) > 0) {
			c_id = str_assign((char *)parse_client_id(json_string));
		}
		prepare_db_statement("INSERT INTO client_info (client_id) values (?)",
							 database,
							 &client_id_statement);
		sqlite3_bind_text(client_id_statement, 1, c_id, -1, SQLITE_TRANSIENT);
		sqlite3_step(client_id_statement);
		printf("Intialized new client_id %s from source...\n", c_id);
	}
	sqlite3_reset(client_id_statement);
	return c_id;
}
Пример #3
0
int sess_get_port(session *sess)
{
	char * host_buf;
	int i, find_flag = 0;
	if (sess == NULL)
	{
		LOGE("null sess\n");
		return -1;
	}
	host_buf = sess->host->p;
	for (i = 0; i < sess->host->cur_use_size; i++, host_buf++)
	{
		if (*host_buf == ':')
		{
			find_flag = 1;
			*host_buf = '\0';
			host_buf++;
			i++;
			str_nassign(&(sess->host_port), host_buf, sess->host->cur_use_size - i);
			sess->host->cur_use_size = i;
			break;
		}
	}
	if (!find_flag)
	{
		str_assign(&(sess->host_port), "80");
	}
	return 0;
}
Пример #4
0
void serialinfo_assign(serialinfo_s *dst, serialinfo_s *src)
{
	dst->port = src->port;
	dst->baud = src->baud;
	dst->parity = src->parity;
	dst->stopbits = src->stopbits;
	dst->databits = src->databits;
	str_assign(&dst->device, src->device);
}
Пример #5
0
void cfg_assign(cfg_s *dst, cfg_s *src)
{
	int i;

	for (i = 0; i < CFG_IEND; i++)
		dst->ints[i].val = src->ints[i].val;

	for (i = 0; i < CFG_SEND; i++)
		str_assign(&dst->strs[i].val, src->strs[i].val);		
}
Пример #6
0
int find_host_port(session *sess)
{
	int ret, start, end, temp_len;
	st_str *str_search;
	st_str *new_str = NULL, *org = NULL;
	if (sess == NULL || sess->p_read == NULL)
	{
		return -1;
	}
	str_search = sess->p_read;
	ret = strncasecmp(str_search->p, STR_HOST, strlen(STR_HOST));
	printf("start find\n");
	if (ret != 0)
	{
		start = kmp(str_search->p, str_search->cur_use_size, kmp_table[KMP_HOST]);
		LOGD("kmp start find:%d\n", start);
		if (start == -1)
		{
			return -1;
		}
		start += kmp_table[KMP_HOST]->length;
		end = kmp(str_search->p + start, str_search->cur_use_size - start, kmp_table[KMP_ENTER]);
		LOGW("kmp end find:%d\n", end);
		str_nassign(&(sess->host), str_search->p + start, end);
		sess_get_port(sess);
		str_delete_space(sess->host);
		LOGD("%s\n", sess->host->p);
		LOGD("%s:%d\n", sess->host_port->p, atoi(sess->host_port->p));
		
		str_assign(&org, "http://");
		str_nadd(&org, sess->host->p, sess->host->cur_use_size);
		end = kmp(str_search->p, str_search->cur_use_size, kmp_table[KMP_ENTER]);
		
		temp_len = str_search->cur_use_size;
		str_search->cur_use_size = end;
		new_str = str_replace(str_search, org->p, org->cur_use_size, "", 0);
		
		str_nadd(&new_str, str_search->p + end, temp_len - end);
		LOGD("%s\n", new_str->p);
		
		sess->p_read = new_str;
		str_free(str_search);
		str_free(org);
	}
	printf("end find\n");
	return 0;
}
Пример #7
0
int readcfg(char *cfgfile)
{
	char ports[BUFSIZ], *p;
	int port;
	pipe_s *pipe;
	cfg_s local;
	serialinfo_s sinfo;
	char *parity, *newlines;
	
	/* Read the global config settings */
	cfg_fromfile(&cfg, cfgfile);
	
	/* Read the comm port list */
	if (cfg_readbuf(cfgfile, "comm_ports", ports, sizeof(ports)) == NULL)
		errend("Couldn't find 'comm_ports' entry in config file '%s'", cfgfile);

	vlist_clear(&pipes);

	/* Parse the comm ports list */
	p = strtok(ports, ",");
	while (p)
	{
		if (sscanf(p, "%d", &port) > 0)
		{
			pipe = malloc(sizeof(pipe_s));
			//pipe_init(pipe);
			if (pipe == NULL)
				perrend("malloc(pipe_s)");

			cfg_init(&local, port);
			
			/* Copy global settings to those for current pipe */
			cfg_assign(&local, &cfg);

			/* Set the comm port */
			local.ints[CFG_IPORT].val = port;

			/* Load this pipe's config */
			cfg_fromfile(&local, cfgfile);

			/* Try initializing the pipe */
			if (pipe_init(pipe, local.ints[CFG_INETPORT].val))
				perrend("pipe_init");

			/* Copy over the rest of the pipe's config */
			pipe->timeout = local.ints[CFG_ITIMEOUT].val;
			serialinfo_init(&sinfo);
			sinfo.port = port;
			sinfo.baud = local.ints[CFG_IBAUD].val;
			sinfo.stopbits = local.ints[CFG_ISTOP].val;
			sinfo.databits = local.ints[CFG_IDATA].val;
			str_assign(&sinfo.device, local.strs[CFG_SDEVICE].val);

			parity = local.strs[CFG_SPARITY].val;

			if (strcmp(parity, "none") == 0)
			{
				sinfo.parity = SIO_PARITY_NONE;
			}
			else if (strcmp(parity, "even") == 0)
			{
				sinfo.parity = SIO_PARITY_EVEN;
			}
			else if (strcmp(parity, "odd") == 0)
			{
				sinfo.parity = SIO_PARITY_ODD;
			}
			else
			{
				errend("Unknown parity string '%s'", parity);
			}
			
			newlines = local.strs[CFG_SNEWLINES].val;
			
			if (strcmp(newlines, "true") == 0)
			{
				pipe->newlines = 1;
			}
			else if (strcmp(newlines, "false") == 0)
			{
				pipe->newlines = 0;
			}
			else
			{
				errend("Unknown value '%s' for newlines to nils (must be 'true' or 'false')", newlines);
			}

			if (sio_setinfo(&pipe->sio, &sinfo))
				errend("Unable to configure comm port %d", port);
			
			/* Finally add the pipe to the pipes list */
			vlist_add(&pipes, pipes.tail, pipe);

			serialinfo_cleanup(&sinfo);

			cfg_cleanup(&local);
		}
		
		p = strtok(NULL, ",");
	}

	/* Clean up local cfg struct */
	cfg_cleanup(&local);
	
	return 0;
}