tskNetwork.c
上传用户:dahaojd
上传日期:2008-01-29
资源大小:14357k
文件大小:8k
源码类别:

DSP编程

开发平台:

C/C++

  1. #include <std.h>
  2. #include <tsk.h>
  3. #include <stdio.h>
  4. #include <csl.h>
  5. #include <csl_cache.h>
  6. #include <csl_dat.h>
  7. #include <chan.h>
  8. #include <scom.h>
  9. #include <utl.h>
  10. #include <ialg.h>
  11. #include "appmain.h"
  12. #include "appThreads.h"
  13. #include "appBiosObjects.h"
  14. #include "netmain.h"  /*change*/
  15. IPN IpDecodeDefault = 0;
  16. IPN IpDecodePeer    = 0;
  17. IPN IpDecodeConnect = 0;
  18. IPN IpEncodePeer    = 0;
  19. int NetCamLocal = 1;
  20. // Some quick cheats to allow us to flash LED4
  21. #define DSK_IOPORT  ((unsigned char *)0x90080017)
  22. #define USER_LED4   8
  23. #define LED_TOGGLE(b)   *DSK_IOPORT ^= b
  24. unsigned int netcmdArgs[CMD_MAX+1] = { 0, 0, 0 };
  25. void tskNetwork()
  26. {
  27.     ScomMessage *pMsgBuf;
  28.     SCOM_Handle fromNettoProc,fromProctoNet;
  29.     int         avgmotion = 0;
  30.     SOCKET   sl_rec  = INVALID_SOCKET;
  31.     SOCKET   sl_play = INVALID_SOCKET;
  32.     SOCKET   srec    = INVALID_SOCKET;
  33.     SOCKET   splay   = INVALID_SOCKET;
  34.     struct   sockaddr_in sin1;
  35.     struct   timeval timeout;           // Timeout struct for select
  36.     int      tmp,readsize,size,arg;
  37.     fd_set   ibits;
  38.     int      jpg_size,org_size;
  39.     UINT8    *jpg_buf;
  40.     UINT8    *pFileBuffer;
  41.     fromProctoNet = SCOM_open("PROCTONET");
  42.     fromNettoProc = SCOM_open("NETTOPROC");
  43.     // Allocate the file environment for this task
  44.     fdOpenSession( TaskSelf() );
  45.     // Create the main TCP RECORD listen socket
  46.     sl_rec = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
  47.     if( sl_rec == INVALID_SOCKET )
  48.         goto leave;
  49.     // Create the main TCP PLAY listen socket
  50.     sl_play = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
  51.     if( sl_play == INVALID_SOCKET )
  52.         goto leave;
  53.     // Set Port = 3000 (rec), 3001 (play), leaving IP address = Any
  54.     bzero( &sin1, sizeof(struct sockaddr_in) );
  55.     sin1.sin_family = AF_INET;
  56.     sin1.sin_len    = sizeof( sin1 );
  57.     sin1.sin_port   = htons(3002);
  58.     // Bind sockets
  59.     if ( bind( sl_rec, (PSA) &sin1, sizeof(sin1) ) < 0 )
  60.         goto leave;
  61.     sin1.sin_port   = htons(3001);
  62.     if ( bind( sl_play, (PSA) &sin1, sizeof(sin1) ) < 0 )
  63.         goto leave;
  64.     // Start listening
  65.     if ( listen( sl_rec, 1) < 0 )
  66.         goto leave;
  67.     if ( listen( sl_play, 1) < 0 )
  68.         goto leave;
  69.     for(;;)
  70.     {
  71.         // Get Input Buffer
  72.         pMsgBuf = SCOM_getMsg(fromProctoNet, SYS_FOREVER);
  73.         org_size = jpg_size = pMsgBuf->sizeLinear;
  74.         jpg_buf  = pMsgBuf->bufLinear;
  75.         if( jpg_size > 0 && jpg_size < 192000 )
  76.         {
  77.             pFileBuffer = mmBulkAlloc( jpg_size );
  78.             if( pFileBuffer )
  79.             {
  80.                 mmCopy( pFileBuffer, jpg_buf, jpg_size );
  81.                 efs_destroyfile( "image1.jpg" );
  82.                 efs_createfilecb( "image1.jpg", jpg_size, pFileBuffer,
  83.                                 (EFSFUN)mmBulkFree, (UINT32)pFileBuffer );
  84.             }
  85.         }
  86.         // Check for a new connections
  87.         // Clear the select flags
  88.         FD_ZERO(&ibits);
  89.         // We examine the listening TCP socket
  90.         FD_SET(sl_rec, &ibits);
  91.         FD_SET(sl_play, &ibits);
  92.         // Check for a connection, but don't block
  93.         timeout.tv_sec  = 0;
  94.         timeout.tv_usec = 0;
  95.         tmp = fdSelect( 3, &ibits, 0, 0, &timeout );
  96.         if( tmp && FD_ISSET(sl_rec, &ibits) )
  97.         {
  98.             if( srec != INVALID_SOCKET )
  99.                 fdClose( srec );
  100.             tmp = sizeof(sin1);
  101.             srec = accept( sl_rec, (PSA)&sin1, &tmp );
  102.             // If connected, set the socket timeout
  103.             if( srec != INVALID_SOCKET )
  104.             {
  105.                 // Set timeout so we don't get stuck in send
  106.                 timeout.tv_sec  = 5;
  107.                 timeout.tv_usec = 0;
  108.                 setsockopt( srec, SOL_SOCKET, SO_SNDTIMEO,
  109.                             &timeout, sizeof(timeout) );
  110.             }
  111.         }
  112.         if( tmp && FD_ISSET(sl_play, &ibits) )
  113.         {
  114.             if( splay != INVALID_SOCKET )
  115.                 fdClose( splay );
  116.             tmp = sizeof(sin1);
  117.             splay = accept( sl_play, (PSA)&sin1, &tmp );
  118.             // If connected, get peer name
  119.             if( splay != INVALID_SOCKET )
  120.             {
  121.                 // Set timeout so we don't get stuck in recv
  122.                 timeout.tv_sec  = 5;
  123.                 timeout.tv_usec = 0;
  124.                 setsockopt( splay, SOL_SOCKET, SO_SNDTIMEO,
  125.                             &timeout, sizeof(timeout) );
  126.             }
  127.         }
  128.         avgmotion -= avgmotion >> 3;          // Sub off 1/8th
  129.         avgmotion += (pMsgBuf->motion << 5);  // Add in 1/8th (8 bit fixpoint)
  130.         // Toggle LED on 0.5% sustained motion frame. Note that
  131.         // the 8 sample sliding average must be 0.5% for the
  132.         // device to record.
  133.         if( avgmotion >= (5<<8) )
  134.             LED_TOGGLE(USER_LED4);
  135.         //
  136.         // If recording and there was motion, send to recorder
  137.         //
  138.         if( srec != INVALID_SOCKET )
  139.         {
  140.             // See if we have a command
  141.             if( recv(srec,(UINT8 *)&tmp,1,MSG_PEEK|MSG_DONTWAIT)==1 )
  142.             {
  143.                 if( recv(srec,(UINT8 *)&tmp,4,MSG_WAITALL) == 4 &&
  144.                     recv(srec,(UINT8 *)&arg,4,MSG_WAITALL) == 4 )
  145.                 {
  146.                     if( tmp >= 0 && tmp <= CMD_MAX )
  147.                     {
  148.                         netcmdArgs[tmp] = arg;
  149.                         if( tmp == CMD_SETCLOCK )
  150.                             netcmdArgs[tmp] -= llTimerGetTime(0);
  151.                     }
  152.                 }
  153.             }
  154.             // Now see if we're going to send a frame
  155.             // Look for a 0.2% change
  156.             if( avgmotion < 512 || !jpg_size || jpg_size > 256000 )
  157.             {
  158.                 // Send a NULL size marker
  159.                 tmp = 0;
  160.                 if( send( srec, (UINT8 *)&tmp, 4, 0 ) < 0 )
  161.                 {
  162.                    fdClose( srec );
  163.                    srec = INVALID_SOCKET;
  164.                 }
  165.             }
  166.             else
  167.             {
  168.                 // Send the file size, then the data
  169.                 if( (send( srec, (UINT8 *)&jpg_size, 4, 0 ) < 0) ||
  170.                     (send( srec, jpg_buf, jpg_size, 0 ) < 0) )
  171.                 {
  172.                    fdClose( srec );
  173.                    srec = INVALID_SOCKET;
  174.                 }
  175.             }
  176.         }
  177.         //
  178.         // If playing, then get the source from the player
  179.         //
  180.         if( splay != INVALID_SOCKET )
  181.         {
  182.             readsize = 0;
  183.             while( readsize < 4 )
  184.             {
  185.                 tmp = recv( splay, ((UINT8 *)&size)+readsize, 4-readsize, 0 );
  186.                 if( tmp < 0 )
  187.                 {
  188.                     // printf("Socket Read Error (%d)n",fdError());
  189. close_play:
  190.                     fdClose( splay );
  191.                     splay = INVALID_SOCKET;
  192.                     jpg_size = 0;
  193.                     goto not_playing;
  194.                 }
  195.                 if( !tmp )
  196.                 {
  197.                     // printf("Peer Disconnected (recv)n");
  198.                     goto close_play;
  199.                 }
  200.                 readsize += tmp;
  201.             }
  202.             readsize = 0;
  203.             while( readsize < size )
  204.             {
  205.                 tmp = recv( splay, jpg_buf+readsize, size-readsize, 0 );
  206.                 if( tmp < 0 )
  207.                 {
  208.                     // printf("Socket Read Error (%d)n",fdError());
  209.                     goto close_play;
  210.                 }
  211.                 if( !tmp )
  212.                 {
  213.                     // printf("Peer Disconnected (recv)n");
  214.                     goto close_play;
  215.                 }
  216.                 readsize += tmp;
  217.             }
  218.             jpg_size = readsize;
  219.         }
  220. not_playing:
  221.         pMsgBuf->sizeLinear = jpg_size;
  222.         if( org_size > jpg_size )
  223.             OEMCacheClean( jpg_buf, org_size );
  224.         else
  225.             OEMCacheClean( jpg_buf, jpg_size );
  226.         OEMCacheCleanSynch();
  227.         // Tell the processing loop we're done
  228.         SCOM_putMsg(fromNettoProc,pMsgBuf);
  229.     }
  230. leave:
  231.     // We only get here on a fatal error - close the sockets
  232.     if( sl_rec != INVALID_SOCKET )
  233.         fdClose( sl_rec );
  234.     if( sl_play != INVALID_SOCKET )
  235.         fdClose( sl_play );
  236.     printf("tskNetwork Fatal Errorn");
  237.     // This task is killed by the system - here, we block
  238.     TaskBlock( TaskSelf() );
  239. }