CORDET Framework - C2 Implementation
pustests/CrFwOutStreamSocket.c
1 
24 #include <stdlib.h>
25 #include "CrFwRepErrStub.h"
26 #include "CrFwOutStreamSocket.h"
27 /* Include FW Profile files */
28 #include "FwSmConstants.h"
29 #include "FwSmConfig.h"
30 #include "FwSmCore.h"
31 #include "FwPrConfig.h"
32 #include "FwPrCore.h"
33 #include "FwPrConstants.h"
34 /* Include configuration files */
35 #include "CrFwOutStreamUserPar.h"
36 #include "CrFwCmpData.h"
37 /* Include framework files */
39 #include "BaseCmp/CrFwBaseCmp.h"
40 #include "Pckt/CrFwPckt.h"
41 #include "CrFwTime.h"
42 #include "CrFwRepErr.h"
44 /* Include file for socket implementation */
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <netdb.h>
53 #include <fcntl.h>
54 #include <errno.h>
55 #include <pthread.h>
56 #include <strings.h>
57 
59 static unsigned short portno = 0;
60 
62 static int sockfd;
63 
65 static int newsockfd;
66 
68 static struct sockaddr_in cli_addr;
69 
71 static socklen_t clilen;
72 
77 static void* acceptThreadEntry(void* ptr);
78 
79 /* ---------------------------------------------------------------------------------------------*/
80 void CrFwOutStreamSocketInitAction(FwPrDesc_t prDesc) {
81  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
82  struct sockaddr_in serv_addr, cli_addr;
83  pthread_t acceptThread;
84  pthread_attr_t attr;
85 
86  /* Create the socket */
87  sockfd = socket(AF_INET, SOCK_STREAM, 0);
88  if (sockfd < 0) {
89  perror("CrFwInStreamSocketInitAction, Socket creation");
90  outStreamData->outcome = 0;
91  return;
92  }
93 
94  bzero((char*) &serv_addr, sizeof(serv_addr));
95  serv_addr.sin_family = AF_INET;
96  serv_addr.sin_addr.s_addr = INADDR_ANY;
97  serv_addr.sin_port = htons(portno);
98  if (bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) {
99  perror("CrFwOutStreamSocketInitAction, Bind Socket");
100  outStreamData->outcome = 0;
101  return;
102  }
103  listen(sockfd,5);
104  clilen = sizeof(cli_addr);
105 
106  /* Create thread which will accept the connection call from the InStream */
107  pthread_attr_init(&attr);
108  if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
109  perror("CrFwOutStreamSocketInitAction, set detached state");
110  outStreamData->outcome = 0;
111  return;
112  }
113  if (pthread_create(&acceptThread, &attr, acceptThreadEntry, NULL) < 0) {
114  outStreamData->outcome = 0;
115  return;
116  };
117 
118  /* Execute default initialization action for OutStream */
120 }
121 
122 /* ---------------------------------------------------------------------------------------------*/
123 void CrFwOutStreamSocketShutdownAction(FwSmDesc_t smDesc) {
125  close(newsockfd);
126  close(sockfd);
127 }
128 
129 /* ---------------------------------------------------------------------------------------------*/
131  unsigned int len = (int)CrFwPcktGetLength(pckt);
132  long int n;
133 
134  n = write(newsockfd, pckt, len);
135 
136  if (n < 0)
137  return 0;
138 
139  if (n != (int)CrFwPcktGetLength(pckt)) {
140  printf("CrFwOutStreamSocketInitAction: error writing to socket\n");
141  return 0;
142  }
143 
144  return 1;
145 }
146 
147 /* ---------------------------------------------------------------------------------------------*/
148 static void* acceptThreadEntry(void* ptr) {
149  (void)(ptr);
150 
151  /* The call to accept blocks until a matching connect is done by the InStream */
152  newsockfd = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen);
153  if (newsockfd < 0) {
154  perror("CrFwInStreamSocketInitAction, Socket Accept");
155  }
156  return NULL;
157 }
158 
159 /* ---------------------------------------------------------------------------------------------*/
160 void CrFwOutStreamSocketInitCheck(FwPrDesc_t prDesc) {
161  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
162 
163  if (portno < 2000)
164  outStreamData->outcome = 0;
165  else
166  outStreamData->outcome = 1;
167 
168  return;
169 }
170 
171 /* ---------------------------------------------------------------------------------------------*/
172 void CrFwOutStreamSocketConfigCheck(FwPrDesc_t prDesc) {
173  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
174 
175  if (newsockfd > 0)
176  outStreamData->outcome = 1;
177  else
178  outStreamData->outcome = 0;
179 
180  return;
181 }
182 
183 /* ---------------------------------------------------------------------------------------------*/
184 void CrFwOutStreamSocketSetPort(unsigned short n) {
185  portno = n;
186 }
187 
Type for the Framework Component Data (FCD).
Interface through which framework components access the current time.
CrFwOutcome_t outcome
The outcome of an action or check executed by a state machine or by one of its procedures.
static int portno
The port number.
Definition of the OutStream component.
Interface for creating and accessing a report or command packet.
int CrFwBool_t
Type used for boolean values (1 represent "true" and 0 represents "false").
Definition: CrFwConstants.h:30
void CrFwOutStreamSocketInitAction(FwPrDesc_t prDesc)
Initialization action for the socket-based OutStream.
Interface for reporting an error detected by a framework component.
CrFwPcktLength_t CrFwPcktGetLength(CrFwPckt_t pckt)
Return the length (in number of bytes) of a packet.
static int newsockfd[2]
The file descriptors for the client sockets.
Definition of the utility functions for the CORDET Framework.
void CrFwOutStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an OutStream.
CrFwBool_t CrFwOutStreamSocketPcktHandover(CrFwPckt_t pckt)
Function implementing the hand-over operation for the socket-based OutStream.
void CrFwOutStreamDefInitAction(FwPrDesc_t prDesc)
Default initialization action for an OutStream.
static int sockfd
The file descriptor for the socket.
static socklen_t clilen
Socket variable.
void CrFwOutStreamSocketSetPort(unsigned short n)
Set the port number for the socket.
Definition of Base Component.
void CrFwOutStreamSocketShutdownAction(FwSmDesc_t smDesc)
Shutdown action for the socket-based OutStream.
static CrFwCmpData_t outStreamData[CR_FW_NOF_OUTSTREAM]
The base data structures for the OutStream State Machines and their Procedures.
Definition: CrFwOutStream.c:80
char * CrFwPckt_t
Type for packets (see CrFwPckt.h).
Definition: CrFwConstants.h:36
static void * acceptThreadEntry(void *ptr)
Entry point for the thread which waits for the incoming connection from the client socket...
static struct sockaddr_in cli_addr
Socket variable.
void CrFwOutStreamSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the socket-based OutStream.
void CrFwOutStreamSocketConfigCheck(FwPrDesc_t prDesc)
Configuration check for the socket-based OutStream.
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved