/*
 * ospInit - Initailize OSP
 * input:
 *    none
 * return
 *    OSP_OK
 */
#define _GNU_SOURCE 1
#define __USE_GNU 1

#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <limits.h>
#include <sys/types.h> 
#include <unistd.h>
#include <sys/wait.h>
#include "osp.h"
#include "ospCfgToBin.h"
#include "ucp_printf.h"
#include "ospSwTimer.h"
#include "ospDump.h"
#include "ospHeartbeat.h"
#include "ospLog.h"

int32_t ospgdb = 1;
int32_t g_ProcessId = 0;

#define MAX_ARGC_NUM  32
#define MAX_ARGV_LEN  64
int32_t parm_num;
char parm[MAX_ARGC_NUM][MAX_ARGV_LEN] = {0};

static int32_t g_osp_init_done = 0;

int32_t osp_init_done()
{

    g_osp_init_done = 1;
    return 0;
}

int32_t osp_is_init_done()
{
    return g_osp_init_done;
}

/*
 *osp_version - show OSP version
 *input:
 *   none
 *return
 *   none
 */

void osp_version(void)
{
    osp_debug_out(CMD_DEBUG_LEVEL, "%s\n", OSP_VERSION" (" __DATE__ " - " __TIME__ ")");
}

void osp_exit(void)
{
    osp_debug_out(CMD_DEBUG_LEVEL, "osp exit ok!");
}

void osp_atexit(void)
{
    if (atexit(osp_exit) != 0)
         osp_debug_out(CMD_DEBUG_LEVEL, "can't register ospExit");
}

int32_t osp_record_init_parm(int32_t argc, char *argv[])
{
    int32_t i;

    if ((argc >= MAX_ARGC_NUM) || (NULL == argv))
    {
        return -1;
    }
    parm_num = argc;
    for (i = 0; i < argc; i++)
    {
        strcpy(parm[i], argv[i]); 
    }

    return 0;
}

int32_t osp_get_init_parm(int32_t *argc, char *argv, int32_t size)
{
    int32_t i;
	
	if ((NULL == argc) || (NULL == argv))
	{
		return -1;
	}
		
	*argc = parm_num;
	
    for (i = 0; i < parm_num; i++)
    {
		strcpy(&argv[i*size], parm[i]); 
    }
	return 0;
}
#if 0
OSP_STATUS main(INT32 argc,char *argv[])
{

    osp_record_init_parm(argc, argv);
    {
        CURRENT_TASKID = 0;

        osp_debug_out_with_time(RUN_DEBUG_LEVEL, "starting OSP initializing...\n");

        /*import all symbols for all so*/
        //osp_find_sym("");
        osp_mem_init();
        osp_mut_process_init();
        osp_sw_queue_init();
        osp_shell_init();
        osp_debug_init();
        osp_log_init();
        osp_msg_que_init();
        osp_buf_init();
        osp_udp_init();
        osp_timer_pool_init();
        osp_diag_init();
        test_main();
        osp_task_init(); 
        osp_delay(1000000);/*make sure all task start*/

        osp_start_task_all();
        osp_init_done();
        osp_version();
        osp_atexit();
        osp_debug_out_with_time(RUN_DEBUG_LEVEL, "OSP initializing finished.\n");

        while (1)
        {
            osp_delay(1000000);
        }    
   }
}
#else
OSP_STATUS osp_init()
{
    printf("lib build: %s %s\n", __DATE__, __TIME__);
    printf("---V1.3.SLEVMY.CK.202306122125_application_platform_2023_week28---\n");

    UCP_PRINT_DEBUG("starting OSP initializing...");

    osp_record_init_parm(0, NULL);
    {
        CURRENT_TASKID = 0;

        UCP_PRINT_DEBUG("starting OSP initializing...\n");

        /*import all symbols for all so*/
        //osp_find_sym("");
        osp_mem_init();
        osp_mut_process_init();
        osp_sw_queue_init();
        osp_shell_init();
        osp_debug_init();
        osp_log_init();
        osp_dump_init();
        osp_msg_que_init();
        osp_buf_init();
        osp_udp_init();
        osp_timer_pool_init();
        osp_diag_init();
        osp_read_cfg_file();
        #ifdef HEARTBEAT_ENABLE
        OspTmrInit();
        #endif
        osp_task_init();
        osp_delay(1000);/*make sure all task start*/
        #ifdef HEARTBEAT_ENABLE
        OspHeartbeatPro();
        #endif
        UCP_PRINT_DEBUG("OSP initializing finished.\n");

        osp_start_task_all();
        osp_init_done();
        osp_version();
        osp_atexit();
        UCP_PRINT_DEBUG("OSP initializing finished.\n");

        /*while (1)
        {
            osp_delay(1000000);
        }*/
        return OSP_OK;
   }
}

#endif
