diff options
author | Suren A. Chilingaryan <csa@dside.dyndns.org> | 2011-03-08 22:46:14 +0100 |
---|---|---|
committer | Suren A. Chilingaryan <csa@dside.dyndns.org> | 2011-03-08 22:46:14 +0100 |
commit | 7f97ea07417de4c2ea260e2860e589011c732e04 (patch) | |
tree | 5e5dd32f4615a40990f8daf10a96f82f39ca3b83 /tools.c | |
parent | 8a33c993a5771b041b67c365378ac40f76365da7 (diff) | |
download | pcitool-7f97ea07417de4c2ea260e2860e589011c732e04.tar.gz pcitool-7f97ea07417de4c2ea260e2860e589011c732e04.tar.bz2 pcitool-7f97ea07417de4c2ea260e2860e589011c732e04.tar.xz pcitool-7f97ea07417de4c2ea260e2860e589011c732e04.zip |
Initial support of IPECamera protocol
Diffstat (limited to 'tools.c')
-rw-r--r-- | tools.c | 90 |
1 files changed, 79 insertions, 11 deletions
@@ -2,24 +2,72 @@ #include <string.h> #include <unistd.h> #include <stdint.h> +#include <assert.h> +#include <arpa/inet.h> -void *memcpy8(void * dst, void const * src, size_t len) { +#include "tools.h" + +uint16_t pcilib_swap16(uint16_t x) { + return (((x<<8)&0xFFFF) | ((x>>8)&0xFFFF)); +} + +uint32_t pcilib_swap32(uint32_t x) { + return ((x & 0xFF) << 24) | \ + ((x & 0xFF00) << 8) | \ + ((x & 0xFF0000) >> 8) | \ + ((x & 0xFF000000) >> 24); +} + +uint64_t pcilib_swap64(uint64_t x) { + return (((uint64_t)(x) << 56) | \ + (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \ + (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \ + (((uint64_t)(x) << 8) & 0xff00000000ULL) | \ + (((uint64_t)(x) >> 8) & 0xff000000ULL) | \ + (((uint64_t)(x) >> 24) & 0xff0000ULL) | \ + (((uint64_t)(x) >> 40) & 0xff00ULL) | \ + ((uint64_t)(x) >> 56)); +} + +void pcilib_swap(void *dst, void *src, size_t size, size_t n) { + int i; + switch (size) { + case 1: + if (src != dst) memcpy(dst, src, n); + break; + case 2: + for (i = 0; i < n; i++) { + ((uint16_t*)dst)[i] = pcilib_swap16(((uint16_t*)src)[i]); + } + break; + case 4: + for (i = 0; i < n; i++) { + ((uint32_t*)dst)[i] = pcilib_swap32(((uint32_t*)src)[i]); + } + break; + case 8: + for (i = 0; i < n; i++) { + ((uint64_t*)dst)[i] = pcilib_swap64(((uint64_t*)src)[i]); + } + break; + default: + pcilib_error("Invalid word size: %i", size); + } +} + +void *pcilib_memcpy8(void * dst, void const * src, size_t len) { int i; for (i = 0; i < len; i++) ((char*)dst)[i] = ((char*)src)[i]; return dst; } - -void *memcpy32(void * dst, void const * src, size_t len) { +void *pcilib_memcpy32(void * dst, void const * src, size_t len) { uint32_t * plDst = (uint32_t *) dst; uint32_t const * plSrc = (uint32_t const *) src; while (len >= 4) { - uint32_t a = (*plSrc & 0xFF) << 24; - a |= (*plSrc & 0xFF00) << 8; - a |= (*plSrc & 0xFF0000) >> 8; - a |= (*plSrc & 0xFF000000) >> 24; - *plDst = a; +// *plDst = ntohl(*plSrc); + *plDst = *plSrc; plSrc++; plDst++; len -= 4; @@ -35,7 +83,8 @@ void *memcpy32(void * dst, void const * src, size_t len) { return (dst); } -void *memcpy64(void * dst, void const * src, size_t len) { + +void *pcilib_memcpy64(void * dst, void const * src, size_t len) { uint64_t * plDst = (uint64_t *) dst; uint64_t const * plSrc = (uint64_t const *) src; @@ -123,11 +172,30 @@ void *memcpy128(void * dst, void const * src, size_t len) { } */ +void *pcilib_datacpy32(void * dst, void const * src, uint8_t size, size_t n, pcilib_endianess_t endianess) { + uint32_t * plDst = (uint32_t *) dst; + uint32_t const * plSrc = (uint32_t const *) src; + + int swap = (endianess == PCILIB_BIG_ENDIAN)?(ntohs(1)!=1):(ntohs(1)==1); + + assert(size == 4); // only 32 bit at the moment + + while (n > 0) { + if (swap) *plDst = ntohl(*plSrc); + else *plDst = *plSrc; + + ++plSrc; + ++plDst; + + --n; + } +} + -int get_page_mask() { +int pcilib_get_page_mask() { int pagesize,pagemask,temp; - pagesize = getpagesize(); + pagesize = sysconf(_SC_PAGESIZE); for( pagemask=0, temp = pagesize; temp != 1; ) { temp = (temp >> 1); |