size_t modem_readline(char *buffer, size_t max, uint32_t timeout) {
  timer_set_timeout(timeout * 1000);
  size_t idx = 0;
  while (idx < max) {
    if (!timer_timeout_remaining()) break;

    int c = modem_read();
    if (c == -1) {
      // nothing in the buffer, allow some sleep
      __WFI();
      continue;
    }

    if (c == '\r') continue;
    if (c == '\n') {
      if (!idx) {
        idx = 0;
        continue;
      }
      break;
    }
    if (max - idx && isprint(c)) buffer[idx++] = (char) c;
  }

  buffer[idx] = 0;
  return idx;
}
size_t modem_read_binary(uint8_t *buffer, size_t max, uint32_t timeout) {
  timer_set_timeout(timeout * 1000);
  size_t idx = 0;
  while (idx < max) {
    if (!timer_timeout_remaining()) break;
    int c = modem_read();
    if (c == -1) {
      // nothing in the buffer, allow some sleep
      __WFI();
      continue;
    }
    if (max - idx) buffer[idx++] = (uint8_t) c;
  }

  return idx;
}
void *rx_thread_fnc(void* ptr)
{

    printf(": Running Rx thread...\n");

    while(modem_running()) {
        modem_read((uint64_t*)rx_buffer, RX_BUF_SIZE);

#if(DEBUG>0)
        int i = 0;
        printf("-------Receiver Side-------\n");

        printf("Received Header Data\n");
        for(i = 0; i < HEADER_FRAME_SIZE+HEADER_FRAME_SIZE; i++)
            printf("%d, ", rx_buffer[i]);
        printf("\n");
#endif
        if(*(uint64_t*)&(rx_buffer[RX_BUF_SIZE - CRC_SIZE]))
            printf("RADIO: CRC ERROR\n");
#if(DEBUG>0)
        printf("Received Full Payload\n");
        for(i = 0; i < TX_BUF_SIZE; i++) {
            printf("%d, ", rx_buffer[i]);
        }
        printf("\n");

        if (INTERNAL_PACKET_GEN > 0) {
            printf("|");
            // Use for internal packet generation print
            for(i = 0; i < RX_BUF_SIZE; i=i+8) {
                printf("%c", rx_buffer[i]);
            }
            printf("|\n");
        }
#endif
        break;
    }

    printf("Exiting Rx thread\n");

    return NULL;
}
示例#4
0
static void
usb_modem_data_stress_test(struct modem *p, uint32_t duration)
{
	struct timeval sub_tv;
	struct timeval ref_tv;
	struct timeval res_tv;

	time_t last_sec;

	uint8_t in_pending = 0;
	uint8_t in_ready = 0;
	uint8_t out_pending = 0;

	uint32_t id = 0;

	uint32_t in_max;
	uint32_t out_max;
	uint32_t io_max;

	uint8_t *in_buffer = 0;
	uint8_t *out_buffer = 0;

	gettimeofday(&ref_tv, 0);

	last_sec = ref_tv.tv_sec;

	printf("\n");

	in_max = libusb20_tr_get_max_total_length(p->xfer_in);
	out_max = libusb20_tr_get_max_total_length(p->xfer_out);

	/* get the smallest buffer size and use that */
	io_max = (in_max < out_max) ? in_max : out_max;

	if (in_max != out_max)
		printf("WARNING: Buffer sizes are un-equal: %u vs %u\n", in_max, out_max);

	in_buffer = malloc(io_max);
	if (in_buffer == NULL)
		goto fail;

	out_buffer = malloc(io_max);
	if (out_buffer == NULL)
		goto fail;

	while (1) {

		gettimeofday(&sub_tv, 0);

		if (last_sec != sub_tv.tv_sec) {

			printf("STATUS: ID=%u, RX=%u bytes/sec, TX=%u bytes/sec, ERR=%d\n",
			    (int)id,
			    (int)p->rx_bytes.bytes,
			    (int)p->tx_bytes.bytes,
			    (int)p->errors);

			p->rx_bytes.bytes = 0;
			p->tx_bytes.bytes = 0;

			fflush(stdout);

			last_sec = sub_tv.tv_sec;

			id++;
		}
		timersub(&sub_tv, &ref_tv, &res_tv);

		if ((res_tv.tv_sec < 0) || (res_tv.tv_sec >= (int)duration))
			break;

		libusb20_dev_process(p->usb_dev);

		if (!libusb20_tr_pending(p->xfer_in)) {
			if (in_pending) {
				if (libusb20_tr_get_status(p->xfer_in) == 0) {
					modem_read(in_buffer, libusb20_tr_get_length(p->xfer_in, 0));
				} else {
					p->errors++;
					usleep(10000);
				}
				in_pending = 0;
				in_ready = 1;
			}
			if (p->loop_data == 0) {
				libusb20_tr_setup_bulk(p->xfer_in, in_buffer, io_max, 0);
				libusb20_tr_start(p->xfer_in);
				in_pending = 1;
				in_ready = 0;
			}
		}
		if (!libusb20_tr_pending(p->xfer_out)) {

			uint32_t len;
			uint32_t dly;

			if (out_pending) {
				if (libusb20_tr_get_status(p->xfer_out) != 0) {
					p->errors++;
					usleep(10000);
				}
			}
			if (p->random_tx_length) {
				len = ((uint32_t)usb_ts_rand_noise()) % ((uint32_t)io_max);
			} else {
				len = io_max;
			}

			if (p->random_tx_delay) {
				dly = ((uint32_t)usb_ts_rand_noise()) % 16000U;
			} else {
				dly = 0;
			}

			if (p->loop_data != 0) {
				if (in_ready != 0) {
					len = libusb20_tr_get_length(p->xfer_in, 0);
					memcpy(out_buffer, in_buffer, len);
					in_ready = 0;
				} else {
					len = io_max + 1;
				}
				if (!libusb20_tr_pending(p->xfer_in)) {
					libusb20_tr_setup_bulk(p->xfer_in, in_buffer, io_max, 0);
					libusb20_tr_start(p->xfer_in);
					in_pending = 1;
				}
			} else {
				modem_write(out_buffer, len);
			}

			if (len <= io_max) {
				libusb20_tr_setup_bulk(p->xfer_out, out_buffer, len, 0);

				if (dly != 0)
					usleep(dly);

				libusb20_tr_start(p->xfer_out);

				out_pending = 1;
			}
		}
		libusb20_dev_wait_process(p->usb_dev, 500);

		if (libusb20_dev_check_connected(p->usb_dev) != 0) {
			printf("Device disconnected\n");
			break;
		}
	}

	libusb20_tr_stop(p->xfer_in);
	libusb20_tr_stop(p->xfer_out);

	printf("\nData stress test done!\n");

fail:
	if (in_buffer)
		free(in_buffer);
	if (out_buffer)
		free(out_buffer);
}
示例#5
0
/**
	@func wmmp_readAppData 
	@brief 读取wmmp接收数据
	@param[in] flowID wmmp流通道
	@param[out] buf 输出BUFF指针
	@param[in-out] len 输入BUFF的最大长度,输出接收数据的实际长度
	@return   0-成功;	其他值-失败 
 */
int wmmp_readAppData(int flowID, char *buf, int *len)
{
	int rc;
	int i;
	int tmpflowID;
	int tmplen;
	int readlen;
	int timeout;
	time_t now,last;
	int retry;

	//char testbuf[4096];
	//char *p;
	char ch;

#ifdef WMMP_READ_DEBUG
	int fp;
#endif



#ifdef WMMP_READ_DEBUG
	fp = fopen("/temp.txt","ab+");
	if(fp == -1)
	{
		DEBUG_PRINTF("open file error\n");
		exit -1;
	}
	fwrite("start", 5, 1, fp);
#endif

	//int fd_ttys2;
	//fd_ttys2 = port_open("/dev/ttyS2");

	//char recbuf[APP_RECV_PACKET_MAXLEN*2+1]="";

	DEBUG_PRINTF("wmmp read data\n");

        retry = 2;
	while(retry --)
	{
		//modem_flushInputBuffer();
		rc = wmmp_waitResponse(NULL,60);
		if(rc != 0)
		{
			DEBUG_PRINTF("WMMP get response error: %x\n",rc);
			goto ExitProcessing;
		}
//	printf("rev4 = %s\n", wmmp_rx_buf.line[0]);
//	printf("*retry = %d\n", retry);

		if(wmmp_rx_buf.enumResponseType != RESPONSE_WMMP_MSG)
		{
			DEBUG_PRINTF("unexpected response: %d\n",wmmp_rx_buf.enumResponseType);
				//rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE);
				//goto ExitProcessing;
				continue;
		}
	

		if(sscanf(wmmp_rx_buf.line[0],"$M2MMSG: 7,%d,%d",&tmpflowID,&readlen) != 2)
		{
			DEBUG_PRINTF("expected msg not found\n");
			//rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE);
			//goto ExitProcessing;
			continue;
		}

		/*printf("flowID = %d \nlen = %d\n",tmpflowID, readlen);*/
		if(tmpflowID != flowID)
		{
			DEBUG_PRINTF("unexpected flowID\n");
			rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE);
			goto ExitProcessing;
		}

		if((readlen > APP_RECV_PACKET_MAXLEN) || (readlen > *len))
		{
			DEBUG_PRINTF("readlen overflow!!!readlen=%d\n",readlen);
			rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_APPREAD_OVERFLOW);
			goto ExitProcessing;
		}
		break;
	}

	if( retry < 0 )
	{
		DEBUG_PRINTF("expected msg not found\n");
		rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_BAD_RESPONSE);
		goto ExitProcessing;
	}

#if 0
	//wait data ?
	timeout = 5000;
	do
	{
		tmplen = modem_charsInBuffer();
		//DEBUG_PRINTF("read data len = %d\n",tmplen);
		timeout --;
	}while( (tmplen < (readlen*2)) && (timeout >0));

	if(timeout <= 0)
	{
		DEBUG_PRINTF("read data len = %d\n",tmplen);
		DEBUG_PRINTF("read data timeout\n");
//		rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,MODEM_DEVICE_TIMEOUT);
//		goto ExitProcessing;
	}
	//read data into wmmp_rx_buf.buf
	modem_read(wmmp_rx_buf.buf,readlen*2);
#endif

#if 0
	//DEBUG_PRINTF("init response rx buffer\n");
	sleep(3);
	wmmp_initRxBuffer();
	time(&last);
	do
	{
#if 0
		tmplen = modem_charsInBuffer();
		if(tmplen != 0)
		{
			modem_read(wmmp_rx_buf.buf_tail,tmplen);
			wmmp_rx_buf.buf_tail += tmplen;
			*(wmmp_rx_buf.buf_tail) = '\0';
			wmmp_rx_buf.rxlen += tmplen;
			DEBUG_PRINTF("read data len = %d\n",wmmp_rx_buf.rxlen);
		}
#else
		//if(modem_charsInBuffer()){
		if(modem_read( wmmp_rx_buf.buf_tail, 1) == 1)
		//if(modem_read( &ch, 1) == 1)
		{
			//fwrite(&ch, 1, 1, fp);
			//modem_read( wmmp_rx_buf.buf_tail, 1);
			//DEBUG_PRINTF("%c",*wmmp_rx_buf.buf_tail);
			//write(fd_ttys2,wmmp_rx_buf.buf_tail,1);
			fwrite(wmmp_rx_buf.buf_tail, 1, 1, fp);
			wmmp_rx_buf.buf_tail++;
//			*(wmmp_rx_buf.buf_tail) = '\0';
			wmmp_rx_buf.rxlen++;
		}
#endif
		if(wmmp_rx_buf.rxlen < readlen*2)
		{
			time(&now);
			if(now > last + 60)			//timeout 60s
			{
				DEBUG_PRINTF("read data len = %d\n",wmmp_rx_buf.rxlen);
				DEBUG_PRINTF("read data timeout\n");
				rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,MODEM_DEVICE_TIMEOUT);
				goto ExitProcessing;
			}
		}
		else
			break;
	}while(1);


#endif

	//20100417 for uart overrun error@115200bps
#if 1
	timeout=35000;
	int recLen = 0;

	/*printf("star receive!\n");*/	
	wmmp_initRxBuffer();
	do
	{
		recLen = modem_read( wmmp_rx_buf.buf_tail, readlen*2-wmmp_rx_buf.rxlen);
		if(recLen != 0)
		{
#ifdef WMMP_READ_DEBUG
			fwrite(wmmp_rx_buf.buf_tail, 1, recLen, fp);
#endif
			wmmp_rx_buf.buf_tail += recLen;
			//			*(wmmp_rx_buf.buf_tail) = '\0';
			wmmp_rx_buf.rxlen += recLen;
			DEBUG_PRINTF("wmmp_rx_buf.rxlen = %d\n", wmmp_rx_buf.rxlen);
		}

	}while((wmmp_rx_buf.rxlen < readlen*2) && timeout-- );

// 	printf("readlen*2 = %d\n",readlen*2);
// 	printf("wmmp_rx_buf.rxlen = %d\n",wmmp_rx_buf.rxlen);

	if(timeout <= 0)
	{
		rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,MODEM_DEVICE_TIMEOUT);
		goto ExitProcessing;
	}


#endif

	//Decode hex into str
	tmplen = str2hex(wmmp_rx_buf.buf,readlen*2,buf,*len);
	/*printf("%s",buf);*/
	//normal return
#ifdef WMMP_READ_DEBUG
	fwrite("end", 3, 1, fp);
	fclose(fp);
#endif
	*len = tmplen;
	return 0;
	//error process
ExitProcessing:
#ifdef WMMP_READ_DEBUG
	fwrite("end", 3, 1, fp);
	fclose(fp);
#endif
	*len = 0;
	return rc;
}
示例#6
0
static int wmmp_waitResponse(char *cmdstr,int timeout)
{
//	char cmd_response[256];
//	int finished = 0;
	char *result_start;
	int error;

	char *seek_start;
	char *seek_end;
	char seek_tag1[64];

	time_t now,last;

	int rc=0;

	//resopnse format: $M2M<CMD>:
	if (cmdstr != NULL )
	{
		//sprintf(cmd_response,"%s",cmdstr);
		DEBUG_PRINTF("keyword:%s\n",cmdstr);
	}

	DEBUG_PRINTF("init response rx buffer\n");
	wmmp_initRxBuffer();

	//init timeout value
	time(&now);
	last = now;

	do
	{
		//DEBUG_PRINTF("state: %d\n",wmmp_rx_buf.enumReceiveState);
		switch(wmmp_rx_buf.enumReceiveState)
		{
		case RECV_STATE_IDLE:
			//read from modem
			//if(modem_charsInBuffer())
			if(modem_read( wmmp_rx_buf.buf_tail, 1) == 1)
			{

				//modem_read( wmmp_rx_buf.buf_tail, 1);
				wmmp_rx_buf.buf_tail++;
				*(wmmp_rx_buf.buf_tail) = '\0';
				wmmp_rx_buf.rxlen++;
				//show rx buff info
				//DEBUG_PRINTF("rx:%s\n",wmmp_rx_buf.buf_head);
			}
			else
			{
				//break switch case
				break;
			}

			/* first check if <cr><lf> is found at end of reply_buf.
			 * the needed length is greater 4 because we don't
			 * need to enter if no result/error will be found. */
			if(wmmp_rx_buf.rxlen >= 6 && !strncmp(wmmp_rx_buf.buf_tail - 2, "\r\n", 2))
			{
				//DEBUG_PRINTF("%s",wmmp_rx_buf.buf);
				DEBUG_PRINTF("find <cr><lf> \n");
				/* try to find previous <cr><lf> */
					result_start = findcrlfbw(wmmp_rx_buf.buf_tail - 2, wmmp_rx_buf.rxlen);
				/* if not found, start at buffer beginning */
				if (!result_start)
				{
					DEBUG_PRINTF("first <cr><lf> missed!!!\n");
					//ignore the words before <cr><lf>
					//update buf_head
					wmmp_rx_buf.buf_head=wmmp_rx_buf.buf_tail -2;
					wmmp_rx_buf.rxlen = 2;
					//to do ...
					//continue the loop;
					break;

				}

				//update buf_head
					wmmp_rx_buf.buf_head = result_start -2;				
				/* there are certainly more that 2 chars in buffer */
				// <cr><lf>OK<cr><lf>
				if (!strncmp(result_start, "OK", 2))
				{
					//OK response
					DEBUG_PRINTF("OK found\n");
					wmmp_rx_buf.enumResponseType = RESPONSE_OK;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;

				}
				//cmd response with output
				else if( (cmdstr != NULL ) && (!strncmp(result_start, cmdstr,strlen(cmdstr)) ) )
				{
					//
					DEBUG_PRINTF("%s found\n",cmdstr);
					wmmp_rx_buf.enumResponseType = RESPONSE_WMMP_CMD_OK;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_WAIT_END_TAG;
					seek_start = wmmp_rx_buf.buf_tail + 1 ;
				}

				//async ack
				else if( !strncmp(result_start, "$M2MACK",7 ) )
				{
					//
					DEBUG_PRINTF("$M2MACK found\n");
					wmmp_rx_buf.enumResponseType = RESPONSE_WMMP_ACK;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_WAIT_END_TAG;
					seek_start = wmmp_rx_buf.buf_tail + 1 ;
				}
				//message
				else if( !strncmp(result_start, "$M2MMSG",7 ))
				{
					//
					DEBUG_PRINTF("$M2MMSG found\n");
					wmmp_rx_buf.enumResponseType = RESPONSE_WMMP_MSG;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_POST_PROCESS;
					//seek_start = wmmp_rx_buf.buf_tail + 1 ;
					//sprintf(seek_tag,"\r\n");
				}
#if (SIMCOM==1)
				else if( !strncmp(result_start, "$M2M$MSG",8 )) //yy 20110818
				{					
					DEBUG_PRINTF("$M2M$MSG found\n");
					wmmp_rx_buf.enumResponseType = RESPONSE_WMMP_SEND_MSG;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_POST_PROCESS;
					//seek_start = wmmp_rx_buf.buf_tail + 1 ;
					//sprintf(seek_tag,"\r\n");
				}
#endif
				else if (sscanf(result_start, "+CME ERROR: %d", &error) == 1)
				{
					DEBUG_PRINTF("+CME ERROR found\n");
					rc = MODEM_ERROR_NUM(MODEM_ERROR_CME,error);
					wmmp_rx_buf.enumResponseType = RESPONSE_ERROR_CME;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
				}
				else if (sscanf(result_start, "+CMS ERROR: %d", &error) == 1)
				{
					DEBUG_PRINTF("+CMS ERROR found\n");
					rc = MODEM_ERROR_NUM(MODEM_ERROR_CMS,error);
					wmmp_rx_buf.enumResponseType = RESPONSE_ERROR_CMS;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
				}
				else if (!strncmp(result_start, "RING", 4))
				{
					DEBUG_PRINTF("RING found\n");
					rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,MODEM_DEVICE_RING);
					wmmp_rx_buf.enumResponseType = RESPONSE_DEVICE_RING;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
				}
				else if (!strncmp(result_start, "BUSY", 4))
				{
					DEBUG_PRINTF("BUSY found\n");
					rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,MODEM_DEVICE_BUSY);
					wmmp_rx_buf.enumResponseType = RESPONSE_DEVICE_BUSY;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
				}
				else if (!strncmp(result_start, "NO ANSWER", 9))
				{
					DEBUG_PRINTF("NO ANSWER found\n");
					rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,MODEM_DEVICE_NO_ANSWER);
					wmmp_rx_buf.enumResponseType = RESPONSE_DEVICE_NO_ANSWER;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
				}
				else if (!strncmp(result_start, "NO CARRIER", 10))
				{
					DEBUG_PRINTF("NO CARRIER found\n");
					rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,MODEM_DEVICE_NO_CARRIER);
					wmmp_rx_buf.enumResponseType = RESPONSE_DEVICE_NO_CARRIER;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
				}
				else if (!strncmp(result_start, "NO DIALTONE", 11))
				{
					DEBUG_PRINTF("NO DIALTONE found\n");
					rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,MODEM_DEVICE_NO_DIALTONE);
					wmmp_rx_buf.enumResponseType = RESPONSE_DEVICE_NO_DIALTONE;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
				}
				else if (sscanf(result_start, "ERROR: %d", &error) == 1)
				{
					DEBUG_PRINTF("ERROR: found\n");
					rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,error);
					wmmp_rx_buf.enumResponseType = RESPONSE_ERROR_NUM;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
				}
				else if (!strncmp(result_start, "ERROR", 5))
				{
					DEBUG_PRINTF("ERROR found\n");
					rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,MODEM_DEVICE_ERROR_SYNTAX);
					wmmp_rx_buf.enumResponseType = RESPONSE_ERROR;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
				}
				else
				{
					DEBUG_PRINTF("no keyword found!!!\n");
					rc = MODEM_ERROR_NUM(MODEM_ERROR_WMMP,ERROR_WMMP_UNKOWN);
					wmmp_rx_buf.enumResponseType = RESPONSE_UNKOWN;
					wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
				}
			}
			else if( !strncmp(wmmp_rx_buf.buf_head, "\r\n>", 3))
			{
				DEBUG_PRINTF("prompt: > found \n");
				wmmp_rx_buf.enumResponseType = RESPONSE_PROMPT;
				wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;

			}
			else
			{

			}
			break;
		case RECV_STATE_WAIT_END_TAG:
			//read from modem
			//if(modem_charsInBuffer())
			if(modem_read( wmmp_rx_buf.buf_tail, 1) == 1)
			{
				//modem_read( wmmp_rx_buf.buf_tail, 1);
				wmmp_rx_buf.buf_tail++;
				*(wmmp_rx_buf.buf_tail) = '\0';
				wmmp_rx_buf.rxlen++;

			}
			else
			{
				//break switch case
				break;
			}

			//parse response
			seek_end = wmmp_rx_buf.buf_tail;

			//$M2MMSG...<cr><lf>
			if(wmmp_rx_buf.enumResponseType == RESPONSE_WMMP_MSG)
			{
				if( !strncmp(wmmp_rx_buf.buf_tail - 2, "\r\n", 2) )
				{
					DEBUG_PRINTF("$M2MMSG received\n");
					wmmp_rx_buf.enumReceiveState = RECV_STATE_POST_PROCESS;
				}

			}
			else
			{
				if(!strncmp(wmmp_rx_buf.buf_tail - 2, "\r\n", 2))
				{
					/* try to find previous <cr><lf> */
					result_start = findcrlfbw(wmmp_rx_buf.buf_tail - 2, wmmp_rx_buf.rxlen);
					/* if not found, start at buffer beginning */
					if (!result_start)
					{
						//result_start = rbuf;
						//ignore the words before <cr><lf>
						//to do ...
						//continue the loop;
						DEBUG_PRINTF("Where am I?!!!\n");

					}

					/* there are certainly more that 2 chars in buffer */

					// <cr><lf>OK<cr><lf>
					if (!strncmp(result_start, "OK", 2))
					{
						DEBUG_PRINTF("OK received\n");
						wmmp_rx_buf.enumReceiveState = RECV_STATE_POST_PROCESS;

					}
					else if (!strncmp(result_start, "ERROR", 5))
					{
						DEBUG_PRINTF("ERROR received\n");
						wmmp_rx_buf.enumResponseType = RESPONSE_WMMP_ERROR;
						wmmp_rx_buf.enumReceiveState = RECV_STATE_POST_PROCESS;
					}

				}
			}

			break;

		case RECV_STATE_POST_PROCESS:
			wmmp_responsePostProcess();
			wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
			break;

		}

		//time out process.
		time(&now);
		if ( now > last + timeout)
		{
			DEBUG_PRINTF("time out!\n");
			rc = MODEM_ERROR_NUM(MODEM_ERROR_DEVICE,MODEM_DEVICE_TIMEOUT);
			wmmp_rx_buf.enumReceiveState = RECV_STATE_END_TAG_DETECTED;
		}

	}while(wmmp_rx_buf.enumReceiveState != RECV_STATE_END_TAG_DETECTED);

	return rc;
}
示例#7
0
static int process_incoming_call(void)
{
	struct vboxuser	 vboxuser;
	struct vboxcall	 vboxcall;
	unsigned char		 line[VBOXMODEM_BUFFER_SIZE + 1];
	int					 haverings;
	int					 waitrings;
	int					 usersetup;
	int					 ringsetup;
	int					 inputisok;
	unsigned char		*stop;
	unsigned char		*todo;

	memset(&vboxuser, 0, sizeof(vboxuser));
	memset(&vboxcall, 0, sizeof(vboxcall));

	haverings =  0;
	waitrings = -1;
	usersetup =  0;
	ringsetup =  0;

	while (modem_read(&vboxmodem, line, modemsetup.ringtimeout) == 0)
	{
		inputisok = 0;

			/* Wenn der Benutzer der angerufenen Nummer ermittelt ist und	*/
			/* dessen Konfigurations abgearbeitet wurde, wird überprüft ob	*/
			/* der Anruf angenommen werden soll.									*/

		if ((usersetup) && (ringsetup))
		{
			if (waitrings >= 0)
			{
				todo = savettydname;
				stop = ctrl_exists(vboxuser.home, "answer", todo);

				if (!stop)
				{
					todo = NULL;
					stop = ctrl_exists(vboxuser.home, "answer", todo);
				}

				if (stop)
				{
					log_line(LOG_D, "Control \"vboxctrl-answer\" detected: %s (%s)...\n", stop, ((char *)todo ? (char *)todo : "global"));

					if ((strcasecmp(stop, "no") == 0) || (strcasecmp(stop, "hangup") == 0) || (strcasecmp(stop, "reject") == 0))
					{
						log_line(LOG_D, "Incoming call will be rejected...\n");
						
						return(0);
					}

					if (strcasecmp(stop, "now") != 0)
					{
						vboxuser.space	= 0;
						waitrings		= xstrtol(stop, waitrings);
					}
					else
					{
						vboxuser.space = 0;
						waitrings		= 1;
					}

					log_line(LOG_D, "Call will be answered after %d ring(s).\n", waitrings);
				}
			}

			if (waitrings > 0)
			{
				if (haverings >= waitrings)
				{
					return(voice_init(&vboxuser, &vboxcall));
				}
			}
		}

			/* Ring abarbeiten: Beim ersten Ring wird die angerufene	*/
			/* Nummer gesichert, die durch ATS13.7=1 mit einem Slash	*/
			/* an den Ringstring angehängt ist.								*/

		if (strncmp(line, "RING/", 5) == 0)
		{
			inputisok++;
			haverings++;
			
			if (!ringsetup)
			{
	         xstrncpy(vboxuser.localphone, &line[5], VBOXUSER_NUMBER);

				ringsetup = 1;
			}				          

			log_line(LOG_A, "%s #%03d (%s)...\n", line, haverings, ((char *)usersetup ? (char *)vboxcall.name : "not known"));
		}

			/* CallerID aus dem Modeminput kopieren. Wenn bereits die	*/
			/* angerufene Nummer ermittelt wurde, wird einmalig die		*/
			/* Konfigurationsdatei des Benutzers abgearbeitet.				*/

		if (strncmp(line, "CALLER NUMBER: ", 15) == 0)
		{
			inputisok++;

			if ((ringsetup) && (!usersetup))
			{
				xstrncpy(vboxuser.incomingid, &line[15], VBOXUSER_CALLID);

				if (userrc_parse(&vboxuser, rc_get_entry(rc_getty_c, "spooldir")) == 0)
				{
					if ((vboxuser.uid != 0) && (vboxuser.gid != 0))
					{
							/* Nachdem "vboxgetty.user" abgearbeitet ist und	*/
							/* ein Benutzer gefunden wurde, werden einige der	*/
							/* Kontrolldateien gelöscht.								*/

						ctrl_remove(vboxuser.home, "suspend", savettydname);
						ctrl_remove(vboxuser.home, "suspend", NULL        );

							/* Die "effective Permissions" des Prozesses auf	*/
							/* die des Benutzers setzen und dessen Konfigurat-	*/
							/* ionsdatei abarbeiten.									*/

						if (set_process_permissions(vboxuser.uid, vboxuser.gid, vboxuser.umask) == 0)
						{
							usersetup = 1;
							waitrings = vboxrc_parse(&vboxcall, vboxuser.home, vboxuser.incomingid);

							if (waitrings <= 0)
							{
								if (waitrings < 0)
									log_line(LOG_W, "Incoming call will be ignored!\n");
								else
									log_line(LOG_D, "Incoming call will be ignored (user setup)!\n");
							}
							else log_line(LOG_D, "Call will be answered after %d ring(s).\n", waitrings);
						}
						else return(-1);
					}
					else log_line(LOG_W, "Useing uid/gid 0 is not allowed - call will be ignored!\n", vboxuser.incomingid);
				}
				else log_line(LOG_W, "Number \"%s\" not bound to a local user - call will be ignored!\n", vboxuser.localphone);
			}
		}

		if (!inputisok)
		{
			log_line(LOG_D, "Got junk line \"");
			log_code(LOG_D, line);
			log_text(LOG_D, "\"...\n");

			continue;
		}
	}

	return(-1);
}
bool modem_enable() {
  char response[10];
  size_t len;

#if BOARD_CELL_PWR_DOMAIN
  CSTDEBUG("GSM #### -- power on\r\n");
  GPIO_WritePinOutput(BOARD_CELL_PWR_EN_GPIO, BOARD_CELL_PWR_EN_PIN, true);
  // TODO check that power has come up correctly
#endif


  // after enabling power, power on the SIM800
  while (modem_read() != -1) /* clear buffer */;

  // we need to identify if the chip is already on by sending AT commands
  // send AT and just ignore the echo and OK to get into a stable state
  // sometimes there is initial noise on the serial line
  modem_send("AT");
  len = modem_readline(response, 9, 500);
  CIODEBUG("GSM (%02d) -> '%s'\r\n", len, response);
  len = modem_readline(response, 9, 500);
  CIODEBUG("GSM (%02d) -> '%s'\r\n", len, response);

  // now identify if the chip is actually on, by issue AT and expecting something
  // if we can't read a response, either AT or OK, we need to run the power on sequence
  modem_send("AT");
  len = modem_readline(response, 9, 1000);
  CIODEBUG("GSM (%02d) -> '%s'\r\n", len, response);

  if (!len) {
    CSTDEBUG("GSM #### !! trigger PWRKEY\r\n");

#if defined(BOARD_UBIRCH_1R02)
    // there is a bug in the circuit on the board which does not use an extra
    // transistor to switch the PWRKEY pin, so the signals are reversed

    // power on the SIM800H
    GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, true);
    delay(10); //10ms
    GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, false);
    delay(1100); // 1.1s
    GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, true);
#else
    // power on the cell phone chip
    GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, false);
    delay(10); //10ms
    GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, true);
    delay(1100); // 1.1s
    GPIO_WritePinOutput(BOARD_CELL_PIN_GPIO, BOARD_CELL_PWRKEY_PIN, false);
#endif
  } else {
    CSTDEBUG("GSM #### !! already on\r\n");
  }

  bool is_on = false;
  // wait for the chip to boot and react to commands
  for (int i = 0; i < 5; i++) {
    modem_send("ATE0");
    // if we still have echo on, this fails and falls through to the next OK
    if ((is_on = modem_expect_OK(1000))) break;
    if ((is_on = modem_expect_OK(1000))) break;
  }

  return is_on;
}