supla-device
Loading...
Searching...
No Matches
key_value.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_STORAGE_KEY_VALUE_H_
20#define SRC_SUPLA_STORAGE_KEY_VALUE_H_
21
22#include <stddef.h>
23#include <stdint.h>
24
25#include "config.h"
26
27#define SUPLA_STORAGE_KEY_SIZE 15
28
29namespace Supla {
30class KeyValueElement;
31
32class KeyValue : public Config {
33 public:
34 ~KeyValue();
35 bool initFromMemory(uint8_t* input, size_t inputSize);
36 // returns size of written structure
37 size_t serializeToMemory(uint8_t* output, size_t outputMaxSize);
38 void removeAllMemory();
39
40 bool generateGuidAndAuthkey() override;
41
42 void removeAll() override;
43 bool setString(const char* key, const char* value) override;
44 bool getString(const char* key, char* value, size_t maxSize) override;
45 int getStringSize(const char* key) override;
46
47 bool setBlob(const char* key, const char* value, size_t blobSize) override;
48 bool getBlob(const char* key, char* value, size_t blobSize) override;
49
50 bool getInt8(const char* key, int8_t* result) override;
51 bool getUInt8(const char* key, uint8_t* result) override;
52 bool getInt32(const char* key, int32_t* result) override;
53 bool getUInt32(const char* key, uint32_t* result) override;
54
55 bool setInt8(const char* key, const int8_t value) override;
56 bool setUInt8(const char* key, const uint8_t value) override;
57 bool setInt32(const char* key, const int32_t value) override;
58 bool setUInt32(const char* key, const uint32_t value) override;
59 bool eraseKey(const char* key) override;
60
61 protected:
62 int getBlobSize(const char* key) override;
63 KeyValueElement* find(const char* key);
64 KeyValueElement* findOrCreate(const char* key);
65 KeyValueElement* first = nullptr;
66};
67
68enum DataType {
69 DATA_TYPE_NOT_SET = 0,
70 DATA_TYPE_UINT8,
71 DATA_TYPE_INT8,
72 DATA_TYPE_UINT32,
73 DATA_TYPE_INT32,
74 DATA_TYPE_BLOB,
75 DATA_TYPE_STRING
76};
77
78class KeyValueElement {
79 public:
80 explicit KeyValueElement(const char* keyName);
81 ~KeyValueElement();
82 bool isKeyEqual(const char* keyToCheck);
83 KeyValueElement* getNext();
84 bool hasNext();
85 void setNext(KeyValueElement* toBeSet);
86 void add(KeyValueElement* toBeAdded);
87
88 size_t serialize(uint8_t* destination, size_t maxSize);
89
90 bool setString(const char* value);
91 bool getString(char* value, size_t maxSize);
92 int getStringSize();
93
94 bool setBlob(const char* value, size_t blobSize);
95 bool getBlob(char* value, size_t blobSize);
96 int getBlobSize();
97
98 bool getInt8(int8_t* result);
99 bool getUInt8(uint8_t* result);
100 bool getInt32(int32_t* result);
101 bool getUInt32(uint32_t* result);
102
103 bool setInt8(const int8_t value);
104 bool setUInt8(const uint8_t value);
105 bool setInt32(const int32_t value);
106 bool setUInt32(const uint32_t value);
107
108 protected:
109 KeyValueElement* next = nullptr;
110 char key[SUPLA_STORAGE_KEY_SIZE] = {};
111 enum DataType dataType = DATA_TYPE_NOT_SET;
112 unsigned int size = 0; // set only for blob and string
113 union {
114 uint8_t* uint8ptr = nullptr;
115 char* charPtr;
116 uint8_t uint8;
117 int8_t int8;
118 uint32_t uint32;
119 int32_t int32;
120 } data;
121};
122}; // namespace Supla
123
124#endif // SRC_SUPLA_STORAGE_KEY_VALUE_H_
Definition key_value.h:78
Definition key_value.h:32