Beispiel #1
0
int rfc1035_open_tcp(struct rfc1035_res *res, const RFC1035_ADDR *addr)
{
RFC1035_NETADDR addrbuf;
int	af;
const struct sockaddr *addrptr;
int	addrptrlen;

int	fd=rfc1035_mksocket(SOCK_STREAM, 0, &af);

	if (fd < 0)	return (-1);

	if (rfc1035_mkaddress(af, &addrbuf,
		addr, htons(53),
		&addrptr, &addrptrlen))
	{
		close(fd);
		return (-1);
	}

	if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NDELAY) < 0)
	{
		sox_close(fd);
		return (-1);
	}

	if (sox_connect(fd, addrptr, addrptrlen) == 0)
		return (fd);

	if (errno == EINPROGRESS || errno == EAGAIN)
	{
	unsigned	w=res->rfc1035_timeout_initial;

		if (!w)	w=DEFAULT_INITIAL_TIMEOUT;
		if (rfc1035_wait_query(fd, w) == 0 &&
			(sox_connect(fd, addrptr, addrptrlen) == 0
				|| errno == EISCONN))
		{
			return (fd);
		}
	}
	sox_close(fd);
	return (-1);
}
int main(int argc, char **argv) {
	char *username = NULL;
	char *password = NULL;
	char *xmpp_server = NULL;
	char pubsub_server[80];
	int xmpp_server_port = 5223;

	int verbose = 0;

	int current_arg_num = 1;
	char *current_arg_name = NULL;
	char *current_arg_val = NULL;
	char *collection_node = NULL;
	int err;

	struct sigaction sig_int_handler;

	conn = sox_conn_new();

	// Add SIGINT handler
	sig_int_handler.sa_handler = int_handler;
	sigemptyset(&sig_int_handler.sa_mask);
	sig_int_handler.sa_flags = 0;
	sigaction(SIGINT, &sig_int_handler, NULL );

	if (argc == 1 || !strcmp(argv[1], "-help")) {
		print_usage(argv[0]);
		return -1;
	}

	while (current_arg_num < argc) {
		current_arg_name = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-help") == 0) {
			print_usage(argv[0]);
			return -1;
		}

		if (strcmp(current_arg_name, "-verbose") == 0) {
			verbose = 1;
			continue;
		}

		if (current_arg_num == argc) {
			print_usage(argv[0]);
			return -1;
		}

		current_arg_val = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-collection") == 0) {
			collection_node = current_arg_val;
		} else if (strcmp(current_arg_name, "-u") == 0) {
			username = current_arg_val;
			xmpp_server = _sox_get_server(username);
			if (xmpp_server == NULL ) {
				fprintf(stderr, "Invalid JID, use format user@domain\n");
				return SOX_ERROR_INVALID_JID;
			}
			strcpy(pubsub_server, "pubsub.");
			strcat(pubsub_server, xmpp_server);
		} else if (strcmp(current_arg_name, "-p") == 0) {
			password = current_arg_val;
		} else {
			fprintf(stderr, "Unknown argument: %s\n", current_arg_name);
			print_usage(argv[0]);
			return -1;
		}
	}

	if (username == NULL ) {
		fprintf(stderr, "Username missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (collection_node == NULL ) {
		fprintf(stderr, "Collection Node missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (password == NULL ) {
		fprintf(stdout, "%s's ", username);
		fflush(stdout);
		password = getpass("password: "******"Invalid password\n");
			print_usage(argv[0]);
			return -1;
		}
	}

	if (verbose) {
		fprintf(stdout, "XMPP Server: %s\n", xmpp_server);
		fprintf(stdout, "XMPP Server Port: %d\n", xmpp_server_port);
		fprintf(stdout, "XMPP PubSub Server: %s\n", pubsub_server);
		fprintf(stdout, "Username: %s\n", username);
		fprintf(stdout, "Collection Node: %s\n", collection_node);
		fprintf(stdout, "Verbose: YES\n");
		fprintf(stdout, "\n");

		sox_connect(username, password, SOX_LEVEL_DEBUG, NULL, NULL, conn);

	} else
		sox_connect(username, password, SOX_LEVEL_ERROR, NULL, NULL, conn);

	response = sox_response_new();
	err = sox_collection_children_query(conn, collection_node, response);
	sox_response_print(response);
	sox_response_free(response);

	sox_disconnect(conn);
	sox_conn_free(conn);
	return err;
}
Beispiel #3
0
int main(int argc, char **argv) {
	char *username = NULL;
	char *password = NULL;
	char *xmpp_server = NULL;
	char pubsub_server[80];
	int xmpp_server_port = 5223;

	int verbose = 0;

	int current_arg_num = 1;
	char *current_arg_name = NULL;
	char *current_arg_val = NULL;

	struct sigaction sig_int_handler;

	// Add SIGINT handler
	sig_int_handler.sa_handler = int_handler;
	sigemptyset(&sig_int_handler.sa_mask);
	sig_int_handler.sa_flags = 0;
	sigaction(SIGINT, &sig_int_handler, NULL );

	if (argc == 1 || !strcmp(argv[1], "-help")) {
		print_usage(argv[0]);
		return -1;
	}

	while (current_arg_num < argc) {
		current_arg_name = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-help") == 0) {
			print_usage(argv[0]);
			return -1;
		}

		if (strcmp(current_arg_name, "-verbose") == 0) {
			verbose = 1;
			continue;
		}

		if (current_arg_num == argc) {
			print_usage(argv[0]);
			return -1;
		}

		current_arg_val = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-u") == 0) {
			username = current_arg_val;
			xmpp_server = _sox_get_server(username);
			if (xmpp_server == NULL ) {
				fprintf(stderr, "Invalid JID, use format user@domain\n");
				return SOX_ERROR_INVALID_KID;
			}
			strcpy(pubsub_server, "pubsub.");
			strcat(pubsub_server, xmpp_server);
		} else if (strcmp(current_arg_name, "-p") == 0) {
			password = current_arg_val;
		} else {
			fprintf(stderr, "Unknown argument: %s\n", current_arg_name);
			print_usage(argv[0]);
			return -1;
		}
	}

	if (username == NULL ) {
		fprintf(stderr, "Username missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (password == NULL ) {
		fprintf(stderr, "Password missing\n");
		print_usage(argv[0]);
		return -1;
	}

	if (verbose) {
		fprintf(stdout, "XMPP Server: %s\n", xmpp_server);
		fprintf(stdout, "XMPP Server Port: %d\n", xmpp_server_port);
		fprintf(stdout, "XMPP PubSub Server: %s\n", pubsub_server);
		fprintf(stdout, "Username: %s\n", username);
		fprintf(stdout, "Verbose: YES\n");
		fprintf(stdout, "\n");
	}

	if (verbose == 0)
		sox_connect(username, password, SOX_LEVEL_ERROR,
				(sox_handler_conn) sox_conn_handler_presence_send, NULL, &conn);
	else
		sox_connect(username, password, SOX_LEVEL_DEBUG,
				(sox_handler_conn) sox_conn_handler_presence_send, NULL, &conn);

//	while (1) 
{
		response = sox_response_new();
		sox_pubsub_data_receive(&conn, response);
		sox_response_print(response);
		sox_response_free(response);
                actuate_light(0,2,150);
	}

	return 0;
}
Beispiel #4
0
int main(int argc, char **argv) {
	char *username = NULL;
	char *password = NULL;
	char *xmpp_server = NULL;
	char *huelink = NULL;
	char *userid = NULL;
	char pubsub_server[80];
	int xmpp_server_port = 5223;
	int verbose = 0;
	int current_arg_num = 1;
	char *current_arg_name = NULL;
	char *current_arg_val = NULL;
	struct sigaction sig_int_handler;
        char *time=NULL;
	FILE *fd;
	int time_val=0;
	// Add SIGINT handler
	sig_int_handler.sa_handler = int_handler;
	sigemptyset(&sig_int_handler.sa_mask);
	sig_int_handler.sa_flags = 0;
	sigaction(SIGINT, &sig_int_handler, NULL );

	if (argc == 1 || !strcmp(argv[1], "-help")) {
		print_usage(argv[0]);
		return -1;
	}

	while (current_arg_num < argc) {
		current_arg_name = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-help") == 0) {
			print_usage(argv[0]);
			return -1;
		}

		if (strcmp(current_arg_name, "-verbose") == 0) {
			verbose = 1;
			continue;
		}

		if (current_arg_num == argc) {
			print_usage(argv[0]);
			return -1;
		}

		current_arg_val = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-u") == 0) {
			username = current_arg_val;
			xmpp_server = _sox_get_server(username);
			if (xmpp_server == NULL ) {
				fprintf(stderr, "Invalid JID, use format user@domain\n");
				//return SOX_ERROR_INVALID_JID;
				return -1;
			}
			strcpy(pubsub_server, "pubsub.");
			strcat(pubsub_server, xmpp_server);
		} else if (strcmp(current_arg_name, "-p") == 0) {
			password = current_arg_val;
		} else if (strcmp(current_arg_name, "-link") == 0) {
			huelink = current_arg_val;
			fd = fopen("huelink.dat", "w");
			if (fd == NULL ) {
				fprintf(stdout,
						"Cannot create a huelink.dat file to store a address to access hue bridge\n");
				return 0;
			}
			if (huelink != NULL ) {
				fprintf(fd, "%s", huelink);
				fprintf(stdout, "Stored Address (%s) in huelink.dat\n",
						huelink);
				fclose(fd);
				exit(1);
			} else {
				fprintf(fd, "Hue Link/Address missing..Aborting\n");
				print_usage(argv[0]);
				fclose(fd);
				exit(1);
			}

		}

		else if(strcmp(current_arg_name,"-time")==0) 
			time = current_arg_val;

		else if (strcmp(current_arg_name, "-set_userid") == 0) {
			if ((hue_login_information() < 0))
				printf(
						"Missing Address of Hue-Bridge\nCannot Create Username\nAborting\n");

			userid = current_arg_val;
			if (create_new_user(userid)) {
				fprintf(stdout,
						"\nUser Id stored in Hue Bridge and username.dat file\n ");
				exit(0);
			} else {
				fprintf(stdout,
						"Error, Press the Center Button on Hue Bridge before setting up the userid\n");
				exit(1);
			}

		}

		else {
			fprintf(stderr, "Unknown argument: %s\n", current_arg_name);
			print_usage(argv[0]);
			return -1;
		}
	}

	if (username == NULL ) {
		fprintf(stderr, "Username missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (password == NULL ) {
		fprintf(stderr, "Password missing\n");
		print_usage(argv[0]);
		return -1;
	}

	if (verbose) {
		fprintf(stdout, "XMPP Server: %s\n", xmpp_server);
		fprintf(stdout, "XMPP Server Port: %d\n", xmpp_server_port);
		fprintf(stdout, "XMPP PubSub Server: %s\n", pubsub_server);
		fprintf(stdout, "Username: %s\n", username);
		fprintf(stdout, "Verbose: YES\n");
		fprintf(stdout, "\n");
	}

	conn = sox_conn_new();

	if (verbose == 0)
		sox_connect(username, password, SOX_LEVEL_ERROR,
				(sox_handler_conn) sox_conn_handler_presence_send, NULL, conn);
	else
		sox_connect(username, password, SOX_LEVEL_DEBUG,
				(sox_handler_conn) sox_conn_handler_presence_send, NULL, conn);

	if ((hue_login_information()) != 1) {
		printf("Address Information Missing for Hue Bridge\n.");
		printf(
				"Add them using -set_userid and -link option at command line\nAborting..\n");
		exit(0);
	}


	/*Checking Time-Delay Value*/
	if(time!=NULL)
		time_val = atoi(time);
	else
		time_val=0;

	if(time_val<1 && time_val>61) { 
	 	printf("Delay Value Entered is invalid. Using the default 2 second delay\n");
	        time_val=2;
	}
printf("not here\n");

/*Thread Argument is a Structure with following members*/
	thread1_args.conn = conn;
	thread1_args.verbose = verbose;
	thread1_args.delay=time_val;
	thread2_args.conn = conn;
	thread2_args.verbose = verbose;
	read_stub_data();

	hueid_read();
	/* Creating Thread to Publish Data */
	pthread_t thread1, thread2;
	pthread_create(&thread1, NULL, publish_hue_data, (void*) &thread1_args);
	pthread_create(&thread2, NULL, actuate_hue, (void*) &thread2_args);
	pthread_join(thread1, NULL );
	pthread_join(thread2, NULL );
	printf("Thread Joined \n");
	sleep(5);
	exit(0);
}
Beispiel #5
0
int main(int argc, char **argv) {
	char domain[80];
	char *result = NULL;

	char *username = NULL;
	char *password = NULL;
	char xmpp_server[80];
	char pubsub_server[80];
	int xmpp_server_port = 5223;

	int verbose = 0;

	int current_arg_num = 1;
	char *current_arg_name = NULL;
	char *current_arg_val = NULL;

	sox_userdata_t userdata;
	sox_conn_t *conn;

	if (argc == 1 || !strcmp(argv[1], "-help")) {
		print_usage(argv[0]);
		return -1;
	}

	strcpy(xmpp_server, "none");
	strcpy(pubsub_server, "none");

	while (current_arg_num < argc) {
		current_arg_name = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-help") == 0) {
			print_usage(argv[0]);
			return -1;
		}

		if (strcmp(current_arg_name, "-verbose") == 0) {
			verbose = 1;
			continue;
		}

		if (current_arg_num == argc) {
			print_usage(argv[0]);
			return -1;
		}

		current_arg_val = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-u") == 0) {
			username = current_arg_val;
			if (strcmp(xmpp_server, "none") == 0) {
				strcpy(domain, username);
				result = strtok(domain, "@");
				result = strtok(NULL, "@");
				strcpy(xmpp_server, result);
				strcpy(pubsub_server, "pubsub.");
				strcat(pubsub_server, xmpp_server);
			}
		} else if (strcmp(current_arg_name, "-p") == 0) {
			password = current_arg_val;
		} else if (strcmp(current_arg_name, "-sID") == 0) {
			s_id = current_arg_val;
		} else if (strcmp(current_arg_name, "-aID") == 0) {
			a_id = current_arg_val;
		} else if (strcmp(current_arg_name, "-aEvent") == 0) {
			actuate_event = current_arg_val;
		} else {
			fprintf(stderr, "Unknown argument: %s\n", current_arg_name);
			print_usage(argv[0]);
			return -1;
		}
	}

	if (username == NULL ) {
		fprintf(stderr, "Username missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (s_id == 0) {
		fprintf(stderr, "Sensor transducer id missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (actuate_event == NULL ) {
		fprintf(stderr, "Actuator event node missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (a_id == 0) {
		fprintf(stderr, "Actuator transducer id missing\n");
		print_usage(argv[0]);
		return -1;
	}

	else if (password == NULL ) {
		fprintf(stdout, "%s's ", username);
		fflush(stdout);
		password = getpass("password: "******"Invalid password\n");
			print_usage(argv[0]);
			return -1;
		}
	}

	if (verbose) {
		fprintf(stdout, "XMPP Server: %s\n", xmpp_server);
		fprintf(stdout, "XMPP Server Port: %d\n", xmpp_server_port);
		fprintf(stdout, "XMPP PubSub Server: %s\n", pubsub_server);
		fprintf(stdout, "Username: %s\n", username);
		fprintf(stdout, "Verbose: YES\n");
		fprintf(stdout, "\n");
	}

	if (verbose == 0)
		conn = sox_connect(username, password, (sox_handler_conn) conn_handler,
				SOX_LEVEL_ERROR, &userdata);
	else
		conn = sox_connect(username, password, (sox_handler_conn) conn_handler,
				SOX_LEVEL_DEBUG, &userdata);

	sox_run(conn, NULL );
	if (userdata.return_value < 0)
		fprintf(stderr, "ERROR: Connection failed\n");

	return 0;
}
Beispiel #6
0
int main(int argc, char **argv) {
	char *username = NULL;
	char *password = NULL;
	char *xmpp_server = NULL;
	char pubsub_server[80];
	int xmpp_server_port = 5223;

	int verbose = 0;
	int current_arg_num = 1;
	char *current_arg_name = NULL;
	char *current_arg_val = NULL;

	char *time_str = NULL;
	sox_stanza_t *item;

	struct sigaction sig_int_handler;

	// Add SIGINT handler
	sig_int_handler.sa_handler = int_handler;
	sigemptyset(&sig_int_handler.sa_mask);
	sig_int_handler.sa_flags = 0;
	sigaction(SIGINT, &sig_int_handler, NULL );

	while (current_arg_num < argc) {
		current_arg_name = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-help") == 0) {
			print_usage(argv[0]);
			return -1;
		}

		if (strcmp(current_arg_name, "-verbose") == 0) {
			verbose = 1;
			continue;
		}

		if (current_arg_num == argc) {
			print_usage(argv[0]);
			return -1;
		}

		current_arg_val = argv[current_arg_num++];

		if (strcmp(current_arg_name, "-event") == 0) {
			event_node = current_arg_val;
		} else if (strcmp(current_arg_name, "-tname") == 0) {
			t_name = current_arg_val;
		} else if (strcmp(current_arg_name, "-tid") == 0) {
			t_id = current_arg_val;
		}  else if (strcmp(current_arg_name, "-rawvalue") == 0) {
			rawvalue = current_arg_val;
		} else if (strcmp(current_arg_name, "-u") == 0) {
			username = current_arg_val;
			xmpp_server = _sox_get_server(username);
			if (xmpp_server == NULL ) {
				fprintf(stderr, "Invalid JID, use format user@domain\n");
				return SOX_ERROR_INVALID_KID;
			}
			strcpy(pubsub_server, "pubsub.");
			strcat(pubsub_server, xmpp_server);
		} else if (strcmp(current_arg_name, "-p") == 0) {
			password = current_arg_val;
		} else {
			fprintf(stderr, "Unknown argument: %s\n", current_arg_name);
			print_usage(argv[0]);
			return -1;
		}
	}

	if (username == NULL ) {
		fprintf(stderr, "Username missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (event_node == NULL ) {
		fprintf(stderr, "Event Node missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (t_name == NULL ) {
		fprintf(stderr, "Transducer name missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (t_id == NULL ) {
		fprintf(stderr, "Transducer ID missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (value == NULL ) {
		fprintf(stderr, "Value missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (rawvalue == NULL ) {
		fprintf(stderr, "Raw Value missing\n");
		print_usage(argv[0]);
		return -1;
	} else if (password == NULL ) {
		fprintf(stdout, "%s's ", username);
		fflush(stdout);
		password = getpass("password: "******"Invalid password\n");
			print_usage(argv[0]);
			return -1;
		}
	}

	if (verbose) {
		fprintf(stdout, "XMPP Server: %s\n", xmpp_server);
		fprintf(stdout, "XMPP Server Port: %d\n", xmpp_server_port);
		fprintf(stdout, "XMPP PubSub Server: %s\n", pubsub_server);
		fprintf(stdout, "Username: %s\n", username);
		fprintf(stdout, "Event Node: %s\n", event_node);
		fprintf(stdout, "Verbose: YES\n");
		fprintf(stdout, "\n");






		sox_connect(username, password, SOX_LEVEL_DEBUG, NULL, NULL, &conn);

	} else
		sox_connect(username, password, SOX_LEVEL_ERROR, NULL, NULL, &conn);


	time_str = sox_timestamp_create();
	item = sox_pubsub_item_data_new(&conn);

	sox_item_transducer_value_add(item, NULL, t_name, t_id, value, rawvalue,
			time_str);
	response = sox_response_new();
	sox_item_publish_data(&conn, item, event_node, response);
	free(time_str);
	sox_response_print(response);
	sox_response_free(response);
	sox_stanza_free(item);

	sox_disconnect(&conn);

	return SOX_OK;
}