123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- /*
- * volcano.c
- *
- * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.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 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.
- *
- * See <http://www.gnu.org/licenses/>.
- */
-
- #include "config.h"
- #include "log.h"
- #include "ble.h"
- #include "volcano.h"
-
- #define UUID_SRVC_1 0x10
- #define UUID_FIRMWARE 0x03
- #define UUID_PRJSTAT1 0x0C
- #define UUID_PRJSTAT2 0x0D
- #define UUID_PRJSTAT3 0x0E
-
- #define UUID_SRVC_2 0x11
- #define UUID_WRITE_SRVC 0x00
- #define UUID_CURRENT_TEMP 0x01
- #define UUID_TARGET_TEMP 0x03
- #define UUID_BRIGHTNESS 0x05
- #define UUID_SHUTOFF_TIME 0x0D
- #define UUID_HEATER_ON 0x0F
- #define UUID_HEATER_OFF 0x10
- #define UUID_PUMP_ON 0x13
- #define UUID_PUMP_OFF 0x14
- #define UUID_HEAT_HOURS 0x15
- #define UUID_HEAT_MINUTES 0x16
-
- #define MASK_PRJSTAT1_HEIZUNG_ENA 0x0020
- #define MASK_PRJSTAT1_AUTOBLESHUTDOWN 0x0200
- #define MASK_PRJSTAT1_PUMPE_FET_ENABLE 0x2000
-
- #define MASK_PRJSTAT2_FAHRENHEIT_ENA 0x0200
- #define MASK_PRJSTAT2_DISPLAY_ON_COOLING 0x1000
-
- #define MASK_PRJSTAT3_VIBRATION 0x0400
-
- // "10xx00xx-5354-4f52-5a26-4249434b454c"
- static uint8_t uuid_base[16] = {
- 0x10, 0xFF, 0x00, 0xFF, 0x53, 0x54, 0x4f, 0x52,
- 0x5a, 0x26, 0x42, 0x49, 0x43, 0x4b, 0x45, 0x4c,
- };
- static uint8_t uuid_base2[16] = {
- 0x10, 0xFF, 0x00, 0xFF, 0x53, 0x54, 0x4f, 0x52,
- 0x5a, 0x26, 0x42, 0x49, 0x43, 0x4b, 0x45, 0x4c,
- };
-
- int8_t volcano_discover_characteristics(bool wf, bool conf) {
- if (wf) {
- uuid_base[1] = UUID_SRVC_2;
- uuid_base2[1] = UUID_SRVC_2;
-
- uuid_base[3] = UUID_WRITE_SRVC;
- int8_t r;
-
- uuid_base2[3] = UUID_TARGET_TEMP;
- r = ble_discover(uuid_base, uuid_base2);
- if (r < 0) {
- return r;
- }
-
- uuid_base2[3] = UUID_HEATER_ON;
- r = ble_discover(uuid_base, uuid_base2);
- if (r < 0) {
- return r;
- }
-
- uuid_base2[3] = UUID_HEATER_OFF;
- r = ble_discover(uuid_base, uuid_base2);
- if (r < 0) {
- return r;
- }
-
- uuid_base2[3] = UUID_PUMP_ON;
- r = ble_discover(uuid_base, uuid_base2);
- if (r < 0) {
- return r;
- }
-
- uuid_base2[3] = UUID_PUMP_OFF;
- r = ble_discover(uuid_base, uuid_base2);
- if (r < 0) {
- return r;
- }
- }
-
- if (conf) {
- uuid_base[1] = UUID_SRVC_1;
- uuid_base2[1] = UUID_SRVC_1;
-
- uuid_base[3] = UUID_WRITE_SRVC;
- int8_t r;
-
- uuid_base2[3] = UUID_PRJSTAT1;
- r = ble_discover(uuid_base, uuid_base2);
- if (r < 0) {
- return r;
- }
-
- uuid_base2[3] = UUID_PRJSTAT2;
- r = ble_discover(uuid_base, uuid_base2);
- if (r < 0) {
- return r;
- }
-
- uuid_base2[3] = UUID_PRJSTAT3;
- r = ble_discover(uuid_base, uuid_base2);
- if (r < 0) {
- return r;
- }
- }
-
- return 0;
- }
-
- int16_t volcano_get_current_temp(void) {
- uuid_base[1] = UUID_SRVC_2;
- uuid_base[3] = UUID_CURRENT_TEMP;
-
- uint8_t buff[4];
- int32_t r = ble_read(uuid_base, buff, sizeof(buff));
- if (r != sizeof(buff)) {
- debug("ble_read unexpected value %ld", r);
- return -1;
- }
-
- uint32_t *v = (uint32_t *)buff;
- return *v;
- }
-
- int16_t volcano_get_target_temp(void) {
- uuid_base[3] = UUID_TARGET_TEMP;
-
- uint8_t buff[4];
- int32_t r = ble_read(uuid_base, buff, sizeof(buff));
- if (r != sizeof(buff)) {
- debug("ble_read unexpected value %ld", r);
- return -1;
- }
-
- uint32_t *v = (uint32_t *)buff;
- return *v;
- }
-
- int8_t volcano_set_target_temp(uint16_t value) {
- uuid_base[1] = UUID_SRVC_2;
- uuid_base2[1] = UUID_SRVC_2;
- uuid_base[3] = UUID_WRITE_SRVC;
- uuid_base2[3] = UUID_TARGET_TEMP;
-
- uint8_t buff[4];
- uint32_t *v = (uint32_t *)buff;
- *v = value;
-
- int8_t r = ble_write(uuid_base, uuid_base2, buff, sizeof(buff));
- if (r != 0) {
- debug("ble_write unexpected value %d", r);
- }
- return r;
- }
-
- int8_t volcano_set_heater_state(bool value) {
- uuid_base[1] = UUID_SRVC_2;
- uuid_base2[1] = UUID_SRVC_2;
- uuid_base[3] = UUID_WRITE_SRVC;
-
- if (value) {
- uuid_base2[3] = UUID_HEATER_ON;
- } else {
- uuid_base2[3] = UUID_HEATER_OFF;
- }
-
- uint8_t d = 0;
- int8_t r = ble_write(uuid_base, uuid_base2, &d, sizeof(d));
- if (r != 0) {
- debug("ble_write unexpected value %d", r);
- }
- return r;
- }
-
- int8_t volcano_set_pump_state(bool value) {
- uuid_base[1] = UUID_SRVC_2;
- uuid_base2[1] = UUID_SRVC_2;
- uuid_base[3] = UUID_WRITE_SRVC;
-
- if (value) {
- uuid_base2[3] = UUID_PUMP_ON;
- } else {
- uuid_base2[3] = UUID_PUMP_OFF;
- }
-
- uint8_t d = 0;
- int8_t r = ble_write(uuid_base, uuid_base2, &d, sizeof(d));
- if (r != 0) {
- debug("ble_write unexpected value %d", r);
- }
- return r;
- }
-
- enum unit volcano_get_unit(void) {
- uuid_base[1] = UUID_SRVC_1;
- uuid_base[3] = UUID_PRJSTAT2;
-
- uint8_t buff[4];
- int32_t r = ble_read(uuid_base, buff, sizeof(buff));
- if (r != sizeof(buff)) {
- debug("ble_read unexpected value %ld", r);
- return UNIT_INVALID;
- }
-
- uint32_t *v = (uint32_t *)buff;
- return (*v & MASK_PRJSTAT2_FAHRENHEIT_ENA) ? UNIT_F : UNIT_C;
- }
-
- enum volcano_state volcano_get_state(void) {
- uuid_base[1] = UUID_SRVC_1;
- uuid_base[3] = UUID_PRJSTAT1;
-
- uint8_t buff[4];
- int32_t r = ble_read(uuid_base, buff, sizeof(buff));
- if (r != sizeof(buff)) {
- debug("ble_read unexpected value %ld", r);
- return VOLCANO_STATE_INVALID;
- }
-
- uint32_t *v = (uint32_t *)buff;
- uint32_t heater = (*v & MASK_PRJSTAT1_HEIZUNG_ENA);
- uint32_t pump = (*v & MASK_PRJSTAT1_PUMPE_FET_ENABLE);
- return (heater ? VOLCANO_STATE_HEATER : 0) | (pump ? VOLCANO_STATE_PUMP : 0);
- }
-
- int8_t volcano_set_unit(enum unit unit) {
- uuid_base[1] = UUID_SRVC_1;
- uuid_base2[1] = UUID_SRVC_1;
- uuid_base[3] = UUID_WRITE_SRVC;
- uuid_base2[3] = UUID_PRJSTAT2;
-
- uint32_t v = MASK_PRJSTAT2_FAHRENHEIT_ENA;
- if (unit == UNIT_F) {
- v |= 0x10000;
- }
-
- int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&v, sizeof(v));
- if (r != 0) {
- debug("ble_write unexpected value %d", r);
- }
- return r;
- }
-
- int8_t volcano_set_vibration(bool value) {
- uuid_base[1] = UUID_SRVC_1;
- uuid_base2[1] = UUID_SRVC_1;
- uuid_base[3] = UUID_WRITE_SRVC;
- uuid_base2[3] = UUID_PRJSTAT3;
-
- uint32_t v = MASK_PRJSTAT3_VIBRATION;
- if (!value) {
- v |= 0x10000;
- }
-
- int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&v, sizeof(v));
- if (r != 0) {
- debug("ble_write unexpected value %d", r);
- }
- return r;
- }
-
- int8_t volcano_get_vibration(void) {
- uuid_base[1] = UUID_SRVC_1;
- uuid_base[3] = UUID_PRJSTAT3;
-
- uint8_t buff[4];
- int32_t r = ble_read(uuid_base, buff, sizeof(buff));
- if (r != sizeof(buff)) {
- debug("ble_read unexpected value %ld", r);
- return -1;
- }
-
- uint32_t *v = (uint32_t *)buff;
- return (*v & MASK_PRJSTAT3_VIBRATION) ? 0 : 1;
- }
-
- int8_t volcano_set_display_cooling(bool value) {
- uuid_base[1] = UUID_SRVC_1;
- uuid_base2[1] = UUID_SRVC_1;
- uuid_base[3] = UUID_WRITE_SRVC;
- uuid_base2[3] = UUID_PRJSTAT2;
-
- uint32_t v = MASK_PRJSTAT2_DISPLAY_ON_COOLING;
- if (!value) {
- v |= 0x10000;
- }
-
- int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&v, sizeof(v));
- if (r != 0) {
- debug("ble_write unexpected value %d", r);
- }
- return r;
- }
-
- int8_t volcano_get_display_cooling(void) {
- uuid_base[1] = UUID_SRVC_1;
- uuid_base[3] = UUID_PRJSTAT2;
-
- uint8_t buff[4];
- int32_t r = ble_read(uuid_base, buff, sizeof(buff));
- if (r != sizeof(buff)) {
- debug("ble_read unexpected value %ld", r);
- return -1;
- }
-
- uint32_t *v = (uint32_t *)buff;
- return (*v & MASK_PRJSTAT2_DISPLAY_ON_COOLING) ? 0 : 1;
- }
-
- int16_t volcano_get_auto_shutoff(void) {
- uuid_base[1] = UUID_SRVC_2;
- uuid_base[3] = UUID_SHUTOFF_TIME;
-
- uint8_t buff[2];
- int32_t r = ble_read(uuid_base, buff, sizeof(buff));
- if (r != sizeof(buff)) {
- debug("ble_read unexpected value %ld", r);
- return -1;
- }
-
- uint16_t *v = (uint16_t *)buff;
- return *v;
-
- }
-
- int8_t volcano_set_auto_shutoff(uint16_t v) {
- uuid_base[1] = UUID_SRVC_2;
- uuid_base2[1] = UUID_SRVC_2;
- uuid_base[3] = UUID_WRITE_SRVC;
- uuid_base2[3] = UUID_SHUTOFF_TIME;
-
- int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&v, sizeof(v));
- if (r != 0) {
- debug("ble_write unexpected value %d", r);
- }
- return r;
- }
-
- int8_t volcano_get_brightness(void) {
- uuid_base[1] = UUID_SRVC_2;
- uuid_base[3] = UUID_BRIGHTNESS;
-
- uint8_t buff[2];
- int32_t r = ble_read(uuid_base, buff, sizeof(buff));
- if (r != sizeof(buff)) {
- debug("ble_read unexpected value %ld", r);
- return -1;
- }
-
- uint16_t *v = (uint16_t *)buff;
- return *v;
- }
-
- int8_t volcano_set_brightness(uint8_t val) {
- uuid_base[1] = UUID_SRVC_2;
- uuid_base2[1] = UUID_SRVC_2;
- uuid_base[3] = UUID_WRITE_SRVC;
- uuid_base2[3] = UUID_BRIGHTNESS;
-
- uint16_t v = val;
- int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&v, sizeof(v));
- if (r != 0) {
- debug("ble_write unexpected value %d", r);
- }
- return r;
- }
-
- int8_t volcano_get_firmware(char *val) {
- if (val == NULL) {
- return -1;
- }
-
- uuid_base[1] = UUID_SRVC_1;
- uuid_base[3] = UUID_FIRMWARE;
-
- uint8_t buff[VOLCANO_FW_LEN];
- int32_t r = ble_read(uuid_base, buff, sizeof(buff));
- if (r != sizeof(buff)) {
- debug("ble_read unexpected value %ld", r);
- return -1;
- }
-
- memcpy(val, buff, sizeof(buff));
- return 0;
- }
-
- int32_t volcano_get_runtime(void) {
- int32_t val = 0;
-
- uuid_base[1] = UUID_SRVC_2;
- uuid_base[3] = UUID_HEAT_HOURS;
-
- uint32_t buff;
- int32_t r = ble_read(uuid_base, (uint8_t *)&buff, sizeof(buff));
- if (r != sizeof(buff)) {
- debug("ble_read 1 unexpected value %ld", r);
- return -1;
- }
-
- val = buff * 60;
- uuid_base[3] = UUID_HEAT_MINUTES;
-
- uint16_t buff2;
- r = ble_read(uuid_base, (uint8_t *)&buff2, sizeof(buff2));
- if (r != sizeof(buff2)) {
- debug("ble_read 2 unexpected value %ld", r);
- return -1;
- }
-
- val += buff2;
- return val;
- }
|