// Embedded Systems // BeagleBoard-XM. // Author : Jose Goncalves //

Similar documents
518 Lecture Notes Week 12

A. Basic Function Calls for Network Communications

chat.h #ifndef _CHAT_H #define _CHAT_H #define LBUFFSIZE 128 #define CBUFFSIZE 331

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Exam 2 Questions (document version 1.0)

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

Piotr Mielecki Ph. D.

How to write a Measurement Telnet Server

Εργαστήριο 9 I/O Multiplexing

CS4514 B08 HELP Session 1

Unix Network Programming

ECE 435 Network Engineering Lecture 2

Application Programming Interfaces

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

Introduction to Client-Server Model

ECE 435 Network Engineering Lecture 2

CSE 421/521 - Operating Systems Fall 2011 Recitations. Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar. Presented by...

Sockets 15H2. Inshik Song

How to write a Measurement Telnet Server


CS 3516: Computer Networks

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Oral. Total. Dated Sign (2) (5) (3) (2)

CS 640: Computer Networking

Ά η η 1 (30%): Sockets. socket () bind () listen () accept () connect () read () write () close ()

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs

ECE322 Systems Programming Project 2: Networking with Matrix Multiplication in C Grant Kimes 12/16/15

Network Socket Programming - 3 BUPT/QMUL

Sockets. 1 Introduction. (Reference:, Gray Chapter 10) Network Programming Lecture Notes by. Turhan TUNALI

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Programming with TCP/IP. Ram Dantu

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Lab 0. Yvan Petillot. Networks - Lab 0 1

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

Network Socket Programming - 3 BUPT/QMUL

Department of Computer Science

Socket Programming TCP UDP

l27 handout.txt buggy server.c Printed by Michael Walfish Apr 29, 10 13:41 Page 1/1 Apr 29, 10 11:51 Page 1/1

Linux Programming

CSE 333 SECTION 8. Sockets, Network Programming

Operating Systems Lab

CS 43: Computer Networks. 07: Concurrency and Non-blocking I/O Sep 17, 2018

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

CS , Fall 2008 Final Exam

Asynchronous I/O With boost.asio

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

CSE 43: Computer Networks Structure, Threading, and Blocking. Kevin Webb Swarthmore College September 14, 2017

ICT 6544 Distributed Systems Lecture 5

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Socket Programming 2007/03/28

TCP Echo Application: Client & Server. TA: Awad A Younis Class: CS457 Fall 2014

CS , Fall 2008 Final Exam

An Introductory 4.4BSD Interprocess Communication Tutorial

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Midterm Exam Questions (document version 1.1)

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

1 /* client.c - adapted from code for example client program that uses TCP */ 2 /*Modified by Vincent Chu, Winter

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

Why a Computer Network??? Stand alone Computer. For What?

CS321: Computer Networks Socket Programming

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

Socket Programming for TCP and UDP

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.3)

518 Lecture Notes Week 3

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports

Tutorial on Socket Programming

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

l27 handout.txt buggy server.c Printed by Michael Walfish Apr 28, 11 15:24 Page 1/1 Apr 27, 11 1:53 Page 1/2

Winsock Server adding Multiple clients support C++

CS4700/CS5700 Fundamentals of Computer Networking

Distributed programming

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

Sockets. UNIX-style IPC. Silberschatz, Galvin and Gagne 2005 Msc. Ivan A. Escobar Broitman 2007

Operating systems fundamentals - B06

Introduction to Berkeley Sockets

CS510 Operating System Foundations. Jonathan Walpole

IPv4 and ipv6 INTEROPERABILITY

Matt Ramsay CS 375 EXAM 2 Part 1

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

Lab 5: Inter-Process Communication

Embedded System Design

CSEN 602-Operating Systems, Spring 2018 Practice Assignment 2 Solutions Discussion:

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Figure 1 Ring Structures

2.2 IoT Internet of Things Android Studio

Compile and execute fifo1.cpp listed above. Try the Balady's anomaly examples discussed in class. Did you observe the Belady's anomaly?

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

Elementary TCP Sockets

Azblink API for Sending XMPP Messages via HTTP POST

Transcription:

// Embedded Systems // BeagleBoard-XM // Author : Jose Goncalves // jose.braga.pt@gmail.com // Teacher : Nuno Peixoto // University : IPCA // 17.03.2014 #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket.h> #include <unistd.h> #include <pthread.h> #include <sys/types.h> #include <netinet/in.h> #include <strings.h> #include <fcntl.h> #include <errno.h> //------------------------------------------------- improvments to do ------------------------------------------------------ // using strings instead of sequential characteres : 'T' + 'E' + 'M' + 'P',... using namespace std ; // --------------------------------------------------- Prototypes ----------------------------------------------------------- #include "Gpio.h" // Class Gpio #include "I2C_sensor.c" //char* Temp() return Temperature value //char* HR() return relative humidity value // Threads functions: void *connection_handler(void *) ; void *GPIO_Input(void *inport); void respond (int sock, Gpio Led1,Gpio Led2) ; void error(char *msg) ; // ---------------------------------------------------- main () ------------------------------------------------------------- int main() // Código do daemon ------------------------------------------------------- pid_t pid, sid; pid = fork(); if(pid < 0) fprintf(stderr, "Fatal error. Cannot initiate the daemon!\n"); exit(exit_failure); if(pid > 0) exit(exit_success) ; // Change the file mode mask umask(0); // Open any logs here //... // Create a new SID for the child process sid = setsid(); if(sid < 0) // Log any failure exit(exit_failure); // Close out the standard file descriptors close(stdin_fileno); close(stdout_fileno); close(stderr_fileno); // inibe stdin, stdout, stderr. por exemplo inibe os printf's() // The big loop while(1) // O nosso código vem aqui

// create thread GPIO_Input --------------------------------------------------------- pthread_t thread_input; int *p ; if( pthread_create( &thread_input, NULL, GPIO_Input, (void*) &p) < 0) perror("could not create thread"); // Create socket -------------------------------------------------------------------- int socket_desc, client_sock, c; // server is a structure of type struct sockaddr_in. This structure has 4 fields. struct sockaddr_in server, client; socket_desc = socket(af_inet, SOCK_STREAM, 0); if (socket_desc == -1) printf("could not create socket"); puts("socket created"); // Prepare the sockaddr_in structure ------------------------------------------------ server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; // INADDR_ANY : symbolic constant which gets the IP address server.sin_port = htons( 5000 ); // Bind ----------------------------------------------------------------------------- // Assign socket to the IP address + port number of the host if( bind(socket_desc,(struct sockaddr *)&server, sizeof(server)) < 0) perror("bind failed. Error"); puts("bind done"); // Listen ---------------------------------------------------------------------------- // initialize a wait queue of connections for socket socket_desc // maximum of 5 clients in the queue of pending connections listen(socket_desc, 5); // Accept and incoming connection // It extracts the first connection request on the queue of pending connections for the listening socket, sockfd, // creates a new connected socket, and returns a new file descriptor referring to that socket. // The newly created socket is not in the listening state. The original socket sockfd is unaffected by this call. pthread_t thread_id; puts("waiting for first incoming connections..."); c = sizeof(struct sockaddr_in); while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) ) puts("connection accepted"); printf("socket client ID : %d \n",client_sock); // create thread connection_handler(void *socket_desc) -------------------------- if( pthread_create( &thread_id, NULL, connection_handler, (void*) &client_sock) < 0) perror("could not create thread"); // Now join the thread, so that we don't terminate before the thread // pthread_join( thread_id, NULL); puts("handler assigned"); if (client_sock < 0) perror("accept failed"); exit(exit_success); return 0;

//--------------------------------------- This will handle connection for each client -------------------------------- void *connection_handler(void *SocketCliente) //Get the socket descriptor int sock = *(int*)socketcliente; int read_size; char *message, client_message[20]; // Send some messages to the client message = "Conexao bem sucedida ao servidor : 192.168.1.63:5000 \n"; write(sock, message, strlen(message)); // Receive a message from client // MSG_PEEK : the data is treated as unread, look at the data but don't remove it from the input queue. // if flag argument was zero, you could as well use "read" instead "recv" Gpio Led1,Led2 ; while( (read_size = recv(sock, client_message, 20, MSG_PEEK)) > 0 ) respond(sock,led1,led2) ; if(read_size == 0) puts("client disconnected"); fflush(stdout); else if(read_size == -1) perror("recv failed"); close(sock); return 0; --- // ----------------------------------------------------------- respond()-------------------------------------------------------- --- void respond (int sock,gpio Led1,Gpio Led2) int n; char buffer[256]; int b = sock ; printf("respond sock %d \n", b); // bzero() sets all values in a buffer to zero. // arguments : ( pointer to buffer, size of buffer ) bzero(buffer,256); // read buffer n = read(sock,buffer,255); //if (n < 0) error("erro na leitura de socket\n"); //printf("mensagem recebida: %s",buffer); // LED:1:0 command received //string str = buffer; //sprintf(buffer, str.c_str());; //str.compare("led:1:0"); if ( (buffer[0]== 'L') && (buffer[1]== 'E') && (buffer[2]== 'D') && (buffer[3]== ':') && (buffer[4]== '1') && (buffer[5]== ':') && (buffer[6])=='0' ) printf ("desligar LED 1 \n\n"); Led1.Gpo(138,0); // write(sock,"led 1 DESLIGADO",15); // LED:1:1 command received if ( (buffer[0]== 'L') && (buffer[1]== 'E') && (buffer[2]== 'D') && (buffer[3]== ':') && (buffer[4]== '1') && (buffer[5]== ':') && (buffer[6])=='1' ) printf ("ligar LED 1 \n\n"); Led1.Gpo(138,1); write(sock,"led 1 LIGADO",12); // LED:2:0 command received

if ( (buffer[0]== 'L') && (buffer[1]== 'E') && (buffer[2]== 'D') && (buffer[3]== ':') && (buffer[4]== '2') && (buffer[5]== ':') && (buffer[6])=='0' ) printf ("desligar LED 2 \n\n"); Led2.Gpo(139,0); write(sock,"led 2 DESLIGADO",15); // LED:2:1 command received if ( (buffer[0]== 'L') && (buffer[1]== 'E') && (buffer[2]== 'D') && (buffer[3]== ':') && (buffer[4]== '2') && (buffer[5]== ':') && (buffer[6])=='1' ) printf ("ligar LED 2 \n\n"); Led2.Gpo(139,1); write(sock,"led 2 LIGADO",12); // TEMP command received if ( (buffer[0]=='t') && (buffer[1]=='e') && (buffer[2]=='m') && (buffer[3]=='p') ) // float temp = Temp() ; // sprintf(ch,"%.2f",temp); // printf ("Temperatura %.3f \n\n", temp ); n = write(sock,temp(),10); //n = write(sock,"mensagem recebida\n",18); return; // HR command received if ( (buffer[0]=='h') && (buffer[1]=='r') ) //float hr = HR() ; //sprintf(ch,"%.2f",hr); //printf ("Humidade relativa %.3f \n\n",hr); n = write(sock,hr(),8); //n = write(sock,"mensagem recebida\n",18); return; n = write(sock,"mensagem errada",15); void *GPIO_Input(void *inport) // Class Gpio teve de ser alterada : memory map teve de ser declarada no construtor da class // estava inicialmente definido nos modos Gpi() e Gpo() // caso contrario abria a cada leitura da porta um novo file descriptor // e quando chegasse a 1023 fd abertos, dava erro de "Segmentation Fault". int in = *(int*)inport; int status = 0, old_status = 0 ; Gpio Input ; Gpio Led ; while (1) status = Input.Gpi(137) ; if (status!= old_status) Led.Gpo(138,0); usleep(300000); Led.Gpo(138,1); usleep(300000); Led.Gpo(138,0); old_status = status; usleep(1000);

exit(0); void error(char *msg) perror(msg); exit(1);