supla-device
Loading...
Searching...
No Matches
key_value.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_STORAGE_KEY_VALUE_H_
5#define SRC_SUPLA_STORAGE_KEY_VALUE_H_
6
7#include <stddef.h>
8#include <stdint.h>
9
10#include "config.h"
11
12#define SUPLA_STORAGE_KEY_SIZE 15
13
14namespace Supla {
15class KeyValueElement;
16
17class KeyValue : public Config {
18 public:
19 ~KeyValue();
20 bool initFromMemory(uint8_t* input, size_t inputSize);
21 // returns size of written structure
22 size_t serializeToMemory(uint8_t* output, size_t outputMaxSize);
23 void removeAllMemory();
24
25 bool generateGuidAndAuthkey() override;
26
27 void removeAll() override;
28 bool setString(const char* key, const char* value) override;
29 bool getString(const char* key, char* value, size_t maxSize) override;
30 int getStringSize(const char* key) override;
31
32 bool setBlob(const char* key, const char* value, size_t blobSize) override;
33 bool getBlob(const char* key, char* value, size_t blobSize) override;
34
35 bool getInt8(const char* key, int8_t* result) override;
36 bool getUInt8(const char* key, uint8_t* result) override;
37 bool getInt32(const char* key, int32_t* result) override;
38 bool getUInt32(const char* key, uint32_t* result) override;
39
40 bool setInt8(const char* key, const int8_t value) override;
41 bool setUInt8(const char* key, const uint8_t value) override;
42 bool setInt32(const char* key, const int32_t value) override;
43 bool setUInt32(const char* key, const uint32_t value) override;
44 bool eraseKey(const char* key) override;
45
46 protected:
47 int getBlobSize(const char* key) override;
48 KeyValueElement* find(const char* key);
49 KeyValueElement* findOrCreate(const char* key);
50 KeyValueElement* first = nullptr;
51};
52
53enum DataType {
54 DATA_TYPE_NOT_SET = 0,
55 DATA_TYPE_UINT8,
56 DATA_TYPE_INT8,
57 DATA_TYPE_UINT32,
58 DATA_TYPE_INT32,
59 DATA_TYPE_BLOB,
60 DATA_TYPE_STRING
61};
62
64 public:
65 explicit KeyValueElement(const char* keyName);
67 bool isKeyEqual(const char* keyToCheck);
68 KeyValueElement* getNext();
69 bool hasNext();
70 void setNext(KeyValueElement* toBeSet);
71 void add(KeyValueElement* toBeAdded);
72
73 size_t serialize(uint8_t* destination, size_t maxSize);
74
75 bool setString(const char* value);
76 bool getString(char* value, size_t maxSize);
77 int getStringSize();
78
79 bool setBlob(const char* value, size_t blobSize);
80 bool getBlob(char* value, size_t blobSize);
81 int getBlobSize();
82
83 bool getInt8(int8_t* result);
84 bool getUInt8(uint8_t* result);
85 bool getInt32(int32_t* result);
86 bool getUInt32(uint32_t* result);
87
88 bool setInt8(const int8_t value);
89 bool setUInt8(const uint8_t value);
90 bool setInt32(const int32_t value);
91 bool setUInt32(const uint32_t value);
92
93 protected:
94 KeyValueElement* next = nullptr;
95 char key[SUPLA_STORAGE_KEY_SIZE + 1] = {};
96 enum DataType dataType = DATA_TYPE_NOT_SET;
97 unsigned int size = 0; // set only for blob and string
98 union {
99 uint8_t* uint8ptr = nullptr;
100 char* charPtr;
101 uint8_t uint8;
102 int8_t int8;
103 uint32_t uint32;
104 int32_t int32;
105 } data;
106};
107}; // namespace Supla
108
109#endif // SRC_SUPLA_STORAGE_KEY_VALUE_H_
Definition config.h:44
Definition key_value.h:63
Definition key_value.h:17