/*
 * testSocket.c
 *
 *  Created on: Aug 29, 2019
 *      Author: root
 */
extern "C"
{
#include <sys/socket.h>
#include <stdio.h>
#include <inttypes.h>
#include <xdc/std.h>
#include <ti/posix/ccs/pthread.h>
#include <ti/posix/ccs/mqueue.h>
#include <ti/sysbios/knl/Task.h>
//#include <ti/ndk/inc/socketndk.h>
extern "C"
{
#include "ti/platform/platform.h"
}
}
extern "C" int    fdOpenSession( void *hTask );
extern "C" void   fdCloseSession( void *hTask );


extern "C" Bool  threadCreate(void * (*threadFuncPtr)(void *), void * param )
{
    //fdOpenSession((void *)pthread_self());
    int status;
    pthread_attr_t attr;
    struct sched_param pParam;
    //intialize thread attributes
    status =pthread_attr_init(&attr);
    if(status!=0)
    {
        //pthread_attr_init return 0 on success
        printf("thread attr init failed\n");
    }
//set thread size.
    status = pthread_attr_setstacksize(&attr,0x2048);
    if(status!=0)
    {
        printf("set stack size failed. status: %d, %d  \n",status,0x2048);
    }

    status = pthread_attr_getschedparam (&attr, &pParam);
    if(status!=0)
    {
        printf("get pthread attribute failed.");

    }
    pParam.sched_priority = 5;
   //set thread priority
    status = pthread_attr_setschedparam (&attr, &pParam);
#ifdef __TI_COMPILER_VERSION__
    //linux this check will fail because we are using non-root account
    if (status!=0)
    {
        printf("pthread set priority failed. error:%d\n",status);
    }
#endif
    //create the thread and run it.
    pthread_t thread;
    status = pthread_create(&thread,&attr,threadFuncPtr,(void*)param);
   printf("%s created\n","HELLO NDK");
    if(status!=0)
    {
        printf("thread creation failed\n");
    }
    pthread_join(thread,NULL);
    return TRUE;
}
///* File Descriptor Functions */
extern "C" int networkStackStarted;
 extern "C" int    fdOpenSession( void *hTask );
 extern "C" void   fdCloseSession( void *hTask );
extern "C" int dtask_udp_hello2( void )
{

    while(!networkStackStarted)
    {
        Task_sleep(1000);
    }
   fdOpenSession((void *)pthread_self());

   int skt;
   skt = (int) socket(AF_INET,SOCK_DGRAM,0);
   if(skt==-1)
   {

       printf("socket create failed.\n");
   }
   fdCloseSession(pthread_self());

   return 0;
}


