CORDET Framework - C2 Implementation
CrDemoSlave2/CrDaClientSocket.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 #include "CrDaClientSocket.h"
21 #include "CrFwConstants.h"
22 /* Include FW Profile files */
23 #include "FwSmConstants.h"
24 #include "FwSmConfig.h"
25 #include "FwSmCore.h"
26 #include "FwPrConfig.h"
27 #include "FwPrCore.h"
28 #include "FwPrConstants.h"
29 /* Include configuration files */
30 #include "CrFwInStreamUserPar.h"
31 #include "CrFwCmpData.h"
32 /* Include framework files */
33 #include "InStream/CrFwInStream.h"
34 #include "BaseCmp/CrFwBaseCmp.h"
35 #include "Pckt/CrFwPckt.h"
36 #include "CrFwTime.h"
37 #include "CrFwRepErr.h"
39 /* Include file for socket implementation */
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <netdb.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 
52 static int portno = 0;
53 
55 static char* hostName = NULL;
56 
58 static int sockfd = 0;
59 
61 static int pcktMaxLength;
62 
64 static unsigned char* readBuffer;
65 
66 /* ---------------------------------------------------------------------------------------------*/
67 void CrDaClientSocketInitAction(FwPrDesc_t prDesc) {
68  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
69  struct sockaddr_in serv_addr;
70  struct hostent* server;
71  int flags;
72 
73  if (sockfd != 0) { /* Check if socket is already initialized */
74  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
76  else
78  return;
79  }
80 
81  /* Create the read buffer */
83  readBuffer = malloc(pcktMaxLength*sizeof(char));
84 
85  sockfd = socket(AF_INET, SOCK_STREAM, 0);
86  if (sockfd < 0) {
87  perror("CrDaClientSocketInitAction, Socket Creation");
88  streamData->outcome = 0;
89  return;
90  }
91 
92  /* Set the socket to non-blocking mode */
93  if ((flags = fcntl(sockfd, F_GETFL, 0)) < 0) {
94  perror("CrDaClientSocketInitAction, Set socket attributes");
95  streamData->outcome = 0;
96  return;
97  }
98  if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
99  perror("CrDaClientSocketInitAction, Set socket attributes");
100  streamData->outcome = 0;
101  return;
102  }
103 
104  server = gethostbyname(hostName);
105  if (server == NULL) {
106  perror("CrDaClientSocketInitAction, Get host name");
107  streamData->outcome = 0;
108  return;
109  }
110 
111  bzero((char*) &serv_addr, sizeof(serv_addr));
112  serv_addr.sin_family = AF_INET;
113  bcopy((char*)server->h_addr,
114  (char*)&serv_addr.sin_addr.s_addr,
115  server->h_length);
116  serv_addr.sin_port = htons(portno);
117 
118  if (connect(sockfd,(struct sockaddr*) &serv_addr,sizeof(serv_addr)) < 0) {
119  if (errno != EINPROGRESS) {
120  perror("CrDaClientSocketInitAction, Connect Socket");
121  streamData->outcome = 0;
122  return;
123  }
124  }
125 
126  /* Execute default initialization action for OutStream/InStream */
127  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
129  else
131 }
132 
133 /* ---------------------------------------------------------------------------------------------*/
134 void CrDaClientSocketShutdownAction(FwSmDesc_t smDesc) {
135  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwSmGetData(smDesc);
136 
137  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
139  else
141 
142  if (sockfd == 0) /* Check if socket was already shutdown */
143  return;
144  free(readBuffer);
145  close(sockfd);
146  sockfd = 0;
147 }
148 
149 /* ---------------------------------------------------------------------------------------------*/
150 void CrDaClientSocketInitCheck(FwPrDesc_t prDesc) {
151  CrFwCmpData_t* prData = (CrFwCmpData_t*)FwPrGetData(prDesc);
152 
153  if (pcktMaxLength > 255) {
154  prData->outcome = 0;
155  return;
156  }
157 
158  if (portno == 0) {
159  prData->outcome = 0;
160  return;
161  }
162 
163  if (hostName == NULL) {
164  prData->outcome = 0;
165  return;
166  }
167 
168  prData->outcome = 1;
169  return;
170 }
171 
172 /* ---------------------------------------------------------------------------------------------*/
173 void CrDaClientSocketConfigAction(FwPrDesc_t prDesc) {
174  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
175 
176  /* Clear Read Buffer */
177  readBuffer[0] = 0;
178 
179  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
181  else if (streamData->typeId == CR_FW_OUTSTREAM_TYPE)
183  else {
184  perror("CrDaClientSocketConfigAction, Incorrect caller type");
185  return;
186  }
187 }
188 
189 /* ---------------------------------------------------------------------------------------------*/
191  int n;
192  FwSmDesc_t inStream;
193  CrFwDestSrc_t src;
194 
195  if (readBuffer[0] != 0) {
197  inStream = CrFwInStreamGet(src);
198  CrFwInStreamPcktAvail(inStream);
199  return;
200  }
201 
202  n = read(sockfd, readBuffer, pcktMaxLength);
203  if (n == -1) /* no data are available from the socket */
204  return;
205  if (n == 0) {
206  printf("CrDaClientSocketPoll: ERROR reading from socket\n");
207  return;
208  }
209  if (n == readBuffer[0]) { /* a valid packet has arrived */
211  inStream = CrFwInStreamGet(src);
212  CrFwInStreamPcktAvail(inStream);
213  return;
214  }
215  if (n != readBuffer[0]) {
216  printf("CrDaClientSocketPoll: invalid packet received from socket\n");
217  readBuffer[0] = 0;
218  return;
219  }
220 }
221 
222 /* ---------------------------------------------------------------------------------------------*/
224  CrFwPckt_t pckt;
225  CrFwDestSrc_t pcktSrc;
226 
227  if (readBuffer[0] != 0) {
229  if (src == pcktSrc) {
230  pckt = CrFwPcktMake((CrFwPcktLength_t)readBuffer[0]);
231  memcpy(pckt, readBuffer, readBuffer[0]);
232  readBuffer[0] = 0;
233  return pckt;
234  } else
235  return NULL;
236  } else
237  return NULL;
238 }
239 
240 /* ---------------------------------------------------------------------------------------------*/
242  int n;
243  CrFwDestSrc_t pcktSrc;
244 
245  if (readBuffer[0] != 0) {
246  return 1;
247  }
248 
249  n = read(sockfd, readBuffer, pcktMaxLength);
250  if (n == -1) /* no data are available from the socket */
251  return 0;
252 
253  if (n == 0) {
254  printf("CrDaClientSocketIsPcktAvail: ERROR reading from socket\n");
255  return 0;
256  }
257  if (n == readBuffer[0]) { /* a valid packet has arrived */
259  if (src == pcktSrc)
260  return 1;
261  else
262  return 0;
263  }
264 
265  if (n != readBuffer[0]) {
266  printf("CrDaClientSocketIsPcktAvail: invalid packet received from socket\n");
267  readBuffer[0] = 0;
268  return 0;
269  }
270 
271  return 0;
272 }
273 
274 /* ---------------------------------------------------------------------------------------------*/
276  int len = (int)CrFwPcktGetLength(pckt);
277  int n;
278 
279  n = write(sockfd, pckt, len);
280 
281  if (n < 0)
282  return 0;
283 
284  if (n != (int)CrFwPcktGetLength(pckt)) {
285  printf("CrDaClientSocketPcktHandover: error writing to socket\n");
286  return 0;
287  }
288 
289  return 1;
290 }
291 
292 /* ---------------------------------------------------------------------------------------------*/
294  portno = n;
295 }
296 
297 /* ---------------------------------------------------------------------------------------------*/
298 void CrDaClientSocketSetHost(char* name) {
299  hostName = name;
300 }
void CrFwOutStreamDefConfigAction(FwPrDesc_t prDesc)
Default configuration action for an OutStream.
void CrDaClientSocketConfigAction(FwPrDesc_t prDesc)
Configuration action for the client socket.
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.
CrFwDestSrc_t CrFwPcktGetSrc(CrFwPckt_t pckt)
Return the source of the command or report encapsulated in a packet.
void CrDaClientSocketSetPort(int n)
Set the port number for the socket.
unsigned char * CrFwPckt_t
Type for packets (see CrFwPckt.h).
Definition: CrFwConstants.h:38
CrFwBool_t CrDaClientSocketIsPcktAvail(CrFwDestSrc_t src)
Function implementing the Packet Available Check Operation for the client socket. ...
unsigned char CrFwDestSrc_t
Type used for the command or report destination and source.
CrFwPckt_t CrFwPcktMake(CrFwPcktLength_t pcktLength)
Make function for command or report packets.
#define CR_FW_INSTREAM_TYPE
Type identifier for the InStream components.
Interface for creating and accessing a report or command packet.
Definition of the InStream component.
void CrDaClientSocketInitAction(FwPrDesc_t prDesc)
Initialization action for the client socket.
int CrFwBool_t
Type used for boolean values (1 represent "true" and 0 represents "false").
Definition: CrFwConstants.h:32
Header file to define all invariant publicly available constants and types for the CORDET Framework...
static int pcktMaxLength
The maximum size of an incoming packet.
void CrDaClientSocketShutdownAction(FwSmDesc_t smDesc)
Shutdown action for the client socket.
Interface for reporting an error detected by a framework component.
static char * hostName
The host name.
void CrFwInStreamDefInitAction(FwPrDesc_t prDesc)
Default initialization action for an InStream.
Definition: CrFwInStream.c:312
CrFwPcktLength_t CrFwPcktGetLength(CrFwPckt_t pckt)
Return the length (in number of bytes) of a packet.
CrFwPckt_t CrDaClientSocketPcktCollect(CrFwDestSrc_t src)
Function implementing the Packet Collect Operation for the client socket.
static unsigned char * readBuffer
The Read Buffer.
Interface for the client socket used in the CORDET Demo.
CrFwBool_t CrDaClientSocketPcktHandover(CrFwPckt_t pckt)
Function implementing the hand-over operation for the client socket.
unsigned short int CrFwPcktLength_t
Type for the packet length.
CrFwPcktLength_t CrFwPcktGetMaxLength()
Return the maximum length of a packet in number of bytes.
Definition of the utility functions for the CORDET Framework.
void CrFwOutStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an OutStream.
void CrFwOutStreamDefInitAction(FwPrDesc_t prDesc)
Default initialization action for an OutStream.
void CrFwInStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an InStream.
Definition: CrFwInStream.c:301
static int sockfd
The file descriptor for the socket.
FwSmDesc_t CrFwInStreamGet(CrFwDestSrc_t src)
Getter function for the InStream corresponding to the argument source.
Definition: CrFwInStream.c:211
Definition of Base Component.
void CrDaClientSocketSetHost(char *name)
Set the host name of the server.
void CrDaClientSocketPoll()
Poll the client socket to check whether a new packet has arrived.
CrFwTypeId_t typeId
The type identifier of the framework component.
void CrFwInStreamDefConfigAction(FwPrDesc_t prDesc)
Default configuration action for an InStream.
Definition: CrFwInStream.c:288
void CrFwInStreamPcktAvail(FwSmDesc_t smDesc)
Query the middleware for available packets and collect them if they are available.
Definition: CrFwInStream.c:241
#define CR_FW_OUTSTREAM_TYPE
Type identifier for the OutStream components.
static int portno
The port number.
void CrDaClientSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the client socket.
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved