nidas  v1.2-1520
WisardMote.h
Go to the documentation of this file.
1 /* -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*- */
2 /* vim: set shiftwidth=4 softtabstop=4 expandtab: */
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2009, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 /*
27 
28  Sensor class supporting EOL "Wisard" motes.
29 
30  This code handles the serial messages from a base radio for a set of motes.
31 
32  The raw messages from each mote have a general format of
33 
34  ID N ':' version msgtype data CRC terminator
35 
36  ID is a non-numeric string of ASCII characters, typically "ID".
37 
38  N is one or more ascii decimal digits, specifying the number of the
39  originating mote. Each mote's number should be unique for the set of motes
40  being acquired by a data system. As explained below the mote
41  number should be in the range 1-127.
42 
43  version is a 1 byte integer indicating the message format version
44  number.
45 
46  msgtype is another 1 byte integer, where 1 indicates a data message.
47 
48  The data portion is a concatenation of one or more sensor data
49  fields. Each sensor data field consists of a 8 bit sensor type
50  followed by the data for that sensor type, where the format and
51  length of the data is defined for each sensor type.
52 
53  The full id of the raw samples will contain the DSM id in the top 16
54  bits, with the DSM sensor id in the low order 16 bits.
55  By convention we use a DSM sensor id of 0x8000 for all base motes
56  attached to a data system.
57 
58  The process() method of this class unpacks the raw samples, generating
59  a floating point sample for each sensor data field found in the raw
60  message.
61 
62  The NIDAS sample id provides 16 bits to uniquely identify a sample
63  on a DSM. In order to manage these ids we have established a convention
64  where the processed samples will have a DSM sensor id of 0x8000,
65  leaving 7 bits for the mote number (1-127), and 8 bits for the sensor type.
66 
67  Mote number of 0 is used in the configuration to specify variable names
68  for a sensor type which should be used for all motes. Therefore
69  a physical mote should not have an id of 0.
70 
71  */
72 
73 #ifndef NIDAS_DYNLD_ISFF_WISARDMOTE_H
74 #define NIDAS_DYNLD_ISFF_WISARDMOTE_H
75 
77 #include <nidas/core/Sample.h>
80 
81 #include <vector>
82 #include <map>
83 #include <string>
84 #include <iostream>
85 #include <exception>
86 
87 //#include <sstream>
88 #include <list>
89 
90 namespace nidas { namespace dynld { namespace isff {
91 
92 using namespace nidas::core;
93 
94 struct VarInfo
95 {
96  const char *name;
97  const char *units;
98  const char *longname;
99  const char *plotrange;
100 };
101 
112 
113 struct SampInfo
114 {
119  int firstst;
120  int lastst;
121  struct VarInfo variables[9];
123 };
124 
126 {
127 public:
128  WisardMote();
129 
130  virtual ~ WisardMote();
131 
132  bool process(const Sample* insamp,
133  std::list<const Sample*>& results) throw();
134 
135  void validate() throw (nidas::util::InvalidParameterException);
136 
140  struct MessageHeader {
141  int moteId;
142  int version;
144  };
145 
150  typedef const char *(WisardMote::*unpack_t)
151  (const char *cp, const char *eos,
152  unsigned int nfields,
153  const struct MessageHeader* hdr,
154  SampleTag* stag, SampleT< float >* osamp);
155 
157 
158 private:
159 
166  void createSampleTags(const SampleTag* stag,const std::vector<int>& motes,std::list<SampleTag*>& newtags)
168 
173  void addImpliedSampleTags(const std::vector<int>& motes);
174 
182  void checkLessUsedSensors(void);
183 
188  void addMoteSampleTag(SampleTag* tag);
189 
193  SampleTag* createSampleTag(SampInfo& sinfo,int mote, int stype);
194 
199  const char *checkEOM(const char *cp,
200  const char *eom, nidas::core::dsm_time_t ttag);
201 
206  const char *checkCRC(const char *cp,
207  const char *eom, nidas::core::dsm_time_t ttag);
208 
213  bool readHead(const char *&cp,const char *eom, dsm_time_t ttag,
214  struct MessageHeader*);
215 
220  int readMoteId(const char* &cp, const char*eos);
221 
226  void convert(SampleTag* stag, SampleT<float>* osamp, float* results=0);
227 
228  const char* unpackPicTime(const char *, const char *,
229  unsigned int, const struct MessageHeader*,
231 
232  const char* unpackUint16(const char *, const char *,
233  unsigned int, const struct MessageHeader*,
235 
236  const char* unpackInt16(const char *, const char *,
237  unsigned int, const struct MessageHeader*,
239 
240  const char* unpackUint32(const char *, const char *,
241  unsigned int, const struct MessageHeader*,
243 
244  const char* unpackInt32(const char *, const char *,
245  unsigned int, const struct MessageHeader*,
247 
248  const char* unpackAccumSec(const char *, const char *,
249  unsigned int, const struct MessageHeader*,
251 
252  const char* unpack100thSec(const char *, const char *,
253  unsigned int, const struct MessageHeader*,
255 
256  const char* unpack10thSec(const char *, const char *,
257  unsigned int, const struct MessageHeader*,
259 
260  const char* unpackPicTimeFields(const char *, const char *,
261  unsigned int, const struct MessageHeader*,
263 
264  const char* unpackTRH(const char *, const char *,
265  unsigned int, const struct MessageHeader*,
267 
268  const char* unpackTsoil(const char *, const char *,
269  unsigned int, const struct MessageHeader*,
271 
272  const char* unpackGsoil(const char *, const char *,
273  unsigned int, const struct MessageHeader*,
275 
276  const char* unpackQsoil(const char *, const char *,
277  unsigned int, const struct MessageHeader*,
279 
280  const char* unpackTP01(const char *, const char *,
281  unsigned int, const struct MessageHeader*,
283 
284  const char* unpackStatus(const char *, const char *,
285  unsigned int, const struct MessageHeader*,
287 
288  const char* unpackXbee(const char *, const char *,
289  unsigned int, const struct MessageHeader*,
291 
292  const char* unpackPower(const char *, const char *,
293  unsigned int, const struct MessageHeader*,
295 
296  const char* unpackRnet(const char *, const char *,
297  unsigned int, const struct MessageHeader*,
299 
300  const char* unpackRsw(const char *, const char *,
301  unsigned int, const struct MessageHeader*,
303 
304  const char* unpackRlw(const char *, const char *,
305  unsigned int, const struct MessageHeader*,
307 
308  const char* unpackRlwKZ(const char *, const char *,
309  unsigned int, const struct MessageHeader*,
311 
312  const char* unpackCNR2(const char *, const char *,
313  unsigned int, const struct MessageHeader*,
315 
316  const char* unpackNR01(const char *, const char *,
317  unsigned int, const struct MessageHeader*,
319 
320  const char* unpackRsw2(const char *, const char *,
321  unsigned int, const struct MessageHeader*,
323 
335  static std::map<dsm_sample_id_t, WisardMote*> _processorSensors;
336 
337  static SampInfo _samps[];
338 
339  static bool _functionsMapped;
340 
344  static std::map<int, std::pair<unpack_t,unsigned int> > _unpackMap;
345 
346  static std::map<int, std::string> _typeNames;
347 
348  static void initFuncMap();
349 
354  std::map<dsm_sample_id_t, SampleTag*> _sampleTagsById;
355 
363 
367  std::map<int, std::map<int, int > > _sensorSerialNumbersByMoteIdAndType;
368 
369  std::map<int, int> _sequenceNumbersByMoteId;
370 
371  std::map<int, unsigned int> _badCRCsByMoteId;
372 
373  std::map<int, int> _tdiffByMoteId;
374 
378  std::map <int, std::map<int, unsigned int> > _numBadSensorTypes;
379 
380  std::map<int, unsigned int> _unconfiguredMotes;
381 
382  std::map <int, std::map<int, unsigned int> > _noSampleTags;
383 
384  std::set<int> _ignoredSensorTypes;
385 
386  std::set<int> _nowarnSensorTypes;
387 
388  static const unsigned int NTSOILS = 4;
389 
392  class TsoilData
393  {
394  public:
395  float tempLast[NTSOILS];
396  dsm_time_t timeLast[NTSOILS];
397  TsoilData(): tempLast(),timeLast()
398  {
399  for (unsigned int i = 0; i < sizeof(tempLast)/sizeof(tempLast[0]); i++) {
400  tempLast[i] = floatNAN;
401  }
402  }
403  };
404  std::map<dsm_sample_id_t, TsoilData> _tsoilData;
405 
407  WisardMote(const WisardMote&);
408 
410  WisardMote& operator=(const WisardMote&);
411 
412 };
413 }}} // nidas::dynld::isff
414 #endif /* WISARDMOTE_H_ */
std::map< int, std::map< int, unsigned int > > _numBadSensorTypes
For each mote id, the number of unrecognized sensor types.
Definition: WisardMote.h:378
static const nidas::util::EndianConverter * fromLittle
Definition: WisardMote.h:156
std::map< int, std::map< int, unsigned int > > _noSampleTags
Definition: WisardMote.h:382
const char * units
Definition: WisardMote.h:97
int version
Definition: WisardMote.h:142
int lastst
Definition: WisardMote.h:120
static bool _functionsMapped
Definition: WisardMote.h:339
Support for a sensor that is sending packets on a TCP socket, a UDP socket, a Bluetooth RF Comm socke...
Definition: SerialSensor.h:64
long long dsm_time_t
Posix time in microseconds, the number of non-leap microseconds since 1970 Jan 1 00:00 UTC...
Definition: Sample.h:61
std::set< int > _nowarnSensorTypes
Definition: WisardMote.h:386
const float floatNAN
Value of a float NAN for general use.
Definition: Sample.cc:31
static std::map< int, std::pair< unpack_t, unsigned int > > _unpackMap
Mapping between sensor type and function which parses the data.
Definition: WisardMote.h:344
std::map< int, unsigned int > _badCRCsByMoteId
Definition: WisardMote.h:371
Definition: WisardMote.h:111
static std::map< dsm_sample_id_t, WisardMote * > _processorSensors
Because the Wisard mote data has internal identifiers, more than one WisardMote can be declared with ...
Definition: WisardMote.h:335
int firstst
firstst and lastst are the range of sensor types containing the list of variables.
Definition: WisardMote.h:119
int messageType
Definition: WisardMote.h:143
Extracted fields from the initial portion of a Wisard message.
Definition: WisardMote.h:140
Virtual base class declaring methods for converting numeric values between little-endian and big-endi...
Definition: EndianConverter.h:304
Definition: WisardMote.h:113
Definition: WisardMote.h:111
std::map< dsm_sample_id_t, TsoilData > _tsoilData
Definition: WisardMote.h:404
std::map< int, unsigned int > _unconfiguredMotes
Definition: WisardMote.h:380
std::map< int, int > _tdiffByMoteId
Definition: WisardMote.h:373
Definition: WisardMote.h:392
Interface to a data sample.
Definition: Sample.h:189
Definition: WisardMote.h:111
int moteId
Definition: WisardMote.h:141
static std::map< int, std::string > _typeNames
Definition: WisardMote.h:346
TsoilData()
Definition: WisardMote.h:397
WISARD_SAMPLE_TYPE
WST_IMPLIED: create sample tags for these sensor types, even if they aren&#39;t found in the XML...
Definition: WisardMote.h:111
const char * plotrange
Definition: WisardMote.h:99
Class describing a group of variables that are sampled and handled together.
Definition: SampleTag.h:87
WisardMote * _processorSensor
Pointer to the WisardMote that does the processing for the samples with this sensor id...
Definition: WisardMote.h:362
Definition: WisardMote.h:111
std::map< int, std::map< int, int > > _sensorSerialNumbersByMoteIdAndType
Sensor serial numbers, from message.
Definition: WisardMote.h:367
std::set< int > _ignoredSensorTypes
Definition: WisardMote.h:384
Definition: WisardMote.h:125
const char * longname
Definition: WisardMote.h:98
std::map< int, int > _sequenceNumbersByMoteId
Definition: WisardMote.h:369
Definition: WisardMote.h:94
std::map< dsm_sample_id_t, SampleTag * > _sampleTagsById
The processed sample tags for each id.
Definition: WisardMote.h:354
const char * name
Definition: WisardMote.h:96
Definition: InvalidParameterException.h:35