06_timer.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "wa-inc/timer_wasm_app.h"
  8. /* User global variable */
  9. int num = 0;
  10. /* Timer callback */
  11. void
  12. timer1_update(user_timer_t timer)
  13. {
  14. if (num < 2)
  15. num++;
  16. }
  17. void
  18. res1_handler(request_t *request)
  19. {
  20. user_timer_t timer;
  21. /* set up a timer */
  22. timer = api_timer_create(1000, true, false, timer1_update);
  23. api_timer_restart(timer, 1000);
  24. response_t response[1];
  25. make_response_for_request(request, response);
  26. set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, NULL, 0);
  27. api_response_send(response);
  28. }
  29. void
  30. res2_handler(request_t *request)
  31. {
  32. response_t response[1];
  33. attr_container_t *payload;
  34. if (num == 2) {
  35. attr_container_t *payload;
  36. printf("### user resource 1 handler called\n");
  37. payload = attr_container_create("wasm app response payload");
  38. if (payload == NULL)
  39. return;
  40. attr_container_set_int(&payload, "num", num);
  41. make_response_for_request(request, response);
  42. set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER,
  43. (const char *)payload,
  44. attr_container_get_serialize_length(payload));
  45. printf("reciver: %lu, mid:%d\n", response->reciever, response->mid);
  46. api_response_send(response);
  47. attr_container_destroy(payload);
  48. }
  49. }
  50. void
  51. on_init()
  52. {
  53. /* register resource uri */
  54. api_register_resource_handler("/res1", res1_handler);
  55. api_register_resource_handler("/check_timer", res2_handler);
  56. }
  57. void
  58. on_destroy()
  59. {
  60. /* real destroy work including killing timer and closing sensor is
  61. * accomplished in wasm app library version of on_destroy() */
  62. }