COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
udp_listen.cpp File Reference
#include "support/configCosmos.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "agent/agentclass.h"
#include "support/jsonlib.h"
#include "support/convertlib.h"
Include dependency graph for udp_listen.cpp:

Macros

#define BUFSIZE   10000
 

Functions

int main (int argc, char *argv[])
 

Variables

bool debug_verbose = false
 
char address [] = "0.0.0.0"
 
uint16_t port = 6868
 
uint16_t bsize = 10000
 
Agentagent
 

Macro Definition Documentation

#define BUFSIZE   10000

Function Documentation

int main ( int  argc,
char *  argv[] 
)
73 {
74  // **** Declare Variables
75 
76  std::string input;
77  std::string token;
78 
79  /*
80  char gps_time[50];
81  char gps_lat[50];
82  char gps_lon[50];
83  char gps_alt[50];
84  char gps_fix[50];
85  char pressure[50];
86  char temp_ext[50];
87  char temp_batt[50];
88  char temp_arduino[50];
89  char acc_x[50];
90  char acc_y[50];
91  char acc_z[50];
92  */
93  double mjd;
94 
95  char logstring[AGENTMAXBUFFER-20];
96 
97  // Variables: Packet Reception: Data handling
98  socket_channel chan; // Agent UDP Channel Sructure
99  unsigned int fromlen; // UDP receive from "fromlen" parameter
100  int received; // Num bytes received per UDP packet
101  uint8_t buf1[BUFSIZE]; // RX Buffer
102 
103  std::string myjstring;
104  std::string jjstring;
105 
106 
107  uint32_t i, j, tlen;
108 
109  // Variables: Scratchpad
110  int32_t iretn; // Scratch Return value
111 
112  // **** Initialize Variables
113 
114 
115 
116  // Begin Program, Initialize Socket, Blocking, Timeout set for 1 second
117  if ((iretn=socket_open(&chan, NetworkType::UDP, address, port, SOCKET_LISTEN, SOCKET_BLOCKING, 1000000)) < 0)
118  {
119  printf("Err: Unable to open connection to [%s:%d]\n",address,port);
120  } else {
121  printf("Netperf Listen is now listening on port %d...\n\n",port);
122  }
123 
124  agent = new Agent("nps", "udp");
125 
126  // Create default logstring
127  strcpy(logstring,json_of_soh(jjstring, agent->cinfo));
128  j = 0;
129  tlen = strlen(logstring);
130  for (i=1; i<tlen-j; ++i)
131  {
132  if (logstring[i+j] == ':')
133  {
134  logstring[i] = ',';
135  do
136  {
137  ++j;
138  } while (i+j < tlen && logstring[i+j] != '{');
139  }
140  else
141  logstring[i] = logstring[i+j];
142  }
143  logstring[i-1] = '}';
144  logstring[i] = 0;
145 
146  // Begin Main Forever Loop
147  while (1)
148  {
149  // UDP Receive: Check for new packet, return -1 if none
150  received = recvfrom( chan.cudp, (char *)buf1, BUFSIZE, 0, (struct sockaddr*) &chan.caddr, (socklen_t*) &fromlen);
151 
152  if (received < 1)
153  {
154  // do nothing
155  printf("\n");
156  }
157  else
158  {
159 
160  printf(">> %s\n",buf1);
161  // Parse Packet
162  // Fields:
163  // 1 - gpsTime (hh:mm:ss.cc),
164  // 2 - gpsLat (deg x 10-5),
165  // 3 - gpsLong (deg x 10-5),
166  // 4 - gpsAlt (m),
167  // 5 - gpsFix (1,0,-1),
168  // 6 - Pressure (kPa),
169  // 7 - externalTemp (K),
170  // 8 - battTemp (K),
171  // 9 - arduinoTemp (degC),
172  // 10- acc_x,
173  // 11- acc_y,
174  // 12- acc_z (g)
175 
176  //std::string string_input;
177  //string_input = buf1;
178  //char string_input[100];
179  //strcpy(input,string_input);
180  //istringstream ss(string_input);
181 
182  int32_t hour, min, lat, lon, alt, press, etemp, btemp, atemp, fix, sec, hsec;
183  float accx, accy, accz;
184 
185  sscanf((char *)buf1,"%2d%2d%2d%2d %d %d %d %d %d %d %d %d %f %f %f",
186  &hour, &min, &sec, &hsec,
187  &lat, &lon, &alt, &fix,
188  &press, &etemp, &btemp, &atemp,
189  &accx, &accy, &accz);
190  /*
191  sscanf((char *)buf1,"%s %s %s %s %s %s %s %s %s %s %s %s",
192  &gps_time,
193  &gps_lat,
194  &gps_lon,
195  &gps_alt,
196  &gps_fix,
197  &pressure,
198  &temp_ext,
199  &temp_batt,
200  &temp_arduino,
201  &acc_x,
202  &acc_y,
203  &acc_z
204  );
205 
206 
207  printf("GPS Time: %s\n", gps_time);
208  printf("GPS Lat: %s\n", gps_lat);
209  printf("GPS Lon: %s\n", gps_lon);
210  printf("GPS Alt: %s\n", gps_alt);
211  printf("GPS Fix: %s\n", gps_fix);
212  printf("Pressure: %s\n", pressure);
213  printf("Temp Ext: %s\n", temp_ext);
214  printf("Temp Batt: %s\n", temp_batt);
215  printf("Temp Ard.: %s\n", temp_arduino);
216  printf("Acc x: %s\n", acc_x);
217  printf("Acc y: %s\n", acc_y);
218  printf("Acc z: %s\n", acc_z);
219  */
220 
221  // IMU
222  agent->cinfo->device[agent->cinfo->devspec.imu[0]].imu.accel.col[0] = accx;
223  agent->cinfo->node.loc.pos.geod.a.lon = (accx/REARTHM)/cos(agent->cinfo->node.loc.pos.geod.s.lat);
224  agent->cinfo->device[agent->cinfo->devspec.imu[0]].imu.accel.col[1] = accy;
225  agent->cinfo->node.loc.pos.geod.a.lat = (accy/REARTHM);
226  agent->cinfo->device[agent->cinfo->devspec.imu[0]].imu.accel.col[2] = accz;
227  agent->cinfo->node.loc.pos.geod.a.h = accz;
228 
229  // Pressure sensor
230  agent->cinfo->device[agent->cinfo->devspec.psen[0]].psen.press = press / 1000.;
231 
232  // Temperatures
233  agent->cinfo->device[agent->cinfo->devspec.tsen[0]].temp = etemp;
234  agent->cinfo->device[agent->cinfo->devspec.tsen[1]].temp = btemp;
235  agent->cinfo->device[agent->cinfo->devspec.tsen[2]].temp = atemp + 273.15;
236 
237  // GPS
238  if (fix == 1)
239  {
240  mjd = (int)currentmjd(0.) + hour / 24. + min / 1400. + sec / 86400. + hsec / 8640000.;;
242  agent->cinfo->device[agent->cinfo->devspec.gps[0]].gps.geocs = agent->cinfo->node.loc.pos.geoc.s;
243  agent->cinfo->node.loc.pos.geod.s.lat = RADOF(lat / 1.e5);
244  agent->cinfo->node.loc.pos.geod.s.lon = RADOF(lon / 1.e5);
245  agent->cinfo->node.loc.pos.geod.s.h = alt;
246  agent->cinfo->device[agent->cinfo->devspec.gps[0]].gps.geocv = rv_zero();
247 
248  agent->cinfo->node.loc.pos.geod.v.lat = 0.;
249  agent->cinfo->node.loc.pos.geod.v.lon = 0.;
250  agent->cinfo->node.loc.pos.geod.v.h = 0.;
251  agent->cinfo->node.loc.att.geoc.s = q_eye();
254 
257  }
258 
259  // Broadcast it
260  agent->post(Agent::AgentMessage::SOH, json_of_list(myjstring, logstring, agent->cinfo));
262 
263  } // End If: packet reception / parse / idle cycle
264 
265  } // End Main Forever Loop
266 
267 } // End Main
char logstring[AGENTMAXBUFFER-20]
Definition: agent_physics.cpp:49
qatt geoc
Definition: convertdef.h:828
Agent socket using Unicast UDP.
double utc
Definition: convertdef.h:261
Agent * agent
Definition: udp_listen.cpp:68
vector< uint16_t > gps
Definition: jsondef.h:3902
int i
Definition: rw_test.cpp:37
rvector a
2nd derivative: Alpha - acceleration
Definition: convertdef.h:483
int iretn
Definition: rw_test.cpp:37
uint16_t port
Definition: udp_listen.cpp:65
cartpos geoc
Definition: convertdef.h:739
vector< uint16_t > imu
Definition: jsondef.h:3904
double utc
Master time for location, in Modified Julian Day.
Definition: convertdef.h:879
#define BUFSIZE
Definition: udp_listen.cpp:61
rvector v
1st derivative: Omega - angular velocity
Definition: convertdef.h:481
vector< devicestruc > device
Vector of all general (common) information for devices (components) in node.
Definition: jsondef.h:4238
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
rvector s
Location.
Definition: convertdef.h:163
int32_t post(messstruc mess)
Post a Cosmos::Agent::messstruc.
Definition: agentclass.cpp:2074
std::string myjstring
Definition: agent_node.cpp:53
attstruc att
attstruc for this time.
Definition: convertdef.h:883
int32_t cudp
Definition: socketlib.h:120
gvector a
Acceleration vector.
Definition: convertdef.h:267
char name[40+1]
Node Name.
Definition: jsondef.h:3556
Definition: agentclass.h:139
char address[]
Definition: udp_listen.cpp:64
Definition: socketlib.h:115
locstruc loc
Location structure.
Definition: jsondef.h:3596
struct sockaddr_in caddr
Definition: socketlib.h:122
#define SOCKET_BLOCKING
Blocking Agent.
Definition: socketlib.h:78
double h
Height in meters.
Definition: vector.h:229
double mjd
Definition: udp_send.cpp:41
double lon
Longitude in radians.
Definition: vector.h:227
vector< uint16_t > tsen
Definition: jsondef.h:3924
int32_t pos_geod(locstruc *loc)
Set Geodetic position.
Definition: convertlib.cpp:576
rvector rv_zero()
Zero row order vector.
Definition: vector.cpp:107
static string jjstring
Definition: agent_exec-2-0.cpp:106
const char * json_of_soh(string &jstring, cosmosstruc *cinfo)
Definition: jsonlib.cpp:9828
void log_write(string node, string agent, double utc, string extra, string type, string record, string location)
Write log entry - full.
Definition: datalib.cpp:75
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
posstruc pos
posstruc for this time.
Definition: convertdef.h:881
#define REARTHM
SI Radius of Earth.
Definition: convertdef.h:60
gvector s
Position vector.
Definition: convertdef.h:263
double lat
Latitude in radians.
Definition: vector.h:225
#define DATA_LOG_TYPE_SOH
Definition: datalib.h:108
const char * json_of_list(string &jstring, string list, cosmosstruc *cinfo)
Create JSON stream from list.
Definition: jsonlib.cpp:8714
#define SOCKET_LISTEN
Listen followed by optional talk (recvfrom INADDRANY)
Definition: socketlib.h:84
quaternion s
0th derivative: Quaternion
Definition: convertdef.h:479
cosmosstruc * cinfo
Definition: agentclass.h:346
#define AGENTMAXBUFFER
Maximum AGENT transfer buffer size.
Definition: jsondef.h:438
quaternion q_eye()
Identity quaternion.
Definition: vector.cpp:1310
geoidpos geod
Definition: convertdef.h:741
devspecstruc devspec
Structure for devices (components) special data in node, by type.
Definition: jsondef.h:4241
uint32_t pass
pass indicator: allows synchronization with other attitude and position values.
Definition: convertdef.h:269
int32_t socket_open(socket_channel *channel, NetworkType ntype, const char *address, uint16_t port, uint16_t role, bool blocking, uint32_t usectimeo, uint32_t rcvbuf, uint32_t sndbuf)
Open UDP socket.
Definition: socketlib.cpp:51
vector< uint16_t > psen
Definition: jsondef.h:3910
gvector v
Velocity vector.
Definition: convertdef.h:265
#define RADOF(deg)
Radians of a Degree value.
Definition: math/constants.h:29

Variable Documentation

bool debug_verbose = false
char address[] = "0.0.0.0"
uint16_t port = 6868
uint16_t bsize = 10000
Agent* agent