/* * 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 "cc1200.h" #include "fcs_calc.h" #include "kiss.h" buffer_handle_t buffer_handle; tnc_settings_t tnc_settings; void kiss_clear_buffer(void) { ESP_LOGD(TNC_TAG, "TNC buffer cleared."); buffer_handle.index = 0; } void kiss_append_buffer(uint8_t chr) { ESP_LOGD(TNC_TAG, "Appending %x to TNC buffer.", chr); // ERROR - Packet Buffer Overflow if (buffer_handle.index >= buffer_handle.max_len) { ESP_LOGE(TNC_TAG, "TNC buffer overflow"); kiss_clear_buffer(); } buffer_handle.buf[(buffer_handle.index)] = chr; buffer_handle.index = buffer_handle.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 = buffer_handle.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", (buffer_handle.index)); // unblock AX.25 code to process code uint32_t fcs = fcs_calc(&buffer_handle.buf[1], buffer_handle.index-1); buffer_handle.buf[buffer_handle.index] = fcs & 0xFF; buffer_handle.buf[buffer_handle.index+1] = fcs>>8 & 0xFF; xRingbufferSend(radio_tx_buf, &buffer_handle.buf[1], ((buffer_handle.index+1)*sizeof(uint8_t)), 10/portTICK_PERIOD_MS); ESP_LOG_BUFFER_HEXDUMP(TNC_TAG, &buffer_handle.buf[1], buffer_handle.index+1, ESP_LOG_INFO); //cc1200_radio_APRSTXPacket(buffer_handle.buf, (buffer_handle.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 = buffer_handle.buf[1]; break; } case KISS_CMD_P: { ESP_LOGI(TNC_TAG, "Update Persistence"); tnc_settings.persistence = buffer_handle.buf[1]; break; } case KISS_CMD_SLOTTIME: { ESP_LOGI(TNC_TAG, "Updated Slottime"); tnc_settings.slot_time = buffer_handle.buf[1]; break; } case KISS_CMD_TXTAIL: { ESP_LOGI(TNC_TAG, "Updated TX Tail"); tnc_settings.tx_tail= buffer_handle.buf[1]; break; } case KISS_CMD_FULLDUPLEX: { ESP_LOGI(TNC_TAG, "Updated Full/Half Duplex Setting"); tnc_settings.full_duplex = buffer_handle.buf[1]; break; } // case KISS_CMD_SETHARDWARE: { // break; // } // case KISS_CMD_RETURN: { // tnc_settings.tx_delay = buffer_handle.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; } // 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) { buffer_handle.max_len = FRAME_BUFFER_SIZE; // TODO: make frame buffer dynamic // tnc_settings.max_frame_size = 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