Rev 4743 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3666 | mejdrech | 1 | /* |
3912 | mejdrech | 2 | * Copyright (c) 2009 Lukas Mejdrech |
3666 | 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 | |||
3912 | mejdrech | 29 | /** @addtogroup netif |
30 | * @{ |
||
3666 | mejdrech | 31 | */ |
32 | |||
33 | /** @file |
||
4350 | mejdrech | 34 | * Network interface module skeleton implementation. |
35 | * @see netif.h |
||
3666 | mejdrech | 36 | */ |
37 | |||
38 | #include <async.h> |
||
4163 | mejdrech | 39 | #include <mem.h> |
4582 | mejdrech | 40 | #include <fibril_sync.h> |
3666 | mejdrech | 41 | #include <stdio.h> |
3846 | mejdrech | 42 | |
3666 | mejdrech | 43 | #include <ipc/ipc.h> |
44 | #include <ipc/services.h> |
||
45 | |||
46 | #include "../err.h" |
||
47 | #include "../messages.h" |
||
48 | #include "../modules.h" |
||
49 | |||
3886 | mejdrech | 50 | #include "../structures/packet/packet.h" |
3901 | mejdrech | 51 | #include "../structures/packet/packet_client.h" |
4163 | mejdrech | 52 | #include "../structures/measured_strings.h" |
3886 | mejdrech | 53 | |
4243 | mejdrech | 54 | #include "../include/device.h" |
4307 | mejdrech | 55 | #include "../include/netif_interface.h" |
56 | #include "../include/nil_interface.h" |
||
4243 | mejdrech | 57 | |
3666 | mejdrech | 58 | #include "netif.h" |
4307 | mejdrech | 59 | #include "netif_messages.h" |
60 | #include "netif_module.h" |
||
3666 | mejdrech | 61 | |
4350 | mejdrech | 62 | /** Network interface module global data. |
63 | */ |
||
3666 | mejdrech | 64 | extern netif_globals_t netif_globals; |
65 | |||
3846 | mejdrech | 66 | DEVICE_MAP_IMPLEMENT( device_map, device_t ) |
3666 | mejdrech | 67 | |
4703 | mejdrech | 68 | /** @name Message processing functions |
69 | */ |
||
70 | /*@{*/ |
||
71 | |||
4350 | mejdrech | 72 | /** Registers the device notification receiver, the network interface layer module. |
4756 | mejdrech | 73 | * @param[in] device_id The device identifier. |
74 | * @param[in] phone The network interface layer module phone. |
||
4350 | mejdrech | 75 | * @returns EOK on success. |
76 | * @returns ENOENT if there is no such device. |
||
77 | * @returns ELIMIT if there is another module registered. |
||
78 | */ |
||
3846 | mejdrech | 79 | int register_message( device_id_t device_id, int phone ); |
3666 | mejdrech | 80 | |
4703 | mejdrech | 81 | /*@}*/ |
82 | |||
4307 | mejdrech | 83 | int netif_probe_req( int netif_phone, device_id_t device_id, int irq, int io ){ |
4243 | mejdrech | 84 | int result; |
85 | |||
4582 | mejdrech | 86 | fibril_rwlock_write_lock( & netif_globals.lock ); |
4243 | mejdrech | 87 | result = netif_probe_message( device_id, irq, io ); |
4582 | mejdrech | 88 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4243 | mejdrech | 89 | return result; |
90 | } |
||
91 | |||
4307 | mejdrech | 92 | int netif_send_msg( int netif_phone, device_id_t device_id, packet_t packet, services_t sender ){ |
4243 | mejdrech | 93 | int result; |
94 | |||
4582 | mejdrech | 95 | fibril_rwlock_write_lock( & netif_globals.lock ); |
4261 | mejdrech | 96 | result = netif_send_message( device_id, packet, sender ); |
4582 | mejdrech | 97 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4243 | mejdrech | 98 | return result; |
99 | } |
||
100 | |||
4307 | mejdrech | 101 | int netif_start_req( int netif_phone, device_id_t device_id ){ |
4261 | mejdrech | 102 | ERROR_DECLARE; |
103 | |||
104 | device_ref device; |
||
4243 | mejdrech | 105 | int result; |
4261 | mejdrech | 106 | int phone; |
4243 | mejdrech | 107 | |
4582 | mejdrech | 108 | fibril_rwlock_write_lock( & netif_globals.lock ); |
4261 | mejdrech | 109 | if( ERROR_OCCURRED( find_device( device_id, & device ))){ |
4582 | mejdrech | 110 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4261 | mejdrech | 111 | return ERROR_CODE; |
112 | } |
||
113 | result = netif_start_message( device ); |
||
114 | if( result > NETIF_NULL ){ |
||
115 | phone = device->nil_phone; |
||
4582 | mejdrech | 116 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4261 | mejdrech | 117 | nil_device_state_msg( phone, device_id, result ); |
118 | return EOK; |
||
119 | }else{ |
||
4582 | mejdrech | 120 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4261 | mejdrech | 121 | } |
4243 | mejdrech | 122 | return result; |
123 | } |
||
124 | |||
4307 | mejdrech | 125 | int netif_stop_req( int netif_phone, device_id_t device_id ){ |
4261 | mejdrech | 126 | ERROR_DECLARE; |
127 | |||
128 | device_ref device; |
||
4243 | mejdrech | 129 | int result; |
4261 | mejdrech | 130 | int phone; |
4243 | mejdrech | 131 | |
4582 | mejdrech | 132 | fibril_rwlock_write_lock( & netif_globals.lock ); |
4261 | mejdrech | 133 | if( ERROR_OCCURRED( find_device( device_id, & device ))){ |
4582 | mejdrech | 134 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4261 | mejdrech | 135 | return ERROR_CODE; |
136 | } |
||
137 | result = netif_stop_message( device ); |
||
138 | if( result > NETIF_NULL ){ |
||
139 | phone = device->nil_phone; |
||
4582 | mejdrech | 140 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4261 | mejdrech | 141 | nil_device_state_msg( phone, device_id, result ); |
142 | return EOK; |
||
143 | }else{ |
||
4582 | mejdrech | 144 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4261 | mejdrech | 145 | } |
4243 | mejdrech | 146 | return result; |
147 | } |
||
148 | |||
4703 | mejdrech | 149 | int netif_stats_req( int netif_phone, device_id_t device_id, device_stats_ref stats ){ |
150 | int res; |
||
151 | |||
152 | fibril_rwlock_read_lock( & netif_globals.lock ); |
||
153 | res = netif_get_device_stats( device_id, stats ); |
||
154 | fibril_rwlock_read_unlock( & netif_globals.lock ); |
||
155 | return res; |
||
156 | } |
||
157 | |||
4743 | mejdrech | 158 | int netif_get_addr_req( int netif_phone, device_id_t device_id, measured_string_ref * address, char ** data ){ |
4243 | mejdrech | 159 | ERROR_DECLARE; |
160 | |||
161 | measured_string_t translation; |
||
162 | |||
4261 | mejdrech | 163 | if( !( address && data )) return EBADMEM; |
4582 | mejdrech | 164 | fibril_rwlock_read_lock( & netif_globals.lock ); |
4243 | mejdrech | 165 | if( ! ERROR_OCCURRED( netif_get_addr_message( device_id, & translation ))){ |
4271 | mejdrech | 166 | * address = measured_string_copy( & translation ); |
167 | ERROR_CODE = ( * address ) ? EOK : ENOMEM; |
||
4243 | mejdrech | 168 | } |
4582 | mejdrech | 169 | fibril_rwlock_read_unlock( & netif_globals.lock ); |
4307 | mejdrech | 170 | * data = ( ** address ).value; |
4243 | mejdrech | 171 | return ERROR_CODE; |
172 | } |
||
173 | |||
4307 | mejdrech | 174 | int netif_bind_service( services_t service, device_id_t device_id, services_t me, async_client_conn_t receiver ){ |
175 | return EOK; |
||
176 | } |
||
177 | |||
3846 | mejdrech | 178 | int find_device( device_id_t device_id, device_ref * device ){ |
4163 | mejdrech | 179 | if( ! device ) return EBADMEM; |
3846 | mejdrech | 180 | * device = device_map_find( & netif_globals.device_map, device_id ); |
3666 | mejdrech | 181 | if( ! * device ) return ENOENT; |
3685 | mejdrech | 182 | if(( ** device ).state == NETIF_NULL ) return EPERM; |
3666 | mejdrech | 183 | return EOK; |
184 | } |
||
185 | |||
3846 | mejdrech | 186 | void null_device_stats( device_stats_ref stats ){ |
4163 | mejdrech | 187 | bzero( stats, sizeof( device_stats_t )); |
3666 | mejdrech | 188 | } |
189 | |||
3846 | mejdrech | 190 | int register_message( device_id_t device_id, int phone ){ |
3666 | mejdrech | 191 | ERROR_DECLARE; |
192 | |||
3846 | mejdrech | 193 | device_ref device; |
194 | |||
195 | ERROR_PROPAGATE( find_device( device_id, & device )); |
||
4163 | mejdrech | 196 | if( device->nil_phone > 0 ) return ELIMIT; |
3846 | mejdrech | 197 | device->nil_phone = phone; |
4307 | mejdrech | 198 | printf( "New receiver of the device %d registered:\n\tphone\t= %d\n", device->device_id, device->nil_phone ); |
3846 | mejdrech | 199 | return EOK; |
200 | } |
||
201 | |||
202 | int netif_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
||
203 | ERROR_DECLARE; |
||
204 | |||
4163 | mejdrech | 205 | size_t length; |
4192 | mejdrech | 206 | device_stats_t stats; |
4163 | mejdrech | 207 | packet_t packet; |
4192 | mejdrech | 208 | measured_string_t address; |
3666 | mejdrech | 209 | |
4307 | mejdrech | 210 | // printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NETIF_FIRST ); |
3846 | mejdrech | 211 | * answer_count = 0; |
212 | switch( IPC_GET_METHOD( * call )){ |
||
3666 | mejdrech | 213 | case IPC_M_PHONE_HUNGUP: |
214 | return EOK; |
||
215 | case NET_NETIF_PROBE: |
||
4307 | mejdrech | 216 | return netif_probe_req( 0, IPC_GET_DEVICE( call ), NETIF_GET_IRQ( call ), NETIF_GET_IO( call )); |
3846 | mejdrech | 217 | case IPC_M_CONNECT_TO_ME: |
4582 | mejdrech | 218 | fibril_rwlock_write_lock( & netif_globals.lock ); |
4261 | mejdrech | 219 | ERROR_CODE = register_message( IPC_GET_DEVICE( call ), IPC_GET_PHONE( call )); |
4582 | mejdrech | 220 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4192 | mejdrech | 221 | return ERROR_CODE; |
3666 | mejdrech | 222 | case NET_NETIF_SEND: |
4307 | mejdrech | 223 | ERROR_PROPAGATE( packet_translate( netif_globals.net_phone, & packet, IPC_GET_PACKET( call ))); |
224 | return netif_send_msg( 0, IPC_GET_DEVICE( call ), packet, IPC_GET_SENDER( call )); |
||
3666 | mejdrech | 225 | case NET_NETIF_START: |
4307 | mejdrech | 226 | return netif_start_req( 0, IPC_GET_DEVICE( call )); |
3666 | mejdrech | 227 | case NET_NETIF_STATS: |
4582 | mejdrech | 228 | fibril_rwlock_read_lock( & netif_globals.lock ); |
4192 | mejdrech | 229 | if( ! ERROR_OCCURRED( ipc_data_read_receive( & callid, & length ))){ |
230 | if( length < sizeof( device_stats_t )){ |
||
231 | ERROR_CODE = EOVERFLOW; |
||
232 | }else{ |
||
4261 | mejdrech | 233 | if( ! ERROR_OCCURRED( netif_get_device_stats( IPC_GET_DEVICE( call ), & stats ))){ |
4192 | mejdrech | 234 | ERROR_CODE = ipc_data_read_finalize( callid, & stats, sizeof( device_stats_t )); |
235 | } |
||
236 | } |
||
237 | } |
||
4582 | mejdrech | 238 | fibril_rwlock_read_unlock( & netif_globals.lock ); |
4192 | mejdrech | 239 | return ERROR_CODE; |
3666 | mejdrech | 240 | case NET_NETIF_STOP: |
4307 | mejdrech | 241 | return netif_stop_req( 0, IPC_GET_DEVICE( call )); |
4163 | mejdrech | 242 | case NET_NETIF_GET_ADDR: |
4582 | mejdrech | 243 | fibril_rwlock_read_lock( & netif_globals.lock ); |
4261 | mejdrech | 244 | if( ! ERROR_OCCURRED( netif_get_addr_message( IPC_GET_DEVICE( call ), & address ))){ |
4192 | mejdrech | 245 | ERROR_CODE = measured_strings_reply( & address, 1 ); |
246 | } |
||
4582 | mejdrech | 247 | fibril_rwlock_read_unlock( & netif_globals.lock ); |
4192 | mejdrech | 248 | return ERROR_CODE; |
3666 | mejdrech | 249 | } |
4243 | mejdrech | 250 | return netif_specific_message( callid, call, answer, answer_count ); |
3666 | mejdrech | 251 | } |
252 | |||
4307 | mejdrech | 253 | int netif_init_module( async_client_conn_t client_connection ){ |
3846 | mejdrech | 254 | ERROR_DECLARE; |
3666 | mejdrech | 255 | |
3846 | mejdrech | 256 | async_set_client_connection( client_connection ); |
4307 | mejdrech | 257 | netif_globals.net_phone = connect_to_service( SERVICE_NETWORKING ); |
3846 | mejdrech | 258 | device_map_initialize( & netif_globals.device_map ); |
3912 | mejdrech | 259 | ERROR_PROPAGATE( pm_init()); |
4582 | mejdrech | 260 | fibril_rwlock_initialize( & netif_globals.lock ); |
4243 | mejdrech | 261 | if( ERROR_OCCURRED( netif_initialize())){ |
3914 | mejdrech | 262 | pm_destroy(); |
263 | return ERROR_CODE; |
||
264 | } |
||
4307 | mejdrech | 265 | return EOK; |
266 | } |
||
3912 | mejdrech | 267 | |
4307 | mejdrech | 268 | int netif_run_module( void ){ |
3846 | mejdrech | 269 | async_manager(); |
270 | |||
3912 | mejdrech | 271 | pm_destroy(); |
3846 | mejdrech | 272 | return EOK; |
3666 | mejdrech | 273 | } |
274 | |||
4192 | mejdrech | 275 | void netif_pq_release( packet_id_t packet_id ){ |
4307 | mejdrech | 276 | pq_release( netif_globals.net_phone, packet_id ); |
4192 | mejdrech | 277 | } |
278 | |||
279 | packet_t netif_packet_get_1( size_t content ){ |
||
4307 | mejdrech | 280 | return packet_get_1( netif_globals.net_phone, content ); |
4192 | mejdrech | 281 | } |
282 | |||
3666 | mejdrech | 283 | /** @} |
284 | */ |