【问题标题】:How to implement coap observer with block2 in Contiki OS如何在 Contiki OS 中使用 block2 实现 coap 观察者
【发布时间】: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)。

【问题讨论】:

    标签: rest iot contiki coap


    【解决方案1】:

    关于您使用的特定系统,我不能说太多,但总的来说,块传输和观察的组合的工作原理是服务器只发送更新资源的第一个块。然后由客户端请求剩余的块,并验证它们的 ETag 选项是否匹配。

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

    在 coapython 方面,您可能需要手动进行重组,或者要求 coapython 自动进行重组(它的代码并没有表明它会支持 blockwise 和 observable 的组合,至少一眼看不到) .

    【讨论】:

      【解决方案2】:

      要“引导”您的开发,您可以考虑使用Eclipse/Californium。 demo-apps/cf-helloworld-client 中的简单客户端需要对观察进行一些更改。如果您需要帮助,请在 github 中打开一个问题。

      我有两年的使用该功能的经验,让我提一下,如果您的数据更改速度快于您的“带宽”能够传输(包括考虑的块 RTT),您可能会发送很多块徒然。 如果数据更改的速度比您发送的最后一个块的速度快,那么到目前为止的完整传输无效。然后有些人开始开发他们的解决方法,但从那以后你就如履薄冰:-)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-01
        相关资源
        最近更新 更多