request_sender.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "wasm_app.h"
  17. static void my_response_handler(response_t *response, void *user_data)
  18. {
  19. char *tag = (char *) user_data;
  20. if (response == NULL) {
  21. printf("[req] request timeout!\n");
  22. return;
  23. }
  24. printf("[req] response handler called mid:%d, status:%d, fmt:%d, payload:%p, len:%d, tag:%s\n",
  25. response->mid, response->status, response->fmt, response->payload,
  26. response->payload_len, tag);
  27. if (response->payload != NULL
  28. && response->payload_len > 0
  29. && response->fmt == FMT_ATTR_CONTAINER) {
  30. printf("[req] dump the response payload:\n");
  31. attr_container_dump((attr_container_t *) response->payload);
  32. }
  33. }
  34. static void test_send_request(char *url, char *tag)
  35. {
  36. request_t request[1];
  37. init_request(request, url, COAP_PUT, 0, NULL, 0);
  38. api_send_request(request, my_response_handler, tag);
  39. }
  40. void on_init()
  41. {
  42. test_send_request("/app/request_handler/url1", "a request to target app");
  43. test_send_request("url1", "a general request");
  44. }
  45. void on_destroy()
  46. {
  47. /* real destroy work including killing timer and closing sensor is
  48. accomplished in wasm app library version of on_destroy() */
  49. }