/alps/ipecamera

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/ipecamera

« back to all changes in this revision

Viewing changes to tools.c

  • Committer: Suren A. Chilingaryan
  • Date: 2011-03-08 21:46:14 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: csa@dside.dyndns.org-20110308214614-g5v841m61jilcrm2
Initial support of IPECamera protocol

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#include <string.h>
3
3
#include <unistd.h>
4
4
#include <stdint.h>
5
 
 
6
 
void *memcpy8(void * dst, void const * src, size_t len) {
 
5
#include <assert.h>
 
6
#include <arpa/inet.h>
 
7
 
 
8
#include "tools.h"
 
9
 
 
10
uint16_t pcilib_swap16(uint16_t x) {
 
11
    return (((x<<8)&0xFFFF) | ((x>>8)&0xFFFF));
 
12
}
 
13
 
 
14
uint32_t pcilib_swap32(uint32_t x) {
 
15
    return ((x & 0xFF) << 24) | \
 
16
        ((x & 0xFF00) << 8) | \
 
17
        ((x & 0xFF0000) >> 8) | \
 
18
        ((x & 0xFF000000) >> 24); 
 
19
}
 
20
 
 
21
uint64_t pcilib_swap64(uint64_t x) {
 
22
    return (((uint64_t)(x) << 56) | \
 
23
        (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
 
24
        (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
 
25
        (((uint64_t)(x) << 8)  & 0xff00000000ULL) | \
 
26
        (((uint64_t)(x) >> 8)  & 0xff000000ULL) | \
 
27
        (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
 
28
        (((uint64_t)(x) >> 40) & 0xff00ULL) | \
 
29
        ((uint64_t)(x)  >> 56));
 
30
}
 
31
 
 
32
void pcilib_swap(void *dst, void *src, size_t size, size_t n) {
 
33
    int i;
 
34
    switch (size) {
 
35
        case 1:
 
36
            if (src != dst) memcpy(dst, src, n);
 
37
        break;
 
38
        case 2:
 
39
            for (i = 0; i < n; i++) {
 
40
                ((uint16_t*)dst)[i] = pcilib_swap16(((uint16_t*)src)[i]);
 
41
            }    
 
42
        break;
 
43
        case 4:
 
44
            for (i = 0; i < n; i++) {
 
45
                ((uint32_t*)dst)[i] = pcilib_swap32(((uint32_t*)src)[i]);
 
46
            }    
 
47
        break;
 
48
        case 8:
 
49
            for (i = 0; i < n; i++) {
 
50
                ((uint64_t*)dst)[i] = pcilib_swap64(((uint64_t*)src)[i]);
 
51
            }    
 
52
        break;
 
53
        default:
 
54
            pcilib_error("Invalid word size: %i", size);
 
55
    }
 
56
}
 
57
 
 
58
void *pcilib_memcpy8(void * dst, void const * src, size_t len) {
7
59
    int i;
8
60
    for (i = 0; i < len; i++) ((char*)dst)[i] = ((char*)src)[i];
9
61
    return dst;
10
62
}
11
63
 
12
 
 
13
 
void *memcpy32(void * dst, void const * src, size_t len) {
 
64
void *pcilib_memcpy32(void * dst, void const * src, size_t len) {
14
65
    uint32_t * plDst = (uint32_t *) dst;
15
66
    uint32_t const * plSrc = (uint32_t const *) src;
16
67
 
17
68
    while (len >= 4) {
18
 
        uint32_t a = (*plSrc & 0xFF) << 24;
19
 
        a |= (*plSrc & 0xFF00) << 8;
20
 
        a |= (*plSrc & 0xFF0000) >> 8;
21
 
        a |= (*plSrc & 0xFF000000) >> 24;
22
 
        *plDst = a;
 
69
//        *plDst = ntohl(*plSrc);
 
70
        *plDst = *plSrc;
23
71
        plSrc++;
24
72
        plDst++;
25
73
        len -= 4;
35
83
    return (dst);
36
84
37
85
 
38
 
void *memcpy64(void * dst, void const * src, size_t len) {
 
86
 
 
87
void *pcilib_memcpy64(void * dst, void const * src, size_t len) {
39
88
    uint64_t * plDst = (uint64_t *) dst;
40
89
    uint64_t const * plSrc = (uint64_t const *) src;
41
90
 
123
172
124
173
*/
125
174
 
126
 
 
127
 
int get_page_mask() {
 
175
void *pcilib_datacpy32(void * dst, void const * src, uint8_t size, size_t n, pcilib_endianess_t endianess) {
 
176
    uint32_t * plDst = (uint32_t *) dst;
 
177
    uint32_t const * plSrc = (uint32_t const *) src;
 
178
 
 
179
    int swap = (endianess == PCILIB_BIG_ENDIAN)?(ntohs(1)!=1):(ntohs(1)==1);
 
180
 
 
181
    assert(size == 4);  // only 32 bit at the moment
 
182
 
 
183
    while (n > 0) {
 
184
        if (swap) *plDst = ntohl(*plSrc);
 
185
        else *plDst = *plSrc;
 
186
 
 
187
        ++plSrc;
 
188
        ++plDst;
 
189
 
 
190
        --n;
 
191
    }
 
192
 
193
 
 
194
 
 
195
int pcilib_get_page_mask() {
128
196
    int pagesize,pagemask,temp;
129
197
 
130
 
    pagesize = getpagesize();
 
198
    pagesize = sysconf(_SC_PAGESIZE);
131
199
 
132
200
    for( pagemask=0, temp = pagesize; temp != 1; ) {
133
201
        temp = (temp >> 1);