// +FHDR------------------------------------------------------------
//                 Copyright (c) 2022 SmartLogic.
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : main.c
// Author        : xianfeng.du
// Created On    : 2022-06-25
// Last Modified :
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------

#define _GNU_SOURCE
#include <sched.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

#include "ospLog.h"
#include "ospShell.h"
#include "ucp_printf.h"
#include "drv_init.h"
#include "stc_drv_api.h"

int32_t test_case(uint32_t argc, int32_t* argvp);

extern OSP_STATUS osp_init();
extern int32_t osp_set_taskcpu(uint8_t cpu, uint8_t pri);
extern uint8_t osp_sw_queue_init();
extern int32_t osp_signal_init(void);


#ifdef ENABLE_JESD_TEST
extern int32_t UCP_API_RFIC_CellInit(void);
extern int32_t UCP_API_RFIC_CellSetup(uint64_t txLo, uint64_t rxLo, uint64_t bw, uint16_t initAtt);
#endif

#define RFIC_SEM_KEY    0x1027
int32_t gi32_rfic_sem = 0;

int32_t rfic_sem_create(void)
{
    int32_t i32_ret = 0;

    /* create sem */
    gi32_rfic_sem = semget((key_t)RFIC_SEM_KEY, 1, IPC_CREAT);
    if (-1 == gi32_rfic_sem)
    {
        printf("rfic_sem_create semget error!\r\n");
        return -1;
    }

    /* set value */
    i32_ret = semctl(gi32_rfic_sem, 0, SETVAL, 0);
    if (-1 == i32_ret)
    {
        printf("rfic_sem_create semctrl error!\r\n");
        return -2;
    }
    return 0;
}

int32_t rfic_sem_wait(int32_t sem_id, int8_t timeout)
{
    struct sembuf   st_sem_buf;
    struct timespec st_time;
    int32_t i32_ret = 0;

    memset(&st_sem_buf, 0, sizeof(st_sem_buf));
    st_sem_buf.sem_num = 0;
    st_sem_buf.sem_op  = 1;
    st_sem_buf.sem_flg = SEM_UNDO;

    memset(&st_time, 0, sizeof(st_time));
    st_time.tv_sec  = timeout/1000;
    st_time.tv_nsec = (timeout%1000)*1000000;

    if (-1 == timeout)
    {
        i32_ret = semop(sem_id, &st_sem_buf, 1);
    }
    else
    {
        i32_ret = semtimedop(sem_id, &st_sem_buf, 1, &st_time);
    }

    return i32_ret;
}

#define MAX_PARA_NUM 4

uint8_t gu8_main_flag = 1;  /* main fun while(): 1 while; 0: out */
int32_t main(int32_t argc, char* argvp[])
{
    //uint32_t stc_cnt = 0, stc_cnt1 = 0;

    UCP_PRINT_DEBUG("Hello world from A72.");

    cpu_set_t mask;
    CPU_ZERO(&mask);
    CPU_SET(4,&mask);
    sched_setaffinity(0,sizeof(cpu_set_t),&mask);

#ifdef ENABLE_JESD_TEST
    int32_t i32_ret = 0;
    i32_ret = rfic_sem_create();
    if (0 != i32_ret)
    {
        printf("rfic_sem_create return error!\r\n");
        return -1;
    }

    i32_ret = rfic_sem_wait(gi32_rfic_sem, -1);
    if (0 != i32_ret)
    {
        printf("rfic_sem_wati return error!\r\n");
        return -2;
    }
#endif

#if 0
#ifdef ENABLE_JESD_TEST
    UCP_API_RFIC_CellInit();
    UCP_API_RFIC_CellSetup(2575770000u, 2575770000u, 100000000u, 0);
#endif
#endif
    osp_init();
    osp_set_taskcpu(7, 95);
    drv_init();
    #if 0
    init_stc();/*new feature#1935*/
    #endif
    osp_signal_init();

#ifdef PALLADIUM_TEST
    UCP_PRINT_DEBUG("entered testmode.");

    int32_t args[MAX_PARA_NUM];
    uint32_t count = argc - 1;
    if(count > MAX_PARA_NUM) {
        UCP_PRINT_ERROR("parameter number[%d] error",count);
        return -1;
    }

    for (uint32_t i=0; i<count;i++) {
        args[i] = (int32_t)strtoul(argvp[i+1],NULL,0);
    }

    test_case(count, args);
#endif

    while (gu8_main_flag)
    {
        usleep(100);
    }

    return 0;
}

