mirror of https://github.com/jeelabs/esp-link.git
parent
da7a1b913f
commit
9e4cbb0dbd
@ -1,20 +1,4 @@ |
||||
#ifndef _USER_CONFIG_H_ |
||||
#define _USER_CONFIG_H_ |
||||
|
||||
#define MQTT_RECONNECT_TIMEOUT 5 // seconds
|
||||
#define MQTT_BUF_SIZE 1024 |
||||
|
||||
#define MQTT_HOST "10.0.0.220" // "mqtt.yourdomain.com" or ip "10.0.0.1"
|
||||
#define MQTT_PORT 1883 |
||||
#define MQTT_SECURITY 0 |
||||
|
||||
#define MQTT_CLIENT_ID "esp-link" // ""
|
||||
#define MQTT_USER "" |
||||
#define MQTT_PASS "" |
||||
#define MQTT_KEEPALIVE 120 // seconds
|
||||
#define MQTT_CLSESSION true |
||||
|
||||
#define PROTOCOL_NAMEv31 // MQTT version 3.1 compatible with Mosquitto v0.15
|
||||
//PROTOCOL_NAMEv311 // MQTT version 3.11 compatible with https://eclipse.org/paho/clients/testing/
|
||||
|
||||
#endif |
||||
#endif |
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,64 @@ |
||||
// Copyright 2015 by Thorsten von Eicken, see LICENSE.txt
|
||||
|
||||
#include <esp8266.h> |
||||
#include "pktbuf.h" |
||||
|
||||
void ICACHE_FLASH_ATTR |
||||
PktBuf_Print(PktBuf *buf) { |
||||
os_printf("PktBuf:"); |
||||
for (int i=-16; i<0; i++) |
||||
os_printf(" %02X", ((uint8_t*)buf)[i]); |
||||
os_printf(" %p", buf); |
||||
for (int i=0; i<16; i++) |
||||
os_printf(" %02X", ((uint8_t*)buf)[i]); |
||||
os_printf("\n"); |
||||
os_printf("PktBuf: next=%p len=0x%04x\n", |
||||
((void**)buf)[-4], ((uint16_t*)buf)[-6]); |
||||
} |
||||
|
||||
|
||||
PktBuf * ICACHE_FLASH_ATTR |
||||
PktBuf_New(uint16_t length) { |
||||
PktBuf *buf = os_zalloc(length+sizeof(PktBuf)); |
||||
buf->next = NULL; |
||||
buf->filled = 0; |
||||
//os_printf("PktBuf_New: %p l=%d->%d d=%p\n",
|
||||
// buf, length, length+sizeof(PktBuf), buf->data);
|
||||
return buf; |
||||
} |
||||
|
||||
PktBuf * ICACHE_FLASH_ATTR |
||||
PktBuf_Push(PktBuf *headBuf, PktBuf *buf) { |
||||
if (headBuf == NULL) { |
||||
//os_printf("PktBuf_Push: %p\n", buf);
|
||||
return buf; |
||||
} |
||||
PktBuf *h = headBuf; |
||||
while (h->next != NULL) h = h->next; |
||||
h->next = buf; |
||||
//os_printf("PktBuf_Push: %p->..->%p\n", headBuf, buf);
|
||||
return headBuf; |
||||
} |
||||
|
||||
PktBuf * ICACHE_FLASH_ATTR |
||||
PktBuf_Unshift(PktBuf *headBuf, PktBuf *buf) { |
||||
buf->next = headBuf; |
||||
//os_printf("PktBuf_Unshift: %p->%p\n", buf, buf->next);
|
||||
return buf; |
||||
} |
||||
|
||||
PktBuf * ICACHE_FLASH_ATTR |
||||
PktBuf_Shift(PktBuf *headBuf) { |
||||
PktBuf *buf = headBuf->next; |
||||
headBuf->next = NULL; |
||||
//os_printf("PktBuf_Shift: (%p)->%p\n", headBuf, buf);
|
||||
return buf; |
||||
} |
||||
|
||||
PktBuf * ICACHE_FLASH_ATTR |
||||
PktBuf_ShiftFree(PktBuf *headBuf) { |
||||
PktBuf *buf = headBuf->next; |
||||
//os_printf("PktBuf_ShiftFree: (%p)->%p\n", headBuf, buf);
|
||||
os_free(headBuf); |
||||
return buf; |
||||
} |
@ -0,0 +1,29 @@ |
||||
// Copyright 2015 by Thorsten von Eicken, see LICENSE.txt
|
||||
|
||||
#ifndef PKTBUF_H |
||||
#define PKTBUF_H |
||||
|
||||
typedef struct PktBuf { |
||||
struct PktBuf *next; // next buffer in chain
|
||||
uint16_t filled; // number of bytes filled in buffer
|
||||
uint8_t data[0]; // data in buffer
|
||||
} PktBuf; |
||||
|
||||
// Allocate a new packet buffer of given length
|
||||
PktBuf *PktBuf_New(uint16_t length); |
||||
|
||||
// Append a buffer to the end of a packet buffer queue, returns new head
|
||||
PktBuf *PktBuf_Push(PktBuf *headBuf, PktBuf *buf); |
||||
|
||||
// Prepend a buffer to the beginning of a packet buffer queue, return new head
|
||||
PktBuf * PktBuf_Unshift(PktBuf *headBuf, PktBuf *buf); |
||||
|
||||
// Shift first buffer off queue, returns new head (not shifted buffer!)
|
||||
PktBuf *PktBuf_Shift(PktBuf *headBuf); |
||||
|
||||
// Shift first buffer off queue, free it, return new head
|
||||
PktBuf *PktBuf_ShiftFree(PktBuf *headBuf); |
||||
|
||||
void PktBuf_Print(PktBuf *buf); |
||||
|
||||
#endif |
@ -1,86 +0,0 @@ |
||||
#include "proto.h" |
||||
|
||||
int8_t ICACHE_FLASH_ATTR
|
||||
PROTO_Init(PROTO_PARSER* parser, PROTO_PARSE_CALLBACK* completeCallback, uint8_t* buf, uint16_t bufSize) { |
||||
parser->buf = buf; |
||||
parser->bufSize = bufSize; |
||||
parser->dataLen = 0; |
||||
parser->callback = completeCallback; |
||||
parser->isEsc = 0; |
||||
return 0; |
||||
} |
||||
|
||||
int8_t ICACHE_FLASH_ATTR
|
||||
PROTO_ParseByte(PROTO_PARSER* parser, uint8_t value) { |
||||
switch (value) { |
||||
case 0x7D: |
||||
parser->isEsc = 1; |
||||
break; |
||||
|
||||
case 0x7E: |
||||
parser->dataLen = 0; |
||||
parser->isEsc = 0; |
||||
parser->isBegin = 1; |
||||
break; |
||||
|
||||
case 0x7F: |
||||
if (parser->callback != NULL) |
||||
parser->callback(); |
||||
parser->isBegin = 0; |
||||
return 0; |
||||
break; |
||||
|
||||
default: |
||||
if (parser->isBegin == 0) break; |
||||
|
||||
if (parser->isEsc) { |
||||
value ^= 0x20; |
||||
parser->isEsc = 0; |
||||
} |
||||
|
||||
if (parser->dataLen < parser->bufSize) |
||||
parser->buf[parser->dataLen++] = value; |
||||
|
||||
break; |
||||
} |
||||
return -1; |
||||
} |
||||
|
||||
int16_t ICACHE_FLASH_ATTR
|
||||
PROTO_ParseRb(RINGBUF* rb, uint8_t* bufOut, uint16_t* len, uint16_t maxBufLen) { |
||||
uint8_t c; |
||||
|
||||
PROTO_PARSER proto; |
||||
PROTO_Init(&proto, NULL, bufOut, maxBufLen); |
||||
while (RINGBUF_Get(rb, &c) == 0) { |
||||
if (PROTO_ParseByte(&proto, c) == 0) { |
||||
*len = proto.dataLen; |
||||
return 0; |
||||
} |
||||
} |
||||
return -1; |
||||
} |
||||
|
||||
int16_t ICACHE_FLASH_ATTR
|
||||
PROTO_AddRb(RINGBUF* rb, const uint8_t* packet, int16_t len) { |
||||
uint16_t i = 2; |
||||
if (RINGBUF_Put(rb, 0x7E) == -1) return -1; |
||||
while (len--) { |
||||
switch (*packet) { |
||||
case 0x7D: |
||||
case 0x7E: |
||||
case 0x7F: |
||||
if (RINGBUF_Put(rb, 0x7D) == -1) return -1; |
||||
if (RINGBUF_Put(rb, *packet++ ^ 0x20) == -1) return -1; |
||||
i += 2; |
||||
break; |
||||
default: |
||||
if (RINGBUF_Put(rb, *packet++) == -1) return -1; |
||||
i++; |
||||
break; |
||||
} |
||||
} |
||||
if (RINGBUF_Put(rb, 0x7F) == -1) return -1; |
||||
|
||||
return i; |
||||
} |
@ -1,21 +0,0 @@ |
||||
#ifndef _PROTO_H_ |
||||
#define _PROTO_H_ |
||||
#include <esp8266.h> |
||||
#include "ringbuf.h" |
||||
|
||||
typedef void (PROTO_PARSE_CALLBACK)(); |
||||
|
||||
typedef struct { |
||||
uint8_t* buf; |
||||
uint16_t bufSize; |
||||
uint16_t dataLen; |
||||
uint8_t isEsc; |
||||
uint8_t isBegin; |
||||
PROTO_PARSE_CALLBACK* callback; |
||||
} PROTO_PARSER; |
||||
|
||||
int8_t PROTO_Init(PROTO_PARSER* parser, PROTO_PARSE_CALLBACK* completeCallback, uint8_t* buf, uint16_t bufSize); |
||||
int16_t PROTO_AddRb(RINGBUF* rb, const uint8_t* packet, int16_t len); |
||||
int8_t PROTO_ParseByte(PROTO_PARSER* parser, uint8_t value); |
||||
int16_t PROTO_ParseRb(RINGBUF* rb, uint8_t* bufOut, uint16_t* len, uint16_t maxBufLen); |
||||
#endif |
@ -1,53 +0,0 @@ |
||||
/* str_queue.c
|
||||
* |
||||
* Copyright (c) 2014-2015, Tuan PM <tuanpm at live dot com> |
||||
* 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 Redis 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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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. |
||||
*/ |
||||
#include "queue.h" |
||||
|
||||
void ICACHE_FLASH_ATTR
|
||||
QUEUE_Init(QUEUE* queue, int bufferSize) { |
||||
queue->buf = (uint8_t*)os_zalloc(bufferSize); |
||||
RINGBUF_Init(&queue->rb, queue->buf, bufferSize); |
||||
} |
||||
|
||||
int32_t ICACHE_FLASH_ATTR
|
||||
QUEUE_Puts(QUEUE* queue, uint8_t* buffer, uint16_t len) { |
||||
return PROTO_AddRb(&queue->rb, buffer, len); |
||||
} |
||||
|
||||
int32_t ICACHE_FLASH_ATTR
|
||||
QUEUE_Gets(QUEUE* queue, uint8_t* buffer, uint16_t* len, uint16_t maxLen) { |
||||
return PROTO_ParseRb(&queue->rb, buffer, len, maxLen); |
||||
} |
||||
|
||||
bool ICACHE_FLASH_ATTR
|
||||
QUEUE_IsEmpty(QUEUE* queue) { |
||||
if (queue->rb.fill_cnt <= 0) |
||||
return TRUE; |
||||
return FALSE; |
||||
} |
@ -1,46 +0,0 @@ |
||||
/* str_queue.h --
|
||||
* |
||||
* Copyright (c) 2014-2015, Tuan PM <tuanpm at live dot com> |
||||
* 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 Redis 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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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 USER_QUEUE_H_ |
||||
#define USER_QUEUE_H_ |
||||
#include <esp8266.h> |
||||
#include "proto.h" |
||||
#include "ringbuf.h" |
||||
|
||||
typedef struct { |
||||
uint8_t* buf; |
||||
RINGBUF rb; |
||||
} QUEUE; |
||||
|
||||
void QUEUE_Init(QUEUE* queue, int bufferSize); |
||||
int32_t QUEUE_Puts(QUEUE* queue, uint8_t* buffer, uint16_t len); |
||||
int32_t QUEUE_Gets(QUEUE* queue, uint8_t* buffer, uint16_t* len, uint16_t maxLen); |
||||
bool QUEUE_IsEmpty(QUEUE* queue); |
||||
#endif /* USER_QUEUE_H_ */ |
@ -1,63 +0,0 @@ |
||||
#include "ringbuf.h" |
||||
|
||||
/**
|
||||
* \brief init a RINGBUF object |
||||
* \param r pointer to a RINGBUF object |
||||
* \param buf pointer to a byte array |
||||
* \param size size of buf |
||||
* \return 0 if successfull, otherwise failed |
||||
*/ |
||||
int16_t ICACHE_FLASH_ATTR
|
||||
RINGBUF_Init(RINGBUF* r, uint8_t* buf, int32_t size) { |
||||
if (r == NULL || buf == NULL || size < 2) return -1; |
||||
|
||||
r->p_o = r->p_r = r->p_w = buf; |
||||
r->fill_cnt = 0; |
||||
r->size = size; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
/**
|
||||
* \brief put a character into ring buffer |
||||
* \param r pointer to a ringbuf object |
||||
* \param c character to be put |
||||
* \return 0 if successfull, otherwise failed |
||||
*/ |
||||
int16_t ICACHE_FLASH_ATTR
|
||||
RINGBUF_Put(RINGBUF* r, uint8_t c) { |
||||
if (r->fill_cnt >= r->size)return -1; // ring buffer is full, this should be atomic operation
|
||||
|
||||
|
||||
r->fill_cnt++; // increase filled slots count, this should be atomic operation
|
||||
|
||||
|
||||
*r->p_w++ = c; // put character into buffer
|
||||
|
||||
if (r->p_w >= r->p_o + r->size) // rollback if write pointer go pass
|
||||
r->p_w = r->p_o; // the physical boundary
|
||||
|
||||
return 0; |
||||
} |
||||
|
||||
/**
|
||||
* \brief get a character from ring buffer |
||||
* \param r pointer to a ringbuf object |
||||
* \param c read character |
||||
* \return 0 if successfull, otherwise failed |
||||
*/ |
||||
int16_t ICACHE_FLASH_ATTR
|
||||
RINGBUF_Get(RINGBUF* r, uint8_t* c) { |
||||
if (r->fill_cnt <= 0)return -1; // ring buffer is empty, this should be atomic operation
|
||||
|
||||
|
||||
r->fill_cnt--; // decrease filled slots count
|
||||
|
||||
|
||||
*c = *r->p_r++; // get the character out
|
||||
|
||||
if (r->p_r >= r->p_o + r->size) // rollback if write pointer go pass
|
||||
r->p_r = r->p_o; // the physical boundary
|
||||
|
||||
return 0; |
||||
} |
@ -1,17 +0,0 @@ |
||||
#ifndef _RING_BUF_H_ |
||||
#define _RING_BUF_H_ |
||||
|
||||
#include <esp8266.h> |
||||
|
||||
typedef struct { |
||||
uint8_t* p_o; /**< Original pointer */ |
||||
uint8_t* volatile p_r; /**< Read pointer */ |
||||
uint8_t* volatile p_w; /**< Write pointer */ |
||||
volatile int32_t fill_cnt; /**< Number of filled slots */ |
||||
int32_t size; /**< Buffer size */ |
||||
} RINGBUF; |
||||
|
||||
int16_t RINGBUF_Init(RINGBUF* r, uint8_t* buf, int32_t size); |
||||
int16_t RINGBUF_Put(RINGBUF* r, uint8_t c); |
||||
int16_t RINGBUF_Get(RINGBUF* r, uint8_t* c); |
||||
#endif |
Loading…
Reference in new issue