/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup lo
* @{
*/
/** @file
*/
#include <async.h>
#include <errno.h>
#include <stdio.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"
#include "../../structures/measured_strings.h"
#include "../../structures/packet/packet_client.h"
#include "../netif.h"
#define DEFAULT_MTU 1500
#define NAME "lo - loopback interface"
netif_globals_t netif_globals;
int change_state_message( device_id_t device_id, device_state_t state );
int create( device_id_t device_id, device_ref * device );
int initialize( void );
void netif_print_name( void );
int probe_auto_message( void );
int probe_message( device_id_t device_id, int irq, int io );
int send_message( device_id_t device_id, packet_t packet );
int start_message( device_id_t device_id );
int stop_message( device_id_t device_id );
int change_state_message( device_id_t device_id, device_state_t state ){
ERROR_DECLARE;
device_ref device;
ERROR_PROPAGATE( find_device( device_id, & device ));
if( device->state != state ){
device->state = state;
nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
}
return EOK;
}
int create( device_id_t device_id, device_ref * device ){
ERROR_DECLARE;
if( device_map_count( & netif_globals.device_map ) > 0 ){
return EXDEV;
}else{
* device
= ( device_ref
) malloc( sizeof( device_t
));
if( !( * device )) return ENOMEM;
( ** device ).device_id = device_id;
( ** device ).nil_phone = -1;
( ** device ).specific = NULL;
null_device_stats( &(( ** device ).stats ));
( ** device ).state = NETIF_STOPPED;
( ** device ).flags = NULL;
( ** device ).mtu = DEFAULT_MTU;
if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device ))){
* device = NULL;
return ERROR_CODE;
}
}
return EOK;
}
int initialize( void ){
ipcarg_t phonehash;
return REGISTER_ME( SERVICE_LO, & phonehash );
}
void netif_print_name( void ){
}
int probe_auto_message( void ){
/* ERROR_DECLARE;
device_ref device;
ERROR_PROPAGATE( create( arg1, & device ));
ipc_call_sync_3_3( netif_globals.networking_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
*/ return ENOTSUP;
}
int probe_message( device_id_t device_id, int irq, int io ){
ERROR_DECLARE;
device_ref device;
aid_t message;
ipc_call_t answer;
measured_string_t configuration[ 1 ] = {{ "MTU", 3 }};
int count = 1;
measured_string_ref settings;
char * data;
// create a new device
ERROR_PROPAGATE( create( device_id, & device ));
// get configuration
message = async_send_2( netif_globals.networking_phone, NET_NET_GET_DEVICE_CONF, device->device_id, count, & answer );
// send names and get settings
if( ERROR_OCCURRED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
|| ERROR_OCCURRED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
async_wait_for( message, NULL );
return ERROR_CODE;
}
// MTU is the first one
if( settings && ( settings[ 0 ].value )){
device
->mtu
= strtoul( settings
[ 0 ].
value, NULL
, 0 );
}else{
device->mtu = DEFAULT_MTU;
}
// print the settings
printf("\n -MTU =\t%d", device
->mtu
);
// end request
async_wait_for( message, NULL );
return EOK;
}
int send_message( device_id_t device_id, packet_t packet ){
ERROR_DECLARE;
device_ref device;
size_t length;
packet_t next;
ERROR_PROPAGATE( find_device( device_id, & device ));
if( device->state != NETIF_ACTIVE ) return EPERM;
next = packet;
do{
++ device->stats.tx_packets;
++ device->stats.rx_packets;
length = packet_get_data_length( next );
device->stats.tx_bytes += length;
device->stats.rx_bytes += length;
next = pq_next( next );
}while( next );
nil_message( device, NET_NIL_RECEIVED, packet_get_id( packet ), PACKET_SELF );
return EOK;
}
int start_message( device_id_t device_id ){
return change_state_message( device_id, NETIF_ACTIVE );
}
int stop_message( device_id_t device_id ){
return change_state_message( device_id, NETIF_STOPPED );
}
/** @}
*/