LightHub 4.6.0
Open-source smart home controller firmware
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1/* Copyright © 2017-2018 Andrey Klimov. All rights reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14
15Homepage: http://lazyhome.ru
16GIT: https://github.com/anklimov/lighthub
17e-mail anklimov@gmail.com
18
19*/
29#pragma once
30
31#define Q(x) #x
32#define QUOTE(x) Q(x)
33#define VALUE_TO_STRING(x) #x
34#define VALUE(x) VALUE_TO_STRING(x)
35#define VAR_NAME_VALUE(var) #var "=" VALUE(var)
36
37#include <Arduino.h>
38#include <IPAddress.h>
39#include "aJSON.h"
40#include "options.h"
41#include "item.h"
42#ifdef WITH_PRINTEX_LIB
43#include "PrintEx.h"
44using namespace ios;
45#else
46#include "Streaming.h"
47#endif
48
55 T_ROOT = 4,
56 T_DEV = 1,
59 };
60
62#if defined(ESP32)
63#define serialParamType uint32_t
64#else
65#define serialParamType uint16_t
66#endif
67
69void PrintBytes(uint8_t* addr, uint8_t count, bool newline);
71void SetBytes(uint8_t* addr, uint8_t count, char * out);
73bool SetAddr(char * in, uint8_t* addr);
75uint8_t HEX2DEC(char i, bool* err);
77long getIntFromStr(char **chan);
79itemCmd getNumber(char ** chan);
81unsigned long freeRam ();
83void parseBytes(const char* str, char separator, byte* bytes, int maxBytes, int base);
85int log(const char *str, ...);
87void printFloatValueToStr(char *valstr, float value);
89uint32_t ReadUniqueID( uint32_t * pdwUniqueID );
91int _inet_aton(const char* aIPAddrString, IPAddress& aResult);
93char *_inet_ntoa_r(IPAddress addr, char *buf, int buflen);
95void printIPAddress(IPAddress ipAddress);
97char* setTopic(char* buf, int8_t buflen, topicType tt, const char* suffix = NULL);
99void printUlongValueToStr(char *valstr, unsigned long value);
101void scan_i2c_bus();
105bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo = 0);
106//bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);
111bool executeCommand(aJsonObject* cmd, int8_t toggle = -1);
121bool executeCommand(aJsonObject* cmd, int8_t toggle, itemCmd _itemCmd, aJsonObject* defaultItem=NULL, aJsonObject* defaultEmit=NULL, aJsonObject* defaultCan = NULL);
123itemCmd mapInt(int32_t arg, aJsonObject* map);
125uint32_t millisNZ(uint8_t shift=0, uint32_t mask=0xFFFFFFFFUL);
129String toString(const IPAddress& address);
131bool getPinVal(uint8_t pin);
133int str2regSize(char * str);
135bool checkToken(char * token, char * data);
137 bool isProtectedPin(short pin);
139 bool i2cReset();
141 uint16_t getCRC(aJsonObject * in);
143 char* getStringFromJson(aJsonObject * a, int i);
145 char* getStringFromJson(aJsonObject * a, const char * name);
147 long getIntFromJson(aJsonObject * a, int i, long def = 0);
149 long getIntFromJson(aJsonObject * a, const char * name, long def = 0);
151 float getFloatFromJson(aJsonObject * a, int i, float def = 0.0);
153 float getFloatFromJson(aJsonObject * a, const char * name, float def = 0.0);
155 long getIntFromJson(aJsonObject * a, const char * name, int i,long def);
157float getFloatFromJson(aJsonObject * a, const char * name, int i, float def);
158
160void setupInPin(short pin);
162bool readInPin(short pin);
164void writeOutPin(short pin, bool val);
165
166// Get object from array, create if absent and return pointer to object
167 template<typename Type>
168 aJsonObject * getCreateObject(aJsonObject * a, int n, Type def); //set num value if created
169 aJsonObject * getCreateObject(aJsonObject * a, int n); //just create null object if not find
170
171// Get object from object by name, create absent find and return pointer to object
172 template<typename Type>
173 aJsonObject * getCreateObject(aJsonObject * a, const char * name, Type def); //set num value if created
174 aJsonObject * getCreateObject(aJsonObject * a, const char * name); //just create null object if not find
175
176 template<typename Type>
177 aJsonObject * setValToJson(aJsonObject * a, int n, Type val);
178 template<typename Type>
179 aJsonObject * setValToJson(aJsonObject * a, const char * name, Type val);
180
181
182
183
184template<typename Type>
185aJsonObject * getCreateObject(aJsonObject * a, int n, Type val)
186{
187if (!a) return NULL;
188if (a->type == aJson_Array)
189 {
190 aJsonObject * element = aJson.getArrayItem(a, n);
191 if (!element)
192 {
193 for (int i = aJson.getArraySize(a); i < n; i++)
194 if (i==n-1)
195 aJson.addItemToArray(a, element = aJson.createItem(val));
196 else aJson.addItemToArray(a, element = aJson.createNull());
197 }
198 return element;
199 }
200 return NULL;
201}
202
203
204
205 template<typename Type>
206 aJsonObject * getCreateObject(aJsonObject * a, const char * name, Type def)
207{
208if (!a) return NULL;
209if (a->type == aJson_Object)
210 {
211 aJsonObject * element = aJson.getObjectItem(a, name);
212 if (!element)
213 {
214 aJson.addNumberToObject(a, name, def);
215 element = aJson.getObjectItem(a, name);
216 }
217 return element;
218 }
219return NULL;
220}
221
222 template<typename Type>
223 aJsonObject * getCreateObject(aJsonObject * a, const char * name, int n, Type def)
224{
225if (!a) return NULL;
226if (a->type == aJson_Object) return getCreateObject(a,name,def);
227else if (a->type == aJson_Array) return getCreateObject(a,n,def);
228else return NULL;
229}
230
231 template<typename Type>
232 aJsonObject * setValToJson(aJsonObject * a, const char * name, Type val)
233{
234aJsonObject * element = getCreateObject(a,name);
235if (element)
236 switch (element->type)
237 {
238 case aJson_Float: element->valuefloat = val;
239 break;
240 case aJson_NULL: element->type = aJson_Int;
241 case aJson_Int: element->valueint = val;
242 }
243return element;
244}
245
246 template<typename Type>
247 aJsonObject * setValToJson(aJsonObject * a, int n, Type val)
248{
249aJsonObject * element = getCreateObject(a,n);
250
251if (element)
252 switch (element->type)
253 {
254 case aJson_Float: element->valuefloat = val;
255 break;
256 case aJson_NULL: element->type = aJson_Int;
257 case aJson_Int: element->valueint =val;
258 }
259return element;
260}
261
262#ifdef CANDRV
263#include "util/crc16_.h"
270class CRCStream : public Stream
271{
272public:
273 CRCStream() : CRC16(0xFFFF){}
275 uint16_t CRC16;
277 uint16_t getCRC16() {return CRC16;}
278
279 // Stream methods
280 virtual int available(){return 0;};
281 virtual int read(){return 0;};
282 virtual int peek(){return 0;};
283
284 virtual void flush(){};
285 // Print methods
286 virtual size_t write(uint8_t c) {CRC16 = crc16_update(CRC16, c);return 1;};
287 virtual int availableForWrite(){return 1;};
288
289};
290#endif
Command + value container passed between items, drivers and MQTT.
Definition itemCmd.h:306
#define a
Definition item.cpp:2410
Item (virtual channel) system of LightHub.
byte getCRC(byte req[], size_t size)
Definition out_ac.cpp:276
int str2regSize(char *str)
Преобразует строку в тип регистра.
Definition out_modbus.cpp:75
bool executeCommand(aJsonObject *cmd, int8_t toggle)
Definition utils.cpp:657
void parseBytes(const char *str, char separator, byte *bytes, int maxBytes, int base)
Definition utils.cpp:254
String toString(const IPAddress &address)
Definition utils.cpp:484
bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo)
Definition utils.cpp:643
void printFloatValueToStr(char *valstr, float value)
Definition utils.cpp:266
bool SetAddr(char *in, uint8_t *addr)
Definition utils.cpp:101
long getIntFromJson(aJsonObject *a, int i, long def)
Definition utils.cpp:1037
bool isProtectedPin(short pin)
Definition utils.cpp:949
bool i2cReset()
Definition utils.cpp:959
serialParamType str2SerialParam(char *str)
Definition utils.cpp:880
void SetBytes(uint8_t *addr, uint8_t count, char *out)
Definition utils.cpp:80
char * _inet_ntoa_r(IPAddress addr, char *buf, int buflen)
Definition utils.cpp:468
long getIntFromStr(char **chan)
Definition utils.cpp:113
int log(const char *str,...)
Definition utils.cpp:285
float getFloatFromJson(aJsonObject *a, int i, float def)
Definition utils.cpp:1072
char * getStringFromJson(aJsonObject *a, int i)
Definition utils.cpp:1015
void PrintBytes(uint8_t *addr, uint8_t count, bool newline)
Definition utils.cpp:68
void setupInPin(short pin)
Definition utils.cpp:1140
bool getPinVal(uint8_t pin)
Definition utils.cpp:896
itemCmd mapInt(int32_t arg, aJsonObject *map)
Definition utils.cpp:835
uint32_t millisNZ(uint8_t shift, uint32_t mask)
Definition utils.cpp:842
int _inet_aton(const char *aIPAddrString, IPAddress &aResult)
Definition utils.cpp:398
char * setTopic(char *buf, int8_t buflen, topicType tt, const char *suffix)
Definition utils.cpp:498
bool readInPin(short pin)
Definition utils.cpp:1154
byte HEX2DEC(char i, bool *err)
Definition utils.cpp:92
void writeOutPin(short pin, bool val)
Definition utils.cpp:1168
aJsonObject * getCreateObject(aJsonObject *a, int n)
Definition utils.cpp:1106
void printIPAddress(IPAddress ipAddress)
Definition utils.cpp:488
void scan_i2c_bus()
Definition utils.cpp:564
bool checkToken(char *token, char *data)
Definition utils.cpp:945
itemCmd getNumber(char **chan)
Definition utils.cpp:132
void printUlongValueToStr(char *valstr, unsigned long value)
Definition utils.cpp:546
topicType
Definition utils.h:54
@ T_BCST
Definition utils.h:57
@ T_OUT
Definition utils.h:58
@ T_ROOT
Definition utils.h:55
@ T_DEV
Definition utils.h:56
unsigned long freeRam()
uint32_t ReadUniqueID(uint32_t *pdwUniqueID)
void softRebootFunc()
aJsonObject * setValToJson(aJsonObject *a, int n, Type val)
Definition utils.h:247
#define serialParamType
Type for a serial parameter (uint32 on ESP32, uint16 elsewhere).
Definition utils.h:65