Subversion Repositories HelenOS

Rev

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