Skip to content
Snippets Groups Projects
var102.h 7.54 KiB
Newer Older
Valerio Pastore's avatar
Valerio Pastore committed
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <sstream>
#include "boost/any.hpp"
#include <Specific.hh>
#include <Encoder.hh>
#include <Decoder.hh>

#include <Base_Packet.h>
namespace VAR102 {

static const int VAR_PIXELS_SIZE = 64;
static const int NUM_PDM = 37;

Valerio Pastore's avatar
Valerio Pastore committed
struct PDMBlock {
Valerio Pastore's avatar
.  
Valerio Pastore committed
	bool pdmVal;
	int32_t samplingPar;
	int32_t pdmID;
	std::vector<int32_t> var_pixels;
	PDMBlock() :
			pdmVal(bool()), samplingPar(int32_t()), pdmID(int32_t()), var_pixels(
					std::vector<int32_t>()) {
	}
Valerio Pastore's avatar
Valerio Pastore committed
};

struct VAR102 {
Valerio Pastore's avatar
.  
Valerio Pastore committed
	int32_t telescopeID;
	int32_t type;
	int32_t subType;
	int32_t ssc;
	int32_t packetLength;
	int32_t year;
	int32_t month;
	int32_t day;
	int32_t hours;
	int32_t minutes;
	int32_t seconds;
	bool validTime;
	int32_t timeTagNanosec;
	int32_t eventCounter;
	bool lid;
	bool fibSt;
	bool fibCont;
	bool fibPuls;
	int32_t rgbCont;
	int32_t rgbPuls;
	int32_t spare_0;
	std::vector<PDMBlock> PDMs;
	VAR102() :
			telescopeID(int32_t()), type(int32_t()), subType(int32_t()), ssc(
					int32_t()), packetLength(int32_t()), year(int32_t()), month(
					int32_t()), day(int32_t()), hours(int32_t()), minutes(
					int32_t()), seconds(int32_t()), validTime(bool()), timeTagNanosec(
					int32_t()), eventCounter(int32_t()), lid(bool()), fibSt(
					bool()), fibCont(bool()), fibPuls(bool()), rgbCont(
					int32_t()), rgbPuls(int32_t()), spare_0(int32_t()), PDMs(
					std::vector<PDMBlock>()) {
	}
Valerio Pastore's avatar
Valerio Pastore committed
};

}
namespace avro {
template<> struct codec_traits<VAR102::PDMBlock> {
Valerio Pastore's avatar
.  
Valerio Pastore committed
	static void encode(Encoder &e, const VAR102::PDMBlock &v) {
		avro::encode(e, v.pdmVal);
		avro::encode(e, v.samplingPar);
		avro::encode(e, v.pdmID);
		avro::encode(e, v.var_pixels);
	}
	static void decode(Decoder &d, VAR102::PDMBlock &v) {
		if (avro::ResolvingDecoder *rd =
				dynamic_cast<avro::ResolvingDecoder*>(&d)) {
			const std::vector<size_t> fo = rd->fieldOrder();
			for (std::vector<size_t>::const_iterator it = fo.begin();
					it != fo.end(); ++it) {
				switch (*it) {
				case 0:
					avro::decode(d, v.pdmVal);
					break;
				case 1:
					avro::decode(d, v.samplingPar);
					break;
				case 2:
					avro::decode(d, v.pdmID);
					break;
				case 3:
					avro::decode(d, v.var_pixels);
					break;
				default:
					break;
				}
			}
		} else {
			avro::decode(d, v.pdmVal);
			avro::decode(d, v.samplingPar);
			avro::decode(d, v.pdmID);
			avro::decode(d, v.var_pixels);
		}
	}
Valerio Pastore's avatar
Valerio Pastore committed
};

template<> struct codec_traits<VAR102::VAR102> {
Valerio Pastore's avatar
.  
Valerio Pastore committed
	static void encode(Encoder &e, const VAR102::VAR102 &v) {
		avro::encode(e, v.telescopeID);
		avro::encode(e, v.type);
		avro::encode(e, v.subType);
		avro::encode(e, v.ssc);
		avro::encode(e, v.packetLength);
		avro::encode(e, v.year);
		avro::encode(e, v.month);
		avro::encode(e, v.day);
		avro::encode(e, v.hours);
		avro::encode(e, v.minutes);
		avro::encode(e, v.seconds);
		avro::encode(e, v.validTime);
		avro::encode(e, v.timeTagNanosec);
		avro::encode(e, v.eventCounter);
		avro::encode(e, v.lid);
		avro::encode(e, v.fibSt);
		avro::encode(e, v.fibCont);
		avro::encode(e, v.fibPuls);
		avro::encode(e, v.rgbCont);
		avro::encode(e, v.rgbPuls);
		avro::encode(e, v.spare_0);
		avro::encode(e, v.PDMs);
	}
	static void decode(Decoder &d, VAR102::VAR102 &v) {
		if (avro::ResolvingDecoder *rd =
				dynamic_cast<avro::ResolvingDecoder*>(&d)) {
			const std::vector<size_t> fo = rd->fieldOrder();
			for (std::vector<size_t>::const_iterator it = fo.begin();
					it != fo.end(); ++it) {
				switch (*it) {
				case 0:
					avro::decode(d, v.telescopeID);
					break;
				case 1:
					avro::decode(d, v.type);
					break;
				case 2:
					avro::decode(d, v.subType);
					break;
				case 3:
					avro::decode(d, v.ssc);
					break;
				case 4:
					avro::decode(d, v.packetLength);
					break;
				case 5:
					avro::decode(d, v.year);
					break;
				case 6:
					avro::decode(d, v.month);
					break;
				case 7:
					avro::decode(d, v.day);
					break;
				case 8:
					avro::decode(d, v.hours);
					break;
				case 9:
					avro::decode(d, v.minutes);
					break;
				case 10:
					avro::decode(d, v.seconds);
					break;
				case 11:
					avro::decode(d, v.validTime);
					break;
				case 12:
					avro::decode(d, v.timeTagNanosec);
					break;
				case 13:
					avro::decode(d, v.eventCounter);
					break;
				case 14:
					avro::decode(d, v.lid);
					break;
				case 15:
					avro::decode(d, v.fibSt);
					break;
				case 16:
					avro::decode(d, v.fibCont);
					break;
				case 17:
					avro::decode(d, v.fibPuls);
					break;
				case 18:
					avro::decode(d, v.rgbCont);
					break;
				case 19:
					avro::decode(d, v.rgbPuls);
					break;
				case 20:
					avro::decode(d, v.spare_0);
					break;
				case 21:
					avro::decode(d, v.PDMs);
					break;
				default:
					break;
				}
			}
		} else {
			avro::decode(d, v.telescopeID);
			avro::decode(d, v.type);
			avro::decode(d, v.subType);
			avro::decode(d, v.ssc);
			avro::decode(d, v.packetLength);
			avro::decode(d, v.year);
			avro::decode(d, v.month);
			avro::decode(d, v.day);
			avro::decode(d, v.hours);
			avro::decode(d, v.minutes);
			avro::decode(d, v.seconds);
			avro::decode(d, v.validTime);
			avro::decode(d, v.timeTagNanosec);
			avro::decode(d, v.eventCounter);
			avro::decode(d, v.lid);
			avro::decode(d, v.fibSt);
			avro::decode(d, v.fibCont);
			avro::decode(d, v.fibPuls);
			avro::decode(d, v.rgbCont);
			avro::decode(d, v.rgbPuls);
			avro::decode(d, v.spare_0);
			avro::decode(d, v.PDMs);
		}
	}
Valerio Pastore's avatar
Valerio Pastore committed
};
void encodeVAR102(Encoder &avroencoder,
Valerio Pastore's avatar
.  
Valerio Pastore committed
		inaf::oasbo::Packets::BasePacket &packet) {
	// PACKET HEADER
	int offset;
	for (offset = 0; offset < 11; offset++)
		avro::encode(avroencoder, (int32_t) packet[offset].value());

	avro::encode(avroencoder, (bool) packet[11].value());
	avro::encode(avroencoder, (int32_t) packet[12].value());
	avro::encode(avroencoder, (int32_t) packet[13].value());
	avro::encode(avroencoder, (bool) packet[14].value());
	avro::encode(avroencoder, (bool) packet[15].value());
	avro::encode(avroencoder, (bool) packet[16].value());
	avro::encode(avroencoder, (bool) packet[17].value());

	avro::encode(avroencoder, (int32_t) packet[18].value());
	avro::encode(avroencoder, (int32_t) packet[19].value());
	avro::encode(avroencoder, (int32_t) packet[20].value());
	offset = 21;
	// PACKET SOURCE DATA FIELD
	avroencoder.arrayStart();
	avroencoder.setItemCount(VAR102::NUM_PDM);

	for (int i = 0; i < VAR102::NUM_PDM; i++) {
		avroencoder.startItem();
		avro::encode(avroencoder, (bool) packet[offset++].value());
		avro::encode(avroencoder, (int32_t) packet[offset++].value());
		avro::encode(avroencoder, (int32_t) packet[offset++].value());

		avroencoder.arrayStart();
		avroencoder.setItemCount(VAR102::VAR_PIXELS_SIZE);
Valerio Pastore's avatar
.  
Valerio Pastore committed
		for (int i = 0; i < VAR102::VAR_PIXELS_SIZE; i++) {
			avroencoder.startItem();
Valerio Pastore's avatar
.  
Valerio Pastore committed
			avro::encode(avroencoder, (int32_t) packet[offset + i].value());
		}
		offset += VAR102::VAR_PIXELS_SIZE;
		avroencoder.arrayEnd();
	}
	avroencoder.arrayEnd();
}