--- /dev/null
+#include "main_include.h"
+
+/*******************************************************************************************************/
+static int agent_get_list_cb(int iReqId, ST_ZJ_AIAGENTLIST* pstAiAgentList, int iRspCode);
+static int agent_get_history_list_cb(int iReqId, ST_ZJ_HISTORYSESSION* pstHistorySessionList, int iRspCode);
+static int agent_trans_output(unsigned char* pucSessionID, unsigned char* pucTurnID, unsigned char* pucTextUrl, int iRspCode);
+static int agent_result_output_text(unsigned char* pucSessionID, unsigned char* pucTurnID, unsigned char* pucTextUrl, int iRspCode);
+static int agent_result_output_audio(unsigned char* pucSessionID, unsigned char* pucTurnID, unsigned char* pucAudioUrl, int iRspCode);
+static int agent_result_output_picture(unsigned char* pucSessionID, unsigned char* pucTurnID, unsigned char* pucPictureUrl, int iRspCode);
+/*******************************************************************************************************/
+static AGENT_INFO agent_info = { 0 };
+/*******************************************************************************************************/
+AGENT_INFO* agent_getInfo(void)
+{
+ return &agent_info;
+}
+
+void agent_init()
+{
+ if(agent_info.bInitFlag == 1) return ;
+ memset(&agent_info, 0, sizeof(AGENT_INFO));
+ agent_info.bInitFlag = 1;
+ ZJ_SetAIAgentAbility(1);
+#ifdef FULL_DUPLEX
+ ZJ_SetAudioDuplexMode(AUDIO_MODE_FULL_DUPLEX);
+#endif
+ ZJ_SetTranslateOutputCbFuncs(agent_trans_output);
+ ZJ_SetResultOutputCbFuncs(agent_result_output_text, agent_result_output_audio, agent_result_output_picture);
+ printf("agent init ok \n");
+ return;
+}
+
+static void* agent_task_loop(void* arg)
+{
+ int iRet = -1;
+
+ while (system_getInfo()->uiDemoRunFlag == 1)
+ {
+ if (agent_getInfo()->need_get_agent_list == 1 && agent_getInfo()->agent_status == 1)
+ {
+ iRet = ZJ_GetAgentList(ZJ_GetOneReqId(), agent_get_list_cb);
+ if (iRet == 0)
+ {
+ agent_getInfo()->need_get_agent_list = 0;
+ }
+ }
+#if 0
+ if(agent_getInfo()->need_get_history_sessions == 1 && system_getInfo()->uiSdkServerStatus == EN_ZJ_SERVER_STATUS_PROCESS)
+ {
+ iRet = ZJ_GetHistorySessions(ZJ_GetOneReqId(), agent_get_history_list_cb);
+ if (iRet == 0)
+ {
+ agent_getInfo()->need_get_history_sessions = 0;
+ }
+ }
+#endif
+#ifndef WIN_TEST
+ usleep(20 * 1000);
+#else
+ Sleep(20);
+#endif
+ }
+ return NULL;
+}
+
+void agent_start()
+{
+ pthread_t pid;
+ if(agent_info.bRunFlag == 1) return;
+ agent_info.bRunFlag = 1;
+ if(pthread_create(&pid, NULL, agent_task_loop, NULL) != 0)
+ {
+ agent_info.bRunFlag = 0;
+ return;
+ }
+ pthread_detach(pid);
+ printf("agent start ok \n");
+ return;
+}
+
+void agent_stop()
+{
+
+}
+
+void agent_destory()
+{
+
+}
+
+/*******************************************************************************************************/
+static int agent_get_list_cb(int iReqId, ST_ZJ_AIAGENTLIST* pstAiAgentList, int iRspCode)
+{
+ ST_ZJ_AIAGENTLIST* pstTmpNode = NULL;
+ do
+ {
+ if (iRspCode != 0) break;
+ while (pstAiAgentList)
+ {
+ pstTmpNode = pstAiAgentList;
+ pstAiAgentList = pstAiAgentList->pstNext;
+ printf("/*******************agent_get_list_cb*******************/\n");
+ printf("AgentID:\t%u\n", pstTmpNode->uiAgentID);
+ printf("AgentName:\t%s\n", pstTmpNode->aucAgentName);
+ if (pstTmpNode->pucDescribe)
+ {
+ printf("AgentDes:\t%s\n", pstTmpNode->pucDescribe);
+ }
+ printf("AgentLanguage:\t%s\n", pstTmpNode->aucLanguage);
+ if (pstTmpNode->pucUiInterTemplate)
+ {
+ printf("UiInterTemplate:\t%s\n", pstTmpNode->pucUiInterTemplate);
+ }
+ if (pstTmpNode->pucPrompt)
+ {
+ printf("AgentPrompt:\t%s\n", pstTmpNode->pucPrompt);
+ }
+ if (pstTmpNode->pucPromptTemplate)
+ {
+ printf("AgentPromptTemplate:\t%s\n", pstTmpNode->pucPromptTemplate);
+ }
+ if (pstTmpNode->pucPrologue)
+ {
+ printf("AgentPrologue:\t%s\n", pstTmpNode->pucPrologue);
+ }
+ if (pstTmpNode->aucBackgroundPicUrl)
+ {
+ printf("AgentBackgroundPicUrl:\t%s\n", pstTmpNode->aucBackgroundPicUrl);
+ }
+ printf("AgentVoiceType:\t%u\n", pstTmpNode->uiVoiceType);
+ printf("AgentAbility:\t%u\n", pstTmpNode->uiAgentAbility);
+ printf("AgentDefaultFlag:\t%d\n", pstTmpNode->ucDefaultFlag);
+ for (int i = 0; i < pstTmpNode->stFileAbility.uiFormatCount; i++)
+ {
+ printf("AgentFormat[%d]:\t%s\n", i, pstTmpNode->stFileAbility.pucFileFormats[i]);
+ }
+ printf("AgentFormatSize:\t%u\n", pstTmpNode->stFileAbility.uiFileSize);
+ for (int i = 0; i < pstTmpNode->stProloguePresetProblem.uiCount; i++)
+ {
+ printf("AgentProloguePresetProblem[%d]:\t%s\n", i, pstTmpNode->stProloguePresetProblem.pucStr[i]);
+ }
+ printf("\n\n");
+ }
+ } while (0);
+
+ // 模拟创建session
+ //agent_getInfo()->need_create_session = 1;
+
+ agent_getInfo()->need_create_ques_thread = 1;
+
+ return 0;
+}
+
+static int agent_get_history_list_cb(int iReqId, ST_ZJ_HISTORYSESSION* pstHistorySessionList, int iRspCode)
+{
+ ST_ZJ_HISTORYSESSION* pstTmpNode = NULL;
+ do
+ {
+ if(iRspCode != 0) return -1;
+ while(pstHistorySessionList)
+ {
+ pstTmpNode = pstHistorySessionList;
+ pstHistorySessionList = pstHistorySessionList->pstNext;
+
+ printf("/*******************agent_get_history_list_cb*******************/\n");
+ printf("SessionID:\t%s\n",pstTmpNode->pucSessionID);
+ printf("AgentID:\t%s\n",pstTmpNode->pucAgentID);
+ printf("Title:\t%s\n",pstTmpNode->pucTitle);
+ printf("\n\n");
+ }
+ }while(0);
+
+ return 0;
+}
+
+static int agent_download_audio_trans_text_cb(unsigned int uiReqID, unsigned char *pucTextBuff, unsigned int uiBuffLen, int bFinished, int iRspCode)
+{
+ if(iRspCode != 0)
+ {
+ printf("agent_download_audio_trans_text_cb ReqID %u Code %d \n", uiReqID, iRspCode);
+ return -1;
+ }
+
+ if (bFinished)
+ {
+ #ifndef FULL_DUPLEX
+ agent_getInfo()->need_send_voice_ques = 1;
+ #endif
+ printf("reqid %u recv tras text %s \n", uiReqID, agent_getInfo()->aucTrasText);
+ }
+
+ if (uiBuffLen != 0)
+ {
+ strncpy(agent_getInfo()->aucTrasText + agent_getInfo()->uiOffset, pucTextBuff, uiBuffLen);
+ agent_getInfo()->uiOffset += uiBuffLen;
+ //printf("recv sdk tras data - %s \n", agent_getInfo()->aucTrasText);
+ }
+ return 0;
+}
+
+// 语音翻译文本忽略pucSessionID 和 pucTurnID
+int agent_trans_output(unsigned char* pucSessionID, unsigned char* pucTurnID, unsigned char* pucTextUrl, int iRspCode)
+{
+ printf("recv audio trans text url [%s] code %d \n",pucTextUrl, iRspCode);
+ if (iRspCode == 0)
+ {
+ ZJ_UrlDownloadTextData(ZJ_GetOneReqId(), pucTextUrl, agent_download_audio_trans_text_cb);
+ memset(agent_getInfo()->aucTrasText, 0, sizeof(agent_getInfo()->aucTrasText));
+ agent_getInfo()->uiOffset = 0;
+ }
+ return 0;
+}
+
+static int agent_text_data_cb(unsigned int uiReqID, unsigned char* pucTextBuff, unsigned int uiBuffLen, int bFinished, int iRspCode)
+{
+ unsigned char *pucTmpBuff = (unsigned char *)malloc(uiBuffLen + 1);
+ memset(pucTmpBuff, 0, uiBuffLen + 1);
+ memcpy(pucTmpBuff, pucTextBuff, uiBuffLen);
+ printf("reqID %u text %s len %d bFinished %d Rsp %d \n", uiReqID, pucTmpBuff, uiBuffLen, bFinished, iRspCode);
+ free(pucTmpBuff);
+ return 0;
+}
+
+static FILE* fp = NULL;
+static unsigned int uiAudioDataLen = 0;
+static int agent_audio_data_cb(unsigned int uiReqID, unsigned char *pucPackage,unsigned int uiPacklen, int bFinished, int iRspCode)
+{
+ if (iRspCode != 0) return -1;
+ //printf("reqID %u audio len %d bFinished %d Rsp %d \n", uiReqID, uiPacklen, bFinished, iRspCode);
+ uiAudioDataLen += uiPacklen;
+ if (fp == NULL)
+ {
+ if (_access("D:/SDK/truck-agent/Demo/resources/answer.pcm", 0) == 0)
+ {
+ remove("D:/SDK/truck-agent/Demo/resources/answer.pcm");
+ }
+ fp = fopen("D:/SDK/truck-agent/Demo/resources/answer.pcm", "wb");
+ if (fp == NULL)
+ {
+ printf("fopen err %d \n", errno);
+ }
+ }
+
+ if (bFinished)
+ {
+ fclose(fp);
+ fp = NULL;
+ printf("reqid %u recv all audio data len %u \n", uiReqID, uiAudioDataLen);
+ uiAudioDataLen = 0;
+ }
+ else
+ {
+ fwrite(pucPackage, 1, uiPacklen, fp);
+ printf("reqid %u recv audio data len %u \n", uiReqID, uiPacklen);
+ }
+
+ return 0;
+}
+
+int agent_result_output_text(unsigned char* pucSessionID, unsigned char* pucTurnID, unsigned char* pucTextUrl, int iRspCode)
+{
+ if (iRspCode == -1)
+ {
+ return -1;
+ }
+ printf("recv result session %s turn %s text url [%s]\n", pucSessionID, pucTurnID, pucTextUrl);
+ if (pucTurnID != NULL && strcmp(agent_getInfo()->turn_id, pucTurnID) != 0)
+ {
+ memset(agent_getInfo()->turn_id, 0, sizeof(agent_getInfo()->turn_id));
+ strncpy(agent_getInfo()->turn_id, pucTurnID, strlen(pucTurnID) + 1);
+ printf("new turnID %s \n", agent_getInfo()->turn_id);
+ }
+ if (agent_getInfo()->need_show_page1 == 0)
+ {
+ agent_getInfo()->need_show_page1 = 1;
+ }
+ agent_getInfo()->download_text_reqId = ZJ_GetOneReqId();
+ ZJ_UrlDownloadTextData(agent_getInfo()->download_text_reqId, pucTextUrl, agent_text_data_cb);
+ return 0;
+}
+
+int agent_result_output_audio(unsigned char* pucSessionID, unsigned char* pucTurnID, unsigned char* pucAudioUrl, int iRspCode)
+{
+ if (iRspCode == -1)
+ {
+ return -1;
+ }
+ printf("recv result audio url [%s]\n", pucAudioUrl);
+ if (pucTurnID != NULL && strcmp(agent_getInfo()->turn_id, pucTurnID) != 0)
+ {
+ memset(agent_getInfo()->turn_id, 0, sizeof(agent_getInfo()->turn_id));
+ strncpy(agent_getInfo()->turn_id, pucTurnID, strlen(pucTurnID) + 1);
+ printf("new turnID %s \n", agent_getInfo()->turn_id);
+ }
+#if 0
+ if (agent_getInfo()->need_show_page1 == 0)
+ {
+ agent_getInfo()->need_show_page1 = 1;
+ }
+#endif
+ agent_getInfo()->download_audio_reqId = ZJ_GetOneReqId();
+
+ ZJ_UrlDownloadAudioData(agent_getInfo()->download_audio_reqId, pucAudioUrl, agent_audio_data_cb);
+ return 0;
+}
+
+int agent_result_output_picture(unsigned char* pucSessionID, unsigned char* pucTurnID, unsigned char* pucPictureUrl, int iRspCode)
+{
+ printf("recv result pic url [%s]\n",pucPictureUrl);
+ if (iRspCode == -1)
+ {
+ return -1;
+ }
+ if (pucTurnID != NULL && strcmp(agent_getInfo()->turn_id, pucTurnID) != 0)
+ {
+ memset(agent_getInfo()->turn_id, 0, sizeof(agent_getInfo()->turn_id));
+ strncpy(agent_getInfo()->turn_id, pucTurnID, strlen(pucTurnID) + 1);
+ }
+#if 0
+ if (agent_getInfo()->need_show_page1 == 0)
+ {
+ agent_getInfo()->need_show_page1 = 1;
+ }
+#endif
+ return 0;
+}
\ No newline at end of file
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <sys/select.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <sys/ioctl.h>
+#include <sched.h>
+#include <sys/prctl.h>
+#include <unistd.h>
+
+#include "zj_type.h"
+#include "zj_err.h"
+#include "zj_syscall.h"
+
+// ÒÔÏÂÉùÃ÷µÄϵͳµ÷Óûص÷º¯Êý¾ùÓɳ§ÉÌʵÏÖ ³§ÉÌʵÏÖ±ê¼Ç
+/***********************************************************************************/
+static ZJ_HANDLE file_open_cb(unsigned char *pucFileName, unsigned int uiFlag);
+static int file_close_cb(ZJ_HANDLE hFile);
+static int file_rmv_cb(unsigned char *pucFileName);
+static int file_eof(ZJ_HANDLE hFile);
+static int file_write(ZJ_HANDLE hFile, unsigned char *pucBuf, int iLen);
+static int file_read(ZJ_HANDLE hFile, unsigned char *pucBuf, int iLen);
+static int file_flush(ZJ_HANDLE hFile);
+static int file_seek(ZJ_HANDLE hFile, unsigned short usFlag, int iOffSet);
+static int file_size(ZJ_HANDLE hFile);
+static int file_tell(ZJ_HANDLE hFile);
+static int file_stat(unsigned char *pucFileName, ST_COS_FILE_INF *pstInf);
+static ZJ_HANDLE dir_open(unsigned char *pucDirName);
+static int dir_close(ZJ_HANDLE hDir);
+static int dir_read(ZJ_HANDLE hDir, unsigned char *pucEntryName, int iMaxLen, ST_COS_FILE_INF *pstInf);
+static int dir_make(unsigned char *pucDirName, unsigned int uiFlag);
+/***********************************************************************************/
+static int mutex_create(void **phOutMutex);
+static void mutex_delete(void *phMutex);
+static int mutex_lock(void *phMutex);
+static int mutex_unlock(void *phMutex);
+
+static int sem_create(void **phOutSem);
+static void sem_delete(void *phSem);
+static int sem_s_wait(void *phSem);
+static int sem_s_post(void *phSem);
+/***********************************************************************************/
+static int inet_get_addr(unsigned char *pucAddrStr, unsigned int *puiDstAddr);
+static unsigned int inet_net_to_host_long(unsigned int uiVal);
+static unsigned int inet_host_to_net_long(unsigned int uiVal);
+static unsigned short inet_net_to_host_short(unsigned short usVal);
+static unsigned short inet_host_to_net_short(unsigned short usVal);
+static int inet_fd_create(void **pFds);
+static int inet_fd_delete(void * fds);
+static int inet_fd_zero(void *fds);
+static int inet_fd_set(int iFd, void *fds);
+static int inet_fd_is_set(int iFd, void *fds);
+static int inet_fd_clr(int iFd, void *fds);
+static int inet_get_host_by_name(unsigned char *pucName, ST_COS_INET_IPARRAY *pstIpArrayInfo);
+static int inet_get_addr_info(unsigned char *pucHostName, unsigned short usPort, int iProtoType, int bPassive, ST_COS_INET_IPARRAY *pstIpArrayInfo);
+static int inet_socket_open(int iNetType, int iProtoType, int bBlock, int bReuseAddr);
+static int inet_socket_close(int sock);
+static int inet_socket_shutdown(int sock, int iHowTo);
+static int inet_socket_select(int imaxFd, void * readFds, void * writeFds, void * exceptFds, int iMillSecWait);
+static int inet_socket_connect(int sock, ST_COS_INET_IP *pstRmtAddr,int *bConnect);
+static int inet_socket_sendTo(int sock, ST_COS_INET_IP *pstRmtAddr, void * pBuf, int ilen, int *pbOutWait);
+static int inet_socket_recvfrom(int sock, ST_COS_INET_IP *pstIORmtAddr, void * pOutBuf, int iLen,int *pbOutClosed);
+static int inet_socket_send(int sock, void * pBuf, int iLen, int *pbOutWait);
+static int inet_socket_recv(int sock, void * pOutBuf, int iLen, int *pbOutClosed);
+static int inet_socket_setRecvBuff(int sock, int iBufSize);
+static int inet_socket_setSendBuff(int sock, int iBufSize);
+static int inet_socket_getLastErr(void);
+static int inet_socket_checkConn(int sock, int *piErr);
+/***********************************************************************************/
+static int thread_create(unsigned char *pucName, int iPriority, unsigned int uiStackSize, PFN_THREADPROC pfunProc, void *pParam, void **phOutOSThread);
+static int thread_delete(void *hOSThread, int bForce);
+static int thread_sleep(unsigned int uiMilliSecond);
+static int thread_SetName(unsigned char *pucName);
+static unsigned int thread_GetCurID(void);
+/***********************************************************************************//***********************************************************************************/
+static time_t time_getTime(time_t *ptmTime);
+static struct tm* time_localTime(time_t *ptmTime,struct tm *ptTm);
+static unsigned int time_getTickCount(void);
+static time_t time_sysTimeToTime(ST_COS_SYS_TIME *pstTime);
+
+
+void syscall_init()
+{
+ // file
+ ZJ_SetFileOpenCb(file_open_cb);
+
+ ZJ_SetFileCloseCb(file_close_cb);
+
+ ZJ_SetFileRmvCb(file_rmv_cb);
+
+ ZJ_SetFileEofCb(file_eof);
+
+ ZJ_SetFileWriteCb(file_write);
+
+ ZJ_SetFileReadCb(file_read);
+
+ ZJ_SetFileFlushCb(file_flush);
+
+ ZJ_SetFileSeekCb(file_seek);
+
+ ZJ_SetFileSizeCb(file_size);
+
+ ZJ_SetFileTellCb(file_tell);
+
+ ZJ_SetFileStatCb(file_stat);
+
+ ZJ_SetDirOpenCb(dir_open);
+
+ ZJ_SetDirCloseCb(dir_close);
+
+ ZJ_SetDirReadCb(dir_read);
+
+ ZJ_SetDirMakeCb(dir_make);
+
+ // mutex sem
+ ZJ_SetMutexCreateCb(mutex_create);
+
+ ZJ_SetMutexDeleteCb(mutex_delete);
+
+ ZJ_SetMutexLockCb(mutex_lock);
+
+ ZJ_SetMutexUnLockCb(mutex_unlock);
+
+ ZJ_SetSemCreateCb(sem_create);
+
+ ZJ_SetSemDeleteCb(sem_delete);
+
+ ZJ_SetSemWaitCb(sem_s_wait);
+
+ ZJ_SetSemPostCb(sem_s_post);
+
+ // socket
+
+ ZJ_SetInetAddrCb(inet_get_addr);
+
+ ZJ_SetInetNtoHLCb(inet_net_to_host_long);
+
+ ZJ_SetInetHtoNLCb(inet_host_to_net_long);
+
+ ZJ_SetInetNtoHSCb(inet_net_to_host_short);
+
+ ZJ_SetInetHtoNSCb(inet_host_to_net_short);
+
+ ZJ_SetInetFdCreateCb(inet_fd_create);
+
+ ZJ_SetInetFdDeleteCb(inet_fd_delete);
+
+ ZJ_SetInetFdZeroCb(inet_fd_zero);
+
+ ZJ_SetInetFdSetCb(inet_fd_set);
+
+ ZJ_SetInetFdIsSetCb(inet_fd_is_set);
+
+ ZJ_SetInetFdClearCb(inet_fd_clr);
+
+ ZJ_SetInetGetHostByNameCb(inet_get_host_by_name);
+
+ ZJ_SetInetGetAddrInfoCb(inet_get_addr_info);
+
+ ZJ_SetInetSocketOpenCb(inet_socket_open);
+
+ ZJ_SetInetSocketCloseCb(inet_socket_close);
+
+ ZJ_SetInetSocketShutDownCb(inet_socket_shutdown);
+
+ ZJ_SetInetSocketSelectCb(inet_socket_select);
+
+ ZJ_SetInetSocketConnectCb(inet_socket_connect);
+
+ ZJ_SetInetSocketSendToCb(inet_socket_sendTo);
+
+ ZJ_SetInetSocketRecvFromCb(inet_socket_recvfrom);
+
+ ZJ_SetInetSocketSendCb(inet_socket_send);
+
+ ZJ_SetInetSocketRecvCb(inet_socket_recv);
+
+ ZJ_SetInetSocketSetRecvBufCb(inet_socket_setRecvBuff);
+
+ ZJ_SetInetSocketSetSendBufCb(inet_socket_setSendBuff);
+
+ ZJ_SetInetSocketGetLastErrCb(inet_socket_getLastErr);
+
+ ZJ_SetInetSocketCheckConnCb(inet_socket_checkConn);
+ // thread
+ ZJ_SetThreadCreateCb(thread_create);
+
+ ZJ_SetThreadDeleteCb(thread_delete);
+
+ ZJ_SetThreadGetCurIdCb(thread_GetCurID);
+
+ ZJ_SetThreadSetNameCb(thread_SetName);
+
+ ZJ_SetSleepCb(thread_sleep);
+ // time
+ ZJ_SetTimeGetTimeCb(time_getTime);
+
+ ZJ_SetTimeGetLocalTimeCb(time_localTime);
+
+ ZJ_SetTimeGetTickCountCb(time_getTickCount);
+
+ ZJ_SetTimeSysTimetoTimeCb(time_sysTimeToTime);
+}
+
+void syscall_start()
+{
+
+}
+
+/***********************************************************************************/
+static ZJ_HANDLE file_open_cb(unsigned char *pucFileName, unsigned int uiFlag)
+{
+ unsigned char aucOpenType[10];
+ unsigned int uiLen = 0;
+ FILE *pstFile = NULL;
+
+ if (uiFlag & COS_FILE_O_RDONLY)
+ {
+ aucOpenType[uiLen++] = 'r';
+ }
+ else if(uiFlag & COS_FILE_O_WRONLY)
+ {
+ if(uiFlag & COS_FILE_O_APPEND)
+ {
+ aucOpenType[uiLen++] = 'a';
+ }
+ else
+ {
+ aucOpenType[uiLen++] = 'w';
+ }
+ }
+ else if(uiFlag & COS_FILE_O_RDWR)
+ {
+ if((uiFlag&COS_FILE_O_TRUNC) || (uiFlag&COS_FILE_O_CREAT))
+ {
+ aucOpenType[uiLen++] = 'w';
+ }
+ else if(uiFlag & COS_FILE_O_APPEND)
+ {
+ aucOpenType[uiLen++] = 'a';
+ }
+ else
+ {
+ aucOpenType[uiLen++] = 'r';
+ }
+ aucOpenType[uiLen++] = '+';
+ }
+ else
+ {
+ return NULL;
+ }
+
+ if (uiFlag & COS_FILE_O_BIN)
+ {
+ aucOpenType[uiLen++] = 'b';
+ }
+ aucOpenType[uiLen] = '\0';
+
+ pstFile = fopen((char*)pucFileName, (char *)aucOpenType);
+ return pstFile;
+}
+
+static int file_close_cb(ZJ_HANDLE hFile)
+{
+ return fclose((FILE *)hFile);
+}
+
+static int file_rmv_cb(unsigned char *pucFileName)
+{
+ if (unlink((char *)pucFileName) != 0)
+ {
+ return EN_ZJ_ERR;
+ }
+
+ return EN_ZJ_ERR_OK;
+}
+
+static int file_eof(ZJ_HANDLE hFile)
+{
+ if (feof((FILE *)hFile) != 0)
+ return 1;
+
+ return EN_ZJ_ERR_OK;
+}
+
+static int file_write(ZJ_HANDLE hFile, unsigned char *pucBuf, int iLen)
+{
+ if (!hFile || !pucBuf || !iLen)
+ return EN_ZJ_ERR_PARAM;
+
+ return fwrite((char *)pucBuf, sizeof(char), (size_t)iLen, (FILE *)hFile);
+}
+
+static int file_read(ZJ_HANDLE hFile, unsigned char *pucBuf, int iLen)
+{
+ if (!hFile || !pucBuf || !iLen)
+ return EN_ZJ_ERR_PARAM;
+
+ return fread((char *)pucBuf, sizeof(char), iLen,(FILE *)hFile);
+}
+
+static int file_flush(ZJ_HANDLE hFile)
+{
+ if (fflush((FILE *)hFile) == EOF)
+ return EN_ZJ_ERR;
+}
+
+static int file_seek(ZJ_HANDLE hFile, unsigned short usFlag, int iOffSet)
+{
+ int iOrigin;
+
+ switch(usFlag)
+ {
+ case COS_FILE_SEEK_CUR:
+ iOrigin = SEEK_CUR;
+ break;
+ case COS_FILE_SEEK_BEGIN:
+ iOrigin = SEEK_SET;
+ break;
+ case COS_FILE_SEEK_END:
+ iOrigin = SEEK_END;
+ break;
+ default:
+ return EN_ZJ_ERR_PARAM;
+ }
+
+ return fseek((FILE *)hFile, iOffSet, iOrigin);
+}
+
+static int file_size(ZJ_HANDLE hFile)
+{
+ unsigned int uiCurPos = 0;
+ unsigned int uiSize = 0;
+
+ uiCurPos = file_tell(hFile);
+ file_seek(hFile, COS_FILE_SEEK_END, 0);
+ uiSize = file_tell(hFile);
+ file_seek(hFile, COS_FILE_SEEK_BEGIN, uiCurPos);
+
+ return uiSize;
+}
+
+static int file_tell(ZJ_HANDLE hFile)
+{
+ return ftell((FILE *)hFile);
+}
+
+static int file_stat(unsigned char *pucFileName, ST_COS_FILE_INF *pstInf)
+{
+ struct stat stStatInfo;
+
+ memset(pstInf,0,sizeof(ST_COS_FILE_INF));
+
+ if (stat((char *)pucFileName, &stStatInfo) != 0)
+ {
+ return EN_ZJ_ERR;
+ }
+
+ switch (stStatInfo.st_mode & S_IFMT)
+ {
+ case S_IFREG:
+ pstInf->ucType = EN_CFILE_REG;
+ break;
+ case S_IFDIR:
+ pstInf->ucType = EN_CFILE_DIR;
+ break;
+ case S_IFLNK:
+ pstInf->ucType = EN_CFILE_LINK;
+ break;
+ case S_IFBLK:
+ pstInf->ucType = EN_CFILE_BLK;
+ break;
+ case S_IFCHR:
+ pstInf->ucType = EN_CFILE_CHR;
+ break;
+ case S_IFIFO:
+ pstInf->ucType = EN_CFILE_PIPE;
+ break;
+ case S_IFSOCK:
+ pstInf->ucType = EN_CFILE_SOCK;
+ break;
+ default:
+ pstInf->ucType = EN_CFILE_UNKNOW;
+ break;
+ }
+
+ if (stStatInfo.st_mode & S_IRUSR)
+ pstInf->uiMode |= COS_FILE_UREAD;
+ if (stStatInfo.st_mode & S_IWUSR)
+ pstInf->uiMode |= COS_FILE_UWRITE;
+ if (stStatInfo.st_mode & S_IXUSR)
+ pstInf->uiMode |= COS_FILE_UEXEC;
+
+ if (stStatInfo.st_mode & S_IRGRP)
+ pstInf->uiMode |= COS_FILE_GREAD;
+ if (stStatInfo.st_mode & S_IWGRP)
+ pstInf->uiMode |= COS_FILE_GWRITE;
+ if (stStatInfo.st_mode & S_IXGRP)
+ pstInf->uiMode |= COS_FILE_GEXEC;
+
+ if (stStatInfo.st_mode & S_IROTH)
+ pstInf->uiMode |= COS_FILE_OREAD;
+ if (stStatInfo.st_mode & S_IWOTH)
+ pstInf->uiMode |= COS_FILE_OWRITE;
+ if (stStatInfo.st_mode & S_IXOTH)
+ pstInf->uiMode |= COS_FILE_OEXEC;
+
+
+ pstInf->uiSize = (unsigned int)(unsigned long long)(stStatInfo.st_size);
+ pstInf->utlastAccTime = stStatInfo.st_atime;
+ pstInf->utlastMdfyTime = stStatInfo.st_mtime;
+ pstInf->utlastChangeTime = stStatInfo.st_ctime;
+
+ return 0;
+}
+
+static ZJ_HANDLE dir_open(unsigned char *pucDirName)
+{
+ ST_DIR *pstDir;
+
+ pstDir = (ST_DIR *)malloc(sizeof(ST_DIR));
+ if(!pstDir) return NULL;
+
+ strncpy((char *)pstDir->aucName, (char *)pucDirName, 256);
+ pstDir->pstDir = opendir((char *)pucDirName);
+ if (!pstDir->pstDir)
+ {
+ free(pstDir);
+ }
+
+ return pstDir;
+}
+
+static int dir_close(ZJ_HANDLE hDir)
+{
+ ST_DIR *pstDir;
+
+ pstDir = (ST_DIR *)hDir;
+
+ if (pstDir && pstDir->pstDir)
+ closedir(pstDir->pstDir);
+
+ free(pstDir);
+ return 0;
+}
+
+static int dir_read(ZJ_HANDLE hDir, unsigned char *pucEntryName, int iMaxLen, ST_COS_FILE_INF *pstInf)
+{
+ unsigned char aucAbsPath[256] = {0};
+ unsigned char *pucName;
+ ST_DIR *pstDir;
+ struct dirent *pstDirent;
+ if(!hDir) return EN_ZJ_ERR_PARAM;
+
+ pstDir = (ST_DIR *)hDir;
+
+
+RETRY:
+ pstDir->pstEntry = (struct dirent *)readdir((DIR *)pstDir->pstDir);
+ if(!pstDir->pstEntry) return EN_ZJ_ERR;
+ pstDirent = (struct dirent *)pstDir->pstEntry;
+ pucName = (unsigned char *)pstDirent->d_name;
+
+ if ((!strcmp((char *)pucName,".")) || (!strcmp((char *)pucName,"..")))
+ {
+ goto RETRY;
+ }
+ strncpy(pucEntryName, pucName, iMaxLen);
+ if (pstInf)
+ {
+ snprintf(aucAbsPath, 256 - 1, (unsigned char *)"%s/%s", pstDir->aucName, pucName);
+ return file_stat(aucAbsPath, pstInf);
+ }
+
+ return 0;
+}
+
+static int dir_make(unsigned char *pucDirName, unsigned int uiFlag)
+{
+ mode_t tMode = 0;
+
+ if (uiFlag & COS_FILE_UREAD) tMode |= S_IRUSR;
+ if (uiFlag & COS_FILE_UWRITE) tMode |= S_IWUSR;
+ if (uiFlag & COS_FILE_UEXEC) tMode |= S_IXUSR;
+
+ if (uiFlag & COS_FILE_GREAD) tMode |= S_IRGRP;
+ if (uiFlag & COS_FILE_GWRITE) tMode |= S_IWGRP;
+ if (uiFlag & COS_FILE_GEXEC) tMode |= S_IXGRP;
+
+ if (uiFlag & COS_FILE_OREAD) tMode |= S_IROTH;
+ if (uiFlag & COS_FILE_OWRITE) tMode |= S_IWOTH;
+ if (uiFlag & COS_FILE_OEXEC) tMode |= S_IXOTH;
+
+ if (mkdir((char *)pucDirName, tMode) != 0)
+ {
+ return EN_ZJ_ERR;
+ }
+ return 0;
+}
+/***********************************************************************************/
+static int mutex_create(void **phOutMutex)
+{
+ if (phOutMutex == NULL) return -1;
+
+ pthread_mutex_t *pMutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
+ if (!pMutex) return -1;
+
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init(pMutex, &attr);
+ pthread_mutexattr_destroy(&attr);
+
+ *phOutMutex = (void *)pMutex;
+ return 0;
+}
+
+static void mutex_delete(void *phMutex)
+{
+ if (!phMutex) return;
+
+ pthread_mutex_t *pMutex = (pthread_mutex_t *)phMutex;
+ pthread_mutex_destroy(pMutex);
+ free(pMutex);
+}
+
+static int mutex_lock(void *phMutex)
+{
+ if (!phMutex) return -1;
+ return pthread_mutex_lock((pthread_mutex_t *)phMutex);
+}
+
+static int mutex_unlock(void *phMutex)
+{
+ if (!phMutex) return -1;
+ return pthread_mutex_unlock((pthread_mutex_t *)phMutex);
+}
+
+static int sem_create(void **phOutSem)
+{
+ if (!phOutSem) return -1;
+
+ sem_t *pSem = (sem_t *)malloc(sizeof(sem_t));
+ if (!pSem) return EN_ZJ_ERR;
+
+ if (sem_init(pSem, 0, 0) != 0) { // 0 表示进程内使用,初值为 0
+ free(pSem);
+ return EN_ZJ_ERR;
+ }
+
+ *phOutSem = (void *)pSem;
+ return 0;
+}
+
+static void sem_delete(void *phSem)
+{
+ if (!phSem) return;
+
+ sem_t *pSem = (sem_t *)phSem;
+ sem_destroy(pSem);
+ free(pSem);
+}
+
+static int sem_s_wait(void *phSem)
+{
+ if (!phSem) return -1;
+ return sem_wait((sem_t *)phSem);
+}
+
+static int sem_s_post(void *phSem)
+{
+ if (!phSem) return -1;
+ return sem_post((sem_t *)phSem);
+}
+/***********************************************************************************/
+static int inet_get_addr(unsigned char *pucAddrStr, unsigned int *puiDstAddr)
+{
+ unsigned int uiAddr;
+ uiAddr = inet_addr((char *)pucAddrStr);
+ *puiDstAddr = (unsigned int)uiAddr;
+
+ if (*puiDstAddr == 0XFFFFFFFF)
+ {
+ if(strcmp(pucAddrStr,"255.255.255.255"))
+ return EN_ZJ_ERR;
+ }
+ return 0;
+}
+
+static unsigned int inet_net_to_host_long(unsigned int uiVal)
+{
+ return ntohl(uiVal);
+}
+
+static unsigned int inet_host_to_net_long(unsigned int uiVal)
+{
+ return htonl(uiVal);
+}
+
+static unsigned short inet_net_to_host_short(unsigned short usVal)
+{
+ return ntohs(usVal);
+}
+
+static unsigned short inet_host_to_net_short(unsigned short usVal)
+{
+ return htons(usVal);
+}
+
+static int inet_fd_create(void **pFds)
+{
+ *pFds = (void **)malloc(sizeof(fd_set));
+ return *pFds ? 0 : EN_ZJ_ERR;
+}
+
+static int inet_fd_delete(void *fds)
+{
+ free(fds);
+ return 0;
+}
+
+static int inet_fd_zero(void *fds)
+{
+ FD_ZERO((fd_set *)fds);
+ return 0;
+}
+
+static int inet_fd_set(int iFd, void *fds)
+{
+ FD_SET((unsigned int)iFd, (fd_set *)fds);
+ return 0;
+}
+
+static int inet_fd_is_set(int iFd, void *fds)
+{
+ return (FD_ISSET(iFd, (fd_set *)fds)) ? 1 : 0;
+}
+
+static int inet_fd_clr(int iFd, void *fds)
+{
+ FD_CLR(iFd, (fd_set *)fds);
+ return 0;
+}
+
+static int inet_get_host_by_name(unsigned char *pucName, ST_COS_INET_IPARRAY *pstIpArrayInfo)
+{
+ struct hostent *pstHostid;
+ ST_COS_INET_IP *pstIp;
+ int i = 0;
+
+ pstIpArrayInfo->uiCount = 0;
+ pstHostid = gethostbyname((char *)pucName);
+ if (!pstHostid || pstHostid->h_addrtype != AF_INET)
+ return EN_ZJ_ERR;
+
+ while(i < COS_INET_ADDR_MAX_NUM && pstHostid->h_addr_list[i])
+ {
+ pstIp = &pstIpArrayInfo->astIps[pstIpArrayInfo->uiCount++];
+ pstIp->usType = EN_CINET_TYPE_IPV4;
+ pstIp->u.uiIp = *((unsigned int *)pstHostid->h_addr_list[i]);
+ i++;
+ }
+
+ return 1;
+}
+
+static int inet_get_addr_info(unsigned char *pucHostName, unsigned short usPort, int iProtoType, int bPassive, ST_COS_INET_IPARRAY *pstIpArrayInfo)
+{
+ int iRet;
+ unsigned char aucService[8];
+ unsigned char *pucService = NULL;
+ struct addrinfo *pstAddr = NULL, *pstCur;
+ struct addrinfo aiHint;
+ ST_COS_INET_IP *pstIp;
+
+ memset(&aiHint, 0, sizeof(aiHint));
+
+ aiHint.ai_family = AF_UNSPEC;
+ if(bPassive == 1)
+ {
+ aiHint.ai_flags = AI_PASSIVE;
+ snprintf(aucService, 8, "%u",usPort);
+ pucService = aucService;
+ }
+
+ pstIpArrayInfo->uiCount = 0;
+ iRet = getaddrinfo((char *)pucHostName, pucService, &aiHint, &pstAddr);
+ if (iRet != 0)
+ {
+ if(pstAddr)
+ {
+ freeaddrinfo(pstAddr);
+ }
+ if(pucHostName)
+ {
+ printf("get host %s failed errno %u\n",pucHostName,errno);
+ }
+ return EN_ZJ_ERR;
+ }
+
+ for (pstCur = pstAddr; pstIpArrayInfo->uiCount < COS_INET_ADDR_MAX_NUM && pstCur; pstCur = pstCur->ai_next)
+ {
+ pstIp = &pstIpArrayInfo->astIps[pstIpArrayInfo->uiCount];
+ pstIp->usPort = usPort;
+ if(pstCur->ai_family == AF_INET){
+ pstIp->usType = EN_CINET_TYPE_IPV4;
+ pstIp->u.uiIp = ((struct sockaddr_in *)(pstCur->ai_addr))->sin_addr.s_addr;
+ }else if(pstCur->ai_family == AF_INET6){
+ pstIp->usType = EN_CINET_TYPE_IPV6;
+ memcpy(pstIp->u.aucIpv6, &(((struct sockaddr_in6*)(pstCur->ai_addr))->sin6_addr), 16);
+ }
+ pstIpArrayInfo->uiCount++;
+ }
+
+ freeaddrinfo(pstAddr);
+
+ return pstIpArrayInfo->uiCount > 0 ? 0 : EN_ZJ_ERR;
+}
+
+static int inet_SocketSetOptBlk(int sock, int bBlock)
+{
+ int iDisable = 0;
+ int iEnable = 1;
+ int iRet;
+
+ if (bBlock)
+ iRet = ioctl(sock, FIONBIO, &iDisable);
+ else
+ iRet = ioctl(sock, FIONBIO,&iEnable);
+
+ if (iRet == -1)
+ {
+ return EN_ZJ_ERR;
+ }
+ return 0;
+}
+static int Clin_SocketSetOptTtl(int sock, unsigned int uiTtlVal)
+{
+ int iRet;
+ int iTtlVal;
+
+ iTtlVal = (int)(unsigned long)(uiTtlVal);
+ iRet = setsockopt(sock, IPPROTO_IP, IP_TTL, (char *)&iTtlVal, sizeof(iTtlVal));
+
+ if(iRet == -1)
+ {
+ return EN_ZJ_ERR;
+ }
+
+ return 0;
+}
+static int Clin_SocketSetOptReuseAddr(int sock, int bReuseAddr)
+{
+ int bDisable = 0;
+ int bEnable = 1;
+ int iRet;
+
+ if (bReuseAddr)
+ iRet = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&bEnable, sizeof(bEnable));
+ else
+ iRet = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&bDisable, sizeof(bDisable));
+
+ if (iRet == -1)
+ {
+ return EN_ZJ_ERR;
+ }
+ return 0;
+}
+static int inet_socket_open(int iNetType, int iProtoType, int bBlock, int bReuseAddr)
+{
+ int iSockType;
+ int cSock;
+
+ if (iNetType == EN_CINET_TYPE_IPV4)
+ iNetType = AF_INET;
+ else if (iNetType == EN_CINET_TYPE_IPV6)
+ iNetType = AF_INET6;
+ else
+ return EN_ZJ_ERR;
+
+ if (iProtoType == EN_CINET_PRTL_UDP)
+ {
+ iProtoType = IPPROTO_UDP;
+ iSockType = SOCK_DGRAM;
+ }
+ else if (iProtoType == EN_CINET_PRTL_TCP)
+ {
+ iProtoType = IPPROTO_TCP;
+ iSockType = SOCK_STREAM;
+ }
+ else if (iProtoType == EN_CINET_PRTL_RAW)
+ {
+ iProtoType = IPPROTO_ICMP;
+ iSockType = SOCK_RAW;
+ }
+ else
+ return EN_ZJ_ERR;
+
+ cSock = socket(iNetType, iSockType, iProtoType);
+ if (cSock == -1)
+ {
+ return EN_ZJ_ERR;
+ }
+ inet_SocketSetOptBlk(cSock, bBlock);
+
+ if (iProtoType != EN_CINET_PRTL_RAW)
+ {
+ Clin_SocketSetOptTtl(cSock, COS_INET_DEFAULT_TTL);
+ Clin_SocketSetOptReuseAddr(cSock, bReuseAddr);
+ }
+
+ return cSock;
+}
+
+static int inet_socket_close(int sock)
+{
+ int iRet;
+ iRet = close(sock);
+ if(iRet == -1)
+ {
+ return EN_ZJ_ERR;
+ }
+ return 0;
+}
+
+static int inet_socket_shutdown(int sock, int iHowTo)
+{
+ int iRet;
+
+ if (iHowTo == EN_CINET_SHTDWN_RECV)
+ iHowTo = SHUT_RD;
+ else if(iHowTo == EN_CINET_SHTDWN_SEND)
+ iHowTo = SHUT_WR;
+ else if(iHowTo == EN_CINET_SHTDWN_BOTH)
+ iHowTo = SHUT_RDWR;
+
+ iRet = shutdown(sock, iHowTo);
+ if (iRet == -1 && errno != ENOTCONN)
+ {
+ return EN_ZJ_ERR;
+ }
+
+ return 0;
+}
+
+static int inet_socket_select(int imaxFd, void * readFds, void * writeFds, void * exceptFds, int iMillSecWait)
+{
+ struct timeval *pstTimeOut;
+ struct timeval stTimeout;
+ int iRet;
+
+ if (iMillSecWait)
+ {
+ stTimeout.tv_sec = (long)(iMillSecWait / 1000);
+ stTimeout.tv_usec = (iMillSecWait % 1000) * 1000;
+ pstTimeOut = &stTimeout;
+ }
+ else
+ {
+ pstTimeOut = NULL;
+ }
+ iRet = select(imaxFd, readFds, writeFds, exceptFds, pstTimeOut);
+
+ return iRet;
+}
+
+static int inet_socket_connect(int sock, ST_COS_INET_IP *pstRmtAddr,int *bConnect)
+{
+ struct sockaddr *pstSockAddr;
+ struct sockaddr_in6 stIn6Addr;
+ struct sockaddr_in stIn4Addr;
+ int iSize;
+ int iRet,iErrCode;
+ *bConnect = 0;
+ if (pstRmtAddr->usType == EN_CINET_TYPE_IPV4)
+ {
+ iSize = sizeof(struct sockaddr_in);
+ memset(&stIn4Addr, 0, iSize);
+ stIn4Addr.sin_family = AF_INET;
+ stIn4Addr.sin_port = htons(pstRmtAddr->usPort);
+ stIn4Addr.sin_addr.s_addr = pstRmtAddr->u.uiIp;
+ pstSockAddr = (struct sockaddr *)&stIn4Addr;
+ }
+ else if (pstRmtAddr->usType == EN_CINET_TYPE_IPV6)
+ {
+ iSize = sizeof(struct sockaddr_in6);
+ memset(&stIn6Addr, 0, iSize);
+ stIn6Addr.sin6_family = AF_INET6;
+ stIn6Addr.sin6_port = htons(pstRmtAddr->usPort);
+ memcpy(&stIn6Addr.sin6_addr, pstRmtAddr->u.aucIpv6, COS_INET_IPV6_ADDR_SIZE);
+ pstSockAddr = (struct sockaddr *)&stIn6Addr;
+ }
+ else
+ return EN_ZJ_ERR_PARAM;
+
+ iRet = connect(sock, pstSockAddr, iSize);
+ if (iRet == -1)
+ {
+ iErrCode = inet_socket_getLastErr();
+ if (iErrCode != EWOULDBLOCK && iErrCode != EISCONN
+ && iErrCode != EALREADY && iErrCode != EINPROGRESS)
+ {
+ return iRet;
+ }
+ if(iErrCode == EISCONN) *bConnect = 1;
+ }else{
+ *bConnect = 1;
+ }
+ return 0;
+}
+
+static int inet_socket_sendTo(int sock, ST_COS_INET_IP *pstRmtAddr, void * pBuf, int ilen, int *pbOutWait)
+{
+ struct sockaddr *pstSockAddr;
+ struct sockaddr_in6 stIn6Addr = {0};
+ struct sockaddr_in stIn4Addr = {0};
+ int iSize;
+ int iRet,iErrCode;
+
+ *pbOutWait = 0;
+
+ if (pstRmtAddr->usType == EN_CINET_TYPE_IPV4)
+ {
+ iSize = sizeof(struct sockaddr_in);
+ memset(&stIn4Addr, 0, iSize);
+ stIn4Addr.sin_family = AF_INET;
+ stIn4Addr.sin_port = htons(pstRmtAddr->usPort);
+ stIn4Addr.sin_addr.s_addr = pstRmtAddr->u.uiIp;
+ pstSockAddr = (struct sockaddr *)&stIn4Addr;
+ }
+ else if (pstRmtAddr->usType == EN_CINET_TYPE_IPV6)
+ {
+ iSize = sizeof(struct sockaddr_in6);
+ memset(&stIn6Addr, 0, iSize);
+ stIn6Addr.sin6_family = AF_INET6;
+ stIn6Addr.sin6_port = htons(pstRmtAddr->usPort);
+ memcpy(&stIn6Addr.sin6_addr, pstRmtAddr->u.aucIpv6, COS_INET_IPV6_ADDR_SIZE);
+ pstSockAddr = (struct sockaddr *)&stIn6Addr;
+ }
+ else
+ return EN_ZJ_ERR_PARAM;
+
+ iRet = sendto(sock, (char *)pBuf, ilen, 0, pstSockAddr, iSize);
+ if (iRet == -1)
+ {
+ iErrCode = inet_socket_getLastErr();
+ if (EINTR == iErrCode || EAGAIN == iErrCode || EWOULDBLOCK == iErrCode)
+ {
+ *pbOutWait = 1;
+ }
+ }
+ return iRet;
+}
+
+static int inet_socket_recvfrom(int sock, ST_COS_INET_IP *pstIORmtAddr, void * pOutBuf, int iLen,int *pbOutClosed)
+{
+ struct sockaddr *pstSockAddr = NULL;
+ struct sockaddr_in6 stIn6Addr;
+ struct sockaddr_in stIn4Addr;
+ int iSize;
+ int iRet,iErrCode;
+
+ if (pstIORmtAddr)
+ {
+ if (pstIORmtAddr->usType == EN_CINET_TYPE_IPV4)
+ {
+ iSize = sizeof(struct sockaddr_in);
+ memset(&stIn4Addr, 0, iSize);
+ pstSockAddr = (struct sockaddr *)&stIn4Addr;
+ }
+ else if (pstIORmtAddr->usType == EN_CINET_TYPE_IPV6)
+ {
+ iSize = sizeof(struct sockaddr_in6);
+ memset(&stIn6Addr, 0, iSize);
+ pstSockAddr = (struct sockaddr *)&stIn6Addr;
+ }
+ else
+ return EN_ZJ_ERR_PARAM;
+ }
+
+ iRet = recvfrom(sock, (char *)pOutBuf, iLen, 0, pstSockAddr, (socklen_t*)&iSize);
+ if (iRet == -1)
+ {
+ iErrCode = inet_socket_getLastErr();
+ if ( EINTR != iErrCode && EAGAIN != iErrCode && EWOULDBLOCK != iErrCode)
+ {
+ *pbOutClosed = 1;
+ }
+ return iRet;
+ }
+ if (pstIORmtAddr && iSize > 0)
+ {
+ if (pstIORmtAddr->usType == EN_CINET_TYPE_IPV4)
+ {
+ pstIORmtAddr->usPort = htons(stIn4Addr.sin_port);
+ pstIORmtAddr->u.uiIp = stIn4Addr.sin_addr.s_addr;
+ }
+ else if (pstIORmtAddr->usType == EN_CINET_TYPE_IPV6)
+ {
+ pstIORmtAddr->usPort = htons(stIn6Addr.sin6_port);
+ memcpy(pstIORmtAddr->u.aucIpv6, &stIn6Addr.sin6_addr, 16);
+ }
+ }
+
+ return iRet;
+}
+
+static int inet_socket_send(int sock, void * pBuf, int iLen, int *pbOutWait)
+{
+ int iRet,iErrCode;
+
+ *pbOutWait = 0;
+ iRet = send(sock, (char *)pBuf, iLen, 0);
+ if (iRet == -1)
+ {
+ iErrCode = inet_socket_getLastErr();
+ if(EINTR == iErrCode || EAGAIN == iErrCode || EWOULDBLOCK == iErrCode)
+ {
+ *pbOutWait = 1;
+ }
+ }
+ return iRet;
+}
+
+static int inet_socket_recv(int sock, void * pOutBuf, int iLen, int *pbOutClosed)
+{
+ int iRet;
+ int iErrCode;
+ *pbOutClosed = 0;
+ iRet = recv(sock, (char *)pOutBuf, iLen, 0);
+ if (iRet <= 0)
+ {
+ iErrCode = inet_socket_getLastErr();
+ if ( EINTR != iErrCode && EAGAIN != iErrCode && EWOULDBLOCK != iErrCode)
+ {
+ *pbOutClosed = 1;
+ }
+ }
+ return iRet ;
+}
+
+static int inet_socket_setRecvBuff(int sock, int iBufSize)
+{
+ int *piOptVal;
+ int iRet;
+
+ piOptVal = (int *)&iBufSize;
+ iRet = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)piOptVal, sizeof(piOptVal));
+
+ if (iRet == -1)
+ {
+ return EN_ZJ_ERR;
+ }
+
+ return 0;
+}
+
+static int inet_socket_setSendBuff(int sock, int iBufSize)
+{
+ int *piOptVal,iRet;
+ piOptVal = (int *)&iBufSize;
+
+ iRet = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)piOptVal, sizeof(piOptVal));
+
+ if (iRet == -1)
+ {
+ return EN_ZJ_ERR;
+ }
+
+ return 0;
+}
+
+static int inet_socket_getLastErr(void)
+{
+ return errno;
+}
+
+static int inet_socket_checkConn(int sock, int *piErr)
+{
+ int iLen = 0;
+ int iCode = -1;
+ iCode = getsockopt(sock, SOL_SOCKET, SO_ERROR, (char*)piErr, (socklen_t*)&iLen );
+ if(iCode < 0 || *piErr){
+ return 0;
+ }
+ return 1;
+}
+
+/***********************************************************************************/
+static int thread_create(unsigned char *pucName, int iPriority, unsigned int uiStackSize, PFN_THREADPROC pfunProc, void *pParam, void **phOutOSThread)
+{
+ typedef void * (*startRoutine)(void *);
+ pthread_attr_t attr;
+ int iOSPriority = 0;
+ *phOutOSThread = NULL;
+ switch(iPriority)
+ {
+ case EN_THREAD_PRIORITY_LOWEST:
+ iOSPriority = 30;
+ break;
+ case EN_THREAD_PRIORITY_LOWER:
+ iOSPriority = 40;
+ break;
+ case EN_THREAD_PRIORITY_NORMAL:
+ iOSPriority = 45;
+ break;
+ case EN_THREAD_PRIORITY_HIGHER:
+ iOSPriority = 50;
+ break;
+ case EN_THREAD_PRIORITY_HIGHEST:
+ iOSPriority = 60;
+ break;
+ default:
+ return EN_ZJ_ERR_PARAM;
+ }
+
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ pthread_attr_setstacksize(&attr, uiStackSize);
+ pthread_attr_setschedpolicy(&attr, SCHED_RR);
+
+ if (pthread_create((pthread_t *)phOutOSThread, &attr, (startRoutine)pfunProc, pParam) != 0)
+ {
+ pthread_attr_destroy(&attr);
+ return EN_ZJ_ERR;
+ }
+
+ pthread_attr_destroy(&attr);
+ return 0;
+}
+
+static int thread_delete(void *hOSThread, int bForce)
+{
+ pthread_t thread = *(pthread_t*)hOSThread;
+ if (!bForce) return EN_ZJ_ERR_OK;
+
+ if(hOSThread == NULL) return EN_ZJ_ERR;
+
+ if (pthread_detach(thread) != 0)
+ return EN_ZJ_ERR;
+
+ return EN_ZJ_ERR_OK;
+}
+
+static int thread_sleep(unsigned int uiMilliSecond)
+{
+ usleep(uiMilliSecond*1000);
+ return EN_ZJ_ERR_OK;
+}
+
+static int thread_SetName(unsigned char *pucName)
+{
+ prctl(PR_SET_NAME,pucName);
+ return EN_ZJ_ERR_OK;
+}
+
+static unsigned int thread_GetCurID(void)
+{
+ return (unsigned int)pthread_self();
+}
+
+/***********************************************************************************//***********************************************************************************/
+static time_t time_getTime(time_t *ptmTime)
+{
+ return time((time_t *)ptmTime);
+}
+
+static struct tm* time_localTime(time_t *ptmTime,struct tm *ptTm)
+{
+ return localtime_r((time_t *)ptmTime,ptTm);
+}
+
+static unsigned int time_getTickCount(void)
+{
+ struct timespec stTime;
+ clock_gettime(CLOCK_MONOTONIC, &stTime);
+ return (stTime.tv_sec * 1000 + stTime.tv_nsec/1000000);
+}
+
+static time_t time_sysTimeToTime(ST_COS_SYS_TIME *pstTime)
+{
+ time_t cRetTime;
+ struct tm tMTime;
+ if(pstTime->usYear >= 1970)
+ tMTime.tm_year = pstTime->usYear - 1900;
+ else
+ tMTime.tm_year = 70;
+ tMTime.tm_mon = pstTime->usMonth -1;
+ tMTime.tm_mday = pstTime->usDay;
+ tMTime.tm_wday = pstTime->usWeekDay;
+ tMTime.tm_hour = pstTime->usHour;
+ tMTime.tm_min = pstTime->usMinute;
+ tMTime.tm_sec = pstTime->usSecond;
+ tMTime.tm_isdst = pstTime->usIsdst;
+
+ cRetTime = (time_t)mktime(&tMTime);
+
+ return cRetTime;
+}