使用Coapthon(Coap python库)进行CoAp逐块传输

时间:2019-05-09 20:13:43

标签: python c coap

我试图将CoAp块有效负载从Linux板上运行的客户端发送到微控制器(Contiki节点)上运行的服务器。 Contiki节点服务器代码在微控制器上运行;

static void
res_post_put_handler_schedule(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
    coap_packet_t *const coap_req = (coap_packet_t *)request;
    uint8_t *incoming = NULL;
    size_t len = 0;
    unsigned int ct = -1;


    if(!REST.get_header_content_type(request, &ct)) {
        REST.set_response_status(response, REST.status.BAD_REQUEST);
        const char *error_msg = "NoContentType";
        REST.set_response_payload(response, error_msg, strlen(error_msg));
        return;
    }

    if((len = REST.get_request_payload(request, (const uint8_t **)&incoming))) {
        if(coap_req->block1_num * coap_req->block1_size + len <= sizeof(large_update_schedule_store)) {

            memcpy(large_update_store + (coap_req->block1_num * coap_req->block1_size), incoming, len);

            REST.set_response_status(response, REST.status.CHANGED);
            coap_set_header_block1(response, coap_req->block1_num, 0 , coap_req->block1_size); //coap_req->block1_more
        } else {
            REST.set_response_status(response, REST.status.REQUEST_ENTITY_TOO_LARGE);
            REST.set_response_payload(response, buffer, snprintf((char *)buffer, MAX_PLUGFEST_PAYLOAD, "%uB max.", sizeof(large_update_schedule_store)));
            return;
        }
    } else {
        REST.set_response_status(response, REST.status.BAD_REQUEST);
        const char *error_msg = "NoPayload";
        REST.set_response_payload(response, error_msg, strlen(error_msg));
        return;
    }
}

我的python(coapthon)代码在linux板上运行;

preset_payload = "{'0_1|0000_1100': [3, 2, 4, 20.0, 22.22, -0.56, 0.28],'0_1|1100_2400': [3, 2, 4, 20.0, 22.22, -0.56, 0.28],'2_3|0000_2400': [2, 2, 4, 20.0, 22.22, -0.56, 0.56],'4|0000_1400': [2, 2, 4, 20.0, 22.22, -0.56, 0.56],'4|1400_2400': [2, 2, 4, 20.0, 22.22, -0.56, 0.56],'5_6|0000_0900': [2, 2, 4, 20.0, 22.22, -0.56, 0.56],'5_6|0900_1900': [2, 2, 4, 20.0, 22.22, -0.56, 0.56],'5_6|1900_2400': [2, 2, 4, 20.0, 22.22, -0.56, 0.56]}"

def conductor_post_schedule (host, payload):    
    client = HelperClient(server=(host, port))
    request = Request()
    request.code = defines.Codes.POST.number
    request.type = defines.Types["CON"]
    request.destination = (host, port)
    request.uri_path = "configure/res_schedule"
    request.content_type = defines.Content_types["text/plain"]
    request.payload = payload
    request.block1 = (0, 1, 64)
    request._mid = 10
    try:
        response = client.send_request(request, timeout = 10)
    except Empty as e:
        print("conductor_post_schedule({0}) timed out". format(host))
    client.stop()

调试上述代码的输出;

2019-05-13 16:00:03,668 - MainThread - coapthon.layers.messagelayer - DEBUG - send_request - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-10, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 10, ] {'0_1|0000_1100': [3...64 bytes
2019-05-13 16:00:03,675 - MainThread - coapthon.client.coap - DEBUG - send_datagram - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-10, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 10, ] {'0_1|0000_1100': [3...64 bytes
2019-05-13 16:00:03,692 - Thread-1   - coapthon.client.coap - DEBUG - Start receiver Thread
2019-05-13 16:00:03,698 - MainThread-Retry-10 - coapthon.client.coap - DEBUG - retransmit loop ... enter
2019-05-13 16:00:06,373 - MainThread-Retry-10 - coapthon.client.coap - DEBUG - retransmit loop ... retransmit Request
2019-05-13 16:00:06,380 - MainThread-Retry-10 - coapthon.client.coap - DEBUG - send_datagram - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-10, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 10, ] {'0_1|0000_1100': [3...64 bytes
2019-05-13 16:00:11,730 - MainThread-Retry-10 - coapthon.client.coap - DEBUG - retransmit loop ... retransmit Request
2019-05-13 16:00:11,736 - MainThread-Retry-10 - coapthon.client.coap - DEBUG - send_datagram - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-10, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 10, ] {'0_1|0000_1100': [3...64 bytes
2019-05-13 16:00:22,427 - MainThread-Retry-10 - coapthon.client.coap - DEBUG - retransmit loop ... retransmit Request
2019-05-13 16:00:22,432 - MainThread-Retry-10 - coapthon.client.coap - DEBUG - send_datagram - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-10, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 10, ] {'0_1|0000_1100': [3...64 bytes
2019-05-13 16:00:23,373 - Thread-1   - coapthon.client.coap - DEBUG - receive_datagram - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-10, CONTINUE-None, [Block1: 10, ] No payload
2019-05-13 16:00:23,378 - Thread-1   - coapthon.layers.messagelayer - DEBUG - receive_response - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-10, CONTINUE-None, [Block1: 10, ] No payload
2019-05-13 16:00:23,384 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:23,398 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:23,415 - MainThread-Retry-10 - coapthon.client.coap - DEBUG - retransmit loop ... exit
2019-05-13 16:00:23,413 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:23,437 - Thread-1   - coapthon.layers.messagelayer - DEBUG - send_request - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-None, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 26, ] 2400': [3, 2, 4, 20....64 bytes
2019-05-13 16:00:23,443 - Thread-1   - coapthon.client.coap - DEBUG - send_datagram - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-53049, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 26, ] 2400': [3, 2, 4, 20....64 bytes
2019-05-13 16:00:23,456 - Thread-1-Retry-53049 - coapthon.client.coap - DEBUG - retransmit loop ... enter
2019-05-13 16:00:24,394 - Thread-1   - coapthon.client.coap - DEBUG - receive_datagram - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-53049, CONTINUE-None, [Block1: 26, ] No payload
2019-05-13 16:00:24,398 - Thread-1   - coapthon.layers.messagelayer - DEBUG - receive_response - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-53049, CONTINUE-None, [Block1: 26, ] No payload
2019-05-13 16:00:24,404 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:24,418 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:24,433 - Thread-1-Retry-53049 - coapthon.client.coap - DEBUG - retransmit loop ... exit
2019-05-13 16:00:24,442 - Thread-1   - coapthon.layers.messagelayer - DEBUG - send_request - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-None, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 42, ] 2, 4, 20.0, 22.22, -...64 bytes
2019-05-13 16:00:24,448 - Thread-1   - coapthon.client.coap - DEBUG - send_datagram - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-53050, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 42, ] 2, 4, 20.0, 22.22, -...64 bytes
2019-05-13 16:00:24,461 - Thread-1-Retry-53050 - coapthon.client.coap - DEBUG - retransmit loop ... enter
2019-05-13 16:00:25,424 - Thread-1   - coapthon.client.coap - DEBUG - receive_datagram - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-53050, CONTINUE-None, [Block1: 42, ] No payload
2019-05-13 16:00:25,429 - Thread-1   - coapthon.layers.messagelayer - DEBUG - receive_response - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-53050, CONTINUE-None, [Block1: 42, ] No payload
2019-05-13 16:00:25,435 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:25,446 - Thread-1-Retry-53050 - coapthon.client.coap - DEBUG - retransmit loop ... exit
2019-05-13 16:00:25,457 - Thread-1   - coapthon.layers.messagelayer - DEBUG - send_request - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-None, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 58, ] 2.22, -0.56, 0.56],'...64 bytes
2019-05-13 16:00:25,464 - Thread-1   - coapthon.client.coap - DEBUG - send_datagram - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-53051, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 58, ] 2.22, -0.56, 0.56],'...64 bytes
2019-05-13 16:00:25,492 - Thread-1-Retry-53051 - coapthon.client.coap - DEBUG - retransmit loop ... enter
2019-05-13 16:00:26,404 - Thread-1   - coapthon.client.coap - DEBUG - receive_datagram - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-53051, CONTINUE-None, [Block1: 58, ] No payload
2019-05-13 16:00:26,408 - Thread-1   - coapthon.layers.messagelayer - DEBUG - receive_response - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-53051, CONTINUE-None, [Block1: 58, ] No payload
2019-05-13 16:00:26,414 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:26,429 - Thread-1-Retry-53051 - coapthon.client.coap - DEBUG - retransmit loop ... exit
2019-05-13 16:00:26,428 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:26,452 - Thread-1   - coapthon.layers.messagelayer - DEBUG - send_request - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-None, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 74, ] 0.56],'5_6|0000_0900...64 bytes
2019-05-13 16:00:26,459 - Thread-1   - coapthon.client.coap - DEBUG - send_datagram - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-53052, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 74, ] 0.56],'5_6|0000_0900...64 bytes
2019-05-13 16:00:26,472 - Thread-1-Retry-53052 - coapthon.client.coap - DEBUG - retransmit loop ... enter
2019-05-13 16:00:27,386 - Thread-1   - coapthon.client.coap - DEBUG - receive_datagram - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-53052, CONTINUE-None, [Block1: 74, ] No payload
2019-05-13 16:00:27,390 - Thread-1   - coapthon.layers.messagelayer - DEBUG - receive_response - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-53052, CONTINUE-None, [Block1: 74, ] No payload
2019-05-13 16:00:27,395 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:27,399 - Thread-1-Retry-53052 - coapthon.client.coap - DEBUG - retransmit loop ... exit
2019-05-13 16:00:27,419 - Thread-1   - coapthon.layers.messagelayer - DEBUG - send_request - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-None, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 82, ] 0900_1900': [2, 2, 4...48 bytes
2019-05-13 16:00:27,425 - Thread-1   - coapthon.client.coap - DEBUG - send_datagram - From None, To ('fd00::500:f8ff:99c1:663e', 5683), CON-53053, POST-None, [Uri-Path: configure, Uri-Path: res_schedule, Content-Type: 0, Block1: 82, ] 0900_1900': [2, 2, 4...48 bytes
2019-05-13 16:00:27,435 - Thread-1-Retry-53053 - coapthon.client.coap - DEBUG - retransmit loop ... enter
2019-05-13 16:00:28,169 - Thread-1   - coapthon.client.coap - DEBUG - receive_datagram - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-53053, CONTINUE-None, [Block1: 90, ] No payload
2019-05-13 16:00:28,174 - Thread-1   - coapthon.layers.messagelayer - DEBUG - receive_response - From ('fd00::500:f8ff:99c1:663e', 5683), To None, ACK-53053, CONTINUE-None, [Block1: 90, ] No payload
2019-05-13 16:00:28,179 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:28,194 - Thread-1   - coapthon.client.coap - DEBUG - Waiting for retransmit thread to finish ...
2019-05-13 16:00:28,208 - Thread-1-Retry-53053 - coapthon.client.coap - DEBUG - retransmit loop ... exit

有时我会获取所有数据块(完整数据),有时只有2或3个任意随机数的块(更多的块即将到来标志为1),我不明白为什么会这样。 有人可以帮我,给出一些代码示例或指向处理区块转移的coapthon的功能/ API吗?谢谢! -艾莉亚

0 个答案:

没有答案