Subversion Repositories HelenOS

Rev

Rev 4261 | Rev 4307 | 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"
4261 mejdrech 54
#include "../include/nil_messages.h"
4243 mejdrech 55
 
3666 mejdrech 56
#include "netif.h"
4192 mejdrech 57
#include "netif_interface.h"
4243 mejdrech 58
#include "netif_wrappers.h"
3666 mejdrech 59
 
4261 mejdrech 60
#if NIL_BUNDLE
61
 
62
    #include "../nil/nil_module.h"
63
 
64
#endif
65
 
3666 mejdrech 66
extern netif_globals_t netif_globals;
67
 
3846 mejdrech 68
DEVICE_MAP_IMPLEMENT( device_map, device_t )
3666 mejdrech 69
 
3846 mejdrech 70
int module_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
71
int netif_start_module( async_client_conn_t client_connection );
72
int register_message( device_id_t device_id, int phone );
3666 mejdrech 73
 
4243 mejdrech 74
int netif_probe_wrapper( device_id_t device_id, int irq, int io ){
75
    int result;
76
 
77
    rwlock_write_lock( & netif_globals.lock );
78
    result = netif_probe_message( device_id, irq, io );
79
    rwlock_write_unlock( & netif_globals.lock );
80
    return result;
81
}
82
 
4261 mejdrech 83
int netif_send_wrapper( device_id_t device_id, packet_t packet, services_t sender ){
4243 mejdrech 84
    int result;
85
 
86
    rwlock_write_lock( & netif_globals.lock );
4261 mejdrech 87
    result = netif_send_message( device_id, packet, sender );
4243 mejdrech 88
    rwlock_write_unlock( & netif_globals.lock );
89
    return result;
90
}
91
 
92
int netif_start_wrapper( device_id_t device_id ){
4261 mejdrech 93
    ERROR_DECLARE;
94
 
95
    device_ref  device;
4243 mejdrech 96
    int result;
4261 mejdrech 97
    int phone;
4243 mejdrech 98
 
99
    rwlock_write_lock( & netif_globals.lock );
4261 mejdrech 100
    if( ERROR_OCCURRED( find_device( device_id, & device ))){
101
        rwlock_write_unlock( & netif_globals.lock );
102
        return ERROR_CODE;
103
    }
104
    result = netif_start_message( device );
105
    if( result > NETIF_NULL ){
106
        phone = device->nil_phone;
107
        rwlock_write_unlock( & netif_globals.lock );
108
        nil_device_state_msg( phone, device_id, result );
109
        return EOK;
110
    }else{
111
        rwlock_write_unlock( & netif_globals.lock );
112
    }
4243 mejdrech 113
    return result;
114
}
115
 
116
int netif_stop_wrapper( device_id_t device_id ){
4261 mejdrech 117
    ERROR_DECLARE;
118
 
119
    device_ref  device;
4243 mejdrech 120
    int result;
4261 mejdrech 121
    int phone;
4243 mejdrech 122
 
123
    rwlock_write_lock( & netif_globals.lock );
4261 mejdrech 124
    if( ERROR_OCCURRED( find_device( device_id, & device ))){
125
        rwlock_write_unlock( & netif_globals.lock );
126
        return ERROR_CODE;
127
    }
128
    result = netif_stop_message( device );
129
    if( result > NETIF_NULL ){
130
        phone = device->nil_phone;
131
        rwlock_write_unlock( & netif_globals.lock );
132
        nil_device_state_msg( phone, device_id, result );
133
        return EOK;
134
    }else{
135
        rwlock_write_unlock( & netif_globals.lock );
136
    }
4243 mejdrech 137
    return result;
138
}
139
 
4261 mejdrech 140
int netif_get_addr_wrapper( device_id_t device_id, measured_string_ref * address, char ** data ){
4243 mejdrech 141
    ERROR_DECLARE;
142
 
143
    measured_string_t   translation;
144
 
4261 mejdrech 145
    if( !( address && data )) return EBADMEM;
4243 mejdrech 146
    rwlock_read_lock( & netif_globals.lock );
147
    if( ! ERROR_OCCURRED( netif_get_addr_message( device_id, & translation ))){
4271 mejdrech 148
        * address = measured_string_copy( & translation );
149
        ERROR_CODE = ( * address ) ? EOK : ENOMEM;
4243 mejdrech 150
    }
151
    rwlock_read_unlock( & netif_globals.lock );
152
    return ERROR_CODE;
153
}
154
 
3846 mejdrech 155
int find_device( device_id_t device_id, device_ref * device ){
4163 mejdrech 156
    if( ! device ) return EBADMEM;
3846 mejdrech 157
    * device = device_map_find( & netif_globals.device_map, device_id );
3666 mejdrech 158
    if( ! * device ) return ENOENT;
3685 mejdrech 159
    if(( ** device ).state == NETIF_NULL ) return EPERM;
3666 mejdrech 160
    return EOK;
161
}
162
 
3846 mejdrech 163
void null_device_stats( device_stats_ref stats ){
4163 mejdrech 164
    bzero( stats, sizeof( device_stats_t ));
3666 mejdrech 165
}
166
 
3846 mejdrech 167
int register_message( device_id_t device_id, int phone ){
3666 mejdrech 168
    ERROR_DECLARE;
169
 
3846 mejdrech 170
    device_ref  device;
171
 
172
    ERROR_PROPAGATE( find_device( device_id, & device ));
4163 mejdrech 173
    if( device->nil_phone > 0 ) return ELIMIT;
3846 mejdrech 174
    device->nil_phone = phone;
4163 mejdrech 175
    printf( "\nNew receiver of the device %d registered:\n\tphone\t= %d", device->device_id, device->nil_phone );
3846 mejdrech 176
    return EOK;
177
}
178
 
179
int netif_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
180
    ERROR_DECLARE;
181
 
4163 mejdrech 182
    size_t              length;
4192 mejdrech 183
    device_stats_t      stats;
4163 mejdrech 184
    packet_t            packet;
4192 mejdrech 185
    measured_string_t   address;
3666 mejdrech 186
 
4261 mejdrech 187
//  printf( "\nmessage %d - %d", IPC_GET_METHOD( * call ), NET_NETIF_FIRST );
188
#if NIL_BUNDLE
189
    if( IS_NET_NIL_MESSAGE( call )){
190
        return nil_message( callid, call, answer, answer_count );
191
    }
192
#endif
3846 mejdrech 193
    * answer_count = 0;
194
    switch( IPC_GET_METHOD( * call )){
3666 mejdrech 195
        case IPC_M_PHONE_HUNGUP:
196
            return EOK;
197
        case NET_NETIF_PROBE_AUTO:
4192 mejdrech 198
            rwlock_write_lock( & netif_globals.lock );
4243 mejdrech 199
            ERROR_CODE = netif_probe_auto_message();
4192 mejdrech 200
            rwlock_write_unlock( & netif_globals.lock );
201
            return ERROR_CODE;
3666 mejdrech 202
        case NET_NETIF_PROBE:
4261 mejdrech 203
            return netif_probe_wrapper( IPC_GET_DEVICE( call ), NETIF_GET_IRQ( call ), NETIF_GET_IO( call ));
3846 mejdrech 204
        case IPC_M_CONNECT_TO_ME:
4261 mejdrech 205
#if NIL_BUNDLE
206
            return nil_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call ));
207
#else
4192 mejdrech 208
            rwlock_write_lock( & netif_globals.lock );
4261 mejdrech 209
            ERROR_CODE = register_message( IPC_GET_DEVICE( call ), IPC_GET_PHONE( call ));
4192 mejdrech 210
            rwlock_write_unlock( & netif_globals.lock );
211
            return ERROR_CODE;
4261 mejdrech 212
#endif
3666 mejdrech 213
        case NET_NETIF_SEND:
4261 mejdrech 214
        case NET_NIL_SEND:
215
            ERROR_PROPAGATE( packet_translate( netif_globals.networking_phone, & packet, IPC_GET_PACKET( call )));
216
            return netif_send_wrapper( IPC_GET_DEVICE( call ), packet, IPC_GET_SENDER( call ));
3666 mejdrech 217
        case NET_NETIF_START:
4261 mejdrech 218
            return netif_start_wrapper( IPC_GET_DEVICE( call ));
3666 mejdrech 219
        case NET_NETIF_STATS:
4192 mejdrech 220
            rwlock_read_lock( & netif_globals.lock );
221
            if( ! ERROR_OCCURRED( ipc_data_read_receive( & callid, & length ))){
222
                if( length < sizeof( device_stats_t )){
223
                    ERROR_CODE = EOVERFLOW;
224
                }else{
4261 mejdrech 225
                    if( ! ERROR_OCCURRED( netif_get_device_stats( IPC_GET_DEVICE( call ), & stats ))){
4192 mejdrech 226
                        ERROR_CODE = ipc_data_read_finalize( callid, & stats, sizeof( device_stats_t ));
227
                    }
228
                }
229
            }
230
            rwlock_read_unlock( & netif_globals.lock );
231
            return ERROR_CODE;
3666 mejdrech 232
        case NET_NETIF_STOP:
4261 mejdrech 233
            return netif_stop_wrapper( IPC_GET_DEVICE( call ));
4163 mejdrech 234
        case NET_NETIF_GET_ADDR:
4261 mejdrech 235
        case NET_NIL_ADDR:
4192 mejdrech 236
            rwlock_read_lock( & netif_globals.lock );
4261 mejdrech 237
            if( ! ERROR_OCCURRED( netif_get_addr_message( IPC_GET_DEVICE( call ), & address ))){
4192 mejdrech 238
                ERROR_CODE = measured_strings_reply( & address, 1 );
239
            }
240
            rwlock_read_unlock( & netif_globals.lock );
241
            return ERROR_CODE;
3666 mejdrech 242
    }
4243 mejdrech 243
    return netif_specific_message( callid, call, answer, answer_count );
3666 mejdrech 244
}
245
 
3846 mejdrech 246
int netif_start_module( async_client_conn_t client_connection ){
247
    ERROR_DECLARE;
3666 mejdrech 248
 
3846 mejdrech 249
    async_set_client_connection( client_connection );
250
    netif_globals.networking_phone = connect_to_service( SERVICE_NETWORKING );
251
    device_map_initialize( & netif_globals.device_map );
3912 mejdrech 252
    ERROR_PROPAGATE( pm_init());
4192 mejdrech 253
    rwlock_initialize( & netif_globals.lock );
4243 mejdrech 254
    if( ERROR_OCCURRED( netif_initialize())){
3914 mejdrech 255
        pm_destroy();
256
        return ERROR_CODE;
257
    }
4261 mejdrech 258
#if NIL_BUNDLE
259
    if( ERROR_OCCURRED( nil_initialize( netif_globals.networking_phone ))){
260
        pm_destroy();
261
        return ERROR_CODE;
262
    }
263
#endif
3912 mejdrech 264
 
3846 mejdrech 265
    async_manager();
266
 
3912 mejdrech 267
    pm_destroy();
3846 mejdrech 268
    return EOK;
3666 mejdrech 269
}
270
 
4192 mejdrech 271
void netif_pq_release( packet_id_t packet_id ){
272
    pq_release( netif_globals.networking_phone, packet_id );
273
}
274
 
275
packet_t netif_packet_get_1( size_t content ){
276
    return packet_get_1( netif_globals.networking_phone, content );
277
}
278
 
3666 mejdrech 279
/** @}
280
 */