Subversion Repositories HelenOS

Rev

Rev 4163 | Rev 4243 | 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
 
3846 mejdrech 52
#include "device.h"
3666 mejdrech 53
#include "netif.h"
4192 mejdrech 54
#include "netif_interface.h"
3666 mejdrech 55
 
3846 mejdrech 56
#define IPC_GET_DEVICE( call )		( device_id_t ) IPC_GET_ARG1( * call )
3901 mejdrech 57
#define IPC_GET_PACKET( call )		( packet_id_t ) IPC_GET_ARG2( * call )
3846 mejdrech 58
#define IPC_GET_IRQ( call )		( int ) IPC_GET_ARG2( * call )
59
#define IPC_GET_IO( call )		( int ) IPC_GET_ARG3( * call )
60
#define IPC_GET_PHONE( call )		( int ) IPC_GET_ARG5( * call )
61
 
3666 mejdrech 62
extern netif_globals_t netif_globals;
63
 
3846 mejdrech 64
DEVICE_MAP_IMPLEMENT( device_map, device_t )
3666 mejdrech 65
 
3846 mejdrech 66
int	module_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
67
int	netif_start_module( async_client_conn_t client_connection );
68
int	register_message( device_id_t device_id, int phone );
3666 mejdrech 69
 
3846 mejdrech 70
int find_device( device_id_t device_id, device_ref * device ){
4163 mejdrech 71
	if( ! device ) return EBADMEM;
3846 mejdrech 72
	* device = device_map_find( & netif_globals.device_map, device_id );
3666 mejdrech 73
	if( ! * device ) return ENOENT;
3685 mejdrech 74
	if(( ** device ).state == NETIF_NULL ) return EPERM;
3666 mejdrech 75
	return EOK;
76
}
77
 
3846 mejdrech 78
void null_device_stats( device_stats_ref stats ){
4163 mejdrech 79
	bzero( stats, sizeof( device_stats_t ));
3666 mejdrech 80
}
81
 
3846 mejdrech 82
int register_message( device_id_t device_id, int phone ){
3666 mejdrech 83
	ERROR_DECLARE;
84
 
3846 mejdrech 85
	device_ref	device;
86
 
87
	ERROR_PROPAGATE( find_device( device_id, & device ));
4163 mejdrech 88
	if( device->nil_phone > 0 ) return ELIMIT;
3846 mejdrech 89
	device->nil_phone = phone;
4163 mejdrech 90
	printf( "\nNew receiver of the device %d registered:\n\tphone\t= %d", device->device_id, device->nil_phone );
3846 mejdrech 91
	return EOK;
92
}
93
 
94
int netif_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
95
	ERROR_DECLARE;
96
 
4163 mejdrech 97
	size_t				length;
4192 mejdrech 98
	device_stats_t		stats;
4163 mejdrech 99
	packet_t			packet;
4192 mejdrech 100
	measured_string_t	address;
3666 mejdrech 101
 
3846 mejdrech 102
	* answer_count = 0;
103
	switch( IPC_GET_METHOD( * call )){
3666 mejdrech 104
		case IPC_M_PHONE_HUNGUP:
105
			return EOK;
106
		case NET_NETIF_PROBE_AUTO:
4192 mejdrech 107
			rwlock_write_lock( & netif_globals.lock );
108
			ERROR_CODE = probe_auto_message();
109
			rwlock_write_unlock( & netif_globals.lock );
110
			return ERROR_CODE;
3666 mejdrech 111
		case NET_NETIF_PROBE:
4192 mejdrech 112
			rwlock_write_lock( & netif_globals.lock );
113
			ERROR_CODE = probe_message( IPC_GET_DEVICE( call ), IPC_GET_IRQ( call ), IPC_GET_IO( call ));
114
			rwlock_write_unlock( & netif_globals.lock );
115
			return ERROR_CODE;
3846 mejdrech 116
		case IPC_M_CONNECT_TO_ME:
4192 mejdrech 117
			rwlock_write_lock( & netif_globals.lock );
118
			ERROR_CODE = register_message( IPC_GET_DEVICE( call ), IPC_GET_PHONE( call ));
119
			rwlock_write_unlock( & netif_globals.lock );
120
			return ERROR_CODE;
3666 mejdrech 121
		case NET_NETIF_SEND:
4192 mejdrech 122
			rwlock_write_lock( & netif_globals.lock );
123
			if( ! ERROR_OCCURRED( packet_translate( netif_globals.networking_phone, & packet, IPC_GET_PACKET( call )))){
124
				ERROR_CODE = send_message( IPC_GET_DEVICE( call ), packet );
125
			}
126
			rwlock_write_unlock( & netif_globals.lock );
127
			return ERROR_CODE;
3666 mejdrech 128
		case NET_NETIF_START:
4192 mejdrech 129
			rwlock_write_lock( & netif_globals.lock );
130
			ERROR_CODE = start_message( IPC_GET_DEVICE( call ));
131
			rwlock_write_unlock( & netif_globals.lock );
132
			return ERROR_CODE;
3666 mejdrech 133
		case NET_NETIF_STATS:
4192 mejdrech 134
			rwlock_read_lock( & netif_globals.lock );
135
			if( ! ERROR_OCCURRED( ipc_data_read_receive( & callid, & length ))){
136
				if( length < sizeof( device_stats_t )){
137
					ERROR_CODE = EOVERFLOW;
138
				}else{
139
					if( ! ERROR_OCCURRED( get_device_stats( IPC_GET_DEVICE( call ), & stats ))){
140
						ERROR_CODE = ipc_data_read_finalize( callid, & stats, sizeof( device_stats_t ));
141
					}
142
				}
143
			}
144
			rwlock_read_unlock( & netif_globals.lock );
145
			return ERROR_CODE;
3666 mejdrech 146
		case NET_NETIF_STOP:
4192 mejdrech 147
			rwlock_write_lock( & netif_globals.lock );
148
			ERROR_CODE = stop_message( IPC_GET_DEVICE( call ));
149
			rwlock_write_unlock( & netif_globals.lock );
150
			return ERROR_CODE;
4163 mejdrech 151
		case NET_NETIF_GET_ADDR:
4192 mejdrech 152
			rwlock_read_lock( & netif_globals.lock );
153
			if( ! ERROR_OCCURRED( get_addr_message( IPC_GET_DEVICE( call ), & address ))){
154
				ERROR_CODE = measured_strings_reply( & address, 1 );
155
			}
156
			rwlock_read_unlock( & netif_globals.lock );
157
			return ERROR_CODE;
3666 mejdrech 158
	}
4192 mejdrech 159
	return specific_message( callid, call, answer, answer_count );
3666 mejdrech 160
}
161
 
3846 mejdrech 162
int netif_start_module( async_client_conn_t client_connection ){
163
	ERROR_DECLARE;
3666 mejdrech 164
 
3846 mejdrech 165
	async_set_client_connection( client_connection );
166
	netif_globals.networking_phone = connect_to_service( SERVICE_NETWORKING );
167
	device_map_initialize( & netif_globals.device_map );
3912 mejdrech 168
	ERROR_PROPAGATE( pm_init());
4192 mejdrech 169
	rwlock_initialize( & netif_globals.lock );
3914 mejdrech 170
	if( ERROR_OCCURRED( initialize())){
171
		pm_destroy();
172
		return ERROR_CODE;
173
	}
3912 mejdrech 174
 
3846 mejdrech 175
	async_manager();
176
 
3912 mejdrech 177
	pm_destroy();
3846 mejdrech 178
	return EOK;
3666 mejdrech 179
}
180
 
4192 mejdrech 181
void netif_pq_release( packet_id_t packet_id ){
182
	pq_release( netif_globals.networking_phone, packet_id );
183
}
184
 
185
packet_t netif_packet_get_1( size_t content ){
186
	return packet_get_1( netif_globals.networking_phone, content );
187
}
188
 
3666 mejdrech 189
/** @}
190
 */