// File: /workspace/github_cpp_makefile_star2_O1/CDISC2020[P]Projet_satellite_agile/src/planManager/planManager.cpp
// Function: PlanManager::executePlan(Controller*, QueuingPort*, bool)
void PlanManager::executePlan(Controller* control,QueuingPort* ChannelCom, bool mode)
{
	if(plan!=NULL)
	{
		GenericInstruction* currentInst;

		bool jumpInstruction;

		currentInst = plan->getInstruction(plan->ptInstruction);
		jumpInstruction = false;

		time_t t = time(0);
		struct tm * now = localtime(&t);
		cout << "Inst " << plan->ptInstruction+1 << "/" << plan->getnInstructions() << endl;
		cout << "now         = " << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << endl;
		cout << "Instruction = " << currentInst->getHour() << ":" << currentInst->getMin() << ":" << currentInst->getSec() << endl;

		int group = currentInst->getIndex();
		if ((bannedInstructions[group]) == true) jumpInstruction = true;

		if ((jumpInstruction == false))
		{
			if ((now->tm_hour == currentInst->getHour()) & (now->tm_min == currentInst->getMin()) & (now->tm_sec == currentInst->getSec()))
			{
			/*
			/ Codes des messages a envoyer :
			/ 0 = Attitude
			/ 1 = Photo
			/ 2 = RetourController
			/ 3 = PlanFilePath
			/ 4 = Status
			/ 5 = data
			*/
				cout<<"C'est l'heure "<<endl;
				bool start_timeout = false;
				/*--------------Call Controller to execute instruction-------------*/
				if(currentInst->getType() == 'p' || currentInst->getType() == 'a')
				{
					cout << "control EXECUTE!" << endl;
					if(mode)
						control->executeInstruction(currentInst, &responseController);
					else
						responseController=1;
					start_timeout = true;
				}
				else
				{
					cout << "hum probleme" << endl;
					Status S;
					S.code = 4;
					S.errorID = 2; // A changer.
					sprintf(S.description, "Wrong instruction type, instruction #%d : group #%d ", plan->ptInstruction+1,  group);

					ChannelCom->SendQueuingMsg((char*)&S, sizeof(Status));
				}
				/*---------------------------------------------------------*/

				/*-------------Wait for Time Out of the controller response-----------*/
				int time_out = 1000000;
				if (start_timeout == true)
				{
					while (time_out > 0 && responseController == 0)
					{
						usleep(1);
						time_out--;
					}
				}
				/*-------------------------------------------------------------------*/

				/*--------------------   Management of the errors    ----------------*/
				if (responseController != 1)
				{
					bannedInstructions[group] = true;
					if (responseController == -1)
					{
						cout<<"Instruction "<<group<<" banned : error from controller"<<endl;
						Status S_controller;
						S_controller.code = 4;

						if (currentInst->getType() == 'a')
						{
							// The attitude was not reached
							S_controller.errorID = 0; 	// Look the error list
							sprintf(S_controller.description, "Attitude not reached. instruction #%d : group #%d ", plan->ptInstruction+1,  group);
						}
						if (currentInst->getType() == 'p')
						{
							// The photo was not taken
							S_controller.errorID = 1; // Look the error list
							sprintf(S_controller.description, "Photo not talken. instruction #%d : group #%d ", plan->ptInstruction+1,  group);
						}

						cout << S_controller.description << endl;

						ChannelCom->SendQueuingMsg((char*)&S_controller, sizeof(Status));
					}
					if (time_out == 0)
					{
						cout<<"Instruction "<<group<<" banned : timeout."<<endl;
						// Time out in the response of the attitude controller
					}
				}
				/*-------------------------------------------------------------------*/

				/*-----------------   Photo sending to the ComGrdMnger  -------------*/
				if ( responseController == 1  && currentInst->getType() == 'p')
				{
					PlanFilePath PhotoPath;
					PhotoPath.code = 3;
					string aux =  ((PhotoInstruction*)currentInst)->getPhotoName(); // attention chemin
					strcpy (PhotoPath.filepath , aux.c_str()) ;
					printf("Sending the photo %s \n", PhotoPath.filepath);
					ChannelCom->SendQueuingMsg((char*)&PhotoPath, sizeof(PlanFilePath));
				}
				/*-------------------------------------------------------------------*/

				responseController = 0;
				plan->ptInstruction++;


			}
			// Cas où le temps de l'instruction est déjà passé... on l'aura jamais !
			else if ( (now->tm_hour > currentInst->getHour())
				|| ((now->tm_hour == currentInst->getHour()) && (now->tm_min > currentInst->getMin()))
				|| ((now->tm_min  == currentInst->getMin())  && (now->tm_sec > currentInst->getSec())))
			{
				cout<<"L'heure est deja passé "<<plan->ptInstruction<<endl;
				Status S_heure;
				S_heure.code = 4;
				S_heure.errorID = 3;
				ChannelCom->SendQueuingMsg((char*)&S_heure, sizeof(Status));

				plan->ptInstruction++;
			}
		}
		else if (jumpInstruction)
		{
			plan->ptInstruction++;
			cout<<"Instruction jumped"<<endl;
		}


		if (plan->ptInstruction >= plan->getnInstructions()) // Fin d'un plan d'instruction, on efface le plan en cours
		{
			cout << "Le plan est terminé" << endl;
			this->destructPlan();
		}
	}

	else
		cout << "No plan to execute !" << endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x51e7
movl %ecx, -0x4c(%rbp)
movq %rsi, -0x70(%rbp)
movq %rdx, -0x48(%rbp)
movl 0x324(%rdi), %esi
callq 0x4f1e
movq %rbx, -0x38(%rbp)
movq %rax, %r12
xorl %edi, %edi
callq 0x3240
leaq -0x78(%rbp), %rdi
movq %rax, (%rdi)
callq 0x31c0
movq %rax, %r15
movq 0x5011(%rip), %rbx      # 0x9fd0
leaq 0x234c(%rip), %rsi      # 0x7312
movl $0x5, %edx
movq %rbx, %rdi
callq 0x32f0
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl 0x324(%rax), %esi
incl %esi
movq %rbx, %rdi
callq 0x3450
movq %rax, %r13
leaq 0x2877(%rip), %rsi      # 0x786b
movl $0x1, %edx
movq %rax, %rdi
callq 0x32f0
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x4f12
movq %r13, %rdi
movl %eax, %esi
callq 0x3450
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r13, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
leaq 0x22d0(%rip), %rsi      # 0x7318
movl $0xe, %edx
movq %rbx, %rdi
callq 0x32f0
movl 0x8(%r15), %esi
movq %rbx, %rdi
callq 0x3450
movq %rax, %r14
leaq 0x22bc(%rip), %r13      # 0x7327
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x32f0
movl 0x4(%r15), %esi
movq %r14, %rdi
callq 0x3450
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x32f0
movl (%r15), %esi
movq %r14, %rdi
callq 0x3450
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
leaq 0x2253(%rip), %rsi      # 0x7329
movl $0xe, %edx
movq %rbx, %rdi
callq 0x32f0
movq %r12, %rdi
callq 0x4494
movq %rbx, %rdi
movl %eax, %esi
callq 0x3450
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x32f0
movq %r12, %rdi
callq 0x449e
movq %r14, %rdi
movl %eax, %esi
callq 0x3450
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
movq %r12, %r13
movq -0x38(%rbp), %r12
callq 0x32f0
movq %r13, %rdi
callq 0x44a8
movq %r14, %rdi
movl %eax, %esi
callq 0x3450
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
movq %r13, %rdi
callq 0x44bc
leaq 0x8(%r12), %rcx
movq %rcx, -0x58(%rbp)
movslq %eax, %rcx
cmpb $0x0, 0x8(%r12,%rcx)
je 0x523a
je 0x5778
movq (%r12), %rax
incl 0x324(%rax)
leaq 0x22c5(%rip), %rsi      # 0x746c
movl $0x12, %edx
movq 0x4e1d(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
jmp 0x5778
movq 0x4de2(%rip), %rbx      # 0x9fd0
leaq 0x229f(%rip), %rsi      # 0x7494
movl $0x14, %edx
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3190
movq %rcx, -0x60(%rbp)
movl %eax, -0x2c(%rbp)
movl 0x8(%r15), %ebx
movq %r13, %rdi
callq 0x4494
movl %eax, %r14d
movl 0x4(%r15), %r12d
movq %r13, %rdi
callq 0x449e
movq %r13, %rdi
movl %eax, %r13d
movq %r15, -0x68(%rbp)
movl (%r15), %r15d
movq %rdi, -0x40(%rbp)
callq 0x44a8
cmpl %r14d, %ebx
jne 0x5355
cmpl %r13d, %r12d
jne 0x5355
cmpl %eax, %r15d
jne 0x5355
leaq 0x20a4(%rip), %rsi      # 0x7338
movl $0xe, %edx
movq 0x4d30(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
movq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x44b2
cmpb $0x70, %al
je 0x52ef
movq %r14, %rdi
callq 0x44b2
cmpb $0x61, %al
jne 0x5488
leaq 0x2051(%rip), %rsi      # 0x7347
movl $0x10, %edx
movq 0x4cce(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
movq -0x38(%rbp), %r12
leaq 0x6c(%r12), %rdx
cmpb $0x0, -0x4c(%rbp)
je 0x5447
movq -0x70(%rbp), %rdi
movq %r14, %rsi
callq 0x685a
jmp 0x544d
movq -0x68(%rbp), %r15
movl 0x8(%r15), %ebx
movq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x4494
cmpl %eax, %ebx
movq -0x38(%rbp), %r12
jle 0x53f7
movq 0x4c54(%rip), %r14      # 0x9fd0
leaq 0x20d0(%rip), %rsi      # 0x7453
movl $0x18, %edx
movq %r14, %rdi
callq 0x32f0
movq (%r12), %rax
movl 0x324(%rax), %esi
movq %r14, %rdi
callq 0x3450
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
movabsq $0x300000004, %rax      # imm = 0x300000004
leaq -0x120(%rbp), %rsi
movq %rax, (%rsi)
movq -0x48(%rbp), %rdi
movl $0x88, %edx
callq 0x3d76
movq (%r12), %rax
jmp 0x5772
movl 0x8(%r15), %ebx
movq %r14, %rdi
callq 0x4494
cmpl %eax, %ebx
jne 0x541b
movl 0x4(%r15), %ebx
movq %r14, %rdi
callq 0x449e
cmpl %eax, %ebx
jg 0x5375
movl 0x4(%r15), %ebx
movq %r14, %rdi
callq 0x449e
cmpl %eax, %ebx
jne 0x5778
movl (%r15), %ebx
movq %r14, %rdi
callq 0x44a8
cmpl %eax, %ebx
jg 0x5375
jmp 0x5778
movl $0x1, (%rdx)
movq -0x48(%rbp), %r15
movl -0x2c(%rbp), %ebx
movl $0xf4240, %r14d         # imm = 0xF4240
cmpl $0x0, 0x6c(%r12)
jne 0x547c
movl $0x1, %edi
callq 0x3360
leal -0x1(%r14), %eax
cmpl $0x1, %r14d
movl %eax, %r14d
ja 0x545a
xorl %r14d, %r14d
testl %r14d, %r14d
sete %r13b
jmp 0x551d
leaq 0x1ec9(%rip), %rsi      # 0x7358
movl $0xc, %edx
movq 0x4b35(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
movabsq $0x200000004, %rax      # imm = 0x200000004
leaq -0x118(%rbp), %rdi
movq %rax, -0x8(%rdi)
movq -0x38(%rbp), %r12
movq (%r12), %rax
movl 0x324(%rax), %edx
incl %edx
leaq 0x1e6f(%rip), %rsi      # 0x7365
xorl %r13d, %r13d
movl -0x2c(%rbp), %ebx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3090
leaq -0x120(%rbp), %rsi
movq -0x48(%rbp), %r15
movq %r15, %rdi
movl $0x88, %edx
callq 0x3d76
movl 0x6c(%r12), %eax
cmpl $0x1, %eax
je 0x56df
movq -0x58(%rbp), %rcx
movq -0x60(%rbp), %rdx
movb $0x1, (%rcx,%rdx)
cmpl $-0x1, %eax
jne 0x5677
movq 0x4a89(%rip), %r15      # 0x9fd0
leaq 0x1e4c(%rip), %rsi      # 0x739a
movl $0xc, %edx
movq %r15, %rdi
callq 0x32f0
movq %r15, %rdi
movl %ebx, %esi
callq 0x3450
movq %rax, %r14
leaq 0x1e38(%rip), %rsi      # 0x73a7
movl $0x1f, %edx
movq %rax, %rdi
callq 0x32f0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
movl $0x4, -0x120(%rbp)
movq -0x40(%rbp), %rdi
callq 0x44b2
cmpb $0x61, %al
jne 0x55e4
leaq -0x118(%rbp), %rdi
movl $0x0, -0x4(%rdi)
movq (%r12), %rax
movl 0x324(%rax), %edx
incl %edx
leaq 0x1dec(%rip), %rsi      # 0x73c7
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3090
movq -0x40(%rbp), %rdi
callq 0x44b2
cmpb $0x70, %al
jne 0x561b
leaq -0x118(%rbp), %rdi
movl $0x1, -0x4(%rdi)
movq (%r12), %rax
movl 0x324(%rax), %edx
incl %edx
leaq 0x1de8(%rip), %rsi      # 0x73fa
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3090
leaq -0x118(%rbp), %r14
movq %r14, %rdi
callq 0x3130
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x32f0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
leaq -0x120(%rbp), %rsi
movq -0x48(%rbp), %r15
movq %r15, %rdi
movl $0x88, %edx
callq 0x3d76
testb %r13b, %r13b
je 0x56df
movq 0x494d(%rip), %r14      # 0x9fd0
leaq 0x1d10(%rip), %rsi      # 0x739a
movl $0xc, %edx
movq %r14, %rdi
callq 0x32f0
movq %r14, %rdi
movl %ebx, %esi
callq 0x3450
movq %rax, %r14
leaq 0x1d7e(%rip), %rsi      # 0x7429
movl $0x12, %edx
movq %rax, %rdi
callq 0x32f0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
movq %r12, %r13
cmpl $0x1, 0x6c(%r12)
movq -0x40(%rbp), %r12
jne 0x5763
movq %r12, %rdi
callq 0x44b2
cmpb $0x70, %al
jne 0x5763
leaq -0x11c(%rbp), %r14
movl $0x3, -0x4(%r14)
movq %r15, %rbx
leaq -0x98(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x4732
movq (%r15), %rsi
movq %r14, %rdi
callq 0x3250
leaq 0x1d0c(%rip), %rdi      # 0x743c
movq %r14, %rsi
xorl %eax, %eax
callq 0x3050
leaq -0x120(%rbp), %rsi
movq %rbx, %rdi
movl $0x68, %edx
callq 0x3d76
leaq -0x88(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5763
callq 0x3260
movq %r13, %r12
movl $0x0, 0x6c(%r13)
movq (%r13), %rax
incl 0x324(%rax)
movq (%r12), %rdi
movl 0x324(%rdi), %ebx
callq 0x4f12
cmpl %eax, %ebx
jl 0x5808
leaq 0x1ced(%rip), %rsi      # 0x747f
movl $0x14, %edx
movq 0x4832(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3190
movq (%r12), %rdi
testq %rdi, %rdi
je 0x57e3
callq 0x3260
movq $0x0, (%r12)
xorps %xmm0, %xmm0
movq -0x58(%rbp), %rax
movups %xmm0, 0x50(%rax)
movups %xmm0, 0x40(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movl $0x0, 0x60(%rax)
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x88(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5832
callq 0x3260
movq %rbx, %rdi
callq 0x3480