Rev 4192 | Rev 4261 | Go to most recent revision | 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 |
||
| 34 | */ |
||
| 35 | |||
| 36 | #include <async.h> |
||
| 4163 | mejdrech | 37 | #include <mem.h> |
| 4192 | mejdrech | 38 | #include <rwlock.h> |
| 3666 | mejdrech | 39 | #include <stdio.h> |
| 3846 | mejdrech | 40 | |
| 3666 | mejdrech | 41 | #include <ipc/ipc.h> |
| 42 | #include <ipc/services.h> |
||
| 43 | |||
| 44 | #include "../err.h" |
||
| 45 | #include "../messages.h" |
||
| 46 | #include "../modules.h" |
||
| 47 | |||
| 3886 | mejdrech | 48 | #include "../structures/packet/packet.h" |
| 3901 | mejdrech | 49 | #include "../structures/packet/packet_client.h" |
| 4163 | mejdrech | 50 | #include "../structures/measured_strings.h" |
| 3886 | mejdrech | 51 | |
| 4243 | mejdrech | 52 | #include "../include/device.h" |
| 53 | #include "../include/netif_messages.h" |
||
| 54 | |||
| 3666 | mejdrech | 55 | #include "netif.h" |
| 4192 | mejdrech | 56 | #include "netif_interface.h" |
| 4243 | mejdrech | 57 | #include "netif_wrappers.h" |
| 3666 | mejdrech | 58 | |
| 59 | extern netif_globals_t netif_globals; |
||
| 60 | |||
| 3846 | mejdrech | 61 | DEVICE_MAP_IMPLEMENT( device_map, device_t ) |
| 3666 | mejdrech | 62 | |
| 3846 | mejdrech | 63 | int module_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ); |
| 64 | int netif_start_module( async_client_conn_t client_connection ); |
||
| 65 | int register_message( device_id_t device_id, int phone ); |
||
| 3666 | mejdrech | 66 | |
| 4243 | mejdrech | 67 | int netif_probe_wrapper( device_id_t device_id, int irq, int io ){ |
| 68 | int result; |
||
| 69 | |||
| 70 | rwlock_write_lock( & netif_globals.lock ); |
||
| 71 | result = netif_probe_message( device_id, irq, io ); |
||
| 72 | rwlock_write_unlock( & netif_globals.lock ); |
||
| 73 | return result; |
||
| 74 | } |
||
| 75 | |||
| 76 | int netif_send_wrapper( device_id_t device_id, packet_t packet ){ |
||
| 77 | int result; |
||
| 78 | |||
| 79 | rwlock_write_lock( & netif_globals.lock ); |
||
| 80 | result = netif_send_message( device_id, packet ); |
||
| 81 | rwlock_write_unlock( & netif_globals.lock ); |
||
| 82 | return result; |
||
| 83 | } |
||
| 84 | |||
| 85 | int netif_start_wrapper( device_id_t device_id ){ |
||
| 86 | int result; |
||
| 87 | |||
| 88 | rwlock_write_lock( & netif_globals.lock ); |
||
| 89 | result = netif_start_message( device_id ); |
||
| 90 | rwlock_write_unlock( & netif_globals.lock ); |
||
| 91 | return result; |
||
| 92 | } |
||
| 93 | |||
| 94 | int netif_stop_wrapper( device_id_t device_id ){ |
||
| 95 | int result; |
||
| 96 | |||
| 97 | rwlock_write_lock( & netif_globals.lock ); |
||
| 98 | result = netif_stop_message( device_id ); |
||
| 99 | rwlock_write_unlock( & netif_globals.lock ); |
||
| 100 | return result; |
||
| 101 | } |
||
| 102 | |||
| 103 | int netif_get_addr_wrapper( device_id_t device_id, measured_string_ref * address ){ |
||
| 104 | ERROR_DECLARE; |
||
| 105 | |||
| 106 | measured_string_t translation; |
||
| 107 | |||
| 108 | if( ! address ) return EBADMEM; |
||
| 109 | rwlock_read_lock( & netif_globals.lock ); |
||
| 110 | if( ! ERROR_OCCURRED( netif_get_addr_message( device_id, & translation ))){ |
||
| 111 | * address = measured_string_create_bulk( translation.value, translation.length ); |
||
| 112 | ERROR_CODE = ( * address ) ? EOK : ENOMEM; |
||
| 113 | } |
||
| 114 | rwlock_read_unlock( & netif_globals.lock ); |
||
| 115 | return ERROR_CODE; |
||
| 116 | } |
||
| 117 | |||
| 3846 | mejdrech | 118 | int find_device( device_id_t device_id, device_ref * device ){ |
| 4163 | mejdrech | 119 | if( ! device ) return EBADMEM; |
| 3846 | mejdrech | 120 | * device = device_map_find( & netif_globals.device_map, device_id ); |
| 3666 | mejdrech | 121 | if( ! * device ) return ENOENT; |
| 3685 | mejdrech | 122 | if(( ** device ).state == NETIF_NULL ) return EPERM; |
| 3666 | mejdrech | 123 | return EOK; |
| 124 | } |
||
| 125 | |||
| 3846 | mejdrech | 126 | void null_device_stats( device_stats_ref stats ){ |
| 4163 | mejdrech | 127 | bzero( stats, sizeof( device_stats_t )); |
| 3666 | mejdrech | 128 | } |
| 129 | |||
| 3846 | mejdrech | 130 | int register_message( device_id_t device_id, int phone ){ |
| 3666 | mejdrech | 131 | ERROR_DECLARE; |
| 132 | |||
| 3846 | mejdrech | 133 | device_ref device; |
| 134 | |||
| 135 | ERROR_PROPAGATE( find_device( device_id, & device )); |
||
| 4163 | mejdrech | 136 | if( device->nil_phone > 0 ) return ELIMIT; |
| 3846 | mejdrech | 137 | device->nil_phone = phone; |
| 4163 | mejdrech | 138 | printf( "\nNew receiver of the device %d registered:\n\tphone\t= %d", device->device_id, device->nil_phone ); |
| 3846 | mejdrech | 139 | return EOK; |
| 140 | } |
||
| 141 | |||
| 142 | int netif_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
||
| 143 | ERROR_DECLARE; |
||
| 144 | |||
| 4163 | mejdrech | 145 | size_t length; |
| 4192 | mejdrech | 146 | device_stats_t stats; |
| 4163 | mejdrech | 147 | packet_t packet; |
| 4192 | mejdrech | 148 | measured_string_t address; |
| 3666 | mejdrech | 149 | |
| 3846 | mejdrech | 150 | * answer_count = 0; |
| 151 | switch( IPC_GET_METHOD( * call )){ |
||
| 3666 | mejdrech | 152 | case IPC_M_PHONE_HUNGUP: |
| 153 | return EOK; |
||
| 154 | case NET_NETIF_PROBE_AUTO: |
||
| 4192 | mejdrech | 155 | rwlock_write_lock( & netif_globals.lock ); |
| 4243 | mejdrech | 156 | ERROR_CODE = netif_probe_auto_message(); |
| 4192 | mejdrech | 157 | rwlock_write_unlock( & netif_globals.lock ); |
| 158 | return ERROR_CODE; |
||
| 3666 | mejdrech | 159 | case NET_NETIF_PROBE: |
| 4243 | mejdrech | 160 | return netif_probe_wrapper( NETIF_GET_DEVICE( call ), NETIF_GET_IRQ( call ), NETIF_GET_IO( call )); |
| 3846 | mejdrech | 161 | case IPC_M_CONNECT_TO_ME: |
| 4192 | mejdrech | 162 | rwlock_write_lock( & netif_globals.lock ); |
| 4243 | mejdrech | 163 | ERROR_CODE = register_message( NETIF_GET_DEVICE( call ), IPC_GET_PHONE( call )); |
| 4192 | mejdrech | 164 | rwlock_write_unlock( & netif_globals.lock ); |
| 165 | return ERROR_CODE; |
||
| 3666 | mejdrech | 166 | case NET_NETIF_SEND: |
| 4243 | mejdrech | 167 | ERROR_PROPAGATE( packet_translate( netif_globals.networking_phone, & packet, NETIF_GET_PACKET( call ))); |
| 168 | return netif_send_wrapper( NETIF_GET_DEVICE( call ), packet ); |
||
| 3666 | mejdrech | 169 | case NET_NETIF_START: |
| 4243 | mejdrech | 170 | return netif_start_wrapper( NETIF_GET_DEVICE( call )); |
| 3666 | mejdrech | 171 | case NET_NETIF_STATS: |
| 4192 | mejdrech | 172 | rwlock_read_lock( & netif_globals.lock ); |
| 173 | if( ! ERROR_OCCURRED( ipc_data_read_receive( & callid, & length ))){ |
||
| 174 | if( length < sizeof( device_stats_t )){ |
||
| 175 | ERROR_CODE = EOVERFLOW; |
||
| 176 | }else{ |
||
| 4243 | mejdrech | 177 | if( ! ERROR_OCCURRED( netif_get_device_stats( NETIF_GET_DEVICE( call ), & stats ))){ |
| 4192 | mejdrech | 178 | ERROR_CODE = ipc_data_read_finalize( callid, & stats, sizeof( device_stats_t )); |
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | rwlock_read_unlock( & netif_globals.lock ); |
||
| 183 | return ERROR_CODE; |
||
| 3666 | mejdrech | 184 | case NET_NETIF_STOP: |
| 4243 | mejdrech | 185 | return netif_stop_wrapper( NETIF_GET_DEVICE( call )); |
| 4163 | mejdrech | 186 | case NET_NETIF_GET_ADDR: |
| 4192 | mejdrech | 187 | rwlock_read_lock( & netif_globals.lock ); |
| 4243 | mejdrech | 188 | if( ! ERROR_OCCURRED( netif_get_addr_message( NETIF_GET_DEVICE( call ), & address ))){ |
| 4192 | mejdrech | 189 | ERROR_CODE = measured_strings_reply( & address, 1 ); |
| 190 | } |
||
| 191 | rwlock_read_unlock( & netif_globals.lock ); |
||
| 192 | return ERROR_CODE; |
||
| 3666 | mejdrech | 193 | } |
| 4243 | mejdrech | 194 | return netif_specific_message( callid, call, answer, answer_count ); |
| 3666 | mejdrech | 195 | } |
| 196 | |||
| 3846 | mejdrech | 197 | int netif_start_module( async_client_conn_t client_connection ){ |
| 198 | ERROR_DECLARE; |
||
| 3666 | mejdrech | 199 | |
| 3846 | mejdrech | 200 | async_set_client_connection( client_connection ); |
| 201 | netif_globals.networking_phone = connect_to_service( SERVICE_NETWORKING ); |
||
| 202 | device_map_initialize( & netif_globals.device_map ); |
||
| 3912 | mejdrech | 203 | ERROR_PROPAGATE( pm_init()); |
| 4192 | mejdrech | 204 | rwlock_initialize( & netif_globals.lock ); |
| 4243 | mejdrech | 205 | if( ERROR_OCCURRED( netif_initialize())){ |
| 3914 | mejdrech | 206 | pm_destroy(); |
| 207 | return ERROR_CODE; |
||
| 208 | } |
||
| 3912 | mejdrech | 209 | |
| 3846 | mejdrech | 210 | async_manager(); |
| 211 | |||
| 3912 | mejdrech | 212 | pm_destroy(); |
| 3846 | mejdrech | 213 | return EOK; |
| 3666 | mejdrech | 214 | } |
| 215 | |||
| 4192 | mejdrech | 216 | void netif_pq_release( packet_id_t packet_id ){ |
| 217 | pq_release( netif_globals.networking_phone, packet_id ); |
||
| 218 | } |
||
| 219 | |||
| 220 | packet_t netif_packet_get_1( size_t content ){ |
||
| 221 | return packet_get_1( netif_globals.networking_phone, content ); |
||
| 222 | } |
||
| 223 | |||
| 3666 | mejdrech | 224 | /** @} |
| 225 | */ |