c tool系列------用poll函数查询IMEI

来源:互联网 发布:两心之外无人知歌词 编辑:程序博客网 时间:2024/05/22 09:00
#include "tthelp.h"
#include "SIMTest.h"
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/poll.h>

#define CMD_READ_IMEI             "AT+CGSN\r"

#define Send_cmd_success           5

   pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
   pthread_cond_t  cond = PTHREAD_COND_INITIALIZER;  //init cond
   pthread_cond_t  cond1 = PTHREAD_COND_INITIALIZER;  //init cond
   static  int sync1=0;
   static  int success=0;

   
void write_data_fun1 (void ){
   
    
sleep(3);

//********************************************************
    
     pthread_mutex_lock(&mutex);      
        write_at_cgsn (g_fd,CMD_SWITCH_SIM_CARD_STATUS);    
         printf("-----1\n");
     pthread_cond_wait(&cond,&mutex);   
     pthread_mutex_unlock(&mutex);
                 
//********************************************************
     pthread_mutex_lock(&mutex);
    
     write_at_cgsn (g_fd, CMD_SWITCH_SIM_CARD_1 );
      printf("-----2\n");
     pthread_cond_wait(&cond,&mutex);    
     pthread_mutex_unlock(&mutex);
     if(sync1!=0)
         {
         pthread_exit(NULL);
         }

//********************************************************
     pthread_mutex_lock(&mutex);
    
     write_at_cgsn (g_fd,CMD_READ_IMEI);
    printf("------3\n");
    pthread_cond_wait(&cond,&mutex);    
     pthread_mutex_unlock(&mutex);    
    
//********************************************************
     pthread_mutex_lock(&mutex);
    
     write_at_cgsn (g_fd,CMD_SWITCH_SIM_CARD_2);
    printf("------4\n");
     pthread_cond_wait(&cond,&mutex);    
     pthread_mutex_unlock(&mutex);
      if(sync1!=0)
         {
         pthread_exit(NULL);
         }
//********************************************************
          pthread_mutex_lock(&mutex);
        
          write_at_cgsn (g_fd,CMD_READ_IMEI);
         printf("-----5\n");
          pthread_cond_wait(&cond,&mutex);    
          pthread_mutex_unlock(&mutex);

      success=1;
       pthread_exit(NULL);
}






int main(int argc, char *argv[]){

int fd1 = 0;
int i=0;
struct pollfd fds[1];
char buf[4096];
void* status;
 pthread_t read_thread;
 pthread_t write_thread;
 printf("11\n");
int rc;

  fds[0].fd = port_open ();
 g_fd=fds[0].fd;
  fds[0].events=POLLIN;

  if( pthread_create( &write_thread, NULL, (void *)write_data_fun1 ,NULL) != 0 ) {
    
        
         if(DEBUG_LOG) ALOGD("SIMTest:read_IMSI1 pthread_create failed.%s",strerror(errno));
         return -1;
     }
 

 
  while(fds[0].events){
   
      pthread_mutex_lock(&mutex);   
if(poll(fds,1,0)<0)
{
return 1;
}

if(fds[0].revents)
{
     rc=read(fds[0].fd,buf,4095);
  //   printf("rc=%d\n",rc);
    for(i=0;i<rc;i++)
        {
      if (buf[i]=='K')
          {
      pthread_cond_signal(&cond); //send sianal to t_b
    printf("buf[i]==%c\n",buf[i]);
          }
        if (buf[i]=='R')
          {
      pthread_cond_signal(&cond); //send sianal to t_b
    return 1;
          }
        }
    
if (rc<0)
{
return 1;
}else if(!rc)
{
fds[0].events=0;
}else
{
buf[rc]='\0';
printf("read:%s",buf);
}
}

    pthread_mutex_unlock(&mutex);
if(success==1)
    break;
      }

pthread_join(write_thread,status);    

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
  //  pthread_cond_destroy(&cond1);

    tthelp_Process(NOPARA_PRTHELP); //start process  
    if(_tthlp_Exit())  //exit
    return -1;
    return 0;
}


0 0
原创粉丝点击