request_sender.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_app.h"
  6. #include "wa-inc/request.h"
  7. static void
  8. my_response_handler(response_t *response, void *user_data)
  9. {
  10. char *tag = (char *)user_data;
  11. if (response == NULL) {
  12. printf("[req] request timeout!\n");
  13. return;
  14. }
  15. printf("[req] response handler called mid:%d, status:%d, fmt:%d, "
  16. "payload:%p, len:%d, tag:%s\n",
  17. response->mid, response->status, response->fmt, response->payload,
  18. response->payload_len, tag);
  19. if (response->payload != NULL && response->payload_len > 0
  20. && response->fmt == FMT_ATTR_CONTAINER) {
  21. printf("[req] dump the response payload:\n");
  22. attr_container_dump((attr_container_t *)response->payload);
  23. }
  24. }
  25. static void
  26. test_send_request(char *url, char *tag)
  27. {
  28. request_t request[1];
  29. init_request(request, url, COAP_PUT, 0, NULL, 0);
  30. api_send_request(request, my_response_handler, tag);
  31. }
  32. void
  33. on_init()
  34. {
  35. test_send_request("/app/request_handler/url1", "a request to target app");
  36. test_send_request("url1", "a general request");
  37. }
  38. void
  39. on_destroy()
  40. {
  41. /* real destroy work including killing timer and closing sensor is
  42. accomplished in wasm app library version of on_destroy() */
  43. }