Пример #1
0
//-----------------------------------------------------------------------------
int gps_soc_request_abort(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (gps_soc_transaction.socket_id >= 0)
    {
        ilm_struct *ilm_send = NULL;
		int ret;

        ret = soc_close(gps_soc_transaction.socket_id);
    	gps_soc_log("soc_close result: %d", ret);
		kal_sleep_task(250);
        soc_close_nwk_account_by_id(MOD_GPS_TCPIP, gps_soc_transaction.nwt_acount_id);

        /* close socket and disconnect bearer here */
        ilm_send = allocate_ilm(MOD_GPS_TCPIP);
        ilm_send->msg_id = MSG_ID_APP_SOC_DEACTIVATE_REQ;
        ilm_send->peer_buff_ptr = NULL;
        ilm_send->local_para_ptr = NULL;
        SEND_ILM(MOD_GPS_TCPIP, MOD_SOC, SOC_APP_SAP, ilm_send);
    }
    gps_soc_transaction.post_retry_counter = 0;

    gps_soc_stop_timer();

    return 0;
}
Пример #2
0
static VMINT vm_tls_finialize_resource(void)
{
    VMINT resHandle = 0, bufSize = 0;
    vm_tls_context_t* ctx_p = NULL;

    while ((resHandle = vm_res_findfirst(-1, VM_RES_TYPE_TLS)) 
        != VM_RES_NOT_FIND)
    {
        if (vm_res_get_data(VM_RES_TYPE_TLS, 
            resHandle, (void**)&ctx_p, &bufSize) == 0 && ctx_p != NULL)
        {
            // TODO:EXIT
            if (ctx_p->soc_id > 0)
            {
                vm_tls_delete_conn(resHandle);
                vm_tls_delete_ctx(resHandle);
                soc_close(ctx_p->soc_id);
                ctx_p->soc_id = 0;
            }
        }
        else
        {
        }
        vm_res_release_data(VM_RES_TYPE_TLS, resHandle);
        vm_res_findclose(VM_RES_TYPE_TLS);
    }
    
    vm_res_type_set_notify_callback(VM_RES_TYPE_TLS, NULL);


    return 0;
}
Пример #3
0
static void vm_tls_notify_process_status(VM_P_HANDLE process_handle, 
    VMINT sys_state)
{
    VMINT resHandle = 0, bufSize = 0;
    vm_tls_context_t * ctx_p = NULL;

    switch (sys_state)
    {
    case VM_PMNG_UNLOAD:
        while ((resHandle = vm_res_findfirst(process_handle, VM_RES_TYPE_TLS)) 
            != VM_RES_NOT_FIND)
        {
            if (vm_res_get_data(VM_RES_TYPE_TLS, 
                resHandle, (void**)&ctx_p, &bufSize) == 0 && ctx_p != NULL)
            {
                // TODO:EXIT
                if (ctx_p->soc_id > 0)
                {
                    vm_tls_delete_conn(resHandle);
                    vm_tls_delete_ctx(resHandle);
                    soc_close(ctx_p->soc_id);
                    ctx_p->soc_id = 0;
                }
            }
            else
            {
            }
            vm_res_release_data(VM_RES_TYPE_TLS, resHandle);
            vm_res_findclose(VM_RES_TYPE_TLS); 
        }
        break;
    }
}
Пример #4
0
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
	char ssochead[1024];  
    ssochead[0] = '\0';  
	char receive_buff[102400];  
    unsigned n_receive_len = 102400;  
    receive_buff[0] = '\0';
	char* strIP = (char*)lpParam;

	//init socket
	if(soc_init(strIP,ssochead) != 0)
	{
		return -1;
	}
	//connect
	SOCKET sock = soc_connect(strIP);
	if(sock == NULL)
	{
		return -1;
	}
	//send HTTP head
	int nret = soc_send(sock,ssochead,strlen(ssochead));
	if(nret < 0)
	{
		return -2;
	}
	// recv
	soc_recv(sock);
	//close
	soc_close(sock);
}
Пример #5
0
void EmPingStartAct(kal_char *serv_addr)
{
   kal_uint32 acc_id = 5;//account id
   kal_uint16 i, j = 0;     
   kal_char str_buff[50];
   kal_uint8 val = 1;

   PING_CONTEXT_INFO(state) = PING_ACTIVE;

   /* Clean all the statistics for the previous session */
   PING_CONTEXT_INFO(maxRspMS) = 0;
   PING_CONTEXT_INFO(minRspMS) = 0;
   PING_CONTEXT_INFO(avgRspMS) = 0;
   PING_CONTEXT_INFO(rcvdCounter) = 0;
   PING_CONTEXT_INFO(lostCounter) = 0;

   if(PING_CONTEXT_INFO(echo_req) != NULL)
   {
      OslMfree(PING_CONTEXT_INFO(echo_req));
   }

   if(PING_CONTEXT_INFO(echo_rsp) != NULL)
   {
      OslMfree(PING_CONTEXT_INFO(echo_rsp));
   }

   //PING_CONTEXT_INFO(socketId) = soc_create(PF_INET, SOCK_RAW, IPPROTO_ICMP, MOD_PING, acc_id);
   PING_CONTEXT_INFO(socketId) = soc_create(PF_INET, SOCK_RAW, IPPROTO_ICMP, MOD_MMI, acc_id);

   if(PING_CONTEXT_INFO(socketId) >= 0)
   {

      if(soc_setsockopt(PING_CONTEXT_INFO(socketId), SOC_NBIO, &val, sizeof(val)) < 0)
      {
         kal_print("Set socket to nonblock mode error !!");
         return ;
      }

      val = SOC_READ | SOC_WRITE;

      if(soc_setsockopt(PING_CONTEXT_INFO(socketId), SOC_ASYNC, &val, sizeof(val)) < 0)
      {
         kal_print("Set socket to nonblock mode error !!");
         return ;
      }

      
      PING_CONTEXT_INFO(echo_req) = (kal_uint8*)OslMalloc(PING_CONTEXT_INFO(pingSize)+8);
      PING_CONTEXT_INFO(echo_rsp) = (kal_uint8*)OslMalloc(PING_CONTEXT_INFO(pingSize)+8);

      PING_CONTEXT_INFO(seqNo) = 0;
      PING_CONTEXT_INFO(currCount) = 0;

      PING_CONTEXT_INFO(pingSrvAddr.port) = 100;//identifier
      PING_CONTEXT_INFO(pingSrvAddr.addr_len) = 4;//any
      PING_CONTEXT_INFO(pingSrvAddr.addr[0])=0;
      PING_CONTEXT_INFO(pingSrvAddr.addr[1])=0;
      PING_CONTEXT_INFO(pingSrvAddr.addr[2])=0;
      PING_CONTEXT_INFO(pingSrvAddr.addr[3])=0;

#ifdef MMI_ON_HARDWARE_P
      if(soc_bind(PING_CONTEXT_INFO(socketId), &PING_CONTEXT_INFO(pingSrvAddr)) != 0)
      {
         soc_close(PING_CONTEXT_INFO(socketId));
         if(PING_CONTEXT_INFO(echo_req) != NULL)
         {
            OslMfree(PING_CONTEXT_INFO(echo_req));
         }

         if(PING_CONTEXT_INFO(echo_rsp) != NULL)
         {
            OslMfree(PING_CONTEXT_INFO(echo_rsp));
         }
         PING_CONTEXT_INFO(echo_req) = NULL;
         PING_CONTEXT_INFO(echo_rsp) = NULL;
         

         kal_print("bind error");
      }
#endif
                  
      PING_CONTEXT_INFO(pingSrvAddr.addr[0]) = atoi(strtok(serv_addr, "."));//ip string: "140.112.1.1" to integer
      PING_CONTEXT_INFO(pingSrvAddr.addr[1]) = atoi(strtok(NULL, "."));
      PING_CONTEXT_INFO(pingSrvAddr.addr[2]) = atoi(strtok(NULL, "."));
      PING_CONTEXT_INFO(pingSrvAddr.addr[3]) = atoi(strtok(NULL, "."));
      PING_CONTEXT_INFO(pingSrvAddr.addr_len) = 4;

      kal_sprintf(str_buff, "Pinging %d.%d.%d.%d with %d bytes of data", PING_CONTEXT_INFO(pingSrvAddr.addr[0])
                                                   , PING_CONTEXT_INFO(pingSrvAddr.addr[1])
                                                   , PING_CONTEXT_INFO(pingSrvAddr.addr[2])
                                                   , PING_CONTEXT_INFO(pingSrvAddr.addr[3])
                                                   , PING_CONTEXT_INFO(pingSize));
      kal_print(str_buff);
      EmPingUpdateScreen(EM_PING_START, -1);
      EmPingSendPkt();


   } //if(s >= 0)
   else
   {
      ASSERT(PING_CONTEXT_INFO(socketId) >= 0);
   }
}
Пример #6
0
kal_bool EmPingRecvPkt(void)
{
   kal_int32    ret_val = -1;
   kal_uint16  i = 0;
   kal_uint32  duration = 0;
   sockaddr_struct   *addr = NULL;
   kal_char    str_buff[100];
   
   addr = &PING_CONTEXT_INFO(pingSrvAddr);
   ret_val = soc_recv(PING_CONTEXT_INFO(socketId), PING_CONTEXT_INFO(echo_rsp), 
               (PING_CONTEXT_INFO(pingSize)+8), 0);

   
   kal_sprintf(str_buff, "Rcvd !seqNO: %d, currCounterL: %d", PING_CONTEXT_INFO(seqNo), PING_CONTEXT_INFO(currCount));
   kal_print(str_buff);

   
   if(ret_val > 0)
   {
      if((PING_CONTEXT_INFO(echo_rsp[6]) *256+PING_CONTEXT_INFO(echo_rsp[7])) < PING_CONTEXT_INFO(currCount))
      {
         /* The is a invalid RSP for the waittime of this PING packet had been expiry, just ignore it */
         kal_print("Received a timed out response");
         return KAL_FALSE;
      }

      PING_CONTEXT_INFO(pingEndFN) = TMD_System_Clock;

      /* Stop timer */
      EmPingStopTimer();

      PING_CONTEXT_INFO(currCount)++;
      
      //check identifier
      if((PING_CONTEXT_INFO(echo_rsp[4]) *256+ PING_CONTEXT_INFO(echo_rsp[5])) != 100)
      {
         PING_CONTEXT_INFO(lostCounter)++;
         kal_print("Ping identifier wrong");
      }
      else
      {
         //check seq no
         if((PING_CONTEXT_INFO(echo_rsp[6]) *256+PING_CONTEXT_INFO(echo_rsp[7])) != (PING_CONTEXT_INFO(seqNo)-1))
         {
            PING_CONTEXT_INFO(lostCounter)++;
            kal_print("Ping seq no wrong");
         }
         else
         {
            //check ping data
            for(i=8;i<PING_CONTEXT_INFO(pingSize);i++)
            {
               if(PING_CONTEXT_INFO(echo_rsp[i]) != (kal_uint8)PING_CONTEXT_INFO(currCount))
               {
                  PING_CONTEXT_INFO(lostCounter)++;
                  kal_print("Ping data is wrong");
                  break;
               }
            }
            
            if(i==PING_CONTEXT_INFO(pingSize))
            {
               /* Pass all check */

               /* Calculate the time period */
               if(PING_CONTEXT_INFO(pingEndFN) >= PING_CONTEXT_INFO(pingStartFN) )
               {
                 duration = (((PING_CONTEXT_INFO(pingEndFN) - PING_CONTEXT_INFO(pingStartFN)) * 462) / 100);
               }
               else
               {
                 duration = (((2715647 + PING_CONTEXT_INFO(pingEndFN) - PING_CONTEXT_INFO(pingStartFN)) * 462) / 100);
               }

               /* Update statistics */
               if(PING_CONTEXT_INFO(maxRspMS) < duration)
                  PING_CONTEXT_INFO(maxRspMS) = duration;

               if((PING_CONTEXT_INFO(minRspMS) == 0) || (PING_CONTEXT_INFO(minRspMS) > duration))
                  PING_CONTEXT_INFO(minRspMS) = duration;
               
               PING_CONTEXT_INFO(avgRspMS) += duration;
               PING_CONTEXT_INFO(rcvdCounter)++;
               
               /* Output the result */
               kal_sprintf(str_buff,"Reply from %d.%d.%d.%d: bytes=%d time=%dms", 
                   addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3], PING_CONTEXT_INFO(pingSize), duration);
               kal_print(str_buff);
               EmPingUpdateScreen(EM_PING_RECV, duration);


               if(PING_CONTEXT_INFO(pingCount) == PING_CONTEXT_INFO(currCount)) 
               {
                  soc_close(PING_CONTEXT_INFO(socketId));
                  if(PING_CONTEXT_INFO(echo_req) != NULL)
                  {
                     OslMfree(PING_CONTEXT_INFO(echo_req));
                  }
                  if(PING_CONTEXT_INFO(echo_rsp) != NULL)
                  {
                     OslMfree(PING_CONTEXT_INFO(echo_rsp));
                  }
                  PING_CONTEXT_INFO(echo_req) = NULL;
                  PING_CONTEXT_INFO(echo_rsp) = NULL;
                  PING_CONTEXT_INFO(state) = PING_INACTIVE;

                  /* Output the statistics */
                  kal_mem_set(&str_buff[0], 0, 100);
                  kal_sprintf(str_buff,"Ping statstics for %d.%d.%d.%d:", 
                     addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3], PING_CONTEXT_INFO(pingSize), duration);
                  kal_print(str_buff);

                  kal_mem_set(&str_buff[0], 0, 100);
                  kal_sprintf(str_buff, "    Packets: Sent = %d, Received = %d, Lost = %d (%d%c loss)",
                     PING_CONTEXT_INFO(pingCount), PING_CONTEXT_INFO(rcvdCounter), PING_CONTEXT_INFO(lostCounter),
                     ((PING_CONTEXT_INFO(lostCounter)*100)/PING_CONTEXT_INFO(pingCount)), '%');
                  kal_print(str_buff);

                  kal_mem_set(&str_buff[0], 0, 100);
                  kal_sprintf(str_buff, "    Min: %dms, Max: %dms, Avg: %dms", 
                     PING_CONTEXT_INFO(minRspMS), PING_CONTEXT_INFO(maxRspMS), 
                     (PING_CONTEXT_INFO(avgRspMS)/PING_CONTEXT_INFO(rcvdCounter)));
                  kal_print(str_buff);
                  
                  EmPingUpdateScreen(EM_PING_FIN, (PING_CONTEXT_INFO(avgRspMS)/PING_CONTEXT_INFO(rcvdCounter)) );
                  
               }
            }
         }

         //PING_CONTEXT_INFO(seqNo)++;

      }

      return KAL_TRUE;
   } 
   else if(ret_val == SOC_WOULDBLOCK)
   {
      /* return KAL_FALSE to break out the for loop and wait for notification or timeout message */
      return KAL_FALSE;
   }
   else
   {
      /* If happen, ask Kavien to have a look */
      ASSERT(ret_val > 0);
   }

   return KAL_TRUE;
   
}
Пример #7
0
void EmPingSendPkt(void)
{
   kal_uint16 i = 0;
   kal_uint16 j = 0;
   kal_bool    ret_val = KAL_FALSE;
   kal_int16  ret = 0;
   
   for(j = PING_CONTEXT_INFO(currCount); j < PING_CONTEXT_INFO(pingCount);j++)
   {
      PING_CONTEXT_INFO(pingStartFN) = 0;
      PING_CONTEXT_INFO(pingEndFN) = 0;

      kal_mem_set(PING_CONTEXT_INFO(echo_req), 0, PING_CONTEXT_INFO(pingSize)+8);
      
      PING_CONTEXT_INFO(echo_req[0]) = 8;//echo req

      PING_CONTEXT_INFO(echo_req[4]) = 0;//identifier
      PING_CONTEXT_INFO(echo_req[5]) = 100;

      PING_CONTEXT_INFO(echo_req[6]) = (kal_uint8)(PING_CONTEXT_INFO(seqNo) >> 8);//seq no
      PING_CONTEXT_INFO(echo_req[7]) = (kal_uint8)(PING_CONTEXT_INFO(seqNo) & 0x00ff);
      
      for(i=8;i<PING_CONTEXT_INFO(pingSize);i++)//dummy data
         PING_CONTEXT_INFO(echo_req[i])= (kal_uint8)(j+1);

      //there are two choices: connect->send or sendto
      //where addr specifies the destionation of ping
      //sendto directly
      ret = soc_sendto(PING_CONTEXT_INFO(socketId),PING_CONTEXT_INFO(echo_req), 
         (PING_CONTEXT_INFO(pingSize)+8), 0, &PING_CONTEXT_INFO(pingSrvAddr));
      if(ret != (PING_CONTEXT_INFO(pingSize)+8))
      {
         if(ret == SOC_WOULDBLOCK)
         {
            kal_print("Socket said send it later");
            return;
         }
         
         kal_print("send ping error");
         soc_close(PING_CONTEXT_INFO(socketId));
         if(PING_CONTEXT_INFO(echo_req) != NULL)
         {
            OslMfree(PING_CONTEXT_INFO(echo_req));
         }
         if(PING_CONTEXT_INFO(echo_rsp) != NULL)
         {
            OslMfree(PING_CONTEXT_INFO(echo_rsp));
         }
         PING_CONTEXT_INFO(echo_req) = NULL;
         PING_CONTEXT_INFO(echo_rsp) = NULL;
         PING_CONTEXT_INFO(state) = PING_INACTIVE;

         return;

      }
      else
      {
         EmPingStartTimer(PING_CONTEXT_INFO(seqNo));
         //PING_CONTEXT_INFO(currCount)++;
         PING_CONTEXT_INFO(seqNo)++;
         PING_CONTEXT_INFO(pingStartFN) = TMD_System_Clock;
      }

      ret_val = EmPingRecvPkt();

      if(ret_val == KAL_FALSE)
      {
         break;
      }
   }

   return;
}
Пример #8
0
//==========================================
//=== Ping Lib
//==========================================
void EmPingTimeourHandler(void)
{
   kal_char str_buff[100];
   kal_uint32  duration = 0;
   kal_uint32  avgRspMS = 0;
   sockaddr_struct   *addr = NULL;

   
   addr = &PING_CONTEXT_INFO(pingSrvAddr);

      PING_CONTEXT_INFO(currCount)++;

      kal_sprintf(str_buff, "Timedout !seqNO: %d, currCounterL: %d", PING_CONTEXT_INFO(seqNo), PING_CONTEXT_INFO(currCount));
      kal_print(str_buff);

      if(PING_CONTEXT_INFO(pingCount) == PING_CONTEXT_INFO(currCount))
      {
         kal_print("Request timed out");
         EmPingUpdateScreen( EM_PING_TIMEOUT, -1 );
         
         PING_CONTEXT_INFO(lostCounter)++;
         soc_close(PING_CONTEXT_INFO(socketId));
         if(PING_CONTEXT_INFO(echo_req) != NULL)
         {
            OslMfree(PING_CONTEXT_INFO(echo_req));
         }
         if(PING_CONTEXT_INFO(echo_rsp) != NULL)
         {
            OslMfree(PING_CONTEXT_INFO(echo_rsp));
         }
         PING_CONTEXT_INFO(echo_req) = NULL;
         PING_CONTEXT_INFO(echo_rsp) = NULL;
         PING_CONTEXT_INFO(state) = PING_INACTIVE;

         if(PING_CONTEXT_INFO(avgRspMS) == 0)
         {
            avgRspMS = 0;
         }
         else
         {
            avgRspMS = PING_CONTEXT_INFO(avgRspMS)/PING_CONTEXT_INFO(rcvdCounter);
         }
         /* Output the statistics */
         kal_mem_set(&str_buff[0], 0, 100);
         kal_sprintf(str_buff,"Ping statstics for %d.%d.%d.%d:", 
            addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3], PING_CONTEXT_INFO(pingSize), duration);
         kal_print(str_buff);

         kal_mem_set(&str_buff[0], 0, 100);
         kal_sprintf(str_buff, "    Packets: Sent = %d, Received = %d, Lost = %d (%d%c loss)",
            PING_CONTEXT_INFO(pingCount), PING_CONTEXT_INFO(rcvdCounter), PING_CONTEXT_INFO(lostCounter),
            ((PING_CONTEXT_INFO(lostCounter)*100)/PING_CONTEXT_INFO(pingCount)), '%');
         kal_print(str_buff);

         kal_mem_set(&str_buff[0], 0, 100);
         kal_sprintf(str_buff, "    Min: %dms, Max: %dms, Avg: %dms", 
            PING_CONTEXT_INFO(minRspMS), PING_CONTEXT_INFO(maxRspMS), 
            avgRspMS);
         kal_print(str_buff);

         EmPingUpdateScreen( EM_PING_FIN, avgRspMS );
      }
      else
      {
         if(PING_CONTEXT_INFO(state) == PING_ACTIVE)
         {
            PING_CONTEXT_INFO(lostCounter)++;
            kal_print("Request timed out");

            EmPingUpdateScreen( EM_PING_TIMEOUT, -1 );
            EmPingSendPkt();
         }
      }

   return;      
}
Пример #9
0
Файл: udpspy.c Проект: malaise/c
int main (const int argc, const char *argv[]) {
  /* Result of operation */
  int res;
  char buffer[255];

  /* The socket and its fd */
  soc_token socket = init_soc;
  int fd;

  /* Event result */
  boolean read;
  timeout_t timeout;
  int evtfd;

  /* parse arguments */
  parse_args (argc, argv);

  /* Create socket and get fd */
  if ( (res = soc_open (&socket, udp_socket)) != SOC_OK) {
    trace ("soc_open error", soc_error (res));
    error ("cannot open socket", "");
  }
  if ( (res = soc_get_id (socket, &fd)) != SOC_OK) {
    trace ("soc_get_id error", soc_error (res));
    error ("cannot get socket fd", "");
  }
  /* Bind socket to lan:port */
  bind_socket (socket);

  /* Attach fd for reading */
  if ( (res = evt_add_fd (fd, TRUE)) != WAIT_OK) {
    trace ("evt_add_fd error", "");
    error ("cannot add fd", "");
  }

  /* Main loop */
  timeout.tv_sec = -1;
  timeout.tv_usec = -1;
  for (;;) {
    /* Infinite wait for events */
    if ( (res = evt_wait (&evtfd, & read, &timeout)) != WAIT_OK) {
      trace ("evt_wait error", "");
      error ("cannot wait for event", "");
    }
    /* Analyse event */
    if (evtfd == SIG_EVENT) {
      if (get_signal() == SIG_TERMINATE) {
        /* Sigterm/sigint */
        break;
      } /* else unexpected signal => drop */
    } else if (evtfd == fd) {
      /* Got a packet: read it */
      res = soc_receive (socket, message, sizeof(message), TRUE);
      if (res < 0) {
        sprintf (buffer, "%d", res);
        trace ("soc_receive error", soc_error (res));
        error ("cannot read message", soc_error(res));
      } else {
        if (res > (int)sizeof(message)) {
          sprintf (buffer, "%d", res);
          trace ("soc_receive truncated message length", "buffer");
          res = (int)sizeof(message);
         }
         /* Put message info */
         display (socket, message, res);
      }
    } else if (evtfd >= 0) {
      /* Unexpected event on an unexpected fd */
      sprintf (buffer, "%d", evtfd);
      trace ("evt_wait got unexpected even on fd", "buffer");
      error ("even on unexpected fd", "");
    }

  } /* Main loop */

  /* Done */
  (void) evt_del_fd (fd, TRUE);
  (void) soc_close (&socket);
  the_end ();
}
Пример #10
0
//-----------------------------------------------------------------------------
int gps_soc_tcp_send_request2(void)
{
	kal_uint8 val = 1; // non-blocking mode, default
	kal_int32 res;
	kal_int8 error; 
	kal_int32 detail_cause;
	kal_bool set_header = KAL_FALSE;

	gps_soc_transaction.socket_id = soc_create(PF_INET, SOCK_STREAM, 0, MOD_GPS_TCPIP, gps_soc_transaction.nwt_acount_id);
	gps_soc_log("soc_create, socket_id: %d", gps_soc_transaction.socket_id);
	if (gps_soc_transaction.socket_id < 0)
	{
		if (gps_soc_transaction.callback != NULL)
		{
	    	gps_soc_transaction.callback(0, NULL, 0);
		}
		return GPS_SOC_ERROR;
	}

	val = 0;	// blocking mode
	res = soc_setsockopt(gps_soc_transaction.socket_id, SOC_NBIO, &val, sizeof(val));
	gps_soc_log("soc_setsockopt, val: %d, result: %d", val, res);
	if (res < 0)
	{
		goto ERROR_HANDLE;
	}
	val = SOC_READ | SOC_WRITE | SOC_CLOSE | SOC_CONNECT;
	res = soc_setsockopt(gps_soc_transaction.socket_id, SOC_ASYNC, &val, sizeof(val));
	gps_soc_log("soc_setsockopt, val: %d, result: %d", val, res);
	if (res < 0)
	{
		goto ERROR_HANDLE;
	}
	
	gps_soc_log("Connect to %d.%d.%d.%d and port: %d",
				    gps_soc_transaction.server_ip_addr.addr[0],
				    gps_soc_transaction.server_ip_addr.addr[1],
				    gps_soc_transaction.server_ip_addr.addr[2],
				    gps_soc_transaction.server_ip_addr.addr[3],
				    gps_soc_transaction.server_ip_addr.port);
	
	res = soc_connect(gps_soc_transaction.socket_id, &gps_soc_transaction.server_ip_addr);
	gps_soc_log("soc_connect res: %d", res);
	if (res < 0)
	{
		res = soc_get_last_error(gps_soc_transaction.socket_id, &error, &detail_cause);
		gps_soc_log("res: %d soc_connect error: %d, detail_cause: %d", 
					res, error, detail_cause);

		goto ERROR_HANDLE;
	}
	
	while (gps_soc_transaction.snd_counter < gps_soc_transaction.snd_data_len)
	{
		const GPS_GPRMC_Packed_Struct_t *pPack;
		char *pBuff;
		int i, count;

		pPack = (GPS_GPRMC_Packed_Struct_t *)gps_soc_transaction.rcvd_buffer;
		pPack += gps_soc_transaction.snd_counter;
		pBuff = gps_soc_transaction.snd_buffer;
		pBuff[0] = '\0';
		if (!set_header)
		{
			sprintf(pBuff, "#%s#%s#%s#%s#%d\r\n", 
					gps_imei_str,
					gps_gprs_username, 
					gps_gprs_userpwd, 
					gps_soc_upldtype_str(gps_soc_transaction.cause_type),
					gps_soc_transaction.snd_data_len);
			set_header = KAL_TRUE;
		}
		count = (gps_soc_transaction.snd_data_len - gps_soc_transaction.snd_counter) <= GPS_SEND_ITEMS_ONETIME ?
				(gps_soc_transaction.snd_data_len - gps_soc_transaction.snd_counter) :
				GPS_SEND_ITEMS_ONETIME;
		for (i = 0; i < count; i++)
		{
		    Result_t result = RESULT_ERROR;

			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			sprintf(pBuff, "#");
			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			sprintf(pBuff, "%04x%04x", pPack->lac, pPack->cid) ;
			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			result = GPS_APP_GPRMC_Packed2Str(pBuff, pPack);
			pPack++;
		}

		if (gps_soc_transaction.snd_counter + count >= (kal_int32) gps_soc_transaction.snd_data_len)
		{
			pBuff = gps_soc_transaction.snd_buffer + strlen(gps_soc_transaction.snd_buffer);
			sprintf(pBuff, "##\r\n");
		}

	    gps_soc_log("send data len: %d, data: %s", 
		   			strlen(gps_soc_transaction.snd_buffer),
		   			gps_soc_transaction.snd_buffer);
		res = soc_send(gps_soc_transaction.socket_id, 
					   (kal_uint8*)gps_soc_transaction.snd_buffer, 
					   strlen(gps_soc_transaction.snd_buffer), 
					   0);
		gps_soc_log("Http send request result, sent_bytes: %d", res);
		if (res < 0)
		{
			res = soc_get_last_error(gps_soc_transaction.socket_id, &error, &detail_cause);
			gps_soc_log("res: %d soc_send error: %d, detail_cause: %d", 
						res, error, detail_cause);
			break;
		}
		kal_sleep_task(250);

		gps_soc_transaction.snd_counter += count;
	}
  
ERROR_HANDLE:
	if (gps_soc_transaction.callback != NULL)
	{
	   	gps_soc_transaction.callback(0, NULL, 0);
	}

	kal_sleep_task(250);
	res = soc_close(gps_soc_transaction.socket_id);
	gps_soc_log("soc_close res: %d", res);
	if (res < 0)
	{
		res = soc_get_last_error(gps_soc_transaction.socket_id, &error, &detail_cause);
		gps_soc_log("res: %d soc_close error: %d, detail_cause: %d", 
					res, error, detail_cause);
	}
	kal_sleep_task(250);

	soc_close_nwk_account_by_id(MOD_GPS_TCPIP, gps_soc_transaction.nwt_acount_id);

	return GPS_SOC_SUCCESS;
}
Пример #11
0
/* THE MAIN */
int main (const int argc, const char * argv[]) {

  /* Socket data */
  soc_token soc = init_soc;
  soc_host lan;
  soc_port port;
  int soc_fd, fd;

  /* Socket message */
  msg_type msg;

  /* Times and timeouts */
  timeout_t start_time, end_time, current_time;
  timeout_t wait_timeout;
  double local_time, remote_time;

  /* Dynamic list of server infos */
  dlist list;
  info_type info;

  /* Utilities */
  boolean for_read;
  char buff[256];
  int res;
  char *index;

  /*********/
  /* Start */
  /*********/
  /* Save prog name */
  strcpy (prog, argv[0]);
  strcpy (prog, basename (prog));

  /*******************/
  /* Parse arguments */
  /*******************/
  /* Check args */
  if (argc != 2) {
    error ("Invalid argument");
  }
  /* Parse IPM address and port */
  strcpy (buff, argv[1]);
  index = strstr (buff, ":");
  if (index == NULL) {
     error ("Invalid argument");
  }
  *index = '\0';
  index++;
  if (soc_str2host (buff, &lan) != SOC_OK) {
    sprintf (buff, "Invalid ipm address %s", buff);
    error (buff);
  }
  if (soc_str2port (index, &port) != SOC_OK) {
    sprintf (buff, "Invalid port num %s", index);
    error (buff);
  }

  /**************/
  /* Initialize */
  /**************/
  /* Init dynamic list */
  dlist_init (& list, sizeof(info_type));
  /* Init socket */
  if (soc_open (&soc, udp_socket) != SOC_OK) {
    perror ("opening socket");
    error ("Socket initialization failed");
  }
  if (soc_set_dest_host_port (soc, &lan, port) != SOC_OK) {
    perror ("setting destination");
    error ("Socket initialization failed");
  }
  if (soc_link_port (soc, port) != SOC_OK) {
    perror ("linking to port");
    error ("Socket initialization failed");
  }
  if (soc_get_dest_host (soc, &lan) != SOC_OK) {
    perror ("getting dest lan");
    error ("Socket initialization failed");
  }
  if (soc_get_dest_port (soc, &port) != SOC_OK) {
    perror ("getting dest port");
    error ("Socket initialization failed");
  }
  /* Add socket to waiting point */
  if (soc_get_id(soc, &soc_fd) != SOC_OK) {
    perror ("getting socket id");
    error ("Socket initialization failed");

  }
  if (evt_add_fd(soc_fd, TRUE) != WAIT_OK) {
    perror("Adding fd");
    error ("Socket initialization failed");
  }
  /* Activate signal catching */
  activate_signal_handling();
  /* Report starting */
  buff[0]='\0';
  addr_image (&lan, buff);
  printf ("%s mcasting at address %s on port %d.\n", prog, buff, (int) port);
  /* Init times */
  get_time (&start_time);
  current_time = start_time;
  end_time = start_time;
  incr_time (&end_time, DELAY_CLIENT_MS);
  /* Send initial ping request */
  msg.ping = TRUE;
  msg.time = start_time;
  if (soc_send (soc, (soc_message) &msg, sizeof(msg)) != SOC_OK) {
    perror ("sending ping");
    error ("Sending ping request failed");
  }

  /*************/
  /* Main loop */
  /*************/
  for (;;) {
    /* First step is to loop until timeout */
    if (wait_timeout.tv_sec != -1) {
      wait_timeout = end_time;
      res = sub_time (&wait_timeout, &current_time);
      if (res <= 0) {
        break;
      }
    }
    if (evt_wait (&fd, &for_read, &wait_timeout) != WAIT_OK) {
      perror ("waiting for event");
      error ("Waiting for events failed");
    }
    if (! for_read) {
      error ("Write event received");
    }
    /* Termination signal */
    if (fd == SIG_EVENT) {
      if (get_signal () == SIG_TERMINATE) {
        break;
      }
    } else if (fd == NO_EVENT) {
      /* Timeout: first step ends with a dump of servers */
      if (dlist_length(&list) != 0) {
        dlist_rewind (&list, TRUE);
        for (;;) {
          dlist_read (&list, &info);
          /* Get host name if possible, else dump address */
          res = soc_host_name_of (&info.host, buff, sizeof(buff));
          if (res != SOC_OK) {
            buff[0]='\0';
            addr_image (&info.host, buff);
          }
          /* Compute (Start_time + Reception_time) / 2 */
          local_time = (time_to_double (&start_time)
                        + time_to_double (&info.reception_time) ) / 2.0;
          remote_time = time_to_double (&info.server_time);
          printf ("Host %s is shifted by %4.03fs\n", buff, remote_time - local_time);

          /* Done when last record has been put */
          if (dlist_get_pos (&list, FALSE) == 1) {
            break;
          }
          dlist_move (&list, TRUE);
        }
      }
      /* Now entering second step: infinite timeout */
      wait_timeout.tv_sec = -1;
      wait_timeout.tv_usec = -1;
      printf ("%s ready.\n", prog);
    } else if (fd != soc_fd) {
      sprintf (buff, "Invalid fd %d received", fd);
      error (buff);
    } else {
      /* Now this is the socket, read message */
      res = soc_receive (soc, (soc_message) &msg, sizeof(msg), TRUE);
      if (res < 0) {
        perror ("reading from socket");
        error ("Reading message failed");
      } else if (res != sizeof(msg)) {
        sprintf (buff, "Invalid size received, expected %d, got %d",
                       (int)sizeof(msg), res);
        error (buff);
      }
      get_time (&current_time);
      /* Client and server different behaviours */
      if ((wait_timeout.tv_sec != -1) && !msg.ping) {
        /* First step: store the address and time of server, if pong */
        if (soc_get_dest_host (soc, &(info.host)) != SOC_OK) {
          perror ("getting dest host");
          error ("Getting server address failed");
        }
        info.server_time = msg.time;
        info.reception_time = current_time;
        dlist_insert (&list, &info, TRUE);

      } else if ( (wait_timeout.tv_sec == -1) && msg.ping) {
        /* Second step: reply pong and time to ping */
        msg.time = current_time;
        msg.ping = FALSE;
        if (soc_send (soc, (soc_message) &msg, sizeof(msg)) != SOC_OK) {
          perror ("sending pong");
          error ("Sending pong request failed");
        }
      }
    }
  } /* End of main loop */


  /* Clean - Close */
  dlist_delete_all (&list);
  (void) evt_del_fd (soc_fd, TRUE);
  (void) soc_close (&soc);
  printf ("Done.\n");
  exit (0);
}
Пример #12
0
kal_int32 ntyHttpClose(int sockId) {
	soc_shutdown(sockId, SHUT_RDWR);
	soc_close(sockId);
	ClearProtocolEventHandler(MSG_ID_APP_SOC_GET_HOST_BY_NAME_IND);
}