sensor.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/sensor.h"
  7. static sensor_t sensor1 = NULL;
  8. static sensor_t sensor2 = NULL;
  9. static char *user_data = NULL;
  10. /* Sensor event callback*/
  11. void
  12. sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data)
  13. {
  14. if (sensor == sensor1) {
  15. printf("### app get sensor event from sensor1\n");
  16. attr_container_dump(event);
  17. }
  18. else {
  19. printf("### app get sensor event from sensor2\n");
  20. attr_container_dump(event);
  21. }
  22. }
  23. void
  24. on_init()
  25. {
  26. attr_container_t *config;
  27. printf("### app on_init 1\n");
  28. /* open a sensor */
  29. user_data = malloc(100);
  30. if (!user_data) {
  31. printf("allocate memory failed\n");
  32. return;
  33. }
  34. printf("### app on_init 2\n");
  35. sensor1 = sensor_open("sensor_test1", 0, sensor_event_handler, user_data);
  36. if (!sensor1) {
  37. printf("open sensor1 failed\n");
  38. return;
  39. }
  40. /* config the sensor */
  41. sensor_config(sensor1, 1000, 0, 0);
  42. printf("### app on_init 3\n");
  43. sensor2 = sensor_open("sensor_test2", 0, sensor_event_handler, user_data);
  44. if (!sensor2) {
  45. printf("open sensor2 failed\n");
  46. return;
  47. }
  48. /* config the sensor */
  49. sensor_config(sensor2, 5000, 0, 0);
  50. printf("### app on_init 4\n");
  51. /*
  52. config = attr_container_create("sensor config");
  53. sensor_config(sensor, config);
  54. attr_container_destroy(config);
  55. */
  56. }
  57. void
  58. on_destroy()
  59. {
  60. if (NULL != sensor1) {
  61. sensor_config(sensor1, 0, 0, 0);
  62. }
  63. if (NULL != sensor2) {
  64. sensor_config(sensor2, 0, 0, 0);
  65. }
  66. if (NULL != user_data) {
  67. free(user_data);
  68. }
  69. /* real destroy work including killing timer and closing sensor is
  70. accomplished in wasm app library version of on_destroy() */
  71. }