Annotation of libaitrpc/src/lists.c, revision 1.9.2.2

1.2       misho       1: /*************************************************************************
                      2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.9.2.2 ! misho       6: * $Id: lists.c,v 1.9.2.1 2012/05/15 22:47:10 misho Exp $
1.2       misho       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: 
1.7       misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.2       misho      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: /*
1.7       misho      50:  * rpc_srv_registerCall() - Register call to RPC server
                     51:  *
1.2       misho      52:  * @srv = RPC Server instance
1.9.2.1   misho      53:  * @tag = Function tag
                     54:  * @funcaddr = Function address
                     55:  * @args = Number of return function arguments
                     56:  * return: -1 error, 0 already registered tag or 1 register ok
1.2       misho      57:  */
                     58: int
1.9.2.1   misho      59: rpc_srv_registerCall(rpc_srv_t * __restrict srv, u_short tag, void *funcaddr, u_short args)
1.2       misho      60: {
                     61:        rpc_func_t *func;
                     62: 
1.9.2.1   misho      63:        if (!srv || !funcaddr) {
1.7       misho      64:                rpc_SetErr(EINVAL, "Invalid parameter can`t register function");
1.2       misho      65:                return -1;
1.9.2.1   misho      66:        } else {
                     67:                /* search for duplicate */
                     68:                TAILQ_FOREACH(func, &srv->srv_funcs, func_node)
                     69:                        if (AIT_KEY(&func->func_name) == tag)
                     70:                                return 0;
1.2       misho      71:        }
1.9.2.1   misho      72: 
1.2       misho      73:        if (!(func = malloc(sizeof(rpc_func_t)))) {
                     74:                LOGERR;
                     75:                return -1;
1.9.2.1   misho      76:        } else {
1.2       misho      77:                memset(func, 0, sizeof(rpc_func_t));
1.9.2.1   misho      78:                func->func_parent = srv;
1.2       misho      79:        }
                     80: 
1.9.2.1   misho      81:        AIT_KEY(&func->func_name) = tag;
                     82:        AIT_SET_PTR(&func->func_name, funcaddr, 0);
1.2       misho      83: 
1.7       misho      84:        /* allocate return variables */
1.9.2.1   misho      85:        if (args > 0 && !(func->func_vars = io_allocVars(args))) {
1.7       misho      86:                AIT_FREE_VAL(&func->func_name);
1.2       misho      87:                free(func);
                     88:                return -1;
                     89:        }
                     90: 
1.7       misho      91:        /* add to list of functions */
1.9.2.1   misho      92:        TAILQ_INSERT_TAIL(&srv->srv_funcs, func, func_node);
                     93:        return 1;
1.2       misho      94: }
                     95: 
                     96: /*
1.7       misho      97:  * rpc_srv_unregisterCall() - Unregister call from RPC server
                     98:  *
1.2       misho      99:  * @srv = RPC Server instance
1.9.2.1   misho     100:  * @tag = Function tag
1.2       misho     101:  * return: -1 error, 0 not found call, 1 unregister ok
                    102:  */
                    103: int
1.9.2.1   misho     104: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, u_short tag)
1.2       misho     105: {
1.9.2.1   misho     106:        rpc_func_t *f;
1.2       misho     107: 
1.9.2.1   misho     108:        if (!srv) {
1.7       misho     109:                rpc_SetErr(EINVAL, "Invalid parameter can`t unregister function");
1.2       misho     110:                return -1;
1.9.2.1   misho     111:        }
1.2       misho     112: 
1.9.2.1   misho     113:        f = rpc_srv_getCall(srv, tag);
1.9       misho     114:        if (!f)                 /* not found element for unregister */
1.2       misho     115:                return 0;
                    116: 
1.9.2.1   misho     117:        TAILQ_REMOVE(&srv->srv_funcs, f, func_node);
                    118: 
1.6       misho     119:        io_freeVars(&f->func_vars);
1.7       misho     120:        AIT_FREE_VAL(&f->func_name);
1.5       misho     121:        free(f);
1.2       misho     122:        return 1;
                    123: }
                    124: 
                    125: /*
1.7       misho     126:  * rpc_srv_getCall()  - Get registered call from RPC server
                    127:  *
1.2       misho     128:  * @srv = RPC Server instance
                    129:  * @tag = tag for function
                    130:  * return: NULL not found call, !=NULL return call
                    131:  */
                    132: inline rpc_func_t *
1.9.2.1   misho     133: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag)
1.2       misho     134: {
1.9.2.1   misho     135:        rpc_func_t *f, *tmp;
1.2       misho     136: 
                    137:        if (!srv) {
1.7       misho     138:                rpc_SetErr(EINVAL, "Invalid parameter can`t get function");
1.2       misho     139:                return NULL;
                    140:        }
                    141: 
1.9.2.1   misho     142:        TAILQ_FOREACH_SAFE(f, &srv->srv_funcs, func_node, tmp)
                    143:                if (AIT_KEY(&f->func_name) == tag)
1.2       misho     144:                        break;
                    145: 
                    146:        return f;
                    147: }
                    148: 
1.9.2.1   misho     149: /* --------------------------------------------------------- */
1.2       misho     150: 
1.9.2.2 ! misho     151: #if 0
1.2       misho     152: /*
1.7       misho     153:  * rpc_srv_getBLOB() - Get registered BLOB 
                    154:  *
1.2       misho     155:  * @srv = RPC Server instance
                    156:  * @var = hash for variable
                    157:  * return: NULL not found, !=NULL return blob var
                    158:  */
                    159: inline rpc_blob_t *
                    160: rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var)
                    161: {
                    162:        rpc_blob_t *b;
                    163: 
                    164:        if (!srv) {
1.7       misho     165:                rpc_SetErr(EINVAL, "Invalid parameter can`t get BLOB variable");
1.2       misho     166:                return NULL;
                    167:        }
                    168: 
                    169:        for (b = srv->srv_blob.blobs; b; b = b->blob_next) {
                    170:                if (b->blob_var == var)
                    171:                        break;
                    172:        }
                    173: 
                    174:        return b;
                    175: }
                    176: 
                    177: /*
1.7       misho     178:  * rpc_srv_registerBLOB() - Register new BLOB to server
                    179:  *
1.2       misho     180:  * @srv = RPC Server instance
                    181:  * @len = BLOB length
                    182:  * return: NULL error or new registered BLOB
                    183:  */
                    184: rpc_blob_t *
                    185: rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len)
                    186: {
                    187:        rpc_blob_t *blob = NULL;
                    188: 
                    189:        if (!srv || !len) {
1.7       misho     190:                rpc_SetErr(EINVAL, "Invalid parameter can`t register BLOB variable");
1.2       misho     191:                return blob;
                    192:        }
                    193: 
                    194:        blob = rpc_srv_blobCreate(srv, len);
                    195:        if (blob) {
                    196:                blob->blob_next = srv->srv_blob.blobs;
                    197:                srv->srv_blob.blobs = blob;
                    198:        }
                    199: 
                    200:        return blob;
                    201: }
                    202: 
                    203: /*
1.7       misho     204:  * rpc_srv_unregisterBLOB() - Unregister BLOB from server
                    205:  *
1.2       misho     206:  * @srv = RPC Server instance
                    207:  * @var = BLOB Variable for unregister
                    208:  * return: -1 error, 0 not found call, 1 unregister ok
                    209:  */
                    210: int
                    211: rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var)
                    212: {
                    213:        rpc_blob_t *b, *curr;
                    214: 
                    215:        if (!srv) {
1.7       misho     216:                rpc_SetErr(EINVAL, "Invalid parameter can`t unregister BLOB variable");
1.2       misho     217:                return -1;
                    218:        }
                    219: 
                    220:        b = rpc_srv_getBLOB(srv, var);
1.3       misho     221:        if (!b)                         /* not found element for unregister */
1.2       misho     222:                return 0;
1.5       misho     223:        /* if BLOB is unmapped force to unmap object */
                    224:        if (b->blob_data)
                    225:                rpc_srv_blobUnmap(b);
1.2       misho     226: 
1.3       misho     227:        if (srv->srv_blob.blobs == b) { /* if is 1st element */
1.2       misho     228:                srv->srv_blob.blobs = srv->srv_blob.blobs->blob_next;
                    229:        } else {
                    230:                for (curr = srv->srv_blob.blobs; curr->blob_next != b; curr = curr->blob_next);
                    231:                curr->blob_next = curr->blob_next->blob_next;
                    232:        }
                    233:        rpc_srv_blobFree(srv, b);
                    234:        free(b);
                    235: 
                    236:        return 1;
                    237: }
1.9.2.2 ! misho     238: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>