Exemplo n.º 1
0
bool Parser::hasMoreCommands() {
	if (f_.eof()) {	// No more file to check.
		return false;
	}

	// We have to look ahead for commands, skipping comments and blank lines.
	std::istream::streampos parsed_pos 	= f_.tellg(); // Saving stream state.
	std::ios::iostate 		status 		= f_.rdstate();
	f_.clear();

	bool res 					= false;
	unsigned int lines_read 	= 0; 	// So that if there's an error it displays where.
	do {
		std::string s;
		std::getline(f_, s);
		lines_read++;
		trimLeft(s);
		trimRight(s);
		if (!s.empty() && !isComment(s)) { 	// Found a command line...
			if (!validCommand(s)) { 	// ...but it's an invalid line!
				std::cerr << "Error at line ";
				std::cerr << current_line_number_ + lines_read;
				std::cerr << ". '" << s << "': Invalid line" << std::endl;
				throw std::runtime_error("Invalid line");
			}
			res = true;
			goto done;
		}
	} while (!f_.eof());

done:	
	f_.seekg(parsed_pos);		// Restoring stream to its original state.
	f_.setstate(status);
	return res;
}
Exemplo n.º 2
0
void checkCommand(char* readBuffer) {
	char tempBuffer[32] = {};
	CopyMemory(tempBuffer, readBuffer, 12);
	char commandBuffer[32] = {};
	CopyMemory(commandBuffer, readBuffer + 3, 4);
	if (validCommand(readBuffer)) {
		prints(L"Command: %S\n", tempBuffer);
		if (readBuffer[1] == currentID[0]) {
			if ((readBuffer[2] == 'X' || readBuffer[2] == currentID[1]) && listenToRadio) {
				prints(L"Command 2: %S\n", tempBuffer);
				if (streqStart(commandBuffer, "STO", 3)) {
					ResetEvent(startSignal);
					SetEvent(stopSignal);
					if (readBuffer[2] == currentID[1]) {
						respondACK();
					}
				}
				else if (streqStart(commandBuffer, "STA", 3)) {
					ResetEvent(stopSignal);
					SetEvent(startSignal);
					if (readBuffer[2] == currentID[1]) {
						respondACK();
					}
				}
			}
		}
	}
}
Exemplo n.º 3
0
std::vector<Token> Parser::tokenize()
{
	std::vector<Token> tokens;

	spacifyInput();
	std::vector<std::string> words = splitInput();

	int i = 0;
	while (i < words.size()) {
		if (words[i] == "\\") {
			if (i+1 >= words.size())
				throw std::runtime_error("Invalid token");

			if (words[i+1] == "pi") {
				tokens.push_back(Token{CMD_BEG, "pi"});
				tokens.push_back(Token{CMD_END, ""});
				i += 2;
			} else {
				if (i+2 >= words.size() ||
					words[i+2] != "{" ||
					!validCommand(words[i+1]))
					throw std::runtime_error("Invalid token");
				tokens.push_back(Token{CMD_BEG, words[i+1]});
				i += 3;
			}
		} else if (words[i] == "{") {
			tokens.push_back(Token{CMD_CNT, ""});
			i++;
		} else if (words[i] == "}") {
			tokens.push_back(Token{CMD_END, ""});
			i++;
		} else if (words[i] == "^" || words[i] == "_") {
			if (i+1 >= words.size() || words[i+1] != "{")
				throw std::runtime_error("Invalid token");
			tokens.push_back(Token{CMD_BEG, words[i]});
			i += 2;
		} else {
			/* Default case: treat word as raw token */
			tokens.push_back(Token{RAW, words[i]});
			i++;
		}
	}

	return tokens;
}
Exemplo n.º 4
0
CommandType Parser::commandType() const {
// PRE:	current_line_ is a validCommand()
	assert(validCommand(current_line_));
	return commands_.at(field(current_line_, 0)).first;
}
Exemplo n.º 5
0
void receiveCommand() { //the character 'a' was received from the radio, now read the buffer, find all the a-s and check for commands
	DWORD bytesRead = 0;
	DWORD start = 0, length = 0; //the current position in the buffer and the length of the unread buffer
	DWORD errors = 0;
	COMSTAT status = {};
	ClearCommError(hCOMRadio, &errors, &status);
	if (errors) {
		prints(L"ClearCommError errors: %X.\n", errors);
	}
	char readBuffer[128] = {};
	//readCOM(hCOMRadio, readBuffer, 12, bytesRead);
	if (!ReadFile(hCOMRadio, readBuffer, 12, &bytesRead, NULL)) {
		prints(L"Radio ReadFile failed with error: %X.\n", GetLastError());
	}
	length += bytesRead;
	if (bytesRead != 0) {
		prints(L"read %d bytes 1: %S\n", bytesRead, readBuffer);
	}
	//prints(L"Checking command v1: %S, valid: %d\n", readBuffer + start, validCommand(readBuffer));
	if (length == 0) {
		return;
	}

	while (true) {
		if (readBuffer[start] != 'a' && length > 0) {
			++start;
			--length;
		}
		else {
			if (length == 0) {
				//readCOM(hCOMRadio, readBuffer + start + length, 12, bytesRead);
				if(!ReadFile(hCOMRadio, readBuffer + start + length, 12, &bytesRead, NULL)) {
					prints(L"Radio ReadFile failed with error: %X.\n", GetLastError());
				}
				if (bytesRead != 0) {
					prints(L"read %d bytes 2: %S\n", bytesRead, readBuffer);
				}
				//prints(L"Checking command v2: %S, valid: %d\n", readBuffer + start, validCommand(readBuffer));
				length += bytesRead;
				if (length == 0) {
					//prints(L"returning\n");
					return;
				}
				if (!validCommand(readBuffer + start)) {
					++start;
					--length;
				}
				else {
					checkCommand(readBuffer + start);
					++start;
					--length;
				}
			}
			else {
				//prints(L"Checking command v3: %S, valid: %d\n", readBuffer + start, validCommand(readBuffer));
				checkCommand(readBuffer + start);
				++start;
				--length;
			}
		}
		if (length == 0) {
			//readCOM(hCOMRadio, readBuffer + start, 12, bytesRead);
			if(!ReadFile(hCOMRadio, readBuffer + start, 12, &bytesRead, NULL)) {
				prints(L"Radio ReadFile failed with error: %X.\n", GetLastError());
			}
			if (bytesRead != 0) {
				prints(L"read %d bytes 3: %S\n", bytesRead, readBuffer);
			}
			length += bytesRead;
			//prints(L"Checking command v4: %S, valid: %d\n", readBuffer + start, validCommand(readBuffer));
			if (length == 0) {
				//prints(L"returning\n");
				return;
			}
		}
		if (start > 64) {
			//prints(L"zeroing\n");
			CopyMemory(readBuffer, readBuffer + start, length);
			ZeroMemory(readBuffer + length, 128 - length);
			start = 0;
		}
	}
	//prints(L"returning\n");
}
Exemplo n.º 6
0
//Main program
int main(void) {
	int receivedBytes;//, sentBytes;
	//unsigned char outBuff[BUFFER_MAX];
	unsigned char inBuff[BUFFER_MAX];
	//Reserve memory
	char* cmd = malloc((BUFFER_MAX+1) * sizeof(char));
	char* par = malloc((BUFFER_MAX+1) * sizeof(char));
	struct commandStructure command;
	
	//Threads variables
	pthread_t logThread;
	pthread_t emailThread;
	//pthread_t debugThread;
	pthread_t webcamThread;
	pthread_t emailPhotoThread;
	
	//Empty buffers
	init();
	
	// Open serial file descriptor
	int fd = openSerial();
		
	//Loop to scan
	while(1){
		receivedBytes = read(fd,inBuff,BUFFER_MAX);
		if(receivedBytes > 0){						// Data found!
			
			if(DEBUG){ 
				printf("\nPayload size: %d\n",receivedBytes);
				int i;
				for(i=0;i<receivedBytes;i++){
					printf("%c",inBuff[i]);
				}
				printf("\n");
			}

			getCmd(inBuff,cmd);
			getPar(inBuff,par);
			int pars = parseParameters(par);
			
			if(!validCommand(cmd)){
				printf("Invalid Command: %s\n\n",cmd);
				continue;
			}else{	
				//printf("Command: %s\n",cmd);
				//int i = 0;
				//printf("Parameters found: %d\n",pars);
				//for(i=0;i<pars;i++) printf("Parameter %d - %s\n",i,PARAMETERS[i]);			
			}
			
			if(compareText(cmd,"Debug")){ //thread is detached so resources can be recycled.
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				
				time_t now = time(NULL);
				printf("%s - %s",PARAMETERS[0],ctime(&now));

				/*
				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&debugThread,&attr,debugFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
				*/
			}

			if(compareText(cmd,"Log")){
				if(pars < 1){
					printf("Error: No message sent\n");
					continue;
				}
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];

				//=======Call logFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&logThread,&attr,logFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
			if(compareText(cmd,"Email")){
				if(pars < 3){  //Need at least the email address and a subject
					printf("Error: Need 3 parameters: address, subject and message\n");
					continue;
				}
				
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				command.par[1] = PARAMETERS[1];
				command.par[2] = PARAMETERS[2];
				
				//=======Call logFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&emailThread,&attr,emailFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
			if(compareText(cmd,"Webcam")){
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];

				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&webcamThread,&attr,webcamFunc,&command))){
					fprintf(stderr,"Error: Could not create webcam thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);				
			}

			if(compareText(cmd,"EmailPhoto")){
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				command.par[1] = PARAMETERS[1];
				command.par[2] = PARAMETERS[2];

				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&emailPhotoThread,&attr,emailPhotoFunc,&command))){
					fprintf(stderr,"Error: Could not create emailPhoto thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
		}else if(receivedBytes == 0){				//No data yet! go back to loop
			continue;					
		}else if(receivedBytes < 0){				//Error reading, exit.
			printf("Error reading from file!\n");
			perror("Error: " );
			close(fd);
			return -1;
		}
		usleep(UDOONEO_POLL_DELAY);	// poll time approx 50mS (faster crashes the app)
	}

	//Free reserved memory
	free((void*)cmd);
	free((void*)par);

	//Close serial's file descriptor
	close(fd);	
}