项目作者: BlackBrickOrg

项目描述 :
Implementation of CoAP client for constrained (really constrained) embedded devices
高级语言: C
项目地址: git://github.com/BlackBrickOrg/tiny-coap.git
创建时间: 2017-04-20T14:43:32Z
项目社区:https://github.com/BlackBrickOrg/tiny-coap

开源协议:BSD 2-Clause "Simplified" License

下载


CoAP client library for embedded devices in C

Allows to add the CoAP functionality for embedded device.

Features

  • very small memory consumption (in the common case it may be about of 200 bytes for both rx/tx buffers, you can tune a PDU size)

  • implemented CoAP over UDP rfc7252

  • implemented CoAP over TCP draft-coap-tcp-tls-07

  • retransmition/acknowledgment functionality

  • parsing of responses. Received data will be return to the user via callback.

  • helpers for block-wise mode. The block-wise mode is located at a higher level of abstraction than this implementation.
    See wiki for example.

How to send CoAP request to server

1) Include tcoap.h in your code.

  1. #include "tcoap.h"

There are several functions in the tcoap.h which are declared how external. You should provide it implementation in your code. See wiki for common case of their implementation.

  1. /**
  2. * @brief In this function user should implement a transmission given data via
  3. * hardware interface (e.g. serial port)
  4. *
  5. */
  6. extern tcoap_error
  7. tcoap_tx_data(tcoap_handle * const handle, const uint8_t * buf, const uint32_t len);
  8. /**
  9. * @brief In this function user should implement a functionality of waiting response.
  10. * This function has to return a control when timeout will expired or
  11. * when response from server will be received.
  12. */
  13. extern tcoap_error
  14. tcoap_wait_event(tcoap_handle * const handle, const uint32_t timeout_ms);
  15. /**
  16. * @brief Through this function the 'tcoap' lib will be notifying about events.
  17. * See possible events here 'tcoap_out_signal'.
  18. */
  19. extern tcoap_error
  20. tcoap_tx_signal(tcoap_handle * const handle, const tcoap_out_signal signal);
  21. /**
  22. * @brief In this function user should implement a generating of message id.
  23. *
  24. */
  25. extern uint16_t
  26. tcoap_get_message_id(tcoap_handle * const handle);
  27. /**
  28. * @brief In this function user should implement a generating of token.
  29. *
  30. */
  31. extern tcoap_error
  32. tcoap_fill_token(tcoap_handle * const handle, uint8_t * token, const uint32_t tkl);
  33. /**
  34. * @brief These functions are using for debug purpose, if user will enable debug mode.
  35. *
  36. */
  37. extern void tcoap_debug_print_packet(tcoap_handle * const handle, const char * msg, uint8_t * data, const uint32_t len);
  38. extern void tcoap_debug_print_options(tcoap_handle * const handle, const char * msg, const tcoap_option_data * options);
  39. extern void tcoap_debug_print_payload(tcoap_handle * const handle, const char * msg, const tcoap_data * const payload);
  40. /**
  41. * @brief In this function user should implement an allocating block of memory.
  42. * In the simple case it may be a static buffer. The 'TCOAP' will make
  43. * two calls of this function before starting work (for rx and tx buffer).
  44. * So, you should have minimum two separate blocks of memory.
  45. *
  46. */
  47. extern tcoap_error tcoap_alloc_mem_block(uint8_t ** block, const uint32_t min_len);
  48. /**
  49. * @brief In this function user should implement a freeing block of memory.
  50. *
  51. */
  52. extern tcoap_error tcoap_free_mem_block(uint8_t * block, const uint32_t min_len);
  53. extern void mem_copy(void * dst, const void * src, uint32_t cnt);
  54. extern bool mem_cmp(const void * dst, const void * src, uint32_t cnt);

2) Define a tcoap_handle object, e.g.

  1. tcoap_handle tc_handle = {
  2. .name = "coap_over_gsm",
  3. .transport = TCOAP_UDP
  4. };

3) Implement a transfer of incoming data from your hardware interface (e.g. serial port) to the tcoap either tcoap_rx_byte or tcoap_rx_packet. E.g.

  1. void uart1_rx_irq_handler()
  2. {
  3. uint8_t byte = UART1->DR;
  4. tcoap_rx_byte(&tc_handle, byte);
  5. }
  6. void eth_rx_irq_handler(uint8_t * data, uint32_t len)
  7. {
  8. tcoap_rx_packet(&tc_handle, data, len);
  9. }

4) Send a coap request and get back response data in the provided callback:

  1. static void data_resource_response_callback(const struct tcoap_request_descriptor * const reqd, const tcoap_result_data * const result)
  2. {
  3. // ... check response
  4. }
  5. void send_request_to_data_resource(uint32_t *token_etag, uint8_t * data, uint32_t len)
  6. {
  7. tcoap_error err;
  8. tcoap_request_descriptor data_request;
  9. tcoap_option_data opt_etag;
  10. tcoap_option_data opt_path;
  11. tcoap_option_data opt_content;
  12. /* fill options - we should adhere an order of options */
  13. opt_content.num = TCOAP_CONTENT_FORMAT_OPT;
  14. opt_content.value = (uint8_t *)"\x2A"; /* 42 = TCOAP_APPLICATION_OCTET_STREAM */
  15. opt_content.len = 1;
  16. opt_content.next = NULL;
  17. opt_path.num = TCOAP_URI_PATH_OPT;
  18. opt_path.value = (uint8_t *)"data";
  19. opt_path.len = 4;
  20. opt_path.next = &opt_content;
  21. opt_etag.num = TCOAP_ETAG_OPT;
  22. opt_etag.value = (uint8_t *)token_etag;
  23. opt_etag.len = 4;
  24. opt_etag.next = &opt_path;
  25. /* fill the request descriptor */
  26. data_request.payload.buf = data;
  27. data_request.payload.len = len;
  28. data_request.code = TCOAP_REQ_POST;
  29. data_request.tkl = 2;
  30. data_request.type = tc_handle.transport == TCOAP_UDP ? TCOAP_MESSAGE_CON : TCOAP_MESSAGE_NON;
  31. data_request.options = &opt_etag;
  32. /* define the callback for response data */
  33. data_request.response_callback = data_resource_response_callback;
  34. /* enable debug */
  35. tcoap_debug(&tc_handle, true);
  36. /* send request */
  37. err = tcoap_send_coap_request(&tc_handle, &data_request);
  38. if (err != TCOAP_OK) {
  39. // error handling
  40. }
  41. }