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