04_request_internal_req.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. uint32 mid;
  8. unsigned long sender;
  9. void
  10. my_response_handler(response_t *response, void *user_data)
  11. {
  12. attr_container_t *payload;
  13. printf("### user resource 1 handler called\n");
  14. payload = attr_container_create("wasm app response payload");
  15. if (payload == NULL)
  16. return;
  17. attr_container_set_string(&payload, "key1", "value1");
  18. attr_container_set_string(&payload, "key2", "value2");
  19. response->mid = mid;
  20. response->reciever = sender;
  21. set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER,
  22. (const char *)payload,
  23. attr_container_get_serialize_length(payload));
  24. printf("reciver: %lu, mid:%d\n", response->reciever, response->mid);
  25. api_response_send(response);
  26. attr_container_destroy(payload);
  27. }
  28. static void
  29. test_send_request(const char *url, const char *tag)
  30. {
  31. request_t request[1];
  32. init_request(request, (char *)url, COAP_PUT, 0, NULL, 0);
  33. api_send_request(request, my_response_handler, (void *)tag);
  34. }
  35. void
  36. res1_handler(request_t *request)
  37. {
  38. mid = request->mid;
  39. sender = request->sender;
  40. test_send_request("url1", "a general request");
  41. }
  42. void
  43. res2_handler(request_t *request)
  44. {
  45. mid = request->mid;
  46. sender = request->sender;
  47. test_send_request("/app/App1/url1", "a general request");
  48. }
  49. void
  50. on_init()
  51. {
  52. /* register resource uri */
  53. api_register_resource_handler("/res1", res1_handler);
  54. api_register_resource_handler("/res2", res2_handler);
  55. }
  56. void
  57. on_destroy()
  58. {
  59. /* real destroy work including killing timer and closing sensor is
  60. * accomplished in wasm app library version of on_destroy() */
  61. }