1 /*
2  * Copyright (C) 2019-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef USERVICE_INTERNAL_H
6 #define USERVICE_INTERNAL_H
7 
8 #include <stdint.h>
9 #include <aos/list.h>
10 #include <aos/kernel.h>
11 #include <uservice/uservice.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 struct uservice {
18     const char *name;
19     void       *context;
20     process_t   process_rpc;
21 
22     utask_t    *task;
23     slist_t     next;
24 };
25 
26 struct rpc_record {
27     uint32_t cmd_id;
28     uint32_t count;
29     struct uservice *srv;
30     slist_t next;
31 };
32 
33 struct utask {
34     aos_task_t  task;
35     int8_t      running;
36 
37     uint8_t     queue_count;
38 #if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_UTASK)
39     uint8_t     queue_max_used;
40     slist_t     rpc_reclist;
41 #endif
42     void       *queue_buffer;
43     aos_queue_t queue;
44     aos_mutex_t mutex;
45     rpc_t      *current_rpc;
46     aos_sem_t   running_wait;
47 
48     slist_t     uservice_lists;
49     slist_t     rpc_buffer_gc_cache;
50     slist_t     node;
51 };
52 
53 struct rpc_buffer {
54     int       timeout_ms;
55     uint8_t * buffer;
56     uint16_t  buf_size;
57     uint16_t  pos;
58     aos_sem_t sem;
59 
60     slist_t  next;
61 };
62 
63 #define TASK_LOCK(task) aos_mutex_lock(&(task->mutex), AOS_WAIT_FOREVER)
64 #define TASK_UNLOCK(task) aos_mutex_unlock(&(task->mutex))
65 
66 void rpc_free(rpc_t *rpc);
67 int  rpc_wait(rpc_t *rpc);
68 
69 typedef struct event_list {
70     slist_t     events;
71     aos_mutex_t mutex;
72 } event_list_t;
73 
74 int  eventlist_init(event_list_t *evlist);
75 void eventlist_uninit(event_list_t *evlist);
76 
77 int  eventlist_subscribe(event_list_t *evlist, uint32_t event_id, event_callback_t cb, void *context);
78 int  eventlist_unsubscribe(event_list_t *evlist, uint32_t event_id, event_callback_t cb, void *context);
79 int  eventlist_publish(event_list_t *evlist, uint32_t event_id, void *data);
80 int  eventlist_remove(event_list_t *evlist, uint32_t event_id);
81 
82 int  eventlist_subscribe_fd(event_list_t *evlist, uint32_t fd, event_callback_t cb, void *context);
83 int  eventlist_unsubscribe_fd(event_list_t *evlist, uint32_t fd, event_callback_t cb, void *context);
84 int  eventlist_publish_fd(event_list_t *evlist, uint32_t fd, void *data);
85 int  eventlist_remove_fd(event_list_t *evlist, uint32_t fd);
86 
87 int  eventlist_setfd(event_list_t *evlist, void *readfds);
88 
89 #ifdef __cplusplus
90 }
91 #endif
92 
93 #endif
94