Ejemplo n.º 1
0
void *sms_read_thread(void *arg)
{
	if (arg == NULL) {
		return NULL;
	}
	handler h = (handler)arg;
	while (TRUE) {
		if ((read_length=read(sms.fd,buffer,sizeof(buffer)))>0) {
			if (strstr(buffer,"0891")) {
				memset(&g_rcv_info,0,sizeof(&g_rcv_info));
				// 1. get the messgae info
				g_rcv_info = handler_receive_msg(strstr(buffer,"0891"));
				// 2.save log
				save_gsm_log(g_rcv_info);
				// 3.delete it from SIM
				memset(buffer,0,sizeof(buffer));
				sprintf(buffer,"AT+CMGD=%s\r",rcv_msg_sim_index);
				write(sms.fd,buffer,strlen(buffer));
				h();//to handler user message . wait next handler and dev control
			} else if (strstr(buffer,"CMTI:")) {//new message is coming
				memset(command,0,sizeof(command));
				strcpy(command,buffer);
				strtok(command,",");				
				memset(rcv_msg_sim_index,0,sizeof(rcv_msg_sim_index));
				strcpy(rcv_msg_sim_index,strtok(NULL,","));
				memset(buffer,0,sizeof(buffer));
				sprintf(buffer,"AT+CMGR=%s\r",rcv_msg_sim_index);
				write(sms.fd,buffer,strlen(buffer));  //read new message command
			}
			memset(buffer,0,sizeof(buffer));
		}
	}
}
int main(int argc,char **argv)
{	
	uart_config();
	gsm_init();
	sendMessage("18664307310", "4E2D");
	global.isReceivedMsg = false;
	pthread_mutex_init(&global.db, NULL);
	if((global.shmid=shmget(IPC_PRIVATE,MAX_BUFFER_SIZE,0666))<0)
	{
		perror("create share-memory");
		exit(1);
	}
	//create receive message Thread
	pthread_t listenClientThreadID;
	//pthread_create(&listenClientThreadID,NULL,listen_client_connect_thread_func,NULL);
	//pthread_detach(listenClientThreadID);

	int read_length = 0;
	char buffer[MAX_BUFFER_SIZE]={0};
	char command[MAX_BUFFER_SIZE] ={0};
	char *share_msg_info_addr;
	
	if((share_msg_info_addr = shmat(global.shmid,0,0))==(void *)-1)
	{
		perror("Parent: shmat:");
		exit(1);
	}
	
	while(true)
	{			
		if(global.isReceivedMsg)
		{
			sendMessage("18664307310", "4E2D");
			global.isReceivedMsg = false;
		}
		if((read_length=read(global.fd,buffer,sizeof(buffer)))>0)
		{
			if(strstr(buffer,"0891"))
			{
				// 1. get the messgae info
				message_info_t rcv_info = handler_receive_msg(strstr(buffer,"0891"));
				// 2.share memory
				memset(buffer,0,sizeof(buffer));
				sprintf(buffer,"%s,%s,%s\0",rcv_info.send_number,rcv_info.send_time,rcv_info.send_content);
				memset(share_msg_info_addr,0,sizeof(share_msg_info_addr));
				memcpy(share_msg_info_addr,buffer,strlen(buffer));
				// 3.save log
				save_gsm_log(rcv_info);
				// 4.delete it from SIM
				memset(buffer,0,sizeof(buffer));
				sprintf(buffer,"AT+CMGD=%s\r\0",rcv_msg_sim_index);
				write(global.fd,buffer,strlen(buffer));
				global.isReceivedMsg = true;
			}
			else if(strstr(buffer,"CMTI:"))//new message is coming
			{
				memset(command,0,sizeof(command));
				strcpy(command,buffer);
				printf("new message : %s\n",buffer);
				memset(buffer,0,sizeof(buffer));
				strtok(command,",");
				strcpy(rcv_msg_sim_index,strtok(NULL,","));
				sprintf(buffer,"AT+CMGR=%s\r\0",rcv_msg_sim_index);
				write(global.fd,buffer,strlen(buffer));  //read new message command
			}
			else
			{
				printf("buffer is: %s\n",buffer);
			}
			memset(buffer,0,sizeof(buffer));
		}
	}
	return 0;
}