├── Projects ├── Project_1_part4 │ └── implementation_solutiones │ │ ├── Ahmed_Sayed │ │ ├── GW_v1.out │ │ ├── client.h │ │ ├── newfile.txt │ │ ├── project_v1.c │ │ └── server.h │ │ ├── Hesham Noureldin │ │ ├── CAN-Client │ │ │ ├── .cproject │ │ │ ├── .project │ │ │ ├── Debug │ │ │ │ ├── CAN-Client │ │ │ │ ├── can_log.txt │ │ │ │ ├── client_main.d │ │ │ │ ├── client_main.o │ │ │ │ ├── client_utils.d │ │ │ │ ├── client_utils.o │ │ │ │ ├── makefile │ │ │ │ ├── objects.mk │ │ │ │ ├── sources.mk │ │ │ │ └── subdir.mk │ │ │ ├── client_main.c │ │ │ ├── client_utils.c │ │ │ └── client_utils.h │ │ └── CAN-Server │ │ │ ├── .cproject │ │ │ ├── .project │ │ │ ├── Debug │ │ │ ├── CAN-Server │ │ │ ├── makefile │ │ │ ├── objects.mk │ │ │ ├── server_main.d │ │ │ ├── server_main.o │ │ │ ├── sources.mk │ │ │ ├── subdir.mk │ │ │ ├── utils.d │ │ │ └── utils.o │ │ │ ├── server_main.c │ │ │ ├── utils.c │ │ │ └── utils.h │ │ ├── Mohamed_zaghloul │ │ ├── client │ │ │ ├── .cproject │ │ │ ├── .project │ │ │ ├── Debug │ │ │ │ ├── client │ │ │ │ ├── client.d │ │ │ │ ├── client.o │ │ │ │ ├── makefile │ │ │ │ ├── objects.mk │ │ │ │ ├── sources.mk │ │ │ │ └── subdir.mk │ │ │ ├── client.c │ │ │ ├── client.h │ │ │ ├── client.out │ │ │ └── output_file.txt │ │ └── server │ │ │ ├── .cproject │ │ │ ├── .project │ │ │ ├── Debug │ │ │ ├── makefile │ │ │ ├── objects.mk │ │ │ ├── server │ │ │ ├── server.c │ │ │ ├── server.d │ │ │ ├── server.o │ │ │ ├── server_client_communication │ │ │ ├── sources.mk │ │ │ └── subdir.mk │ │ │ ├── server.c │ │ │ ├── server.h │ │ │ └── server.out │ │ ├── Readme │ │ ├── ahmed_gad │ │ ├── Socket_Client_Test │ │ │ ├── Can_Log.txt │ │ │ ├── README │ │ │ ├── ethernet_socket.c │ │ │ ├── ethernet_socket.h │ │ │ ├── main.c │ │ │ ├── my_signals.c │ │ │ ├── my_signals.h │ │ │ ├── threads.c │ │ │ └── threads.h │ │ └── Socket_Server_test │ │ │ ├── README │ │ │ ├── ethernet_socket.c │ │ │ ├── ethernet_socket.h │ │ │ ├── main.c │ │ │ ├── signal_handling.c │ │ │ ├── signal_handling.h │ │ │ ├── thread.c │ │ │ └── thread.h │ │ ├── ahmed_nader │ │ ├── Client_ECU │ │ │ ├── README.txt │ │ │ ├── ethernet_socket.c │ │ │ ├── ethernet_socket.h │ │ │ ├── main.c │ │ │ ├── my_signals.c │ │ │ ├── my_signals.h │ │ │ ├── threads.c │ │ │ └── threads.h │ │ └── Server_ECU │ │ │ ├── README.txt │ │ │ ├── ethernet_socket.c │ │ │ ├── ethernet_socket.h │ │ │ ├── main.c │ │ │ ├── serial.c │ │ │ ├── serial.h │ │ │ ├── signal_handling.c │ │ │ ├── signal_handling.h │ │ │ ├── thread.c │ │ │ └── thread.h │ │ └── project_pres.pdf └── project_2_OTA │ ├── Ahmed_Nader │ ├── OTA pres.pdf │ ├── OTA_video.mp4 │ ├── ReadME │ ├── lab2.sh │ ├── u-boot │ ├── vexpress_blob │ └── vexpress_blob.txt │ └── Ahmed_Sayed │ ├── NAP_SD_2M.img │ ├── OTA.sh │ ├── README.md │ ├── step_photos │ ├── RUN_Change-env.png │ ├── TFTP_Different_SD_CARD.png │ └── TFTP_SAME_SD_CARD.png │ ├── tftp │ └── test.elf │ ├── u-boot │ └── videos │ ├── OTA_steps │ └── tftp ├── README.md └── file1.txt /Projects/Project_1_part4/implementation_solutiones/Ahmed_Sayed/GW_v1.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Ahmed_Sayed/GW_v1.out -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Ahmed_Sayed/client.h: -------------------------------------------------------------------------------- 1 | 2 | extern packet p; 3 | extern char x[100]; 4 | extern int fd_out; 5 | extern pthread_mutex_t fill_mutex; 6 | extern pthread_cond_t cond_var; 7 | int runclient() 8 | { 9 | 10 | fd_out=open("newfile.txt",O_RDWR | O_TRUNC | O_CREAT,0744);// open write file 11 | int sockfd =0,n=0; 12 | char recvbuff[100]; 13 | time_t ticks; 14 | char *s; 15 | struct sockaddr_in serv_addr; 16 | 17 | 18 | memset(recvbuff,0,sizeof(recvbuff)); 19 | 20 | if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) 21 | { 22 | 23 | printf("\n Error: Could not create socket \n"); 24 | return 1; 25 | } 26 | memset(&serv_addr, '0',sizeof(serv_addr)); 27 | 28 | serv_addr.sin_family=AF_INET; 29 | serv_addr.sin_port=htons(5200); 30 | serv_addr.sin_addr.s_addr= inet_addr("127.0.0.1"); 31 | 32 | if(connect(sockfd,(struct sockaddr *)&serv_addr, sizeof(serv_addr)) <0) 33 | { 34 | printf("\nError : connect Failed client \n"); 35 | return 1; 36 | 37 | } 38 | 39 | while ( (n=read(sockfd, recvbuff,sizeof(recvbuff)-1))>0) 40 | { 41 | // memset(recvbuff,'0',sizeof(recvbuff)); 42 | pthread_mutex_lock(&fill_mutex); 43 | printf("i am in cleint and mutex locked\n"); 44 | 45 | 46 | memset(&s,0,sizeof(s)-1); 47 | ticks =time(NULL); 48 | s=ctime(&ticks)+4; 49 | s[strlen(s)-1]=0; 50 | // printf("\n%s\n",s); 51 | 52 | 53 | strcat(recvbuff,s); 54 | strcat(recvbuff,"\n"); 55 | write(fd_out,recvbuff,(strlen(recvbuff))); 56 | memset(recvbuff,0,sizeof(recvbuff)); 57 | pthread_cond_wait(&cond_var,&fill_mutex); 58 | pthread_mutex_unlock(&fill_mutex); 59 | //pthread_exit(NULL); 60 | } 61 | 62 | 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Ahmed_Sayed/newfile.txt: -------------------------------------------------------------------------------- 1 | Ahmed;12;12;00012;Jan 3 20:17:30 2020;Jan 3 20:17:34 2020 2 | Ahmed;12;12;00012;Jan 3 20:17:35 2020;Jan 3 20:17:53 2020 3 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Ahmed_Sayed/project_v1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | 17 | 18 | 19 | 20 | #include "server.h" 21 | #include "client.h" 22 | 23 | packet p; 24 | char x[100]; 25 | int fd_out; 26 | int confd; 27 | pthread_mutex_t fill_mutex; 28 | pthread_cond_t cond_var; 29 | void my_signal_handler(int signum); 30 | void *server_thread(void *data){ 31 | runserver(); 32 | } 33 | 34 | void *client_thread(void *data) 35 | { 36 | 37 | runclient(); 38 | } 39 | int main(int argc, char* argv[]) 40 | { 41 | (void) signal(SIGINT,my_signal_handler); 42 | pthread_t th1,th2; 43 | int ret; 44 | void *res; 45 | pthread_create(&th1, NULL, &server_thread , NULL); 46 | pthread_create(&th1, NULL, &client_thread , NULL); 47 | 48 | pthread_join(th1,&res); 49 | pthread_join(th2,&res); 50 | 51 | 52 | while(1){} 53 | } 54 | 55 | void my_signal_handler(int signum) 56 | { 57 | printf("i am handling the signal %d \n",signum); 58 | close(fd_out); 59 | //close(confd); 60 | printf("file and server closed \n"); 61 | (void) signal(SIGINT,SIG_DFL); 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Ahmed_Sayed/server.h: -------------------------------------------------------------------------------- 1 | typedef struct can_packet 2 | { 3 | char username[10]; 4 | char id[5]; 5 | char dlc[5]; 6 | char data[5]; 7 | int payload[5]; 8 | } packet; 9 | 10 | extern packet p; 11 | extern char x[100]; 12 | pthread_mutex_t fill_mutex; 13 | pthread_cond_t cond_var=PTHREAD_COND_INITIALIZER; 14 | extern int confd=0; 15 | void runserver() 16 | { 17 | int listenfd=0; 18 | struct sockaddr_in serv_addr; 19 | 20 | char sendbuff[100]; 21 | time_t ticks; 22 | 23 | listenfd =socket(AF_INET,SOCK_STREAM,0); 24 | memset(&serv_addr,'0',sizeof(serv_addr)); 25 | memset(sendbuff,'0',sizeof(sendbuff)); 26 | serv_addr.sin_family=AF_INET; 27 | serv_addr.sin_addr.s_addr=htonl(INADDR_ANY); 28 | serv_addr.sin_port=htons(5200); 29 | 30 | bind(listenfd, (struct sockaddr*) &serv_addr,sizeof(serv_addr)); 31 | 32 | listen(listenfd,10); 33 | 34 | confd= accept(listenfd, (struct sockaddr*)NULL,NULL); 35 | 36 | while(1) 37 | { 38 | 39 | //ticks =time(NULL); 40 | memset(sendbuff,0,sizeof(sendbuff)); 41 | 42 | //snprintf(sendbuff, sizeof(sendbuff),"sent time%.24s \n",ctime(&ticks)); 43 | //write(confd,sendbuff,strlen(sendbuff)); 44 | 45 | pthread_mutex_lock(&fill_mutex); 46 | pthread_cond_signal(&cond_var); 47 | 48 | printf("i am in server\n"); 49 | read_packet(); 50 | 51 | write(confd,x,strlen(x)); 52 | pthread_mutex_unlock(&fill_mutex); 53 | //pthread_exit(NULL); 54 | sleep(1); 55 | 56 | 57 | 58 | } 59 | } 60 | 61 | 62 | void read_packet() 63 | { 64 | 65 | char *s; 66 | char d[5]; 67 | time_t ticks; 68 | memset(&s,0,sizeof(s)-1); 69 | ticks =time(NULL); 70 | s=ctime(&ticks)+4; 71 | s[strlen(s)-1]=0; 72 | 73 | 74 | 75 | memset(&x,0,sizeof(x)); 76 | memset(&(p.data),'0',sizeof(p.data)); 77 | printf("Enter username:"); 78 | scanf("%s", &(p.username)); 79 | 80 | printf("Enter ID:"); 81 | scanf("%s", &(p.id)); 82 | printf("Enter DLC:"); 83 | scanf("%s", &(p.dlc)); 84 | 85 | printf("Enter Data:"); 86 | scanf("%s", &(d)); 87 | 88 | strcpy(p.data+strlen(p.data)-strlen(d),d); 89 | 90 | strcpy(x,p.username); 91 | strcat(x,";"); 92 | strcat(x,p.id); 93 | strcat(x,";"); 94 | strcat(x,p.dlc); 95 | strcat(x,";"); 96 | strcat(x,p.data); 97 | strcat(x,";"); 98 | strcat(x,s); 99 | strcat(x,";"); 100 | printf("%s",x); 101 | 102 | } 103 | 104 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 32 | 33 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 74 | 75 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | CAN-Client 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 24 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 25 | 26 | 27 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/CAN-Client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/CAN-Client -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/can_log.txt: -------------------------------------------------------------------------------- 1 | EEEE,4,EECCAAQQ,Sat Dec 28 14:40:19 2019,Sat Dec 28 14:41:00 2019 2 | E,2,AXCC,Sat Dec 28 14:48:46 2019,Sat Dec 28 14:49:02 2019 3 | FF,2,WWCC,Sat Dec 28 14:55:38 2019,Sat Dec 28 14:55:50 2019 4 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/client_main.d: -------------------------------------------------------------------------------- 1 | client_main.d: ../client_main.c ../client_utils.h 2 | 3 | ../client_utils.h: 4 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/client_main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/client_main.o -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/client_utils.d: -------------------------------------------------------------------------------- 1 | client_utils.d: ../client_utils.c ../client_utils.h 2 | 3 | ../client_utils.h: 4 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/client_utils.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/client_utils.o -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | -include ../makefile.init 6 | 7 | RM := rm -rf 8 | 9 | # All of the sources participating in the build are defined here 10 | -include sources.mk 11 | -include subdir.mk 12 | -include objects.mk 13 | 14 | ifneq ($(MAKECMDGOALS),clean) 15 | ifneq ($(strip $(C_DEPS)),) 16 | -include $(C_DEPS) 17 | endif 18 | endif 19 | 20 | -include ../makefile.defs 21 | 22 | # Add inputs and outputs from these tool invocations to the build variables 23 | 24 | # All Target 25 | all: CAN-Client 26 | 27 | # Tool invocations 28 | CAN-Client: $(OBJS) $(USER_OBJS) 29 | @echo 'Building target: $@' 30 | @echo 'Invoking: GCC C Linker' 31 | gcc -o "CAN-Client" $(OBJS) $(USER_OBJS) $(LIBS) 32 | @echo 'Finished building target: $@' 33 | @echo ' ' 34 | 35 | # Other Targets 36 | clean: 37 | -$(RM) $(EXECUTABLES)$(OBJS)$(C_DEPS) CAN-Client 38 | -@echo ' ' 39 | 40 | .PHONY: all clean dependents 41 | .SECONDARY: 42 | 43 | -include ../makefile.targets 44 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := 6 | 7 | LIBS := -lpthread 8 | 9 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | OBJ_SRCS := 6 | ASM_SRCS := 7 | C_SRCS := 8 | O_SRCS := 9 | S_UPPER_SRCS := 10 | EXECUTABLES := 11 | OBJS := 12 | C_DEPS := 13 | 14 | # Every subdirectory with source files must be described here 15 | SUBDIRS := \ 16 | . \ 17 | 18 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/Debug/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | C_SRCS += \ 7 | ../client_main.c \ 8 | ../client_utils.c 9 | 10 | OBJS += \ 11 | ./client_main.o \ 12 | ./client_utils.o 13 | 14 | C_DEPS += \ 15 | ./client_main.d \ 16 | ./client_utils.d 17 | 18 | 19 | # Each subdirectory must supply rules for building sources it contributes 20 | %.o: ../%.c 21 | @echo 'Building file: $<' 22 | @echo 'Invoking: GCC C Compiler' 23 | gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" 24 | @echo 'Finished building: $<' 25 | @echo ' ' 26 | 27 | 28 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/client_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | * client_main.c 4 | * 5 | * Created on: Dec 27, 2019 6 | * Author: embedded_system_ks 7 | */ 8 | 9 | #include 10 | #include 11 | #include "client_utils.h" 12 | 13 | 14 | int ret; 15 | void *res; 16 | pthread_t dumpfile_thread; 17 | pthread_t client_thread; 18 | 19 | int main(int argc , char * argv[]) 20 | { 21 | printf(">>>>>>>>>>>>>>>>>>>>> Start Client Side <<<<<<<<<<<<<<<<<<<<<< \n \n"); 22 | 23 | (void) signal(SIGINT,kill_Process_Handller); 24 | 25 | printf("-> Please Enter Server IP \n"); 26 | scanf("%s",&IP_Address[0]); 27 | 28 | //Socket connection establish 29 | Client_Socket_Connection_Establish (); 30 | 31 | //create Threads 32 | ret = pthread_create(&dumpfile_thread,NULL,&Dump_Data,NULL); 33 | ret = pthread_create(&client_thread,NULL,&Receive_Data,NULL); 34 | 35 | 36 | pthread_join(dumpfile_thread,&res); 37 | pthread_join(client_thread,&res); 38 | 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/client_utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * client_utils.c 3 | * 4 | * Created on: Dec 27, 2019 5 | * Author: Hesham Noureldin 6 | */ 7 | 8 | 9 | /* 10 | * utils.c 11 | 12 | * 13 | * Created on: Dec 26, 2019 14 | * Author: Hesham Noureldin 15 | */ 16 | #include 17 | #include "client_utils.h" 18 | #include 19 | #include 20 | 21 | 22 | 23 | pthread_mutex_t data_mutex; 24 | pthread_cond_t mutex_lock_cond = PTHREAD_COND_INITIALIZER; 25 | 26 | char Rx_Time[25] ={0}; 27 | char IP_Address[25]={0}; 28 | char RecvBuffer[BUFFER_SIZE]; 29 | char path[PATH_MAX]; 30 | char *logfile = "can_log.txt"; 31 | 32 | //socket parameters definitions 33 | int sockfd = 0; 34 | int fd_out = -1; 35 | struct sockaddr_in serv_addr; //server structure 36 | 37 | 38 | void Client_Socket_Connection_Establish() 39 | { 40 | 41 | memset(&serv_addr,'0', sizeof(serv_addr)); 42 | 43 | /*==== step 1:create client socket ====*/ 44 | 45 | if((sockfd = socket(AF_INET,SOCK_STREAM,0)) <0) 46 | { 47 | printf( "[-] Error in creating socket. \n"); 48 | exit(EXIT_FAILURE); 49 | } 50 | printf("[+] Client socket is created. \n"); 51 | 52 | serv_addr.sin_family =AF_INET; 53 | serv_addr.sin_addr.s_addr = inet_addr(IP_Address); 54 | serv_addr.sin_port = htons(PORT); 55 | 56 | /*==== step 2:connect to server ====*/ 57 | if( connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(serv_addr))<0) 58 | { 59 | printf("[-] Error in connection. \n"); 60 | exit(EXIT_FAILURE); 61 | } 62 | printf("[+] Connected to server. \n"); 63 | 64 | 65 | } 66 | 67 | 68 | void kill_Process_Handller() 69 | { 70 | printf("[-] Session Terminated From Client Side !!. \n "); 71 | //close sockets 72 | close(sockfd); 73 | //close file handler 74 | close(fd_out); 75 | //open log file 76 | char *args [] = {"gedit",logfile,NULL}; 77 | int ret = execvp(args[0],args); 78 | if((int)ret<0) 79 | { 80 | perror("[-] Error opening log file. \n"); 81 | } 82 | 83 | (void) signal(SIGINT,SIG_DFL); 84 | } 85 | 86 | 87 | void Get_Rx_Time(){ 88 | time_t rawtime = time (NULL ); 89 | snprintf( Rx_Time, sizeof(Rx_Time),"%24s\r\n",ctime(&rawtime)); 90 | } 91 | 92 | void * Dump_Data() 93 | { 94 | while(1) 95 | { 96 | pthread_mutex_lock (&data_mutex); 97 | pthread_cond_wait(&mutex_lock_cond,&data_mutex); 98 | //get data from structure && dump to file 99 | if(strstr(RecvBuffer, "exit") == NULL) { 100 | strcat(RecvBuffer,","); 101 | strcat(RecvBuffer,Rx_Time); 102 | Write_Data_To_File(); 103 | } 104 | memset(RecvBuffer,0,sizeof(RecvBuffer)); 105 | pthread_mutex_unlock(&data_mutex); 106 | sleep(1); 107 | } 108 | 109 | } 110 | 111 | void * Receive_Data() 112 | { 113 | while(1) 114 | { 115 | (void) Get_Rx_Time(); 116 | if(strcmp(RecvBuffer,"exit")== 0) 117 | { 118 | close(sockfd ); 119 | printf("[-] Disconnect from Server. \n"); 120 | //open log file 121 | char *args [] = {"gedit",logfile,NULL}; 122 | int ret = execvp(args[0],args); 123 | if((int)ret<0) 124 | { 125 | perror("[-] Error opening log file. \n"); 126 | } 127 | exit(EXIT_FAILURE); 128 | } 129 | if(recv(sockfd,RecvBuffer,sizeof(RecvBuffer),0)<0) 130 | { 131 | printf("[-] Error in Receiving Data. \n"); 132 | exit(EXIT_FAILURE); 133 | 134 | } 135 | 136 | printf("[+] Received Data From Server: %s. \n",RecvBuffer); 137 | //fill structure 138 | pthread_mutex_lock (&data_mutex); 139 | pthread_cond_signal(&mutex_lock_cond); 140 | pthread_mutex_unlock(&data_mutex); 141 | 142 | } 143 | } 144 | 145 | void Write_Data_To_File() 146 | { 147 | get_path(); 148 | char * OUTPUT_FILEPATH = path ; 149 | int retw =0; 150 | 151 | fd_out = open(OUTPUT_FILEPATH ,O_RDWR | O_CREAT |O_APPEND, 0744); 152 | if(fd_out == -1) 153 | { 154 | printf("[-] Error in opening the output file. \n"); 155 | exit(EXIT_FAILURE); 156 | } 157 | strcat(RecvBuffer,"\n"); 158 | 159 | retw = write(fd_out,RecvBuffer,strlen(RecvBuffer)); 160 | //write(fd_out," \n"); 161 | if(retw != strlen(RecvBuffer)) 162 | { 163 | printf("[-] Error in writing complete data to the output file. \n"); 164 | close(fd_out); 165 | exit(0); 166 | } 167 | printf("[+] Write Data Successfully to log file . \n"); 168 | close(fd_out); 169 | 170 | } 171 | 172 | void get_path() 173 | { 174 | if (getcwd(path, sizeof(path)) != NULL) { 175 | strcat(path,"/"); 176 | strcat(path,logfile); 177 | //printf("[+] Current working directory: %s .\n", path); 178 | } else { 179 | perror("[-] Error getting current working directory. \n "); 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Client/client_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * client_utils.h 3 | * 4 | * Created on: Dec 27, 2019 5 | * Author: embedded_system_ks 6 | */ 7 | 8 | #ifndef CLIENT_UTILS_H_ 9 | #define CLIENT_UTILS_H_ 10 | 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define PORT 8080 24 | #define BUFFER_SIZE 1024 25 | 26 | 27 | void * Dump_Data(); 28 | void * Receive_Data(); 29 | void Get_Rx_Time(); 30 | void kill_Process_Handller(); 31 | void Write_Data_To_File(); 32 | void get_path(); 33 | void Client_Socket_Connection_Establish(); 34 | 35 | 36 | extern char IP_Address[25]; 37 | 38 | #endif /* CLIENT_UTILS_H_ */ 39 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 32 | 33 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 74 | 75 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | CAN-Server 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 24 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 25 | 26 | 27 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/CAN-Server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/CAN-Server -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | -include ../makefile.init 6 | 7 | RM := rm -rf 8 | 9 | # All of the sources participating in the build are defined here 10 | -include sources.mk 11 | -include subdir.mk 12 | -include objects.mk 13 | 14 | ifneq ($(MAKECMDGOALS),clean) 15 | ifneq ($(strip $(C_DEPS)),) 16 | -include $(C_DEPS) 17 | endif 18 | endif 19 | 20 | -include ../makefile.defs 21 | 22 | # Add inputs and outputs from these tool invocations to the build variables 23 | 24 | # All Target 25 | all: CAN-Server 26 | 27 | # Tool invocations 28 | CAN-Server: $(OBJS) $(USER_OBJS) 29 | @echo 'Building target: $@' 30 | @echo 'Invoking: GCC C Linker' 31 | gcc -o "CAN-Server" $(OBJS) $(USER_OBJS) $(LIBS) 32 | @echo 'Finished building target: $@' 33 | @echo ' ' 34 | 35 | # Other Targets 36 | clean: 37 | -$(RM) $(EXECUTABLES)$(OBJS)$(C_DEPS) CAN-Server 38 | -@echo ' ' 39 | 40 | .PHONY: all clean dependents 41 | .SECONDARY: 42 | 43 | -include ../makefile.targets 44 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := 6 | 7 | LIBS := -lpthread 8 | 9 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/server_main.d: -------------------------------------------------------------------------------- 1 | server_main.d: ../server_main.c ../utils.h 2 | 3 | ../utils.h: 4 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/server_main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/server_main.o -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | OBJ_SRCS := 6 | ASM_SRCS := 7 | C_SRCS := 8 | O_SRCS := 9 | S_UPPER_SRCS := 10 | EXECUTABLES := 11 | OBJS := 12 | C_DEPS := 13 | 14 | # Every subdirectory with source files must be described here 15 | SUBDIRS := \ 16 | . \ 17 | 18 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | C_SRCS += \ 7 | ../server_main.c \ 8 | ../utils.c 9 | 10 | OBJS += \ 11 | ./server_main.o \ 12 | ./utils.o 13 | 14 | C_DEPS += \ 15 | ./server_main.d \ 16 | ./utils.d 17 | 18 | 19 | # Each subdirectory must supply rules for building sources it contributes 20 | %.o: ../%.c 21 | @echo 'Building file: $<' 22 | @echo 'Invoking: GCC C Compiler' 23 | gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" 24 | @echo 'Finished building: $<' 25 | @echo ' ' 26 | 27 | 28 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/utils.d: -------------------------------------------------------------------------------- 1 | utils.d: ../utils.c ../utils.h 2 | 3 | ../utils.h: 4 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/utils.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/Debug/utils.o -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/server_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | * server_main.c 4 | * 5 | * Created on: Dec 26, 2019 6 | * Author: embedded_system_ks 7 | */ 8 | 9 | #include 10 | #include 11 | #include "utils.h" 12 | 13 | 14 | int ret; 15 | void *res; 16 | pthread_t data_thread; 17 | pthread_t server_thread; 18 | 19 | int main(int argc , char * argv[]) 20 | { 21 | printf(">>>>>>>>>>>>>>>>>>>> Start Server Side <<<<<<<<<<<<<<<<<<<< \n \n"); 22 | 23 | (void) signal(SIGINT,kill_Process_Handller); 24 | 25 | //Socket connection establish 26 | Server_Socket_Connection_Establish(); 27 | 28 | //create Threads 29 | ret = pthread_create(&data_thread,NULL,&Collect_Data,NULL); 30 | ret = pthread_create(&server_thread,NULL,&Send_Data,NULL); 31 | 32 | 33 | pthread_join(data_thread,&res); 34 | pthread_join(server_thread,&res); 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * utils.c 3 | 4 | * 5 | * Created on: Dec 26, 2019 6 | * Author: Hesham Noureldin 7 | */ 8 | #include 9 | #include "utils.h" 10 | #include 11 | 12 | 13 | 14 | pthread_mutex_t data_mutex; 15 | pthread_cond_t mutex_lock_cond = PTHREAD_COND_INITIALIZER; 16 | 17 | 18 | char SendBuffer[BUFFER_SIZE]; 19 | char Temp[25]; 20 | char Tx_Time[25] ={0}; 21 | int DLC; 22 | 23 | //socket parameters definition 24 | int sockfd; 25 | int bind_ret; 26 | int clientSocket = 0; 27 | struct sockaddr_in serv_addr; 28 | struct sockaddr_in clint_addr; 29 | socklen_t addr_size; 30 | 31 | void Server_Socket_Connection_Establish() 32 | { 33 | memset(&serv_addr,'0', sizeof(serv_addr)); 34 | 35 | /*==== step 1:create Server socket ====*/ 36 | 37 | if((sockfd = socket(AF_INET,SOCK_STREAM,0)) <0) 38 | { 39 | printf( "[-] Error in creating socket. \n"); 40 | exit(EXIT_FAILURE); 41 | } 42 | printf("[+] Server socket is created. \n"); 43 | 44 | 45 | /*==== step 2:Binding Server to the port and IP ====*/ 46 | serv_addr.sin_family =AF_INET; 47 | serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); 48 | serv_addr.sin_port = htons(PORT); 49 | 50 | bind_ret = bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); 51 | if(bind_ret <0){ 52 | printf( "[-] Error in binding. \n"); 53 | exit(EXIT_FAILURE); 54 | } 55 | printf( "[+] Bind server to port %d. \n",PORT); 56 | 57 | /*==== step 3:Listening to Clients ====*/ 58 | if(listen(sockfd,10)==0){ 59 | printf( "[+] Listening ............ \n"); 60 | } 61 | else{ 62 | printf( "[-] Error in Listening .\n"); 63 | exit(EXIT_FAILURE); 64 | } 65 | 66 | /*==== step 4:Accept connection from client ====*/ 67 | clientSocket = accept(sockfd,(struct sockaddr*)&clint_addr,&addr_size); 68 | if(clientSocket <0) 69 | { 70 | printf( "[-] Error in accepting connection .\n"); 71 | exit(EXIT_FAILURE); 72 | } 73 | printf("[+] Connection accepted from %s:%d \n",inet_ntoa(clint_addr.sin_addr),ntohs(clint_addr.sin_port)); 74 | 75 | } 76 | 77 | void kill_Process_Handller() 78 | { 79 | printf(" [-] Session Terminated From Server Side !! \n"); 80 | memset(Temp,0,sizeof(Temp)); 81 | strcpy(Temp,"exit"); 82 | send(clientSocket,Temp,strlen(Temp),0); 83 | memset(Temp,0,sizeof(Temp)); 84 | sleep(3); 85 | //close sockets 86 | close(clientSocket); 87 | (void) signal(SIGINT,SIG_DFL); 88 | } 89 | 90 | 91 | void Get_Tx_Time(){ 92 | time_t rawtime = time (NULL ); 93 | snprintf( Tx_Time, sizeof(Tx_Time),"%24s\r\n",ctime(&rawtime)); 94 | } 95 | 96 | void * Collect_Data() 97 | { 98 | while(1) 99 | { 100 | 101 | printf("\n \n --> Please Insert CAN Frame ID : \n"); 102 | scanf("%s" , &Temp[0]); 103 | strcat(SendBuffer,Temp); 104 | printf("--> Please Insert Data Length from (1 to 64):\n"); 105 | scanf("%d" , &DLC); 106 | while((DLC < 1)||(DLC >64)) 107 | { 108 | printf("[-] Invalid Data Length , Please Try Again .\n"); 109 | scanf("%d" , &DLC); 110 | } 111 | snprintf (Temp, sizeof(Temp), "%d",DLC); 112 | strcat(SendBuffer,","); 113 | strcat(SendBuffer,Temp); 114 | strcat(SendBuffer,","); 115 | 116 | //Insert Data Frame 117 | for(int i =0 ; i< DLC ;i++) 118 | { 119 | printf("--> please Insert byte #: %d \n ",i+1); 120 | scanf("%s",&Temp[0]); 121 | strcat(SendBuffer,Temp); 122 | sleep(1); 123 | } 124 | strcat(SendBuffer,","); 125 | printf( "[+] SendData = %s . \n",SendBuffer); 126 | pthread_mutex_lock (&data_mutex); 127 | pthread_cond_signal(&mutex_lock_cond); 128 | pthread_mutex_unlock(&data_mutex); 129 | } 130 | 131 | } 132 | 133 | void * Send_Data() 134 | { 135 | while(1) 136 | { 137 | 138 | (void) Get_Tx_Time(); 139 | pthread_mutex_lock (&data_mutex); 140 | pthread_cond_wait(&mutex_lock_cond,&data_mutex); 141 | strcat(SendBuffer,Tx_Time); 142 | //send data 143 | send(clientSocket,SendBuffer,strlen(SendBuffer),0); 144 | memset(SendBuffer,0,sizeof(SendBuffer)); 145 | pthread_mutex_unlock(&data_mutex); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Hesham Noureldin/CAN-Server/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * utils.h 3 | * 4 | * Created on: Dec 26, 2019 5 | * Author: embedded_system_ks 6 | */ 7 | 8 | #ifndef UTILS_H_ 9 | #define UTILS_H_ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #define PORT 8080 21 | #define BUFFER_SIZE 1024 22 | 23 | 24 | 25 | void * Collect_Data(); 26 | void * Send_Data(); 27 | void Get_Tx_Time(); 28 | void kill_Process_Handller(); 29 | void Server_Socket_Connection_Establish(); 30 | 31 | 32 | #endif /* UTILS_H_ */ 33 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 32 | 33 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 74 | 75 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | client 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 24 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 25 | 26 | 27 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/Debug/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/Debug/client -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/Debug/client.d: -------------------------------------------------------------------------------- 1 | client.d: ../client.c ../client.h 2 | 3 | ../client.h: 4 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/Debug/client.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/Debug/client.o -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/Debug/makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | -include ../makefile.init 6 | 7 | RM := rm -rf 8 | 9 | # All of the sources participating in the build are defined here 10 | -include sources.mk 11 | -include subdir.mk 12 | -include objects.mk 13 | 14 | ifneq ($(MAKECMDGOALS),clean) 15 | ifneq ($(strip $(C_DEPS)),) 16 | -include $(C_DEPS) 17 | endif 18 | endif 19 | 20 | -include ../makefile.defs 21 | 22 | # Add inputs and outputs from these tool invocations to the build variables 23 | 24 | # All Target 25 | all: client 26 | 27 | # Tool invocations 28 | client: $(OBJS) $(USER_OBJS) 29 | @echo 'Building target: $@' 30 | @echo 'Invoking: GCC C Linker' 31 | gcc -o "client" $(OBJS) $(USER_OBJS) $(LIBS) 32 | @echo 'Finished building target: $@' 33 | @echo ' ' 34 | 35 | # Other Targets 36 | clean: 37 | -$(RM) $(EXECUTABLES)$(OBJS)$(C_DEPS) client 38 | -@echo ' ' 39 | 40 | .PHONY: all clean dependents 41 | .SECONDARY: 42 | 43 | -include ../makefile.targets 44 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/Debug/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := 6 | 7 | LIBS := -lpthread 8 | 9 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/Debug/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | OBJ_SRCS := 6 | ASM_SRCS := 7 | C_SRCS := 8 | O_SRCS := 9 | S_UPPER_SRCS := 10 | EXECUTABLES := 11 | OBJS := 12 | C_DEPS := 13 | 14 | # Every subdirectory with source files must be described here 15 | SUBDIRS := \ 16 | . \ 17 | 18 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/Debug/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | C_SRCS += \ 7 | ../client.c 8 | 9 | OBJS += \ 10 | ./client.o 11 | 12 | C_DEPS += \ 13 | ./client.d 14 | 15 | 16 | # Each subdirectory must supply rules for building sources it contributes 17 | %.o: ../%.c 18 | @echo 'Building file: $<' 19 | @echo 'Invoking: GCC C Compiler' 20 | gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" 21 | @echo 'Finished building: $<' 22 | @echo ' ' 23 | 24 | 25 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * 4 | * Created on: Dec 26, 2019 5 | * Author: Mohamed Abo Raia 6 | */ 7 | 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "client.h" 24 | #include 25 | 26 | // buffer to hold the data 27 | static char data[150] = {'\0'} ; 28 | 29 | pthread_mutex_t mutex_1; 30 | static int socket_id ; 31 | static int output_file_id; 32 | 33 | 34 | 35 | 36 | int main(int argc, char *argv[]) 37 | { 38 | 39 | pthread_t thread_1_id ; 40 | pthread_t thread_2_id ; 41 | 42 | // create two threads 43 | pthread_create(&thread_1_id, NULL, recieve_data_from_server, NULL); 44 | pthread_create(&thread_2_id, NULL, write_data_in_afile, NULL); 45 | 46 | // change the default signal handler 47 | signal(SIGINT, signal_handler); 48 | 49 | // join two the two threads to main 50 | pthread_join(thread_1_id, NULL); 51 | pthread_join(thread_2_id, NULL); 52 | 53 | 54 | 55 | 56 | // success 57 | return 0; 58 | } 59 | 60 | 61 | 62 | // function to receive data from server 63 | int recieve_data(void) 64 | { 65 | 66 | int temp = 0; 67 | 68 | 69 | 70 | // initialize the client 71 | client_init(); 72 | 73 | 74 | 75 | while (1) 76 | { 77 | // acquire mutex 78 | pthread_mutex_lock(&mutex_1); 79 | 80 | memset(&data, '\0', sizeof(data)) ; 81 | 82 | 83 | // receive data from server 84 | temp = read(socket_id, data, sizeof(data) -1); 85 | 86 | 87 | // check for error 88 | if (temp < 0) 89 | { 90 | perror("read"); 91 | 92 | return -1; 93 | } 94 | 95 | // release mutex 96 | pthread_mutex_unlock(&mutex_1); 97 | 98 | sleep(1); 99 | 100 | } 101 | 102 | // success 103 | return 0; 104 | 105 | } 106 | 107 | // function to store data in a file 108 | int write_data(char *data) 109 | { 110 | 111 | int ret_2 ; 112 | time_t rx_time = 0; 113 | 114 | 115 | // open the output file 116 | output_file_id = open("output_file.txt", O_WRONLY | O_CREAT | O_APPEND, 0744); 117 | 118 | // check for error 119 | if (output_file_id == -1) 120 | { 121 | perror("output file open"); 122 | return -1; 123 | } 124 | 125 | while (1) 126 | { 127 | sleep(2); 128 | 129 | char rx_time_s[30] = "Rx Time : " ; 130 | 131 | 132 | // acquire mutex 133 | pthread_mutex_lock(&mutex_1); 134 | 135 | // calculate the recieve time 136 | rx_time = time(NULL); 137 | strcat(rx_time_s, ctime(&rx_time)) ; 138 | 139 | // add the recieve time to the data 140 | strcat(data, rx_time_s) ; 141 | 142 | // write data in the file 143 | ret_2 = write(output_file_id, (void *) data, strlen(data)); 144 | 145 | // check for error 146 | if (-1 == ret_2) 147 | { 148 | perror("write"); 149 | 150 | close(output_file_id); 151 | 152 | return -1 ; 153 | } 154 | 155 | 156 | // clear the buffer 157 | memset(&rx_time_s, '\0', sizeof(rx_time_s)) ; 158 | 159 | 160 | // release mutex 161 | pthread_mutex_unlock(&mutex_1); 162 | 163 | } 164 | 165 | 166 | //success 167 | return 0; 168 | } 169 | 170 | // thread for recieving data from the server 171 | void* recieve_data_from_server(void *arg) 172 | { 173 | 174 | int temp ; 175 | 176 | 177 | // Receive data from the server 178 | temp = recieve_data() ; 179 | 180 | // check for error 181 | if (temp == -1) 182 | { 183 | perror("Receive data"); 184 | return NULL; 185 | } 186 | 187 | 188 | return NULL ; 189 | } 190 | 191 | // thread for writing data in a file 192 | void* write_data_in_afile(void *arg) 193 | { 194 | 195 | // store the data in a file 196 | write_data(data); 197 | 198 | return NULL; 199 | } 200 | 201 | // fuction to initialize the client 202 | int client_init(void) 203 | { 204 | 205 | struct sockaddr_in client ; 206 | int temp = 0; 207 | 208 | // open a socket 209 | socket_id = socket(PF_INET, SOCK_STREAM, 0); 210 | 211 | // check if there is an error when opening the socket 212 | if (socket_id == -1) 213 | { 214 | perror("socket"); 215 | 216 | return -1; 217 | } 218 | 219 | // initialize the struct with zero 220 | memset(&client, '0', sizeof(client)); 221 | 222 | printf("Client starting......\n"); 223 | 224 | 225 | 226 | // communication domain (Internet) 227 | client.sin_family = AF_INET ; 228 | // Attach a port number 500 to the socket 229 | client.sin_port = htons(9000); 230 | // add the client IP 231 | client.sin_addr.s_addr = inet_addr("127.0.0.1"); 232 | 233 | // connect to the server 234 | temp = connect(socket_id, (struct sockaddr *) &client, sizeof(client)) ; 235 | 236 | // check for error 237 | if (temp < 0) 238 | { 239 | perror("connect"); 240 | 241 | return -1; 242 | } 243 | 244 | printf("Connected\n"); 245 | 246 | 247 | printf("Receiving data.........\n"); 248 | 249 | // success 250 | return 0; 251 | 252 | } 253 | 254 | // signal handler for SIGINT 255 | void signal_handler(int arg) 256 | { 257 | int temp = 0; 258 | 259 | pid_t pid ; 260 | 261 | // create new process 262 | pid = fork() ; 263 | 264 | // for the parent process 265 | if (pid > 0) 266 | { 267 | // exit 268 | exit(0); 269 | } 270 | // for the child process 271 | else if (pid == 0) 272 | { 273 | //close the socket 274 | close(socket_id); 275 | 276 | // close the output file 277 | close(output_file_id); 278 | 279 | // open the output file with gedit 280 | temp = execl("/usr/bin/gedit", "gedit", "output_file.txt", NULL); ; 281 | 282 | // check for error 283 | if (temp < 0) 284 | { 285 | perror("execl"); 286 | } 287 | 288 | } 289 | 290 | // check if there is an error when creating new process 291 | else 292 | { 293 | perror("fork"); 294 | } 295 | 296 | 297 | } 298 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * client.h 3 | * 4 | * Created on: Dec 27, 2019 5 | * Author: embedded_system_ks 6 | */ 7 | 8 | #ifndef CLIENT_H_ 9 | #define CLIENT_H_ 10 | /* 11 | * Function to receive data from the server 12 | * The function will open a socket and connect to a server and 13 | * read the data in a global buffer. 14 | * 15 | * @param : The function will take nothing . 16 | * @return : The function will return (0) if every thing done correctly 17 | * and (-1) otherwise . 18 | * 19 | */ 20 | int recieve_data(void); 21 | /* 22 | * Function to write data to an output file 23 | * The function will take a data from a buffer and 24 | * write this data into an output file 25 | * 26 | * @param : The function will take the address of the data . 27 | * @return : The function will return (0) if every thing done correctly 28 | * and (-1) otherwise . 29 | * 30 | */ 31 | int write_data(char *data); 32 | /* 33 | * Thread for recieving data from the server 34 | * 35 | * This thread will just call the function recieve_data(). 36 | * 37 | * @param : pointer to void . 38 | * @return : pointer to void . 39 | */ 40 | 41 | void* recieve_data_from_server(void *arg); 42 | /* 43 | * Thread for writing data into a file 44 | * 45 | * This thread will just call the function write_data(); 46 | * 47 | * @param : pointer to void . 48 | * @return : pointer to void . 49 | */ 50 | void* write_data_in_afile(void *arg); 51 | /* 52 | * Function to intialize the client socket 53 | * 54 | * The function will open a socket and add the information of the socket like 55 | * port number and IP address and connect to the server. 56 | * 57 | * @param : The function will take nothing. 58 | * @return : The function will return (0) if every thing done correctly 59 | * and (-1) otherwise .. 60 | */ 61 | int client_init(void); 62 | /* 63 | * Function that will replace the default handler of SIGINT (quit signal) 64 | * 65 | * This function will be invoked when the client quit with quit signal . 66 | * when the function invoked it will create a child process , in this child process 67 | * it will close the socket and the file and open the output file with gedit . 68 | * and the parent process will terminated. 69 | * 70 | * @param : The function an integer. 71 | * @return : The function will return nothing to the caller. 72 | */ 73 | void signal_handler(int ); 74 | 75 | 76 | #endif /* CLIENT_H_ */ 77 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/client.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/client.out -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/output_file.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/client/output_file.txt -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 32 | 33 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 74 | 75 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | server 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 24 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 25 | 26 | 27 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | -include ../makefile.init 6 | 7 | RM := rm -rf 8 | 9 | # All of the sources participating in the build are defined here 10 | -include sources.mk 11 | -include subdir.mk 12 | -include objects.mk 13 | 14 | ifneq ($(MAKECMDGOALS),clean) 15 | ifneq ($(strip $(C_DEPS)),) 16 | -include $(C_DEPS) 17 | endif 18 | endif 19 | 20 | -include ../makefile.defs 21 | 22 | # Add inputs and outputs from these tool invocations to the build variables 23 | 24 | # All Target 25 | all: server 26 | 27 | # Tool invocations 28 | server: $(OBJS) $(USER_OBJS) 29 | @echo 'Building target: $@' 30 | @echo 'Invoking: GCC C Linker' 31 | gcc -o "server" $(OBJS) $(USER_OBJS) $(LIBS) 32 | @echo 'Finished building target: $@' 33 | @echo ' ' 34 | 35 | # Other Targets 36 | clean: 37 | -$(RM) $(EXECUTABLES)$(OBJS)$(C_DEPS) server 38 | -@echo ' ' 39 | 40 | .PHONY: all clean dependents 41 | .SECONDARY: 42 | 43 | -include ../makefile.targets 44 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := 6 | 7 | LIBS := -lpthread 8 | 9 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/server -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/server.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/server.c -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/server.d: -------------------------------------------------------------------------------- 1 | server.d: ../server.c ../server.h 2 | 3 | ../server.h: 4 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/server.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/server.o -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/server_client_communication: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/server_client_communication -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | OBJ_SRCS := 6 | ASM_SRCS := 7 | C_SRCS := 8 | O_SRCS := 9 | S_UPPER_SRCS := 10 | EXECUTABLES := 11 | OBJS := 12 | C_DEPS := 13 | 14 | # Every subdirectory with source files must be described here 15 | SUBDIRS := \ 16 | . \ 17 | 18 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/Debug/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | C_SRCS += \ 7 | ../server.c 8 | 9 | OBJS += \ 10 | ./server.o 11 | 12 | C_DEPS += \ 13 | ./server.d 14 | 15 | 16 | # Each subdirectory must supply rules for building sources it contributes 17 | %.o: ../%.c 18 | @echo 'Building file: $<' 19 | @echo 'Invoking: GCC C Compiler' 20 | gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" 21 | @echo 'Finished building: $<' 22 | @echo ' ' 23 | 24 | 25 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * 4 | * Created on: Dec 26, 2019 5 | * Author: Mohamed Abo Raia 6 | */ 7 | 8 | 9 | 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include "server.h" 23 | 24 | 25 | 26 | // buffer to hold the data 27 | static char data[125] ; 28 | 29 | pthread_mutex_t mutex_1; 30 | 31 | 32 | 33 | int main(void) 34 | { 35 | 36 | pthread_t thread_1_id ; 37 | pthread_t thread_2_id ; 38 | 39 | // create two threads 40 | pthread_create(&thread_1_id, NULL, read_data_from_terminal, NULL); 41 | pthread_create(&thread_2_id, NULL, send_data_to_client, NULL); 42 | 43 | // join two the two threads to main 44 | pthread_join(thread_1_id, NULL); 45 | pthread_join(thread_2_id, NULL); 46 | 47 | 48 | // success 49 | return 0; 50 | } 51 | 52 | 53 | // send data to a client 54 | int send_data(char *data) 55 | { 56 | int temp ; 57 | int *socket_id ; 58 | int socket_cl ; 59 | 60 | 61 | time_t tx_time = 0; 62 | 63 | // initialize the server 64 | socket_id = server_init() ; 65 | 66 | socket_cl = *socket_id ; 67 | 68 | while (1) 69 | { 70 | sleep(2); 71 | 72 | char tx_time_s[30] = "Tx Time : " ; 73 | 74 | 75 | // Acquire mutex 76 | pthread_mutex_lock(&mutex_1); 77 | 78 | // calculate the tx time 79 | tx_time = time(NULL); 80 | strcat(tx_time_s, ctime(&tx_time)) ; 81 | 82 | // add the transmit time to the data 83 | strcat(data, tx_time_s) ; 84 | 85 | 86 | // send data to client 87 | temp = write(socket_cl, data, strlen(data)); 88 | 89 | 90 | 91 | // check for error 92 | if (temp <= 0) 93 | { 94 | perror("write"); 95 | return -1; 96 | } 97 | 98 | // clear the buffer 99 | memset(&tx_time_s, '\0', sizeof(tx_time_s)) ; 100 | 101 | // release mutex 102 | pthread_mutex_unlock(&mutex_1); 103 | 104 | 105 | } 106 | 107 | 108 | // success 109 | return 0; 110 | 111 | } 112 | 113 | 114 | // thread for reading data from the terminal 115 | void* read_data_from_terminal(void *arg) 116 | { 117 | 118 | 119 | while (1) 120 | { 121 | sleep(1) ; 122 | 123 | // Acquire mutex 124 | pthread_mutex_lock(&mutex_1); 125 | 126 | // clear the buffer 127 | memset(&data, '\0', sizeof(data)) ; 128 | 129 | // read the data from the terminal 130 | printf("Enter the can packet like -->(ID : 30, DLC : 4, Data : FFFFFFFF) : "); 131 | fgets(data, sizeof(data), stdin) ; 132 | 133 | // release the mutex 134 | pthread_mutex_unlock(&mutex_1); 135 | 136 | 137 | } 138 | 139 | return NULL; 140 | } 141 | 142 | // thread to send data to client 143 | void* send_data_to_client(void *x) 144 | { 145 | // send the data to the client 146 | send_data(data) ; 147 | 148 | return NULL ; 149 | 150 | } 151 | 152 | // function to initialize the server socket 153 | int* server_init(void) 154 | { 155 | 156 | int socket_id ; // to hold the socket id 157 | struct sockaddr_in server, client ; // two structs to hold the data of the server and client 158 | int temp = 0; 159 | static int socket_cl ; 160 | int len ; 161 | 162 | // acquire mutex 163 | pthread_mutex_lock(&mutex_1); 164 | 165 | // open a socket 166 | socket_id = socket(AF_INET, SOCK_STREAM, 0); 167 | 168 | // check if there is an error when openning the socket 169 | if (socket_id == -1) 170 | { 171 | perror("socket"); 172 | 173 | return NULL; 174 | } 175 | 176 | printf("Server Starting..............\n"); 177 | 178 | // initialize the struct with zero 179 | memset(&server, '0', sizeof(server)); 180 | // initialize the struct with zero 181 | memset(&client, '0', sizeof(client)); 182 | 183 | // calculate the size of client structure 184 | len = sizeof(client); 185 | 186 | // communication domain (Internet) 187 | server.sin_family = PF_INET ; 188 | // Attach a port number 500 to the socket 189 | server.sin_port = htons(9000); 190 | // make the socket available for all addresses 191 | server.sin_addr.s_addr = htonl(INADDR_ANY); 192 | 193 | 194 | 195 | // attach the the address an port number to the socket 196 | temp = bind(socket_id, (struct sockaddr *) &server, sizeof(server)); 197 | 198 | //check for error 199 | if(temp < 0) 200 | { 201 | perror("bind"); 202 | 203 | return NULL; 204 | } 205 | 206 | printf("Binding..............\n"); 207 | 208 | // listen for connections 209 | temp = listen(socket_id, 10); 210 | 211 | // check for error 212 | if (temp < 0) 213 | { 214 | perror("listen"); 215 | 216 | return NULL; 217 | } 218 | 219 | printf("Listening..............\n"); 220 | 221 | // accept connection from the client 222 | socket_cl = accept(socket_id, (struct sockaddr *) &client, &len); 223 | 224 | // check for error 225 | if (socket_cl < 0) 226 | { 227 | perror("accept"); 228 | 229 | return NULL; 230 | } 231 | 232 | printf("Connection Established\n") ; 233 | 234 | // release mutex 235 | pthread_mutex_unlock(&mutex_1); 236 | // succcess 237 | return &socket_cl; 238 | 239 | } 240 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * server.h 3 | * 4 | * Created on: Dec 27, 2019 5 | * Author: Mohamed Abo Raia 6 | */ 7 | 8 | #ifndef SERVER_H_ 9 | #define SERVER_H_ 10 | 11 | /* 12 | * Function to send data to client 13 | * 14 | * The function will open a socket and wait for a client to connect 15 | * and send the data to him . 16 | * 17 | * @param : char *data The data to be sent . 18 | * @return : The function will return 0 if every thing done correctly 19 | * and -1 otherwise. 20 | * 21 | */ 22 | int send_data(char *data); 23 | /* 24 | * Thread for reading data from the terminal 25 | * 26 | * This thread will just acquire mutex will read the data from the terminal 27 | * then release the mutex and do it again and again . 28 | * 29 | * @param : pointer to void . 30 | * @return : pointer to void . 31 | */ 32 | void* read_data_from_terminal(void*); 33 | /* 34 | * Thread for sending data to the client 35 | * 36 | * This thread will just call the function send data and it will do the whole work . 37 | * 38 | * @param : pointer to void . 39 | * @return : pointer to void . 40 | */ 41 | void* send_data_to_client(void *x); 42 | /* 43 | * Function to intilalize the server socket 44 | * 45 | * The function will open a socket and bind the address and port number 46 | * to it and will go into listening state waiting for a client request 47 | * 48 | * @param : The function will take nothing . 49 | * @return : The function will return the socket id . 50 | * 51 | */ 52 | int* server_init(void) ; 53 | 54 | 55 | #endif /* SERVER_H_ */ 56 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/server.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Mohamed_zaghloul/server/server.out -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/Readme: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/Readme -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Client_Test/Can_Log.txt: -------------------------------------------------------------------------------- 1 | ID:22|DLC:3|data:HEY|TX time:Tue Dec 31 13:08:11 201|RX time:Tue Dec 31 13:08:11 2019 2 | ---------------------------------------------------------------------------------------------- 3 | ID:1200|DLC:4|data:From|TX time:Tue Dec 31 13:08:28 201|RX time:Tue Dec 31 13:08:28 2019 4 | ---------------------------------------------------------------------------------------------- 5 | ID:1|DLC:3|data:Gad|TX time:Tue Dec 31 13:08:49 201 201|RX time:|Tue Dec 31 13:08:49 201 6 | ---------------------------------------------------------------------------------------------- 7 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Client_Test/README: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------------------- 2 | -waiting data from server to print report of msg received from the server 3 | -process 4 | add the packet reception time. 5 | parse the packet readable format. 6 | -Sample of output 7 | ID:22|DLC:3|data:HEY|TX time:Tue Dec 31 13:08:11 201|RX time:Tue Dec 31 13:08:11 2019 8 | ---------------------------------------------------------------------------------------------- 9 | ID:1200|DLC:4|data:From|TX time:Tue Dec 31 13:08:28 201|RX time:Tue Dec 31 13:08:28 2019 10 | ---------------------------------------------------------------------------------------------- 11 | ID:1|DLC:3|data:Gad|TX time:Tue Dec 31 13:08:49 201 201|RX time:|Tue Dec 31 13:08:49 201 12 | ---------------------------------------------------------------------------------------------- 13 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Client_Test/ethernet_socket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ethernet_socket.c 3 | * 4 | * Created on: Dec 27, 2019 5 | * Author: root 6 | */ 7 | 8 | #include "ethernet_socket.h" 9 | 10 | int client_fd = -1; 11 | 12 | void ethernetSocketClient_init(const char *IP) { 13 | 14 | struct sockaddr_in serv_addr; 15 | 16 | //set server parameters 17 | serv_addr.sin_family = AF_INET; 18 | serv_addr.sin_port = htons(PORT); 19 | serv_addr.sin_addr.s_addr = inet_addr(IP); 20 | 21 | // Create client socket 22 | if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 23 | perror("Socket Creation:"); 24 | exit(0); 25 | } 26 | printf("Socket no=%d\n", client_fd); 27 | if (connect(client_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) 28 | < 0) { 29 | perror("Socket Connection:"); 30 | exit(0); 31 | } 32 | printf("Connection established!!\n "); 33 | } 34 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Client_Test/ethernet_socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ethernet_socket.h 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Gad 6 | */ 7 | 8 | #ifndef ETHERNET_SOCKET_H_ 9 | #define ETHERNET_SOCKET_H_ 10 | 11 | /*********************** includes **********************************/ 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | /****************************** Definitions *******************************/ 20 | #define PORT 5454 21 | 22 | /*********************** Prototypes **********************************/ 23 | void ethernetSocketClient_init(const char *IP) ; 24 | 25 | /****************************** externs *******************************/ 26 | extern int client_fd; 27 | #endif /* ETHERNET_SOCKET_H_ */ 28 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Client_Test/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: embedded_system_ks 6 | */ 7 | #include "threads.h" 8 | #include "my_signals.h" 9 | 10 | 11 | int main(int argc, char * argv[]) { 12 | 13 | pthread_t clientSocket_thread; 14 | pthread_t dumpFile_thread; 15 | 16 | //acquire mutex 17 | pthread_mutex_lock(&recvMutex); 18 | //Create threads 19 | signal(SIGINT, int_Signal_handler); 20 | 21 | if (pthread_create(&clientSocket_thread, NULL, Thread_SocketClient, argv[1]) 22 | < 0) { 23 | perror("clientSocket_thread"); 24 | exit(0); 25 | } 26 | if (pthread_create(&dumpFile_thread, NULL, Thread_WriteFile, NULL) < 0) { 27 | perror("dumpFile_thread"); 28 | exit(0); 29 | } 30 | 31 | //join threads 32 | pthread_join(clientSocket_thread, NULL); 33 | pthread_join(dumpFile_thread, NULL); 34 | } 35 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Client_Test/my_signals.c: -------------------------------------------------------------------------------- 1 | /* 2 | * my_signals.c 3 | 4 | * 5 | * Created on: Dec 24, 2019 6 | * Author: embedded_system_ks 7 | */ 8 | #include "my_signals.h" 9 | 10 | void int_Signal_handler(int signum) { 11 | 12 | pid_t pid = fork(); 13 | 14 | if (pid > 0) { 15 | //parent code 16 | printf("Parent Process has been Killed\n"); 17 | exit(1); 18 | 19 | } else if (!pid) { 20 | //child code 21 | printf("Closing Files and open Can Log file by Child Process!"); 22 | 23 | close(file_fd); 24 | close(client_fd); 25 | execl("/usr/bin/gedit", "gedit", FILE_PATH, NULL); 26 | 27 | while (1) { 28 | } 29 | 30 | } else { 31 | perror("Fork:"); 32 | } 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Client_Test/my_signals.h: -------------------------------------------------------------------------------- 1 | /* 2 | * my_signals.h 3 | * 4 | * Created on: Dec 24, 2019 5 | * Author: Ahmed Gad 6 | 7 | */ 8 | 9 | #ifndef MY_SIGNALS_H_ 10 | #define MY_SIGNALS_H_ 11 | 12 | #include "threads.h" 13 | #include "ethernet_socket.h" 14 | 15 | void int_Signal_handler(int arg); 16 | 17 | 18 | #endif /* MY_SIGNALS_H_ */ 19 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Client_Test/threads.c: -------------------------------------------------------------------------------- 1 | /* 2 | * threads.c 3 | * 4 | * Created on: Dec 24, 2019 5 | * Author: Ahmed Gad 6 | */ 7 | #include "threads.h" 8 | 9 | pthread_mutex_t recvMutex; 10 | 11 | unsigned char canPacketBuffer[PACKET_MAX_SIZE]={0}; 12 | int received_bytes = 0; 13 | int file_fd = -1; 14 | 15 | void * Thread_WriteFile(void *arg) { 16 | 17 | time_t Epoch_date = 0; 18 | 19 | file_fd = open(FILE_PATH, O_RDWR | O_CREAT | O_TRUNC, 0774); 20 | 21 | if (file_fd == -1) { 22 | perror("File Open"); 23 | exit(0); 24 | } 25 | while (1) { 26 | 27 | pthread_mutex_lock(&recvMutex); 28 | 29 | printf("File Opened waiting data...... \n"); 30 | 31 | Epoch_date = time(NULL); 32 | 33 | memcpy(canPacketBuffer + strlen(canPacketBuffer), 34 | ctime(&Epoch_date), TIME_SIZE); 35 | 36 | write_in_file(); 37 | } 38 | } 39 | 40 | void * Thread_SocketClient(void * arg) { 41 | 42 | ethernetSocketClient_init((char *) arg); 43 | 44 | while (1) { 45 | while (!received_bytes) { 46 | received_bytes = read(client_fd,canPacketBuffer, 47 | sizeof(canPacketBuffer)); 48 | } 49 | memset(canPacketBuffer+ received_bytes, 0, 50 | sizeof(canPacketBuffer) - received_bytes); 51 | 52 | received_bytes = 0; 53 | 54 | //release mutex 55 | pthread_mutex_unlock(&recvMutex); 56 | } 57 | } 58 | 59 | void write_in_file(void) { 60 | 61 | 62 | int bufferIndex=0,tempIndex=0; 63 | //get ID 64 | for(bufferIndex=tempIndex+1;canPacketBuffer[bufferIndex]!='|';bufferIndex++); 65 | write(file_fd, "ID:", strlen("ID:")); 66 | write(file_fd,canPacketBuffer, bufferIndex+1); 67 | tempIndex=bufferIndex; 68 | 69 | //get DLC 70 | for(bufferIndex=tempIndex+1;canPacketBuffer[bufferIndex]!='|';bufferIndex++); 71 | write(file_fd, "DLC:", strlen("DLC:")); 72 | write(file_fd,canPacketBuffer+(tempIndex+1), bufferIndex-(tempIndex)); 73 | tempIndex=bufferIndex; 74 | 75 | 76 | //get date 77 | for(bufferIndex=tempIndex+1;canPacketBuffer[bufferIndex]!='|';bufferIndex++); 78 | write(file_fd, "data:", strlen("data:")); 79 | write(file_fd,canPacketBuffer+(tempIndex+1), bufferIndex-(tempIndex)); 80 | tempIndex=bufferIndex; 81 | 82 | //get Tx time 83 | for(bufferIndex=tempIndex+1;canPacketBuffer[bufferIndex]!='|';bufferIndex++); 84 | write(file_fd, "TX time:", strlen("TX time:")); 85 | write(file_fd,canPacketBuffer+(tempIndex+1), bufferIndex-(tempIndex)); 86 | tempIndex=bufferIndex; 87 | 88 | //get Rx time 89 | write(file_fd, "RX time:", strlen("RX time:")); 90 | write(file_fd,canPacketBuffer+(tempIndex+1),TIME_SIZE); 91 | write(file_fd, "\n", strlen("\n")); 92 | write(file_fd, 93 | "----------------------------------------------------------------------------------------------", 94 | strlen( 95 | "----------------------------------------------------------------------------------------------")); 96 | write(file_fd, "\n", strlen("\n")); 97 | } 98 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Client_Test/threads.h: -------------------------------------------------------------------------------- 1 | /* 2 | * threads.h 3 | * 4 | * Created on: Dec 24, 2019 5 | * Author: Ahmed Gad 6 | */ 7 | 8 | #ifndef THREADS_H_ 9 | #define THREADS_H_ 10 | 11 | /*********************** includes **********************************/ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "ethernet_socket.h" 31 | 32 | 33 | /*********************** Definitions ***********************/ 34 | #define TIME_SIZE 24 35 | #define DATA_MAX_SIZE 64 36 | #define ID_MAX_SIZE 4 37 | #define DLC_MAX_SIZE 2 38 | #define PACKET_MAX_SIZE ID_MAX_SIZE+DLC_MAX_SIZE+DATA_MAX_SIZE + (TIME_SIZE*2) 39 | #define FILE_PATH "/home/embedded_system_ks/labs/Socket_Client_ECUB/Can_Log.txt" 40 | /*********************** Decelerations ***********************/ 41 | 42 | extern pthread_mutex_t recvMutex; 43 | extern int file_fd; 44 | extern unsigned char canPacketBuffer[PACKET_MAX_SIZE]; 45 | /*********************** Prototypes **********************************/ 46 | void * Thread_WriteFile(void * arg); 47 | void * Thread_SocketClient(void * arg); 48 | void write_in_file(void); 49 | 50 | #endif /* THREADS_H_ */ 51 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Server_test/README: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------- 2 | - ECUA waiting data from user by Terminal 3 | 1- Massage ID must be unsigned and less than 11 bit 4 | 2- DLC (data length Control) must be less than or equal 64 5 | 3- data as string 6 | 7 | -Sample of Input 8 | ID:1245 9 | DLC:8 10 | Data:EngineON 11 | - Process 12 | add the time of packet transmition. 13 | send whole packet in asscci. 14 | seperate between every field by | seperator. 15 | ----------------------------------------------------------*/ 16 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Server_test/ethernet_socket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ethernet_socket.c 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Nader 6 | */ 7 | #include "ethernet_socket.h" 8 | 9 | int new_socket; 10 | int server_fd; 11 | 12 | void ethernetSocketServer_init(void) { 13 | 14 | struct sockaddr_in address; 15 | 16 | memset(&address, 0x00, sizeof(address)); 17 | 18 | // Creating socket file descriptor 19 | if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { 20 | perror("socket failed"); 21 | close(server_fd); 22 | exit(EXIT_FAILURE); 23 | } 24 | 25 | address.sin_family = AF_INET; 26 | address.sin_addr.s_addr = htonl(INADDR_ANY); 27 | address.sin_port = htons(PORT); 28 | 29 | // Forcefully attaching socket to the port 30 | if (bind(server_fd, (struct sockaddr *) &address, sizeof(address)) < 0) { 31 | perror("bind failed"); 32 | close(server_fd); 33 | exit(EXIT_FAILURE); 34 | } 35 | 36 | if (listen(server_fd, 10) < 0) { 37 | perror("listen"); 38 | exit(EXIT_FAILURE); 39 | } 40 | 41 | if ((new_socket = accept(server_fd, NULL, NULL)) < 0) { 42 | perror("accept"); 43 | exit(EXIT_FAILURE); 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Server_test/ethernet_socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ethernet_socket.h 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Nader 6 | */ 7 | 8 | #ifndef ETHERNET_SOCKET_H_ 9 | #define ETHERNET_SOCKET_H_ 10 | 11 | /*********************** includes **********************************/ 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | /****************************** Definitions *******************************/ 20 | #define PORT 5454 21 | 22 | /*********************** Prototypes **********************************/ 23 | void ethernetSocketServer_init(void); 24 | 25 | /****************************** externs *******************************/ 26 | extern int new_socket; 27 | extern int server_fd; 28 | #endif /* ETHERNET_SOCKET_H_ */ 29 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Server_test/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Nader 6 | */ 7 | 8 | #include"pthread.h" 9 | #include "thread.h" 10 | #include "signal_handling.h" 11 | #include "ethernet_socket.h" 12 | 13 | canPacket recievedPacket; 14 | 15 | pthread_mutex_t inputData_mutex; 16 | int main(int argc, char * argv[]) { 17 | 18 | pthread_t input_thread; 19 | pthread_t socketServer_thread; 20 | 21 | 22 | pthread_mutex_lock(&inputData_mutex); 23 | 24 | (void) signal(SIGINT, int_signal_handler); 25 | 26 | if ((pthread_create(&input_thread, NULL, input_read, NULL)) < 0) { 27 | perror("input_thread_create"); 28 | exit(0); 29 | } 30 | 31 | if ((pthread_create(&socketServer_thread, NULL, socket_server, NULL)) < 0) { 32 | perror("server_thread_create"); 33 | exit(0); 34 | } 35 | pthread_join(input_thread, NULL); 36 | pthread_join(socketServer_thread, NULL); 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Server_test/signal_handling.c: -------------------------------------------------------------------------------- 1 | /* 2 | * signal_handling.c 3 | * 4 | * Created on: Dec 24, 2019 5 | * Author: root 6 | */ 7 | 8 | #include "signal_handling.h" 9 | 10 | void int_signal_handler(void *arg) { 11 | 12 | printf("Termination !!!!\n"); 13 | printf("Closing serial reading and socket server\n"); 14 | 15 | close(server_fd); 16 | 17 | exit(0); 18 | } 19 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Server_test/signal_handling.h: -------------------------------------------------------------------------------- 1 | /* 2 | * signal_handling.h 3 | * 4 | * Created on: Dec 24, 2019 5 | * Author: root 6 | */ 7 | 8 | #ifndef SIGNAL_HANDLING_H_ 9 | #define SIGNAL_HANDLING_H_ 10 | 11 | /*********************** includes **********************************/ 12 | #include 13 | #include 14 | #include 15 | #include "ethernet_socket.h" 16 | 17 | /*********************** Prototypes **********************************/ 18 | void int_signal_handler(void *arg); 19 | 20 | #endif /* SIGNAL_HANDLING_H_ */ 21 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Server_test/thread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * thread.c 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Nader 6 | * Modified by:Ahmed Gad 7 | */ 8 | 9 | #include "thread.h" 10 | 11 | unsigned char canPacketBuffer[PACKET_MAX_SIZE]={0}; 12 | unsigned char bufferIndex=0; 13 | 14 | void * input_read(void * arg) { 15 | time_t Epoch_date = 0; 16 | printf(".....Inside input_read thread.....\n"); 17 | while (1) { 18 | printf("\nID:"); 19 | 20 | scanf("%d", &recievedPacket.packet.ID); 21 | if (recievedPacket.packet.ID > 2047) // means if ID large than 11 bit 22 | { 23 | printf("Invalid MSG ID"); 24 | exit(0); 25 | } 26 | printf("DLC:"); 27 | scanf("%d", &recievedPacket.packet.DLC); 28 | 29 | if (recievedPacket.packet.DLC > 64) // means if DLC large than 64 30 | { 31 | 32 | printf("Invalid DLC"); 33 | exit(0); 34 | } 35 | printf("Data:"); 36 | getc(stdin);// to skip new line 37 | scanf("%s",recievedPacket.packet.data); 38 | 39 | if(strlen(recievedPacket.packet.data)!=recievedPacket.packet.DLC){ 40 | 41 | printf("Invalid data must be equal length of DLC\n DLC=%d and data length=%d",recievedPacket.packet.DLC,strlen(recievedPacket.packet.data)); 42 | printf("\ndata:%s\n",recievedPacket.packet.data); 43 | exit(0); 44 | } 45 | recievedPacket.packet.data[strlen(recievedPacket.packet.data)]='|'; 46 | //clear buffer 47 | memset( 48 | recievedPacket.packetInBytes 49 | +recievedPacket.packet.DLC+4, 0x00, 50 | sizeof(recievedPacket.packetInBytes) 51 | - recievedPacket.packet.DLC+4); 52 | 53 | 54 | 55 | // add Tx time to our packet 56 | Epoch_date = time(NULL); 57 | 58 | memcpy(recievedPacket.packet.data + recievedPacket.packet.DLC+1, 59 | ctime(&Epoch_date), TIME_SIZE); 60 | // make packet contains only assci 61 | itos(canPacketBuffer,recievedPacket.packet.ID); 62 | bufferIndex=strlen(canPacketBuffer); 63 | canPacketBuffer[bufferIndex++]='|';// seperate between ID and DLC 64 | 65 | itos(canPacketBuffer+bufferIndex,recievedPacket.packet.DLC); 66 | bufferIndex=strlen(canPacketBuffer); 67 | canPacketBuffer[bufferIndex++]='|';// seperate between DLC and data 68 | 69 | memcpy(canPacketBuffer+bufferIndex,recievedPacket.packet.data ,recievedPacket.packet.DLC+TIME_SIZE); 70 | bufferIndex=strlen(canPacketBuffer); 71 | canPacketBuffer[bufferIndex]='|';// seperate between Tx Time and Rx Time 72 | 73 | 74 | 75 | pthread_mutex_unlock(&inputData_mutex); 76 | } 77 | } 78 | 79 | void * socket_server(void *arg) { 80 | 81 | ethernetSocketServer_init(); 82 | 83 | while (1) { 84 | 85 | pthread_mutex_lock(&inputData_mutex); 86 | write(new_socket, canPacketBuffer, 87 | strlen(canPacketBuffer)); 88 | } 89 | 90 | } 91 | void itos(unsigned char * s, int number) { 92 | unsigned char index = 0, s_index = 0; 93 | if (!number) { 94 | s[index++] = '0'; 95 | s[index] = 0; 96 | return; 97 | } 98 | if (number < 0) { 99 | s[index++] = '-'; 100 | s_index++; 101 | number *= -1; 102 | } 103 | while (number > 0) { 104 | s[index++] = (number % 10) + 48; 105 | number /= 10; 106 | } 107 | if (index != 1) // to not call fn if number is only one digit 108 | REVERSE(s, s_index, index); //reverse(s, index, s_index); 109 | s[index] = 0; 110 | } 111 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_gad/Socket_Server_test/thread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * thread.h 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: root 6 | */ 7 | 8 | #ifndef HEADER_THREAD_H_ 9 | #define HEADER_THREAD_H_ 10 | 11 | /*********************** includes **********************************/ 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "ethernet_socket.h" 27 | #include "signal_handling.h" 28 | /*********************** Definitions **********************************/ 29 | #define TIME_SIZE 24 30 | #define DATA_MAX_SIZE 64 31 | #define ID_MAX_SIZE 4 32 | #define DLC_MAX_SIZE 2 33 | #define PACKET_MAX_SIZE ID_MAX_SIZE+DLC_MAX_SIZE+DATA_MAX_SIZE + TIME_SIZE 34 | #define REVERSE(ARR_NAME,ARR_INDX,ARR_LEN) for(uint8_t j = ARR_LEN - 1, idx=ARR_INDX,temp = 0;idx for running server and Client in the same PC 17 | $ sudo ./worspace/Client_ECU/Debug/Client_ECU ==> for running server and Client in different PCs 18 | 19 | note: PORT number for establishing Ethernet socket connection is 5454 20 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Client_ECU/ethernet_socket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ethernet_socket.c 3 | * 4 | * Created on: Dec 27, 2019 5 | * Author: root 6 | */ 7 | 8 | #include "ethernet_socket.h" 9 | 10 | int client_fd = -1; 11 | 12 | void ethernetSocketClient_init(const char *IP) { 13 | 14 | struct sockaddr_in serv_addr; 15 | 16 | //set server parameters 17 | serv_addr.sin_family = AF_INET; 18 | serv_addr.sin_port = htons(PORT); 19 | serv_addr.sin_addr.s_addr = inet_addr(IP); 20 | 21 | // Create client socket 22 | if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 23 | perror("Socket Creation:"); 24 | exit(0); 25 | } 26 | printf("Socket no=%d\n", client_fd); 27 | if (connect(client_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) 28 | < 0) { 29 | perror("Socket Connection:"); 30 | exit(0); 31 | } 32 | printf("Connection established!!\n "); 33 | } 34 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Client_ECU/ethernet_socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ethernet_socket.h 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Nader 6 | */ 7 | 8 | #ifndef ETHERNET_SOCKET_H_ 9 | #define ETHERNET_SOCKET_H_ 10 | 11 | /*********************** includes **********************************/ 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | /****************************** Definitions *******************************/ 20 | #define PORT 5454 21 | 22 | /*********************** Prototypes **********************************/ 23 | void ethernetSocketClient_init(const char *IP); 24 | 25 | /****************************** externs *******************************/ 26 | extern int client_fd; 27 | #endif /* ETHERNET_SOCKET_H_ */ 28 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Client_ECU/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Nader 6 | */ 7 | 8 | #include "threads.h" 9 | #include "my_signals.h" 10 | 11 | canPacket receivedPacket; 12 | 13 | int main(int argc, char * argv[]) { 14 | 15 | pthread_t clientSocket_thread; 16 | pthread_t dumpFile_thread; 17 | 18 | //acquire mutex 19 | pthread_mutex_lock(&recvMutex); 20 | //Create threads 21 | signal(SIGINT, int_Signal_handler); 22 | 23 | if (pthread_create(&clientSocket_thread, NULL, Thread_SocketClient, argv[1]) 24 | < 0) { 25 | perror("clientSocket_thread"); 26 | exit(0); 27 | } 28 | if (pthread_create(&dumpFile_thread, NULL, Thread_WriteFile, NULL) < 0) { 29 | perror("dumpFile_thread"); 30 | exit(0); 31 | } 32 | 33 | //join threads 34 | pthread_join(clientSocket_thread, NULL); 35 | pthread_join(dumpFile_thread, NULL); 36 | } 37 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Client_ECU/my_signals.c: -------------------------------------------------------------------------------- 1 | /* 2 | * my_signals.c 3 | 4 | * 5 | * Created on: Dec 24, 2019 6 | * Author: embedded_system_ks 7 | */ 8 | #include "my_signals.h" 9 | 10 | void int_Signal_handler(int signum) { 11 | 12 | pid_t pid = fork(); 13 | 14 | if (pid > 0) { 15 | //parent code 16 | printf("Parent Process has been Killed\n"); 17 | exit(1); 18 | 19 | } else if (!pid) { 20 | //child code 21 | printf("Closing Files and open Can Log file by Child Process!"); 22 | 23 | close(file_fd); 24 | close(client_fd); 25 | execl("/usr/bin/gedit", "gedit", FILE_PATH, NULL); 26 | 27 | while (1) { 28 | } 29 | 30 | } else { 31 | perror("Fork:"); 32 | } 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Client_ECU/my_signals.h: -------------------------------------------------------------------------------- 1 | /* 2 | * my_signals.h 3 | * 4 | * Created on: Dec 24, 2019 5 | * Author: embedded_system_ks 6 | */ 7 | 8 | #ifndef MY_SIGNALS_H_ 9 | #define MY_SIGNALS_H_ 10 | 11 | #include "threads.h" 12 | #include "ethernet_socket.h" 13 | 14 | void int_Signal_handler(int arg); 15 | 16 | 17 | #endif /* MY_SIGNALS_H_ */ 18 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Client_ECU/threads.c: -------------------------------------------------------------------------------- 1 | /* 2 | * threads.c 3 | * 4 | * Created on: Dec 24, 2019 5 | * Author: Ahmed Nader 6 | */ 7 | #include "threads.h" 8 | 9 | pthread_mutex_t recvMutex; 10 | 11 | int received_bytes = 0; 12 | int file_fd = -1; 13 | 14 | void * Thread_WriteFile(void *arg) { 15 | 16 | time_t Epoch_date = 0; 17 | 18 | file_fd = open(FILE_PATH, O_RDWR | O_CREAT | O_TRUNC, 0744); 19 | 20 | if (file_fd == -1) { 21 | perror("File Open"); 22 | exit(0); 23 | } 24 | while (1) { 25 | 26 | pthread_mutex_lock(&recvMutex); 27 | 28 | printf("File Opened waiting data...... \n"); 29 | 30 | Epoch_date = time(NULL); 31 | 32 | memcpy(receivedPacket.packet.data + strlen(receivedPacket.packet.data), 33 | ctime(&Epoch_date), TIME_SIZE); 34 | 35 | printf("writing : %s\n\n",receivedPacket.packetInBytes); 36 | 37 | 38 | write_in_file(); 39 | } 40 | } 41 | 42 | void * Thread_SocketClient(void * arg) { 43 | 44 | ethernetSocketClient_init((char *)arg) ; 45 | 46 | while (1) { 47 | while (!received_bytes) { 48 | received_bytes = read(client_fd, receivedPacket.packetInBytes, 49 | sizeof(receivedPacket.packetInBytes)); 50 | } 51 | memset(receivedPacket.packetInBytes + received_bytes, 0, 52 | sizeof(receivedPacket.packetInBytes) - received_bytes); 53 | 54 | printf("received packet: %s\nand number of bytes are %d\n", 55 | receivedPacket.packetInBytes, received_bytes); 56 | 57 | received_bytes = 0; 58 | 59 | //release mutex 60 | pthread_mutex_unlock(&recvMutex); 61 | } 62 | } 63 | void write_in_file(void){ 64 | 65 | 66 | write(file_fd ,"ID = ",strlen("ID = ")); 67 | write(file_fd, &receivedPacket.packet.ID,sizeof(receivedPacket.packet.ID)); 68 | write(file_fd, "\n", strlen("\n")); 69 | 70 | write(file_fd ,"DLC = ",strlen("DLC = ")); 71 | write(file_fd, &receivedPacket.packet.DLC,sizeof(receivedPacket.packet.DLC)); 72 | write(file_fd, "\n", strlen("\n")); 73 | 74 | write(file_fd ,"data = ",strlen("data = ")); 75 | write(file_fd, receivedPacket.packet.data,receivedPacket.packet.DLC -'0' ); 76 | write(file_fd, "\n", strlen("\n")); 77 | 78 | write(file_fd ,"TX time = ",strlen("TX time = ")); 79 | write(file_fd, receivedPacket.packet.data+receivedPacket.packet.DLC -'0' ,TIME_SIZE ); 80 | write(file_fd, "\n", strlen("\n")); 81 | 82 | 83 | write(file_fd ,"RX time = ",strlen("RX time = ")); 84 | write(file_fd, receivedPacket.packet.data+receivedPacket.packet.DLC -'0'+TIME_SIZE,TIME_SIZE ); 85 | write(file_fd, "\n", strlen("\n")); 86 | 87 | write(file_fd, "=======================================", strlen("=======================================")); 88 | write(file_fd, "\n", strlen("\n")); 89 | } 90 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Client_ECU/threads.h: -------------------------------------------------------------------------------- 1 | /* 2 | * threads.h 3 | * 4 | * Created on: Dec 24, 2019 5 | * Author: embedded_system_ks 6 | */ 7 | 8 | #ifndef THREADS_H_ 9 | #define THREADS_H_ 10 | 11 | /*********************** includes **********************************/ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "ethernet_socket.h" 31 | 32 | 33 | /*********************** Definitions ***********************/ 34 | #define TIME_SIZE 24 35 | #define DATA_MAX_SIZE 64 36 | 37 | #define FILE_PATH "./dataLog.txt" 38 | /*********************** Decelerations ***********************/ 39 | 40 | #pragma pack(push,1) 41 | struct packet_struct { 42 | unsigned short ID; 43 | unsigned char DLC; 44 | char data[DATA_MAX_SIZE + 2*TIME_SIZE]; 45 | 46 | }packet; 47 | #pragma pack(pop) 48 | 49 | typedef struct packet_struct packet_t; 50 | 51 | typedef union { 52 | packet_t packet; 53 | char packetInBytes[sizeof(packet)]; 54 | } canPacket; 55 | 56 | extern pthread_mutex_t recvMutex; 57 | extern int file_fd; 58 | extern canPacket receivedPacket; 59 | 60 | /*********************** Prototypes **********************************/ 61 | void * Thread_WriteFile(void * arg); 62 | void * Thread_SocketClient(void * arg); 63 | void write_in_file(void); 64 | 65 | #endif /* THREADS_H_ */ 66 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Server_ECU/README.txt: -------------------------------------------------------------------------------- 1 | this is the implementation of server ECU code. 2 | 3 | Server ECU : 4 | - receives data packet from dummy ECU via serial interface. 5 | - validate these data, 6 | - add the received time to the packet 7 | - Send the packet to ECU-2 via Ethernet Socket communication. 8 | 9 | If ECU-1 terminates, 10 | - It closes socket connections. 11 | - It closes serial connections. 12 | 13 | to run it correctly : 14 | execute the .out file with passing the USB device file path which the Linux OS has create it 15 | for example, if your USB read as a file in "/dev/ttyUSB0" you must execute it as 16 | 17 | $ sudo ./worspace/Server_ECU/Debug/Server_ECU /dev/ttyUSB0 18 | 19 | note: PORT number for establishing Ethernet socket connection is 5454 20 | 21 | note: serial congiguration is 8N1 with baud rate 115200 22 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Server_ECU/ethernet_socket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ethernet_socket.c 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Nader 6 | */ 7 | #include "ethernet_socket.h" 8 | 9 | int new_socket; 10 | int server_fd; 11 | 12 | void ethernetSocketServer_init(void) { 13 | 14 | struct sockaddr_in address; 15 | 16 | memset(&address, 0x00, sizeof(address)); 17 | 18 | // Creating socket file descriptor 19 | if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { 20 | perror("socket failed"); 21 | exit(EXIT_FAILURE); 22 | } 23 | 24 | address.sin_family = AF_INET; 25 | address.sin_addr.s_addr = htonl(INADDR_ANY); 26 | address.sin_port = htons(PORT); 27 | 28 | // Forcefully attaching socket to the port 29 | if (bind(server_fd, (struct sockaddr *) &address, sizeof(address)) < 0) { 30 | perror("bind failed"); 31 | exit(EXIT_FAILURE); 32 | } 33 | 34 | if (listen(server_fd, 10) < 0) { 35 | perror("listen"); 36 | exit(EXIT_FAILURE); 37 | } 38 | 39 | if ((new_socket = accept(server_fd, NULL, NULL)) < 0) { 40 | perror("accept"); 41 | exit(EXIT_FAILURE); 42 | } 43 | printf("connection established \n"); 44 | } 45 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Server_ECU/ethernet_socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ethernet_socket.h 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Nader 6 | */ 7 | 8 | #ifndef ETHERNET_SOCKET_H_ 9 | #define ETHERNET_SOCKET_H_ 10 | 11 | /*********************** includes **********************************/ 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | /****************************** Definitions *******************************/ 20 | #define PORT 5454 21 | 22 | /*********************** Prototypes **********************************/ 23 | void ethernetSocketServer_init(void); 24 | 25 | /****************************** externs *******************************/ 26 | extern int new_socket; 27 | extern int server_fd; 28 | #endif /* ETHERNET_SOCKET_H_ */ 29 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Server_ECU/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Nader 6 | */ 7 | 8 | #include "serial.h" 9 | #include "thread.h" 10 | #include "signal_handling.h" 11 | #include "ethernet_socket.h" 12 | 13 | canPacket recievedPacket; 14 | 15 | int main(int argc, char * argv[]) { 16 | 17 | pthread_t serial_thread; 18 | pthread_t dataValidation_thread; 19 | pthread_t socketServer_thread; 20 | 21 | serial_init(argv[1]); 22 | 23 | pthread_mutex_lock(&serialData_mutex); 24 | pthread_mutex_lock(&dataValidation_mutex); 25 | 26 | signal(SIGINT, int_signal_handler); 27 | 28 | if ((pthread_create(&serial_thread, NULL, serial_read, NULL)) < 0) { 29 | perror("serial_thread_create"); 30 | exit(0); 31 | } 32 | if ((pthread_create(&dataValidation_thread, NULL, data_validation, NULL)) 33 | < 0) { 34 | perror("dataValidation_thread_create"); 35 | exit(0); 36 | } 37 | if ((pthread_create(&socketServer_thread, NULL, socket_server, NULL)) < 0) { 38 | perror("server_thread_create"); 39 | exit(0); 40 | } 41 | pthread_join(serial_thread, NULL); 42 | pthread_join(dataValidation_thread, NULL); 43 | pthread_join(socketServer_thread, NULL); 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Server_ECU/serial.c: -------------------------------------------------------------------------------- 1 | 2 | #include "serial.h" 3 | 4 | int serial_fd;/*File Descriptor*/ 5 | 6 | void serial_init(const char * serial_path) { 7 | 8 | /*------------------------------- Opening the Serial Port -------------------------------*/ 9 | 10 | /* Change /dev/ttyUSB0 to the one corresponding to your system */ 11 | serial_fd = open(serial_path, O_RDWR | O_NOCTTY); /* ttyS0 is the FT232 based USB2SERIAL Converter */ 12 | /* O_RDWR - Read/Write access to serial port */ 13 | /* O_NOCTTY - No terminal will control the process */ 14 | /* Open in blocking mode,read will wait */ 15 | 16 | 17 | if(serial_fd == -1) /* Error Checking */ 18 | { 19 | printf("Error! in Opening serial_fd \n"); 20 | exit(0); 21 | } 22 | else 23 | printf("serial_fd Opened Successfully\n"); 24 | 25 | /*---------- Setting the Attributes of the serial port using termios structure --------- */ 26 | 27 | struct termios SerialPortSettings; /* Create the structure */ 28 | 29 | tcgetattr(serial_fd, &SerialPortSettings); /* Get the current attributes of the Serial port */ 30 | 31 | /* Setting the Baud rate */ 32 | cfsetispeed(&SerialPortSettings, B115200); /* Set Read Speed as 115200 */ 33 | cfsetospeed(&SerialPortSettings, B115200); /* Set Write Speed as 115200 */ 34 | 35 | /* 8N1 Mode */ 36 | SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines */ 37 | SerialPortSettings.c_cflag &= ~PARENB; /* Disables the Parity Enable bit(PARENB),So No Parity */ 38 | SerialPortSettings.c_cflag &= ~CSTOPB; /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */ 39 | SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the mask for setting the data size */ 40 | SerialPortSettings.c_cflag |= CS8; /* Set the data bits = 8 */ 41 | 42 | SerialPortSettings.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG); 43 | 44 | SerialPortSettings.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK 45 | | INPCK | ISTRIP | IXON); /* Disable XON/XOFF flow control both i/p and o/p */ 46 | 47 | SerialPortSettings.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL 48 | | OPOST);/*No Output Processing*/ 49 | 50 | /* Setting Time outs */ 51 | #ifndef BLOCKING_SERIAL 52 | SerialPortSettings.c_cc[VMIN] = 255; /* non-blocking mode */ 53 | SerialPortSettings.c_cc[VTIME] = 1; /* Wait 0.5 seconds */ 54 | 55 | #elif BLOCKING_SERIAL 56 | SerialPortSettings.c_cc[VMIN] = 0; 57 | SerialPortSettings.c_cc[VTIME] = 5; 58 | #endif // BLOCKING_SERIAL 59 | 60 | 61 | tcsetattr(serial_fd, TCSANOW, &SerialPortSettings); 62 | 63 | 64 | if((tcsetattr(serial_fd,TCSANOW,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/ 65 | { 66 | printf("\n ERROR ! in serial_fd Setting attributes\n"); 67 | exit(0); 68 | } 69 | 70 | 71 | 72 | /*------------------------------- Read data from serial port -----------------------------*/ 73 | fcntl(serial_fd, F_SETFL, 0); 74 | tcflush(serial_fd, TCIOFLUSH); /* Discards old data in the tx/rx buffer */ 75 | } 76 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Server_ECU/serial.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * serial.h 4 | * 5 | * Created on: Jul 25, 2018 6 | * Author: root 7 | */ 8 | 9 | #ifndef SERIAL_H_ 10 | #define SERIAL_H_ 11 | 12 | /*********************** includes **********************************/ 13 | 14 | #include 15 | #include /* File Control Definitions */ 16 | #include /* POSIX Terminal Control Definitions */ 17 | #include /* UNIX Standard Definitions */ 18 | #include /* ERROR Number Definitions */ 19 | #include 20 | #include 21 | 22 | 23 | /*********************** Prototypes **********************************/ 24 | void serial_init(const char *serial_path); 25 | 26 | /*********************** Decelerations ***********************/ 27 | extern int serial_fd; // file descriptor for serial 28 | 29 | 30 | 31 | #endif /* SERIAL_H_ */ 32 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Server_ECU/signal_handling.c: -------------------------------------------------------------------------------- 1 | /* 2 | * signal_handling.c 3 | * 4 | * Created on: Dec 24, 2019 5 | * Author: root 6 | */ 7 | 8 | #include "signal_handling.h" 9 | 10 | void int_signal_handler(void *arg) { 11 | 12 | printf("Alert !!!!\n"); 13 | printf("Closing serial reading and socket server\n"); 14 | 15 | close(server_fd); 16 | close(serial_fd); 17 | 18 | exit(0); 19 | } 20 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Server_ECU/signal_handling.h: -------------------------------------------------------------------------------- 1 | /* 2 | * signal_handling.h 3 | * 4 | * Created on: Dec 24, 2019 5 | * Author: root 6 | */ 7 | 8 | #ifndef SIGNAL_HANDLING_H_ 9 | #define SIGNAL_HANDLING_H_ 10 | 11 | /*********************** includes **********************************/ 12 | #include 13 | #include 14 | #include 15 | 16 | #include "serial.h" 17 | #include "ethernet_socket.h" 18 | 19 | /*********************** Prototypes **********************************/ 20 | void int_signal_handler(void *arg); 21 | 22 | #endif /* SIGNAL_HANDLING_H_ */ 23 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Server_ECU/thread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * thread.c 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: Ahmed Nader 6 | */ 7 | 8 | #include "thread.h" 9 | 10 | pthread_mutex_t serialData_mutex = PTHREAD_MUTEX_INITIALIZER; 11 | pthread_mutex_t dataValidation_mutex = PTHREAD_MUTEX_INITIALIZER; 12 | 13 | static unsigned int bytes_read = 0; 14 | 15 | void * serial_read(void * arg) { 16 | 17 | while (1) { 18 | printf(".....Inside serial_read thread.....\n"); 19 | 20 | bytes_read = read(serial_fd, (char *) recievedPacket.packetInBytes, 21 | sizeof(recievedPacket.packetInBytes)); 22 | //clear buffer 23 | memset(recievedPacket.packetInBytes + bytes_read, 0x00, 24 | sizeof(recievedPacket.packetInBytes) - bytes_read); 25 | //Echo received bytes again 26 | write(serial_fd, recievedPacket.packetInBytes, strlen(recievedPacket.packetInBytes)); 27 | 28 | printf("received packet is = %s\nand number of bytes are %d\n", 29 | recievedPacket.packetInBytes, bytes_read); 30 | 31 | pthread_mutex_unlock(&serialData_mutex); 32 | } 33 | } 34 | 35 | void * data_validation(void * arg) { 36 | 37 | time_t Epoch_date = 0; 38 | 39 | while (1) { 40 | 41 | pthread_mutex_lock(&serialData_mutex); 42 | 43 | printf(".....Inside data_validation thread.....\n"); 44 | 45 | if (((recievedPacket.packetInBytes[2] - '0') 46 | == strlen(recievedPacket.packetInBytes) - 3) 47 | && ((recievedPacket.packetInBytes[2] - '0') == bytes_read - 3)) { 48 | 49 | printf("received data is valid\n"); 50 | 51 | Epoch_date = time(NULL); 52 | 53 | memcpy(recievedPacket.packet.data + strlen(recievedPacket.packet.data), 54 | ctime(&Epoch_date), TIME_SIZE); 55 | 56 | pthread_mutex_unlock(&dataValidation_mutex); 57 | 58 | } else { 59 | 60 | printf("received data is not valid\nPlease try again\n"); 61 | 62 | } 63 | 64 | } 65 | 66 | } 67 | 68 | void * socket_server(void *arg) { 69 | 70 | ethernetSocketServer_init(); 71 | 72 | while (1) { 73 | 74 | pthread_mutex_lock(&dataValidation_mutex); 75 | 76 | printf("packet to be sent = %s\n", recievedPacket.packetInBytes); 77 | write(new_socket, recievedPacket.packetInBytes, strlen(recievedPacket.packetInBytes)); 78 | 79 | printf("\n\n"); 80 | 81 | } 82 | 83 | } 84 | 85 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/ahmed_nader/Server_ECU/thread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * thread.h 3 | * 4 | * Created on: Dec 23, 2019 5 | * Author: root 6 | */ 7 | 8 | #ifndef HEADER_THREAD_H_ 9 | #define HEADER_THREAD_H_ 10 | 11 | /*********************** includes **********************************/ 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "serial.h" 27 | #include "ethernet_socket.h" 28 | #include "signal_handling.h" 29 | 30 | #define TIME_SIZE 25 31 | #define DATA_MAX_SIZE 64 32 | /*********************** Prototypes **********************************/ 33 | void * serial_read(void * arg); 34 | void * data_validation(void * arg); 35 | void * socket_server(void *arg); 36 | 37 | /*********************** Definitions **********************************/ 38 | #define TIME_SIZE 24 39 | #define DATA_MAX_SIZE 64 40 | 41 | /*********************** Decelerations *******************************/ 42 | #pragma pack(push,1) 43 | struct packet_t { 44 | unsigned short ID; 45 | unsigned char DLC; 46 | char data[DATA_MAX_SIZE + TIME_SIZE]; 47 | 48 | } packet; 49 | #pragma pack(pop) 50 | 51 | typedef union { 52 | struct packet_t packet; 53 | char packetInBytes[sizeof(packet)]; 54 | } canPacket; 55 | 56 | /****************************** externs *******************************/ 57 | 58 | extern canPacket recievedPacket; 59 | 60 | extern pthread_mutex_t serialData_mutex; 61 | extern pthread_mutex_t dataValidation_mutex; 62 | 63 | #endif /* HEADER_THREAD_H_ */ 64 | -------------------------------------------------------------------------------- /Projects/Project_1_part4/implementation_solutiones/project_pres.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/Project_1_part4/implementation_solutiones/project_pres.pdf -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Nader/OTA pres.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Nader/OTA pres.pdf -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Nader/OTA_video.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Nader/OTA_video.mp4 -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Nader/ReadME: -------------------------------------------------------------------------------- 1 | This is a simple proof of concept for over the air technology (OTA) 2 | In this project, QEMU Emulator is used, to emulate Vexpress development kit .besides, there is a a virtual SD card. 3 | 4 | 5 | Vexpress kit is connected with SD Card which contains: 6 | o Test.elf => The applications 7 | o Vexpress_blob => pre-built environment variables 8 | 9 | Project Steps: 10 | ================= 11 | load test.elf file from MMC to ram in address 0x73000000; 12 | save test.elf size downloaded from MMC; 13 | setup TFTP credentials 14 | load test.elf file from TFTP to ram in address 0x70000000; 15 | if (load test.elf file from TFTP to ram in address 0x70000000) 16 | { 17 | save test.elf size downloaded from TTFP server; 18 | if ( MMC_file_size == tftp_file_size ){ 19 | print "same Version" 20 | } 21 | else{ 22 | print "different Version" 23 | remove old backup.elf file from SD card; 24 | save new image ,downloaded from TFTP server, to MMC under test.elf; 25 | save old image ,already exist in mmc, to MMC under backup.elf; 26 | } 27 | } 28 | else{ 29 | load from 0x70000000 30 | } 31 | boot test.elf 32 | 33 | ======================================================================================================================= 34 | pre-built environment variables 35 | ================================== 36 | setenv mmcLoadAddr 0x73000000 37 | setenv tftpLoadAddr 0x70000000 38 | 39 | setenv bootcmd 'fatload mmc 0:1 ${mmcLoadAddr} test.elf ; 40 | setenv mmcFileSize $filesize ; 41 | dhcp ; 42 | setenv serverip 192.168.50.188; 43 | if tftp ${tftpLoadAddr} test.elf ; 44 | then; 45 | setenv tftpFileSize $filesize ; 46 | if test "${tftpFileSize}" = "${mmcFileSize}"; 47 | then ; 48 | echo "same version"; 49 | else ; 50 | echo "New version"; 51 | fatrm mmc 0:1 backup.elf ; 52 | fatwrite mmc 0:1 ${mmcLoadAddr} backup.elf ${mmcFileSize}; 53 | fatwrite mmc 0:1 ${tftpLoadAddr} test.elf ${tftpFileSize}; 54 | fi; 55 | else; 56 | setenv tftpLoadAddr $mmcLoadAddr ; 57 | fi; 58 | bootelf ${tftpLoadAddr}' 59 | ========================================================================================================================== 60 | Prerequisites to run OTA project 61 | ================================= 62 | • Uboot => executable uboot for vexpress 63 | • KS_SD_512M.img => Virtual SD card 64 | • lab2.sh => shell script to vexpress on QEMU 65 | 66 | ./lab2.sh u-boot/u-boot KS_SD_512M.img 67 | -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Nader/lab2.sh: -------------------------------------------------------------------------------- 1 | #@Copyright www.learn-in-depth.com 2 | #Author: Keroles Shenouda 3 | # Learn In Depth 4 | qemu-system-arm -M vexpress-a9 -m 512M \ 5 | -kernel $1 \ 6 | -no-reboot \ 7 | -monitor telnet:127.0.0.1:1234,server,nowait \ 8 | -serial stdio \ 9 | -nographic \ 10 | -sd $2 11 | #-S -gdb tcp::1212 \ 12 | 13 | -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Nader/u-boot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Nader/u-boot -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Nader/vexpress_blob: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Nader/vexpress_blob -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Nader/vexpress_blob.txt: -------------------------------------------------------------------------------- 1 | 2 | setenv mmcLoadAddr 0x73000000 3 | 4 | setenv tftpLoadAddr 0x70000000 5 | 6 | setenv bootcmd 'fatload mmc 0:1 ${mmcLoadAddr} test.elf ;setenv mmcFileSize $filesize ;dhcp ;setenv serverip 192.168.50.188;if tftp ${tftpLoadAddr} test.elf ;then ;setenv tftpFileSize $filesize ;if test "${tftpFileSize}" = "${mmcFileSize}";then ;echo "same version";else ;echo "New version";fatrm mmc 0:1 backup.elf ; fatwrite mmc 0:1 ${mmcLoadAddr} backup.elf ${mmcFileSize} ; fatwrite mmc 0:1 ${tftpLoadAddr} test.elf ${tftpFileSize}; fi ;else ;setenv tftpLoadAddr $mmcLoadAddr ;fi ;bootelf ${tftpLoadAddr}' 7 | 8 | ./lab2.sh /media/embedded_system_ks/Embedded_KS_labs/u-boot/u-boot KS_SD_512M.img 9 | -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Sayed/NAP_SD_2M.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Sayed/NAP_SD_2M.img -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Sayed/OTA.sh: -------------------------------------------------------------------------------- 1 | 2 | #Author: A_Napster_H 3 | 4 | qemu-system-arm -M vexpress-a9 -m 512M \ 5 | -kernel u-boot \ 6 | -no-reboot \ 7 | -monitor telnet:127.0.0.1:1234,server,nowait \ 8 | -serial stdio \ 9 | -nographic \ 10 | -sd NAP_SD_2M.img 2> /dev/null 11 | -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Sayed/README.md: -------------------------------------------------------------------------------- 1 | #Author: A_Napster_H 2 | #Project 2 Embedded_Linux Over the Air simple concept 3 | 4 | ------------------------------------------The below code is simple concept for implementation of OTA app-------------------------------------- 5 | 6 | The code will be doing the following:- 7 | 8 | - Prepare the TFTP server 9 | - Load file from TFTP server to memory 10 | - Create variable to store the tftp size 11 | - Load file from sd card to memory 12 | - Create variable to store the sd card size 13 | - check if tftp file size is equal the sd card size 14 | the file will load from sd card 15 | Otherwise 16 | change the name of the file to testbackup.elf 17 | copy the tftp file to sd card and load it 18 | 19 | -------------------------------------------------copy the below code in u boot command window------------------------------------------------ 20 | 21 | setenv tftp_load 'echo "=======>TFTP LOAD";dhcp;setenv serverip 192.168.1.10;tftp 0x70000000 test.elf;setenv tftp_size $filesize' 22 | setenv sd_load 'echo "=======>SD CARD LOAD";fatload mmc 0:1 0x72000000 test.elf;setenv sd_size $filesize' 23 | setenv cmp_backup 'echo "======>Start backup";fatwrite mmc 0:1 0x72000000 testbackup.elf $sd_size;fatrm mmc 0:1 test.elf;fatwrite mmc 0:1 0x70000000 test.elf $tftp_size' 24 | setenv ota_cmp 'echo "=======>Start Checking";if test "${tftp_size}" = "${sd_size}";then echo "======>SAME SIZE";bootelf 0x72000000;else echo "======>DIFFERENT SIZE";run cmp_backup;bootelf 0x70000000;fi' 25 | setenv OTA 'run tftp_load ;run sd_load; run ota_cmp' 26 | setenv bootcmd 'echo "A_napster_H OTA";run OTA' 27 | 28 | -------------------------------------------------The following is details of the above code---------------------------------------------------- 29 | 30 | -dhcp // Prepare the TFTP server 31 | -setenv serverip // Set the IP server 32 | -tftp 0x70000000 test.elf // Load file from TFTP server to memory 33 | -setenv tftp_file_size $filesize // Create variable to store the tftp size 34 | -fatload mmc 0:1 0x72000000 test.elf // Load file from sd card to memory 35 | -setenv sd_file_size $filesize // Create variable to store the sd card size 36 | -if test "${tftp_file_size}" = "${sd_file_size}"; // check if tftp file size is equal the sd card size 37 | then 38 | bootelf 0x72000000 // load the file from sd_card 39 | else 40 | fatwrite mmc 0:1 0x72000000 testbackup.elf $sd_card_size // copy test.elf to testbackup.elf "sd_card copy" 41 | fatrm mmc 0:1 test.elf // remove test.elf 42 | fatwrite mmc 0:1 0x70000000 test.elf $tftp_file_size // copy from memory to sd_card "sd_card copy" 43 | bootelf 0x70000000 // boot from memory 44 | -fi 45 | 46 | 47 | --------------------------------------------- To run this APP you should do the following ----------------------------------------------------- 48 | 49 | open terminal to run TFTP server 50 | - tftp // server ip address come from ifconfig if it will be localhost 51 | - get test.elf //test.elf is the filename 52 | open terminal 53 | -./OTA.sh 54 | -press anykey to stop auto uboot 55 | 56 | -setenv tftp_load 'echo "=======>TFTP LOAD";dhcp;setenv serverip 192.168.1.10;tftp 0x70000000 test.elf;setenv tftp_size $filesize' 57 | -setenv sd_load 'echo "=======>SD CARD LOAD";fatload mmc 0:1 0x72000000 test.elf;setenv sd_size $filesize' 58 | -setenv cmp_backup 'echo "======>Start backup";fatwrite mmc 0:1 0x72000000 testbackup.elf $sd_size;fatrm mmc 0:1 test.elf;fatwrite mmc 0:1 0x70000000 test.elf $tftp_size' 59 | -setenv ota_cmp 'echo "=======>Start Checking";if test "${tftp_size}" = "${sd_size}";then echo "======>SAME SIZE";bootelf 0x72000000;else echo "======>DIFFERENT SIZE";run cmp_backup;bootelf 0x70000000;fi' 60 | -setenv OTA 'run tftp_load ;run sd_load; run ota_cmp' 61 | -setenv bootcmd 'echo "A_napster_H OTA";run OTA' 62 | -saveenv 63 | -ctrl+C to kill the process 64 | -./OTA.sh 65 | 66 | -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Sayed/step_photos/RUN_Change-env.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Sayed/step_photos/RUN_Change-env.png -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Sayed/step_photos/TFTP_Different_SD_CARD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Sayed/step_photos/TFTP_Different_SD_CARD.png -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Sayed/step_photos/TFTP_SAME_SD_CARD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Sayed/step_photos/TFTP_SAME_SD_CARD.png -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Sayed/tftp/test.elf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Sayed/tftp/test.elf -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Sayed/u-boot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Sayed/u-boot -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Sayed/videos/OTA_steps: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Sayed/videos/OTA_steps -------------------------------------------------------------------------------- /Projects/project_2_OTA/Ahmed_Sayed/videos/tftp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/keroles/embedded_linux_1/bcc02e8008f3476a4ae690af3ee7876427c54dce/Projects/project_2_OTA/Ahmed_Sayed/videos/tftp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # embedded_linux_1 2 | ++++++++++++++++++++ 3 | 4 | EMBEDDED linux 2019/2020 5 | Eng.Keroles Shenouda 6 | https://www.learn-in-depth.com/ 7 | https://www.facebook.com/groups/embedded.system.KS/ 8 | 9 | 10 | -------------------------------------------------------------------------------- /file1.txt: -------------------------------------------------------------------------------- 1 | learn-in-depth 2 | --------------------------------------------------------------------------------