/* * 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 #include "kiss.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); // 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 // TODO: Can we eliminate TX buffer and use BLE Buffer only????? 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); // activate callback function 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); // 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) { // minimum kiss frame is 2 bytes if (rx_buffer.index < 2) return; uint8_t data_byte, tnc_number; data_byte = rx_buffer.buf[0]; tnc_number = (data_byte >> 4) & 0x0F; // confirm packet is for this device if (tnc_number == kiss_settings.tnc_number) kiss_settings.rx_callback(rx_buffer.buf, rx_buffer.index); kiss_clear_buffer(); } // 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(uint8_t tnc_number, kiss_cb_t tx_cb, kiss_cb_t rx_cb) { // TODO: make frame buffer dynamic rx_buffer.max_len = FRAME_BUFFER_SIZE; tx_buffer.max_len = FRAME_BUFFER_SIZE; // set tnc number kiss_settings.tnc_number = tnc_number; // set callback functions kiss_settings.rx_callback = rx_cb; kiss_settings.tx_callback = tx_cb; } // 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