aboutsummaryrefslogtreecommitdiffstats
path: root/hasplib-simple.c
diff options
context:
space:
mode:
Diffstat (limited to 'hasplib-simple.c')
-rw-r--r--hasplib-simple.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/hasplib-simple.c b/hasplib-simple.c
new file mode 100644
index 0000000..e246533
--- /dev/null
+++ b/hasplib-simple.c
@@ -0,0 +1,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;
+} \ No newline at end of file