/*
 * vos_Base.c
 *
 *  Created on: 2025年9月8日
 *      Author: adan
 */
#include "CM_Types.h"
#include "OFM_Base.h"
#include "MSCP_Define.h"
#include "CM_Socket.h"
#include <pthread.h>

#define CPU_MAX   16u

/*
 *设置cpu亲和力
 * */
void set_thread_affinity(CM_UINT32 mask_bits)
{
#if QNX
    void* pmask = (void*)mask_bits;

    ThreadCtl(_NTO_TCTL_RUNMASK, pmask) ;
#elif LINUX
    cpu_set_t mask;
    CPU_ZERO(&mask);
    for (int i = 0; mask_bits; i++)
    {
        /*根据输入参数掩码设置cpu*/
        if (mask_bits & (1UL << i))
        {
            CPU_SET(i, &mask);
        }
    }
    sched_setaffinity(0, sizeof(cpu_set_t), &mask);

#elif SylixOS
    cpu_set_t mask;
    CPU_ZERO(&mask);
    for (int i = 0; i < CPU_MAX; i++)
    {
        /*根据输入参数掩码设置cpu*/
        if (mask_bits & (1UL << i))
        {
            CPU_SET(i, &mask);
        }
    }
    sched_setaffinity(0, sizeof(cpu_set_t), &mask);

#elif ReWorks
    cpuset_t affinity;
    CM_THREAD_T tid;
    CPUSET_ZERO(affinity);
    /*根据输入参数掩码设置cpu*/
    for (int i = 0; i < CPU_MAX; i++)
    {
        /*根据输入参数掩码设置cpu*/
        if (mask_bits & (1UL << i))
        {
            CPUSET_SET(affinity, i);
        }
    }

    tid = pthread_self();
    pthread_affinity_set(tid, affinity);

#else

#endif
}

#if 0
/*
 * 设置网卡ip
 * */
void set_network_ip(CM_UINT8 *p_ifname, CM_UINT8 *p_ifaddr, CM_UINT8 *p_netmask, CM_UINT8 *p_gateway)
{
    const char *ifname  = NULL;//需要设置的网卡名
    const char *ipaddr  = NULL;//需要设置的ip地址
    const char *netmask = NULL;//需要设置的子网掩码
    const char *gateway = NULL;//需要设置的网关


    CM_SOCKET sockfd = 0;
    struct ifreq ifr = {0};
    int ret;
    CM_SOCKADDR_T *sin = NULL;


    if((NULL !=p_ifname)&&(NULL !=p_ifaddr)&&(NULL !=p_netmask)&&(NULL !=p_gateway))
    {
        ifname  =    p_ifname;
        ipaddr  =    p_ifaddr;
        netmask =    p_netmask;
        gateway =    p_gateway;

        //创建一个socket
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if(sockfd < 0)
        {

        }
        else
        {
             memset(&ifr, 0, sizeof(struct ifreq));

             strcpy(ifr.ifr_name, (const char *)ifname);
             sin = (struct sockaddr_in *)&ifr.ifr_addr;
             sin->sin_family = AF_INET;

             /* IP地址设置 */
             /*将点分十进制 转换为网络字节序*/
             inet_aton(ipaddr, &(sin->sin_addr));
             ret = ioctl(sockfd, SIOCSIFADDR, &ifr);
             if(ret < 0)
             {
                 close(sockfd);
                 /*设置ip失败*/

             }
             else
             {
                 /* 子网掩码设置 */
                 inet_aton(netmask, &(sin->sin_addr));
                 ret = ioctl(sockfd, SIOCSIFNETMASK, &ifr);
                 if(ret < 0)
                 {
                     /*设置子网掩码失败*/
                     close(sockfd);
                 }
                 else
                 {

                     /*暂不设置网关*/
                 }
             }

        }
    }
    else
    {
        //CM_ASSERT(0,MAIN_MODE, DARK_FAULT_CODE_FUN_ORDER_3, TSCP_DARK_ASSERT_1);
    }
}

#endif


