aura  0.1
 All Data Structures Functions Variables Modules Pages
aura.h
1 #ifndef AURA_H
2 #define AURA_H
3 
4 #include <search.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <aura/slog.h>
13 #include <search.h>
14 #include <errno.h>
15 #include <stdbool.h>
16 
17 #include "list.h"
18 #include "format.h"
19 #include "endian.h"
20 
21 
23 enum aura_node_status {
24  AURA_STATUS_OFFLINE,
25  AURA_STATUS_ONLINE,
26 };
27 
29 enum aura_call_status {
30  AURA_CALL_COMPLETED,
31  AURA_CALL_TIMEOUT,
32  AURA_CALL_TRANSPORT_FAIL,
33 };
34 
36 enum aura_fd_action {
37  AURA_FD_ADDED,
38  AURA_FD_REMOVED
39 };
40 
41 struct aura_pollfds {
42  int magic;
43  struct aura_node *node;
44  int fd;
45  uint32_t events;
46 };
47 
48 struct aura_object;
49 struct aura_node {
50  const struct aura_transport *tr;
51  struct aura_export_table *tbl;
52  void *transport_data;
53  void *user_data;
54  enum aura_node_status status;
55  struct list_head outbound_buffers;
56  struct list_head inbound_buffers;
57 
58  /* A simple, memory efficient buffer pool */
59  struct list_head buffer_pool;
60  int num_buffers_in_pool;
61  int gc_threshold;
62  /* Synchronos calls put their stuff here */
63  bool sync_call_running;
64  bool need_endian_swap;
65  bool is_opening;
66  struct aura_buffer *sync_ret_buf;
67  int sync_call_result;
68 
69  /* Synchronous event storage */
70  struct list_head event_buffers;
71  int sync_event_max;
72  int sync_event_count;
73 
74  /* General callbacks */
75  void *status_changed_arg;
76  void (*status_changed_cb)(struct aura_node *node, int newstatus, void *arg);
77 
78  void *etable_changed_arg;
79  void (*etable_changed_cb)(struct aura_node *node,
80  struct aura_export_table *old,
81  struct aura_export_table *newtbl,
82  void *arg);
83 
84  void *object_migration_failed_arg;
85  void (*object_migration_failed_cb)(struct aura_node *node,
86  struct aura_object *failed,
87  void *arg);
88 
89  void (*unhandled_evt_cb)(struct aura_node *node,
90  struct aura_buffer *ret,
91  void *arg);
92  void *unhandled_evt_arg;
93  void *fd_changed_arg;
94  void (*fd_changed_cb)(const struct aura_pollfds *fd,
95  enum aura_fd_action act, void *arg);
96 
97 
98  /* Event system and polling */
99  int numfds; /* Currently available space for descriptors */
100  int nextfd; /* Next descriptor to add */
101  struct aura_pollfds *fds; /* descriptor and event array */
102 
103  void *eventsys_data; /* eventloop structure */
104  unsigned int poll_timeout;
105  uint64_t last_checked;
106  struct list_head eventloop_node_list;
107  const struct aura_object *current_object;
108 };
109 
110 
111 struct aura_object {
112  int id;
113  char *name;
114  char *arg_fmt;
115  char *ret_fmt;
116 
117  int valid;
118  char* arg_pprinted;
119  char* ret_pprinted;
120  int num_args;
121  int num_rets;
122 
123  int arglen;
124  int retlen;
125 
126  /* Store callbacks here.
127  TODO: Can there be several calls of the same method pending? */
128  int pending;
129  void (*calldonecb)(struct aura_node *dev, int status, struct aura_buffer *ret, void *arg);
130  void *arg;
131 };
132 
133 struct aura_eventloop {
134  int autocreated;
135  int keep_running;
136  int poll_timeout;
137  struct list_head nodelist;
138  void *eventsysdata;
139 };
140 
141 
142 #define object_is_event(o) (o->arg_fmt==NULL)
143 #define object_is_method(o) (o->arg_fmt!=NULL)
144 
145 struct aura_export_table {
146  int size;
147  int next;
148  struct aura_node *owner;
149  struct hsearch_data index;
150  struct aura_object objects[];
151 };
152 
153 
154 #define __BIT(n) (1<< n)
155 
163 {
168  const char *name;
173  uint32_t flags;
174 
186 
196 
210  int (*open)(struct aura_node *node, const char *opts);
221  void (*close)(struct aura_node *node);
240  void (*loop)(struct aura_node *node, const struct aura_pollfds *fd);
241 
253  void (*buffer_put)(struct aura_buffer *dst, struct aura_buffer *buf);
265  struct aura_buffer *(*buffer_get)(struct aura_buffer *buf);
291  struct aura_buffer *(*buffer_request)(struct aura_node *node, int size);
303  void (*buffer_release)(struct aura_buffer *buf);
304 
308  int usage;
309 
314 };
315 
333 struct aura_buffer {
335  uint32_t magic;
337  int size;
339  int pos;
341  struct aura_object *object;
343  struct aura_node *owner;
347  char *data;
348 };
349 
355 struct aura_buffer *aura_serialize(struct aura_node *node, const char *fmt, int size, va_list ap);
356 int aura_fmt_len(struct aura_node *node, const char *fmt);
357 char* aura_fmt_pretty_print(const char* fmt, int *valid, int *num_args);
358 
359 
360 const struct aura_transport *aura_transport_lookup(const char *name);
361 void aura_set_status(struct aura_node *node, int status);
362 
363 
364 #define AURA_TRANSPORT(s) \
365  void __attribute__((constructor (101))) do_reg_##s(void) { \
366  aura_transport_register(&s); \
367  }
368 
369 enum aura_endianness aura_get_host_endianness();
370 void aura_hexdump (char *desc, void *addr, int len);
371 const char *aura_get_version();
372 unsigned int aura_get_version_code();
373 
374 void aura_set_node_endian(struct aura_node *node, enum aura_endianness en);
375 
376 void aura_queue_buffer(struct list_head *list, struct aura_buffer *buf);
377 struct aura_buffer *aura_dequeue_buffer(struct list_head *head);
378 void aura_requeue_buffer(struct list_head *head, struct aura_buffer *buf);
379 struct aura_buffer *aura_peek_buffer(struct list_head *head);
380 
381 struct aura_export_table *aura_etable_create(struct aura_node *owner, int n);
382 void aura_etable_add(struct aura_export_table *tbl,
383  const char *name,
384  const char *argfmt,
385  const char *retfmt);
386 void aura_etable_activate(struct aura_export_table *tbl);
387 
388 struct aura_object *aura_etable_find(struct aura_export_table *tbl,
389  const char *name);
390 struct aura_object *aura_etable_find_id(struct aura_export_table *tbl,
391  int id);
392 
393 #define AURA_DECLARE_CACHED_ID(idvar, node, name)\
394  static int idvar = -1; \
395  if (idvar == -1) {\
396  struct aura_object *tmp = aura_etable_find(node->tbl, name); \
397  if (!tmp) \
398  BUG(node, "Static lobject lookup failed!"); \
399  idvar = tmp->id; \
400  }
401 
402 void aura_etable_destroy(struct aura_export_table *tbl);
403 
404 struct aura_node *aura_open(const char* name, const char *opts);
405 void aura_close(struct aura_node *dev);
406 
407 
408 int aura_queue_call(struct aura_node *node,
409  int id,
410  void (*calldonecb)(struct aura_node *dev, int status, struct aura_buffer *ret, void *arg),
411  void *arg,
412  struct aura_buffer *buf);
413 
415  struct aura_node *dev,
416  int id,
417  void (*calldonecb)(struct aura_node *dev, int status, struct aura_buffer *ret, void *arg),
418  void *arg,
419  ...);
420 
421 int aura_start_call(
422  struct aura_node *dev,
423  const char *name,
424  void (*calldonecb)(struct aura_node *dev, int status, struct aura_buffer *ret, void *arg),
425  void *arg,
426  ...);
427 
428 int aura_call_raw(
429  struct aura_node *dev,
430  int id,
431  struct aura_buffer **ret,
432  ...);
433 
434 int aura_call(
435  struct aura_node *dev,
436  const char *name,
437  struct aura_buffer **ret,
438  ...);
439 
441  struct aura_node *node,
442  int id,
443  void (*calldonecb)(struct aura_node *dev, int status, struct aura_buffer *ret, void *arg),
444  void *arg);
445 
447  struct aura_node *node,
448  const char *event,
449  void (*calldonecb)(struct aura_node *dev, int status, struct aura_buffer *ret, void *arg),
450  void *arg);
451 
452 void aura_enable_sync_events(struct aura_node *node, int count);
453 int aura_get_pending_events(struct aura_node *node);
454 int aura_get_next_event(struct aura_node *node, const struct aura_object ** obj, struct aura_buffer **retbuf);
455 
456 
457 void __attribute__((noreturn)) aura_panic(struct aura_node *node);
458 int BUG(struct aura_node *node, const char *msg, ...);
459 
473 uint8_t aura_buffer_get_u8 (struct aura_buffer *buf);
474 
485 uint16_t aura_buffer_get_u16(struct aura_buffer *buf);
486 
497 uint32_t aura_buffer_get_u32(struct aura_buffer *buf);
498 
509 uint64_t aura_buffer_get_u64(struct aura_buffer *buf);
510 
519 int8_t aura_buffer_get_s8 (struct aura_buffer *buf);
520 
531 int16_t aura_buffer_get_s16(struct aura_buffer *buf);
532 
543 int32_t aura_buffer_get_s32(struct aura_buffer *buf);
544 
556 int64_t aura_buffer_get_s64(struct aura_buffer *buf);
557 
568 void aura_buffer_put_u8 (struct aura_buffer *buf, uint8_t value);
569 
581 void aura_buffer_put_u16(struct aura_buffer *buf, uint16_t value);
582 
594 void aura_buffer_put_u32(struct aura_buffer *buf, uint32_t value);
595 
607 void aura_buffer_put_u64(struct aura_buffer *buf, uint64_t value);
608 
618 void aura_buffer_put_s8 (struct aura_buffer *buf, int8_t value);
619 
631 void aura_buffer_put_s16(struct aura_buffer *buf, int16_t value);
632 
644 void aura_buffer_put_s32(struct aura_buffer *buf, int32_t value);
645 
658 void aura_buffer_put_s64(struct aura_buffer *buf, int64_t value);
659 
660 
672 const void *aura_buffer_get_bin(struct aura_buffer *buf, int len);
673 
674 
687 void aura_buffer_put_bin(struct aura_buffer *buf, const void *data, int len);
688 
689 
702 struct aura_buffer *aura_buffer_get_buf(struct aura_buffer *buf);
703 
710 const void *aura_buffer_get_ptr(struct aura_buffer *buf);
711 
716 void aura_etable_changed_cb(struct aura_node *node,
717  void (*cb)(struct aura_node *node,
718  struct aura_export_table *old,
719  struct aura_export_table *newtbl,
720  void *arg),
721  void *arg);
722 
723 void aura_status_changed_cb(struct aura_node *node,
724  void (*cb)(struct aura_node *node, int newstatus, void *arg),
725  void *arg);
726 
727 void aura_fd_changed_cb(struct aura_node *node,
728  void (*cb)(const struct aura_pollfds *fd, enum aura_fd_action act, void *arg),
729  void *arg);
730 
731 void aura_unhandled_evt_cb(struct aura_node *node,
732  void (*cb)(struct aura_node *node,
733  struct aura_buffer *buf,
734  void *arg),
735  void *arg);
736 
737 void aura_object_migration_failed_cb(struct aura_node *node,
738  void (*cb)(struct aura_node *node,
739  struct aura_object *failed,
740  void *arg),
741  void *arg);
742 
743 const struct aura_object *aura_get_current_object(struct aura_node *node);
744 
745 void aura_add_pollfds(struct aura_node *node, int fd, uint32_t events);
746 void aura_del_pollfds(struct aura_node *node, int fd);
747 int aura_get_pollfds(struct aura_node *node, const struct aura_pollfds **fds);
748 
749 
759 #define aura_eventloop_create(...) \
760  aura_eventloop_create__(0, ##__VA_ARGS__, NULL)
761 
762 
769 void aura_eventloop_destroy(struct aura_eventloop *loop);
770 void *aura_eventloop_vcreate(va_list ap);
771 void *aura_eventloop_create__(int dummy, ...);
773 void aura_eventloop_add(struct aura_eventloop *loop, struct aura_node *node);
774 void aura_eventloop_del(struct aura_node *node);
775 void aura_eventloop_break(struct aura_eventloop *loop);
776 
777 
778 void aura_handle_events(struct aura_eventloop *loop);
779 void aura_handle_events_timeout(struct aura_eventloop *loop, int timeout_ms);
780 void aura_handle_events_forever(struct aura_eventloop *loop);
781 void aura_wait_status(struct aura_node *node, int status);
782 
783 struct aura_buffer *aura_buffer_request(struct aura_node *nd, int size);
784 
785 void aura_buffer_release(struct aura_buffer *buf);
786 void aura_buffer_destroy(struct aura_buffer *buf);
787 void aura_bufferpool_preheat(struct aura_node *nd, int size, int count);
788 
789 /* event system data access functions */
790 struct aura_eventloop *aura_eventloop_get_data(struct aura_node *node);
791 
792 
793 #include <aura/inlines.h>
794 
795 #define min_t(type, x, y) ({ \
796  type __min1 = (x); \
797  type __min2 = (y); \
798  __min1 < __min2 ? __min1: __min2; })
799 
800 
801 #define max_t(type, x, y) ({ \
802  type __max1 = (x); \
803  type __max2 = (y); \
804  __max1 > __max2 ? __max1: __max2; })
805 
806 
807 #endif
void aura_buffer_put_s16(struct aura_buffer *buf, int16_t value)
Put a signed 16 bit integer to aura buffer.
int aura_get_next_event(struct aura_node *node, const struct aura_object **obj, struct aura_buffer **retbuf)
Definition: aura.c:753
void aura_requeue_buffer(struct list_head *head, struct aura_buffer *buf)
Definition: queue.c:62
void(* loop)(struct aura_node *node, const struct aura_pollfds *fd)
Required.
Definition: aura.h:240
void aura_set_status(struct aura_node *node, int status)
Definition: aura.c:808
struct aura_buffer * aura_buffer_request(struct aura_node *nd, int size)
Definition: buffer.c:40
struct aura_buffer * aura_dequeue_buffer(struct list_head *head)
Definition: queue.c:45
const char * name
Required.
Definition: aura.h:168
void * aura_eventloop_vcreate(va_list ap)
Definition: eventloop.c:128
uint32_t magic
Definition: aura.h:335
int buffer_offset
Optional.
Definition: aura.h:195
void aura_buffer_put_s32(struct aura_buffer *buf, int32_t value)
Put a signed 32 bit integer to aura buffer.
struct list_head registry
Private.
Definition: aura.h:313
struct aura_node * owner
Definition: aura.h:343
void aura_handle_events_forever(struct aura_eventloop *loop)
Definition: eventloop.c:187
int64_t aura_buffer_get_s64(struct aura_buffer *buf)
Get a signed 64 bit integer from aura buffer.
void aura_buffer_put_u16(struct aura_buffer *buf, uint16_t value)
Put an unsigned 16 bit integer to aura buffer.
void aura_buffer_put_bin(struct aura_buffer *buf, const void *data, int len)
Copy data of len bytes to aura buffer from a buffer pointed by data.
Definition: retparse.c:71
void * aura_eventloop_create__(int dummy,...)
Definition: eventloop.c:151
Definition: list.h:61
void aura_eventloop_add(struct aura_eventloop *loop, struct aura_node *node)
Definition: eventloop.c:38
void(* buffer_release)(struct aura_buffer *buf)
Optional.
Definition: aura.h:303
void aura_wait_status(struct aura_node *node, int status)
Definition: aura.c:607
struct list_head qentry
Definition: aura.h:345
void * aura_eventloop_create_empty()
Definition: eventloop.c:101
int32_t aura_buffer_get_s32(struct aura_buffer *buf)
Get a signed 32 bit integer from aura buffer.
void aura_handle_events_timeout(struct aura_eventloop *loop, int timeout_ms)
Definition: eventloop.c:217
int pos
Definition: aura.h:339
int8_t aura_buffer_get_s8(struct aura_buffer *buf)
Get a signed 8 bit integer from aura buffer.
struct aura_eventloop * aura_eventloop_get_data(struct aura_node *node)
Definition: aura.c:243
void aura_buffer_put_s8(struct aura_buffer *buf, int8_t value)
Put a signed 8 bit integer to aura buffer.
void aura_buffer_put_u32(struct aura_buffer *buf, uint32_t value)
Put an unsigned 32 bit integer to aura buffer.
void aura_bufferpool_preheat(struct aura_node *nd, int size, int count)
Definition: buffer.c:147
void aura_buffer_put_u8(struct aura_buffer *buf, uint8_t value)
Put an unsigned 8 bit integer to aura buffer.
void aura_object_migration_failed_cb(struct aura_node *node, void(*cb)(struct aura_node *node, struct aura_object *failed, void *arg), void *arg)
Definition: aura.c:339
uint32_t flags
Optional.
Definition: aura.h:173
int buffer_overhead
Optional.
Definition: aura.h:185
int usage
Private.
Definition: aura.h:308
int aura_set_event_callback_raw(struct aura_node *node, int id, void(*calldonecb)(struct aura_node *dev, int status, struct aura_buffer *ret, void *arg), void *arg)
Definition: aura.c:505
void aura_handle_events(struct aura_eventloop *loop)
Definition: eventloop.c:201
uint32_t aura_buffer_get_u32(struct aura_buffer *buf)
Get an unsigned 32 bit integer from aura buffer.
void(* close)(struct aura_node *node)
Required.
Definition: aura.h:221
uint16_t aura_buffer_get_u16(struct aura_buffer *buf)
Get an unsigned 16 bit integer from aura buffer.
int aura_call_raw(struct aura_node *dev, int id, struct aura_buffer **ret,...)
Definition: aura.c:626
void aura_buffer_put_u64(struct aura_buffer *buf, uint64_t value)
Put an unsigned 64 bit integer to aura buffer.
void aura_eventloop_destroy(struct aura_eventloop *loop)
Definition: eventloop.c:166
int aura_get_pending_events(struct aura_node *node)
Definition: aura.c:730
int aura_call(struct aura_node *dev, const char *name, struct aura_buffer **ret,...)
Definition: aura.c:666
uint8_t aura_buffer_get_u8(struct aura_buffer *buf)
Get an unsigned 8 bit integer from aura buffer.
struct aura_node * aura_open(const char *name, const char *opts)
Definition: aura.c:34
void aura_fd_changed_cb(struct aura_node *node, void(*cb)(const struct aura_pollfds *fd, enum aura_fd_action act, void *arg), void *arg)
Definition: aura.c:275
const void * aura_buffer_get_ptr(struct aura_buffer *buf)
int(* open)(struct aura_node *node, const char *opts)
Required.
Definition: aura.h:210
const void * aura_buffer_get_bin(struct aura_buffer *buf, int len)
Get a pointer to the binary data block within buffer and advance internal pointer by len bytes...
Definition: retparse.c:60
int aura_start_call_raw(struct aura_node *dev, int id, void(*calldonecb)(struct aura_node *dev, int status, struct aura_buffer *ret, void *arg), void *arg,...)
Definition: aura.c:462
int size
Definition: aura.h:337
void aura_buffer_put_s64(struct aura_buffer *buf, int64_t value)
Put a signed 64 bit integer to aura buffer.
void aura_eventloop_del(struct aura_node *node)
Definition: eventloop.c:69
uint64_t aura_buffer_get_u64(struct aura_buffer *buf)
Get an unsigned 64 bit integer from aura buffer.
void aura_queue_buffer(struct list_head *list, struct aura_buffer *buf)
Definition: queue.c:16
void __attribute__((noreturn)) aura_panic(struct aura_node *node)
Definition: panic.c:33
void aura_enable_sync_events(struct aura_node *node, int count)
Definition: aura.c:711
const struct aura_object * aura_get_current_object(struct aura_node *node)
Obtain the pointer to the current aura_object.
Definition: aura.c:226
void aura_status_changed_cb(struct aura_node *node, void(*cb)(struct aura_node *node, int newstatus, void *arg), void *arg)
Definition: aura.c:258
void aura_etable_changed_cb(struct aura_node *node, void(*cb)(struct aura_node *node, struct aura_export_table *old, struct aura_export_table *new, void *arg), void *arg)
Definition: aura.c:295
void aura_unhandled_evt_cb(struct aura_node *node, void(*cb)(struct aura_node *node, struct aura_buffer *buf, void *arg), void *arg)
Definition: aura.c:314
void aura_eventloop_break(struct aura_eventloop *loop)
Definition: eventloop.c:244
int16_t aura_buffer_get_s16(struct aura_buffer *buf)
Get a signed 16 bit integer from aura buffer.
void aura_buffer_release(struct aura_buffer *buf)
Definition: buffer.c:80
void aura_close(struct aura_node *dev)
Definition: aura.c:102
void aura_set_node_endian(struct aura_node *node, enum aura_endianness en)
Definition: aura.c:870
void aura_buffer_destroy(struct aura_buffer *buf)
Definition: buffer.c:101
int aura_start_call(struct aura_node *dev, const char *name, void(*calldonecb)(struct aura_node *dev, int status, struct aura_buffer *ret, void *arg), void *arg,...)
Definition: aura.c:564
struct aura_buffer * aura_buffer_get_buf(struct aura_buffer *buf)
Retrieve aura_buffer pointer from within the buffer and advance internal pointer by it's size...
Definition: retparse.c:83
char * data
Definition: aura.h:347
struct aura_buffer * aura_peek_buffer(struct list_head *head)
Definition: queue.c:28
struct aura_object * object
Definition: aura.h:341
int aura_set_event_callback(struct aura_node *node, const char *event, void(*calldonecb)(struct aura_node *dev, int status, struct aura_buffer *ret, void *arg), void *arg)
Definition: aura.c:536
void(* buffer_put)(struct aura_buffer *dst, struct aura_buffer *buf)
Optional.
Definition: aura.h:253