COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
agent_arduino.cpp File Reference
#include "support/configCosmos.h"
#include <stdio.h>
#include "agent/agentclass.h"
#include "device/cpu/devicecpu.h"
#include "device/general/cssl_lib.h"
Include dependency graph for agent_arduino.cpp:

Macros

#define MAXBUFFERSIZE   2560
 
#define REQUEST_RUN_PROGRAM   0
 

Functions

int32_t request_run_program (string &request, string &response, Agent *)
 
int main (int argc, char *argv[])
 

Variables

std::string agentname = "arduino"
 
std::string node = "null"
 
std::string comport = "//./"
 
int waitsec = 5
 
Agentagent
 
int32_t stateflag
 
int32_t myport
 
uint8_t buffer [400]
 

Macro Definition Documentation

#define MAXBUFFERSIZE   2560
#define REQUEST_RUN_PROGRAM   0

Function Documentation

int32_t request_run_program ( string &  request,
string &  response,
Agent  
)
125 {
126  int i;
127  int32_t iretn = 0;
128  FILE *pd;
129  bool flag;
130 
131  // Run Program
132  flag = false;
133  for (i=0; i<AGENTMAXBUFFER-1; i++)
134  {
135  if (flag)
136  {
137  if (request[i] != ' ')
138  break;
139  }
140  else
141  {
142  if (request[i] == ' ')
143  flag = true;
144  }
145  }
146 
147  if (i == AGENTMAXBUFFER-1)
148  {
149  response = "unmatched";
150  }
151  else
152  {
153  // open process and read response
154 #ifdef COSMOS_WIN_BUILD_MSVC
155  if ((pd=_popen(&request[i], "r")) != NULL)
156 #else
157  if ((pd=popen(&request[i],"r")) != NULL)
158 #endif
159  {
160  response.resize(AGENTMAXBUFFER);
161  iretn = fread(&response[0],1,AGENTMAXBUFFER-1,pd);
162  response[iretn] = 0;
163  iretn = 1;
164 #ifdef COSMOS_WIN_BUILD_MSVC
165  _pclose(pd);
166 #else
167  pclose(pd); // close process
168 #endif
169  }
170  else
171  {
172  response.clear();
173  iretn = 0;
174  }
175  }
176 
177  return iretn;
178 }
double pd[9][150]
Definition: nrlmsise-00_data.cpp:88
int i
Definition: rw_test.cpp:37
int iretn
Definition: rw_test.cpp:37
#define AGENTMAXBUFFER
Maximum AGENT transfer buffer size.
Definition: jsondef.h:438
int main ( int  argc,
char *  argv[] 
)
61 {
62  int32_t iretn;
63 
64  if (argc == 3)
65  {
66  node = argv[1];
67  comport += argv[2];
68  }
69  else
70  {
71  printf("Usage: arduino_agent node comport\n");
72  exit (1);
73  }
74 
75  // Initialize the Agent
76  if (!(agent = new Agent(node, agentname, 1., MAXBUFFERSIZE, true)))
77  {
78  printf("Error starting server\n");
80  }
81 
82  // Add additional requests
83  if ((iretn=agent->add_request("runprogram", request_run_program)))
84  exit (iretn);
85 
86  // Setup Heartbeat information
87  char arduino_soh[2000] = "{\"device_imu_utc_000\",\"device_imu_accel_000\",\"device_imu_omega_000\",\"device_imu_mag_000\",\"device_imu_temp_000\"}";
88  agent->set_sohstring(arduino_soh);
89 
90  // Start main thread
91  double nmjd;
92 // int32_t sleept;
93 
94  // Initialize connection to arduino
95  cssl_start();
96  cssl_t *serial = cssl_open(comport.c_str(), 115200, 8, 0, 1);
97  cssl_settimeout(serial, 0, .1);
98 
99  // Start performing the body of the agent
100  nmjd = currentmjd(0.);
101  uint32_t counter = 0;
102  while(agent->running())
103  {
104  // Set beginning of next cycle;
105  nmjd += agent->cinfo->agent[0].aprd/86400.;
106 
107  // Gather arduino data
108  iretn = cssl_getdata(serial, buffer, 400);
109  if (iretn > 0)
110  {
111  std::string message = (char *)buffer;
112  json_parse(message, agent->cinfo);
113  printf("%u: %s\n", ++counter, buffer);
114  }
115 
116 // sleept = (int32_t)((nmjd - currentmjd(0.))*86400000000.);
117 // if (sleept < 0) sleept = 0;
118 // COSMOS_USLEEP(sleept);
119  }
120  return 0;
121 }
int32_t request_run_program(string &request, string &response, Agent *)
Definition: agent_arduino.cpp:124
int32_t json_parse(string jstring, cosmosstruc *cinfo)
Parse JSON using Name Space.
Definition: jsonlib.cpp:4799
int iretn
Definition: rw_test.cpp:37
int32_t set_sohstring(string list)
Set Limited SOH string.
Definition: agentclass.cpp:641
int32_t cssl_start()
Definition: cssl_lib.cpp:73
uint16_t running()
Check if we&#39;re supposed to be running.
Definition: agentclass.cpp:391
int32_t add_request(string token, external_request_function function, string synopsis="", string description="")
Add internal request to Agent request list with description and synopsis.
Definition: agentclass.cpp:312
uint8_t message[300]
Definition: kpc9612p_send.cpp:36
Definition: agentclass.h:139
std::string agentname
Definition: agent_arduino.cpp:42
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
Agent * agent
Definition: agent_arduino.cpp:48
vector< agentstruc > agent
Single entry vector for agent information.
Definition: jsondef.h:4247
cssl_t * cssl_open(const char *fname, int baud, int bits, int parity, int stop)
Definition: cssl_lib.cpp:108
cosmosstruc * cinfo
Definition: agentclass.h:346
uint8_t buffer[400]
Definition: agent_arduino.cpp:58
#define AGENT_ERROR_JSON_CREATE
Definition: cosmos-errno.h:105
int32_t cssl_getdata(cssl_t *serial, uint8_t *buffer, int size)
Definition: cssl_lib.cpp:738
std::string comport
Definition: agent_arduino.cpp:44
#define MAXBUFFERSIZE
Definition: agent_arduino.cpp:50
int32_t cssl_settimeout(cssl_t *serial, int, double timeout)
Set read timeout.
Definition: cssl_lib.cpp:469
cssl_t * serial
Definition: arduino_lib.cpp:63
Definition: cssl_lib.h:34
std::string node
Definition: agent_arduino.cpp:43

Variable Documentation

std::string agentname = "arduino"
std::string node = "null"
std::string comport = "//./"
int waitsec = 5
Agent* agent
int32_t stateflag
int32_t myport
uint8_t buffer[400]