/* BSD Socket API Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "tcpip_adapter.h"
#include "protocol_examples_common.h"

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include <lwip/netdb.h>


#ifdef CONFIG_EXAMPLE_IPV4
#define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR
#else
#define HOST_IP_ADDR CONFIG_EXAMPLE_IPV6_ADDR
#endif

#define PORT CONFIG_EXAMPLE_PORT

static char *TAG = "example";
static char payload[29] = {0x47,0x01,0x25,0x00,
                            0x00,0x00,0x00,0x00,
	                        0x00,0x00,0x00,0x00,
	                        0x00,0x00,0x00,0x00,
	                        0x00,0x00,0x00,0x00,
	                        
	                        0x00,0x00,0x00,0x00,
	                        0x00,0x00,0x00,0x00,
	                        0x74};




static void udp_client_task(void *pvParameters)
{
    char rx_buffer[128];
    char addr_str[128];
    int addr_family;
    int ip_protocol;

    while (1) {

#ifdef CONFIG_EXAMPLE_IPV4
        struct sockaddr_in dest_addr;
        dest_addr.sin_addr.s_addr = inet_addr("192.168.1.122");
        dest_addr.sin_family = AF_INET;
        dest_addr.sin_port = htons(19999);
        addr_family = AF_INET;
        ip_protocol = IPPROTO_IP;
        inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str) - 1);
#else // IPV6
        struct sockaddr_in6 dest_addr;
        inet6_aton(HOST_IP_ADDR, &dest_addr.sin6_addr);
        dest_addr.sin6_family = AF_INET6;
        dest_addr.sin6_port = htons(PORT);
        addr_family = AF_INET6;
        ip_protocol = IPPROTO_IPV6;
        inet6_ntoa_r(dest_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
#endif

        int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
        if (sock < 0) {
            ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
            break;
        }
       ESP_LOGI(TAG, "Socket created, sending to %s:%d", "192.168.1.102",9999);
       // uint8_t  flag1 = 2;

       int i = 0; 
	   int errtimer = 0;
		while(1){
			    int err = sendto(sock, payload, 29, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
	            if (err < 0) {
					errtimer++;
	                printf("errno:%d\n",errno);
				    printf("send%d  errno:%d  /",i,errtimer);
					int heapmini = esp_get_minimum_free_heap_size();
				    printf("hmi : %d /",heapmini);
					int ferrHeap = esp_get_free_heap_size();
					printf("fh : %d /:",ferrHeap);
                    int ferrinHeap = esp_get_free_heap_size();
					printf("fih : %d \n",ferrinHeap);
	            }
				vTaskDelay(4);//4ms
				i++;
				if(i%200 == 0)
				{
				 	printf("send%d  errno:%d  /",i,errtimer);
					int heapmini = esp_get_minimum_free_heap_size();
				    printf("hmi : %d /",heapmini);
					int ferrHeap = esp_get_free_heap_size();
					printf("fh : %d /:",ferrHeap);
                    int ferrinHeap = esp_get_free_heap_size();
					printf("fih : %d \n",ferrinHeap);
				}
		}

		 vTaskDelete(NULL);

    }
}
	

void app_main()
{
    ESP_ERROR_CHECK(nvs_flash_init());
    tcpip_adapter_init();
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
     * Read "Establishing Wi-Fi or Ethernet Connection" section in
     * examples/protocols/README.md for more information about this function.
     */
    ESP_ERROR_CHECK(example_connect());

    xTaskCreate(udp_client_task, "udp_client", 4096, NULL, 5, NULL);
}
