Exemplo n.º 1
0
int
tool_cmd_delete(
	smcp_t smcp, int argc, char* argv[]
) {
	previous_sigint_handler = signal(SIGINT, &signal_interrupt);
	smcp_transaction_t transaction;

	char url[1000] = "";

	if((2 == argc) && (0 == strcmp(argv[1], "--help"))) {
		printf("Help not yet implemented for this command.\n");
		return ERRORCODE_HELP;
	}

	gRet = ERRORCODE_UNKNOWN;

	if(getenv("SMCP_CURRENT_PATH"))
		strncpy(url, getenv("SMCP_CURRENT_PATH"), sizeof(url));

	if(argc == 2) {
		url_change(url, argv[1]);
	} else {
		fprintf(stderr, "Bad args.\n");
		gRet = ERRORCODE_BADARG;
		goto bail;
	}

	require((transaction=send_delete_request(smcp, url)), bail);

	gRet = ERRORCODE_INPROGRESS;

	while(ERRORCODE_INPROGRESS == gRet) {
		smcp_wait(smcp,1000);
		smcp_process(smcp);
	}

	smcp_transaction_end(smcp, transaction);

bail:
	signal(SIGINT, previous_sigint_handler);
	return gRet;
}
Exemplo n.º 2
0
int
main(void) {
	smcp_t instance;

	SMCP_LIBRARY_VERSION_CHECK();

	// Create our instance on the default CoAP port. If the port
	// is already in use, we will pick the next available port number.
	instance = smcp_create(0);

	if(!instance) {
		perror("Unable to create SMCP instance");
		abort();
	}

	printf("Listening on port %d\n",smcp_get_port(instance));

	// SMCP will always respond to requests with METHOD_NOT_IMPLEMENTED
	// unless a request handler is set. Unless your program is only
	// making CoAP requests, you'll need a line like the following
	// in your program. The request handler may either handle the
	// request itself or route the request to the appropriate handler.
	smcp_set_default_request_handler(instance, &request_handler, NULL);

	// Loop forever. This is the most simple kind of main loop you
	// can haave with SMCP. It is appropriate for simple CoAP servers
	// and clients which do not need asynchronous I/O.
	while(1) {
		smcp_wait(instance, CMS_DISTANT_FUTURE);
		smcp_process(instance);
	}

	// We won't actually get to this line with the above loop, but it
	// is always a good idea to clean up when you are done. If you
	// provide a way to gracefully exit from your own main loop, you
	// can tear down the SMCP instance using the following command.
	smcp_release(instance);

	return 0;
}
Exemplo n.º 3
0
int
tool_cmd_get_url(void *thingML_context) {
    ThingMLCOAPContext* context = (ThingMLCOAPContext*) thingML_context;

    get_show_headers = context->client_coap_config.get_show_headers;
    next_len = context->client_coap_config.next_len;
    redirect_count = context->client_coap_config.redirect_count;
    size_request = context->client_coap_config.size_request;
    get_observe = context->client_coap_config.get_observe;
    get_keep_alive = context->client_coap_config.get_keep_alive;
    get_timeout = context->client_coap_config.get_timeout; // Default timeout is 30 seconds.
    last_observe_value = context->client_coap_config.last_observe_value;
    request_accept_type = context->client_coap_config.request_accept_type;
    observe_once = context->client_coap_config.observe_once;
    observe_ignore_first = context->client_coap_config.observe_ignore_first;
    get_tt = context->client_coap_config.get_tt;

    smcp_t gSMCPInstance = smcp_create(((ThingMLCOAPContext*) thingML_context)->port);

    gRet = ERRORCODE_INPROGRESS;
    require(send_get_request(gSMCPInstance, NULL, 0, thingML_context), bail);


    while(ERRORCODE_INPROGRESS == gRet) {
        smcp_wait(gSMCPInstance,1000);
        smcp_process(gSMCPInstance);
    }

bail:
    smcp_transaction_end(gSMCPInstance,&transaction);

    if(gSMCPInstance)
        smcp_release(gSMCPInstance);

    return gRet;
}
Exemplo n.º 4
0
int
tool_cmd_pair(
	smcp_t smcp, int argc, char* argv[]
) {
	previous_sigint_handler = signal(SIGINT, &signal_interrupt);
	coap_msg_id_t tid=0;

	char url[1000];
	url[0] = 0;

	if((2 == argc) && (0 == strcmp(argv[1], "--help"))) {
		printf("Help not yet implemented for this command.\n");
		return ERRORCODE_HELP;
	}

	gRet = ERRORCODE_UNKNOWN;

	if(getenv("SMCP_CURRENT_PATH")) {
		strncpy(url, getenv("SMCP_CURRENT_PATH"), sizeof(url));
		if(argc >= 2)
			url_change(url, argv[1]);
	} else {
		if(argc >= 2) {
			strncpy(url, argv[1], sizeof(url));
		} else {
			fprintf(stderr, "Bad args.\n");
			gRet = ERRORCODE_BADARG;
			goto bail;
		}
	}

	if(argc == 2) {
		require((tid=send_pair_request(smcp, getenv(
					"SMCP_CURRENT_PATH"), url)), bail);
	} else if(argc == 3) {
		char url2[1000];

		if(getenv("SMCP_CURRENT_PATH")) {
			strncpy(url2, getenv("SMCP_CURRENT_PATH"), sizeof(url2));
			url_change(url2, argv[2]);
		} else {
			strncpy(url2, argv[2], sizeof(url2));
		}

		require((tid=send_pair_request(smcp, url, url2)), bail);
	} else {
		fprintf(stderr, "Bad args.\n");
		gRet = ERRORCODE_BADARG;
		goto bail;
	}

	gRet = ERRORCODE_INPROGRESS;

	while(ERRORCODE_INPROGRESS == gRet) {
		smcp_wait(smcp,1000);
		smcp_process(smcp);
	}

bail:
	smcp_invalidate_transaction_old(smcp, tid);
	signal(SIGINT, previous_sigint_handler);
	return gRet;
}