/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: 2015-04-27 00:28:57 UTC
  • Revision ID: csa@suren.me-20150427002857-82fk6r3e8rfgy4wr
First stand-alone ipecamera implementation

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#define _POSIX_C_SOURCE 200112L
2
 
#define _GNU_SOURCE
3
 
 
4
 
#include <stdio.h>
5
 
#include <string.h>
6
 
#include <unistd.h>
7
 
#include <stdint.h>
8
 
#include <assert.h>
9
 
#include <ctype.h>
10
 
#include <time.h>
11
 
#include <sched.h>
12
 
#include <arpa/inet.h>
13
 
#include <sys/time.h>
14
 
 
15
 
#include "tools.h"
16
 
#include "error.h"
17
 
 
18
 
int pcilib_isnumber(const char *str) {
19
 
    int i = 0;
20
 
    for (i = 0; str[i]; i++) 
21
 
        if (!isdigit(str[i])) return 0;
22
 
    return 1;
23
 
}
24
 
 
25
 
int pcilib_isxnumber(const char *str) {
26
 
    int i = 0;
27
 
    
28
 
    if ((str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
29
 
    
30
 
    for (; str[i]; i++) 
31
 
        if (!isxdigit(str[i])) return 0;
32
 
 
33
 
    return 1;
34
 
}
35
 
 
36
 
int pcilib_isnumber_n(const char *str, size_t len) {
37
 
    int i = 0;
38
 
    for (i = 0; (str[i])&&(i < len); i++) 
39
 
        if (!isdigit(str[i])) return 0;
40
 
    return 1;
41
 
}
42
 
 
43
 
int pcilib_isxnumber_n(const char *str, size_t len) {
44
 
    int i = 0;
45
 
    
46
 
    if ((len > 1)&&(str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
47
 
    
48
 
    for (; (str[i])&&(i < len); i++) 
49
 
        if (!isxdigit(str[i])) return 0;
50
 
 
51
 
    return 1;
52
 
}
53
 
 
54
 
 
55
 
uint16_t pcilib_swap16(uint16_t x) {
56
 
    return (((x<<8)&0xFFFF) | ((x>>8)&0xFFFF));
57
 
}
58
 
 
59
 
uint32_t pcilib_swap32(uint32_t x) {
60
 
    return ((x & 0xFF) << 24) | \
61
 
        ((x & 0xFF00) << 8) | \
62
 
        ((x & 0xFF0000) >> 8) | \
63
 
        ((x & 0xFF000000) >> 24); 
64
 
}
65
 
 
66
 
uint64_t pcilib_swap64(uint64_t x) {
67
 
    return (((uint64_t)(x) << 56) | \
68
 
        (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
69
 
        (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
70
 
        (((uint64_t)(x) << 8)  & 0xff00000000ULL) | \
71
 
        (((uint64_t)(x) >> 8)  & 0xff000000ULL) | \
72
 
        (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
73
 
        (((uint64_t)(x) >> 40) & 0xff00ULL) | \
74
 
        ((uint64_t)(x)  >> 56));
75
 
}
76
 
 
77
 
void pcilib_swap(void *dst, void *src, size_t size, size_t n) {
78
 
    int i;
79
 
    switch (size) {
80
 
        case 1:
81
 
            if (src != dst) memcpy(dst, src, n);
82
 
        break;
83
 
        case 2:
84
 
            for (i = 0; i < n; i++) {
85
 
                ((uint16_t*)dst)[i] = pcilib_swap16(((uint16_t*)src)[i]);
86
 
            }    
87
 
        break;
88
 
        case 4:
89
 
            for (i = 0; i < n; i++) {
90
 
                ((uint32_t*)dst)[i] = pcilib_swap32(((uint32_t*)src)[i]);
91
 
            }    
92
 
        break;
93
 
        case 8:
94
 
            for (i = 0; i < n; i++) {
95
 
                ((uint64_t*)dst)[i] = pcilib_swap64(((uint64_t*)src)[i]);
96
 
            }    
97
 
        break;
98
 
        default:
99
 
            pcilib_error("Invalid word size: %i", size);
100
 
    }
101
 
}
102
 
 
103
 
void *pcilib_memcpy8(void * dst, void const * src, size_t len) {
104
 
    int i;
105
 
    for (i = 0; i < len; i++) ((char*)dst)[i] = ((char*)src)[i];
106
 
    return dst;
107
 
}
108
 
 
109
 
void *pcilib_memcpy32(void * dst, void const * src, size_t len) {
110
 
    uint32_t * plDst = (uint32_t *) dst;
111
 
    uint32_t const * plSrc = (uint32_t const *) src;
112
 
 
113
 
    while (len >= 4) {
114
 
//        *plDst = ntohl(*plSrc);
115
 
        *plDst = *plSrc;
116
 
        plSrc++;
117
 
        plDst++;
118
 
        len -= 4;
119
 
    }
120
 
 
121
 
    char * pcDst = (char *) plDst;
122
 
    char const * pcSrc = (char const *) plSrc;
123
 
 
124
 
    while (len--) {
125
 
        *pcDst++ = *pcSrc++;
126
 
    }
127
 
 
128
 
    return (dst);
129
 
130
 
 
131
 
 
132
 
void *pcilib_memcpy64(void * dst, void const * src, size_t len) {
133
 
    uint64_t * plDst = (uint64_t *) dst;
134
 
    uint64_t const * plSrc = (uint64_t const *) src;
135
 
 
136
 
    while (len >= 8) {
137
 
        *plDst++ = *plSrc++;
138
 
        len -= 8;
139
 
    }
140
 
 
141
 
    char * pcDst = (char *) plDst;
142
 
    char const * pcSrc = (char const *) plSrc;
143
 
 
144
 
    while (len--) {
145
 
        *pcDst++ = *pcSrc++;
146
 
    }
147
 
 
148
 
    return (dst);
149
 
150
 
 
151
 
/*
152
 
void *memcpy128(void * dst, void const * src, size_t len) {
153
 
 
154
 
    long pos = - (len>>2);
155
 
    char * plDst = (char *) dst - 4 * pos;
156
 
    char const * plSrc = (char const *) src - 4 * pos;
157
 
 
158
 
    if (pos) {
159
 
        __asm__ __volatile__ (
160
 
            "1:                                         \n\t"
161
 
            "mov        (%0,%2,4), %%edi                \n\t"
162
 
            "mov        %%edi, (%1,%2,4)                \n\t"
163
 
            "inc        %2                              \n\t"
164
 
            "jnz        1b                              \n\t"
165
 
        : 
166
 
        : "r" (plSrc), "r" (plDst), "r" (pos)
167
 
        : "%edi"
168
 
        );
169
 
    }
170
 
 
171
 
 
172
 
 
173
 
    long pos = - ((len>>4)<<4);
174
 
    char * plDst = (char *) dst - pos;
175
 
    char const * plSrc = (char const *) src - pos;
176
 
 
177
 
    if (pos) {
178
 
        __asm__ __volatile__ (
179
 
            "1:                                         \n\t"
180
 
//            "movdqa   (%0,%2), %%xmm0                 \n\t"
181
 
            "mov        (%0,%2), %%esi                  \n\t"
182
 
            "movd       %%esi, %%xmm0                   \n\t"
183
 
            "mov        4(%0,%2), %%esi                 \n\t"
184
 
            "movd       %%esi, %%xmm1                   \n\t"
185
 
            "mov        8(%0,%2), %%esi                 \n\t"
186
 
            "movd       %%esi, %%xmm2                   \n\t"
187
 
            "mov        12(%0,%2), %%esi                \n\t"
188
 
            "movd       %%esi, %%xmm3                   \n\t"
189
 
            "pslldq     $4, %%xmm1                      \n\t"
190
 
            "por        %%xmm1, %%xmm0                  \n\t"
191
 
            "pslldq     $8, %%xmm2                      \n\t"
192
 
            "por        %%xmm2, %%xmm0                  \n\t"
193
 
            "pslldq     $12, %%xmm3                     \n\t"
194
 
            "por        %%xmm3, %%xmm0                  \n\t"
195
 
            
196
 
            "movntdq    %%xmm0, (%1,%2)                 \n\t"
197
 
            "add        $16, %2                         \n\t"
198
 
            "jnz        1b                              \n\t"
199
 
        : 
200
 
        : "r" (plSrc), "r" (plDst), "r" (pos)
201
 
        : "%rsi"
202
 
        );
203
 
    }
204
 
 
205
 
 
206
 
 
207
 
    len &= 0x3;
208
 
 
209
 
    char * pcDst = (char *) plDst;
210
 
    char const * pcSrc = (char const *) plSrc;
211
 
 
212
 
    while (len--) {
213
 
        *pcDst++ = *pcSrc++;
214
 
    }
215
 
 
216
 
    return (dst);
217
 
218
 
*/
219
 
 
220
 
void *pcilib_datacpy32(void * dst, void const * src, uint8_t size, size_t n, pcilib_endianess_t endianess) {
221
 
    uint32_t * plDst = (uint32_t *) dst;
222
 
    uint32_t const * plSrc = (uint32_t const *) src;
223
 
 
224
 
    int swap = 0;
225
 
 
226
 
    if (endianess) 
227
 
        swap = (endianess == PCILIB_BIG_ENDIAN)?(ntohs(1)!=1):(ntohs(1)==1);
228
 
 
229
 
    assert(size == 4);  // only 32 bit at the moment
230
 
 
231
 
    if (swap) {
232
 
        while (n > 0) {
233
 
            *plDst = ntohl(*plSrc);
234
 
            ++plSrc;
235
 
            ++plDst;
236
 
            --n;
237
 
        }
238
 
    } else {
239
 
        while (n > 0) {
240
 
            *plDst = *plSrc;
241
 
            ++plSrc;
242
 
            ++plDst;
243
 
            --n;
244
 
        }
245
 
    }
246
 
 
247
 
    return dst;
248
 
249
 
 
250
 
int pcilib_get_page_mask() {
251
 
    int pagesize,pagemask,temp;
252
 
 
253
 
    pagesize = sysconf(_SC_PAGESIZE);
254
 
 
255
 
    for( pagemask=0, temp = pagesize; temp != 1; ) {
256
 
        temp = (temp >> 1);
257
 
        pagemask = (pagemask << 1)+1;
258
 
    }
259
 
    return pagemask;
260
 
}
261
 
 
262
 
int pcilib_get_cpu_count() {
263
 
    int err;
264
 
 
265
 
    int cpu_count;
266
 
    cpu_set_t mask;
267
 
 
268
 
    err = sched_getaffinity(getpid(), sizeof(mask), &mask);
269
 
    if (err) return 1;
270
 
 
271
 
#ifdef CPU_COUNT
272
 
    cpu_count = CPU_COUNT(&mask);
273
 
#else
274
 
    for (cpu_count = 0; cpu_count < CPU_SETSIZE; cpu_count++) {
275
 
        if (!CPU_ISSET(cpu_count, &mask)) break;
276
 
    }
277
 
#endif
278
 
 
279
 
    if (!cpu_count) cpu_count = PCILIB_DEFAULT_CPU_COUNT;
280
 
    return cpu_count;    
281
 
}
282
 
 
283
 
 
284
 
int pcilib_add_timeout(struct timeval *tv, pcilib_timeout_t timeout) {
285
 
    tv->tv_usec += timeout%1000000;
286
 
    if (tv->tv_usec > 999999) {
287
 
        tv->tv_usec -= 1000000;
288
 
        tv->tv_sec += 1 + timeout/1000000;
289
 
    } else {
290
 
        tv->tv_sec += timeout/1000000;
291
 
    }
292
 
 
293
 
    return 0;
294
 
}
295
 
 
296
 
int pcilib_calc_deadline(struct timeval *tv, pcilib_timeout_t timeout) {
297
 
    gettimeofday(tv, NULL);
298
 
    pcilib_add_timeout(tv, timeout);
299
 
 
300
 
    return 0;
301
 
}
302
 
 
303
 
int pcilib_check_deadline(struct timeval *tve, pcilib_timeout_t timeout) {
304
 
    int64_t res;
305
 
    struct timeval tvs;
306
 
 
307
 
    if (!tve->tv_sec) return 0;
308
 
 
309
 
    gettimeofday(&tvs, NULL);
310
 
    res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
311
 
        // Hm... Some problems comparing signed and unsigned. So, sign check first
312
 
    if ((res < 0)||(res < timeout)) {
313
 
        return 1;
314
 
    }
315
 
 
316
 
    return 0;
317
 
}
318
 
 
319
 
pcilib_timeout_t pcilib_calc_time_to_deadline(struct timeval *tve) {
320
 
    int64_t res;
321
 
    struct timeval tvs;
322
 
    
323
 
    gettimeofday(&tvs, NULL);
324
 
    res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
325
 
    
326
 
    if (res < 0) return 0;
327
 
    return res;
328
 
}
329
 
 
330
 
int pcilib_sleep_until_deadline(struct timeval *tv) {
331
 
    struct timespec wait;
332
 
    pcilib_timeout_t duration;
333
 
 
334
 
    duration = pcilib_calc_time_to_deadline(tv);
335
 
    if (duration > 0) {
336
 
        wait.tv_sec = duration / 1000000;
337
 
        wait.tv_nsec = 1000 * (duration % 1000000);
338
 
        nanosleep(&wait, NULL);
339
 
    }
340
 
 
341
 
    return 0;
342
 
}
343
 
 
344
 
pcilib_timeout_t pcilib_timediff(struct timeval *tvs, struct timeval *tve) {
345
 
    return ((tve->tv_sec - tvs->tv_sec)*1000000 + (tve->tv_usec - tvs->tv_usec));
346
 
}
347
 
 
348
 
int pcilib_timecmp(struct timeval *tv1, struct timeval *tv2) {
349
 
    if (tv1->tv_sec > tv2->tv_sec) return 1;
350
 
    else if (tv1->tv_sec < tv2->tv_sec) return -1;
351
 
    else if (tv1->tv_usec > tv2->tv_usec) return 1;
352
 
    else if (tv1->tv_usec < tv2->tv_usec) return -1;
353
 
    return 0;
354
 
}