[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>
|
---|
[21580dd] | 41 |
|
---|
| 42 | #include <ipc/services.h>
|
---|
| 43 |
|
---|
[c7a8442] | 44 | #include <net/modules.h>
|
---|
[21580dd] | 45 |
|
---|
[849ed54] | 46 | #include <adt/generic_char_map.h>
|
---|
| 47 | #include <adt/module_map.h>
|
---|
[21580dd] | 48 |
|
---|
[aadf01e] | 49 | GENERIC_CHAR_MAP_IMPLEMENT(modules, module_t)
|
---|
[21580dd] | 50 |
|
---|
[25d2de69] | 51 | /** Adds module to the module map.
|
---|
| 52 | *
|
---|
| 53 | * @param[out] module The module structure added.
|
---|
| 54 | * @param[in] modules The module map.
|
---|
| 55 | * @param[in] name The module name.
|
---|
| 56 | * @param[in] filename The full path filename.
|
---|
| 57 | * @param[in] service The module service.
|
---|
| 58 | * @param[in] task_id The module current task identifier. Zero means not
|
---|
| 59 | * running.
|
---|
| 60 | * @param[in] connect_module The module connecting function.
|
---|
[1bfd3d3] | 61 | * @return EOK on success.
|
---|
| 62 | * @return ENOMEM if there is not enough memory left.
|
---|
[25d2de69] | 63 | */
|
---|
| 64 | int
|
---|
[61bfc370] | 65 | add_module(module_t **module, modules_t *modules, const uint8_t *name,
|
---|
| 66 | const uint8_t *filename, services_t service, task_id_t task_id,
|
---|
[25d2de69] | 67 | connect_module_t connect_module)
|
---|
| 68 | {
|
---|
[88a1bb9] | 69 | module_t *tmp_module;
|
---|
[0ab68f6] | 70 | int rc;
|
---|
[21580dd] | 71 |
|
---|
[88a1bb9] | 72 | tmp_module = (module_t *) malloc(sizeof(module_t));
|
---|
[25d2de69] | 73 | if (!tmp_module)
|
---|
[aadf01e] | 74 | return ENOMEM;
|
---|
[25d2de69] | 75 |
|
---|
[21580dd] | 76 | tmp_module->task_id = task_id;
|
---|
| 77 | tmp_module->phone = 0;
|
---|
| 78 | tmp_module->usage = 0;
|
---|
| 79 | tmp_module->name = name;
|
---|
| 80 | tmp_module->filename = filename;
|
---|
| 81 | tmp_module->service = service;
|
---|
| 82 | tmp_module->connect_module = connect_module;
|
---|
[25d2de69] | 83 |
|
---|
[0ab68f6] | 84 | rc = modules_add(modules, tmp_module->name, 0, tmp_module);
|
---|
| 85 | if (rc != EOK) {
|
---|
[aadf01e] | 86 | free(tmp_module);
|
---|
[0ab68f6] | 87 | return rc;
|
---|
[21580dd] | 88 | }
|
---|
[25d2de69] | 89 | if (module)
|
---|
[aadf01e] | 90 | *module = tmp_module;
|
---|
[25d2de69] | 91 |
|
---|
[21580dd] | 92 | return EOK;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[25d2de69] | 95 | /** Searches and returns the specified module.
|
---|
| 96 | *
|
---|
| 97 | * If the module is not running, the module filaname is spawned.
|
---|
| 98 | * If the module is not connected, the connect_function is called.
|
---|
| 99 | *
|
---|
| 100 | * @param[in] modules The module map.
|
---|
| 101 | * @param[in] name The module name.
|
---|
[1bfd3d3] | 102 | * @return The running module found. It does not have to be
|
---|
[25d2de69] | 103 | * connected.
|
---|
[1bfd3d3] | 104 | * @return NULL if there is no such module.
|
---|
[25d2de69] | 105 | */
|
---|
[61bfc370] | 106 | module_t *get_running_module(modules_t *modules, uint8_t *name)
|
---|
[25d2de69] | 107 | {
|
---|
[88a1bb9] | 108 | module_t *module;
|
---|
[21580dd] | 109 |
|
---|
[aadf01e] | 110 | module = modules_find(modules, name, 0);
|
---|
[25d2de69] | 111 | if (!module)
|
---|
[aadf01e] | 112 | return NULL;
|
---|
[25d2de69] | 113 |
|
---|
| 114 | if (!module->task_id) {
|
---|
[61bfc370] | 115 | module->task_id = net_spawn(module->filename);
|
---|
[25d2de69] | 116 | if (!module->task_id)
|
---|
[aadf01e] | 117 | return NULL;
|
---|
[21580dd] | 118 | }
|
---|
[25d2de69] | 119 | if (!module->phone)
|
---|
[aadf01e] | 120 | module->phone = module->connect_module(module->service);
|
---|
[25d2de69] | 121 |
|
---|
[21580dd] | 122 | return module;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[61bfc370] | 125 | /** Start the given module.
|
---|
| 126 | *
|
---|
| 127 | * @param[in] fname The module full or relative path filename.
|
---|
| 128 | *
|
---|
| 129 | * @return The new module task identifier on success.
|
---|
| 130 | * @return Zero if there is no such module.
|
---|
[25d2de69] | 131 | *
|
---|
| 132 | */
|
---|
[61bfc370] | 133 | task_id_t net_spawn(const uint8_t *fname)
|
---|
[d9fae235] | 134 | {
|
---|
[0485135] | 135 | task_id_t id;
|
---|
| 136 | int rc;
|
---|
[d9fae235] | 137 |
|
---|
[61bfc370] | 138 | rc = task_spawnl(&id, (const char *) fname, (const char *) fname, NULL);
|
---|
[0485135] | 139 | if (rc != EOK)
|
---|
| 140 | return 0;
|
---|
[d9fae235] | 141 |
|
---|
[0485135] | 142 | return id;
|
---|
[21580dd] | 143 | }
|
---|
| 144 |
|
---|
| 145 | /** @}
|
---|
| 146 | */
|
---|