source: mainline/uspace/lib/net/adt/module_map.c@ 6b82009

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6b82009 was 6b82009, checked in by Martin Decky <martin@…>, 14 years ago

networking stack: convert to the new async framework

  • Property mode set to 100644
File size: 4.1 KB
Line 
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
29/** @addtogroup libnet
30 * @{
31 */
32
33/** @file
34 * Character string to module map implementation.
35 */
36
37#include <malloc.h>
38#include <task.h>
39#include <unistd.h>
40#include <errno.h>
41#include <net/modules.h>
42#include <adt/generic_char_map.h>
43#include <adt/module_map.h>
44
45GENERIC_CHAR_MAP_IMPLEMENT(modules, module_t)
46
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.
60 *
61 */
62int add_module(module_t **module, modules_t *modules, const uint8_t *name,
63 const uint8_t *filename, services_t service, task_id_t task_id,
64 connect_module_t connect_module)
65{
66 module_t *tmp_module;
67 int rc;
68
69 tmp_module = (module_t *) malloc(sizeof(module_t));
70 if (!tmp_module)
71 return ENOMEM;
72
73 tmp_module->task_id = task_id;
74 tmp_module->sess = NULL;
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;
80
81 rc = modules_add(modules, tmp_module->name, 0, tmp_module);
82 if (rc != EOK) {
83 free(tmp_module);
84 return rc;
85 }
86
87 if (module)
88 *module = tmp_module;
89
90 return EOK;
91}
92
93/** Search and return the specified module.
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 *
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 *
105 */
106module_t *get_running_module(modules_t *modules, uint8_t *name)
107{
108 module_t *module = modules_find(modules, name, 0);
109 if (!module)
110 return NULL;
111
112 if (!module->task_id) {
113 module->task_id = net_spawn(module->filename);
114 if (!module->task_id)
115 return NULL;
116 }
117
118 if (!module->sess)
119 module->sess = module->connect_module(module->service);
120
121 return module;
122}
123
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.
130 *
131 */
132task_id_t net_spawn(const uint8_t *fname)
133{
134 task_id_t id;
135 int rc;
136
137 rc = task_spawnl(&id, (const char *) fname, (const char *) fname, NULL);
138 if (rc != EOK)
139 return 0;
140
141 return id;
142}
143
144/** @}
145 */
Note: See TracBrowser for help on using the repository browser.