2
aoslsi.c - LTP over AOS Link Service Adapter, input
6
/* 7/6/2010, copied from udplsi, as per issue 101-LTP-over-AOS-via-UDP
7
Greg Menke, Raytheon, under contract METS-MR-679-0909 with NASA GSFC */
12
static void interruptThread()
14
isignal(SIGTERM, interruptThread);
15
ionKillMainThread("aoslsi");
18
/* * * Receiver thread functions * * */
24
} ReceiverThreadParms;
26
static void *handleDatagrams(void *parm)
28
/* Main loop for AOS datagram reception and handling. */
30
ReceiverThreadParms *rtp = (ReceiverThreadParms *) parm;
31
char *procName = "aoslsi";
34
struct sockaddr_in fromAddr;
35
unsigned int fromSize;
37
snooze(1); /* Let main thread become interruptible. */
38
buffer = MTAKE(AOSLSA_BUFSZ);
41
putErrmsg("aoslsi can't get AOS buffer.", NULL);
42
ionKillMainThread(procName);
46
/* Can now start receiving bundles. On failure, take
51
fromSize = sizeof fromAddr;
52
segmentLength = irecvfrom(rtp->linkSocket, buffer, AOSLSA_BUFSZ,
53
0, (struct sockaddr *) &fromAddr, &fromSize);
54
switch (segmentLength)
57
putSysErrmsg("Can't acquire segment", NULL);
58
ionKillMainThread(procName);
60
/* Intentional fall-through to next case. */
62
case 1: /* Normal stop. */
67
if (ltpHandleInboundSegment(buffer, segmentLength) < 0)
69
putErrmsg("Can't handle inbound segment.", NULL);
70
ionKillMainThread(procName);
75
/* Make sure other tasks have a chance to run. */
81
writeMemo("[i] aoslsi receiver thread has ended.");
89
/* * * Main thread functions * * * */
91
#if defined (VXWORKS) || defined (RTEMS)
92
int aoslsi(int a1, int a2, int a3, int a4, int a5,
93
int a6, int a7, int a8, int a9, int a10)
95
char *endpointSpec = (char *) a1;
97
int main(int argc, char *argv[])
99
char *endpointSpec = (argc > 1 ? argv[1] : NULL);
102
unsigned short portNbr = 0;
103
unsigned int ipAddress = 0;
104
struct sockaddr socketName;
105
struct sockaddr_in *inetName;
106
ReceiverThreadParms rtp;
107
unsigned int nameLength;
108
pthread_t receiverThread;
112
/* Note that ltpadmin must be run before the first
113
* invocation of ltplsi, to initialize the LTP database
114
* (as necessary) and dynamic database. */
116
if (ltpInit(0, 0) < 0)
118
putErrmsg("aoslsi can't initialize LTP.", NULL);
123
if (vdb->lsiPid != ERROR && vdb->lsiPid != sm_TaskIdSelf())
125
putErrmsg("LSI task is already started.", itoa(vdb->lsiPid));
129
/* All command-line arguments are now validated. */
133
if(parseSocketSpec(endpointSpec, &portNbr, &ipAddress) != 0)
135
putErrmsg("Can't get IP/port for endpointSpec.", endpointSpec);
141
portNbr = LtpAosDefaultPortNbr;
143
portNbr = htons(portNbr);
144
ipAddress = htonl(ipAddress);
146
memset((char *) &socketName, 0, sizeof socketName);
147
inetName = (struct sockaddr_in *) &socketName;
148
inetName->sin_family = AF_INET;
149
inetName->sin_port = portNbr;
150
memcpy((char *) &(inetName->sin_addr.s_addr), (char *) &ipAddress, 4);
151
rtp.linkSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
152
if (rtp.linkSocket < 0)
154
putSysErrmsg("LSI can't open AOS socket", NULL);
158
nameLength = sizeof(struct sockaddr);
159
if (reUseAddress(rtp.linkSocket)
160
|| bind(rtp.linkSocket, &socketName, nameLength) < 0
161
|| getsockname(rtp.linkSocket, &socketName, &nameLength) < 0)
163
closesocket(rtp.linkSocket);
164
putSysErrmsg("Can't initialize socket", NULL);
168
/* Set up signal handling; SIGTERM is shutdown signal. */
170
ionNoteMainThread("aoslsi");
171
isignal(SIGTERM, interruptThread);
173
/* Start the receiver thread. */
176
if (pthread_create(&receiverThread, NULL, handleDatagrams, &rtp))
178
closesocket(rtp.linkSocket);
179
putSysErrmsg("aoslsi can't create receiver thread", NULL);
183
/* Now sleep until interrupted by SIGTERM, at which point
184
* it's time to stop the link service. */
189
isprintf(txt, sizeof(txt),
190
"[i] aoslsi is running, spec=[%s:%d].",
191
inet_ntoa(inetName->sin_addr), ntohs(portNbr));
195
ionPauseMainThread(-1);
197
/* Time to shut down. */
201
/* Wake up the receiver thread by sending it a 1-byte
204
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
207
isendto(fd, &quit, 1, 0, &socketName, sizeof(struct sockaddr));
211
pthread_join(receiverThread, NULL);
212
closesocket(rtp.linkSocket);
214
writeMemo("[i] aoslsi duct has ended.");