/* * hasplib-simple.c: A simple wrapper around the main API to always do the right thing. * * Copyright (C) 2014 Jason A. Donenfeld . 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 . */ #include "hasplib.h" #include 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; }