COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
Add a new device

As an example we are going to add a new generic device to measure the temperature named "temperatureStation". Go to jsondef.h approx in line 960 and create the structure that contains the information you want to use.

1 struct temperatureStationStruc
2 {
3  //! Generic info must be here for every device
4  genstruc gen;
5  //! the following is any data specific to this device
6  float temperature; // your temperature data will be stored here
7 } ;

add your temperatureStationStruc structure to the devicestruc union (apporox in line 1400)

1 typedef struct
2 {
3  union
4  {
5  allstruc all;
6  ...
7  thststruc thst;
8  tsenstruc tsen;
9  temperatureStationStruc temperatureStation; // << --- add here
10  };
11 } devicestruc;

add your temperatureStationStruc structure to the devspecstruc structure (apporox in line 1500)

1 typedef struct
2 {
3  uint16_t ant_cnt;
4  ...
5  uint16_t thst_cnt;
6  uint16_t tsen_cnt;
7  uint16_t temperatureStation_cnt; // << --- add here
8  vector<allstruc *>all;
9  ...
10  vector<thststruc *>thst;
11  vector<tsenstruc *>tsen;
12  vector<temperatureStationStruc *>temperatureStation; // << --- add here
13 } devspecstruc;

now go to jsonlib.cpp , add your temperatureStation to the end of device_type_string

1 vector <string> device_type_string
2 {
3  "pload",
4  ...
5  "cam",
6  "temperatureStation" // <-- add here
7 };

in jsondef.h you also must add the device type to the end of device_type\ enum (approx in line 400)

1 enum
2  {
3  //! Payload
4  DEVICE_TYPE_PLOAD=0,
5  ...
6  //! Camera
7  DEVICE_TYPE_CAM=26,
8  //! your tempStation here
9  DEVICE_TYPE_TEMPSTATION = 27, // <- add here
10  //! List count
11  DEVICE_TYPE_COUNT,
12  //! Not a Component
13  DEVICE_TYPE_NONE=65535
14  };

now we are going to modify some functions in the code. The first one is json_detroy in jsonlib.cpp

1 void json_destroy(cosmosstruc *cdata)
2 {
3  for (uint16_t i=0; i<2; ++i)
4  {
5  cdata[i].devspec.ant.resize(0);
6  ...
7  cdata[i].devspec.tsen.resize(0);
8  cdata[i].devspec.temperatureStation.resize(0); // <- add here
9  cdata[i].device.resize(0);
10 
11  }
12 
13  delete [] cdata;
14  cdata = NULL;
15 }

(side note: for a really complex type further definitions must be added to the namespace, but most common types are already supported, so this is an advanced feature)

go to json_devices_specific and inside the for loop that goes over each type add some of the following

1 const char *json_devices_specific(string &jstring, cosmosstruc *cdata)
2 {
3 ...
4 
5  for (uint16_t j=0; j<*cnt; ++j)
6  {
7  ...
8 
9  // Dump Temperature Station
10  if (!strcmp(device_type_string[i].c_str(),"tempStation"))
11  {
12  json_out_1d(jstring,(char *)"device_tempStation_temperature",j,cdata);
13  json_out_character(jstring, '\n');
14  }
15 
16  }
17 }

go to json_clone

1 int32_t json_clone(cosmosstruc *cdata)
2 {
3 ...
4  case DEVICE_TYPE_TEMPSTATION:
5  cdata[1].devspec.tempStation[cdata[1].device[i].all.gen.didx] =
6  &cdata[1].device[i].tempStation;
7  break;
8 ...
9 }

add name for the device count in json_addbaseentry

1 uint16_t json_addbaseentry(cosmosstruc *cdata)
2 {
3 
4  ...
5  json_addentry("device_tempStation_cnt",
6  UINT16_MAX,
7  UINT16_MAX,
8  offsetof(devspecstruc,tempStation_cnt),
9  COSMOS_SIZEOF(uint16_t),
10  (uint16_t)JSON_TYPE_UINT16,
11  JSON_GROUP_DEVSPEC,
12  cdata);
13 
14 }

to json_adddeviceentry add

1 uint16_t json_adddeviceentry(uint16_t i, cosmosstruc *cdata)
2 {
3 
4  ...
5  case DEVICE_TYPE_TEMPSTATION:
6 
7  json_addentry("device_tempStation_utc",
8  didx,
9  UINT16_MAX,
10  (ptrdiff_t)offsetof(genstruc,utc)+i*sizeof(devicestruc),
11  COSMOS_SIZEOF(double),
12  (uint16_t)JSON_TYPE_DOUBLE,
13  JSON_GROUP_DEVICE,
14  cdata);
15 
16  json_addentry("device_tempStation_temperature",
17  didx,
18  UINT16_MAX,
19  (ptrdiff_t)offsetof(temperatureStationStruc,temperature) +
20  i*sizeof(devicestruc),
21  COSMOS_SIZEOF(double),
22  (uint16_t)JSON_TYPE_DOUBLE,
23  JSON_GROUP_DEVICE,
24  cdata);
25 
26  cdata[0].devspec.tempStation.push_back(
27  (temperatureStationStruct *)&cdata[0].device[i].tempStation);
28  cdata[0].devspec.tempStation_cnt =
29  (uint16_t)cdata[0].devspec.tempStation.size();
30  break;
31 
32 }