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

📄 tsknetwork.c

📁 北京瑞泰创新的DM642光盘资料.非常有用.里面有很多源代码
💻 C
字号:
#include <std.h>
#include <tsk.h>
#include <stdio.h>
#include <csl.h>
#include <csl_cache.h>
#include <csl_dat.h>
#include <chan.h>
#include <scom.h>
#include <utl.h>
#include <ialg.h>

#include "appmain.h"
#include "appThreads.h"
#include "appBiosObjects.h"

#include "c:\ti\c6000\ndk\inc\netmain.h"

IPN IpDecodeDefault = 0;
IPN IpDecodePeer    = 0;
IPN IpDecodeConnect = 0;
IPN IpEncodePeer    = 0;
int NetCamLocal = 1;


// Some quick cheats to allow us to flash LED4
#define DSK_IOPORT  ((unsigned char *)0x90080017)
#define USER_LED4   8
#define LED_TOGGLE(b)   *DSK_IOPORT ^= b

unsigned int netcmdArgs[CMD_MAX+1] = { 0, 0, 0 };

void tskNetwork()
{
    ScomMessage *pMsgBuf;
    SCOM_Handle fromNettoProc,fromProctoNet;
    int         avgmotion = 0;
    SOCKET   sl_rec  = INVALID_SOCKET;
    SOCKET   sl_play = INVALID_SOCKET;
    SOCKET   srec    = INVALID_SOCKET;
    SOCKET   splay   = INVALID_SOCKET;
    struct   sockaddr_in sin1;
    struct   timeval timeout;           // Timeout struct for select
    int      tmp,readsize,size,arg;
    fd_set   ibits;

    int      jpg_size,org_size;
    UINT8    *jpg_buf;
    UINT8    *pFileBuffer;

    fromProctoNet = SCOM_open("PROCTONET");
    fromNettoProc = SCOM_open("NETTOPROC");

    // Allocate the file environment for this task
    fdOpenSession( TaskSelf() );

    // Create the main TCP RECORD listen socket
    sl_rec = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
    if( sl_rec == INVALID_SOCKET )
        goto leave;

    // Create the main TCP PLAY listen socket
    sl_play = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
    if( sl_play == INVALID_SOCKET )
        goto leave;

    // Set Port = 3000 (rec), 3001 (play), leaving IP address = Any
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family = AF_INET;
    sin1.sin_len    = sizeof( sin1 );
    sin1.sin_port   = htons(3002);

    // Bind sockets
    if ( bind( sl_rec, (PSA) &sin1, sizeof(sin1) ) < 0 )
        goto leave;

    sin1.sin_port   = htons(3001);
    if ( bind( sl_play, (PSA) &sin1, sizeof(sin1) ) < 0 )
        goto leave;

    // Start listening
    if ( listen( sl_rec, 1) < 0 )
        goto leave;

    if ( listen( sl_play, 1) < 0 )
        goto leave;

    for(;;)
    {
        // Get Input Buffer
        pMsgBuf = SCOM_getMsg(fromProctoNet, SYS_FOREVER);

        org_size = jpg_size = pMsgBuf->sizeLinear;
        jpg_buf  = pMsgBuf->bufLinear;

        if( jpg_size > 0 && jpg_size < 192000 )
        {
            pFileBuffer = mmBulkAlloc( jpg_size );
            if( pFileBuffer )
            {
                mmCopy( pFileBuffer, jpg_buf, jpg_size );
                efs_destroyfile( "image1.jpg" );
                efs_createfilecb( "image1.jpg", jpg_size, pFileBuffer,
                                (EFSFUN)mmBulkFree, (UINT32)pFileBuffer );
            }
        }


        // Check for a new connections

        // Clear the select flags
        FD_ZERO(&ibits);

        // We examine the listening TCP socket
        FD_SET(sl_rec, &ibits);
        FD_SET(sl_play, &ibits);

        // Check for a connection, but don't block
        timeout.tv_sec  = 0;
        timeout.tv_usec = 0;
        tmp = fdSelect( 3, &ibits, 0, 0, &timeout );

        if( tmp && FD_ISSET(sl_rec, &ibits) )
        {
            if( srec != INVALID_SOCKET )
                fdClose( srec );

            tmp = sizeof(sin1);
            srec = accept( sl_rec, (PSA)&sin1, &tmp );

            // If connected, set the socket timeout
            if( srec != INVALID_SOCKET )
            {
                // Set timeout so we don't get stuck in send
                timeout.tv_sec  = 5;
                timeout.tv_usec = 0;
                setsockopt( srec, SOL_SOCKET, SO_SNDTIMEO,
                            &timeout, sizeof(timeout) );
            }

        }

        if( tmp && FD_ISSET(sl_play, &ibits) )
        {
            if( splay != INVALID_SOCKET )
                fdClose( splay );

            tmp = sizeof(sin1);
            splay = accept( sl_play, (PSA)&sin1, &tmp );

            // If connected, get peer name
            if( splay != INVALID_SOCKET )
            {
                // Set timeout so we don't get stuck in recv
                timeout.tv_sec  = 5;
                timeout.tv_usec = 0;
                setsockopt( splay, SOL_SOCKET, SO_SNDTIMEO,
                            &timeout, sizeof(timeout) );
            }
        }

        avgmotion -= avgmotion >> 3;          // Sub off 1/8th
        avgmotion += (pMsgBuf->motion << 5);  // Add in 1/8th (8 bit fixpoint)

        // Toggle LED on 0.5% sustained motion frame. Note that
        // the 8 sample sliding average must be 0.5% for the
        // device to record.
        if( avgmotion >= (5<<8) )
            LED_TOGGLE(USER_LED4);

        //
        // If recording and there was motion, send to recorder
        //
        if( srec != INVALID_SOCKET )
        {
            // See if we have a command
            if( recv(srec,(UINT8 *)&tmp,1,MSG_PEEK|MSG_DONTWAIT)==1 )
            {
                if( recv(srec,(UINT8 *)&tmp,4,MSG_WAITALL) == 4 &&
                    recv(srec,(UINT8 *)&arg,4,MSG_WAITALL) == 4 )
                {
                    if( tmp >= 0 && tmp <= CMD_MAX )
                    {
                        netcmdArgs[tmp] = arg;
                        if( tmp == CMD_SETCLOCK )
                            netcmdArgs[tmp] -= llTimerGetTime(0);
                    }
                }
            }

            // Now see if we're going to send a frame
            // Look for a 0.2% change
            if( avgmotion < 512 || !jpg_size || jpg_size > 256000 )
            {
                // Send a NULL size marker
                tmp = 0;
                if( send( srec, (UINT8 *)&tmp, 4, 0 ) < 0 )
                {
                   fdClose( srec );
                   srec = INVALID_SOCKET;
                }
            }
            else
            {

                // Send the file size, then the data
                if( (send( srec, (UINT8 *)&jpg_size, 4, 0 ) < 0) ||
                    (send( srec, jpg_buf, jpg_size, 0 ) < 0) )
                {
                   fdClose( srec );
                   srec = INVALID_SOCKET;
                }
            }
        }

        //
        // If playing, then get the source from the player
        //
        if( splay != INVALID_SOCKET )
        {
            readsize = 0;
            while( readsize < 4 )
            {
                tmp = recv( splay, ((UINT8 *)&size)+readsize, 4-readsize, 0 );

                if( tmp < 0 )
                {
                    // printf("Socket Read Error (%d)\n",fdError());
close_play:
                    fdClose( splay );
                    splay = INVALID_SOCKET;
                    jpg_size = 0;
                    goto not_playing;
                }
                if( !tmp )
                {
                    // printf("Peer Disconnected (recv)\n");
                    goto close_play;
                }
                readsize += tmp;
            }

            readsize = 0;
            while( readsize < size )
            {
                tmp = recv( splay, jpg_buf+readsize, size-readsize, 0 );

                if( tmp < 0 )
                {
                    // printf("Socket Read Error (%d)\n",fdError());
                    goto close_play;
                }
                if( !tmp )
                {
                    // printf("Peer Disconnected (recv)\n");
                    goto close_play;
                }
                readsize += tmp;
            }

            jpg_size = readsize;
        }

not_playing:
        pMsgBuf->sizeLinear = jpg_size;

        if( org_size > jpg_size )
            OEMCacheClean( jpg_buf, org_size );
        else
            OEMCacheClean( jpg_buf, jpg_size );
        OEMCacheCleanSynch();

        // Tell the processing loop we're done
        SCOM_putMsg(fromNettoProc,pMsgBuf);
    }

leave:
    // We only get here on a fatal error - close the sockets
    if( sl_rec != INVALID_SOCKET )
        fdClose( sl_rec );
    if( sl_play != INVALID_SOCKET )
        fdClose( sl_play );

    printf("tskNetwork Fatal Error\n");

    // This task is killed by the system - here, we block
    TaskBlock( TaskSelf() );
}

⌨️ 快捷键说明

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