unsigned int rpi_nano_get_sigfox_id(int fd)
{
	RPI_NANO_AT_MODE_ON();
	
	//Create AT commend.
	char buf[_RPI_NANO_SIZE_BUF]="AT$V01\n";

	//Send AT commend.
	if(write(fd, buf, strlen(buf)) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		RPI_NANO_AT_MODE_OFF();
		return 0;
	}
	
	//Read response.
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, -1, -1) == -1)
	{
		RPI_NANO_AT_MODE_OFF();
		return 0;
	}
	RPI_NANO_AT_MODE_OFF();
		
	//Parse and check response.
	unsigned int id;
	if(sscanf(buf, "%x", &id) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		return 0;
	}
	
	return id;
}
int rpi_nano_reboot(int fd)
{
	RPI_NANO_AT_MODE_ON();
	
	//Send AT commend.
	if(write(fd, "ATR\n", 4) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
	
	//Read response.
	char buf[_RPI_NANO_SIZE_BUF];
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, -1, -1) == -1)
	{
		fprintf(stderr, "ERROR - %s: Can't read correctly.\n", __func__);
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
	RPI_NANO_AT_MODE_OFF();
	
	//Parse and check response.
	if(strstr(buf, "RESTARTING") == NULL)
	{
		fprintf(stderr, "ERROR - %s: Bad response from ARM-nano.\n", __func__);
		return -1;
	}
	
	//Read for wait reboot.
	usleep(_RPI_NANO_TIME_BOOT);
	
	return 0;
}
//! \todo modifier le timeout qui est actuellement à 15s
int _rpi_nano_switch_to_AT(int fd)
{
	//Read response.
	char buf[_RPI_NANO_SIZE_BUF];
	
	
	
	//Switch to AT commend mode.
	if(write(fd, "+++", 3) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		return -1;
	}

	
	//First timeout 15s
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, 15000000, -1) == -1)
	{
		fprintf(stderr, "ERROR - %s:  Can't read correctly.\n", __func__);
		return -1;
	}

	//Parse and check response.
	if(strstr(buf, "+++") != NULL) //If echo ("+++") the ARM-nano is already AT commend mode.
		return 0;
	if(strstr(buf, "WELCOME IN SETUP MODE") == NULL)
	{	
		fprintf(stderr,"Received:%s\n",buf);
		fprintf(stderr, "ERROR - %s: Bad response from ARM-nano.\n", __func__);
		return -1;
	}
	
	return 0;
}
//! \todo remove sleep function
int _rpi_nano_exit_AT(int fd)
{
	//Send AT commend.
	if(write(fd, "ATQ\n", 4) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		return -1;
	}
	
	//Read response.
	//sleep(4);
	char buf[_RPI_NANO_SIZE_BUF];
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, -1, -1) == -1)
	{
		fprintf(stderr, "ERROR - %s:  Can't read correctly.\n", __func__);
		return -1;
	}
	
	if(strstr(buf, "QUIT SETUP") == NULL)
	{
		fprintf(stderr, "ERROR - %s: Bad response from ARM-nano.\n", __func__);
		return -1;
	}
	usleep(100000);
	if (tcflush(fd,TCIFLUSH) == -1){
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		return -1;
	}
	return 0;
}
rpi_nano_version_e rpi_nano_get_version(int fd)
{
	RPI_NANO_AT_MODE_ON();
		
	//Send AT commend.
	if(write(fd, "ATV\n", 4) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__,  strerror(errno));
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
	
	//Read response.
	char buf[_RPI_NANO_SIZE_BUF];
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, -1, -1) == -1)
	{
		fprintf(stderr, "ERROR - %s: Can't read correctly.\n", __func__);
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
	
	RPI_NANO_AT_MODE_OFF();
	
	//Parse response.
	char* bufPower = strrchr(buf, ' ');
	if(bufPower == NULL)
	{
		fprintf(stderr, "ERROR - %s: Bad response from ARM-nano.\n", __func__);
		return -1;
	}
	*bufPower='\0';
	bufPower++;
	bufPower[5] = '\0';
	char* bufFrequency = strrchr(buf, ' ');
	if(bufFrequency == NULL)
	{
		fprintf(stderr, "ERROR - %s: Bad response from ARM-nano.\n", __func__);
		return -1;
	}
	bufFrequency++;

	//Read response.
	if(strcmp(bufFrequency, "868MHZ") == 0)
	{
		if(strcmp(bufPower, "27DBM") == 0)
			return RPI_NANO_VERSION_N8LD;
		return RPI_NANO_VERSION_N8LP;
	}
	
	RPI_NANO_AT_MODE_OFF();
	return RPI_NANO_VERSION_N4;
}
Example #6
0
//! \bug Le premier caractère envoyer est bizarre (seulement sur le Raspberry pi ?)
int rpi_nano_init(	const char* path,
					rpi_nano_uart_baudrate_e baudrate,
					rpi_nano_uart_parity_e parity,
					rpi_nano_uart_stopbit_e stopbit)
{
	//Gpio reset in output.
	if(_rpi_nano_gpio_reset_export_output() == -1)
	{
		return -1;
	}
	if(rpi_nano_enable_reset() == -1)
	{
		_rpi_nano_gpio_reset_unexport_output();
		return -1;
	}
	
	//Open serial port
	int rpi_nano_fd;
	if(path != NULL)
		rpi_nano_fd = open(path, O_RDWR);
	else
		rpi_nano_fd = open("/dev/ttyAMA0", O_RDWR);
	
	if(rpi_nano_fd == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		return -1;
	}
	
	//Set parameter for configure the uart
	if(_rpi_nano_set_fd_parameter(rpi_nano_fd, baudrate, parity, stopbit) == -1)
	{
		close(rpi_nano_fd);
		return -1;
	}
	
	//Enable the module.
	if(rpi_nano_disable_reset() == -1)
	{
		_rpi_nano_gpio_reset_unexport_output();
		close(rpi_nano_fd);
		return -1;
	}
	
	//Booting
	sleep(3);
	char buf[_RPI_NANO_SIZE_BUF];
	_rpi_nano_read(rpi_nano_fd, buf, _RPI_NANO_SIZE_BUF, _RPI_NANO_SIZE_BUF, -1);
	
	return rpi_nano_fd;
}
int _rpi_nano_set_reg(int fd, char* reg, int val)
{
	//Create AT commend.
	char buf[_RPI_NANO_SIZE_BUF];
	fprintf(stderr, "AT%s=%.2x\n", reg, val);
	sprintf(buf, "AT%s=%.2x\n", reg, val);

	//Send AT commend.
	if(write(fd, buf, strlen(buf)) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		return -1;
	}
	
	//Read response.
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, _RPI_NANO_READ_TIMEOUT*2, -1) == -1)
		return -1;
	
	//Parse response.
	sscanf(buf, "%s", buf);
	
	//Extract the at commend on the value.
	char* bufVal = strchr(buf, '=');
	if(bufVal == NULL)
	{
		fprintf(stderr, "ERROR - %s: Bad response from ARM-nano.\n", __func__);
		return -1;
	}
	*bufVal = '\0';
	bufVal++;
	bufVal[2]= '\0';
	
	//Convert the value.
	int _val = 0;
	sscanf(bufVal, "%x", &_val);
		
		
	if(_val == -1 || _val!=val)
	{
		fprintf(stderr, "ERROR - %s: The value didn't change.\n", __func__);
		return -1;
	}
	
	return 0;
}
int rpi_nano_start_ping_pong(int fd, rpi_nano_mode_ping_pong_e mode)
{
	RPI_NANO_AT_MODE_ON();
	
	//Create AT commend.
	char buf[_RPI_NANO_SIZE_BUF];
	
	if(mode == RPI_NANO_MODE_PING_PONG_MASTER)
		strcpy(buf, "ATT00\n");
	else
		strcpy(buf, "ATT01\n");

	//Send AT commend.
	if(write(fd, buf, strlen(buf)) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
	
	//Read response.
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, -1, -1) == -1)
	{
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
		
	//Parse and check response.
	if(strstr(buf, "Entering in ping-pong mode") == NULL)
	{
		fprintf(stderr, "ERROR - %s: Bad response from ARM-nano.\n", __func__);
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
	
	return 0;
}
int _rpi_nano_get_reg(int fd, char* reg, int* val)
{
	//Create AT commend.
	char buf[_RPI_NANO_SIZE_BUF];
	sprintf(buf, "AT%s\n", reg);

	//Send AT commend.
	if(write(fd, buf, strlen(buf)) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		return -1;
	}
	
	//Read response.
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, -1, -1) == -1)
		return -1;
	
	////Parse response.
	//sscanf(buf, "%s", buf);
	
	//Extract the at commend on the value.
	char* bufVal = strchr(buf, '=');
	if(bufVal == NULL)
	{
		fprintf(stderr, "ERROR - %s: Bad response from ARM-nano.\n", __func__);
		return -1;
	}
	*bufVal = '\0';
	bufVal++;
	bufVal[2]= '\0';
	
	//Convert the value.
	sscanf(bufVal, "%x", val);
		
	return 0;
}
int rpi_nano_rssi(int fd)
{
	int rrssi = -1;
	
	RPI_NANO_AT_MODE_ON();
	
	//Create AT commend.
	char buf[_RPI_NANO_SIZE_BUF];
	strcpy(buf, "ATT0A\n");

	//Send AT commend.
	if(write(fd, buf, strlen(buf)) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
	
	//Read response.
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, _RPI_NANO_READ_TIMEOUT, -1) == -1)
	{
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
		
	//Parse and check response.
	if(strstr(buf, "Entering in spectrum-analyser mode") == NULL)
	{
		fprintf(stderr, "ERROR - %s: Bad response from ARM-nano.\n", __func__);
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
	
	//Read response.
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, _RPI_NANO_READ_TIMEOUT*50, -1) == -1)
	{
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
	
	//Parse Rssi value.
	char* ptrbuf = strstr(buf, "RSSI:");
	sscanf(ptrbuf, "RSSI:%idBm", &rrssi);

	
	//Send stop commend.
	if(write(fd, "a", 1) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		return -1;
	}
	
	//Read response for clear.
	if(_rpi_nano_read(fd, buf, _RPI_NANO_SIZE_BUF, -1, -1) == -1)
	{
		RPI_NANO_AT_MODE_OFF();
		return -1;
	}
	
	RPI_NANO_AT_MODE_OFF();
	
	return rrssi;
}
int rpi_nano_stop_ping_pong(int fd)
{
	//Send stop commend.
	if(write(fd, "a", 1) == -1)
	{
		fprintf(stderr, "ERROR - %s: %s\n", __func__, strerror(errno));
		return -1;
	}

	//Read all.
	char buf[_RPI_NANO_SIZE_BUF*2+1];
	char buf0[_RPI_NANO_SIZE_BUF+1];
	char buf1[_RPI_NANO_SIZE_BUF+1];
	char* bufptr = buf0;
	int n0 = 0;
	int n1 = 0;
	int n = 0;
	
	while(1)
	{
		//First timeout 4s
		n = _rpi_nano_read(fd, bufptr, _RPI_NANO_SIZE_BUF, 4000000, -1);
		
		if(n==0)
			break;
			
		bufptr[n] = '\0';
		
		if((n == 1) && (bufptr[0] == 'A'))
			break;

		//Switch buffer
		if(bufptr == buf0)
		{
			n0 = n;
			bufptr = buf1;
			
			memcpy(buf, buf1, n1);
			memcpy(buf+n1, buf0, n0);
		}
		else
		{
			n1 = n;
			bufptr = buf0;
			
			memcpy(buf, buf0, n0);
			memcpy(buf+n0, buf1, n1);
		}
		
		//Parse response.
		buf[n0+n1] = '\0';
		if(strstr(buf, "Test mode stopped") != NULL)
		{
			RPI_NANO_AT_MODE_OFF();
			return 0;
		}
			
	}
	
	fprintf(stderr, "ERROR - %s: Bad response from ARM-nano.\n", __func__);
	return -1;
}
int rpi_nano_status_ping_pong(int fd, char rssiResult[], ssize_t sizeRssi, char berResult[], ssize_t sizeBer, char perResult[], ssize_t sizePer )
{
	
	//Check the size status
	if(sizeRssi < 16)
	{
		fprintf(stderr, "ERROR - %s: The size of your 'rssiResult' is too small.\n", __func__);
		return -1;
	}else if(sizeBer<10){
		fprintf(stderr, "ERROR - %s: The size of your 'berResult' is too small.\n", __func__);
		return -1;
	}else if(sizePer<10){
		fprintf(stderr, "ERROR - %s: The size of your 'perResult' is too small.\n", __func__);
		return -1;		
	}
	
	//Read all.
	char buf[_RPI_NANO_SIZE_BUF*2*2+1];
	char buf0[_RPI_NANO_SIZE_BUF*2+1];
	char buf1[_RPI_NANO_SIZE_BUF*2+1];
	char* bufptr = buf0;
	int n0 = 0;
	int n1 = 0;
	int n = 0;
	
	//First timeout 2s.
	int timeout = 2000000;
	
	while(1)
	{
		n = _rpi_nano_read(fd, bufptr, _RPI_NANO_SIZE_BUF*2, timeout, -1);
		timeout = _RPI_NANO_READ_TIMEOUT;
		
		if(n==0)
			break;
			
		bufptr[n] = '\0';
		
		//Switch buffer
		if(bufptr == buf0)
		{
			n0 = n;
			bufptr = buf1;
			
			memcpy(buf, buf1, n1);
			memcpy(buf+n1, buf0, n0);
		}
		else
		{
			n1 = n;
			bufptr = buf0;
			
			memcpy(buf, buf0, n0);
			memcpy(buf+n0, buf1, n1);
		}
	}
	//Parse the last test.
	char* per = strstr(buf, "PER:");
	char* ber = strstr(buf, "BER:");
	char* rssi = strstr(buf, "RSSI:");
	char* dbm1 ;
	char* dbm2;
	if (rssi != NULL){
		dbm1 = strstr(rssi, "dBm/");
		dbm2 = strstr(dbm1+3, "dBm");
	}else{
		dbm1=NULL;
		dbm2=NULL;
	}
	if ((per != NULL) && (perResult != NULL)){
		strncpy(perResult, per+4, 9);
		perResult[9] = '\0';
	}else{
		perResult[0] = '\0';
	}
	if ((ber != NULL) && (berResult != NULL)){
		strncpy(berResult, ber+4, 9);
		berResult[9] = '\0';
	}else{
		berResult[0] = '\0';
	}
	if ((rssi != NULL) && (rssiResult != NULL)){
		printf("%i\n",(dbm2+3)-(rssi+5));
		strncpy(rssiResult, rssi+5, (dbm2+3)-(rssi+5));
		rssiResult[(dbm2+3)-(rssi+5)] = '\0';
	}else{
		rssiResult[0] = '\0';
	}
	printf("%s %s %s\n", perResult, berResult, rssiResult);
	return 0;
}