⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udptask.c

📁 在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LEA_4S的驱动,位置速寻算法,语音芯片ISD4004的录放音驱动,LED页面管理等等.从启动代码到操作系统的移植以及到业
💻 C
字号:
/****************************************************************
**                                                              *
**  FILE         :  UdpTask.C                                   *
**  COPYRIGHT    :  (c) 2001 .Xiamen Yaxon NetWork CO.LTD       *
**                                                              *
**                                                              *
**  By : CCH 2002.1.15                                          *
****************************************************************/
#include "includes.h"
#include "message.h"
#include "tools.h"
#include "gsmtask.h"
#include "gprsrecv.h"
#include "udptask.h"
#include "watchdog.h"

#include "ipport.h"
#include "q.h"
#include "netbuf.h"
#include "net.h"
#include "udp.h"



/*
********************************************************************************
*                  DEFINE CONFIG PARAMETERS
********************************************************************************
*/
#define SIZE_MSGBUF             20
#define MEM_SIZE                2048
#define MAX_WAIT_SEND           (120 * 20)                  /* MAX_WAIT_SEND = 120 second */
#define MAX_WAIT_ERR            (15 * 60 * 20)              /* MAX_WAIT_ERR  = 15 m */

#define LOCAL_PORT              9001                        /* local udp port */


/*
********************************************************************************
*                  DEFINE status
********************************************************************************
*/
#define CLOSED_                 0
#define OPENED_                 1


/*
********************************************************************************
*                  DEFINE MODULE VARIANT
********************************************************************************
*/
static OS_EVENT *msgque;
static OS_QMSG   curmsg;
static OS_QMSG   msgbuf[SIZE_MSGBUF];
static OS_STK    UDPTaskStack[STACK_UDPTASK];

static UDPCONN   udpcom;
static INT8U     status = CLOSED_;
static INT16U    ct_sendwait;
static INT16U    sendlen;
static INT8U     sendmem[MEM_SIZE];

static unshort   host_ipport;
static ip_addr   host_ipaddr;

extern queue     bigfreeq;    /* big free buffers */
extern queue     lilfreeq;    /* small free buffers */

static INT8U     wdgid;



static int CallbackProc(PACKET pkt, void *data)
{
    data = data;

    /* handle receive data */
    #if DEBUG_UARTNo_UDP <= 3
    PrintFromUART(DEBUG_UARTNo_UDP, "<receive data from udp>\n");
    FormatPrintDataBlock(FORMAT_HEX, DEBUG_UARTNo_UDP, (INT8U *)pkt->nb_prot, pkt->nb_plen);
    #endif

    /* handle receive data */
    InformGPRSRecv(CHA_UDP, (INT8U *)pkt->nb_prot, pkt->nb_plen);
    udp_free(pkt);
    return 0;
}

static void CloseUDPCom(void)
{
    if (udpcom != 0) {
        udp_close(udpcom);
        udpcom = 0;
    }
    #if DEBUG_UARTNo_UDP <= 3
    PrintFromUART(DEBUG_UARTNo_UDP, "<UDP com is closed>\n");
    #endif

    status  = CLOSED_;
    sendlen = 0;
    OSQPost(GsmTaskMsgQue, MSG_UDPTASK_CLOSE, 0, 0);
}

static void OpenUDPCom(void)
{
    udpcom = udp_open(host_ipaddr, host_ipport, LOCAL_PORT, CallbackProc, 0);
    if (udpcom == 0) {
       dtrap("sorry, can not open udp port, and now exit\n");
       return;
    } else {
        #if DEBUG_UARTNo_UDP <= 3
        PrintFromUART(DEBUG_UARTNo_UDP, "<UDP com is opened>\n");
        #endif

        status = OPENED_;
        OSQPost(GsmTaskMsgQue, MSG_UDPTASK_OPEN, 0, 0);
    }
}

static void InitUDPTask(void)
{
    sendlen     = 0;
    udpcom      = 0;
    ct_sendwait = 0;
    
    #if DEBUG_UARTNo_UDP <= 3
    InitUART(DEBUG_UARTNo_UDP, DEBUG_UART_BAUD);
    #endif
    
    wdgid = ApplyWatchDogID();
}

static void UDPTask(void *pdata)
{
    PACKET pkt;

    pdata = pdata;
    InitUDPTask();
    for (;;) {
        ResetWatchDog(wdgid);                               /* clear watch dog */
        if (OSQPend(msgque, 1, &curmsg) == OS_NO_ERR) {
            if (curmsg.MsgID == MSG_UDPTASK_REQUESTOPEN) {
                OpenUDPCom();
            } else if (curmsg.MsgID == MSG_UDPTASK_REQUESTCLOSE) {
                CloseUDPCom();
            }
        }
        if (status == OPENED_) {
            if (lilfreeq.q_len < 5 || bigfreeq.q_len < 3) {
                ct_sendwait++;
                if (ct_sendwait == MAX_WAIT_SEND) 
                    OSQPost(GsmTaskMsgQue, MSG_UDPTASK_SENDFLOW, 0, 0);
                else if (ct_sendwait > MAX_WAIT_ERR)
                    dtrap("TCPIP queue buffer is wrong!\n");
            } else if (sendlen > 0) {
                ct_sendwait = 0;
                if ((pkt = udp_alloc(sendlen, 0)) != NULL) {
                    pkt->nb_plen = sendlen;
                    pkt->fhost   = host_ipaddr;
                    pkt->net     = NULL;
                    memcpy(pkt->nb_prot, sendmem, sendlen);
                    if (udp_send(host_ipport, LOCAL_PORT, pkt) != 0) {
                        #if DEBUG_UARTNo_UDP <= 3
                        PrintFromUART(DEBUG_UARTNo_UDP, "<sorry, can not send from UDP, now free packet>\n");
                        #endif
                        udp_free(pkt);
                    } else {
                        #if DEBUG_UARTNo_UDP <= 3
                        PrintFromUART(DEBUG_UARTNo_UDP, "<send data from UDP>\n");
                        #endif
                        sendlen = 0;
                        OSQPost(GsmTaskMsgQue, MSG_UDPTASK_FREE, 0, 0);
                    }
                } else {
                   #if DEBUG_UARTNo_UDP <= 3
                   PrintFromUART(DEBUG_UARTNo_UDP, "<sorry, can not alloc memory for UDP com>\n");
                   #endif
                }
            }
        }
    }
}

BOOLEAN CanSendFromUDPCom(void)
{
    if (status != OPENED_ || sendlen > 0) return FALSE;
    else return TRUE;
}

BOOLEAN SendFromUDPCom(INT8U *data, INT16U len)
{
    if (CanSendFromUDPCom() && len <= sizeof(sendmem)) {
        memcpy(sendmem, data, len);
        sendlen = len;
        OSQPost(msgque, MSG_UDPTASK_TSK, 0, 0);
        return TRUE;
    } else {
        return FALSE;
    }
}

BOOLEAN RequestCloseUDPCom(void)
{
    if (status == CLOSED_) return FALSE;
    else {
        OSQPost(msgque, MSG_UDPTASK_REQUESTCLOSE, 0, 0);
        return TRUE;
    }
}

BOOLEAN RequestOpenUDPCom(char *ipaddr, char *ipport)
{
    unsigned snbits;

    if (status != CLOSED_) return FALSE;
    else {
        if (parse_ipad(&host_ipaddr, &snbits, ipaddr) != NULL) {
            #if DEBUG_UARTNo_UDP <= 3
            PrintFromUART(DEBUG_UARTNo_UDP, "<sorry, udp IP address error>\n");
            #endif
            return FALSE;
        }
        host_ipport = AsciiToDec((INT8U *)ipport, strlen(ipport));
        host_ipport = host_ipport;
        OSQPost(msgque, MSG_UDPTASK_REQUESTOPEN, 0, 0);
        return TRUE;
    }
}

BOOLEAN UDPComIsClosed(void)
{
    if (status == CLOSED_) return TRUE;
    else return FALSE;
}

BOOLEAN UDPComIsOpened(void)
{
    if (status == OPENED_) return TRUE;
    else return FALSE;
}

void CreateUDPTask(void)
{
    msgque = OSQCreate(msgbuf, sizeof(msgbuf)/sizeof(msgbuf[0]));
    OSTaskCreate(UDPTask, (void *)0, &UDPTaskStack[STACK_UDPTASK], PRIO_UDPTASK);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -