[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[25d2de69] | 29 | /** @addtogroup libnet
|
---|
| 30 | * @{
|
---|
[21580dd] | 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
[25d2de69] | 34 | * Character string to module map implementation.
|
---|
[21580dd] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <malloc.h>
|
---|
| 38 | #include <task.h>
|
---|
| 39 | #include <unistd.h>
|
---|
[0ab68f6] | 40 | #include <errno.h>
|
---|
[c7a8442] | 41 | #include <net/modules.h>
|
---|
[849ed54] | 42 | #include <adt/generic_char_map.h>
|
---|
| 43 | #include <adt/module_map.h>
|
---|
[21580dd] | 44 |
|
---|
[aadf01e] | 45 | GENERIC_CHAR_MAP_IMPLEMENT(modules, module_t)
|
---|
[21580dd] | 46 |
|
---|
[6b82009] | 47 | /** Add module to the module map.
|
---|
| 48 | *
|
---|
| 49 | * @param[out] module Module structure added.
|
---|
| 50 | * @param[in] modules Module map.
|
---|
| 51 | * @param[in] name Module name.
|
---|
| 52 | * @param[in] filename Full path filename.
|
---|
| 53 | * @param[in] service Module service.
|
---|
| 54 | * @param[in] task_id Module current task identifier.
|
---|
| 55 | * Zero means not running.
|
---|
| 56 | * @param[in] connect_module Module connecting function.
|
---|
| 57 | *
|
---|
| 58 | * @return EOK on success.
|
---|
| 59 | * @return ENOMEM if there is not enough memory left.
|
---|
[25d2de69] | 60 | *
|
---|
| 61 | */
|
---|
[6b82009] | 62 | int add_module(module_t **module, modules_t *modules, const uint8_t *name,
|
---|
[61bfc370] | 63 | const uint8_t *filename, services_t service, task_id_t task_id,
|
---|
[25d2de69] | 64 | connect_module_t connect_module)
|
---|
| 65 | {
|
---|
[88a1bb9] | 66 | module_t *tmp_module;
|
---|
[0ab68f6] | 67 | int rc;
|
---|
[6b82009] | 68 |
|
---|
[88a1bb9] | 69 | tmp_module = (module_t *) malloc(sizeof(module_t));
|
---|
[25d2de69] | 70 | if (!tmp_module)
|
---|
[aadf01e] | 71 | return ENOMEM;
|
---|
[6b82009] | 72 |
|
---|
[21580dd] | 73 | tmp_module->task_id = task_id;
|
---|
[6b82009] | 74 | tmp_module->sess = NULL;
|
---|
[21580dd] | 75 | tmp_module->usage = 0;
|
---|
| 76 | tmp_module->name = name;
|
---|
| 77 | tmp_module->filename = filename;
|
---|
| 78 | tmp_module->service = service;
|
---|
| 79 | tmp_module->connect_module = connect_module;
|
---|
[6b82009] | 80 |
|
---|
[0ab68f6] | 81 | rc = modules_add(modules, tmp_module->name, 0, tmp_module);
|
---|
| 82 | if (rc != EOK) {
|
---|
[aadf01e] | 83 | free(tmp_module);
|
---|
[0ab68f6] | 84 | return rc;
|
---|
[21580dd] | 85 | }
|
---|
[6b82009] | 86 |
|
---|
[25d2de69] | 87 | if (module)
|
---|
[aadf01e] | 88 | *module = tmp_module;
|
---|
[6b82009] | 89 |
|
---|
[21580dd] | 90 | return EOK;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[6b82009] | 93 | /** Search and return the specified module.
|
---|
[25d2de69] | 94 | *
|
---|
| 95 | * If the module is not running, the module filaname is spawned.
|
---|
| 96 | * If the module is not connected, the connect_function is called.
|
---|
| 97 | *
|
---|
[6b82009] | 98 | * @param[in] modules Module map.
|
---|
| 99 | * @param[in] name Module name.
|
---|
| 100 | *
|
---|
| 101 | * @return The running module found. It does not have to be
|
---|
| 102 | * connected.
|
---|
| 103 | * @return NULL if there is no such module.
|
---|
| 104 | *
|
---|
[25d2de69] | 105 | */
|
---|
[61bfc370] | 106 | module_t *get_running_module(modules_t *modules, uint8_t *name)
|
---|
[25d2de69] | 107 | {
|
---|
[6b82009] | 108 | module_t *module = modules_find(modules, name, 0);
|
---|
[25d2de69] | 109 | if (!module)
|
---|
[aadf01e] | 110 | return NULL;
|
---|
[6b82009] | 111 |
|
---|
[25d2de69] | 112 | if (!module->task_id) {
|
---|
[61bfc370] | 113 | module->task_id = net_spawn(module->filename);
|
---|
[25d2de69] | 114 | if (!module->task_id)
|
---|
[aadf01e] | 115 | return NULL;
|
---|
[21580dd] | 116 | }
|
---|
[6b82009] | 117 |
|
---|
| 118 | if (!module->sess)
|
---|
| 119 | module->sess = module->connect_module(module->service);
|
---|
| 120 |
|
---|
[21580dd] | 121 | return module;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[61bfc370] | 124 | /** Start the given module.
|
---|
| 125 | *
|
---|
| 126 | * @param[in] fname The module full or relative path filename.
|
---|
| 127 | *
|
---|
| 128 | * @return The new module task identifier on success.
|
---|
| 129 | * @return Zero if there is no such module.
|
---|
[25d2de69] | 130 | *
|
---|
| 131 | */
|
---|
[61bfc370] | 132 | task_id_t net_spawn(const uint8_t *fname)
|
---|
[d9fae235] | 133 | {
|
---|
[0485135] | 134 | task_id_t id;
|
---|
| 135 | int rc;
|
---|
[d9fae235] | 136 |
|
---|
[61bfc370] | 137 | rc = task_spawnl(&id, (const char *) fname, (const char *) fname, NULL);
|
---|
[0485135] | 138 | if (rc != EOK)
|
---|
| 139 | return 0;
|
---|
[d9fae235] | 140 |
|
---|
[0485135] | 141 | return id;
|
---|
[21580dd] | 142 | }
|
---|
| 143 |
|
---|
| 144 | /** @}
|
---|
| 145 | */
|
---|