util.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef RAZER_UTIL_H_
  2. #define RAZER_UTIL_H_
  3. #include "librazer.h"
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <sys/time.h>
  8. #include <time.h>
  9. #include <byteswap.h>
  10. #include <stdio.h>
  11. #include <stdbool.h>
  12. #undef min
  13. #undef max
  14. #undef offsetof
  15. #define offsetof(type, member) ((size_t)&((type *)0)->member)
  16. #define min(x, y) ({ __typeof__(x) __x = (x); \
  17. __typeof__(y) __y = (y); \
  18. __x < __y ? __x : __y; })
  19. #define max(x, y) ({ __typeof__(x) __x = (x); \
  20. __typeof__(y) __y = (y); \
  21. __x > __y ? __x : __y; })
  22. #define BUILD_BUG_ON(x) ((void)sizeof(char[1 - 2 * !!(x)]))
  23. #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
  24. #define _packed __attribute__((__packed__))
  25. typedef uint16_t be16_t;
  26. typedef uint32_t be32_t;
  27. typedef uint16_t le16_t;
  28. typedef uint32_t le32_t;
  29. #ifdef BIG_ENDIAN_HOST
  30. # define RAZER_HOST_BE 1
  31. #else
  32. # define RAZER_HOST_BE 0
  33. #endif
  34. static inline uint16_t cond_swap16(uint16_t v, bool to_from_bigendian)
  35. {
  36. if ((unsigned int)to_from_bigendian ^ RAZER_HOST_BE)
  37. return bswap_16(v);
  38. return v;
  39. }
  40. static inline uint32_t cond_swap32(uint32_t v, bool to_from_bigendian)
  41. {
  42. if ((unsigned int)to_from_bigendian ^ RAZER_HOST_BE)
  43. return bswap_32(v);
  44. return v;
  45. }
  46. static inline be16_t cpu_to_be16(uint16_t v) { return (be16_t)cond_swap16(v, 1); }
  47. static inline uint16_t be16_to_cpu(be16_t v) { return cond_swap16((uint16_t)v, 1); }
  48. static inline be32_t cpu_to_be32(uint32_t v) { return (be32_t)cond_swap32(v, 1); }
  49. static inline uint32_t be32_to_cpu(be32_t v) { return cond_swap32((uint32_t)v, 1); }
  50. static inline le16_t cpu_to_le16(uint16_t v) { return (le16_t)cond_swap16(v, 0); }
  51. static inline uint16_t le16_to_cpu(le16_t v) { return cond_swap16((uint16_t)v, 0); }
  52. static inline le32_t cpu_to_le32(uint32_t v) { return (le32_t)cond_swap32(v, 0); }
  53. static inline uint32_t le32_to_cpu(le32_t v) { return cond_swap32((uint32_t)v, 0); }
  54. static inline void * zalloc(size_t size)
  55. {
  56. return calloc(1, size);
  57. }
  58. void razer_free(void *ptr, size_t size);
  59. char * razer_strsplit(char *str, char sep);
  60. int razer_split_tuple(const char *str, char sep, size_t elems_max_len, ...);
  61. int razer_string_to_int(const char *string, int *i);
  62. int razer_string_to_bool(const char *string, bool *b);
  63. int razer_string_to_mode(const char *string, enum razer_led_mode *mode);
  64. int razer_string_to_color(const char *string, struct razer_rgb_color *color);
  65. char * razer_string_strip(char *str);
  66. void razer_timeval_add_msec(struct timeval *tv, unsigned int msec);
  67. bool razer_timeval_after(const struct timeval *a, const struct timeval *b);
  68. int razer_timeval_msec_diff(const struct timeval *a, const struct timeval *b);
  69. le16_t razer_xor16_checksum(const void *_buffer, size_t size);
  70. be16_t razer_xor16_checksum_be(const void *_buffer, size_t size);
  71. uint8_t razer_xor8_checksum(const void *_buffer, size_t size);
  72. void razer_dump(const char *prefix, const void *_buf, size_t size);
  73. static inline bool razer_buffer_is_all_zero(const void *_buf, size_t size)
  74. {
  75. const uint8_t *buf = _buf;
  76. uint8_t value = 0;
  77. size_t i;
  78. for (i = 0; i < size; i++)
  79. value |= buf[i];
  80. return value == 0;
  81. }
  82. #endif /* RAZER_UTIL_H_ */