Subversion Repositories HelenOS

Rev

Rev 4192 | Rev 4261 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 Lukas 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.  
  29. /** @addtogroup netif
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  */
  35.  
  36. #include <async.h>
  37. #include <mem.h>
  38. #include <rwlock.h>
  39. #include <stdio.h>
  40.  
  41. #include <ipc/ipc.h>
  42. #include <ipc/services.h>
  43.  
  44. #include "../err.h"
  45. #include "../messages.h"
  46. #include "../modules.h"
  47.  
  48. #include "../structures/packet/packet.h"
  49. #include "../structures/packet/packet_client.h"
  50. #include "../structures/measured_strings.h"
  51.  
  52. #include "../include/device.h"
  53. #include "../include/netif_messages.h"
  54.  
  55. #include "netif.h"
  56. #include "netif_interface.h"
  57. #include "netif_wrappers.h"
  58.  
  59. extern netif_globals_t netif_globals;
  60.  
  61. DEVICE_MAP_IMPLEMENT( device_map, device_t )
  62.  
  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 );
  66.  
  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.  
  118. int find_device( device_id_t device_id, device_ref * device ){
  119.     if( ! device ) return EBADMEM;
  120.     * device = device_map_find( & netif_globals.device_map, device_id );
  121.     if( ! * device ) return ENOENT;
  122.     if(( ** device ).state == NETIF_NULL ) return EPERM;
  123.     return EOK;
  124. }
  125.  
  126. void null_device_stats( device_stats_ref stats ){
  127.     bzero( stats, sizeof( device_stats_t ));
  128. }
  129.  
  130. int register_message( device_id_t device_id, int phone ){
  131.     ERROR_DECLARE;
  132.  
  133.     device_ref  device;
  134.  
  135.     ERROR_PROPAGATE( find_device( device_id, & device ));
  136.     if( device->nil_phone > 0 ) return ELIMIT;
  137.     device->nil_phone = phone;
  138.     printf( "\nNew receiver of the device %d registered:\n\tphone\t= %d", device->device_id, device->nil_phone );
  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.  
  145.     size_t              length;
  146.     device_stats_t      stats;
  147.     packet_t            packet;
  148.     measured_string_t   address;
  149.  
  150.     * answer_count = 0;
  151.     switch( IPC_GET_METHOD( * call )){
  152.         case IPC_M_PHONE_HUNGUP:
  153.             return EOK;
  154.         case NET_NETIF_PROBE_AUTO:
  155.             rwlock_write_lock( & netif_globals.lock );
  156.             ERROR_CODE = netif_probe_auto_message();
  157.             rwlock_write_unlock( & netif_globals.lock );
  158.             return ERROR_CODE;
  159.         case NET_NETIF_PROBE:
  160.             return netif_probe_wrapper( NETIF_GET_DEVICE( call ), NETIF_GET_IRQ( call ), NETIF_GET_IO( call ));
  161.         case IPC_M_CONNECT_TO_ME:
  162.             rwlock_write_lock( & netif_globals.lock );
  163.             ERROR_CODE = register_message( NETIF_GET_DEVICE( call ), IPC_GET_PHONE( call ));
  164.             rwlock_write_unlock( & netif_globals.lock );
  165.             return ERROR_CODE;
  166.         case NET_NETIF_SEND:
  167.             ERROR_PROPAGATE( packet_translate( netif_globals.networking_phone, & packet, NETIF_GET_PACKET( call )));
  168.             return netif_send_wrapper( NETIF_GET_DEVICE( call ), packet );
  169.         case NET_NETIF_START:
  170.             return netif_start_wrapper( NETIF_GET_DEVICE( call ));
  171.         case NET_NETIF_STATS:
  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{
  177.                     if( ! ERROR_OCCURRED( netif_get_device_stats( NETIF_GET_DEVICE( call ), & stats ))){
  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;
  184.         case NET_NETIF_STOP:
  185.             return netif_stop_wrapper( NETIF_GET_DEVICE( call ));
  186.         case NET_NETIF_GET_ADDR:
  187.             rwlock_read_lock( & netif_globals.lock );
  188.             if( ! ERROR_OCCURRED( netif_get_addr_message( NETIF_GET_DEVICE( call ), & address ))){
  189.                 ERROR_CODE = measured_strings_reply( & address, 1 );
  190.             }
  191.             rwlock_read_unlock( & netif_globals.lock );
  192.             return ERROR_CODE;
  193.     }
  194.     return netif_specific_message( callid, call, answer, answer_count );
  195. }
  196.  
  197. int netif_start_module( async_client_conn_t client_connection ){
  198.     ERROR_DECLARE;
  199.  
  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 );
  203.     ERROR_PROPAGATE( pm_init());
  204.     rwlock_initialize( & netif_globals.lock );
  205.     if( ERROR_OCCURRED( netif_initialize())){
  206.         pm_destroy();
  207.         return ERROR_CODE;
  208.     }
  209.  
  210.     async_manager();
  211.  
  212.     pm_destroy();
  213.     return EOK;
  214. }
  215.  
  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.  
  224. /** @}
  225.  */
  226.