// +FHDR------------------------------------------------------------ // Copyright (c) 2022 SmartLogic. // ALL RIGHTS RESERVED // ----------------------------------------------------------------- // Filename : test.c // Author : xianfeng.du // Created On : 2024-12-14 // Last Modified : // ----------------------------------------------------------------- // Description: // (Aut@qieYuan_2025.05.04) 新增数据接收与发送 收发聚合无多线程 // ----------------------------------------------------------------- #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include "typedef.h" #include "ucp_printf.h" #include "msg_transfer_mem.h" #include "pet_sm_mgt.h" #include "ucp_handshake.h" #include "ospShell.h" #include "ospLog.h" #include "ospCfgToBin.h" #include "ospTask.h" #include "drv_init.h" #include "ospSwTimer.h" #include "stc_drv_api.h" //---------------------------------------------------- // Last modified time: 2025.05.03 // Last Version: V1.0 //---------------------------------------------------- #include // udp #include // 绑定CPU #include // thread #include // POSIX api #include // open() #include // MMAP #include //---------------------------------------------------- #define SERVER_IP_CASE52 "172.29.118.76" // IP #define SERVER_PORT_CASE52 28864 // 端口 #define BUFFER_SIZE_CASE52 16016 // 用户缓冲区 int sockfd_case52; // 句柄 struct sockaddr_in server_addr_case52; // 套接字 #define MAP_SIZE_CASE52 1028096 // #define MAP_SIZE_ST_CASE52 4096 // 4096 #define MAP_ADDR_TX_BASE_CASE52 0x84C00000 // TX 数据位起始 #define MAP_ADDR_RX_BASE_CASE52 0x85001000 // RX 数据位起始 #define MAP_ADDR_RXFLAG_DATA_CASE52 0x85000000 // wr 计数起始位 int mem_fd_case52; // 统一文件描述符 ssize_t send_len; void *map_base_tx_case52; // TX映射地址 20块 void *map_base_rx_case52; // RX映射地址 8块 void *map_base_rxf_case52; // wr 计数映射地址 uint8_t w_idx, r_idx; // RX 信号 //---------------------------------------------------- uint32_t slot_ind_flag = 0; uint32_t slot_ind_time_flag = 0; uint32_t g_slot_time = 0; uint32_t gu32_value = 0; uint32_t gu32_tick_receive_ctrl = 0; uint32_t gu32_tick_from_tx_ctrl = 0; uint8_t ucp_print_err_f = 0; // Description: // 初始化 → 读文件发送 → APE → 接受数据 → 发送上位机 void *handleUdpTransmission(void *arg) { (void)arg; printf("[socket]:/************ linux_UDP ***************/\n"); if ((sockfd_case52 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { printf("[socket]:socket error!\n"); perror("socket creation failed\n"); exit(EXIT_FAILURE); } server_addr_case52.sin_family = AF_INET; // IPv4 server_addr_case52.sin_port = htons(SERVER_PORT_CASE52); // 端口转换 server_addr_case52.sin_addr.s_addr = inet_addr(SERVER_IP_CASE52); // IP转换 int flags = fcntl(sockfd_case52, F_GETFL, 0); fcntl(sockfd_case52, F_SETFL, flags | O_NONBLOCK); printf("[socket]:UDP ready\n"); // mmap /************************************************************************************** */ printf("[socket]: Opening /dev/mem for all mappings\n"); mem_fd_case52 = open("/dev/mem", O_RDWR | O_SYNC); if (mem_fd_case52 == -1) { printf("[mmap]: Failed to open /dev/mem !!!!\n"); perror("open /dev/mem"); exit(EXIT_FAILURE); } // TX printf("[mmap]:/*************** mmap_tx ***************/\n"); map_base_tx_case52 = mmap(NULL, MAP_SIZE_CASE52, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd_case52, MAP_ADDR_TX_BASE_CASE52); if (map_base_tx_case52 == MAP_FAILED) { printf("[mmap]: tx mapping failed!\n"); perror("mmap tx error"); close(mem_fd_case52); exit(EXIT_FAILURE); } printf("[mmap]: tx ready\n"); // RX printf("[mmap]:/*************** mmap_rx ***************/\n"); map_base_rx_case52 = mmap(NULL, MAP_SIZE_CASE52, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd_case52, MAP_ADDR_RX_BASE_CASE52); if (map_base_rx_case52 == MAP_FAILED) { printf("[mmap]: rx mapping failed!\n"); perror("mmap rx error"); munmap(map_base_tx_case52, MAP_SIZE_CASE52); // clean TX close(mem_fd_case52); exit(EXIT_FAILURE); } printf("[mmap]: rx ready\n"); // RX_FLAG printf("[mmap]:/************** rx_flag ***************/\n"); map_base_rxf_case52 = mmap(NULL, MAP_SIZE_ST_CASE52, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd_case52, MAP_ADDR_RXFLAG_DATA_CASE52); if (map_base_rxf_case52 == MAP_FAILED) { printf("[mmap]: RX_FLAG mapping failed!\n"); perror("mmap RX_FLAG error"); munmap(map_base_tx_case52, MAP_SIZE_CASE52); // clean TX munmap(map_base_rx_case52, MAP_SIZE_CASE52); // clean RX close(mem_fd_case52); exit(EXIT_FAILURE); } printf("[mmap]: wr_idx ready\n"); // 初始化消费者索引 /************************************************************************************** */ r_idx = 0; printf("[store]: r_idx ready == 0\n"); // rd txt /************************************************************************************** */ printf("[rd txt]:/************** FILE ***************/\n"); FILE *data_txt_fd; //const char *path = "/path/to/data.txt"; data_txt_fd = fopen("/opt/data.txt","r"); // 此处应为绝对路径 if (data_txt_fd == NULL) { printf("[txt_fd]:data_txt_fd error! :(\n"); perror("open txt error!"); exit(EXIT_FAILURE); } uint16_t txt_buffer[65535] = {0}; for (int txt_num = 0; txt_num < 65535; txt_num++) { if (fscanf(data_txt_fd, "%hu", &txt_buffer[txt_num]) != 1) { fprintf(stderr, "[Error] Failed to read number at position %d :(\n", txt_num); fclose(data_txt_fd); exit(EXIT_FAILURE); } } fclose(data_txt_fd); printf("[rd txt]: txt_buffer ready\n"); /************************************************************************************** */ printf("[tx]:wr...\n"); size_t wr_cunt = 0; uint16_t udp_data_buffer[8008]; printf("[tx]:先写入10块数据...\n"); size_t data_index = 0; for (int ready_num = 0; ready_num < 10; ready_num++) { for (int arr_cunt = 0; arr_cunt < 8008; ++arr_cunt) { // 偏移 size_t tx_data_offset = wr_cunt * 16016 + arr_cunt * 2; // cpy memcpy((char*)map_base_tx_case52 + tx_data_offset, &txt_buffer[data_index], 2);//65536 // 更新数据索引 data_index = (data_index + 1) % 65535; } // 更新地址块索引 wr_cunt = (wr_cunt + 1) % 20; } printf("[tx]:初始10块写入完成,当前地址块=%zu,数据索引=%zu\n", wr_cunt, data_index); printf("[while(1)]:/*************************************/\n"); size_t current_pos = 0; uint16_t value_mem = 0; uint8_t block_n = 0; uint8_t cut_i = 0; while (1) { usleep(10); if (1 == slot_ind_flag) { slot_ind_flag = 0; /************************************************************************************** */ // wr 16016*20(byte),一次while循环会写入 1/20 的数据,触发条件为slot_ind_flag current_pos = wr_cunt * 16016; for (int i = 0; i < 8008; ++i) { value_mem = txt_buffer[(data_index + i) % 65535]; memcpy((char*)map_base_tx_case52 + current_pos + i*2, &value_mem, 2); } data_index = (data_index + 8008) % 65535; wr_cunt = (wr_cunt + 1) % 20; /************************************************************************************** */ // 更新生产者计数 w_idx = __atomic_load_n( (volatile uint8_t *)(map_base_rxf_case52 + 0), __ATOMIC_ACQUIRE ); // w_idx = *((volatile uint8_t *)map_base_rxf_case52); /************************************************************************************** */ // 尝试读取 block_n = 0; if (w_idx == r_idx){ printf("[while(1)]: w_idx = %hhu, r_idx = %hhu\n", w_idx, r_idx); printf("[while(1)]: pass\n"); } else { if (r_idx < w_idx) { printf("[while(1)]: w_idx = %hhu, r_idx = %hhu\n", w_idx, r_idx); printf("[while(1)]: r_idx < w_idx\n"); block_n = w_idx - r_idx; for (cut_i = 0; cut_i < block_n; cut_i++) { memcpy(udp_data_buffer, (char*)map_base_rx_case52 + r_idx * 16016, 16016); // 发送数据到上位机 send_len = sendto( sockfd_case52, udp_data_buffer, sizeof(udp_data_buffer), 0, (const struct sockaddr*)&server_addr_case52, sizeof(server_addr_case52) ); if (send_len == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { printf("[UDP] UDP发送缓冲区满\n"); continue; }else{ perror("sendto error"); continue; } } r_idx ++ ; } // 更新r_idx,如果到头 r_idx = r_idx % 8; } else { //r_idx > w_idx printf("[while(1)]: w_idx < r_idx\n"); printf("[while(1)]: w_idx = %hhu, r_idx = %hhu\n", w_idx, r_idx); // 取完末尾 for (cut_i = 0; cut_i < (8-r_idx); cut_i++) { memcpy(udp_data_buffer, (char*)map_base_rx_case52 + r_idx * 16016, 16016); // 发送数据到上位机 send_len = sendto( sockfd_case52, udp_data_buffer, sizeof(udp_data_buffer), 0, (const struct sockaddr*)&server_addr_case52, sizeof(server_addr_case52) ); if (send_len == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { printf("[UDP] UDP发送缓冲区满\n"); continue; }else{ perror("sendto error"); continue; } } r_idx ++ ; } r_idx = r_idx % 8; // 取从头开始 for (cut_i = 0; cut_i < w_idx; cut_i++) { memcpy(udp_data_buffer, (char*)map_base_rx_case52 + cut_i * 16016, 16016); // 发送数据到上位机 send_len = sendto( sockfd_case52, udp_data_buffer, sizeof(udp_data_buffer), 0, (const struct sockaddr*)&server_addr_case52, sizeof(server_addr_case52) ); if (send_len == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { printf("[UDP] UDP发送缓冲区满\n"); continue; }else{ perror("sendto error"); continue; } } r_idx ++; } } } } } return NULL; } uint32_t rx_callback_data(const char* buf,uint32_t payloadSize) { uint32_t stc_cnt = read_stc_local_timer(); uint32_t value = *(uint32_t *)buf; uint16_t sfn = value >> 16; uint16_t slot = value & 0xffff; uint32_t tick_from_tx_ctrl = *(uint32_t *)(buf+4); // printf("[rx_callback_data]:slot_ind_flag == 1 \n"); slot_ind_flag = 1; gu32_value = value; gu32_tick_receive_ctrl = stc_cnt; gu32_tick_from_tx_ctrl = tick_from_tx_ctrl; uint32_t diff = stc_cnt - tick_from_tx_ctrl; if(0 == slot_ind_time_flag) { slot_ind_time_flag = 1; diff = 500000; } else { diff = stc_cnt - g_slot_time; } g_slot_time = stc_cnt; if ((diff > 520000) || (diff < 480000)) { if(ucp_print_err_f == 1){ UCP_PRINT_ERROR("qNO[%d],sfn[%d],slot[%d],diff[%d]",UCP4008_TRAFFIC_NR_eMBB_DATA,sfn,slot,diff); } } return payloadSize; } uint32_t rx1_callback_data(const char* buf,uint32_t payloadSize) { return payloadSize; } uint32_t rx_callback_ctrl(const char* buf,uint32_t payloadSize) { return payloadSize; } static inline void get_msg_transfer_info(uint16_t port_index, uint16_t inst_id, uint16_t transfer_type, transfer_type_info_s* transfer_type_info_ptr) { queue_info_s c_plane,u_plane; switch (transfer_type) { case CU_SPLIT: c_plane.rx_block_size = 0x25800; c_plane.rx_block_num = 32; c_plane.rx_callback = rx_callback_ctrl; c_plane.tx_block_size = 0x8000; c_plane.tx_block_num = 8; c_plane.tx_callback = NULL; u_plane.rx_block_size = 0x28000; u_plane.rx_block_num = 8; //u_plane.rx_callback = rx_callback_data; if (0 == inst_id) { u_plane.rx_callback = rx_callback_data; } else { u_plane.rx_callback = rx1_callback_data; } u_plane.tx_block_size = 0x28000; u_plane.tx_block_num = 8; u_plane.tx_callback = NULL; transfer_type_info_ptr->queue_cplane_info = c_plane; transfer_type_info_ptr->queue_uplane_info = u_plane; break; case OAM: c_plane.rx_block_size = 0x100000; c_plane.rx_block_num = 16; c_plane.rx_callback = rx_callback_oam; c_plane.tx_block_size = 0x8000; c_plane.tx_block_num = 4; c_plane.tx_callback = NULL; transfer_type_info_ptr->queue_cplane_info = c_plane; break; default: UCP_PRINT_ERROR("get_msg_queue_cfg doesn't support transfer_type[%d] .",transfer_type); break; } return; } static inline void msg_transfer_cfg(void) { uint8_t port_id = 0; uint16_t inst_id = 0; uint16_t transfer_type = 0; int32_t handle_id = 0; transfer_type_info_s transfer_type_info; #ifdef PCIE_WITH_JESD for (inst_id = 0; inst_id < (MAX_INSTANCE_NUM / 2); inst_id++) #else for (inst_id = 0; inst_id < MAX_INSTANCE_NUM; inst_id++) #endif { /*********************************transfer_type = CU_SPLIT**********************************/ transfer_type = CU_SPLIT; get_msg_transfer_info(port_id,inst_id,transfer_type, &transfer_type_info); handle_id = msg_transfer_init(port_id, transfer_type, inst_id, &transfer_type_info); if (handle_id < 0) { UCP_PRINT_ERROR("phy_cfg_init transfer_type:CU_SPLIT, handle_id[0x%08x] error.",handle_id); } /*********************************transfer_type = OAM**********************************/ transfer_type = OAM; get_msg_transfer_info(port_id,inst_id,transfer_type, &transfer_type_info); handle_id = msg_transfer_init(port_id, transfer_type, inst_id, &transfer_type_info); if (handle_id < 0) { UCP_PRINT_ERROR("phy_cfg_init transfer_type:OAM, handle_id[0x%08x] error.",handle_id); } } PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt(); MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt(); pPetSmLocalMgt->pSyncInfo->queueCfgFlag = ++pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag; return; } static inline void msg_transfer_queue_polling(void) { uint8_t port_id = 0; HandleId_t handler; uint16_t cu_flag; uint32_t offset = 0; uint32_t len = 0; uint8_t* msg_ptr; #ifdef PCIE_WITH_JESD for (uint32_t i = 0; i < (MAX_INSTANCE_NUM / 2); i++) #else for (uint32_t i = 0; i < MAX_INSTANCE_NUM; i++) #endif { handler.port_id = port_id; handler.inst_id = i; handler.type_id = CU_SPLIT; cu_flag = C_PLANE; msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr); cu_flag = U_PLANE; msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr); handler.type_id = OAM; msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr); } return; } #define CELL_SETUP_TYPE_SIMULATION (0x5a6b7c8d) void cell_setup_simulation(uint8_t scs_id, uint8_t cell_id, uint32_t run_core, uint8_t frame_type) { uint32_t size = 32; char* buf; uint32_t availableSize,offset; uint16_t cu_flag = C_PLANE; HandleId_t handler; handler.port_id = 0; handler.inst_id = 0; handler.type_id = CU_SPLIT; int32_t ret = msg_transfer_send_start(handler.value,cu_flag); /************C_PLANE***************/ ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset); if ( SUCCESS != ret) { UCP_PRINT_ERROR("cell_setup_simulation call msg_transfer_alloc_msg err."); return ; //continue; } *(uint32_t*)(buf + 0) = CELL_SETUP_TYPE_SIMULATION; *(uint32_t*)(buf + 4) = read_stc_local_timer(); *(uint32_t*)(buf + 12) = 0; // build cell *(uint32_t*)(buf + 16) = scs_id; *(uint32_t*)(buf + 20) = cell_id; *(uint32_t*)(buf + 24) = run_core; *(uint32_t*)(buf + 28) = frame_type; ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size); ret = msg_transfer_send_end(handler.value,cu_flag); return; } #define ORX_MSG_TYPE_SIMULATION (0xaabb7788) void orx_msg_send(void) { uint32_t size = 16; char* buf = NULL; uint32_t availableSize,offset; uint16_t cu_flag = C_PLANE; HandleId_t handler; handler.port_id = 0; handler.inst_id = 0; handler.type_id = CU_SPLIT; //usleep(10000); //sleep(1); int32_t ret = msg_transfer_send_start(handler.value,cu_flag); /************C_PLANE***************/ ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset); if ( SUCCESS != ret) { UCP_PRINT_ERROR("cell_setup_simulation call msg_transfer_alloc_msg err."); return ; //continue; } *(uint32_t*)(buf + 0) = ORX_MSG_TYPE_SIMULATION; *(uint32_t*)(buf + 4) = read_stc_local_timer(); ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size); ret = msg_transfer_send_end(handler.value,cu_flag); UCP_PRINT_DEBUG("start send orx message."); return; } uint64_t osp_orx_msg_proc_task(void) { sleep(1); orx_msg_send(); return OSP_OK; } int32_t osp_set_task_orx(uint8_t cpu, uint8_t pri) { OSP_TASKMSG_REG task_reg_tabl; OSP_STATUS ret = OSP_OK; if (cpu > 7) { UCP_PRINT_DEBUG("coreid %d beyond 7\n", cpu); return OSP_ERROR; } memset(&task_reg_tabl, 0, sizeof(OSP_TASKMSG_REG)); task_reg_tabl.TaskId = OspDbgLog+3; strcpy((void *)&task_reg_tabl.TaskName, "OspDbgOrx"); task_reg_tabl.TaskPri = RT_NOMSG_PRI(pri); task_reg_tabl.Init = NULL; task_reg_tabl.MainLoop = (OSP_FUNCPTR)osp_orx_msg_proc_task; task_reg_tabl.Cpu = cpu; task_reg_tabl.MsgType = OSP_PROCESS_MSG; ret = osp_regtask(&task_reg_tabl); return ret; } int32_t test_case(uint32_t argc, int32_t* argvp) { UCP_PRINT_DEBUG("start running testcase 46."); osp_read_spe_cfg_file("/ramfs/cfgDat"); msg_transfer_mem_init(); msg_transfer_cfg(); ucp_handshake(); UCP_PRINT_DEBUG("start transfering message."); // uint32_t size = 100; // char* buf; // uint32_t availableSize,offset; // uint8_t* ptr; // uint16_t cu_flag = C_PLANE; // HandleId_t handler; // handler.port_id = 0; // handler.inst_id = 0; // handler.type_id = CU_SPLIT; // int32_t ret; uint8_t scs_id = 1; // NR 30K // 0; // NR 15K uint8_t cell_id = 0; //0xFF; uint32_t run_ape = 0x11; //0x11 // uint32_t run_ape = 0x1; // 0; uint8_t frame_type = 0; // fdd cell_setup_simulation(scs_id, cell_id, run_ape, frame_type); //osp_set_task_orx(6, 95); /********************************************************* */ pthread_attr_t udp_attr; // 定义线程包 pthread_attr_init(&udp_attr); // 包初始化 cpu_set_t udp_cpuset; // CPU集合 CPU_ZERO(&udp_cpuset); // 集合初始化 CPU_SET(2, &udp_cpuset); // 选择核心2 pthread_attr_setaffinity_np(&udp_attr, sizeof(udp_cpuset), &udp_cpuset); // 绑定 pthread_t thread_udp; if (pthread_create(&thread_udp, &udp_attr, handleUdpTransmission, NULL) != 0) { printf("[SOCKET]:thread error!\n"); perror("pthread_create"); pthread_attr_destroy(&udp_attr); exit(EXIT_FAILURE); } pthread_attr_destroy(&udp_attr); // 销毁 /********************************************************* */ // 进入循环 while(1) { msg_transfer_queue_polling(); // if(1 == slot_ind_flag) // { // handler.inst_id = 0; // cu_flag = C_PLANE; // ret = msg_transfer_send_start(handler.value,cu_flag); // ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset); // if ( SUCCESS != ret) { // UCP_PRINT_ERROR("0,c_plane alloc error\r\n"); // continue; // } // ptr = (uint8_t *)buf; // *(uint32_t*)(ptr + 0) = gu32_value; // *(uint32_t*)(ptr + 4) = gu32_tick_from_tx_ctrl; // *(uint32_t*)(ptr + 8) = gu32_tick_receive_ctrl; // *(uint32_t*)(ptr + 12) = read_stc_local_timer(); // ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size); // if ( SUCCESS != ret) { // UCP_PRINT_ERROR("0,c_plane send error\r\n"); // } // ret = msg_transfer_send_end(handler.value,cu_flag); // /************U_PLANE***************/ // cu_flag = U_PLANE; // ret = msg_transfer_send_start(handler.value,cu_flag); // ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset); // if ( SUCCESS != ret) { // UCP_PRINT_ERROR("0,u_plane alloc error\r\n"); // continue; // } // ptr = (uint8_t *)buf; // *(uint32_t*)(ptr + 0) = gu32_value; // *(uint32_t*)(ptr + 4) = gu32_tick_from_tx_ctrl; // *(uint32_t*)(ptr + 8) = gu32_tick_receive_ctrl; // *(uint32_t*)(ptr + 12) = read_stc_local_timer(); // ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size); // if ( SUCCESS != ret) { // UCP_PRINT_ERROR("0,u_plane send error\r\n"); // } // ret = msg_transfer_send_end(handler.value,cu_flag); // } } return 0; }