source: mainline/uspace/srv/devman/main.c@ bda60d9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bda60d9 was bda60d9, checked in by Lenka Trochtova <trochtova.lenka@…>, 15 years ago

adding child device - parts of code

  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[e2b9a993]1/*
2 * Copyright (c) 2010 Lenka Trochtova
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/**
30 * @defgroup devman Device manager.
31 * @brief HelenOS device manager.
32 * @{
33 */
34
35/** @file
36 */
37
38#include <assert.h>
39#include <ipc/services.h>
40#include <ipc/ns.h>
41#include <async.h>
42#include <stdio.h>
43#include <errno.h>
44#include <bool.h>
45#include <fibril_synch.h>
46#include <stdlib.h>
47#include <string.h>
48#include <dirent.h>
49#include <fcntl.h>
50#include <sys/stat.h>
51#include <ctype.h>
52#include <ipc/devman.h>
53
54#include "devman.h"
55
56#define DRIVER_DEFAULT_STORE "/srv/drivers"
57
[0c3666d]58static driver_list_t drivers_list;
[e2b9a993]59static dev_tree_t device_tree;
60
[729fa2d6]61/**
62 * Register running driver.
63 */
64static driver_t * devman_driver_register(void)
65{
66 printf(NAME ": devman_driver_register \n");
67
68 ipc_call_t icall;
69 ipc_callid_t iid = async_get_call(&icall);
70 driver_t *driver = NULL;
71
72 if (IPC_GET_METHOD(icall) != DEVMAN_DRIVER_REGISTER) {
73 ipc_answer_0(iid, EREFUSED);
74 return NULL;
75 }
76
[92413de]77 char *drv_name = NULL;
[729fa2d6]78
79 // Get driver name
[92413de]80 int rc = async_string_receive(&drv_name, DEVMAN_NAME_MAXLEN, NULL);
[729fa2d6]81 if (rc != EOK) {
82 ipc_answer_0(iid, rc);
83 return NULL;
84 }
85 printf(NAME ": the %s driver is trying to register by the service.\n", drv_name);
86
87 // Find driver structure
88 driver = find_driver(&drivers_list, drv_name);
[92413de]89
90 free(drv_name);
91 drv_name = NULL;
92
[729fa2d6]93 if (NULL == driver) {
94 printf(NAME ": no driver named %s was found.\n", drv_name);
95 ipc_answer_0(iid, ENOENT);
96 return NULL;
97 }
98 printf(NAME ": registering the running instance of the %s driver.\n", driver->name);
99
100 // Create connection to the driver
101 printf(NAME ": creating connection to the %s driver.\n", driver->name);
102 ipc_call_t call;
103 ipc_callid_t callid = async_get_call(&call);
104 if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
105 ipc_answer_0(callid, ENOTSUP);
106 ipc_answer_0(iid, ENOTSUP);
107 return NULL;
108 }
109
[c16cf62]110 // remember driver's phone
111 set_driver_phone(driver, IPC_GET_ARG5(call));
[729fa2d6]112
113 printf(NAME ": the %s driver was successfully registered as running.\n", driver->name);
114
115 ipc_answer_0(callid, EOK);
116
117 ipc_answer_0(iid, EOK);
118
119 return driver;
120}
[e2b9a993]121
[bda60d9]122static void devman_add_child(ipc_callid_t callid, ipc_call_t *call, driver_t *driver)
123{
124 printf(NAME ": devman_add_child\n");
125
126 // TODO
127
128}
129
[e2b9a993]130/** Function for handling connections to device manager.
131 *
132 */
[924c75e1]133static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
[729fa2d6]134{
[e2b9a993]135 /* Accept the connection */
136 ipc_answer_0(iid, EOK);
137
[729fa2d6]138 driver_t *driver = devman_driver_register();
139 if (driver == NULL)
140 return;
141
[c16cf62]142 initialize_running_driver(driver);
[729fa2d6]143
144 ipc_callid_t callid;
145 ipc_call_t call;
146 bool cont = true;
147 while (cont) {
[e2b9a993]148 callid = async_get_call(&call);
149
150 switch (IPC_GET_METHOD(call)) {
[729fa2d6]151 case IPC_M_PHONE_HUNGUP:
152 cont = false;
153 continue;
[e2b9a993]154 case DEVMAN_ADD_CHILD_DEVICE:
[bda60d9]155 devman_add_child(callid, &call, driver);
[e2b9a993]156 break;
157 default:
158 ipc_answer_0(callid, EINVAL);
159 break;
160 }
161 }
162}
163
[924c75e1]164/** Function for handling connections to device manager.
165 *
166 */
167static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
168{
169 // Select interface
170 switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
171 case DEVMAN_DRIVER:
172 devman_connection_driver(iid, icall);
173 break;
174 /*case DEVMAN_CLIENT:
175 devmap_connection_client(iid, icall);
176 break;
177 case DEVMAN_CONNECT_TO_DEVICE:
178 // Connect client to selected device
179 devmap_forward(iid, icall);
180 break;*/
181 default:
182 /* No such interface */
183 ipc_answer_0(iid, ENOENT);
184 }
185}
186
[e2b9a993]187/** Initialize device manager internal structures.
188 */
189static bool devman_init()
190{
[0c3666d]191 printf(NAME ": devman_init - looking for available drivers. \n");
[08d9c4e6]192
[e2b9a993]193 // initialize list of available drivers
[0c3666d]194 init_driver_list(&drivers_list);
[e2b9a993]195 if (0 == lookup_available_drivers(&drivers_list, DRIVER_DEFAULT_STORE)) {
196 printf(NAME " no drivers found.");
197 return false;
198 }
[08d9c4e6]199 printf(NAME ": devman_init - list of drivers has been initialized. \n");
[e2b9a993]200
201 // create root device node
202 if (!init_device_tree(&device_tree, &drivers_list)) {
203 printf(NAME " failed to initialize device tree.");
204 return false;
205 }
206
207 return true;
208}
209
210int main(int argc, char *argv[])
211{
212 printf(NAME ": HelenOS Device Manager\n");
213
214 if (!devman_init()) {
215 printf(NAME ": Error while initializing service\n");
216 return -1;
217 }
218
219 // Set a handler of incomming connections
220 async_set_client_connection(devman_connection);
221
222 // Register device manager at naming service
223 ipcarg_t phonead;
224 if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAN, 0, 0, &phonead) != 0)
225 return -1;
226
227 printf(NAME ": Accepting connections\n");
228 async_manager();
229
230 // Never reached
231 return 0;
232}
[c16cf62]233
234/** @}
235 */
Note: See TracBrowser for help on using the repository browser.