123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/ringbuf.h"
- #include "esp_log.h"
- #include <stdio.h>
- #include "cc1200.h"
- #include "kiss.h"
- #include "bt_spp.h"
- #include "fcs_calc.h"
- kiss_buffer_t rx_buffer;
- kiss_buffer_t tx_buffer;
- kiss_settings_t kiss_settings;
- void static kiss_clear_tx_buffer(void)
- {
- ESP_LOGD(TNC_TAG, "TNC buffer cleared.");
- tx_buffer.index = 0;
- tx_buffer.status = FRAME_INCOMPLETE;
- }
- void static kiss_append_tx_buffer(uint8_t chr)
- {
- ESP_LOGD(TNC_TAG, "Appending %x to KISS TX buffer.", chr);
-
- if (tx_buffer.index >= tx_buffer.max_len)
- {
- ESP_LOGE(TNC_TAG, "TNC TX buffer overflow");
- kiss_clear_tx_buffer();
- }
- tx_buffer.buf[(tx_buffer.index)] = chr;
- tx_buffer.index = tx_buffer.index + 1;
- }
- void kiss_transmit(uint8_t type, uint8_t *data, uint32_t len)
- {
- uint32_t i;
-
-
- kiss_clear_tx_buffer();
- kiss_append_tx_buffer(KISS_FEND);
- kiss_append_tx_buffer(type);
-
- for (i=0; i<(len); i++)
- {
- if (data[i] == KISS_FEND)
- {
- kiss_append_tx_buffer(KISS_FESC);
- kiss_append_tx_buffer(KISS_TFEND);
- }
- else if (data[i] == KISS_FESC)
- {
- kiss_append_tx_buffer(KISS_FESC);
- kiss_append_tx_buffer(KISS_TFESC);
- }
- else
- {
- kiss_append_tx_buffer(data[i]);
- }
- }
- kiss_append_tx_buffer(KISS_FEND);
-
- kiss_settings.tx_callback(tx_buffer.buf, tx_buffer.index);
- }
- void kiss_clear_buffer(void)
- {
- ESP_LOGD(TNC_TAG, "TNC buffer cleared.");
- rx_buffer.index = 0;
- }
- void kiss_append_buffer(uint8_t chr)
- {
- ESP_LOGD(TNC_TAG, "Appending %x to TNC buffer.", chr);
-
- if (rx_buffer.index >= rx_buffer.max_len)
- {
- ESP_LOGE(TNC_TAG, "TNC buffer overflow");
- kiss_clear_buffer();
- }
- rx_buffer.buf[(rx_buffer.index)] = chr;
- rx_buffer.index = rx_buffer.index + 1;
- }
- void kiss_process_frame(void)
- {
-
- if (rx_buffer.index < 2)
- return;
- uint8_t data_byte, tnc_number;
- data_byte = rx_buffer.buf[0];
- tnc_number = (data_byte >> 4) & 0x0F;
-
- if (tnc_number == kiss_settings.tnc_number)
- kiss_settings.rx_callback(rx_buffer.buf, rx_buffer.index);
- kiss_clear_buffer();
- }
- void kiss_init(uint8_t tnc_number, kiss_cb_t tx_cb, kiss_cb_t rx_cb)
- {
-
- rx_buffer.max_len = FRAME_BUFFER_SIZE;
- tx_buffer.max_len = FRAME_BUFFER_SIZE;
-
- kiss_settings.tnc_number = tnc_number;
-
- kiss_settings.rx_callback = rx_cb;
- kiss_settings.tx_callback = tx_cb;
- }
- KISS_STATE_t kiss_state = FRAME_END;
- void kiss_receive(uint8_t* data, uint16_t len)
- {
- uint32_t i;
- uint8_t chr;
- for (i=0; i<len; i++)
- {
- chr = data[i];
- switch(kiss_state) {
- case ESC_MODE: {
- if (chr == KISS_TFEND)
- {
- kiss_append_buffer(KISS_FEND);
- kiss_state = FRAME_ASS;
- }
- else if (chr == KISS_TFESC)
- {
- kiss_append_buffer(KISS_FESC);
- kiss_state = FRAME_ASS;
- }
- else if (chr == KISS_FEND)
- {
- kiss_process_frame();
- kiss_state = FRAME_END;
- }
- else
- {
-
- kiss_append_buffer(chr);
- kiss_state = FRAME_ASS;
- }
- break;
- }
- case FRAME_END: {
- if (chr != KISS_FEND)
- {
- kiss_append_buffer(chr);
- kiss_state = FRAME_ASS;
- }
- else
- {
- kiss_state = FRAME_END;
- }
- break;
- }
- case FRAME_ASS: {
- if (chr == KISS_FESC)
- {
- kiss_state = ESC_MODE;
- }
- else if (chr == KISS_FEND)
- {
- kiss_process_frame();
- kiss_state = FRAME_END;
- }
- else
- {
- kiss_append_buffer(chr);
- kiss_state = FRAME_ASS;
- }
- break;
- default: {
- ESP_LOGE(TNC_TAG, "TNC FSM Error");
- }
- }
- }
- }
- }
|