#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/uart.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define UART_DEV_NAME DT_LABEL(DT_NODELABEL(uart1))
#define UART_BUF_SIZE 1024

static void uart_cb(const struct device * uart, void * user_data)
{
	static uint8_t data_array[UART_BUF_SIZE];
    static uint8_t index = 0;
	
	uart_irq_update(uart);
	
	if (uart_irq_rx_ready(uart)) {
		
		int data_length;
		data_length = uart_fifo_read(uart, &data_array[index],
					     UART_BUF_SIZE-index);
		index += data_length;
		
		if (index > 0) {	
			if ((index == UART_BUF_SIZE) ||
			   (data_array[index - 1] == '\n') ||
			   (data_array[index - 1] == '\r')) {
				printk("%s", (char *)data_array);
				
				char dest[strlen(data_array)];
				strcpy(dest, (char *)data_array);
				printk("%s", dest);
				const char ch = ',';
				char * ret;
				ret = strchr((char *)data_array, ch);
				
				if(ret != NULL){
					printk("%s", ret);
				}

				index = 0;
				memset(data_array, 0, sizeof(data_array));
			}
		}
	}
}

void main(void)
{
        
	const struct device *uart = device_get_binding(UART_DEV_NAME);

	uart_irq_callback_set(uart, uart_cb);
	uart_irq_rx_enable(uart);
	printk("UART sample start\n");
	while (1) {
		k_cpu_idle();
	}
}