Exemple #1
0
smcp_status_t
async_request_handler(
	smcp_node_t		node
) {
	smcp_status_t ret = SMCP_STATUS_OK;
	smcp_method_t	method = smcp_inbound_get_code();
	if(method==COAP_METHOD_GET) {
		ret = smcp_start_async_response(&async_response,0);
		if(ret==SMCP_STATUS_DUPE) {
			printf("  ** Dupe, already preparing async response.\n");
		} else if(!ret) {
			printf(" *** Request requires an async response...!\n");
		}
		require_noerr(ret, bail);

		ret = smcp_begin_transaction_old(
			smcp_get_current_instance(),
			smcp_get_next_msg_id(smcp_get_current_instance()),
			30*1000,	// Retry for thirty seconds.
			SMCP_TRANSACTION_DELAY_START, // Flags
			(void*)&resend_async_response,
			(void*)&async_response_ack_handler,
			(void*)&async_response
		);
	} else {
		ret = SMCP_STATUS_NOT_ALLOWED;
	}

bail:
	return ret;
}
Exemple #2
0
static coap_msg_id_t
send_pair_request(
	smcp_t smcp, const char* url, const char* url2
) {
	bool ret = false;
	coap_msg_id_t tid;
	const char** url_ = calloc(2,sizeof(char*));

	require(url_,bail);
	url_[0] = url;
	url_[1] = url2;

	tid = smcp_get_next_msg_id(smcp);
	//static char tid_str[30];

	gRet = ERRORCODE_INPROGRESS;

	if(strcmp(url, url2) == 0) {
		fprintf(stderr, "Pairing a URL to itself is invalid.\n");
		goto bail;
	}

	printf("Pairing \"%s\" to \"%s\"...\n", url, url2);

	require_noerr(smcp_begin_transaction_old(
			smcp,
			tid,
			30*1000,	// Retry for thirty seconds.
			0, // Flags
			(void*)&resend_pair_request,
			(void*)&pair_response_handler,
			(void*)url_
		), bail);

	ret = true;

bail:
	if(!ret) {
		tid = 0;
		free(url_);
	}
	return tid;
}
Exemple #3
0
static smcp_status_t
response_test_handler(int statuscode, void* context) {
	test_data_s * const test_data = context;
	smcp_t interface = smcp_get_current_instance();
	bool has_observe_option = false;
	smcp_status_t status = 0;
	if(!test_data->finished) {
		if (statuscode < 0) {
			test_data->finished = true;
			if (statuscode != SMCP_STATUS_TRANSACTION_INVALIDATED) {
				test_data->error = statuscode;
			}
		} else {
			const uint8_t* value;
			coap_size_t value_len;
			coap_option_key_t key;
			test_data->msg_id = smcp_inbound_get_packet()->msg_id;

			if (smcp_inbound_is_dupe()) {
				test_data->inbound_dupe_packets++;
			} else {
				test_data->inbound_packets++;
			}

			while ((key=smcp_inbound_next_option(&value, &value_len))!=COAP_OPTION_INVALID) {
				if (key == COAP_OPTION_BLOCK1) {
					test_data->block1_option = 0;
					test_data->has_block1_option = 1;
					test_data->block1_option = coap_decode_uint32(value,(uint8_t)value_len);
					if ( statuscode == COAP_RESULT_231_CONTINUE) {
						smcp_transaction_new_msg_id(interface, test_data->transaction, smcp_get_next_msg_id(interface));
						smcp_transaction_tickle(interface, test_data->transaction);
					}
				}
				if (key == COAP_OPTION_BLOCK2) {
					test_data->block2_option = 0;
					test_data->has_block2_option = 1;
					test_data->block2_option = coap_decode_uint32(value,(uint8_t)value_len);
					if (!(test_data->block2_option & (1<<3))) {
						test_data->finished = true;
					}
				}
				if (key == COAP_OPTION_OBSERVE) {
					if (test_data->inbound_packets > 2) {
						test_data->finished = true;
					}
					has_observe_option = true;
					printf("\tobs-response: \"%s\"\n", smcp_inbound_get_content_ptr());
				}
			}

			if ( !has_observe_option
			  && !test_data->has_block2_option
			  && statuscode != COAP_RESULT_231_CONTINUE
			) {
				test_data->finished = true;
			}

			test_data->inbound_content_len += smcp_inbound_get_content_len();
			test_data->inbound_code = (coap_code_t)statuscode;

			if (test_data->has_block2_option) {
				strncat(test_data->response, smcp_inbound_get_content_ptr(), smcp_inbound_get_content_len());
			} else {
				strncpy(test_data->response, smcp_inbound_get_content_ptr(), smcp_inbound_get_content_len());
			}
		}
	}
	return status;
}
Exemple #4
0
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;
}