/*----------------------------------------------------------------------------*/ void pinInput(struct Pin pin) { LPC_GPIO_Type * const reg = calcPort(pin.data); commonPinInit(pin); reg->DIR &= ~(1 << pin.data.offset); }
/*----------------------------------------------------------------------------*/ void pinOutput(struct Pin pin, bool value) { LPC_GPIO_Type * const reg = calcPort(pin.data); commonPinInit(pin); reg->DIR |= 1 << pin.data.offset; pinWrite(pin, value); }
/*----------------------------------------------------------------------------*/ struct Pin pinInit(PinNumber id) { struct Pin pin; pin.data.port = PIN_TO_PORT(id); pin.data.offset = PIN_TO_OFFSET(id); pin.reg = id ? calcPort(pin.data) : 0; return pin; }
/*----------------------------------------------------------------------------*/ static inline void *calcMaskedReg(struct PinData data) { return (void *)(calcPort(data)->MASKED_ACCESS + (1UL << data.offset)); }
//Server - reciever void createReciever(argsOfThreadRecieve *args) { int i, index = 0; int len = (args->dvCount + 1);//correct length for buffer = DV+1 int* m_buffer = malloc(sizeof(int)*len); if(m_buffer == NULL) { printf("Error Allocating\n"); pthread_exit(NULL); } int fixPort = calcPort(args->name, args->port);//calculate the correct port int m_socket_fd = 0 , m_conn_fd = 0; struct sockaddr_in m_socket_struct; struct sockaddr_in client_addr; socklen_t size = sizeof (client_addr); //creates an endpoint for communication and returns a descriptor m_socket_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (m_socket_fd < 0 ) { perror("Error creating socket\n"); free(m_buffer); pthread_exit(NULL); } memset(&m_socket_struct, 0, sizeof(m_socket_struct)); //initialize the sockaddr_in memory block to 0 m_socket_struct.sin_family = AF_INET; m_socket_struct.sin_addr.s_addr = htonl(INADDR_ANY); m_socket_struct.sin_port = htons(fixPort); //assigning a name to a socket if ((bind(m_socket_fd, (struct sockaddr*) &m_socket_struct, sizeof(m_socket_struct))) < 0 ) { perror("Error in Bind\n"); close(m_socket_fd); free(m_buffer); pthread_exit(NULL); } //listen for connections on a socket if (listen(m_socket_fd, 10) < 0 ) { perror("Error listening to socket\n"); close(m_socket_fd); free(m_buffer); pthread_exit(NULL); } //accept/reject jobs sent to a destination m_conn_fd = accept(m_socket_fd, (struct sockaddr *) &client_addr, &size); if (m_conn_fd < 0) { perror("Error on accepting\n"); free(m_buffer); pthread_exit(NULL); } while(1) { pthread_mutex_lock(&mutexLock); if(calc == 1)//reciever done { break; } int res; pthread_mutex_unlock(&mutexLock); //read from a file descriptor(m_buffer in our case) sleep(1); if ((res = read(m_conn_fd, m_buffer, len*sizeof(int))) < 0) { perror("Error in read\n"); close(m_conn_fd); free(m_buffer); pthread_exit(NULL); } else { //If read was successful //findes the index and update the global array when done wake up calc index = findNeiIndex(args->name, args->mySelf); pthread_mutex_lock(&mutexLock); //copy data to neighborsDv for (i = 0; i < len; ++i) { args->neighborsDv[index][i] = m_buffer[i]; } //check if got all neighbors DV int check = 1; for (i = 0; i < args->neighbors; ++i) { if(args->neighborsDv[i][0] == -1) { check = 0; } } if(check)//got all neighbors DV { pthread_cond_signal(&calcCond); } pthread_mutex_unlock(&mutexLock); } } pthread_mutex_unlock(&mutexLock); free(m_buffer); close(m_conn_fd); pthread_exit(NULL); }
//Client - sender void createSender(argsOfThreadSender *args) { int m_socket_fd = 0, m_conn_fd, res, j, i; int fixPort = calcPort(args->name, args->port);//calculate the correct port char* localHost = args->ip; int len = (args->dvCount + 1);//correct length for buffer = DV+1 int* m_buffer = malloc(sizeof(int)*len); if(m_buffer == NULL) { printf("Error Allocating\n"); pthread_exit(NULL); } struct sockaddr_in m_serv_addr; //creates an endpoint for communication and returns a descriptor m_socket_fd = socket(AF_INET, SOCK_STREAM, 0); if (m_socket_fd < 0 ) { perror("Error creating socket\n"); free(m_buffer); pthread_exit(NULL); } bzero(&m_serv_addr, sizeof(m_serv_addr)); m_serv_addr.sin_family = AF_INET; m_serv_addr.sin_port = htons(fixPort); m_serv_addr.sin_addr.s_addr = inet_addr(localHost); //initiate a connection on a socket i = 0; while(i < (args->conLoop))//attempts to connect { m_conn_fd = connect(m_socket_fd,(struct sockaddr *)&m_serv_addr, sizeof(m_serv_addr)); if (m_conn_fd < 0) { sleep(1); } else { break; } i++; } sleep(1); if(m_conn_fd < 0) { printf("ERROR in connecting - server\n"); close(m_socket_fd); free(m_buffer); pthread_exit(NULL); } while(1) { pthread_mutex_lock(&mutexLock); if(dvIsChaneged == 1)//own dv changed { m_buffer[0] = 1; } else { m_buffer[0] = 0; dvIsChaneged = 0; } //copy DV for (j = 0; j < (len - 1); ++j) { m_buffer[j+1] = args->routerDV[j]; } pthread_mutex_unlock(&mutexLock); //send the DV if((res = write(m_socket_fd, m_buffer, len*sizeof(int))) < 0) { free(m_buffer); close(m_socket_fd); pthread_exit(NULL); } pthread_mutex_lock(&mutexLock); if(calc == 1)//sender done { break; } pthread_cond_wait(&senderCond, &mutexLock);//wait until calc wakes me up pthread_mutex_unlock(&mutexLock); } pthread_mutex_unlock(&mutexLock); free(m_buffer); // if (close(m_socket_fd) < 0) // printf("Error closing socketFD\n"); close(m_socket_fd); pthread_exit(NULL); }