【发布时间】:2019-11-06 13:56:15
【问题描述】:
我先解释一下设置;
设置:我有一个微控制器板,运行 Coap 休息服务器(使用 Contiki OS)和一个可观察资源和一个客户端(使用 Coapthon - Coap 的 python 库)观察在 Linux SOM 上运行的资源。我成功地观察到了从服务器(微控制器)到客户端(Linux SOM)的少量数据(64 字节)。我将在描述完所有内容后添加代码。
问题:我需要帮助将大量数据(假设为 1024 字节)从 Coap 服务器发送到客户端观察者。我该怎么做(在此先感谢您的任何帮助,如果我能在这方面获得任何帮助,我将不胜感激)?
我正在发布 Contiki 可观察资源代码和 coapthon 客户端代码(我正在发布不发送大数据的代码)。 Contiki代码:
char * temp_payload = "Behold empty data";
PERIODIC_RESOURCE(res_periodic_ext_temp_data,
"title=\"Temperature\";rt=\"Temperature\";obs",
res_get_handler_of_periodic_ext_temp_data,
NULL,
NULL,
res_delete_handler_ext_temp_data,
(15*CLOCK_SECOND),
res_periodic_handler_of_ext_temp_data);
static void
res_get_handler_of_periodic_ext_temp_data(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
/*
* For minimal complexity, request query and options should be ignored for GET on observable resources.
* Otherwise the requests must be stored with the observer list and passed by REST.notify_subscribers().
* This would be a TODO in the corresponding files in contiki/apps/erbium/!
*/
/* Check the offset for boundaries of the resource data. */
if(*offset >= 1024) {
REST.set_response_status(response, REST.status.BAD_OPTION);
/* A block error message should not exceed the minimum block size (16). */
const char *error_msg = "BlockOutOfScope";
REST.set_response_payload(response, error_msg, strlen(error_msg));
return;
}
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
REST.set_response_payload(response,(temp_payload + *offset), MIN( (int32_t)strlen(temp_payload) - *offset, preferred_size));
REST.set_response_status(response, REST.status.OK);
/* IMPORTANT for chunk-wise resources: Signal chunk awareness to REST engine. */
*offset += preferred_size;
/* Signal end of resource representation. */
if(*offset >= (int32_t)strlen( temp_payload) + 1) {
*offset = -1;
}
REST.set_header_max_age(response, MAX_AGE);
}
我没有为周期性处理程序添加代码,获取处理程序会定期从周期性处理程序获得通知。 Coapthon代码:
def ext_temp_data_callback_observe(response):
print response.pretty_print()
def observe_ext_temp_data(host, callback):
client = HelperClient(server=(host, port))
request = Request()
request.code = defines.Codes.GET.number
request.type = defines.Types["CON"]
request.destination = (host, port)
request.uri_path = "data/res_periodic_ext_temp_data"
request.content_type = defines.Content_types["text/plain"]
request.observe = 0
request.block2 = (0, 0, 64)
try:
response = client.send_request(request, callback)
print response.pretty_print()
except Empty as e:
print("listener_post_observer_rate_of_change({0}) timed out". format(host))
再次,我需要帮助来实现带有 coap 块明智传输的观察者 (https://www.rfc-editor.org/rfc/rfc7959#page-26)。
【问题讨论】: