#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#include "sys_init.h"
#include "lwip_network.h"
#include "wifi_api.h"
#if defined(MTK_MINICLI_ENABLE)
#include "cli_def.h"
#endif

#include "jerry-api.h"
#include "jerry_run.h"
#include "jerry_targetjs.h"

#include "bsp_gpio_ept_config.h"

void js_lib_init() {
  /*
   */ 
ml_pinmux_init();
ml_gpio_init();
ml_wifi_init();
ml_http_init();
ml_timer_init();
ml_pwm_init();
ml_wdt_init();
ml_md5_init();
ml_sha_init();
ml_adc_init();
ml_mqtt_init();
/* 
  */
}

int fota_mode = 0;

void js_init() {
  int retcode;

  DECLARE_JS_CODES;

  /* run main.js */
  retcode = js_entry (js_codes[0].source, js_codes[0].length);
  if (retcode != 0)
  {
    printf ("js_entry failed code(%d) [%s]\r\n", retcode, js_codes[0].name);
    js_exit ();
    return -1;
  }

  vTaskDelete(NULL);
}

int main(void)
{
    /* Do system initialization, eg: hardware, nvdm, logging and random seed. */
    system_init();

    /* bsp_ept_gpio_setting_init() under driver/board/mt76x7_hdk/ept will initialize the GPIO settings
     * generated by easy pinmux tool (ept). ept_*.c and ept*.h are the ept files and will be used by
     * bsp_ept_gpio_setting_init() for GPIO pinumux setup.
     */
    bsp_ept_gpio_setting_init();

    /* Initialize cli task to enable user input cli command from uart port.*/
#if defined(MTK_MINICLI_ENABLE)
    cli_def_create();
    cli_task_create();
#endif
    xTaskCreate(js_init,"js_init", 15096, NULL, 2, NULL);

    vTaskStartScheduler();

    /* If all is well, the scheduler will now be running, and the following line
    will never be reached.  If the following line does execute, then there was
    insufficient FreeRTOS heap memory available for the idle and/or timer tasks
    to be created.  See the memory management section on the FreeRTOS web site
    for more details. */
    for ( ;; );
}

