1 /*
2  * Copyright (C) 2019-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef YOC_USERVICE_H
6 #define YOC_USERVICE_H
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 
12 #include <uservice/event.h>
13 
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #define QUEUE_MSG_COUNT 8
20 
21 typedef struct uservice   uservice_t;
22 typedef struct utask      utask_t;
23 typedef struct rpc_buffer rpc_buffer_t;
24 
25 /**
26  * rpc_t - remote procedure call struct
27  *
28  * @srv:
29  * @cmd_id:
30  * @timeout_ms:
31  * @data:
32  */
33 typedef struct _rpc_t {
34     uservice_t   *srv;
35     uint32_t      cmd_id;
36     rpc_buffer_t *data;
37 } rpc_t;
38 
39 typedef int (*process_t)(void *context, rpc_t *rpc);
40 
41 typedef struct {
42     int cmd_id;
43     process_t process;
44 } rpc_process_t;
45 
46 /**
47  * rpc_init() - initialise a rpc
48  *
49  * This function initialise a rpc. Returns zero or a negative error code
50  *
51  * @rpc: the rpc to initialise, Cannot be an empty pointer.
52  * @cmd_id: the rpc command id
53  * @timeout_ms: the rpc call wait time.
54  *   >0: wait time (ms)
55  *   =0: is asynchronous rpc comamnd
56  *   =AOS_WAIT_FOREVER: is synchronous rpc command
57  *
58  * Returns:
59  *   %0 on success
60  *   -%ENOMEM when memory allocation fails.
61  *   -%EINVAL on an invalid param.
62  *
63  */
64 int  rpc_init(rpc_t *rpc, int cmd_id, int timeout_ms);
65 
66 /**
67  * rpc_put_reset - reset rpc data buffer
68  *
69  * This function destroy the rpc data buffer, and reset put status
70  *
71  * @rpc: the initialized rpc pointer, cannot be an empty pointer
72  *
73  */
74 void rpc_put_reset(rpc_t *rpc);
75 
76 /**
77  * rpc_put_int - put integer value into the rpc
78  * rpc_put_uint8 - put uint8_t value into the rpc
79  * rpc_put_double - put double value into the rpc
80  * rpc_put_point - put point value into the rpc
81  *
82  * @rpc: the initialized rpc pointer, cannot be an empty pointer
83  * @v: the value
84  *
85  * Returns:
86  *   -%0 on success
87  *   -%ENOMEM when memory allocation fails.
88  */
89 int rpc_put_int(rpc_t *rpc, int v);
90 int rpc_put_uint8(rpc_t *rpc, uint8_t v);
91 int rpc_put_double(rpc_t *rpc, double v);
92 int rpc_put_point(rpc_t *rpc, const void *v);
93 
94 /**
95  * rpc_put_buffer - put buffer into the rpc
96  *
97  * @rpc: the initialized rpc pointer, cannot be an empty pointer
98  * @data: buffer pointer
99  * @size: buffer memory size
100  *
101  * Returns:
102  *   -%0 on success
103  *   -%ENOMEM when memory allocation fails.
104  *   -%EINVAL on an invalid data or size, data is NULL or size is zero
105  */
106 int rpc_put_buffer(rpc_t *rpc, const void *data, size_t size);
107 
108 /**
109  * rpc_put_buffer - put string into the rpc
110  *
111  * @rpc: the initialized rpc pointer, cannot be an empty pointer
112  * @str: string pointer
113  *
114  * Returns:
115  *   -%0 on success
116  *   -%ENOMEM when memory allocation fails.
117  *   -%EINVAL on an invalid str pointer
118  */
119 int rpc_put_string(rpc_t *rpc, char *str);
120 
121 /**
122  * rpc_get_reset - reset rpc data buffer
123  *
124  * This function destroy the rpc data buffer, and reset put status
125  *
126  * @rpc: the initialized rpc pointer, cannot be an empty pointer
127  *
128  */
129 void rpc_get_reset(rpc_t *rpc);
130 
131 /**
132  * rpc_get_int - put integer value into the rpc
133  * rpc_get_uint8 - put uint8_t value into the rpc
134  * rpc_get_double - put double value into the rpc
135  * rpc_get_string - put point value into the rpc
136  * rpc_get_point
137  *
138  * @rpc: the initialized rpc pointer, cannot be an empty pointer
139  * @v: the value
140  *
141  * Returns:
142  *   -%0 on success
143  *   -%ENOMEM when memory allocation fails.
144  */
145 int     rpc_get_int(rpc_t *rpc);
146 uint8_t rpc_get_uint8(rpc_t *rpc);
147 double  rpc_get_double(rpc_t *rpc);
148 char   *rpc_get_string(rpc_t *rpc);
149 void   *rpc_get_point(rpc_t *rpc);
150 
151 
152 void   *rpc_get_buffer(rpc_t *rpc, int *count);
153 
154 void rpc_reply(rpc_t *rpc);
155 void rpc_deinit(rpc_t *rpc);
156 
157 uservice_t *uservice_new(const char *name, process_t process_rpc, void *context);
158 void        uservice_destroy(uservice_t *srv);
159 
160 int      uservice_call_sync(uservice_t *srv, int cmd, void *param, void *resp, size_t resp_size);
161 int      uservice_call_async(uservice_t *srv, int cmd, void *param, size_t param_size);
162 int      uservice_call(uservice_t *srv, rpc_t *rpc);
163 void     uservice_lock(uservice_t *srv);
164 void     uservice_unlock(uservice_t *srv);
165 int      uservice_process(void *context, rpc_t *rpc, const rpc_process_t rpcs[]);
166 void     uservice_subscribe(uservice_t *srv, uint32_t event_id);
167 
168 utask_t  *utask_new(const char *name, size_t stack_size, int queue_length, int prio);
169 void     utask_destroy(utask_t *task);
170 void     utask_join(utask_t *task);
171 void     utask_add(utask_t *task, uservice_t *srv);
172 void     utask_remove(utask_t *task, uservice_t *srv);
173 void     utask_lock(utask_t *task);
174 void     utask_unlock(utask_t *task);
175 void     utask_set_softwdt_timeout(int ms);
176 
177 int  event_service_init(utask_t *task);
178 
179 void event_subscribe(uint32_t event_id, event_callback_t cb, void *context);
180 void event_unsubscribe(uint32_t event_id, event_callback_t cb, void *context);
181 void event_publish(uint32_t event_id, void *data);
182 void event_publish_delay(uint32_t event_id, void *data, int timeout_ms);
183 
184 void event_subscribe_fd(uint32_t fd, event_callback_t cb, void *context);
185 void event_unsubscribe_fd(uint32_t fd, event_callback_t cb, void *context);
186 void event_publish_fd(uint32_t fd, void *data, int sync);
187 
188 void tasks_debug();
189 
190 #ifdef __cplusplus
191 }
192 #endif
193 
194 #endif
195