243 lines
8.2 KiB
C
243 lines
8.2 KiB
C
#include <itss-transport/TransportRequest.h>
|
|
#include <itss-transport/TransportIndication.h>
|
|
#include <itss-facilities/FacilitiesIndication.h>
|
|
#include <itss-facilities/FacilitiesRequest.h>
|
|
#include <itss-management/ManagementRequest.h>
|
|
#include <itss-management/ManagementReply.h>
|
|
|
|
#include <pthread.h>
|
|
#include <zmq.h>
|
|
#include <unistd.h>
|
|
|
|
void hex2bin(char* hex, uint8_t** bin, uint16_t* bin_len) {
|
|
*bin_len = strlen(hex) / 2;
|
|
*bin = malloc(*bin_len);
|
|
char substr[3];
|
|
substr[2] = '\0';
|
|
for (int i = 0; i < *bin_len; ++i) {
|
|
memcpy(substr, hex + i * 2, 2);
|
|
(*bin)[i] = (uint8_t)strtoul(substr, NULL, 16);
|
|
}
|
|
}
|
|
|
|
int init(void* ctx) {
|
|
printf("Simulating [management] ... \n"); fflush(stdout);
|
|
|
|
void* management_socket = zmq_socket(ctx, ZMQ_REP);
|
|
|
|
zmq_bind(management_socket, "ipc:///tmp/itss/management");
|
|
uint8_t buffer[2048];
|
|
|
|
// Request get
|
|
zmq_recv(management_socket, buffer, 2048, 0);
|
|
int rv = 0;
|
|
|
|
ManagementRequest_t* mreq = NULL;
|
|
asn_dec_rval_t dec = asn_decode(NULL, ATS_CANONICAL_OER, &asn_DEF_ManagementRequest, (void**) &mreq, buffer, 2048);
|
|
if (dec.code) {
|
|
rv = 1;
|
|
printf(" FAIL: MREQ decode error\n"); fflush(stdout);
|
|
goto cleanup;
|
|
}
|
|
printf(" - Received MREQ.get\n"); fflush(stdout);
|
|
|
|
ManagementReply_t* mrep = calloc(1, sizeof(ManagementReply_t));
|
|
switch (mreq->present) {
|
|
case ManagementRequest_PR_attributes:
|
|
switch (mreq->choice.attributes.present) {
|
|
case ManagementRequestAttributes_PR_get:
|
|
mrep->returnCode = ManagementReplyReturnCode_accepted;
|
|
mrep->attributes = calloc(1, sizeof(ManagementReplyAttributes_t));
|
|
|
|
if (mreq->choice.attributes.choice.get.stationID) {
|
|
mrep->attributes->stationID = malloc(sizeof(long));
|
|
*mrep->attributes->stationID = 78;
|
|
}
|
|
|
|
if (mreq->choice.attributes.choice.get.gpsType) {
|
|
mrep->attributes->gpsType = malloc(sizeof(long));
|
|
*mrep->attributes->gpsType = 0;
|
|
}
|
|
|
|
if (mreq->choice.attributes.choice.get.clockType) {
|
|
mrep->attributes->clockType = malloc(sizeof(long));
|
|
*mrep->attributes->clockType = 0;
|
|
}
|
|
|
|
if (mreq->choice.attributes.choice.get.clock) {
|
|
mrep->attributes->clock = calloc(1, sizeof(TimestampIts_t));
|
|
asn_ulong2INTEGER(mrep->attributes->clock, 666);
|
|
}
|
|
|
|
if (mreq->choice.attributes.choice.get.clockOffset) {
|
|
mrep->attributes->clockOffset = calloc(1, sizeof(TimestampIts_t));
|
|
asn_ulong2INTEGER(mrep->attributes->clockOffset, 0);
|
|
}
|
|
|
|
if (mreq->choice.attributes.choice.get.coordinates) {
|
|
mrep->attributes->coordinates = calloc(1, sizeof(WGS84Coordinates_t));
|
|
mrep->attributes->coordinates->latitude = 0;
|
|
mrep->attributes->coordinates->longitude = 0;
|
|
}
|
|
|
|
if (mreq->choice.attributes.choice.get.altitude) {
|
|
mrep->attributes->altitude = calloc(1, sizeof(Altitude_t));
|
|
mrep->attributes->altitude->altitudeValue = 0;
|
|
mrep->attributes->altitude->altitudeConfidence = 1;
|
|
}
|
|
|
|
if (mreq->choice.attributes.choice.get.speed) {
|
|
mrep->attributes->speed = calloc(1, sizeof(Speed_t));
|
|
mrep->attributes->speed->speedValue = 0;
|
|
mrep->attributes->speed->speedConfidence = 1;
|
|
}
|
|
|
|
if (mreq->choice.attributes.choice.get.heading) {
|
|
mrep->attributes->heading = calloc(1, sizeof(Heading_t));
|
|
mrep->attributes->heading->headingValue = 0;
|
|
mrep->attributes->heading->headingConfidence = 1;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
rv = 1;
|
|
printf(" FAIL: MREQ.ATTRIBUTES. not GET (%d)\n", mreq->present); fflush(stdout);
|
|
goto cleanup;
|
|
}
|
|
break;
|
|
|
|
case ManagementRequest_PR_sa:
|
|
default:
|
|
rv = 1;
|
|
printf(" FAIL: MREQ. not ATTRIBUTES (%d)\n", mreq->present); fflush(stdout);
|
|
goto cleanup;
|
|
}
|
|
|
|
asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_ManagementReply, NULL, mrep, buffer, 256);
|
|
zmq_send(management_socket, buffer, enc.encoded, 0);
|
|
|
|
printf(" OK\n"); fflush(stdout);
|
|
|
|
cleanup:
|
|
ASN_STRUCT_FREE(asn_DEF_ManagementRequest, mreq);
|
|
ASN_STRUCT_FREE(asn_DEF_ManagementReply, mrep);
|
|
|
|
return rv;
|
|
}
|
|
|
|
int cam_gen(void* ctx) {
|
|
printf("Testing CAM generation:\n"); fflush(stdout);
|
|
|
|
void* transport_socket = zmq_socket(ctx, ZMQ_REP);
|
|
|
|
int rv = zmq_bind(transport_socket, "ipc:///tmp/itss/transport");
|
|
uint8_t buffer[2048];
|
|
zmq_recv(transport_socket, buffer, 2048, 0);
|
|
int code = 0;
|
|
zmq_send(transport_socket, &code, 1, 0);
|
|
|
|
zmq_close(transport_socket);
|
|
|
|
TransportRequest_t* tr = NULL;
|
|
asn_dec_rval_t dec = asn_decode(NULL, ATS_CANONICAL_OER, &asn_DEF_TransportRequest, (void**) &tr, buffer+1, 2047);
|
|
if (dec.code) {
|
|
printf(" FAIL\n"); fflush(stdout);
|
|
return 1;
|
|
}
|
|
printf(" - Received CAM TR (%ldB)\n", tr->choice.packet.choice.btp.data.size); fflush(stdout);
|
|
|
|
if (tr->choice.packet.choice.btp.destinationPort == Port_cam) {
|
|
printf(" OK\n"); fflush(stdout);
|
|
} else {
|
|
printf(" FAIL\n"); fflush(stdout);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int forward_up(void* ctx) {
|
|
printf("Testing forwarding up:\n"); fflush(stdout);
|
|
|
|
TransportIndication_t* ti = calloc(1, sizeof(TransportIndication_t));
|
|
ti->present = TransportIndication_PR_packet;
|
|
ti->choice.packet.present = TransportPacketIndication_PR_btp;
|
|
BTPPacketIndication_t* bpi = &ti->choice.packet.choice.btp;
|
|
|
|
char* cam_hex = "02027dfddf4403ca4059bba5f38cc40dba9ffffffc2230eff200e11fc0078082a88a8337fee3fff600004dffea800618d08018efff14003ac68800c77ff8e002263460063bffd1000fb1a30031dffe2800958d30018efff840048c68800c77ffae002c63480063bffbd001a31a40031dfff28002d8cf0018c0";
|
|
|
|
uint8_t* cam;
|
|
uint16_t cam_len;
|
|
hex2bin(cam_hex, &cam, &cam_len);
|
|
|
|
bpi->data.buf = cam;
|
|
bpi->data.size = cam_len;
|
|
bpi->gn.destinationAddress.buf = calloc(1, 6);
|
|
bpi->gn.destinationAddress.size = 6;
|
|
bpi->destinationPort = Port_cam;
|
|
|
|
uint8_t b_ti_cam[512];
|
|
b_ti_cam[0] = 3;
|
|
|
|
asn_enc_rval_t enc = asn_encode_to_buffer(NULL, ATS_CANONICAL_OER, &asn_DEF_TransportIndication, ti, b_ti_cam+1, 511);
|
|
|
|
void* facilities_socket = zmq_socket(ctx, ZMQ_REQ);
|
|
|
|
int code = 0;
|
|
|
|
zmq_connect(facilities_socket, "ipc:///tmp/itss/facilities");
|
|
zmq_send(facilities_socket, b_ti_cam, enc.encoded+1, 0);
|
|
zmq_recv(facilities_socket, &code, 1, 0);
|
|
if (code) {
|
|
printf(" FAIL\n"); fflush(stdout);
|
|
return 1;
|
|
}
|
|
printf(" - Sent CAM TI (%ldB)\n", bpi->data.size); fflush(stdout);
|
|
|
|
void* applications_socket = zmq_socket(ctx, ZMQ_REP);
|
|
|
|
zmq_bind(applications_socket, "ipc:///tmp/itss/applications");
|
|
uint8_t buffer[2048];
|
|
zmq_recv(applications_socket, buffer, 2048, 0);
|
|
code = 0;
|
|
zmq_send(applications_socket, &code, 1, 0);
|
|
|
|
zmq_close(applications_socket);
|
|
zmq_close(facilities_socket);
|
|
|
|
FacilitiesIndication_t* fi = NULL;
|
|
asn_dec_rval_t dec = asn_decode(NULL, ATS_CANONICAL_OER, &asn_DEF_FacilitiesIndication, (void**) &fi, buffer+1, 2047);
|
|
if (dec.code) {
|
|
printf(" FAIL\n"); fflush(stdout);
|
|
return 1;
|
|
}
|
|
printf(" - Received CAM FI (%ldB)\n", fi->choice.message.data.size); fflush(stdout);
|
|
|
|
if (fi->choice.message.data.size == bpi->data.size) {
|
|
printf(" OK\n"); fflush(stdout);
|
|
} else {
|
|
printf(" FAIL\n"); fflush(stdout);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int main() {
|
|
|
|
int rv = 0;
|
|
|
|
void* ctx = zmq_ctx_new();
|
|
|
|
rv += init(ctx);
|
|
if (rv) return 1;
|
|
|
|
rv += forward_up(ctx);
|
|
//rv += cam_gen(ctx);
|
|
|
|
return rv;
|
|
}
|
|
|