aboutsummaryrefslogtreecommitdiffstats
path: root/hasplib-simple.c
blob: e24653353a7adc0dc593ab68dca28e99e2c40a5c (plain) (blame)
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
/*
 * hasplib-simple.c: A simple wrapper around the main API to always do the right thing.
 * 
 * Copyright (C) 2014 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 * 
 *  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 3 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "hasplib.h"
#include <stdlib.h>

struct _hasp_simple {
	hasp_dongle *dongle;
	uint16_t password1;
	uint16_t password2;
};

static void relogin(hasp_simple *simple)
{
	if (simple->dongle)
		hasp_free_dongle(simple->dongle);
	simple->dongle = hasp_find_login_first_dongle(simple->password1, simple->password2);
}

hasp_simple *hasp_simple_login(uint16_t password1, uint16_t password2)
{
	hasp_simple *simple = calloc(1, sizeof(hasp_simple));
	if (!simple)
		return NULL;
	simple->password1 = password1;
	simple->password2 = password2;
	relogin(simple);
	return simple;
}

void hasp_simple_free(hasp_simple *simple)
{
	if (simple->dongle)
		hasp_free_dongle(simple->dongle);
	free(simple);
}

uint32_t hasp_simple_id(hasp_simple *simple)
{
	uint32_t ret;
	if (simple->dongle && hasp_id(simple->dongle, &ret))
		return ret;
	relogin(simple);
	if (simple->dongle && hasp_id(simple->dongle, &ret))
		return ret;
	return 0;
}

uint16_t hasp_simple_read(hasp_simple *simple, uint16_t location)
{
	uint16_t ret;
	if (simple->dongle && hasp_read(simple->dongle, location, &ret))
		return ret;
	relogin(simple);
	if (simple->dongle && hasp_read(simple->dongle, location, &ret))
		return ret;
	return 0;
}

bool hasp_simple_write(hasp_simple *simple, uint16_t location, uint16_t value)
{
	if (simple->dongle && hasp_write(simple->dongle, location, value))
		return true;
	relogin(simple);
	if (simple->dongle && hasp_write(simple->dongle, location, value))
		return true;
	return false;
}