// File: /workspace/github_cpp_makefile_star2_O3/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 0x5149
movl %ecx, -0x58(%rbp)
movq %rsi, -0x70(%rbp)
movq %rdx, -0x40(%rbp)
movl 0x324(%rdi), %esi
callq 0x4e9a
movq %rax, -0x38(%rbp)
xorl %edi, %edi
callq 0x3250
leaq -0x78(%rbp), %rdi
movq %rax, (%rdi)
callq 0x31d0
movq %rax, %r15
movq 0x509e(%rip), %r12      # 0x9fd0
leaq 0x23d9(%rip), %rsi      # 0x7312
movl $0x5, %edx
movq %r12, %rdi
callq 0x3300
movq (%rbx), %rax
movl 0x324(%rax), %esi
incl %esi
movq %r12, %rdi
callq 0x3460
movq %rax, %r13
leaq 0x2908(%rip), %rsi      # 0x786b
movl $0x1, %edx
movq %rax, %rdi
callq 0x3300
movq (%rbx), %rdi
callq 0x4e8e
movq %r13, %rdi
movl %eax, %esi
callq 0x3460
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %r13, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq 0x2365(%rip), %rsi      # 0x7318
movl $0xe, %edx
movq %r12, %rdi
callq 0x3300
movl 0x8(%r15), %esi
movq %r12, %rdi
callq 0x3460
movq %rax, %r14
leaq 0x2351(%rip), %r13      # 0x7327
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x3300
movl 0x4(%r15), %esi
movq %r14, %rdi
callq 0x3460
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x3300
movq %r15, -0x60(%rbp)
movl (%r15), %esi
movq %r14, %rdi
callq 0x3460
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq 0x22e4(%rip), %rsi      # 0x7329
movl $0xe, %edx
movq %r12, %rdi
callq 0x3300
movq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x440c
movq %r12, %rdi
movl %eax, %esi
callq 0x3460
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x3300
movq %r15, %rdi
callq 0x4416
movq %r14, %rdi
movl %eax, %esi
callq 0x3460
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x3300
movq %r15, %rdi
callq 0x4420
movq %r14, %rdi
movl %eax, %esi
callq 0x3460
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq %r15, %rdi
callq 0x4434
leaq 0x8(%rbx), %r13
movslq %eax, %rcx
cmpb $0x0, 0x8(%rbx,%rcx)
je 0x519c
movq (%rbx), %rax
incl 0x324(%rax)
leaq 0x2366(%rip), %rsi      # 0x746c
movl $0x12, %edx
movq 0x4ebe(%rip), %r14      # 0x9fd0
movq %r14, %rdi
callq 0x3300
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq %rbx, %r12
jmp 0x56e2
movq 0x4e80(%rip), %rbx      # 0x9fd0
leaq 0x233d(%rip), %rsi      # 0x7494
movl $0x14, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
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 0x31a0
movq %rcx, -0x68(%rbp)
movl %eax, -0x2c(%rbp)
movq %r13, -0x50(%rbp)
movq %rbx, -0x48(%rbp)
movq -0x60(%rbp), %r12
movl 0x8(%r12), %ebx
movq %r15, %rdi
callq 0x440c
movl %eax, %r14d
movl 0x4(%r12), %eax
movl %eax, -0x54(%rbp)
movq %r15, %rdi
callq 0x4416
movl %eax, %r13d
movl (%r12), %r12d
movq %r15, %rdi
callq 0x4420
cmpl %r14d, %ebx
jne 0x52bb
cmpl %r13d, -0x54(%rbp)
jne 0x52bb
cmpl %eax, %r12d
jne 0x52bb
leaq 0x2137(%rip), %rsi      # 0x7338
movl $0xe, %edx
movq 0x4dc3(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x442a
cmpb $0x70, %al
je 0x525c
movq %r14, %rdi
callq 0x442a
cmpb $0x61, %al
jne 0x53f9
leaq 0x20e4(%rip), %rsi      # 0x7347
movl $0x10, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq -0x48(%rbp), %r12
leaq 0x6c(%r12), %rdx
cmpb $0x0, -0x58(%rbp)
je 0x53b7
movq -0x70(%rbp), %rdi
movq %r14, %rsi
callq 0x67ca
jmp 0x53bd
movq -0x60(%rbp), %r15
movl 0x8(%r15), %ebx
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x440c
cmpl %eax, %ebx
movq -0x48(%rbp), %r12
movq -0x50(%rbp), %r13
jle 0x5367
movq 0x4cea(%rip), %r14      # 0x9fd0
leaq 0x2166(%rip), %rsi      # 0x7453
movl $0x18, %edx
movq %r14, %rdi
callq 0x3300
movq (%r12), %rax
movl 0x324(%rax), %esi
movq %r14, %rdi
callq 0x3460
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movabsq $0x300000004, %rax      # imm = 0x300000004
leaq -0x120(%rbp), %rsi
movq %rax, (%rsi)
movq -0x40(%rbp), %rdi
movl $0x88, %edx
callq 0x3d86
movq (%r12), %rax
incl 0x324(%rax)
jmp 0x56e2
movl 0x8(%r15), %ebx
movq %r14, %rdi
callq 0x440c
cmpl %eax, %ebx
jne 0x538b
movl 0x4(%r15), %ebx
movq %r14, %rdi
callq 0x4416
cmpl %eax, %ebx
jg 0x52df
movl 0x4(%r15), %ebx
movq %r14, %rdi
callq 0x4416
cmpl %eax, %ebx
jne 0x56e2
movl (%r15), %ebx
movq %r14, %rdi
callq 0x4420
cmpl %eax, %ebx
jg 0x52df
jmp 0x56e2
movl $0x1, (%rdx)
movq -0x40(%rbp), %rbx
movl $0xf4240, %r14d         # imm = 0xF4240
cmpl $0x0, 0x6c(%r12)
jne 0x53e9
movl $0x1, %edi
callq 0x3370
leal -0x1(%r14), %eax
cmpl $0x1, %r14d
movl %eax, %r14d
ja 0x53c7
xorl %r14d, %r14d
testl %r14d, %r14d
sete %r13b
movq -0x38(%rbp), %r14
jmp 0x5485
leaq 0x1f58(%rip), %rsi      # 0x7358
movl $0xc, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movabsq $0x200000004, %rax      # imm = 0x200000004
leaq -0x118(%rbp), %rdi
movq %rax, -0x8(%rdi)
movq -0x48(%rbp), %r12
movq (%r12), %rax
movl 0x324(%rax), %edx
incl %edx
leaq 0x1f05(%rip), %rsi      # 0x7365
xorl %r13d, %r13d
movl -0x2c(%rbp), %ecx
xorl %eax, %eax
callq 0x3090
leaq -0x120(%rbp), %rsi
movq -0x40(%rbp), %rbx
movq %rbx, %rdi
movl $0x88, %edx
callq 0x3d86
movl 0x6c(%r12), %eax
cmpl $0x1, %eax
je 0x5659
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %rdx
movb $0x1, (%rcx,%rdx)
cmpl $-0x1, %eax
jne 0x55e4
movq 0x4b21(%rip), %r15      # 0x9fd0
leaq 0x1ee4(%rip), %rsi      # 0x739a
movl $0xc, %edx
movq %r15, %rdi
callq 0x3300
movq %r15, %rdi
movl -0x2c(%rbp), %esi
callq 0x3460
movq %rax, %r14
leaq 0x1ecf(%rip), %rsi      # 0x73a7
movl $0x1f, %edx
movq %rax, %rdi
callq 0x3300
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %r14, %rdi
movq -0x38(%rbp), %r14
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movl $0x4, -0x120(%rbp)
movq %r14, %rdi
callq 0x442a
cmpb $0x61, %al
jne 0x5551
leaq -0x118(%rbp), %rdi
movl $0x0, -0x4(%rdi)
movq (%r12), %rax
movl 0x324(%rax), %edx
incl %edx
leaq 0x1e80(%rip), %rsi      # 0x73c7
movl -0x2c(%rbp), %ecx
xorl %eax, %eax
callq 0x3090
movq %r14, %rdi
callq 0x442a
cmpb $0x70, %al
jne 0x5588
leaq -0x118(%rbp), %rdi
movl $0x1, -0x4(%rdi)
movq (%r12), %rax
movl 0x324(%rax), %edx
incl %edx
leaq 0x1e7c(%rip), %rsi      # 0x73fa
movl -0x2c(%rbp), %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 0x3300
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq -0x120(%rbp), %rsi
movq %rbx, %rdi
movl $0x88, %edx
callq 0x3d86
movq -0x38(%rbp), %r14
testb %r13b, %r13b
je 0x5651
movq 0x49e0(%rip), %r14      # 0x9fd0
leaq 0x1da3(%rip), %rsi      # 0x739a
movl $0xc, %edx
movq %r14, %rdi
callq 0x3300
movq %r14, %rdi
movl -0x2c(%rbp), %esi
callq 0x3460
movq %rax, %r14
leaq 0x1e10(%rip), %rsi      # 0x7429
movl $0x12, %edx
movq %rax, %rdi
callq 0x3300
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %r14, %rdi
movq -0x38(%rbp), %r14
callq 0x3030
movq %rax, %rdi
callq 0x31a0
cmpl $0x1, 0x6c(%r12)
jne 0x56cb
movq %r14, %rdi
callq 0x442a
cmpb $0x70, %al
jne 0x56cb
movq %r14, %rsi
leaq -0x11c(%rbp), %r14
movl $0x3, -0x4(%r14)
leaq -0x98(%rbp), %r15
movq %r15, %rdi
callq 0x46aa
movq (%r15), %rsi
movq %r14, %rdi
callq 0x3260
leaq 0x1da4(%rip), %rdi      # 0x743c
movq %r14, %rsi
xorl %eax, %eax
callq 0x3050
leaq -0x120(%rbp), %rsi
movq %rbx, %rdi
movl $0x68, %edx
callq 0x3d86
leaq -0x88(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x56cb
callq 0x3270
movl $0x0, 0x6c(%r12)
movq (%r12), %rax
incl 0x324(%rax)
movq -0x50(%rbp), %r13
movq (%r12), %rdi
movl 0x324(%rdi), %ebx
callq 0x4e8e
cmpl %eax, %ebx
jl 0x577a
leaq 0x1d7f(%rip), %rsi      # 0x747f
movl $0x14, %edx
movq 0x48c4(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq (%r12), %rdi
testq %rdi, %rdi
je 0x5751
callq 0x3270
movq $0x0, (%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%r13)
movups %xmm0, 0x40(%r13)
movups %xmm0, 0x30(%r13)
movups %xmm0, 0x20(%r13)
movups %xmm0, 0x10(%r13)
movups %xmm0, (%r13)
movl $0x0, 0x60(%r13)
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 0x57a4
callq 0x3270
movq %rbx, %rdi
callq 0x3490