request_sender.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 my_response_handler(response_t *response, void *user_data)
  8. {
  9. char *tag = (char *) user_data;
  10. if (response == NULL) {
  11. printf("[req] request timeout!\n");
  12. return;
  13. }
  14. printf("[req] response handler called mid:%d, status:%d, fmt:%d, payload:%p, len:%d, tag:%s\n",
  15. response->mid, response->status, response->fmt, response->payload,
  16. response->payload_len, tag);
  17. if (response->payload != NULL
  18. && response->payload_len > 0
  19. && response->fmt == FMT_ATTR_CONTAINER) {
  20. printf("[req] dump the response payload:\n");
  21. attr_container_dump((attr_container_t *) response->payload);
  22. }
  23. }
  24. static void test_send_request(char *url, char *tag)
  25. {
  26. request_t request[1];
  27. init_request(request, url, COAP_PUT, 0, NULL, 0);
  28. api_send_request(request, my_response_handler, tag);
  29. }
  30. void on_init()
  31. {
  32. test_send_request("/app/request_handler/url1", "a request to target app");
  33. test_send_request("url1", "a general request");
  34. }
  35. void on_destroy()
  36. {
  37. /* real destroy work including killing timer and closing sensor is
  38. accomplished in wasm app library version of on_destroy() */
  39. }