aboutsummaryrefslogtreecommitdiff
path: root/dumblogger/Monitor.h
blob: 4b47beb247855b94cec40d7fd0f27db6ad743e1c (plain)
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
/*------------------------------------------------------------------------Monitor.h
 *
 * The pourpose of the monitor is to interpret and dispatch data The idea is to 
 * try to set up a relatively flexible interpreter for data coming in from the 
 * serial port and also commands coming from the usb port. Using a jump table which
 * is populated by other modules who register the action to be taken. 
 *
 * ------------------------------------------- current vocabulary.
 * Deal with multi line data.
 * SOD[  :]Start of multiline data
 * EOD[  :]End of multiline data
 *
 * Log file and disk commands
 * DIR[?  ] directory of the disk.
 * TYP[?  ]<fname> type file
 * DEL[ ! ]<fname> delete file
 * DDS[?  ] dates from logs.
 * DMP[ ! ] dump all logs [in order]
 * 
 * DVP[?  ] device is present?
 * DID[?  ] ssn of device?
 * DVT[?  ] timestamp for last device contact.
 * 
 * LOG[ !:] <data line> log data to current log file
 * LSV[? :] logger software version
 * LTS[ ! ] sync time with client
 * LNT[ ! ] sync time from network.
 * LTM[? :] logger rtc time
 *
 * TX2[ ! ]<text sent to radio on tx2>
 * NSC[?  ] network scan
 * NPW[ ! ]<passwd> network password
 * NJN[ ! ]<ssid> join network.
 * NST[?  ] network status.
 *
 * PFD[?!:] pachube feed
 * PKY[?!:] pachube key
 * PIP[?!:] pachube server ip
 *
 * TS[1-5][ :] temp data sent to pachube
 * TPT[ ! ]<data> test data sent to pachube slot #1
 * FAN[  :]<fan setting>
 * CHL[  :]<chiller setting>
 * STC[  :]<state#> <Description> state changes sent to pachube (slots #6 and #7)
 *
 * SSV[ ! ] save settings (read	 bom5k.set)
 * SGT[?  ] get settings  (write bom5k.set)
 * 
 * all remaining items are sent to the monitored device
 * 
 *   Copyright (c) 2012, Donald Delmar Davis, Suspect Devices
 *   All rights reserved.
 *    
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *   * Neither the name of the Suspect Devices nor the
 *   names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *    
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *   DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 *   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 *-----------------------------------------------------------------------------*/

#ifndef __MONITOR_H__
#define __MONITOR_H__ 1
#include <stdio.h>
#include <io.h>
#include <signal.h>
#include <stdint.h>
#include <stdarg.h>
#include <wirish/wirish.h>
#include <libmaple/usart.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>

//#define BOM_VERSION "BOM5K_"__DATE__
#define BOM_VERSION "BOM5K_GEN_"__DATE__ " "__TIME__

#define KEYWORDS \
"SYN" "ACK" "NAK" "SWV" "HWV" "MEM" "SSN" "HLP" "FTL" "ALT" "WRN" "INF" "DBG"\
"LOG" "STC" "DVL" "LVL" "DLG" "RST" "SHD" "ALM" "CPT" "CND" "BTN" "WBP" "ERF"\
"NOW" "TIM" "THR" "THM" "DFR" "TS1" "TS2" "TS3" "TS4" "TS5" "AMT" "CHT" "AIT"\
"FAN" "CHL" "???" "PWR" "PCT" "TSC" "CPR" "CPP" "CPI" "CPD" "FPP" "FPI" "FPD"\
"RED" "GRN" "BLU" "TSS" "NOP"

enum keywordIndex {
_SYN_,_ACK_,_NAK_,_SWV_,_HWV_,_MEM_,_SSN_,_HLP_,_FTL_,_ALT_,_WRN_,_INF_,_DBG_,
_LOG_,_STC_,_DVL_,_LVL_,_DLG_,_RST_,_SHD_,_ALM_,_CPT_,_CND_,_BTN_,_WBP_,_ERF_,
_NOW_,_TIM_,_THR_,_THM_,_DFR_,_TS1_,_TS2_,_TS3_,_TS4_,_TS5_,_AMT_,_CHT_,_AIT_,
_FAN_,_CHL_,_TMP_,_PWR_,_PCT_,_TSC_,_CPR_,_CPP_,_CPI_,_CPD_,_FPP_,_FPI_,_FPD_,
_RED_,_GRN_,_BLU_,_TSS_,_NOP_
};

// use enum to determine the size of the keyword arrays.
#define NKEYWORDS  _NOP_ + 1 
//NKEYWORDS;
#define LOGGERKEYWORDS \
"DIR"\
"DEL" "TX2" "TYP" "NSC" "NJN" "NPW" "NST" "TPT" "FMT" "PKY"\
"PFD" "PTO" "PIP" "PON" "POF" "SSV" "SGT" "TFN" "TFD" "LTM" "DVP" "DVT"\
"DTM" "DID" "LVB" "LFM" "LAM" "LFR" "LSV" 


enum loggerKeywordIndex {
_DIR_=_NOP_+1,
_DEL_,_TX2_,_TYP_,_NSC_,_NJN_,_NPW_,_NST_,_TPT_,_FMT_,_PKY_,
_PFD_,_PTO_,_PIP_,_PON_,_POF_,_SSV_,_SGT_,_TFN_,_TFD_,_LTM_,_DVP_,_DVT_,
_DTM_,_DID_,_LVB_,_LFM_,_LAM_,_LFR_,_LSV_
};

//int patchubeIndex[]={_TS1_,_TS2_,_TS3_,_TS4_,_TS5_,_FAN_,_CHL_,_STC_};

#undef NKEYWORDS
#define NKEYWORDS  _LSV_ + 1 
#define MAX_COMMAND_LINE_LENGTH 104

/*-----------------------------------------------------------------DECLARATIONS
 * 
 *----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdarg.h>
#include <io.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <stdint.h>
#include <wirish/wirish.h>

#include <sys/stat.h>
#include <errno.h>

#include <libmaple/libmaple.h>
#include <libmaple/timer.h>
#include <libmaple/usart.h>

#ifndef CONFIG_HEAP_START
extern char _lm_heap_start;
#define CONFIG_HEAP_START               ((caddr_t)&_lm_heap_start)
#endif
#ifndef CONFIG_HEAP_END
extern char _lm_heap_end;
#define CONFIG_HEAP_END                 ((caddr_t)&_lm_heap_end)
#endif


#ifndef uint8_t 
#ifndef uint8
#define uint8 unsigned char
#define uint16 unsigned short int
#endif 
#define uint8_t uint8
#define uint16_t uint16 
//#define uint32_t uint32 
#endif

extern const char *keywords;

typedef uint8_t (actionfunc)(uint8_t source);
typedef uint8_t (*actionptr)(uint8_t source);

extern actionptr actions[];
extern char actionBuffer[MAX_COMMAND_LINE_LENGTH];
//extern const char *theNak;

typedef struct __cmdbuff {
    unsigned char line[MAX_COMMAND_LINE_LENGTH];
    unsigned char * args;
    unsigned char key[4]; //unused at present. 
    unsigned char verb;
    int kwIndex;
    bool gotline;
    uint8_t len;
//    xSemaphoreHandle xGotLineSemaphore;
} cmdBuffer;


extern cmdBuffer deviceComm;
extern cmdBuffer consoleComm;

enum sourceTypes {
    CONSOLE,
    DEVICE 
};
enum inputReturnValues {
    COMMAND_INVALID=-2,
    COMMAND_ERROR=-1,
    COMMAND_OK,
    COMMAND_FORWARDED,
    COMMAND_IGNORED
};

int get_free_memory();
int lookupIndex(unsigned char *key); 
char *lookupKey(int ndx, unsigned char *keyBuffer); 
int handleDeviceInput(cmdBuffer *);
int handleConsoleInput(cmdBuffer *);
void toConsole(const char *format, ...);
void toDevice(const char *format, ...);
void registerAction(uint8_t, actionptr);
char *actBuff(const char *format, ...);
void setupKeywords(void);

#endif