From: hcj <1224667075@qq.com> Date: Sat, 23 Aug 2025 08:59:07 +0000 (+0800) Subject: 增加AI代码 X-Git-Tag: leiniao_debug_1.0.0_20250922_020716~99^2~32 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=622c4a82d38647b5f346ec3e861d96213ef57b5e;p=mirrors%2FFD230.git 增加AI代码 --- diff --git a/code/sdk/zhihui/AI/agent/include/agent.h b/code/sdk/zhihui/AI/agent/include/agent.h new file mode 100644 index 000000000..be921f14e --- /dev/null +++ b/code/sdk/zhihui/AI/agent/include/agent.h @@ -0,0 +1,44 @@ +#ifndef AGENT_H +#define AGENT_H + +typedef struct agent_info +{ + unsigned char bInitFlag; + unsigned char bRunFlag; + + unsigned char need_get_agent_list; // 获取智能体列表标志位 + unsigned char need_start_agent; // 启动智能体应用标志位 + //unsigned char need_create_session; // 创建会话标志位 + unsigned char need_create_ques_thread; + + unsigned char agent_status; + + unsigned char need_stop_send_audio; // + + + unsigned char agent_session_id[256]; // 会话ID + unsigned char agent_file_id[256]; + unsigned char need_send_pic_ques; + + unsigned char need_show_page1; + + unsigned char aucTrasText[1024]; + unsigned int uiOffset; + unsigned char need_send_voice_ques; + + unsigned char turn_id[64]; + + unsigned int download_text_reqId; + unsigned int download_audio_reqId; +}AGENT_INFO; + +AGENT_INFO* agent_getInfo(void); + +void agent_init(); +void agent_start(); +void agent_stop(); +void agent_destory(); + +int agent_audio_data_cb(unsigned int uiReqID, unsigned char* pucPackage, unsigned int uiPacklen, int bFinished, int iRspCode); + +#endif diff --git a/code/sdk/zhihui/AI/agent/src/agent.c b/code/sdk/zhihui/AI/agent/src/agent.c new file mode 100644 index 000000000..90b3d43e1 --- /dev/null +++ b/code/sdk/zhihui/AI/agent/src/agent.c @@ -0,0 +1,326 @@ +#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 diff --git a/code/sdk/zhihui/AI/ota/include/ota.h b/code/sdk/zhihui/AI/ota/include/ota.h new file mode 100644 index 000000000..5394b7893 --- /dev/null +++ b/code/sdk/zhihui/AI/ota/include/ota.h @@ -0,0 +1,7 @@ +#ifndef __OTA_H__ +#define __OTA_H__ + +int ota_init(); +int ota_start(); + +#endif \ No newline at end of file diff --git a/code/sdk/zhihui/AI/ota/src/ota.c b/code/sdk/zhihui/AI/ota/src/ota.c new file mode 100644 index 000000000..8a21269cf --- /dev/null +++ b/code/sdk/zhihui/AI/ota/src/ota.c @@ -0,0 +1,131 @@ +#include "main_include.h" + +/********************************************************************** +OTA升级流程 +1、设备注册ZJ_SetOtaCBFuncs相关函数。 +2、SDK收到升级请求,调用pFunNewVersionCb回调函数通知设备有新版固件需要升级,设备需要准备充足的资源。 +3、设备准备完成调用ZJ_StartUpdate接口通知SDK开始升级。 +4、SDK调用pFunVersonDataDownCb回调接口将升级包数据传给设备,设备应做好升级包缓存。 +5、升级包数据下载完成,SDK调用pFunCoverImageNotice回调函数,通知设备升级包是否校验通过,校验通过设备进行覆盖升级,否则删除缓存数据重启设备。 +6、设备通过ZJ_SetBurnningProgress接口上报烧录进度(0 - 100)。 +7、烧录成功重启设备。 +***********************************************************************/ +/** + * 设备版本状态回调接口,告诉设备当前版本升级状态 + */ +int ota_newversion_notice_cb(unsigned char* pucNewVersion, unsigned int uiFileSize, unsigned char* pucDownUrl) +{ + if (pucNewVersion == NULL || uiFileSize <= 0) + { + printf("device ota recv new version failed, version[%p] size [%u]\n", pucNewVersion, uiFileSize); + return -1; + } + printf("device ota recv new version notice ... , version [%s] size [%u]", pucNewVersion, uiFileSize); + + /** + * 升级时,释放相关资源。SDK不可以释放否则无法正常升级 + */ + + /** + * 设备已经准备完毕,开始进行升级 + */ + ZJ_StartUpdate(); + printf("device ota recv new version notice ok start upgrade, start update version [%s] size [%u]", pucNewVersion, uiFileSize); + return 0; +} + +/** + * 设备版本状态回调接口,告诉设备当前版本升级状态 + */ +int ota_newversion_datadown_cb(unsigned char* pucPackage, unsigned int uiPacklen, unsigned int uiEndFlag, unsigned int uiDataType) +{ + static int iDownloadFlag = 0; + static int iPackCount = 0; + static int iPackSize = 0; + if (iDownloadFlag == 0) + { + iDownloadFlag = 1; + printf("device ota start download update pack ..."); + } + + if (uiDataType == 0) + { + //固件数据 + } + else if (uiDataType == 1) + { + //配置数据 + } + //printf("device ota download firmware pack ..., pack len [%d] end flag [%d]", uiPacklen, uiEndFlag); + iPackCount += 1; + iPackSize += (int)uiPacklen; + + /** + * 将收到的升级包分片依次缓存下来 + */ + + if (uiEndFlag == 1) + { + printf("device ota download update pack write complete, pack count [%d] pack len[%d]", iPackCount, iPackSize); + } + return 0; +} + +/** + * 停止下载文件并释放升级包占用的空间 + */ +int ota_stopupgrade_cb(void) +{ + /** + * 停止升级,删除升级包缓存内存,重启设备 + */ + printf("device ota stop upgrade \n"); + return 0; +} + +/** + * 1 校验成功,通知固件覆盖镜像、 0 校验不成功,释放占用空间并重启 + */ +int ota_coverimage_notice_cb(unsigned int uiCoverFlag) +{ + int i = 0; + if (uiCoverFlag) + { + /** + * 开始固件覆盖,固件覆盖进度需要上报,从70-100进行上报 + */ + while (i < 100) + { + i += 5; + /** + * 升级进度上报100后,升级成功,部分设备升级完成会直接重启,可以重启后直接上报进度100完成升级 + */ + ZJ_SetBurnningProgress(i); +#ifndef WIN_TEST + sleep(1); +#else + Sleep(1000); +#endif + } + } + return 0; +} + + +int ota_init() +{ + printf("device ota init \n"); + /*设置设备升级状态回调函数注册,通过回调返回升级状态;设备上层收到后,启用升级程序下载升级固件*/ + ZJ_SetOtaCBFuncs(ota_newversion_notice_cb, + ota_newversion_datadown_cb, + ota_stopupgrade_cb, + ota_coverimage_notice_cb); + + return 0; +} + +int ota_start() +{ + + return 0; +} \ No newline at end of file diff --git a/code/sdk/zhihui/AI/syscall/include/syscall.h b/code/sdk/zhihui/AI/syscall/include/syscall.h new file mode 100644 index 000000000..0f7cf74a7 --- /dev/null +++ b/code/sdk/zhihui/AI/syscall/include/syscall.h @@ -0,0 +1,7 @@ +#ifndef SYSCALL_H +#define SYSCALL_H + +void syscall_init(); +void syscall_start(); + +#endif \ No newline at end of file diff --git a/code/sdk/zhihui/AI/syscall/src/syscall.c b/code/sdk/zhihui/AI/syscall/src/syscall.c new file mode 100644 index 000000000..ac38f73d7 --- /dev/null +++ b/code/sdk/zhihui/AI/syscall/src/syscall.c @@ -0,0 +1,1206 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +} diff --git a/code/sdk/zhihui/AI/system/include/system.h b/code/sdk/zhihui/AI/system/include/system.h new file mode 100644 index 000000000..9ed07d06d --- /dev/null +++ b/code/sdk/zhihui/AI/system/include/system.h @@ -0,0 +1,38 @@ +#ifndef SYSTEM_INIT_H +#define SYSTEM_INIT_H + +// 厂商不要使用此三码 +#ifdef WIN_TEST +//#define COMPANYID "auexhqe13em49cdv" +//#define APPID "sntfEFZXtDWmFbHi" +//#define CTEI "860036070000362" +// gx test +#define COMPANYID "auexhqe13em49cdv" +#define APPID "2lKQKUjba6vXsCOW" +#define CTEI "YNWHMY250430CKAJ" +#else +// 厂商需要自己申请三码 +#define COMPANYID "xxxxxxxxxxxxxxxx" +#define APPID "xxxxxxxxxxxxxxxx" +#define CTEI "xxxxxxxxxxxxxxxx" +#endif +#define DEVMODEL "TEST_AGENT_001" +#define APPVERSION "ZJ_ARM8852_0530_01_202004131758" + +typedef struct system_info +{ + unsigned int uiSdkServerStatus; + + unsigned int uiDemoRunFlag; +}SYS_INFO; +SYS_INFO* system_getInfo(void); + +// +void system_init(void); +void system_start(void); +void system_stop(void); +void system_destory(void); + + + +#endif diff --git a/code/sdk/zhihui/AI/system/src/system.c b/code/sdk/zhihui/AI/system/src/system.c new file mode 100644 index 000000000..67b604429 --- /dev/null +++ b/code/sdk/zhihui/AI/system/src/system.c @@ -0,0 +1,60 @@ +#include "main_include.h" + +static SYS_INFO sys_info = { 0 }; + +static int system_status_cb(EN_ZJ_SERVER_STATUS enDeviceStatus, int iErrCode); + +SYS_INFO* system_getInfo(void) +{ + return &sys_info; +} + +void system_init(void) +{ + memset(&sys_info, 0, sizeof(sys_info)); + sys_info.uiDemoRunFlag = 1; + + // 设置设备基本信息 + ZJ_SetCompanyInfo((unsigned char *)COMPANYID, (unsigned char*)APPID); + ZJ_SetCTEIID((unsigned char*)CTEI); + ZJ_SetDeviceModel((unsigned char*)DEVMODEL); + ZJ_SetDevOsType(EN_ZJ_OS_TYPE_LINUX); + ZJ_SetAppVersion((const char *)APPVERSION); + + // 设置日志开关和等级 + ZJ_SetDebugMode(1); + ZJ_SetLogOpenFlag(1); + ZJ_SetDebugLevel(EN_ZJ_LOG_LVL_ALL); + + // 设置网络类型以及连接状态,当网络ok时通过此接口告诉SDK + ZJ_SetNetWorkType(EN_ZJ_NETWORK_TYPE_WIRED, 1); + // 注册SDK状态通知 + ZJ_Set_SystemStatusCB(system_status_cb); +} + +void system_start(void) +{ + +} + +void system_stop(void) +{ + +} + +void system_destory(void) +{ + +} + +static int system_status_cb(EN_ZJ_SERVER_STATUS enDeviceStatus, int iErrCode) +{ + system_getInfo()->uiSdkServerStatus = enDeviceStatus; + // debug + if (system_getInfo()->uiSdkServerStatus >= EN_ZJ_SERVER_STATUS_WAITBIND) + { + agent_getInfo()->need_start_agent = 1; + } + printf("recv sdk server status [%d] \n", enDeviceStatus); + return 0; +} \ No newline at end of file