1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: lists.c,v 1.10 2012/05/19 00:29:51 misho Exp $
7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
49: /*
50: * rpc_srv_registerCall() - Register call to RPC server
51: *
52: * @srv = RPC Server instance
53: * @tag = Function tag
54: * @funcaddr = Function address
55: * return: -1 error, 0 already registered tag or 1 register ok
56: */
57: int
58: rpc_srv_registerCall(rpc_srv_t * __restrict srv, u_short tag, void *funcaddr)
59: {
60: rpc_func_t *func;
61:
62: if (!srv || !funcaddr) {
63: rpc_SetErr(EINVAL, "Invalid parameter can`t register function");
64: return -1;
65: } else {
66: /* search for duplicate */
67: TAILQ_FOREACH(func, &srv->srv_funcs, func_node)
68: if (AIT_KEY(&func->func_name) == tag)
69: return 0;
70: }
71:
72: if (!(func = malloc(sizeof(rpc_func_t)))) {
73: LOGERR;
74: return -1;
75: } else {
76: memset(func, 0, sizeof(rpc_func_t));
77: func->func_parent = srv;
78: }
79:
80: AIT_KEY(&func->func_name) = tag;
81: AIT_SET_PTR(&func->func_name, funcaddr, 0);
82:
83: /* add to list of functions */
84: TAILQ_INSERT_TAIL(&srv->srv_funcs, func, func_node);
85: return 1;
86: }
87:
88: /*
89: * rpc_srv_unregisterCall() - Unregister call from RPC server
90: *
91: * @srv = RPC Server instance
92: * @tag = Function tag
93: * return: -1 error, 0 not found call, 1 unregister ok
94: */
95: int
96: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, u_short tag)
97: {
98: rpc_func_t *f;
99:
100: if (!srv) {
101: rpc_SetErr(EINVAL, "Invalid parameter can`t unregister function");
102: return -1;
103: }
104:
105: f = rpc_srv_getCall(srv, tag);
106: if (!f) /* not found element for unregister */
107: return 0;
108:
109: TAILQ_REMOVE(&srv->srv_funcs, f, func_node);
110:
111: AIT_FREE_VAL(&f->func_name);
112: free(f);
113: return 1;
114: }
115:
116: /*
117: * rpc_srv_getCall() - Get registered call from RPC server
118: *
119: * @srv = RPC Server instance
120: * @tag = tag for function
121: * return: NULL not found call, !=NULL return call
122: */
123: inline rpc_func_t *
124: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag)
125: {
126: rpc_func_t *f, *tmp;
127:
128: if (!srv) {
129: rpc_SetErr(EINVAL, "Invalid parameter can`t get function");
130: return NULL;
131: }
132:
133: TAILQ_FOREACH_SAFE(f, &srv->srv_funcs, func_node, tmp)
134: if (AIT_KEY(&f->func_name) == tag)
135: break;
136:
137: return f;
138: }
139:
140: /* --------------------------------------------------------- */
141:
142: /*
143: * rpc_srv_getBLOB() - Get registered BLOB
144: *
145: * @srv = RPC Server instance
146: * @var = hash for variable
147: * return: NULL not found, !=NULL return blob var
148: */
149: inline rpc_blob_t *
150: rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var)
151: {
152: rpc_blob_t *b, *tmp;
153:
154: if (!srv) {
155: rpc_SetErr(EINVAL, "Invalid parameter can`t get BLOB variable");
156: return NULL;
157: }
158:
159: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp)
160: if (b->blob_var == var)
161: break;
162:
163: return b;
164: }
165:
166: /*
167: * rpc_srv_registerBLOB() - Register new BLOB to server
168: *
169: * @srv = RPC Server instance
170: * @len = BLOB length
171: * return: NULL error or new registered BLOB
172: */
173: rpc_blob_t *
174: rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len)
175: {
176: rpc_blob_t *blob = NULL;
177:
178: if (!srv || !len) {
179: rpc_SetErr(EINVAL, "Invalid parameter can`t register BLOB variable");
180: return blob;
181: }
182:
183: blob = rpc_srv_blobCreate(srv, len);
184:
185: TAILQ_INSERT_TAIL(&srv->srv_blob.blobs, blob, blob_node);
186: return blob;
187: }
188:
189: /*
190: * rpc_srv_unregisterBLOB() - Unregister BLOB from server
191: *
192: * @srv = RPC Server instance
193: * @var = BLOB Variable for unregister
194: * return: -1 error, 0 not found call, 1 unregister ok
195: */
196: int
197: rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var)
198: {
199: rpc_blob_t *b;
200:
201: if (!srv) {
202: rpc_SetErr(EINVAL, "Invalid parameter can`t unregister BLOB variable");
203: return -1;
204: }
205:
206: b = rpc_srv_getBLOB(srv, var);
207: if (!b) /* not found element for unregister */
208: return 0;
209:
210: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
211:
212: rpc_srv_blobFree(srv, b);
213: free(b);
214: return 1;
215: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>