added support for creating/detecting blinken protocol control frames
Stefan Schuermans

Stefan Schuermans commited on 2011-11-15 23:35:20
Showing 10 changed files, with 373 additions and 40 deletions.

... ...
@@ -21,6 +21,8 @@ typedef DWORD uint32_t;
21 21
 #include <BlinkenLib/BlinkenFrame.h>
22 22
 #include <BlinkenLib/Tools.h>
23 23
 
24
+#include <BlinkenLib/BlinkenProtoIntern.h>
25
+
24 26
 struct sBlinkenFrame {
25 27
   int height;
26 28
   int width;
... ...
@@ -30,30 +32,6 @@ struct sBlinkenFrame {
30 32
   unsigned char **ppData;
31 33
 };
32 34
 
33
-// blinken protocol headers
34
-typedef struct sBlinkenProtoBlpHdr {
35
-  uint32_t magic;
36
-  uint32_t frameNo;
37
-  uint16_t width;
38
-  uint16_t height;
39
-} stBlinkenProtoBlpHdr;
40
-#define BlinkenProtoBlpMagic 0xDEADBEEF
41
-typedef struct sBlinkenProtoEblpHdr {
42
-  uint32_t magic;
43
-  uint32_t frameNo;
44
-  uint16_t width;
45
-  uint16_t height;
46
-} stBlinkenProtoEblpHdr;
47
-#define BlinkenProtoEblpMagic 0xFEEDBEEF
48
-typedef struct sBlinkenProtoMcufHdr {
49
-  uint32_t magic;
50
-  uint16_t height;
51
-  uint16_t width;
52
-  uint16_t channels;
53
-  uint16_t maxval;
54
-} stBlinkenProtoMcufHdr;
55
-#define BlinkenProtoMcufMagic 0x23542666
56
-
57 35
 stBlinkenFrame *BlinkenFrameNew(int height, int width, int channels,
58 36
                                 int maxval, int duration)
59 37
 {
... ...
@@ -6,16 +6,14 @@
6 6
 #ifndef INC_BlinkenLib_BlinkenFrame
7 7
 #define INC_BlinkenLib_BlinkenFrame
8 8
 
9
+#include <BlinkenLib/BlinkenProto.h>
10
+
9 11
 #ifdef __cplusplus
10 12
 extern "C" {
11 13
 #endif
12 14
 
13 15
 typedef struct sBlinkenFrame stBlinkenFrame;
14 16
 
15
-typedef enum eBlinkenProto { BlinkenProtoNone, BlinkenProtoBlp,
16
-  BlinkenProtoEblp, BlinkenProtoMcuf
17
-} etBlinkenProto;
18
-
19 17
 stBlinkenFrame *BlinkenFrameNew(int height, int width, int channels,
20 18
                                 int maxval, int duration);
21 19
 
... ...
@@ -8,7 +8,8 @@
8 8
 
9 9
 #include <BlinkenLib/config.h>
10 10
 #include <BlinkenLib/BlinkenColorizer.h>
11
-#include <BlinkenLib/BlinkenMovie.h>
12 11
 #include <BlinkenLib/BlinkenFrame.h>
12
+#include <BlinkenLib/BlinkenMovie.h>
13
+#include <BlinkenLib/BlinkenProto.h>
13 14
 
14 15
 #endif // #ifndef INC_BlinkenLib_BlinkenLib
... ...
@@ -0,0 +1,265 @@
1
+/* BlinkenLib
2
+   Copyright 2004-2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <string.h>
7
+#ifdef WIN32
8
+#include <winsock2.h>
9
+#include <windows.h>
10
+typedef WORD uint16_t;
11
+typedef DWORD uint32_t;
12
+#else
13
+#include <stdint.h>
14
+#include <netinet/in.h>
15
+#endif
16
+
17
+#include <BlinkenLib/BlinkenProto.h>
18
+
19
+#include <BlinkenLib/BlinkenProtoIntern.h>
20
+
21
+int BlinkenProtoMakePacket(etBlinkenProto proto, etBlinkenPacket packet,
22
+                           char *pData, int maxLength)
23
+// returns length or -1 on error
24
+{
25
+  uint32_t magic;
26
+  char *body;
27
+  int len;
28
+
29
+  magic = 0;
30
+  len = -1;
31
+
32
+  // info packet processing: body identical for all protocols
33
+  switch (packet) {
34
+    case BlinkenPacketStreamEnd:
35
+      body = "END\0\0\0\0\0";
36
+      len = 8;
37
+      break;
38
+    case BlinkenPacketNone:
39
+    case BlinkenPacketFrame:
40
+    case BlinkenPacketRequest:
41
+    case BlinkenPacketEndRequest:
42
+      // keep compiler happy
43
+      break;
44
+  }
45
+
46
+  switch (proto) {
47
+  case BlinkenProtoNone:
48
+    break;
49
+  case BlinkenProtoBlp:
50
+    switch (packet) {
51
+      case BlinkenPacketFrame:
52
+        magic = BlinkenProtoBlpMagic;
53
+        body = "\0\0\0\0\0\0\0\0"; // size 0x0
54
+        len = 8;
55
+        break;
56
+      case BlinkenPacketRequest:
57
+        magic = BlinkenProtoBlpReqMagic;
58
+        body = "REFRESH";
59
+        len = 7;
60
+        break;
61
+      case BlinkenPacketEndRequest:
62
+        magic = BlinkenProtoBlpReqMagic;
63
+        body = "CLOSE";
64
+        len = 5;
65
+        break;
66
+      default: // might be info packet, set only magic
67
+        magic = BlinkenProtoBlpInfoMagic;
68
+        break;
69
+    }
70
+    break;
71
+  case BlinkenProtoEblp:
72
+    switch (packet) {
73
+      case BlinkenPacketFrame:
74
+        magic = BlinkenProtoEblpMagic;
75
+        body = "\0\0\0\0\0\0\0\0"; // size 0x0
76
+        len = 8;
77
+        break;
78
+      case BlinkenPacketRequest:
79
+        magic = BlinkenProtoBlpReqMagic;
80
+        body = "REFRESH256";
81
+        len = 10;
82
+        break;
83
+      case BlinkenPacketEndRequest:
84
+        magic = BlinkenProtoBlpReqMagic;
85
+        body = "CLOSE256";
86
+        len = 8;
87
+        break;
88
+      default: // might be info packet, set only magic
89
+        magic = BlinkenProtoEblpInfoMagic;
90
+        break;
91
+    }
92
+    break;
93
+  case BlinkenProtoMcuf:
94
+    switch (packet) {
95
+      case BlinkenPacketFrame:
96
+        magic = BlinkenProtoMcufMagic;
97
+        body = "\0\0\0\0\0\0\0\0"; // size 0x0
98
+        len = 8;
99
+        break;
100
+      case BlinkenPacketRequest:
101
+        magic = BlinkenProtoMcufReqMagic;
102
+        body = "\0\0\0\0\0\0\0\0";
103
+        len = 8;
104
+        break;
105
+      case BlinkenPacketEndRequest:
106
+        magic = BlinkenProtoMcufEndReqMagic;
107
+        body = "\0\0\0\0\0\0\0\0";
108
+        len = 8;
109
+        break;
110
+      default: // might be info packet, set only magic
111
+        magic = BlinkenProtoMcufInfoMagic;
112
+        break;
113
+    }
114
+    break;
115
+  }
116
+
117
+  // magic not set or body not set -> error
118
+  if (magic == 0 || len < 0)
119
+    return -1;
120
+
121
+  // fill buffer
122
+  if (maxLength < (int)sizeof(magic))
123
+    return -1;
124
+  *(uint32_t *)pData = htonl(magic);
125
+  if (maxLength < (int)sizeof(magic) + len) {
126
+    memcpy(pData + sizeof(magic), body, maxLength - sizeof(magic));
127
+    return -1;
128
+  }
129
+  memcpy(pData + sizeof(magic), body, len);
130
+  return sizeof(magic) + len;
131
+}
132
+
133
+void BlinkenProtoDetectPacket(const char *pData, int length,
134
+                              etBlinkenProto * pProto,
135
+                              etBlinkenPacket * pPacket)
136
+// returns protocol in *pProto if pProto not NULL
137
+// returns packet type in *pPacket if pPacket not NULL
138
+{
139
+  uint32_t magic;
140
+  int info;
141
+
142
+  // get magic
143
+  if (length < (int)sizeof(magic)) {
144
+    if (pProto)
145
+      *pProto = BlinkenProtoNone;
146
+    if (pPacket)
147
+      *pPacket = BlinkenPacketNone;
148
+    return;
149
+  }
150
+  magic = ntohl(*(uint32_t *)pData);
151
+
152
+  info = 0;
153
+  switch (magic) {
154
+  case BlinkenProtoBlpMagic:
155
+    if (length >= (int)sizeof(stBlinkenProtoBlpHdr)) {
156
+      if (pProto)
157
+        *pProto = BlinkenProtoBlp;
158
+      if (pPacket)
159
+        *pPacket = BlinkenPacketFrame;
160
+      return;
161
+    }
162
+    break;
163
+  case BlinkenProtoBlpReqMagic:
164
+    if (length >= (int)sizeof(magic) + 10 && !memcmp(pData + sizeof(magic), "REFRESH256", 10)) {
165
+      if (pProto)
166
+        *pProto = BlinkenProtoEblp;
167
+      if (pPacket)
168
+        *pPacket = BlinkenPacketRequest;
169
+      return;
170
+    }
171
+    if (length >= (int)sizeof(magic) + 8 && !memcmp(pData + sizeof(magic), "CLOSE256", 8)) {
172
+      if (pProto)
173
+        *pProto = BlinkenProtoEblp;
174
+      if (pPacket)
175
+        *pPacket = BlinkenPacketEndRequest;
176
+      return;
177
+    }
178
+    if (length >= (int)sizeof(magic) + 7 && !memcmp(pData + sizeof(magic), "REFRESH", 7)) {
179
+      if (pProto)
180
+        *pProto = BlinkenProtoBlp;
181
+      if (pPacket)
182
+        *pPacket = BlinkenPacketRequest;
183
+      return;
184
+    }
185
+    if (length >= (int)sizeof(magic) + 5 && !memcmp(pData + sizeof(magic), "CLOSE", 5)) {
186
+      if (pProto)
187
+        *pProto = BlinkenProtoBlp;
188
+      if (pPacket)
189
+        *pPacket = BlinkenPacketEndRequest;
190
+      return;
191
+    }
192
+    break;
193
+  case BlinkenProtoBlpInfoMagic:
194
+    if (length >= (int)sizeof(magic) + 8) {
195
+      if (pProto)
196
+        *pProto = BlinkenProtoBlp;
197
+      info = 1; // further processing of info packet below
198
+    }
199
+    break;
200
+  case BlinkenProtoEblpMagic:
201
+    if (length >= (int)sizeof(stBlinkenProtoEblpHdr)) {
202
+      if (pProto)
203
+        *pProto = BlinkenProtoEblp;
204
+      if (pPacket)
205
+        *pPacket = BlinkenPacketFrame;
206
+      return;
207
+    }
208
+    break;
209
+  case BlinkenProtoEblpInfoMagic:
210
+    if (length >= (int)sizeof(magic) + 8) {
211
+      if (pProto)
212
+        *pProto = BlinkenProtoEblp;
213
+      info = 1; // further processing of info packet below
214
+    }
215
+    break;
216
+  case BlinkenProtoMcufMagic:
217
+    if (length >= (int)sizeof(stBlinkenProtoMcufHdr)) {
218
+      if (pProto)
219
+        *pProto = BlinkenProtoMcuf;
220
+      if (pPacket)
221
+        *pPacket = BlinkenPacketFrame;
222
+      return;
223
+    }
224
+    break;
225
+  case BlinkenProtoMcufReqMagic:
226
+    if (pProto)
227
+      *pProto = BlinkenProtoMcuf;
228
+    if (pPacket)
229
+      *pPacket = BlinkenPacketRequest;
230
+    return;
231
+  case BlinkenProtoMcufEndReqMagic:
232
+    if (pProto)
233
+      *pProto = BlinkenProtoMcuf;
234
+    if (pPacket)
235
+      *pPacket = BlinkenPacketEndRequest;
236
+    return;
237
+  case BlinkenProtoMcufInfoMagic:
238
+    if (length >= sizeof(magic) + 8) {
239
+      if (pProto)
240
+        *pProto = BlinkenProtoMcuf;
241
+      info = 1; // further processing of info packet below
242
+    }
243
+    break;
244
+  }
245
+
246
+  // info packet processing: body identical for all protocols
247
+  if (info) {
248
+    if (!memcmp(pData + sizeof(magic), "END\0\0\0\0\0", 8)) {
249
+      if (pPacket)
250
+        *pPacket = BlinkenPacketStreamEnd;
251
+      return;
252
+    }
253
+    // unknown info packet
254
+    if (pPacket)
255
+      *pPacket = BlinkenPacketNone;
256
+    return;
257
+  }
258
+
259
+  // unknown packet
260
+  if (pProto)
261
+    *pProto = BlinkenProtoNone;
262
+  if (pPacket)
263
+    *pPacket = BlinkenPacketNone;
264
+}
265
+
... ...
@@ -0,0 +1,35 @@
1
+/* BlinkenLib
2
+   Copyright 2004-2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef INC_BlinkenLib_BlinkenProto
7
+#define INC_BlinkenLib_BlinkenProto
8
+
9
+#ifdef __cplusplus
10
+extern "C" {
11
+#endif
12
+
13
+typedef enum eBlinkenProto { BlinkenProtoNone, BlinkenProtoBlp,
14
+  BlinkenProtoEblp, BlinkenProtoMcuf
15
+} etBlinkenProto;
16
+
17
+typedef enum eBlinkenPacket { BlinkenPacketNone, BlinkenPacketFrame,
18
+  BlinkenPacketRequest, BlinkenPacketEndRequest, BlinkenPacketStreamEnd
19
+} etBlinkenPacket;
20
+
21
+int BlinkenProtoMakePacket(etBlinkenProto proto, etBlinkenPacket packet,
22
+                           char *pData, int maxLength);
23
+// returns length or -1 on error
24
+
25
+void BlinkenProtoDetectPacket(const char *pData, int length,
26
+                              etBlinkenProto * pProto,
27
+                              etBlinkenPacket * pPacket);
28
+// returns protocol in *pProto if pProto not NULL
29
+// returns packet type in *pPacket if pPacket not NULL
30
+
31
+#ifdef __cplusplus
32
+} // extern "C"
33
+#endif
34
+
35
+#endif // #ifndef INC_BlinkenLib_BlinkenProto
... ...
@@ -0,0 +1,47 @@
1
+/* BlinkenLib
2
+   Copyright 2004-2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef INC_BlinkenLib_BlinkenProtoIntern
7
+#define INC_BlinkenLib_BlinkenProtoIntern
8
+
9
+#ifdef WIN32
10
+#include <windows.h>
11
+typedef WORD uint16_t;
12
+typedef DWORD uint32_t;
13
+#else
14
+#include <stdint.h>
15
+#endif
16
+
17
+// blinken protocol headers
18
+typedef struct sBlinkenProtoBlpHdr {
19
+  uint32_t magic;
20
+  uint32_t frameNo;
21
+  uint16_t width;
22
+  uint16_t height;
23
+} stBlinkenProtoBlpHdr;
24
+#define BlinkenProtoBlpMagic 0xDEADBEEF
25
+#define BlinkenProtoBlpReqMagic 0xDEADBECD
26
+#define BlinkenProtoBlpInfoMagic 0xDEADBE23
27
+typedef struct sBlinkenProtoEblpHdr {
28
+  uint32_t magic;
29
+  uint32_t frameNo;
30
+  uint16_t width;
31
+  uint16_t height;
32
+} stBlinkenProtoEblpHdr;
33
+#define BlinkenProtoEblpMagic 0xFEEDBEEF
34
+#define BlinkenProtoEblpInfoMagic 0xFEEDBE23
35
+typedef struct sBlinkenProtoMcufHdr {
36
+  uint32_t magic;
37
+  uint16_t height;
38
+  uint16_t width;
39
+  uint16_t channels;
40
+  uint16_t maxval;
41
+} stBlinkenProtoMcufHdr;
42
+#define BlinkenProtoMcufMagic 0x23542666
43
+#define BlinkenProtoMcufReqMagic 0x42424242
44
+#define BlinkenProtoMcufEndReqMagic 0x42424243
45
+#define BlinkenProtoMcufInfoMagic 0x42424223
46
+
47
+#endif // #ifndef INC_BlinkenLib_BlinkenProtoIntern
... ...
@@ -33,7 +33,8 @@ BLINKEN_MNG_O=BlinkenMng.o
33 33
 LMNG=-lmng -lz
34 34
 endif
35 35
 
36
-LIB_OBJS=BlinkenColorizer.o BlinkenFrame.o BlinkenMovie.o $(BLINKEN_MNG_O) Tools.o
36
+LIB_OBJS=BlinkenColorizer.o BlinkenFrame.o BlinkenMovie.o $(BLINKEN_MNG_O) \
37
+         BlinkenProto.o Tools.o
37 38
 
38 39
 .phony: all clean
39 40
 
... ...
@@ -45,13 +46,16 @@ config.h: ../config/config.h
45 46
 BlinkenColorizer.o: BlinkenColorizer.c BlinkenColorizer.h Tools.h
46 47
 	$(CC) $(CFLAGS) -c -o $@ $<
47 48
 
48
-BlinkenFrame.o: BlinkenFrame.c BlinkenConstants.h BlinkenColorizer.h BlinkenFrame.h BlinkenColorizer.h Tools.h
49
+BlinkenProto.o: BlinkenProto.c BlinkenProto.h BlinkenProtoIntern.h
49 50
 	$(CC) $(CFLAGS) -c -o $@ $<
50 51
 
51
-BlinkenMovie.o: BlinkenMovie.c BlinkenConstants.h BlinkenColorizer.h BlinkenFrame.h BlinkenMovie.h $(BLINKEN_MNG_H) Tools.h config.h
52
+BlinkenFrame.o: BlinkenFrame.c BlinkenConstants.h BlinkenColorizer.h BlinkenFrame.h BlinkenProto.h BlinkenProtoIntern.h Tools.h
52 53
 	$(CC) $(CFLAGS) -c -o $@ $<
53 54
 
54
-BlinkenMng.o: BlinkenMng.c BlinkenConstants.h BlinkenColorizer.h BlinkenFrame.h BlinkenMovie.h Tools.h config.h
55
+BlinkenMovie.o: BlinkenMovie.c BlinkenConstants.h BlinkenColorizer.h BlinkenFrame.h BlinkenProto.h BlinkenMovie.h $(BLINKEN_MNG_H) Tools.h config.h
56
+	$(CC) $(CFLAGS) -c -o $@ $<
57
+
58
+BlinkenMng.o: BlinkenMng.c BlinkenConstants.h BlinkenColorizer.h BlinkenFrame.h BlinkenProto.h BlinkenMovie.h Tools.h config.h
55 59
 	$(CC) $(CFLAGS) -c -o $@ $<
56 60
 
57 61
 Tools.o: Tools.c Tools.h
... ...
@@ -72,25 +76,25 @@ libBlinkenLib.$(SHLIBEXT): libBlinkenLib.$(SHLIBEXT).$(VERSION_MAJOR)
72 76
 	rm -f $@
73 77
 	ln -s $< $@
74 78
 
75
-BlinkenConv.o: BlinkenConv.c BlinkenLib.h config.h BlinkenColorizer.h BlinkenFrame.h BlinkenMovie.h
79
+BlinkenConv.o: BlinkenConv.c BlinkenLib.h config.h BlinkenColorizer.h BlinkenProto.h BlinkenFrame.h BlinkenMovie.h
76 80
 	$(CC) $(CFLAGS) -c -o $@ $<
77 81
 
78 82
 BlinkenConv: BlinkenConv.o libBlinkenLib.$(SHLIBEXT)
79 83
 	$(CC) $(LFLAGS) -o $@ $< -lBlinkenLib
80 84
 
81
-BlinkenSend.o: BlinkenSend.c BlinkenLib.h config.h BlinkenColorizer.h BlinkenFrame.h BlinkenMovie.h
85
+BlinkenSend.o: BlinkenSend.c BlinkenLib.h config.h BlinkenColorizer.h BlinkenProto.h BlinkenFrame.h BlinkenMovie.h
82 86
 	$(CC) $(CFLAGS) -c -o $@ $<
83 87
 
84 88
 BlinkenSend: BlinkenSend.o libBlinkenLib.$(SHLIBEXT)
85 89
 	$(CC) $(LFLAGS) -o $@ $< -lBlinkenLib
86 90
 
87
-BlinkenRecv.o: BlinkenRecv.c BlinkenLib.h config.h BlinkenColorizer.h BlinkenFrame.h BlinkenMovie.h
91
+BlinkenRecv.o: BlinkenRecv.c BlinkenLib.h config.h BlinkenColorizer.h BlinkenProto.h BlinkenFrame.h BlinkenMovie.h
88 92
 	$(CC) $(CFLAGS) -c -o $@ $<
89 93
 
90 94
 BlinkenRecv: BlinkenRecv.o libBlinkenLib.$(SHLIBEXT)
91 95
 	$(CC) $(LFLAGS) -o $@ $< -lBlinkenLib
92 96
 
93
-BlinkenOutput.o: BlinkenOutput.c BlinkenLib.h config.h BlinkenColorizer.h BlinkenFrame.h BlinkenMovie.h
97
+BlinkenOutput.o: BlinkenOutput.c BlinkenLib.h config.h BlinkenColorizer.h BlinkenProto.h BlinkenFrame.h BlinkenMovie.h
94 98
 	$(CC) $(CFLAGS) -c -o $@ $<
95 99
 
96 100
 BlinkenOutput: BlinkenOutput.o libBlinkenLib.$(SHLIBEXT)
... ...
@@ -1,3 +1,7 @@
1
+0.7.0 2011-11-15
2
+----------------
3
+added support for creating/detecting blinken protocol control frames
4
+
1 5
 0.6.9 2011-11-14
2 6
 ----------------
3 7
 fixed threshold in BLP network packet generation
... ...
@@ -28,7 +28,8 @@ else
28 28
   endif
29 29
 endif
30 30
 
31
-HEADERS=BlinkenLib.h BlinkenMovie.h BlinkenFrame.h BlinkenColorizer.h config.h
31
+HEADERS=BlinkenLib.h BlinkenMovie.h BlinkenFrame.h BlinkenColorizer.h \
32
+        BlinkenProto.h config.h
32 33
 LIBS=libBlinkenLib.a libBlinkenLib.$(SHLIBEXT).$(VERSION)
33 34
 EXECS=BlinkenConv BlinkenSend BlinkenRecv BlinkenOutput
34 35
 
... ...
@@ -1,5 +1,5 @@
1 1
 VERSION_MAJOR=0
2
-VERSION_MINOR=6
3
-VERSION_REVISION=9
2
+VERSION_MINOR=7
3
+VERSION_REVISION=0
4 4
 VERSION=$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_REVISION)
5 5
 
6 6