You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

operator.c 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "../../apis/c/operator/operator_api.h"
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. DoraInitResult_t dora_init_operator(void)
  7. {
  8. void *context = malloc(1);
  9. char *context_char = (char *)context;
  10. *context_char = 0;
  11. DoraInitResult_t result = {.operator_context = context};
  12. return result;
  13. }
  14. DoraResult_t dora_drop_operator(void *operator_context)
  15. {
  16. free(operator_context);
  17. DoraResult_t result = {};
  18. return result;
  19. }
  20. OnEventResult_t dora_on_event(
  21. const RawEvent_t *event,
  22. const SendOutput_t *send_output,
  23. void *operator_context)
  24. {
  25. char *counter = (char *)operator_context;
  26. if (event->input != NULL)
  27. {
  28. // input event
  29. Input_t *input = event->input;
  30. char id[input->id.len + 1];
  31. memcpy(id, input->id.ptr, input->id.len);
  32. id[input->id.len] = 0;
  33. if (strcmp(id, "message") == 0)
  34. {
  35. char data[input->data.len + 1];
  36. memcpy(data, input->data.ptr, input->data.len);
  37. data[input->data.len] = 0;
  38. *counter += 1;
  39. printf("C operator received message `%s`, counter: %i\n", data, *counter);
  40. char *out_id = "counter";
  41. char *out_id_heap = strdup(out_id);
  42. int data_alloc_size = 100;
  43. char *out_data = (char *)malloc(data_alloc_size);
  44. int count = snprintf(out_data, data_alloc_size, "The current counter value is %d", *counter);
  45. assert(count >= 0 && count < 100);
  46. Output_t output = {.id = {
  47. .ptr = (uint8_t *)out_id_heap,
  48. .len = strlen(out_id_heap),
  49. .cap = strlen(out_id_heap) + 1,
  50. },
  51. .data = {.ptr = (uint8_t *)out_data, .len = strlen(out_data), .cap = data_alloc_size}};
  52. DoraResult_t res = (send_output->send_output.call)(send_output->send_output.env_ptr, output);
  53. OnEventResult_t result = {.result = res, .status = DORA_STATUS_CONTINUE};
  54. return result;
  55. }
  56. else
  57. {
  58. printf("C operator received unexpected input %s, context: %i\n", id, *counter);
  59. }
  60. }
  61. if (event->stop)
  62. {
  63. printf("C operator received stop event\n");
  64. }
  65. OnEventResult_t result = {.status = DORA_STATUS_CONTINUE};
  66. return result;
  67. }

DORA (Dataflow-Oriented Robotic Architecture) is middleware designed to streamline and simplify the creation of AI-based robotic applications. It offers low latency, composable, and distributed datafl