/******************************************************************************* * File Name : qxwz_user_impl.c * Description : * Created on : 2021Äê1ÔÂ7ÈÕ * Author : www.hido-studio.com *******************************************************************************/ /******************************************************************************* * Include Files * *******************************************************************************/ #include "qxwz_user_impl.h" #include "HIDO_TypeDef.h" #include "Uart.h" #include "stdarg.h" #include "stdio.h" #include "stdlib.h" #include "Socket.h" #include "HIDO_Debug.h" #include "HIDO_Timer.h" #include "HIDO_ATLite.h" #include "EC600SDriver.h" #include "EC600SFSM.h" #include "Internet.h" #include "DBG.h" /******************************************************************************* * Macro * *******************************************************************************/ /******************************************************************************* * Type Definition * *******************************************************************************/ /******************************************************************************* * Local Variable * *******************************************************************************/ /******************************************************************************* * Local Function Declaration * *******************************************************************************/ /******************************************************************************* * Local Function * *******************************************************************************/ /******************************************************************************* * Global Function * *******************************************************************************/ /******************************************************************************* * Function Name : qxwz_malloc * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_void_t *qxwz_malloc(qxwz_uint32_t size) { qxwz_void_t *addr = malloc(size); if(HIDO_NULL == addr) { addr = addr; HIDO_Debug("qxwz_malloc error \r\n"); } return addr; } /******************************************************************************* * Function Name : qxwz_calloc * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ void *qxwz_calloc(qxwz_uint32_t nmemb, qxwz_uint32_t size) { qxwz_void_t *addr = calloc(nmemb, size); if(HIDO_NULL == addr) { addr = addr; HIDO_Debug("qxwz_calloc error \r\n"); } return addr; } /******************************************************************************* * Function Name : qxwz_calloc * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_void_t *qxwz_realloc(qxwz_void_t *ptr, qxwz_uint32_t size) { qxwz_void_t *addr = realloc(ptr, size); if(HIDO_NULL == addr) { addr = addr; HIDO_Debug("qxwz_realloc error \r\n"); } return addr; } /******************************************************************************* * Function Name : qxwz_calloc * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ void qxwz_free(qxwz_void_t *ptr) { free(ptr); } /******************************************************************************* * Function Name : qxwz_mutex_init * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_mutex_init(qxwz_mutex_t *mutex) { return 0; } /******************************************************************************* * Function Name : qxwz_mutex_init * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_mutex_trylock(qxwz_mutex_t *mutex) { return 0; } /******************************************************************************* * Function Name : qxwz_mutex_unlock * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_mutex_lock(qxwz_mutex_t *mutex) { return 0; } /******************************************************************************* * Function Name : qxwz_mutex_unlock * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_mutex_unlock(qxwz_mutex_t *mutex) { return 0; } /******************************************************************************* * Function Name : qxwz_mutex_destroy * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_mutex_destroy(qxwz_mutex_t *mutex) { return 0; } /******************************************************************************* * Function Name : qxwz_SocketEventProc * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ static HIDO_VOID qxwz_SocketEventProc(HIDO_INT32 _i32SockID, E_SocketEvent _eEvent, HIDO_VOID *_pData) { HIDO_Debug("Event:%u\r\n", _eEvent); } /******************************************************************************* * Function Name : qxwz_sock_create * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_sock_create(void) { HIDO_Debug("## qxwz_sock_create \r\n"); HIDO_INT32 i32Socket = 0; static HIDO_INT32 i32LastSocket1 = -1; static HIDO_INT32 i32LastSocket2 = -1; if(-1 == i32LastSocket1) { if(Socket_Create(&i32Socket, SOCKET_TYPE_TCP, qxwz_SocketEventProc, HIDO_NULL) != HIDO_OK) { return -1; } i32LastSocket1 = i32Socket; return i32Socket; } else { switch(Socket_GetSocketState(i32LastSocket1)) { case SOCKET_STATE_CLOSED: case SOCKET_STATE_CONNECT_FAILED: { return i32LastSocket1; } } if(-1 == i32LastSocket2) { if(Socket_Create(&i32Socket, SOCKET_TYPE_TCP, qxwz_SocketEventProc, HIDO_NULL) != HIDO_OK) { return -1; } i32LastSocket2 = i32Socket; return i32Socket; } else { switch(Socket_GetSocketState(i32LastSocket2)) { case SOCKET_STATE_CLOSED: case SOCKET_STATE_CONNECT_FAILED: { return i32LastSocket2; } } } } return -1; } /******************************************************************************* * Function Name : qxwz_sock_connect * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_sock_connect(qxwz_int32_t sock, qxwz_sock_host_t *serv) { HIDO_BOOL bIsConnected = HIDO_FALSE; if(HIDO_NULL == serv) { return -1; } if(Socket_Connect(sock, (HIDO_CHAR *)serv->hostname, serv->port) != HIDO_OK) { return -1; } HIDO_UINT32 tick = HIDO_TimerGetTick(); while((HIDO_TimerGetTick() - tick) < HIDO_TIMER_TICK_S(10)) { EC600SDriver_TimerPoll(); HIDO_ATLitePoll(); Internet_Poll(); if(EC600S_IsIPIdle() == HIDO_TRUE) { if(Socket_IsConnected(sock, &bIsConnected) != HIDO_OK) { return -1; } if(bIsConnected == HIDO_TRUE) { return 0; } } } return 0; } /******************************************************************************* * Function Name : qxwz_sock_send * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_sock_send(qxwz_int32_t sock, const qxwz_uint8_t *send_buf, qxwz_uint32_t len) { if(HIDO_NULL == send_buf || 0 == len) { return -1; } if(Socket_Send(sock, (HIDO_UINT8 *)send_buf, len) != HIDO_OK) { HIDO_Debug("## Socket_Send error \r\n"); return -1; } return len; } /******************************************************************************* * Function Name : qxwz_sock_send * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_sock_recv(qxwz_int32_t sock, qxwz_uint8_t *recv_buf, qxwz_uint32_t len) { HIDO_BOOL bIsClosed = HIDO_FALSE; HIDO_UINT32 u32RecvLen = 0; if(HIDO_NULL == recv_buf || 0 == len) { return -2; } if(Socket_Recv(sock, recv_buf, len, &u32RecvLen) != HIDO_OK) { return -2; } if(0 == u32RecvLen) { if(Socket_IsClosed(sock, &bIsClosed) != HIDO_OK) { return -2; } if(HIDO_TRUE == bIsClosed) { return 0; } return -1; } HIDO_Debug("## qxwz_sock_recv %u \r\n", u32RecvLen); return u32RecvLen; } /******************************************************************************* * Function Name : qxwz_sock_close * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_sock_close(qxwz_int32_t sock) { HIDO_Debug("## qxwz_sock_close \r\n"); HIDO_BOOL bIsClosed = HIDO_FALSE; if(Socket_Close(sock) != HIDO_OK) { return -1; } HIDO_UINT32 tick = HIDO_TimerGetTick(); while((HIDO_TimerGetTick() - tick) < HIDO_TIMER_TICK_S(10)) { EC600SDriver_TimerPoll(); HIDO_ATLitePoll(); Internet_Poll(); if(EC600S_IsIPIdle() == HIDO_TRUE) { if(Socket_IsClosed(sock, &bIsClosed) != HIDO_OK) { break; } if(bIsClosed == HIDO_TRUE) { break; } } } // if(Socket_Destroy(sock) != HIDO_OK) // { // return -1; // } return 0; } /******************************************************************************* * Function Name : qxwz_sock_close * Description : * Input : * Output : * Return : * Author : www.hido-studio.com * Modified Date: : 2021Äê1ÔÂ7ÈÕ *******************************************************************************/ qxwz_int32_t qxwz_printf(const qxwz_char_t *fmt, ...) { if(DBG_GetMode() != DBG_MODE_SHELL) { return HIDO_OK; } va_list ap; HIDO_CHAR acOutputBuf[512]; HIDO_UINT32 u32Len = 0; va_start(ap, fmt); u32Len = vsnprintf(acOutputBuf, sizeof(acOutputBuf), fmt, ap); va_end(ap); if(u32Len > (sizeof(acOutputBuf) - 1)) { u32Len = sizeof(acOutputBuf) - 1; } return Uart_Send(UART_ID_DBG, (HIDO_UINT8 *) acOutputBuf, u32Len); }