Debian Codegraph @

src/libais/ais8_1_26.cpp

raw symbols SCIP
1 // The USCG RTCM SC 121 Environmental Sensor Message tested in
2 // Tampa, FL from 200[78] util ?
3
4 // http://www.dtic.mil/cgi-bin/GetTRDoc?AD=ADA504755
5 // Phase I Summary Report on AIS Transmit Project (Environmental Message)
6
7 #include "ais.h"
8
9 namespace libais {
10
11 Ais8_1_26_Location::Ais8_1_26_Location(const AisBitset &bits,
12 const size_t offset) {
13 position = bits.ToAisPoint(offset, 55);
14 z = bits.ToUnsignedInt(offset + 55, 11) / 10.;
15 owner = bits.ToUnsignedInt(offset + 66, 4);
16 timeout = bits.ToUnsignedInt(offset + 70, 3);
17 spare = bits.ToUnsignedInt(offset + 73, 12);
18 }
19
20 Ais8_1_26_Station::Ais8_1_26_Station(const AisBitset &bits,
21 const size_t offset) {
22 name = bits.ToString(offset, 84);
23 spare = bits.ToUnsignedInt(offset + 84, 1);
24 }
25
26 Ais8_1_26_Wind::Ais8_1_26_Wind(const AisBitset &bits,
27 const size_t offset) {
28 wind_speed = bits.ToUnsignedInt(offset, 7);
29 wind_gust = bits.ToUnsignedInt(offset + 7, 7); // knots
30 wind_dir = bits.ToUnsignedInt(offset + 14, 9);
31 wind_gust_dir = bits.ToUnsignedInt(offset + 23, 9);
32 sensor_type = bits.ToUnsignedInt(offset + 32, 3);
33 wind_forecast = bits.ToUnsignedInt(offset + 35, 7);
34 wind_gust_forecast = bits.ToUnsignedInt(offset + 42, 7); // knots
35 wind_dir_forecast = bits.ToUnsignedInt(offset + 49, 9);
36 utc_day_forecast = bits.ToUnsignedInt(offset + 58, 5);
37 utc_hour_forecast = bits.ToUnsignedInt(offset + 63, 5);
38 utc_min_forecast = bits.ToUnsignedInt(offset + 68, 6);
39 duration = bits.ToUnsignedInt(offset + 74, 8);
40 spare = bits.ToUnsignedInt(offset + 82, 3);
41 }
42
43 Ais8_1_26_WaterLevel::Ais8_1_26_WaterLevel(const AisBitset &bits,
44 const size_t offset) {
45 type = bits[offset];
46 level = bits.ToInt(offset + 1, 16) / 100.;
47 trend = bits.ToUnsignedInt(offset + 17, 2);
48 vdatum = bits.ToUnsignedInt(offset + 19, 5);
49 sensor_type = bits.ToUnsignedInt(offset + 24, 3);
50 forecast_type = bits[offset + 27];
51 level_forecast = bits.ToInt(offset + 28, 16) / 100.;
52 utc_day_forecast = bits.ToUnsignedInt(offset + 44, 5);
53 utc_hour_forecast = bits.ToUnsignedInt(offset + 49, 5);
54 utc_min_forecast = bits.ToUnsignedInt(offset + 54, 6);
55 duration = bits.ToUnsignedInt(offset + 60, 8);
56 spare = bits.ToUnsignedInt(offset + 68, 17);
57 }
58
59 Ais8_1_26_Curr2D::Ais8_1_26_Curr2D(const AisBitset &bits,
60 const size_t offset) {
61 for (size_t idx = 0; idx < 3; idx++) {
62 size_t start = offset + idx * 26;
63 currents[idx].speed = bits.ToUnsignedInt(start, 8) / 10.;
64 currents[idx].dir = bits.ToUnsignedInt(start + 8, 9);
65 currents[idx].depth = bits.ToUnsignedInt(start + 17, 9);
66 }
67 type = bits.ToUnsignedInt(offset + 78, 3);
68 spare = bits.ToUnsignedInt(offset + 81, 4);
69 }
70
71 Ais8_1_26_Curr3D::Ais8_1_26_Curr3D(const AisBitset &bits,
72 const size_t offset) {
73 for (size_t idx = 0; idx < 2; idx++) {
74 size_t start = offset + idx * 33;
75 currents[idx].north = bits.ToUnsignedInt(start, 8) / 10.;
76 currents[idx].east = bits.ToUnsignedInt(start + 8, 8) / 10.;
77 currents[idx].up = bits.ToUnsignedInt(start + 16, 8) / 10.;
78 currents[idx].depth = bits.ToUnsignedInt(start + 24, 9);
79 }
80 type = bits.ToUnsignedInt(offset + 66, 3);
81 spare = bits.ToUnsignedInt(offset + 69, 16);
82 }
83
84 Ais8_1_26_HorzFlow::Ais8_1_26_HorzFlow(const AisBitset &bits,
85 const size_t offset) {
86 for (size_t idx = 0; idx < 2; idx++) {
87 size_t start = offset + idx * 42;
88 currents[idx].bearing = bits.ToUnsignedInt(start, 9);
89 currents[idx].dist = bits.ToUnsignedInt(start + 9, 7);
90 currents[idx].speed = bits.ToUnsignedInt(start + 16, 8) / 10.;
91 currents[idx].dir = bits.ToUnsignedInt(start + 24, 9);
92 currents[idx].level = bits.ToUnsignedInt(start + 33, 9);
93 }
94 spare = bits[offset + 84];
95 }
96
97 Ais8_1_26_SeaState::Ais8_1_26_SeaState(const AisBitset &bits,
98 const size_t offset) {
99 swell_height = bits.ToUnsignedInt(offset, 8) / 10.;
100 swell_period = bits.ToUnsignedInt(offset + 8, 6);
101 swell_dir = bits.ToUnsignedInt(offset + 14, 9);
102 sea_state = bits.ToUnsignedInt(offset + 23, 4);
103 swell_sensor_type = bits.ToUnsignedInt(offset + 27, 3);
104 water_temp = bits.ToInt(offset + 30, 10) / 10.;
105 water_temp_depth = bits.ToUnsignedInt(offset + 40, 7) / 10.;
106 water_sensor_type = bits.ToUnsignedInt(offset + 47, 3);
107 wave_height = bits.ToUnsignedInt(offset + 50, 8) / 10.;
108 wave_period = bits.ToUnsignedInt(offset + 58, 6);
109 wave_dir = bits.ToUnsignedInt(offset + 64, 9);
110 wave_sensor_type = bits.ToUnsignedInt(offset + 73, 3);
111 salinity = bits.ToUnsignedInt(offset + 76, 9) / 10.;
112 }
113
114 Ais8_1_26_Salinity::Ais8_1_26_Salinity(const AisBitset &bits,
115 const size_t offset) {
116 water_temp = bits.ToUnsignedInt(offset, 10) / 10. - 10;
117 conductivity = bits.ToUnsignedInt(offset + 10, 10) / 100.;
118 pressure = bits.ToUnsignedInt(offset + 20, 16) / 10.;
119 salinity = bits.ToUnsignedInt(offset + 36, 9) / 10.;
120 salinity_type = bits.ToUnsignedInt(offset + 45, 2);
121 sensor_type = bits.ToUnsignedInt(offset + 47, 3);
122 spare[0] = bits.ToUnsignedInt(offset + 50, 32);
123 spare[1] = bits.ToUnsignedInt(offset + 82, 3);
124 }
125
126 Ais8_1_26_Wx::Ais8_1_26_Wx(const AisBitset &bits,
127 const size_t offset) {
128 air_temp = bits.ToInt(offset, 11) / 10.;
129 air_temp_sensor_type = bits.ToUnsignedInt(offset + 11, 3);
130 precip = bits.ToUnsignedInt(offset + 14, 2);
131 horz_vis = bits.ToUnsignedInt(offset + 16, 8) / 10.;
132 dew_point = bits.ToInt(offset + 24, 10) / 10.;
133 dew_point_type = bits.ToUnsignedInt(offset + 34, 3);
134 air_pressure = (bits.ToUnsignedInt(offset + 37, 9) + 800) / 100.0; // Pa.
135 air_pressure_trend = bits.ToUnsignedInt(offset + 46, 2);
136 air_pressor_type = bits.ToUnsignedInt(offset + 48, 3);
137 salinity = bits.ToUnsignedInt(offset + 51, 9) / 10.;
138 spare = bits.ToUnsignedInt(offset + 60, 25);
139 }
140
141 Ais8_1_26_AirDraught::Ais8_1_26_AirDraught(const AisBitset &bits,
142 const size_t offset) {
143 draught = bits.ToUnsignedInt(offset, 13) / 100.;
144 gap = bits.ToUnsignedInt(offset + 13, 13) / 10.;
145 trend = bits.ToUnsignedInt(offset + 26, 2);
146 forecast_gap = bits.ToUnsignedInt(offset + 28, 13) / 10.;
147 utc_day_forecast = bits.ToUnsignedInt(offset + 41, 5);
148 utc_hour_forecast = bits.ToUnsignedInt(offset + 46, 5);
149 utc_min_forecast = bits.ToUnsignedInt(offset + 51, 6);
150 spare = bits.ToUnsignedInt(offset + 57, 28);
151 }
152
153 // TODO(schwehr): Refactor to be like the 8:367:22 factory.
154 Ais8_1_26_SensorReport*
155 ais8_1_26_sensor_report_factory(const AisBitset &bits,
156 const size_t offset) {
157 const Ais8_1_26_SensorEnum rpt_type =
158 (Ais8_1_26_SensorEnum)bits.ToUnsignedInt(offset, 4);
159
160 // WARNING: out of order decoding
161 // Only get the report header if we can decode the type
162 const size_t rpt_start = offset + 27; // skip tp after site_id
163 bits.SeekTo(rpt_start);
164 Ais8_1_26_SensorReport *rpt = nullptr;
165 switch (rpt_type) {
166 case AIS8_1_26_SENSOR_LOCATION:
167 rpt = new Ais8_1_26_Location(bits, rpt_start);
168 break;
169 case AIS8_1_26_SENSOR_STATION:
170 rpt = new Ais8_1_26_Station(bits, rpt_start);
171 break;
172 case AIS8_1_26_SENSOR_WIND:
173 rpt = new Ais8_1_26_Wind(bits, rpt_start);
174 break;
175 case AIS8_1_26_SENSOR_WATER_LEVEL:
176 rpt = new Ais8_1_26_WaterLevel(bits, rpt_start);
177 break;
178 case AIS8_1_26_SENSOR_CURR_2D:
179 rpt = new Ais8_1_26_Curr2D(bits, rpt_start);
180 break;
181 case AIS8_1_26_SENSOR_CURR_3D:
182 rpt = new Ais8_1_26_Curr3D(bits, rpt_start);
183 break;
184 case AIS8_1_26_SENSOR_HORZ_FLOW:
185 rpt = new Ais8_1_26_HorzFlow(bits, rpt_start);
186 break;
187 case AIS8_1_26_SENSOR_SEA_STATE:
188 rpt = new Ais8_1_26_SeaState(bits, rpt_start);
189 break;
190 case AIS8_1_26_SENSOR_SALINITY:
191 rpt = new Ais8_1_26_Salinity(bits, rpt_start);
192 break;
193 case AIS8_1_26_SENSOR_WX:
194 rpt = new Ais8_1_26_Wx(bits, rpt_start);
195 break;
196 case AIS8_1_26_SENSOR_AIR_DRAUGHT:
197 rpt = new Ais8_1_26_AirDraught(bits, rpt_start);
198 break;
199 case AIS8_1_26_SENSOR_RESERVED_11: break; // Leave rpt == 0 to indicate error
200 case AIS8_1_26_SENSOR_RESERVED_12: break;
201 case AIS8_1_26_SENSOR_RESERVED_13: break;
202 case AIS8_1_26_SENSOR_RESERVED_14: break;
203 case AIS8_1_26_SENSOR_RESERVED_15: break;
204 default:
205 {} // Leave rpt == 0 to indicate error
206 }
207
208 if (!rpt)
209 return rpt;
210
211 rpt->report_type = rpt_type;
212 bits.SeekTo(offset + 4);
213 rpt->utc_day = bits.ToUnsignedInt(offset + 4, 5);
214 rpt->utc_hr = bits.ToUnsignedInt(offset + 9, 5);
215 rpt->utc_min = bits.ToUnsignedInt(offset + 14, 6);
216 rpt->site_id = bits.ToUnsignedInt(offset + 20, 7);
217 return rpt;
218 }
219
220 Ais8_1_26::Ais8_1_26(const char *nmea_payload, const size_t pad)
221 : Ais8(nmea_payload, pad) {
222 assert(dac == 1);
223 assert(fi == 26);
224
225 if (!CheckStatus()) {
226 return;
227 }
228 if (168 > num_bits || num_bits > 1098) {
229 status = AIS_ERR_BAD_BIT_COUNT;
230 return;
231 }
232
233 const size_t num_sensor_reports = (num_bits - 56) / AIS8_1_26_REPORT_SIZE;
234
235 // TODO(schwehr): what to do about extra data in sensor report msg 8_1_26?
236 // if ((num_bits - 56) % AIS8_1_26_REPORT_SIZE)
237
238 for (size_t report_idx = 0; report_idx < num_sensor_reports; report_idx++) {
239 const size_t start = 56 + report_idx * AIS8_1_26_REPORT_SIZE;
240 bits.SeekTo(start);
241 Ais8_1_26_SensorReport *sensor =
242 ais8_1_26_sensor_report_factory(bits, start);
243 if (sensor) {
244 reports.push_back(sensor);
245 } else {
246 status = AIS_ERR_BAD_SUB_SUB_MSG;
247 return;
248 }
249 }
250
251 // TODO(schwehr): Enable this assert after fixing the message.
252 // assert(bits.GetRemaining() == 0);
253 status = AIS_OK;
254 }
255
256 // TODO(schwehr): Use unique_ptr to manage memory.
257 Ais8_1_26::~Ais8_1_26() {
258 for (size_t i = 0; i < reports.size(); i++) {
259 delete reports[i];
260 reports[i] = nullptr;
261 }
262 }
263
264 } // namespace libais