static int send_at_and_handlersp(char *cmd, int cmd_len, const char *rsp, int rsp_len)
{
    int ret = 0;
    int i;

    //printf("send_at_and_handlersp:cmd:%s,len:%d,rsp:%s,len:%d\n", cmd, cmd_len, rsp, rsp_len);    
    
#if ENABLE_HANDLE_RSP
    for (i=0; i<RSP_GET_RETRY; i++)
    {
        send_at_command(cmd, cmd_len);
        
        if (AT_RSP_MACH == get_at_response(rsp, rsp_len))
        {
            ret = 1;
            break;
        }
        
        // clear the uart data and resend
        modem_uart_purge();
        printf("retry...\n");
        sleep(1);
    }

#else
    send_at_command(cmd, cmd_len);
#endif //ENABLE_HANDLE_RSP
    return ret;
}
static void at_fifo_check(void *arg)
{
	static uint8_t wait_response_tick = 0;

	if(s_module_status == MODULE_START)
	{
		send_at_command("AT\r\n");
	}
	else if(MODULE_INIT ==s_module_status|| MODULE_INIT_DONE == s_module_status)
	{
		if ((NULL == s_current_at_command))
		{
			if ((!FIFO_isEmpty(&s_at_fifo)) &&(s_at_status == ATC_RSP_FINISH))
			{
				s_current_at_command = (struct at_cmd_entity*)mymalloc(sizeof(struct at_cmd_entity));
	            if (FIFO_Get(&s_at_fifo, s_current_at_command->at_name,  s_current_at_command->at_cmd))
	            {
	            	s_at_status = ATC_RSP_WAIT;
	                send_at_command(s_current_at_command->at_cmd);
	            }
			}
			else
			{
				printf("fifo is empty or the previous at command not complete!\n");
			}
		}
		else
		{
			printf("the at command is in sending!\n");
			if(wait_response_tick > AT_MAX_RESPONSE_TIME)
			{
				printf("the at command sending is timeout!\n");
			}
			wait_response_tick++;

		}
	}
}
Esempio n. 3
0
/* main - the main routine */
int main(void)
{
    XbeeFrameInit_t init;
    
    /* initialize the sockets */
    memset(sockets, 0, sizeof(sockets));
    
    printf("Starting frame driver\n");
    if (XbeeFrame_start(&init, mailbox, XBEE_RX, XBEE_TX, XBEE_RTS, XBEE_BAUD) < 0) {
        printf("failed to start frame driver\n");
        return 1;
    }
    
    /* set the port and ask for our IP address */
    //send_at_command(0, "C0%x", PORT); // this doesn't seem to work for some reason
    send_at_command(1, "MY");
    
    printf("Listening for frames\n");
    while (1) {
        uint8_t *frame;
        int length;
        
        if ((frame = XbeeFrame_recvframe(mailbox, &length)) != NULL) {
        
#ifdef FRAME_RX_DEBUG
            printf("[RX]");
            show_frame(frame, length);
#endif
            
            /* handle the frame */
            switch (frame[0]) {
            case ID_IPV4RX:
                handle_ipv4_frame((IPV4RX_header_t *)frame, length);
                break;
            case ID_ATRESPONSE:
                handle_atresponse_frame((ATResponse_t *)frame, length);
                break;
            case ID_TXSTATUS:
                handle_txstatus_frame((TXStatus_t *)frame, length);
                break;
            default:
                break;
            }
                
            XbeeFrame_release(mailbox);
        }
    }
    
    return 0;
}
Esempio n. 4
0
int main(void) {

	int serial_fd, major;
	struct termios tio;
	int ldisc = N_GSM0710;
	struct gsm_config gsm;
	char atcommand[40];

	/* print global parameters */
	dbg("SERIAL_PORT = %s", SERIAL_PORT);

	/* open the serial port */
	serial_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
	if (serial_fd == -1)
		err(EXIT_FAILURE, "Cannot open %s", SERIAL_PORT);
	
	/* get the current attributes of the serial port */
	if (tcgetattr(serial_fd, &tio) == -1)
		err(EXIT_FAILURE, "Cannot get line attributes");
	
	/* set the new attrbiutes */
	tio.c_iflag = 0;
	tio.c_oflag = 0;
	tio.c_cflag = CS8 | CREAD | CLOCAL;
//	tio.c_cflag |= CRTSCTS;
	tio.c_lflag = 0;
	tio.c_cc[VMIN] = 1;
	tio.c_cc[VTIME] = 0;
	
	/* write the speed of the serial line */
	if (cfsetospeed(&tio, LINE_SPEED) < 0 || cfsetispeed(&tio, LINE_SPEED) < 0)
		err(EXIT_FAILURE, "Cannot set line speed");
	
	/* write the attributes */
	if (tcsetattr(serial_fd, TCSANOW, &tio) == -1)
		err(EXIT_FAILURE, "Cannot set line attributes");

	/**
	*	Send AT commands to put the modem in CMUX mode.
	*	This is vendor specific and should be changed 
	*	to fit your modem needs.
	*	The following matches Quectel M95.
	*/
//	if (send_at_command(serial_fd, "AT+IFC=2,2\r") == -1)
//		errx(EXIT_FAILURE, "AT+IFC=2,2: bad response");	
	if (send_at_command(serial_fd, "AT+GMM\r") == -1)
		warnx("AT+GMM: bad response");
	if (send_at_command(serial_fd, "AT\r") == -1)
		warnx("AT: bad response");
	if (send_at_command(serial_fd, "AT+IPR=38400\r") == -1)
		errx(EXIT_FAILURE, "AT+IPR=38400: bad response");
	sprintf(atcommand, "AT+CMUX=0,0,3,%d,10,3,30,10,2\r", MTU);
	if (send_at_command(serial_fd, atcommand) == -1)
		errx(EXIT_FAILURE, "Cannot enable modem CMUX");

	/* use n_gsm line discipline */
	sleep(2);
	if (ioctl(serial_fd, TIOCSETD, &ldisc) < 0)
		err(EXIT_FAILURE, "Cannot set line dicipline. Is 'n_gsm' module registred?");

	/* get n_gsm configuration */
	if (ioctl(serial_fd, GSMIOC_GETCONF, &gsm) < 0)
		err(EXIT_FAILURE, "Cannot get GSM multiplex parameters");

	/* set and write new attributes */
	gsm.initiator = 1;
	gsm.encapsulation = 0; /* 0 or 1, basic/advanced */
	gsm.mru = MTU;
	gsm.mtu = MTU;
	gsm.t1 = 10;
	gsm.n2 = 3;
	gsm.t2 = 30;
	gsm.t3 = 10;

	if (ioctl(serial_fd, GSMIOC_SETCONF, &gsm) < 0)
		err(EXIT_FAILURE, "Cannot set GSM multiplex parameters");
	dbg("Line dicipline set");
	
	/* create the virtual TTYs */
	if (CREATE_NODES) {
		int created;
		if ((major = get_major(DRIVER_NAME)) < 0)
			errx(EXIT_FAILURE, "Cannot get major number");
		if ((created = make_nodes(major, BASENAME_NODES, NUM_NODES)) < NUM_NODES)
			warnx("Cannot create all nodes, only %d/%d have been created.", created, NUM_NODES);
	}

	/* detach from the terminal if needed */
	if (DAEMONIZE) {
		dbg("Going to background");
		if (daemon(0,0) != 0)
			err(EXIT_FAILURE, "Cannot daemonize");
	}

	/* wait to keep the line discipline enabled, wake it up with a signal */
	signal(SIGINT, signal_callback_handler);
	signal(SIGTERM, signal_callback_handler);
	pause();
	
	/* remove the created virtual TTYs */
	if (CREATE_NODES) {
		remove_nodes(BASENAME_NODES, NUM_NODES);
	}

	/* close the serial line */
	close(serial_fd);

	return EXIT_SUCCESS;
}