Subversion Repositories HelenOS

Rev

Rev 4307 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3901 mejdrech 1
/*
3912 mejdrech 2
 * Copyright (c) 2009 Lukas Mejdrech
3901 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 packet
3901 mejdrech 30
 *  @{
31
 */
32
 
33
/** @file
3912 mejdrech 34
 *  Packet client implementation.
3901 mejdrech 35
 */
36
 
37
#include <async.h>
38
#include <errno.h>
4192 mejdrech 39
#include <mem.h>
3901 mejdrech 40
#include <unistd.h>
41
//#include <stdio.h>
42
 
43
#include <ipc/ipc.h>
44
#include <sys/mman.h>
45
 
46
#include "../../err.h"
47
#include "../../messages.h"
48
 
49
#include "packet.h"
50
#include "packet_header.h"
51
#include "packet_client.h"
52
 
53
int packet_copy_data( packet_t packet, const void * data, size_t length ){
54
    if( ! packet_is_valid( packet )) return EINVAL;
55
    if( packet->data_start + length >= packet->length ) return ENOMEM;
56
    memcpy(( void * ) packet + packet->data_start, data, length );
57
    if( packet->data_start + length > packet->data_end ){
58
        packet->data_end = packet->data_start + length;
59
    }
60
    return EOK;
61
}
62
 
3912 mejdrech 63
void * packet_prefix( packet_t packet, size_t length ){
3901 mejdrech 64
    if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) - 2 * packet->addr_len < length )) return NULL;
65
    packet->data_start -= length;
66
    return ( void * ) packet + packet->data_start;
67
}
68
 
3912 mejdrech 69
void * packet_suffix( packet_t packet, size_t length ){
3901 mejdrech 70
    if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= packet->length )) return NULL;
71
    packet->data_end += length;
72
    return ( void * ) packet + packet->data_end - length;
73
}
74
 
3912 mejdrech 75
int packet_trim( packet_t packet, size_t prefix, size_t suffix ){
76
    if( ! packet_is_valid( packet )) return EINVAL;
77
    if( prefix + suffix > packet->data_end - packet->data_start ) return ENOMEM;
3901 mejdrech 78
    packet->data_start += prefix;
3912 mejdrech 79
    packet->data_end -= suffix;
3901 mejdrech 80
    return EOK;
81
}
82
 
3912 mejdrech 83
packet_id_t packet_get_id( const packet_t packet ){
3901 mejdrech 84
    return packet_is_valid( packet ) ? packet->packet_id : 0;
85
}
86
 
87
int packet_get_addr( const packet_t packet, uint8_t ** src, uint8_t ** dest ){
4395 mejdrech 88
    if( ! packet_is_valid( packet )) return EINVAL;
3912 mejdrech 89
    if( ! packet->addr_len ) return 0;
4395 mejdrech 90
    if( src ) * src = ( void * ) packet + packet->src_addr;
91
    if( dest ) * dest = ( void * ) packet + packet->dest_addr;
3901 mejdrech 92
    return packet->addr_len;
93
}
94
 
95
size_t packet_get_data_length( const packet_t packet ){
96
    if( ! packet_is_valid( packet )) return 0;
97
    return packet->data_end - packet->data_start;
98
}
99
 
100
void * packet_get_data( const packet_t packet ){
101
    if( ! packet_is_valid( packet )) return NULL;
102
    return ( void * ) packet + packet->data_start;
103
}
104
 
105
int packet_set_addr( packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len ){
106
    size_t  padding;
4395 mejdrech 107
    size_t  allocated;
3901 mejdrech 108
 
3912 mejdrech 109
    if( ! packet_is_valid( packet )) return EINVAL;
4395 mejdrech 110
    allocated = packet->dest_addr - packet->src_addr;
111
    if( allocated < addr_len ) return ENOMEM;
112
    padding = allocated - addr_len;
113
    packet->addr_len = addr_len;
3901 mejdrech 114
    if( src ){
115
        memcpy(( void * ) packet + packet->src_addr, src, addr_len );
4243 mejdrech 116
        if( padding ) bzero(( void * ) packet + packet->src_addr + addr_len, padding );
3901 mejdrech 117
    }else{
4395 mejdrech 118
        bzero(( void * ) packet + packet->src_addr, allocated );
3901 mejdrech 119
    }
120
    if( dest ){
121
        memcpy(( void * ) packet + packet->dest_addr, dest, addr_len );
4243 mejdrech 122
        if( padding ) bzero(( void * ) packet + packet->dest_addr + addr_len, padding );
3901 mejdrech 123
    }else{
4395 mejdrech 124
        bzero(( void * ) packet + packet->dest_addr, allocated );
3901 mejdrech 125
    }
126
    return EOK;
127
}
128
 
129
/** @}
130
 */