coap_client.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "user_config.h"
  2. #include <stdint.h>
  3. #include "coap.h"
  4. #include "hash.h"
  5. #include "node.h"
  6. extern coap_queue_t *gQueue;
  7. void coap_client_response_handler(char *data, unsigned short len, unsigned short size, const uint32_t ip, const uint32_t port)
  8. {
  9. NODE_DBG("coap_client_response_handler is called.\n");
  10. coap_packet_t pkt;
  11. pkt.content.p = NULL;
  12. pkt.content.len = 0;
  13. int rc;
  14. if (0 != (rc = coap_parse(&pkt, data, len))){
  15. NODE_DBG("Bad packet rc=%d\n", rc);
  16. }
  17. else
  18. {
  19. #ifdef COAP_DEBUG
  20. coap_dumpPacket(&pkt);
  21. #endif
  22. /* check if this is a response to our original request */
  23. if (!check_token(&pkt)) {
  24. /* drop if this was just some message, or send RST in case of notification */
  25. if (pkt.hdr.t == COAP_TYPE_CON || pkt.hdr.t == COAP_TYPE_NONCON){
  26. // coap_send_rst(pkt); // send RST response
  27. // or, just ignore it.
  28. }
  29. goto end;
  30. }
  31. if (pkt.hdr.t == COAP_TYPE_RESET) {
  32. NODE_DBG("got RST\n");
  33. goto end;
  34. }
  35. coap_tid_t id = COAP_INVALID_TID;
  36. coap_transaction_id(ip, port, &pkt, &id);
  37. /* transaction done, remove the node from queue */
  38. // stop timer
  39. coap_timer_stop();
  40. // remove the node
  41. coap_remove_node(&gQueue, id);
  42. // calculate time elapsed
  43. coap_timer_update(&gQueue);
  44. coap_timer_start(&gQueue);
  45. if (COAP_RESPONSE_CLASS(pkt.hdr.code) == 2)
  46. {
  47. /* There is no block option set, just read the data and we are done. */
  48. NODE_DBG("%d.%02d\t", (pkt.hdr.code >> 5), pkt.hdr.code & 0x1F);
  49. NODE_DBG((char *)(pkt.payload.p));
  50. }
  51. else if (COAP_RESPONSE_CLASS(pkt.hdr.code) >= 4)
  52. {
  53. NODE_DBG("%d.%02d\t", (pkt.hdr.code >> 5), pkt.hdr.code & 0x1F);
  54. NODE_DBG((char *)(pkt.payload.p));
  55. }
  56. }
  57. end:
  58. if(!gQueue){ // if there is no node pending in the queue, disconnect from host.
  59. }
  60. }