-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpmax.h
334 lines (278 loc) · 9.62 KB
/
pmax.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#pragma once
#include <stdio.h>
#include "FixedSizeQueue.h"
#include "MemoryMap.h"
#define MAX_BUFFER_SIZE 250
#define MAX_SEND_BUFFER_SIZE 15
#define MAX_SEND_QUEUE_DEPTH 15
#define PACKET_TIMEOUT_DEFINED 2000
#define MAX_ZONE_COUNT 31
//This Pin is a temporary pin for powerlink (this app). Does not have to match any of user or installer codes.
//If your pin is 1234, you need to return 0x1234 (this is strange, as 0x makes it hex, but the only way it works).
#define POWERLINK_PIN 0x3622;
class PowerMaxAlarm;
enum PmaxCommand
{
Pmax_ACK,
Pmax_PING,
Pmax_GETEVENTLOG,
Pmax_DISARM,
Pmax_ARMHOME,
Pmax_ARMAWAY,
Pmax_ARMAWAY_INSTANT,
Pmax_REQSTATUS,
Pmax_ENROLLREPLY,
Pmax_INIT,
Pmax_RESTORE,
Pmax_DL_START,
Pmax_DL_GET,
Pmax_DL_EXIT, //stop download mode
Pmax_DL_PANELFW,
Pmax_DL_SERIAL,
Pmax_DL_ZONESTR
};
enum ZoneEvent
{
ZE_None,
ZE_TamperAlarm,
ZE_TamperRestore,
ZE_Open,
ZE_Closed,
ZE_Violated,
ZE_PanicAlarm,
ZE_RFJamming,
ZE_TamperOpen,
ZE_CommunicationFailure,
ZE_LineFailure,
ZE_Fuse,
ZE_NotActive,
ZE_LowBattery,
ZE_ACFailure,
ZE_FireAlarm,
ZE_Emergency,
ZE_SirenTamper,
ZE_SirenTamperRestore,
ZE_SirenLowBattery,
ZE_SirenACFail
};
enum SystemStatus
{
SS_Disarm = 0x00,
SS_Exit_Delay = 0x01,
SS_Exit_Delay2 = 0x02,
SS_Entry_Delay = 0x03,
SS_Armed_Home = 0x04,
SS_Armed_Away = 0x05,
SS_User_Test = 0x06,
SS_Downloading = 0x07,
SS_Programming = 0x08,
SS_Installer = 0x09,
SS_Home_Bypass = 0x0A,
SS_Away_Bypass = 0x0B,
SS_Ready = 0x0C,
SS_Not_Ready = 0x0D
};
enum PmAckType
{
ACK_1,
ACK_2
};
//this abstract class is used by DumpToJson API
//it allows to redirect JSON to file, console, www output
class IOutput
{
public:
virtual void write(const char* str) = 0;
void writeQuotedStr(const char* str);
void writeJsonTag(const char* name, bool value, bool addComma = true);
void writeJsonTag(const char* name, int value, bool addComma = true);
void writeJsonTag(const char* name, const char* value, bool addComma = true, bool quoteValue = true);
};
class ConsoleOutput : public IOutput
{
public:
void write(const char* str);
};
struct PlinkCommand {
unsigned char buffer[MAX_SEND_BUFFER_SIZE];
int size;
const char* description;
void (PowerMaxAlarm::*action)(const struct PlinkBuffer *);
};
struct PlinkBuffer {
unsigned char buffer[MAX_BUFFER_SIZE];
int size;
};
struct ZoneState {
bool lowBattery; //battery needs replacing
bool tamper; //someone tampered with the device
bool doorOpen; //door is open (either intrusion or not redy to arm)
bool bypased; //user temporarly disabled this zone
bool active; //commication with one is OK
};
struct Zone {
bool enrolled; //PowerMax knows about this zone (it's configured)
char name[0x11]; //Will be dowloaded from PowerMax eprom
unsigned char zonetype;
unsigned char sensorid;
const char* sensortype;
const char* autocreate;
ZoneState stat; //basic state of the zone
ZoneEvent lastEvent; //last event recodred for this zone
unsigned long lastEventTime;
void DumpToJson(IOutput* outputStream);
};
struct PmQueueItem
{
unsigned char buffer[MAX_SEND_BUFFER_SIZE];
int bufferLen;
const char* description;
unsigned char expectedRepply;
const char* options;
};
struct PmConfig
{
bool parsedOK;
char installerPin[5];
char masterInstallerPin[5];
char powerLinkPin[5];
char userPins[48][5];
//telephone numbers to call:
char phone[4][15]; //15: max 14 digits + NULL
char serialNumber[15];
char eprom[17];
char software[17];
unsigned char partitionCnt;
//panel max capabilities (not actual count used):
unsigned char maxZoneCnt;
unsigned char maxCustomCnt;
unsigned char maxUserCnt;
unsigned char maxPartitionCnt;
unsigned char maxSirenCnt;
unsigned char maxKeypad1Cnt;
unsigned char maxKeypad2Cnt;
unsigned char maxKeyfobCnt;
PmConfig()
{
Init();
}
void Init()
{
memset(this, 0, sizeof(PmConfig));
}
void DumpToJson(IOutput* outputStream);
int GetMasterPinAsHex() const;
};
//NOTE: if you want to add some new functionality (like MQTT support), don't modify this class.
//Rather than: derive from this class, and override one of the virtual functions.
//This class should contain only code to comminicate with PowerMax alarms.
class PowerMaxAlarm
{
protected:
//Flags with[*] are one-shot notifications of last event
//For example when user arms away - bit 6 will be set
//When system will deliver 'last 10 sec' notification - bit 6 will be cleared
//bit 0: Ready if set
//bit 1: [?] Alert in Memory if set
//bit 2: [?] Trouble if set
//bit 3: [?] Bypass On if set
//bit 4: [*] Last 10 seconds of entry or exit delay if set
//bit 5: [*] Zone event if set
//bit 6: [*] Arm, disarm event
//bit 7: [?] Alarm event if set
unsigned char flags;
//overall system status
SystemStatus stat;
//status of all zones (0 is not used, system)
Zone zone[MAX_ZONE_COUNT];
//config downloaded from PM, parsed using ProcessSettings
PmConfig m_cfg;
//used to detect when PowerMax stops talking to us, that will trigger re-establish comms message
time_t lastIoTime;
FixedSizeQueue<PmQueueItem, MAX_SEND_QUEUE_DEPTH> m_sendQueue;
bool m_bEnrolCompleted;
bool m_bDownloadMode;
int m_iPanelType;
int m_iModelType;
bool m_bPowerMaster;
PmAckType m_ackTypeForLastMsg;
//used to store data downloaded from PM
MemoryMap m_mapMain;
MemoryMap m_mapExtended;
PlinkCommand m_lastSentCommand;
unsigned long m_ulLastPing;
public:
void init();
void sendNextCommand();
void clearQueue(){ m_sendQueue.clear(); }
bool sendCommand(PmaxCommand cmd);
void handlePacket(PlinkBuffer * commandBuffer);
static bool isBufferOK(const PlinkBuffer* commandBuffer);
const char* getZoneName(unsigned char zoneId);
unsigned int getEnrolledZoneCnt() const;
unsigned long getSecondsFromLastComm() const;
void dumpToJson(IOutput* outputStream);
#ifdef _MSC_VER
void IZIZTODO_testMap();
#endif
//override thise function in derived class to handle notifications
virtual void OnStatusUpdateZoneBat(const PlinkBuffer * Buff);
virtual void OnStatusUpdatePanel(const PlinkBuffer * Buff);
virtual void OnStatusUpdateZoneBypassed(const PlinkBuffer * Buff);
virtual void OnStatusUpdateZoneTamper(const PlinkBuffer * Buff);
virtual void OnStatusChange(const PlinkBuffer * Buff);
virtual void OnStatusUpdate(const PlinkBuffer * Buff);
virtual void OnEventLog(const PlinkBuffer * Buff);
virtual void OnAccessDenied(const PlinkBuffer * Bufff);
virtual void OnAck(const PlinkBuffer * Buff);
virtual void OnTimeOut(const PlinkBuffer * Buff);
virtual void OnStop(const PlinkBuffer * Buff);
virtual void OnEnroll(const PlinkBuffer * Buff);
virtual void OnPing(const PlinkBuffer * Buff);
virtual void OnPanelInfo(const PlinkBuffer * Buff);
virtual void OnDownloadInfo(const PlinkBuffer * Buff);
virtual void OnDownloadSettings(const PlinkBuffer * Buff);
protected:
void addPin(unsigned char* bufferToSend, int pos = 4, bool useMasterCode = false);
bool isFlagSet(unsigned char id) const { return (flags & 1<<id) != 0; }
bool isAlarmEvent() const{ return isFlagSet(7); }
bool isZoneEvent() const{ return isFlagSet(5); }
void format_SystemStatus(char* tpbuff, int buffSize);
//buffer is coppied, description and options need to be in pernament addressess (not something from stack)
bool queueCommand(const unsigned char* buffer, int bufferLen, const char* description, unsigned char expectedRepply = 0x00, const char* options = NULL);
void powerLinkEnrolled();
void processSettings();
int readMemoryMap(const unsigned char* msg, unsigned char* buffOut, int buffOutSize);
void writeMemoryMap(int iPage, int iIndex, const unsigned char* sData, int sDataLen);
bool sendBuffer(const unsigned char * data, int bufferSize);
void sendBuffer(struct PlinkBuffer * Buff);
static PmAckType calculateAckType(const unsigned char* deformattedBuffer, int bufferLen);
void startKeepAliveTimer();
void stopKeepAliveTimer();
};
/* This section specifies OS specific functions. */
/* Implementation for Windows (MSVS) is provided */
/* If you compile for other OS provide your own. */
#ifndef LOG_INFO
#define LOG_EMERG 0 /* system is unusable */
#define LOG_ALERT 1 /* action must be taken immediately */
#define LOG_CRIT 2 /* critical conditions */
#define LOG_ERR 3 /* error conditions */
#define LOG_WARNING 4 /* warning conditions */
#define LOG_NOTICE 5 /* normal but significant condition */
#define LOG_INFO 6 /* informational */
#define LOG_DEBUG 7 /* debug-level messages */
#endif
#define LOG_NO_FILTER 0 /* message always outputed */
#define DEBUG(x,...) os_debugLog(x, false,__FUNCTION__,__LINE__,__VA_ARGS__);
#define DEBUG_RAW(x,...) os_debugLog(x, true,__FUNCTION__,__LINE__,__VA_ARGS__);
int log_console_setlogmask(int mask);
bool os_serialPortInit(const char* portName);
int os_serialPortRead(void* writePos, int bytesToRead);
int os_serialPortWrite(const void* dataToWrite, int bytesToWrite);
bool os_serialPortClose();
void os_usleep(int microseconds);
int os_cfg_getPacketTimeout();
void os_debugLog(int priority, bool raw, const char *function, int line,const char *format, ...);
void os_strncat_s(char* dst, int dst_size, const char* src);
unsigned long os_getCurrentTimeSec();