coap_server.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "user_config.h"
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <stdlib.h>
  5. #include "coap.h"
  6. size_t coap_server_respond(char *req, unsigned short reqlen, char *rsp, unsigned short rsplen)
  7. {
  8. NODE_DBG("coap_server_respond is called.\n");
  9. size_t rlen = rsplen;
  10. coap_packet_t pkt;
  11. pkt.content.p = NULL;
  12. pkt.content.len = 0;
  13. uint8_t scratch_raw[4];
  14. coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};
  15. int rc;
  16. #ifdef COAP_DEBUG
  17. NODE_DBG("Received: ");
  18. coap_dump(req, reqlen, true);
  19. NODE_DBG("\n");
  20. #endif
  21. if (0 != (rc = coap_parse(&pkt, req, reqlen))){
  22. NODE_DBG("Bad packet rc=%d\n", rc);
  23. return 0;
  24. }
  25. else
  26. {
  27. coap_packet_t rsppkt;
  28. rsppkt.content.p = NULL;
  29. rsppkt.content.len = 0;
  30. #ifdef COAP_DEBUG
  31. coap_dumpPacket(&pkt);
  32. #endif
  33. coap_handle_req(&scratch_buf, &pkt, &rsppkt);
  34. if (0 != (rc = coap_build(rsp, &rlen, &rsppkt))){
  35. NODE_DBG("coap_build failed rc=%d\n", rc);
  36. // return 0;
  37. rlen = 0;
  38. }
  39. else
  40. {
  41. #ifdef COAP_DEBUG
  42. NODE_DBG("Responding: ");
  43. coap_dump(rsp, rlen, true);
  44. NODE_DBG("\n");
  45. #endif
  46. #ifdef COAP_DEBUG
  47. coap_dumpPacket(&rsppkt);
  48. #endif
  49. }
  50. if(rsppkt.content.p){
  51. free(rsppkt.content.p);
  52. rsppkt.content.p = NULL;
  53. rsppkt.content.len = 0;
  54. }
  55. return rlen;
  56. }
  57. }