コード例 #1
0
ファイル: main-server.c プロジェクト: errordeveloper/smcp
int
main(int argc, char * argv[]) {
	smcp_t smcp = smcp_create(0);
	struct plugtest_server_s plugtest_server = {};
	struct smcp_node_s root_node = {};

	// Set up the root node.
	smcp_node_init(&root_node,NULL,NULL);

	// Set up the node router.
	smcp_set_default_request_handler(smcp, &smcp_node_router_handler, &root_node);

#if DEBUG
	fprintf(stderr,"DEBUG = %d\n",DEBUG);
#endif
#if VERBOSE_DEBUG
	fprintf(stderr,"VERBOSE_DEBUG = %d\n",VERBOSE_DEBUG);
#endif
	fprintf(stderr,"SMCP_EMBEDDED = %d\n",SMCP_EMBEDDED);
	fprintf(stderr,"SMCP_USE_BSD_SOCKETS = %d\n",SMCP_USE_BSD_SOCKETS);
	fprintf(stderr,"SMCP_DEFAULT_PORT = %d\n",SMCP_DEFAULT_PORT);
	fprintf(stderr,"SMCP_MAX_PATH_LENGTH = %d\n",SMCP_MAX_PATH_LENGTH);
	fprintf(stderr,"SMCP_MAX_URI_LENGTH = %d\n",SMCP_MAX_URI_LENGTH);
	fprintf(stderr,"SMCP_MAX_PACKET_LENGTH = %d\n",(int)SMCP_MAX_PACKET_LENGTH);
	fprintf(stderr,"SMCP_MAX_CONTENT_LENGTH = %d\n",SMCP_MAX_CONTENT_LENGTH);
	fprintf(stderr,"SMCP_USE_CASCADE_COUNT = %d\n",SMCP_USE_CASCADE_COUNT);
	fprintf(stderr,"SMCP_MAX_CASCADE_COUNT = %d\n",SMCP_MAX_CASCADE_COUNT);
	fprintf(stderr,"SMCP_ADD_NEWLINES_TO_LIST_OUTPUT = %d\n",SMCP_ADD_NEWLINES_TO_LIST_OUTPUT);
	fprintf(stderr,"SMCP_AVOID_PRINTF = %d\n",SMCP_AVOID_PRINTF);
	fprintf(stderr,"SMCP_AVOID_MALLOC = %d\n",SMCP_AVOID_MALLOC);
	fprintf(stderr,"SMCP_CONF_USE_DNS = %d\n",SMCP_CONF_USE_DNS);
	fprintf(stderr,"SMCP_CONF_MAX_TRANSACTIONS = %d\n",SMCP_CONF_MAX_TRANSACTIONS);
	fprintf(stderr,"SMCP_CONF_MAX_ALLOCED_NODES = %d\n",SMCP_CONF_MAX_ALLOCED_NODES);
	fprintf(stderr,"SMCP_CONF_MAX_TIMEOUT = %d\n",SMCP_CONF_MAX_TIMEOUT);
	fprintf(stderr,"SMCP_CONF_DUPE_BUFFER_SIZE = %d\n",SMCP_CONF_DUPE_BUFFER_SIZE);
	fprintf(stderr,"SMCP_OBSERVATION_KEEPALIVE_INTERVAL = %d\n",SMCP_OBSERVATION_KEEPALIVE_INTERVAL);
	fprintf(stderr,"SMCP_OBSERVATION_DEFAULT_MAX_AGE = %d\n",SMCP_OBSERVATION_DEFAULT_MAX_AGE);
	fprintf(stderr,"SMCP_VARIABLE_MAX_VALUE_LENGTH = %d\n",SMCP_VARIABLE_MAX_VALUE_LENGTH);
	fprintf(stderr,"SMCP_VARIABLE_MAX_KEY_LENGTH = %d\n",SMCP_VARIABLE_MAX_KEY_LENGTH);
#ifdef SMCP_DEBUG_OUTBOUND_DROP_PERCENT
	fprintf(stderr,"SMCP_DEBUG_OUTBOUND_DROP_PERCENT = %.1f%%\n",SMCP_DEBUG_OUTBOUND_DROP_PERCENT*100.0);
#endif
#ifdef SMCP_DEBUG_INBOUND_DROP_PERCENT
	fprintf(stderr,"SMCP_DEBUG_INBOUND_DROP_PERCENT = %.1f%%\n",SMCP_DEBUG_INBOUND_DROP_PERCENT*100.0);
#endif


	plugtest_server_init(&plugtest_server,&root_node);

	fprintf(stderr,"\nPlugtest server listening on port %d.\n", smcp_get_port(smcp));

	while(1)
		smcp_process(smcp,30*MSEC_PER_SEC);

	return 0;
}
コード例 #2
0
ファイル: example-1.c プロジェクト: paulobrizolara/smcp
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();

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

	smcp_plat_bind_to_port(instance, SMCP_SESSION_TYPE_UDP, COAP_DEFAULT_PORT);

	printf("Listening on port %d\n",smcp_plat_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_plat_wait(instance, CMS_DISTANT_FUTURE);
		smcp_plat_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 EXIT_SUCCESS;
}
コード例 #3
0
ファイル: example-2.c プロジェクト: errordeveloper/smcp
int
main(void) {
	smcp_t instance = smcp_create(0);

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

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

	smcp_set_default_request_handler(instance, &request_handler, NULL);

	while(1) {
		smcp_process(instance, CMS_DISTANT_FUTURE);
	}

	smcp_release(instance);

	return 0;
}
コード例 #4
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;
}
コード例 #5
0
ファイル: main-client.c プロジェクト: paulobrizolara/smcp
int
main(int argc, char * argv[]) {
	smcp_t smcp;
	const char* url = "coap://localhost/";
	int errorcount = 0;
	int round_count = 1;
	int round = 0;
	test_data_s test_data;

	SMCP_LIBRARY_VERSION_CHECK();

	smcp = smcp_create();

	smcp_plat_bind_to_port(smcp, SMCP_SESSION_TYPE_UDP, 0);


//	url = "coap://contiki.local./";
//	url = "coap://coap.me/";
//	url = "coap://vs0.inf.ethz.ch/";

	if (argc > 1) url = argv[1];
	if (!smcp) {
		fprintf(stderr,"Unable to allocate smcp instance\n");
		exit(-1);
	}

	if (argc > 2) {
		round_count = atoi(argv[2]);
	}

	printf("Client using port %d. Will test %d rounds.\n", smcp_plat_get_port(smcp), round_count);

#define do_test(x)	do { printf("%s: Testing...\n",#x); if(test_ ## x(smcp,url,&test_data)) { dump_test_results(&test_data); printf("\tresult = OK\n"); } else { dump_test_results(&test_data); printf("\tresult = FAIL\n"); errorcount++; } } while(0)

#define do_test_expect_fail(x)	do { printf("%s: Testing...\n",#x); if(test_ ## x(smcp,url,&test_data)) { dump_test_results(&test_data); printf("\tresult = OK (Unexpected)\n"); } else { dump_test_results(&test_data); printf("\tresult = FAIL (Expected)\n"); } } while(0)

	for(round = 0;round<round_count && errorcount==0;round++) {
		printf("\n## Round %d...\n",round+1);

		do_test(TD_COAP_CORE_01);
		do_test(TD_COAP_CORE_02);
		do_test(TD_COAP_CORE_03);
		do_test(TD_COAP_CORE_04);
		do_test(TD_COAP_CORE_05);
		do_test(TD_COAP_CORE_06);
		do_test(TD_COAP_CORE_07);
		do_test(TD_COAP_CORE_08);
		do_test(TD_COAP_CORE_09);

		do_test(TD_COAP_CORE_13);
		do_test(TD_COAP_CORE_14);
		do_test(TD_COAP_CORE_17);
		do_test(TD_COAP_CORE_31);

		do_test(TD_COAP_LINK_01);

		do_test(TD_COAP_BLOCK_01);
		do_test(TD_COAP_BLOCK_02);

		do_test_expect_fail(TD_COAP_BLOCK_03);
		do_test_expect_fail(TD_COAP_BLOCK_04);
		do_test_expect_fail(TD_COAP_BLOCK_05);
		do_test(TD_COAP_OBS_01);
	}

	if (errorcount == 0) {
		printf("\nPASS\n");
	} else {
		printf("\nFAIL\n");
	}

	return errorcount;
}
コード例 #6
0
ファイル: cmd_test.c プロジェクト: ndaskalov-mms/smcp
int
tool_cmd_test(
	smcp_t smcp_instance, int argc, char* argv[]
) {
	if((2 == argc) && (0 == strcmp(argv[1], "--help"))) {
		printf("Help not yet implemented for this command.\n");
		return ERRORCODE_HELP;
	}

	smcp_t smcp;
	smcp_t smcp2;

	struct smcp_timer_node_s timer_node = {};
	struct smcp_variable_node_s device_node = {};
	struct smcp_node_s async_response_node = {};

	smcp2 = smcp_create(12345);
	if(smcp_get_port(smcp_instance) == SMCP_DEFAULT_PORT)
		smcp = smcp_instance;
	else
		smcp = smcp_create(SMCP_DEFAULT_PORT);

	//printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children));

#if HAVE_LIBCURL
	struct smcp_curl_proxy_node_s proxy_node = {};
//	curl_global_init(CURL_GLOBAL_ALL);

//	CURLM* curl_multi_handle = curl_multi_init();
	smcp_curl_proxy_node_init(
		&proxy_node,
		smcp_get_root_node(smcp),
		"proxy"
	);
#endif

	//printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children));

//	smcp_pairing_init(
//		smcp_get_root_node(smcp),
//		SMCP_PAIRING_DEFAULT_ROOT_PATH
//	);
//
//	smcp_pairing_init(
//		smcp_get_root_node(smcp2),
//		SMCP_PAIRING_DEFAULT_ROOT_PATH
//	);

	//printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children));

	smcp_variable_node_init(
		&device_node,
		smcp_get_root_node(smcp),
		"device"
	);
	device_node.func = device_func;

	//printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children));

	smcp_variable_node_init(
		NULL,
		smcp_get_root_node(smcp2),
		"device"
	)->func = device_func;

	//printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children));

	smcp_timer_node_init(
		&timer_node,
		smcp_get_root_node(smcp),
		"timer"
	);

	//printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children));

	smcp_node_init((smcp_node_t)&async_response_node,
		smcp_get_root_node(smcp),
		"async_response"
	);
	async_response_node.request_handler = &async_request_handler;

	//printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children));

#if !SMCP_CONF_OBSERVING_ONLY
	{
		char url[256];
		snprintf(url,
			sizeof(url),
			"smcp://127.0.0.1:%d/device/action",
			smcp_get_port(smcp2));
		smcp_pair_with_uri(smcp,
			"device/loadavg",
			url,
			0,
			NULL);
		printf("EVENT_NODE PAIRED WITH %s\n", url);
	}

	{
		char url[256];
		snprintf(url,
			sizeof(url),
			"smcp://[::1]:%d/device/loadavg",
			smcp_get_port(smcp));
		smcp_pair_with_uri(smcp2, "device/action", url, 0, NULL);
		printf("ACTION_NODE PAIRED WITH %s\n", url);
	}
#endif

	//printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children));

	// Just adding some random nodes so we can browse thru them with another process...
	{
		smcp_node_t subdevice = smcp_node_init(NULL,
			smcp_get_root_node(smcp),
			"lots_of_devices");
		unsigned char i = 0;
#if 1
		for(i = i * 97 + 101; i; i = i * 97 + 101) {
#else
		for(i = 0; i != 255; i++) {
#endif
			char *name = NULL;
			asprintf(&name, "subdevice_%d", i); // Will leak, but we don't care.
			smcp_node_init(NULL, (smcp_node_t)subdevice, name);
		}
//		{
//			unsigned int i = bt_rebalance(
//				    (void**)&((smcp_node_t)subdevice)->children);
//			printf("Balance operation took %u rotations\n", i);
//		}
//		{
//			unsigned int i = bt_rebalance(
//				    (void**)&((smcp_node_t)subdevice)->children);
//			printf("Second balance operation took %u rotations\n", i);
//		}
//		{
//			unsigned int i = bt_rebalance(
//				    (void**)&((smcp_node_t)subdevice)->children);
//			printf("Third balance operation took %u rotations\n", i);
//		}
//		{
//			unsigned int i = bt_rebalance(
//				    (void**)&((smcp_node_t)subdevice)->children);
//			printf("Fourth balance operation took %u rotations\n", i);
//		}
	}

	//printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children));

	{
		coap_msg_id_t tid = smcp_get_next_msg_id(smcp2);

		char url[256];

#if 0
		snprintf(url,
			sizeof(url),
			"smcp://["COAP_MULTICAST_IP6_ALLDEVICES"]:%d/device/",
			smcp_get_port(smcp));
#else
		snprintf(url,
			sizeof(url),
			"smcp://[::1]:%d/device/",
			smcp_get_port(smcp));
#endif

		smcp_begin_transaction_old(
			smcp2,
			tid,
			5 * MSEC_PER_SEC,
			0, // Flags
			NULL,
			&list_response_handler,
			NULL
		);

		smcp_outbound_begin(smcp2,COAP_METHOD_GET,COAP_TRANS_TYPE_CONFIRMABLE);
			smcp_outbound_set_msg_id(tid);
			smcp_outbound_set_uri(url, 0);
		smcp_outbound_send();

		fprintf(stderr, " *** Sent LIST request...\n");
	}

	//printf(__FILE__":%d: root node child count = %d\n",__LINE__,(int)bt_count(&smcp_get_root_node(smcp)->children));

	int i;
	for(i = 0; 1/*i < 3000000*/; i++) {
#if 1
		if((i - 1) % 250 == 0) {
			fprintf(stderr, " *** Forcing variable refresh...\n");
			smcp_variable_node_did_change(&device_node,0,NULL);
		}
#endif
		smcp_process(smcp);
		smcp_process(smcp2);
#if HAVE_LIBCURL
		smcp_curl_proxy_node_process(&proxy_node);
#endif
	}

bail:

	return 0;
}