BlinkenArea - GitList
Repositories
Blog
Wiki
BlinkenLib
Code
Commits
Branches
Tags
Search
Tree:
c9469b6
Branches
Tags
master
v0.1
v0.2
v0.3
v0.3.1
v0.4
v0.4.1
v0.5
v0.5.1
v0.5.2
v0.5.3
v0.5.4
v0.5.5
v0.6.0
v0.6.1
v0.6.2
v0.6.3
v0.6.4
v0.6.5
v0.6.6
v0.6.7
v0.6.8
v0.6.9
v0.7.0
v0.7.1
v0.7.10
v0.7.2
v0.7.3
v0.7.4
v0.7.5
v0.7.6
v0.7.7
v0.7.8
v0.7.9
v0.8.0
v0.8.1
BlinkenLib
src
BlinkenProto.c
restructure directories
Stefan Schuermans
commited
c9469b6
at 2019-05-30 18:28:57
BlinkenProto.c
Blame
History
Raw
/* BlinkenLib Copyright 2004-2014 Stefan Schuermans <stefan@schuermans.info> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #include <string.h> #ifdef WIN32 #include <winsock2.h> #include <windows.h> typedef WORD uint16_t; typedef DWORD uint32_t; #else #include <stdint.h> #include <netinet/in.h> #endif #include <BlinkenLib/BlinkenProto.h> #include "BlinkenProtoIntern.h" int BlinkenProtoMakePacket(etBlinkenProto proto, etBlinkenPacket packet, char *pData, int maxLength) // returns length or -1 on error { uint32_t magic; char *body; int len; magic = 0; len = -1; // info packet processing: body identical for all protocols switch (packet) { case BlinkenPacketStreamEnd: body = "END\0\0\0\0\0"; len = 8; break; case BlinkenPacketNone: case BlinkenPacketFrame: case BlinkenPacketRequest: case BlinkenPacketEndRequest: // keep compiler happy break; } switch (proto) { case BlinkenProtoNone: break; case BlinkenProtoBlp: switch (packet) { case BlinkenPacketFrame: magic = BlinkenProtoBlpMagic; body = "\0\0\0\0\0\0\0\0"; // size 0x0 len = 8; break; case BlinkenPacketRequest: magic = BlinkenProtoBlpReqMagic; body = "REFRESH"; len = 7; break; case BlinkenPacketEndRequest: magic = BlinkenProtoBlpReqMagic; body = "CLOSE"; len = 5; break; default: // might be info packet, set only magic magic = BlinkenProtoBlpInfoMagic; break; } break; case BlinkenProtoEblp: switch (packet) { case BlinkenPacketFrame: magic = BlinkenProtoEblpMagic; body = "\0\0\0\0\0\0\0\0"; // size 0x0 len = 8; break; case BlinkenPacketRequest: magic = BlinkenProtoBlpReqMagic; body = "REFRESH256"; len = 10; break; case BlinkenPacketEndRequest: magic = BlinkenProtoBlpReqMagic; body = "CLOSE256"; len = 8; break; default: // might be info packet, set only magic magic = BlinkenProtoEblpInfoMagic; break; } break; case BlinkenProtoMcuf: switch (packet) { case BlinkenPacketFrame: magic = BlinkenProtoMcufMagic; body = "\0\0\0\0\0\0\0\0"; // size 0x0 len = 8; break; case BlinkenPacketRequest: magic = BlinkenProtoMcufReqMagic; body = "\0\0\0\0\0\0\0\0"; len = 8; break; case BlinkenPacketEndRequest: magic = BlinkenProtoMcufEndReqMagic; body = "\0\0\0\0\0\0\0\0"; len = 8; break; default: // might be info packet, set only magic magic = BlinkenProtoMcufInfoMagic; break; } break; } // magic not set or body not set -> error if (magic == 0 || len < 0) return -1; // fill buffer if (maxLength < (int)sizeof(magic)) return -1; *(uint32_t *)pData = htonl(magic); if (maxLength < (int)sizeof(magic) + len) { memcpy(pData + sizeof(magic), body, maxLength - sizeof(magic)); return -1; } memcpy(pData + sizeof(magic), body, len); return sizeof(magic) + len; } void BlinkenProtoDetectPacket(const char *pData, int length, etBlinkenProto * pProto, etBlinkenPacket * pPacket) // returns protocol in *pProto if pProto not NULL // returns packet type in *pPacket if pPacket not NULL { uint32_t magic; int info; // get magic if (length < (int)sizeof(magic)) { if (pProto) *pProto = BlinkenProtoNone; if (pPacket) *pPacket = BlinkenPacketNone; return; } magic = ntohl(*(uint32_t *)pData); info = 0; switch (magic) { case BlinkenProtoBlpMagic: if (length >= (int)sizeof(stBlinkenProtoBlpHdr)) { if (pProto) *pProto = BlinkenProtoBlp; if (pPacket) *pPacket = BlinkenPacketFrame; return; } break; case BlinkenProtoBlpReqMagic: if (length >= (int)sizeof(magic) + 10 && !memcmp(pData + sizeof(magic), "REFRESH256", 10)) { if (pProto) *pProto = BlinkenProtoEblp; if (pPacket) *pPacket = BlinkenPacketRequest; return; } if (length >= (int)sizeof(magic) + 8 && !memcmp(pData + sizeof(magic), "CLOSE256", 8)) { if (pProto) *pProto = BlinkenProtoEblp; if (pPacket) *pPacket = BlinkenPacketEndRequest; return; } if (length >= (int)sizeof(magic) + 7 && !memcmp(pData + sizeof(magic), "REFRESH", 7)) { if (pProto) *pProto = BlinkenProtoBlp; if (pPacket) *pPacket = BlinkenPacketRequest; return; } if (length >= (int)sizeof(magic) + 5 && !memcmp(pData + sizeof(magic), "CLOSE", 5)) { if (pProto) *pProto = BlinkenProtoBlp; if (pPacket) *pPacket = BlinkenPacketEndRequest; return; } break; case BlinkenProtoBlpInfoMagic: if (length >= (int)sizeof(magic) + 8) { if (pProto) *pProto = BlinkenProtoBlp; info = 1; // further processing of info packet below } break; case BlinkenProtoEblpMagic: if (length >= (int)sizeof(stBlinkenProtoEblpHdr)) { if (pProto) *pProto = BlinkenProtoEblp; if (pPacket) *pPacket = BlinkenPacketFrame; return; } break; case BlinkenProtoEblpInfoMagic: if (length >= (int)sizeof(magic) + 8) { if (pProto) *pProto = BlinkenProtoEblp; info = 1; // further processing of info packet below } break; case BlinkenProtoMcufMagic: if (length >= (int)sizeof(stBlinkenProtoMcufHdr)) { if (pProto) *pProto = BlinkenProtoMcuf; if (pPacket) *pPacket = BlinkenPacketFrame; return; } break; case BlinkenProtoMcufReqMagic: if (pProto) *pProto = BlinkenProtoMcuf; if (pPacket) *pPacket = BlinkenPacketRequest; return; case BlinkenProtoMcufEndReqMagic: if (pProto) *pProto = BlinkenProtoMcuf; if (pPacket) *pPacket = BlinkenPacketEndRequest; return; case BlinkenProtoMcufInfoMagic: if (length >= (int)sizeof(magic) + 8) { if (pProto) *pProto = BlinkenProtoMcuf; info = 1; // further processing of info packet below } break; } // info packet processing: body identical for all protocols if (info) { if (!memcmp(pData + sizeof(magic), "END\0\0\0\0\0", 8)) { if (pPacket) *pPacket = BlinkenPacketStreamEnd; return; } // unknown info packet if (pPacket) *pPacket = BlinkenPacketNone; return; } // unknown packet if (pProto) *pProto = BlinkenProtoNone; if (pPacket) *pPacket = BlinkenPacketNone; }