编译方法:.\examples\build\对应的.bat 文件双击执行或打开就可以编译。
生成文件:.\out\对应目录\hex\M601_example_**.pac
1 TCP例程介绍
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "zyf_app.h"
#include "zyf_trace.h"
#include "zyf_timer.h"
#include "zyf_socket.h"
#include "zyf_thread.h"
/* tcp client */
static ZYF_SockParam_t s_tTCPParam;
static ZYF_SockClient_t s_tTCPClient;
static Uart_Param_t g_uart1param;
void UartWriteCallBack(void* Param) // general com
{
Uart_Param_t *uartparam = (Uart_Param_t *)Param;
if(Param == NULL)
{
return;
}
ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n"));
ZYF_UartWriteCallbackSwitch(uartparam->port,false);
}
void UartReadCallBack(void* Param) //
{
uint32_t recvlen = 0;
Uart_Param_t *uartparam = (Uart_Param_t *)Param;
/*
UART_PORT1 = 0,
UART_PORT2 = 1,
UART_PORT3 = 2,
*/
ZYF_LOG("Uart%d recv",uartparam->port);
while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1))
{
ZYF_LOG("recv :%02x",uartparam->uartbuf[recvlen]);
recvlen++;
}
ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen);
ZYF_UartWriteCallbackSwitch(uartparam->port,true);
}
static void AppUartInit(void)
{
int32_t ret;
g_uart1param.port = DEBUG_PORT;
ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param);
ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param);
ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE);
ZYF_LOG("AppUartInit");
return;
}
void ZYF_TcpPdpActCallback(uint8_t status)
{
ZYF_SockClient_t *ptClient = &s_tTCPClient;
ZYF_AppMsg_t tMsg;
if (ptClient->ptParam != NULL) {
tMsg.wMsgId = status;
ZYF_MsgQPut(ptClient->ptParam->ptMsg, (void *)&tMsg);
}
}
void ZYF_PdpActiveCnf(uint8_t contextId, int32_t errCode)
{
ZYF_LOG("PDP: Active");
ZYF_TcpPdpActCallback(APP_MSG_PDP_ACTIVE);
}
void ZYF_PdpDeactiveCnf(uint8_t contextId, int32_t errCode)
{
ZYF_LOG("PDP: Deactive");
}
static void ZYF_TCPReconnReq(void)
{
ZYF_SockClient_t *ptClient = &s_tTCPClient;
ZYF_AppMsg_t tMsg;
if (!ptClient->ptParam->bConnected) {
tMsg.wMsgId = APP_MSG_TCP_RECONN;
ZYF_MsgQPut(ptClient->ptParam->ptMsg, (void *)&tMsg);
}
}
static void ZYF_TCPReconnCallback(void *pvArg)
{
ZYF_SockClient_t *ptClient = (ZYF_SockClient_t *)pvArg;
ZYF_TCPReconnReq();
ZYF_StopTimer(ptClient->ptParam->ptConnTimer);
}
static void ZYF_TCPSendDataReq(uint8_t *pchBuffer, uint16_t hwLen)
{
ZYF_SockClient_t *ptClient = &s_tTCPClient;
ZYF_AppMsg_t tMsg;
if (ptClient->ptParam->bConnected &&
ptClient->ptParam->bSent /*|| (check ack num) */) {
ptClient->ptParam->bSent = false;
tMsg.wMsgId = APP_MSG_TCP_SEND;
ptClient->ptParam->hwTxSize = hwLen;
memcpy((void *)ptClient->ptParam->chTxBuffer, pchBuffer, hwLen);
ZYF_MsgQPut(ptClient->ptParam->ptMsg, (void *)&tMsg);
}
}
static void ZYF_TCPTimerCallback(void *pvParam)
{
ZYF_SockClient_t *ptClient = (ZYF_SockClient_t *)pvParam;
const char *pcStr = "Hello, world!\r\n";
ZYF_TCPSendDataReq((uint8_t *)pcStr, strlen(pcStr));
ZYF_StartTimer(ptClient->ptParam->ptSendTimer, 300000);
}
static void ZYF_TCPClientCallback(ZYF_SocketEvt_t tEvtId)
{
ZYF_SockClient_t *ptClient = &s_tTCPClient;
switch (tEvtId) {
case SOC_CONNECT:
ZYF_LOG("TCP event: SOC_CONNECT");
ZYF_LOG("tcp socket connect ok");
ptClient->ptParam->bConnected = true;
ptClient->ptParam->bSent = true;
ZYF_StartTimer(ptClient->ptParam->ptSendTimer, 5000);
break;
case SOC_CLOSE:
ZYF_LOG("TCP event: SOC_CLOSE");
ptClient->ptParam->bConnected = false;
ptClient->ptParam->bSent = false;
break;
case SOC_ERR_IND:
ZYF_LOG("TCP event: SOC_ERR_IND");
ptClient->ptParam->bConnected = false;
ptClient->ptParam->bSent = false;
break;
case SOC_BEARER_LOSING_IND:
ZYF_LOG("TCP event: SOC_BEARER_LOSING_IND");
ptClient->ptParam->bConnected = false;
ptClient->ptParam->bSent = false;
break;
case SOC_CLOSE_IND:
ZYF_LOG("TCP event: SOC_CLOSE_IND");
ptClient->ptParam->bConnected = false;
ptClient->ptParam->bSent = false;
break;
case SOC_WRITE:
ZYF_LOG("TCP event: SOC_WRITE");
ptClient->ptParam->bSent = true;
break;
case SOC_READ:
ZYF_LOG("TCP event: SOC_READ");
int iRet = -1;
iRet = ZYF_SocketRecv(ptClient->nSockFd,
ptClient->ptParam->chRxBuffer,
SOCKET_RECV_MAX_SIZE);
if (iRet <= 0) {
ZYF_LOG("failed to recv tcp");
} else {
ptClient->ptParam->hwRxSize = iRet;
ZYF_LOG("recv tcp ok => len: %d, %s", ptClient->ptParam->hwRxSize, ptClient->ptParam->chRxBuffer);
memset((void *)ptClient->ptParam->chRxBuffer, 0, SOCKET_RECV_MAX_SIZE);
}
break;
}
}
static void ZYF_TCPClientConn(ZYF_SockClient_t *ptClient)
{
ptClient->nSockFd = ZYF_SocketCreate(SOC_TYPE_TCP);
if (ptClient->nSockFd >= 0) {
ZYF_LOG("tcp socket create ok => %d", ptClient->nSockFd);
ZYF_SocketRegister(ptClient->nSockFd, ZYF_TCPClientCallback);
if (!ZYF_SocketConnect(ptClient->nSockFd, ptClient->chIpAddr, ptClient->hwPort)) {
} else {
ZYF_SocketClose(ptClient->nSockFd);
ptClient->ptParam->bConnected = false;
ZYF_StartTimer(ptClient->ptParam->ptConnTimer, 10000);
}
}
}
void ZYF_GetIpByNameCallBack(uint8_t contexId, int32_t errCode, uint8_t *ipAddr)
{
(void)contexId; //unuse
if (SOC_SUCCESS == errCode) {
ZYF_LOG("Host IP: %d.%d.%d.%d", ipAddr[0],ipAddr[1],ipAddr[2],ipAddr[3]);
}
}
static void ZYF_TCPClientThread(void *pvParam)
{
ZYF_SockClient_t *ptClient = (ZYF_SockClient_t *)pvParam;
ZYF_AppMsg_t tMsg;
int iRet = -1;
ZYF_LOG("thread enter!");
while (1) {
iRet = ZYF_MsgQGet(ptClient->ptParam->ptMsg, (void *)&tMsg);
if (iRet < 0) {
ZYF_LOG("Failed to get msg");
ZYF_ThreadSleep(10);
}
switch (tMsg.wMsgId) {
case APP_MSG_PDP_ACTIVE:
ZYF_LOG("PDP active!");
uint8_t chIpAddr[4] = {0};
ZYF_ConvertIpAddr((uint8_t *)"192.168.1.100", chIpAddr); //just for test
ZYF_LOG("Convert: %d.%d.%d.%d", chIpAddr[0],chIpAddr[1],chIpAddr[2],chIpAddr[3]);
ZYF_GetIPByHostName(1, "mqtt2.gpstracktech.com", ZYF_GetIpByNameCallBack); //just for test
ZYF_TCPClientConn(ptClient);
ZYF_SleepEnable();
break;
case APP_MSG_PDP_DEACTIVE:
ZYF_LOG("PDP Deactive!");
ZYF_StopTimer(ptClient->ptParam->ptSendTimer);
if (!ZYF_SocketClose(ptClient->nSockFd)) {
ZYF_LOG("tcp socket close ok");
ptClient->ptParam->bConnected = false;
}
break;
case APP_MSG_TCP_RECONN:
ZYF_TCPClientConn(ptClient);
break;
case APP_MSG_TCP_SEND:
if (ptClient->ptParam->hwTxSize > 0) {
uint16_t hwSentLen = 0;
int rc = 0;
do {
rc = ZYF_SocketSend(ptClient->nSockFd,
&ptClient->ptParam->chTxBuffer[hwSentLen],
ptClient->ptParam->hwTxSize - hwSentLen);
if (rc > 0) {
hwSentLen += rc;
} else if (rc < 0) { //fixme: check if err here and then re-send
ZYF_LOG("failed to send tcp");
break;
}
} while (hwSentLen < ptClient->ptParam->hwTxSize); //fixme: check if timeout here and then re-send
if (rc > 0) {
ZYF_LOG("sent tcp ok");
}
}
break;
default:
break;
}
}
}
void ZYF_TCPClientTest(void)
{
uint8_t chIpAddr[4] = {120,77,177,56};//{219,133,34,198};
s_tTCPClient.nSockFd = -1;
s_tTCPClient.hwPort = 4489;
memcpy((void *)s_tTCPClient.chIpAddr, chIpAddr, 4);
s_tTCPParam.bConnected = false;
s_tTCPParam.bSent = false;
s_tTCPParam.hwTxSize = 0;
memset((void *)s_tTCPParam.chTxBuffer, 0, SOCKET_SEND_MAX_SIZE);
s_tTCPParam.hwRxSize = 0;
memset((void *)s_tTCPParam.chRxBuffer, 0, SOCKET_RECV_MAX_SIZE);
s_tTCPClient.ptParam = &s_tTCPParam;
s_tTCPClient.ptParam->ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
if (s_tTCPClient.ptParam->ptMsg != NULL) {
ZYF_LOG("Succeeded to create network msg");
}
s_tTCPClient.ptParam->ptSendTimer = ZYF_CreateTimer(ZYF_TCPTimerCallback, (void *)&s_tTCPClient);
s_tTCPClient.ptParam->ptConnTimer = ZYF_CreateTimer(ZYF_TCPReconnCallback, (void *)&s_tTCPClient);
ZYF_ThreadCreate("ZYF_TCPClientThread", ZYF_TCPClientThread, (void *)&s_tTCPClient, ZYF_PRIORITY_NORMAL, 1024 * 2);
}
void TcpClient_Example(void * Param)
{
ZYF_MsgQ_t *ptMsg;
ZYF_AppMsg_t tMsg;
int iRet = -1;
ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
ZYF_LOG("thread enter!");
ZYF_NetworkInit();
ZYF_TCPClientTest();
while (1) {
ZYF_LOG("in while.");
iRet = ZYF_MsgQGet(ptMsg, (void *)&tMsg);
if (iRet < 0) {
ZYF_LOG("Failed to get msg");
ZYF_ThreadSleep(1000);
}
}
}
static void prvInvokeGlobalCtors(void)
{
extern void (*__init_array_start[])();
extern void (*__init_array_end[])();
size_t count = __init_array_end - __init_array_start;
for (size_t i = 0; i < count; ++i)
__init_array_start[i]();
}
int appimg_enter(void *param)
{
AppUartInit();
ZYF_LOG("application image enter, param 0x%x", param);
prvInvokeGlobalCtors();
ZYF_ThreadCreate("TcpClient_Example", TcpClient_Example, NULL, ZYF_PRIORITY_HIGH, 10*1024);
return 0;
}
void appimg_exit(void)
{
OSI_LOGI(0, "application image exit");
}
2 UDP例程介绍
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "zyf_app.h"
#include "zyf_trace.h"
#include "zyf_timer.h"
#include "zyf_socket.h"
#include "zyf_thread.h"
/* udp client */
static ZYF_SockParam_t s_tUDPParam;
static ZYF_SockClient_t s_tUDPClient;
static Uart_Param_t g_uart1param;
void UartWriteCallBack(void* Param) // general com
{
Uart_Param_t *uartparam = (Uart_Param_t *)Param;
if(Param == NULL)
{
return;
}
ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n"));
ZYF_UartWriteCallbackSwitch(uartparam->port,false);
}
void UartReadCallBack(void* Param) //
{
uint32_t recvlen = 0;
Uart_Param_t *uartparam = (Uart_Param_t *)Param;
/*
UART_PORT1 = 0,
UART_PORT2 = 1,
UART_PORT3 = 2,
*/
ZYF_LOG("Uart%d recv",uartparam->port);
while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1))
{
ZYF_LOG("recv :%02x",uartparam->uartbuf[recvlen]);
recvlen++;
}
ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen);
ZYF_UartWriteCallbackSwitch(uartparam->port,true);
}
static void AppUartInit(void)
{
int32_t ret;
g_uart1param.port = DEBUG_PORT;
ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param);
ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param);
ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE);
ZYF_LOG("AppUartInit");
return;
}
void ZYF_UdpPdpActCallback(uint8_t status)
{
ZYF_SockClient_t *ptClient = &s_tUDPClient;
ZYF_AppMsg_t tMsg;
if (ptClient->ptParam != NULL) {
tMsg.wMsgId = status;
ZYF_MsgQPut(ptClient->ptParam->ptMsg, (void *)&tMsg);
}
}
void ZYF_PdpActiveCnf(uint8_t contextId, int32_t errCode)
{
ZYF_LOG("PDP: Active");
ZYF_UdpPdpActCallback(APP_MSG_PDP_ACTIVE);
}
void ZYF_PdpDeactiveCnf(uint8_t contextId, int32_t errCode)
{
ZYF_LOG("PDP: Deactive");
}
static void ZYF_UDPReconnReq(void)
{
ZYF_SockClient_t *ptClient = &s_tUDPClient;
ZYF_AppMsg_t tMsg;
if (!ptClient->ptParam->bConnected) {
tMsg.wMsgId = APP_MSG_UDP_RECONN;
ZYF_MsgQPut(ptClient->ptParam->ptMsg, (void *)&tMsg);
}
}
static void ZYF_UDPReconnCallback(void *pvArg)
{
ZYF_SockClient_t *ptClient = (ZYF_SockClient_t *)pvArg;
ZYF_UDPReconnReq();
ZYF_StopTimer(ptClient->ptParam->ptConnTimer);
}
void ZYF_UDPSendDataReq(uint8_t *pchBuffer, uint16_t hwLen)
{
ZYF_SockClient_t *ptClient = &s_tUDPClient;
ZYF_AppMsg_t tMsg;
tMsg.wMsgId = APP_MSG_UDP_SEND;
ptClient->ptParam->hwTxSize = hwLen;
memcpy((void *)ptClient->ptParam->chTxBuffer, pchBuffer, hwLen);
ZYF_MsgQPut(ptClient->ptParam->ptMsg, (void *)&tMsg);
}
static void ZYF_UDPTimerCallback(void *pvParam)
{
ZYF_SockClient_t *ptClient = (ZYF_SockClient_t *)pvParam;
const char *pcStr = "Hello, M601!\r\n";
ZYF_UDPSendDataReq((uint8_t *)pcStr, strlen(pcStr));
ZYF_StartTimer(ptClient->ptParam->ptSendTimer, 5000);
}
static void ZYF_UDPClientCallback(ZYF_SocketEvt_t tEvtId)
{
ZYF_SockClient_t *ptClient = &s_tUDPClient;
switch (tEvtId) {
case SOC_CONNECT:
ZYF_LOG("UDP event: SOC_CONNECT");
break;
case SOC_CLOSE:
ZYF_LOG("UDP event: SOC_CLOSE");
break;
case SOC_ERR_IND:
ZYF_LOG("UDP event: SOC_ERR_IND");
break;
case SOC_BEARER_LOSING_IND:
ZYF_LOG("UDP event: SOC_BEARER_LOSING_IND");
break;
case SOC_CLOSE_IND:
ZYF_LOG("UDP event: SOC_CLOSE_IND");
break;
case SOC_WRITE:
ZYF_LOG("UDP event: SOC_WRITE");
break;
case SOC_READ:
ZYF_LOG("UDP event: SOC_READ");
int iRet = -1;
iRet = ZYF_SocketRecvFrom(ptClient->nSockFd,
ptClient->ptParam->chRxBuffer,
SOCKET_RECV_MAX_SIZE,
&ptClient->chIpAddr[0],
&ptClient->hwPort);
if (iRet <= 0) {
ZYF_LOG("failed to recv udp");
} else {
ptClient->ptParam->hwRxSize = iRet;
ZYF_LOG("recv udp ok => len: %d, %s", ptClient->ptParam->hwRxSize, ptClient->ptParam->chRxBuffer);
memset((void *)ptClient->ptParam->chRxBuffer, 0, SOCKET_RECV_MAX_SIZE);
}
break;
default:
ZYF_LOG("UDP event: unkown %X",tEvtId);
break;
}
}
static void ZYF_UDPClientConn(ZYF_SockClient_t *ptClient)
{
ptClient->nSockFd = ZYF_SocketCreate(SOC_TYPE_UDP);
if (ptClient->nSockFd >= 0) {
ZYF_LOG("udp socket create ok => %d", ptClient->nSockFd);
ZYF_SocketRegister(ptClient->nSockFd, ZYF_UDPClientCallback);
ZYF_StartTimer(ptClient->ptParam->ptSendTimer, 5000);
}
}
static void ZYF_UDPClientThread(void *pvParam)
{
ZYF_SockClient_t *ptClient = (ZYF_SockClient_t *)pvParam;
ZYF_AppMsg_t tMsg;
int iRet = -1;
ZYF_LOG("thread enter!");
while (1) {
iRet = ZYF_MsgQGet(ptClient->ptParam->ptMsg, (void *)&tMsg);
if (iRet < 0) {
ZYF_LOG("Failed to get msg");
ZYF_ThreadSleep(10);
}
switch (tMsg.wMsgId) {
case APP_MSG_PDP_ACTIVE:
ZYF_LOG("PDP active!");
ZYF_UDPClientConn(ptClient);
break;
case APP_MSG_PDP_DEACTIVE:
ZYF_LOG("PDP Deactive!");
ZYF_StopTimer(ptClient->ptParam->ptSendTimer);
if (!ZYF_SocketClose(ptClient->nSockFd)) {
ZYF_LOG("udp socket close ok");
}
break;
case APP_MSG_UDP_SEND:
if (ptClient->ptParam->hwTxSize > 0) {
uint32_t wTicksToWait = 3000; // 3S
uint16_t hwSentLen = 0;
do {
int rc = 0;
ZYF_SocketSetOpt(ptClient->nSockFd,
ZYF_SOL_SOCKET,
ZYF_SO_SNDTIMEO,
&wTicksToWait,
sizeof(wTicksToWait));
//iRet = CFW_TcpipSocketSendto(ptSockCtx->cSocketId, (uint8_t *)pchBuffer, (uint16_t)wSize, 0, &ptSockCtx->tDestAddr, ptSockCtx->tDestAddr.sin_len);
rc = ZYF_SocketSendTo(ptClient->nSockFd,
&ptClient->ptParam->chTxBuffer[hwSentLen],
ptClient->ptParam->hwTxSize - hwSentLen,
&ptClient->chIpAddr[0],
ptClient->hwPort);
if (rc > 0) {
hwSentLen += rc;
} else if (rc < 0) { //fixme: check if err here and then re-send
ZYF_LOG("failed to send udp");
break;
}
} while (hwSentLen < ptClient->ptParam->hwTxSize); //fixme: check if timeout here and then re-send
ZYF_LOG("sent udp ok");
}
break;
default:
break;
}
}
}
void ZYF_UDPClientTest(void)
{
uint8_t chIpAddr[4] = {120,77,177,56};
s_tUDPClient.nSockFd = -1;
s_tUDPClient.hwPort = 4499;
memcpy((void *)s_tUDPClient.chIpAddr, chIpAddr, 4);
s_tUDPParam.bConnected = false;
s_tUDPParam.bSent = false;
s_tUDPParam.hwTxSize = 0;
memset((void *)s_tUDPParam.chTxBuffer, 0, 1024);
s_tUDPParam.hwRxSize = 0;
memset((void *)s_tUDPParam.chRxBuffer, 0, 1024);
s_tUDPClient.ptParam = &s_tUDPParam;
s_tUDPClient.ptParam->ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
if (s_tUDPClient.ptParam->ptMsg != NULL) {
ZYF_LOG("Succeeded to create network msg");
}
s_tUDPClient.ptParam->ptSendTimer = ZYF_CreateTimer(ZYF_UDPTimerCallback, (void *)&s_tUDPClient);
s_tUDPClient.ptParam->ptConnTimer = ZYF_CreateTimer(ZYF_UDPReconnCallback, (void *)&s_tUDPClient);
ZYF_ThreadCreate("ZYF_UDPClientThread", ZYF_UDPClientThread, (void *)&s_tUDPClient, ZYF_PRIORITY_NORMAL, 1024 * 2);
}
void UdpClient_Example(void * Param)
{
ZYF_MsgQ_t *ptMsg;
ZYF_AppMsg_t tMsg;
int iRet = -1;
ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
ZYF_LOG("thread enter!");
ZYF_NetworkInit();
ZYF_UDPClientTest();
while (1) {
ZYF_LOG("in while.");
iRet = ZYF_MsgQGet(ptMsg, (void *)&tMsg);
if (iRet < 0) {
ZYF_LOG("Failed to get msg");
ZYF_ThreadSleep(1000);
}
}
}
static void prvInvokeGlobalCtors(void)
{
extern void (*__init_array_start[])();
extern void (*__init_array_end[])();
size_t count = __init_array_end - __init_array_start;
for (size_t i = 0; i < count; ++i)
__init_array_start[i]();
}
int appimg_enter(void *param)
{
AppUartInit();
ZYF_LOG("application image enter, param 0x%x", param);
prvInvokeGlobalCtors();
ZYF_ThreadCreate("UdpClient_Example", UdpClient_Example, NULL, ZYF_PRIORITY_HIGH, 10*1024);
return 0;
}
void appimg_exit(void)
{
OSI_LOGI(0, "application image exit");
}