[21580dd] | 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 | * Network interface module skeleton implementation.
|
---|
| 35 | * @see netif.h
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <async.h>
|
---|
| 39 | #include <mem.h>
|
---|
| 40 | #include <fibril_synch.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 |
|
---|
| 43 | #include <ipc/ipc.h>
|
---|
| 44 | #include <ipc/services.h>
|
---|
| 45 |
|
---|
| 46 | #include "../err.h"
|
---|
| 47 | #include "../messages.h"
|
---|
| 48 | #include "../modules.h"
|
---|
| 49 |
|
---|
| 50 | #include "../structures/packet/packet.h"
|
---|
| 51 | #include "../structures/packet/packet_client.h"
|
---|
| 52 | #include "../structures/measured_strings.h"
|
---|
| 53 |
|
---|
| 54 | #include "../include/device.h"
|
---|
| 55 | #include "../include/netif_interface.h"
|
---|
| 56 | #include "../include/nil_interface.h"
|
---|
| 57 |
|
---|
| 58 | #include "netif.h"
|
---|
| 59 | #include "netif_messages.h"
|
---|
| 60 | #include "netif_module.h"
|
---|
| 61 |
|
---|
| 62 | /** Network interface module global data.
|
---|
| 63 | */
|
---|
| 64 | extern netif_globals_t netif_globals;
|
---|
| 65 |
|
---|
[aadf01e] | 66 | DEVICE_MAP_IMPLEMENT(device_map, device_t)
|
---|
[21580dd] | 67 |
|
---|
| 68 | /** @name Message processing functions
|
---|
| 69 | */
|
---|
| 70 | /*@{*/
|
---|
| 71 |
|
---|
| 72 | /** Registers the device notification receiver, the network interface layer module.
|
---|
| 73 | * @param[in] device_id The device identifier.
|
---|
| 74 | * @param[in] phone The network interface layer module phone.
|
---|
| 75 | * @returns EOK on success.
|
---|
| 76 | * @returns ENOENT if there is no such device.
|
---|
| 77 | * @returns ELIMIT if there is another module registered.
|
---|
| 78 | */
|
---|
[aadf01e] | 79 | int register_message(device_id_t device_id, int phone);
|
---|
[21580dd] | 80 |
|
---|
| 81 | /*@}*/
|
---|
| 82 |
|
---|
[aadf01e] | 83 | int netif_probe_req(int netif_phone, device_id_t device_id, int irq, int io){
|
---|
| 84 | int result;
|
---|
[21580dd] | 85 |
|
---|
[aadf01e] | 86 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
| 87 | result = netif_probe_message(device_id, irq, io);
|
---|
| 88 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[21580dd] | 89 | return result;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[aadf01e] | 92 | int netif_send_msg(int netif_phone, device_id_t device_id, packet_t packet, services_t sender){
|
---|
| 93 | int result;
|
---|
[21580dd] | 94 |
|
---|
[aadf01e] | 95 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
| 96 | result = netif_send_message(device_id, packet, sender);
|
---|
| 97 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[21580dd] | 98 | return result;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[aadf01e] | 101 | int netif_start_req(int netif_phone, device_id_t device_id){
|
---|
[21580dd] | 102 | ERROR_DECLARE;
|
---|
| 103 |
|
---|
[aadf01e] | 104 | device_ref device;
|
---|
| 105 | int result;
|
---|
| 106 | int phone;
|
---|
[21580dd] | 107 |
|
---|
[aadf01e] | 108 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
| 109 | if(ERROR_OCCURRED(find_device(device_id, &device))){
|
---|
| 110 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[21580dd] | 111 | return ERROR_CODE;
|
---|
| 112 | }
|
---|
[aadf01e] | 113 | result = netif_start_message(device);
|
---|
| 114 | if(result > NETIF_NULL){
|
---|
[21580dd] | 115 | phone = device->nil_phone;
|
---|
[aadf01e] | 116 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 117 | nil_device_state_msg(phone, device_id, result);
|
---|
[21580dd] | 118 | return EOK;
|
---|
| 119 | }else{
|
---|
[aadf01e] | 120 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[21580dd] | 121 | }
|
---|
| 122 | return result;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[aadf01e] | 125 | int netif_stop_req(int netif_phone, device_id_t device_id){
|
---|
[21580dd] | 126 | ERROR_DECLARE;
|
---|
| 127 |
|
---|
[aadf01e] | 128 | device_ref device;
|
---|
| 129 | int result;
|
---|
| 130 | int phone;
|
---|
[21580dd] | 131 |
|
---|
[aadf01e] | 132 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
| 133 | if(ERROR_OCCURRED(find_device(device_id, &device))){
|
---|
| 134 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[21580dd] | 135 | return ERROR_CODE;
|
---|
| 136 | }
|
---|
[aadf01e] | 137 | result = netif_stop_message(device);
|
---|
| 138 | if(result > NETIF_NULL){
|
---|
[21580dd] | 139 | phone = device->nil_phone;
|
---|
[aadf01e] | 140 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 141 | nil_device_state_msg(phone, device_id, result);
|
---|
[21580dd] | 142 | return EOK;
|
---|
| 143 | }else{
|
---|
[aadf01e] | 144 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[21580dd] | 145 | }
|
---|
| 146 | return result;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[aadf01e] | 149 | int netif_stats_req(int netif_phone, device_id_t device_id, device_stats_ref stats){
|
---|
[21580dd] | 150 | int res;
|
---|
| 151 |
|
---|
[aadf01e] | 152 | fibril_rwlock_read_lock(&netif_globals.lock);
|
---|
| 153 | res = netif_get_device_stats(device_id, stats);
|
---|
| 154 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
[21580dd] | 155 | return res;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[aadf01e] | 158 | int netif_get_addr_req(int netif_phone, device_id_t device_id, measured_string_ref * address, char ** data){
|
---|
[21580dd] | 159 | ERROR_DECLARE;
|
---|
| 160 |
|
---|
[aadf01e] | 161 | measured_string_t translation;
|
---|
[21580dd] | 162 |
|
---|
[aadf01e] | 163 | if(!(address && data)){
|
---|
| 164 | return EBADMEM;
|
---|
[21580dd] | 165 | }
|
---|
[aadf01e] | 166 | fibril_rwlock_read_lock(&netif_globals.lock);
|
---|
| 167 | if(! ERROR_OCCURRED(netif_get_addr_message(device_id, &translation))){
|
---|
| 168 | *address = measured_string_copy(&translation);
|
---|
| 169 | ERROR_CODE = (*address) ? EOK : ENOMEM;
|
---|
| 170 | }
|
---|
| 171 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
| 172 | *data = (** address).value;
|
---|
[21580dd] | 173 | return ERROR_CODE;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[aadf01e] | 176 | int netif_bind_service(services_t service, device_id_t device_id, services_t me, async_client_conn_t receiver){
|
---|
[21580dd] | 177 | return EOK;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[aadf01e] | 180 | int find_device(device_id_t device_id, device_ref * device){
|
---|
| 181 | if(! device){
|
---|
| 182 | return EBADMEM;
|
---|
| 183 | }
|
---|
| 184 | *device = device_map_find(&netif_globals.device_map, device_id);
|
---|
| 185 | if(! * device){
|
---|
| 186 | return ENOENT;
|
---|
| 187 | }
|
---|
| 188 | if((** device).state == NETIF_NULL) return EPERM;
|
---|
[21580dd] | 189 | return EOK;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[aadf01e] | 192 | void null_device_stats(device_stats_ref stats){
|
---|
| 193 | bzero(stats, sizeof(device_stats_t));
|
---|
[21580dd] | 194 | }
|
---|
| 195 |
|
---|
[aadf01e] | 196 | int register_message(device_id_t device_id, int phone){
|
---|
[21580dd] | 197 | ERROR_DECLARE;
|
---|
| 198 |
|
---|
[aadf01e] | 199 | device_ref device;
|
---|
[21580dd] | 200 |
|
---|
[aadf01e] | 201 | ERROR_PROPAGATE(find_device(device_id, &device));
|
---|
| 202 | if(device->nil_phone > 0){
|
---|
| 203 | return ELIMIT;
|
---|
| 204 | }
|
---|
[21580dd] | 205 | device->nil_phone = phone;
|
---|
[aadf01e] | 206 | printf("New receiver of the device %d registered:\n\tphone\t= %d\n", device->device_id, device->nil_phone);
|
---|
[21580dd] | 207 | return EOK;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[aadf01e] | 210 | int netif_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
|
---|
[21580dd] | 211 | ERROR_DECLARE;
|
---|
| 212 |
|
---|
[aadf01e] | 213 | size_t length;
|
---|
| 214 | device_stats_t stats;
|
---|
| 215 | packet_t packet;
|
---|
| 216 | measured_string_t address;
|
---|
[21580dd] | 217 |
|
---|
[aadf01e] | 218 | // printf("message %d - %d\n", IPC_GET_METHOD(*call), NET_NETIF_FIRST);
|
---|
| 219 | *answer_count = 0;
|
---|
| 220 | switch(IPC_GET_METHOD(*call)){
|
---|
[21580dd] | 221 | case IPC_M_PHONE_HUNGUP:
|
---|
| 222 | return EOK;
|
---|
| 223 | case NET_NETIF_PROBE:
|
---|
[aadf01e] | 224 | return netif_probe_req(0, IPC_GET_DEVICE(call), NETIF_GET_IRQ(call), NETIF_GET_IO(call));
|
---|
[21580dd] | 225 | case IPC_M_CONNECT_TO_ME:
|
---|
[aadf01e] | 226 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
| 227 | ERROR_CODE = register_message(IPC_GET_DEVICE(call), IPC_GET_PHONE(call));
|
---|
| 228 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[21580dd] | 229 | return ERROR_CODE;
|
---|
| 230 | case NET_NETIF_SEND:
|
---|
[aadf01e] | 231 | ERROR_PROPAGATE(packet_translate(netif_globals.net_phone, &packet, IPC_GET_PACKET(call)));
|
---|
| 232 | return netif_send_msg(0, IPC_GET_DEVICE(call), packet, IPC_GET_SENDER(call));
|
---|
[21580dd] | 233 | case NET_NETIF_START:
|
---|
[aadf01e] | 234 | return netif_start_req(0, IPC_GET_DEVICE(call));
|
---|
[21580dd] | 235 | case NET_NETIF_STATS:
|
---|
[aadf01e] | 236 | fibril_rwlock_read_lock(&netif_globals.lock);
|
---|
| 237 | if(! ERROR_OCCURRED(async_data_read_receive(&callid, &length))){
|
---|
| 238 | if(length < sizeof(device_stats_t)){
|
---|
[21580dd] | 239 | ERROR_CODE = EOVERFLOW;
|
---|
| 240 | }else{
|
---|
[aadf01e] | 241 | if(! ERROR_OCCURRED(netif_get_device_stats(IPC_GET_DEVICE(call), &stats))){
|
---|
| 242 | ERROR_CODE = async_data_read_finalize(callid, &stats, sizeof(device_stats_t));
|
---|
[21580dd] | 243 | }
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
[aadf01e] | 246 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
[21580dd] | 247 | return ERROR_CODE;
|
---|
| 248 | case NET_NETIF_STOP:
|
---|
[aadf01e] | 249 | return netif_stop_req(0, IPC_GET_DEVICE(call));
|
---|
[21580dd] | 250 | case NET_NETIF_GET_ADDR:
|
---|
[aadf01e] | 251 | fibril_rwlock_read_lock(&netif_globals.lock);
|
---|
| 252 | if(! ERROR_OCCURRED(netif_get_addr_message(IPC_GET_DEVICE(call), &address))){
|
---|
| 253 | ERROR_CODE = measured_strings_reply(&address, 1);
|
---|
[21580dd] | 254 | }
|
---|
[aadf01e] | 255 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
[21580dd] | 256 | return ERROR_CODE;
|
---|
| 257 | }
|
---|
[aadf01e] | 258 | return netif_specific_message(callid, call, answer, answer_count);
|
---|
[21580dd] | 259 | }
|
---|
| 260 |
|
---|
[aadf01e] | 261 | int netif_init_module(async_client_conn_t client_connection){
|
---|
[21580dd] | 262 | ERROR_DECLARE;
|
---|
| 263 |
|
---|
[aadf01e] | 264 | async_set_client_connection(client_connection);
|
---|
| 265 | netif_globals.net_phone = connect_to_service(SERVICE_NETWORKING);
|
---|
| 266 | device_map_initialize(&netif_globals.device_map);
|
---|
| 267 | ERROR_PROPAGATE(pm_init());
|
---|
| 268 | fibril_rwlock_initialize(&netif_globals.lock);
|
---|
| 269 | if(ERROR_OCCURRED(netif_initialize())){
|
---|
[21580dd] | 270 | pm_destroy();
|
---|
| 271 | return ERROR_CODE;
|
---|
| 272 | }
|
---|
| 273 | return EOK;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[aadf01e] | 276 | int netif_run_module(void){
|
---|
[21580dd] | 277 | async_manager();
|
---|
| 278 |
|
---|
| 279 | pm_destroy();
|
---|
| 280 | return EOK;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[aadf01e] | 283 | void netif_pq_release(packet_id_t packet_id){
|
---|
| 284 | pq_release(netif_globals.net_phone, packet_id);
|
---|
[21580dd] | 285 | }
|
---|
| 286 |
|
---|
[aadf01e] | 287 | packet_t netif_packet_get_1(size_t content){
|
---|
| 288 | return packet_get_1(netif_globals.net_phone, content);
|
---|
[21580dd] | 289 | }
|
---|
| 290 |
|
---|
| 291 | /** @}
|
---|
| 292 | */
|
---|