?login_element?

Subversion Repositories NedoOS

Rev

Rev 1456 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <oscalls.h>
  5. #include <socket.h>
  6. #include <stdlib.h>
  7.  
  8. extern unsigned char    buf_rx[4*1024];  
  9. extern unsigned char *  ptr_in_rx;
  10. extern unsigned char *  ptr_out_rx;
  11. extern unsigned char    u32_intcount[4];
  12. extern unsigned char    flag_int_change;
  13. extern unsigned char    flag_syncrply;
  14. extern unsigned char    msg_hello[9];
  15. extern unsigned char    msg_framesync[5];
  16. no_init union APP_PAGES app_pages;
  17. no_init int                     len;
  18.  
  19. SOCKET                                  cmds = 0;
  20. SOCKET                                  datasoc = 0;
  21. char                                    grmod =0;
  22.  
  23. no_init struct sockaddr_in web_ia;
  24.  
  25.  
  26. SOCKET                                  udps = 0;
  27. no_init struct sockaddr_in udps_ia;
  28. no_init struct sockaddr_in udpr_ia;
  29. no_init unsigned char udp_buf[50];
  30.  
  31. no_init unsigned char * ptr;
  32.  
  33. void int_play(void);
  34. void int_null(void);
  35. void ptr_increment(int l);
  36. void shutup(void);
  37.  
  38.  
  39. unsigned char scrredraw(void){
  40.         puts(msg_hello+2);
  41.         return 0;
  42. }
  43.  
  44. void exit(int e){
  45.         shutup();
  46.         if(cmds)closesocket(cmds,0);
  47.         if(datasoc)closesocket(datasoc,0);
  48.         if(udps)closesocket(udps,0);
  49.         if(e!=0){      
  50.                 OS_SETGFX(6);
  51.                 puts((char*)e);
  52.                 puts("Press any key");
  53.                 getchar();
  54.         }
  55.         ((void(*)(int))0x0000)(e);
  56. }
  57.  
  58. void initMCU(void){
  59.         YIELD();
  60.         app_pages.l = OS_GETMAINPAGES();
  61.         //OS_SETMUSIC(int_play, app_pages.pgs.window_1);
  62.         OS_SETGFX(6);
  63.         puts(msg_hello+2);
  64. }
  65.  
  66. void putserr(const char * s1,const char * s2){
  67.         if(grmod == 0){
  68.                 OS_SETGFX(6);
  69.                 grmod = 1;
  70.         }
  71.         printf("Error: %s %s!",s1,s2);
  72. }
  73.  
  74.  
  75. C_task main (int argc, char *argv[])
  76. {
  77.         int l=1;
  78.     initMCU();
  79.         ptr_out_rx =  buf_rx;
  80.         ptr_in_rx = buf_rx + sizeof(buf_rx);
  81.         web_ia.sin_port=htons(16729); //'AY' chars
  82.         udpr_ia.sin_port=htons(16729);
  83.         while(l!=argc){
  84.                 char * p=argv[l];
  85.                 if(p[0]!='-') exit((int)"Wrong parameter");
  86.                 switch (p[1]&0xdf){
  87.                         case 'P':
  88.                                 web_ia.sin_port=htons(strtoul(p+2,&ptr,10));
  89.                                 break;
  90.                         default:
  91.                                 exit((int)"Wrong parameter");
  92.                 }
  93.                 l++;
  94.         }
  95.         cmds=socket(AF_INET,SOCK_STREAM,0);
  96.         bind(cmds,&web_ia,sizeof(web_ia));
  97.         listen(cmds,0);
  98.         while(1){
  99.                 if(datasoc == 0){
  100.                         if(udps == 0){
  101.                                 udps = socket(AF_INET,SOCK_DGRAM,0);
  102.                                 bind(udps,&udpr_ia, sizeof (udpr_ia));
  103.                         }
  104.                         l=recvfrom(udps,udp_buf,sizeof(udp_buf),0,&udps_ia,0);
  105.                         if(l>0){
  106.                                 udp_buf[l] = 0x00;
  107.                                 puts(udp_buf);
  108.                                 sendto(udps,msg_hello+2,8,0,&udps_ia,0);
  109.                         }              
  110.                         if(cmds == 0){
  111.                                 cmds = socket(AF_INET,SOCK_STREAM,0);
  112.                                 bind(cmds, &web_ia, sizeof(web_ia));
  113.                                 listen(cmds, 0);
  114.                         }
  115.                         datasoc=accept(cmds, 0, 0);
  116.                         if(datasoc < 0){
  117.                                 datasoc=0;
  118.                                 OS_SETMUSIC(int_null, app_pages.pgs.window_1);
  119.                                 if(errno != ERR_EAGAIN){
  120.                                         closesocket(cmds, 0);
  121.                                         cmds = 0;
  122.                                 }else{
  123.                                         _low_level_get();
  124.                                 }
  125.                                 continue;
  126.                         }else{//to do else
  127.                                 closesocket(cmds, 0);
  128.                                 cmds = 0;
  129.                                 closesocket(udps, 0);
  130.                                 udps = 0;
  131.                                 send(datasoc, msg_hello, 9, 0);
  132.                                 ptr_in_rx =  buf_rx;
  133.                                 ptr_out_rx = NULL;
  134.                                 OS_SETMUSIC(int_play, app_pages.pgs.window_1);
  135.                         }
  136.                 }
  137.                 if(flag_int_change){
  138.                         flag_int_change = 0;
  139.                         if(flag_syncrply == 2){
  140.                                 send(datasoc, msg_framesync, 10, 0);
  141.                         }else{
  142.                                 send(datasoc, msg_framesync, 5, 0);
  143.                         }
  144.                         flag_syncrply = 0;
  145.                 }
  146.                 if(ptr_out_rx < ptr_in_rx){
  147.                         l=recv(datasoc, ptr_in_rx, buf_rx+sizeof(buf_rx)-ptr_in_rx, 0);
  148.                 }else if(ptr_out_rx > ptr_in_rx){
  149.                         l=recv(datasoc, ptr_in_rx, ptr_out_rx - ptr_in_rx, 0);
  150.                 }else{
  151.                         continue;
  152.                 }
  153.                 if(l < 0){
  154.                         shutup();
  155.                         closesocket(datasoc, 0);
  156.                         OS_SETMUSIC(int_null, app_pages.pgs.window_1);
  157.                         datasoc = 0;
  158.                         continue;
  159.                 }else if(l == 0){
  160.                         _low_level_get();
  161.                         continue;
  162.                 }
  163.                 //тут складываем пакет в буфер
  164.                 ptr_increment(l);
  165.                
  166.         }
  167. }  
  168.