summaryrefslogtreecommitdiffstats
path: root/librazer/util.c
blob: 2ca76d5b445b1a607dc4f6f0f7a08bc83ddb79eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 *   Copyright (C) 2007-2010 Michael Buesch <mb@bu3sch.de>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "util.h"
#include "razer_private.h"

#include <unistd.h>
#include <errno.h>
#include <ctype.h>


void razer_free(void *ptr, size_t size)
{
	if (ptr) {
		memset(ptr, 0, size);
		free(ptr);
	}
}

char * razer_strsplit(char *str, char sep)
{
	char c;

	if (!str)
		return NULL;
	for (c = *str; c != '\0' && c != sep; c = *str)
		str++;
	if (c == sep) {
		*str = '\0';
		return str + 1;
	}

	return NULL;
}

int razer_split_pair(const char *str, char sep, char *a, char *b, size_t len)
{
	char *tmp;

	if (strlen(str) >= len)
		return -EINVAL;
	strcpy(a, str);
	tmp = razer_strsplit(a, sep);
	if (!tmp)
		return -EINVAL;
	strcpy(b, tmp);

	return 0;
}

int razer_string_to_int(const char *string, int *i)
{
	char *tailptr;
	long retval;

	retval = strtol(string, &tailptr, 0);
	if (tailptr == string || tailptr[0] != '\0')
		return -EINVAL;
	*i = retval;

	return 0;
}

int razer_string_to_bool(const char *string, bool *b)
{
	int i;

	if (strcasecmp(string, "yes") == 0 ||
	    strcasecmp(string, "true") == 0 ||
	    strcasecmp(string, "on") == 0) {
		*b = 1;
		return 0;
	}
	if (strcasecmp(string, "no") == 0 ||
	    strcasecmp(string, "false") == 0 ||
	    strcasecmp(string, "off") == 0) {
		*b = 0;
		return 0;
	}
	if (!razer_string_to_int(string, &i)) {
		*b = !!i;
		return 0;
	}

	return -EINVAL;
}

char * razer_string_strip(char *str)
{
	char *start = str;
	size_t len;

	if (!str)
		return NULL;
	while (*start != '\0' && isspace(*start))
		start++;
	len = strlen(start);
	while (len && isspace(start[len - 1])) {
		start[len - 1] = '\0';
		len--;
	}

	return start;
}

void razer_timeval_add_msec(struct timeval *tv, unsigned int msec)
{
	unsigned int seconds, usec;

	seconds = msec / 1000;
	msec = msec % 1000;
	usec = msec * 1000;

	tv->tv_usec += usec;
	while (tv->tv_usec >= 1000000) {
		tv->tv_sec++;
		tv->tv_usec -= 1000000;
	}
	tv->tv_sec += seconds;
}

/* Returns true, if a is after b. */
bool razer_timeval_after(const struct timeval *a, const struct timeval *b)
{
	if (a->tv_sec > b->tv_sec)
		return 1;
	if ((a->tv_sec == b->tv_sec) && (a->tv_usec > b->tv_usec))
		return 1;
	return 0;
}

/* Return a-b in milliseconds */
int razer_timeval_msec_diff(const struct timeval *a, const struct timeval *b)
{
	int64_t usec_a, usec_b, usec_diff;

	usec_a = (int64_t)a->tv_sec * 1000000;
	usec_a += (int64_t)a->tv_usec;

	usec_b = (int64_t)b->tv_sec * 1000000;
	usec_b += (int64_t)b->tv_usec;

	usec_diff = usec_a - usec_b;

	return usec_diff / 1000;
}

void razer_msleep(unsigned int msecs)
{
	int err;
	struct timespec time;

	time.tv_sec = 0;
	while (msecs >= 1000) {
		time.tv_sec++;
		msecs -= 1000;
	}
	time.tv_nsec = msecs;
	time.tv_nsec *= 1000000;
	do {
		err = nanosleep(&time, &time);
	} while (err && errno == EINTR);
	if (err) {
		razer_error("nanosleep() failed with: %s\n",
			strerror(errno));
	}
}

le16_t razer_xor16_checksum(const void *_buffer, size_t size)
{
	const uint8_t *buffer = _buffer;
	uint16_t sum = 0;
	size_t i;

	for (i = 0; i < size; i += 2) {
		sum ^= buffer[i];
		if (i < size - 1)
			sum ^= ((uint16_t)(buffer[i + 1])) << 8;
	}

	return cpu_to_le16(sum);
}

uint8_t razer_xor8_checksum(const void *_buffer, size_t size)
{
	const uint8_t *buffer = _buffer;
	uint8_t sum = 0;
	size_t i;

	for (i = 0; i < size; i++)
		sum ^= buffer[i];

	return sum;
}

void razer_dump(const char *prefix, const void *_buf, size_t size)
{
	const unsigned char *buf = _buf;
	size_t i;

	for (i = 0; i < size; i++) {
		if (i % 16 == 0) {
			if (i != 0)
				printf("\n");
			printf("%s-[%04X]:  ", prefix, (unsigned int)i);
		}
		printf("%02X%s", buf[i], (i % 2) ? " " : "");
	}
	printf("\n\n");
}
bues.ch cgit interface