02_request.c 1.9 KB

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