123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- /*
- * tnc_kiss.c
- *
- * Created on: Feb 26, 2019
- * Author: curiousmuch
- */
- // basic set of functions at this moment which decodes KISS
- #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;
- tnc_settings_t tnc_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);
- // ERROR - Packet Buffer Overflow
- 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;
- // TODO: Cancel TX to BLE TX Buffer if overflow is detected
- kiss_clear_tx_buffer();
- kiss_append_tx_buffer(KISS_FEND);
- kiss_append_tx_buffer(type);
- // cycle
- 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);
- bt_spp_tx(tx_buffer.buf, tx_buffer.index);
- // send via BLE or feed to TX buffer
- }
- 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);
- // ERROR - Packet Buffer Overflow
- 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;
- }
- extern RingbufHandle_t radio_tx_buf;
- // Frame Format
- // [ Command Byte ][ Data ]
- void kiss_process_frame(void)
- {
- uint8_t data_byte, tnc_number;
- data_byte = rx_buffer.buf[0];
- tnc_number = (data_byte >> 4) & 0x0F; // TODO: Have KISS code respect TNC number
- switch( data_byte ) {
- case KISS_DATAFRAME: {
- ESP_LOGI(TNC_TAG, "Received Data Frame - Length %d", (rx_buffer.index));
- // unblock AX.25 code to process code
- uint32_t fcs = fcs_calc(&rx_buffer.buf[1], rx_buffer.index-1);
- rx_buffer.buf[rx_buffer.index] = fcs & 0xFF;
- rx_buffer.buf[rx_buffer.index+1] = fcs>>8 & 0xFF;
- xRingbufferSend(radio_tx_buf, &rx_buffer.buf[1], ((rx_buffer.index+1)*sizeof(uint8_t)), 10/portTICK_PERIOD_MS);
- ESP_LOG_BUFFER_HEXDUMP(TNC_TAG, &rx_buffer.buf[1], rx_buffer.index+1, ESP_LOG_INFO);
- //cc1200_radio_APRSTXPacket(rx_buffer.buf, (rx_buffer.index+2), tnc_settings.tx_delay, tnc_settings.tx_tail);
- break;
- }
- case KISS_CMD_TXDELAY: {
- ESP_LOGI(TNC_TAG, "Updated TX Delay");
- tnc_settings.tx_delay = rx_buffer.buf[1];
- break;
- }
- case KISS_CMD_P: {
- ESP_LOGI(TNC_TAG, "Update Persistence");
- tnc_settings.persistence = rx_buffer.buf[1];
- break;
- }
- case KISS_CMD_SLOTTIME: {
- ESP_LOGI(TNC_TAG, "Updated Slottime");
- tnc_settings.slot_time = rx_buffer.buf[1];
- break;
- }
- case KISS_CMD_TXTAIL: {
- ESP_LOGI(TNC_TAG, "Updated TX Tail");
- tnc_settings.tx_tail= rx_buffer.buf[1];
- break;
- }
- case KISS_CMD_FULLDUPLEX: {
- ESP_LOGI(TNC_TAG, "Updated Full/Half Duplex Setting");
- tnc_settings.full_duplex = rx_buffer.buf[1];
- break;
- }
- // case KISS_CMD_SETHARDWARE: {
- // break;
- // }
- // case KISS_CMD_RETURN: {
- // tnc_settings.tx_delay = rx_buffer.buff_ptr[1];
- // break;
- // }
- default: {
- // error
- break;
- }
- }
- kiss_clear_buffer();
- return;
- }
- void kiss_set_tnc_paramters(tnc_settings_t s)
- {
- tnc_settings.tx_delay = s.tx_delay;
- tnc_settings.persistence = s.persistence;
- tnc_settings.slot_time = s.slot_time;
- tnc_settings.tx_tail = s.tx_tail;
- tnc_settings.full_duplex = s.full_duplex;
- // tnc_settings.tnc_number = s.tnc_number;
- }
- void kiss_set_tx_cb(tnc_cb_t tx_cb)
- {
- tnc_settings.tx_callback = tx_cb;
- }
- void kiss_set_rx_cb(tnc_cb_t rx_cb)
- {
- tnc_settings.rx_callback = rx_cb;
- }
- // TNC needs the ability to RX KISS frame, decode it, schedule it, and then send out via APRS or Arrow-Net
- // TNC needs the ability to switch to RX mode,
- void kiss_init(void)
- {
- // TODO: make frame buffer dynamic
- rx_buffer.max_len = FRAME_BUFFER_SIZE;
- tx_buffer.max_len = FRAME_BUFFER_SIZE;
- // TODO: Store / retrieve default TNC settings from flash?
- // set default TNC settings
- tnc_settings.tx_delay = 5;
- tnc_settings.persistence = 63;
- tnc_settings.slot_time = 10;
- tnc_settings.tx_tail = 1;
- tnc_settings.full_duplex = 0;
- // setup TNC task with queue
- }
- // Decode KISS-TNC data from SPP Interface and combine until there is
- // is a valid packet. Pass valid packet / frame to AX.25 engine.
- // Data Flow:
- // (1) SPP interface hands over data array which is runs a state machine to decode the KISS Frame
- // (2) Decoded information is uploaded thrown into a buffer
- // (3) When frame is complete the state machine will copy or indicate to another task to dump the buffer
- // If the frame is not completed, the state machine will wait until more data is provided by the SPP interface.
- // If the frame is corrupted or the buffer fills, the frame will be dumped and the error must be logged.
- 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); // append FEND to frame
- kiss_state = FRAME_ASS; // return to assembly
- }
- else if (chr == KISS_TFESC)
- {
- kiss_append_buffer(KISS_FESC); // append FESC to frame
- kiss_state = FRAME_ASS; // return to assembly
- }
- else if (chr == KISS_FEND)
- {
- kiss_process_frame(); // process frame
- kiss_state = FRAME_END; // end frame assembly
- }
- else
- {
- // log error
- kiss_append_buffer(chr); // append chr to frame
- kiss_state = FRAME_ASS; // return to assembly
- }
- break;
- }
- case FRAME_END: {
- if (chr != KISS_FEND)
- {
- kiss_append_buffer(chr); // append chr to frame
- kiss_state = FRAME_ASS; // return to assembly
- }
- else
- {
- kiss_state = FRAME_END; // stay in frame end
- }
- break;
- }
- case FRAME_ASS: {
- if (chr == KISS_FESC)
- {
- kiss_state = ESC_MODE;
- }
- else if (chr == KISS_FEND)
- {
- kiss_process_frame(); // process frame
- kiss_state = FRAME_END; // end frame assembly
- }
- else
- {
- kiss_append_buffer(chr); // append chr to frame
- kiss_state = FRAME_ASS; // continue frame assembly
- }
- break;
- default: {
- ESP_LOGE(TNC_TAG, "TNC FSM Error");
- }
- }
- }
- }
- }
|