/******************************************************************************
版权所有：    @copyright (C) 2024-2034 HaiYang Technology Corp. All rights reserved.
文件名称：    main_demo.c
文件版本：    01.01
创建作者:     zhaoyang
创建日期：    2025-07-28
功能说明：    主程序
其它说明：
修改记录：
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sched.h>
#include <sys/resource.h>
#include "bsp_ustimer.h"
#include <sys/random.h> // 生成随机数，用于测试
#include "bsp_share.h"
#include <sys/mman.h>
#include "bsp_ccu.h"
#include "bsp_shm.h"
#include "bsp_packet.h"

void *thread_function(void *arg) {
    int ret, i = 0;
    long diff = 0;
	unsigned int *tmp_buf;

    if (shmem_init(&shmem_fd)) {
        printf("shmem_init failed!\n");
        return NULL;
    }                         
	
	tmp_buf = mmap(NULL, shmem_fd.linux_read_len, PROT_READ|PROT_WRITE, 
		       MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
	if (tmp_buf <= 0) {
        printf("mmap failed!\n");
        return NULL;
    }
	
	printf("tmp_buf:%lx\n", (unsigned long)tmp_buf);
    while (true) {
        static unsigned long tmp = 0;
        unsigned int test_data;
		unsigned short crc;
		int len = shmem_fd.linux_write_data_len;
        unsigned long origin = ustimer_get_origin();
        diff = (origin > tmp + 1000 * 5) ? (origin - tmp - 1000 * 5) : (tmp + 1000 * 5 - origin);
        printf("Current timer value: %lu diff: %lu priority: %d\n", origin, diff, getpriority(PRIO_PROCESS, 0));
        
        tmp = origin;
        // 模拟一些工作 [46ms]
        // 获取随机数，用于测试
		if (getrandom(&test_data, sizeof(test_data), GRND_RANDOM) == -1) {
			printf("getrandom failed");
			goto done;
		}

		for (i = 0; i < len/4; i++)
			tmp_buf[i] = test_data;
		ret = shmem_write(&shmem_fd, tmp_buf, len, 0);
		if (ret < 0) {
			printf("failed to write shmem %d\n", ret);
			// goto done;
		} else {
            printf("write crc=%04x len=%d\n", crc, ret);
        }

		ret = shmem_read(&shmem_fd, tmp_buf, len, 0);
		if (ret < 0) {
			printf("failed to read shmem %d\n", ret);
			// goto done;
		} else {
            printf(" read crc=%04x len=%d\n", crc, ret);
        }

        usleep((1000 * 5 - diff) > 0 ? (1000 * 5 - diff) : 1000 * 5);
    }

done:
	munmap(tmp_buf, shmem_fd.linux_read_len);
	shmem_exit(&shmem_fd);
    return NULL;
}

/* we test used here */
int main_demo(int argc, char *argv[]){
    pthread_attr_t attr;
    struct sched_param param;
    pthread_t thread;

    ccu_init();

    ustimer_init();

    shm_init();

#ifdef UES_THREAD_RT
    // 初始化线程属性
    pthread_attr_init(&attr);

    // 设置调度策略为 SCHED_FIFO
    pthread_attr_setschedpolicy(&attr, SCHED_FIFO);

    // 设置线程优先级
    param.sched_priority = 90;
    pthread_attr_setschedparam(&attr, &param);

    // 显式指定调度策略
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);

    // 创建线程
    pthread_create(&thread, &attr, thread_function, NULL);

    // 等待线程结束
    pthread_join(thread, NULL);

    // 销毁线程属性
    pthread_attr_destroy(&attr);
#endif

    while (true) {
        unsigned long origin = ustimer_get_origin();
        printf("Current timer value: %lu\n", origin);

        shm_comm_test();

        usleep(1000 * 2000);
    }
   
    ustimer_exit();
    ccu_exit();
    shm_exit();
    return 0;
}
