Rest 如何在Contiki OS中使用block2实现coap观察器

Rest 如何在Contiki OS中使用block2实现coap观察器,rest,iot,contiki,coap,Rest,Iot,Contiki,Coap,我将首先解释设置 设置:我有一个微控制器板运行一个Coap rest服务器(使用Contiki OS)和一个客户端(使用Coap的Coapthon-python库)来观察在Linux SOM上运行的资源。我能够成功地观察到从服务器(微控制器)到客户端(Linux SOM)的少量数据(64字节)。在描述完所有内容后,我将在末尾添加代码 问题:我需要帮助从Coap服务器向客户机观察员发送一大块数据(假设1024字节)。我如何才能做到这一点(提前感谢您提供的任何帮助,我将感谢您为此提供的任何帮助) 我

我将首先解释设置

设置:我有一个微控制器板运行一个Coap rest服务器(使用Contiki OS)和一个客户端(使用Coap的Coapthon-python库)来观察在Linux SOM上运行的资源。我能够成功地观察到从服务器(微控制器)到客户端(Linux SOM)的少量数据(64字节)。在描述完所有内容后,我将在末尾添加代码

问题:我需要帮助从Coap服务器向客户机观察员发送一大块数据(假设1024字节)。我如何才能做到这一点(提前感谢您提供的任何帮助,我将感谢您为此提供的任何帮助)

我发布了Contiki可观测资源代码和coapthon客户代码(我发布的代码不发送大数据)。 康蒂基代码:

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);
}
我并没有为周期处理程序添加代码,get处理程序会定期收到周期处理程序的通知。 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分块传输()实现observer时,我需要帮助。

我不能告诉您使用的特定系统的太多信息,但通常分块传输和观察的组合是有效的,因为服务器只发送更新资源的第一个块。然后由客户机请求剩余的块,并验证它们的ETag选项是否匹配

contiki代码看起来应该足够了,因为它将偏移量设置为-1,这可能会设置块头中的“更多数据”位

在Copython侧,您可能需要手动重新组装,或者要求COAPython自动进行重新组装(其代码不表明它支持块的组合并观察,至少不是在短时间内观察)。

< P>到“Bootstrap”您的开发可以考虑使用。 演示应用程序/cf helloworld客户端中的简单客户端需要对其进行一些更改。如果您需要帮助,只需在github中打开一个问题

我要提到的是,在使用该功能两年的经验中,如果您的数据更改速度快于您的“带宽”能够传输的速度(包括所考虑的块的RTT),那么您可能会徒劳地发送大量块。 如果数据的变化速度刚好快于最后一个数据块的发送速度,那么目前为止的完整传输将失效。有些人随后开始发展他们的工作,但从那以后,你的工作就变得非常困难:-)