From 1d875728dd23dc41d38c8112c0ffe4a319f4984b Mon Sep 17 00:00:00 2001 From: Dave Akerman Date: Fri, 13 May 2016 10:50:43 +0000 Subject: [PATCH] Added packet logging --- .gitignore | 1 + gateway.c | 105 ++++++++++++++++++++++++++++++++++++++++++---------- gateway.txt | 46 +++++++++++++++-------- global.h | 6 +++ server.c | 46 +++++++++++++++++++---- 5 files changed, 162 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index 5b27b6d..20f7ca9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ desktop.ini *.ack gateway telemetry.txt +packets.txt ssdv diff --git a/gateway.c b/gateway.c index bcb4598..cc057fb 100644 --- a/gateway.c +++ b/gateway.c @@ -167,6 +167,27 @@ uint8_t readRegister(int Channel, uint8_t reg) return val; } +void LogPacket(int Channel, int8_t SNR, int RSSI, double FreqError, int Bytes, unsigned char MessageType) +{ + if (Config.EnablePacketLogging) + { + FILE *fp; + + if ((fp = fopen("packets.txt", "at")) != NULL) + { + time_t now; + struct tm *tm; + + now = time(0); + tm = localtime(&now); + + fprintf(fp, "%02d:%02d:%02d - Ch %d, SNR %d, RSSI %d, FreqErr %.1lf, Bytes %d, Type %02Xh\n", tm->tm_hour, tm->tm_min, tm->tm_sec, Channel, SNR, RSSI, FreqError, Bytes, MessageType); + + fclose(fp); + } + } +} + void LogTelemetryPacket(char *Telemetry) { if (Config.EnableTelemetryLogging) @@ -182,6 +203,7 @@ void LogTelemetryPacket(char *Telemetry) tm = localtime(&now); fprintf(fp, "%02d:%02d:%02d - %s\n", tm->tm_hour, tm->tm_min, tm->tm_sec, Telemetry); + fclose(fp); } } @@ -630,16 +652,49 @@ void DoPositionCalcs(Channel) void ProcessLine(int Channel, char *Line) { int FieldCount; + int Speed, Heading, Satellites; + float TempInt, TempExt; - FieldCount = sscanf(Line+2, "%15[^,],%u,%8[^,],%lf,%lf,%u", - &(Config.LoRaDevices[Channel].Payload), - &(Config.LoRaDevices[Channel].Counter), - &(Config.LoRaDevices[Channel].Time), - &(Config.LoRaDevices[Channel].Latitude), - &(Config.LoRaDevices[Channel].Longitude), - &(Config.LoRaDevices[Channel].Altitude)); - - // HAB->HAB_status = FieldCount == 6; + Config.LoRaDevices[Channel].FlightMode = -1; + + if (Config.EnableDev) + { + FieldCount = sscanf(Line+2, "%15[^,],%u,%8[^,],%lf,%lf,%u,%d,%d,%d,%f,%f,%lf,%lf,%lf,%lf,%d,%d,%d,%lf,%d,%d,%d,%d,%lf,%d", + &(Config.LoRaDevices[Channel].Payload), + &(Config.LoRaDevices[Channel].Counter), + &(Config.LoRaDevices[Channel].Time), + &(Config.LoRaDevices[Channel].Latitude), + &(Config.LoRaDevices[Channel].Longitude), + &(Config.LoRaDevices[Channel].Altitude), + &(Config.LoRaDevices[Channel].Speed), + &(Config.LoRaDevices[Channel].Heading), + &Satellites, + &TempInt, &TempExt, + &(Config.LoRaDevices[Channel].cda), + &(Config.LoRaDevices[Channel].PredictedLatitude), + &(Config.LoRaDevices[Channel].PredictedLongitude), + &(Config.LoRaDevices[Channel].PredictedLandingSpeed), + &(Config.LoRaDevices[Channel].PredictedTime), + &(Config.LoRaDevices[Channel].CompassActual), + &(Config.LoRaDevices[Channel].CompassTarget), + &(Config.LoRaDevices[Channel].AirSpeed), + &(Config.LoRaDevices[Channel].AirDirection), + &(Config.LoRaDevices[Channel].ServoLeft), + &(Config.LoRaDevices[Channel].ServoRight), + &(Config.LoRaDevices[Channel].ServoTime), + &(Config.LoRaDevices[Channel].GlideRatio), + &(Config.LoRaDevices[Channel].FlightMode)); + } + else + { + FieldCount = sscanf(Line+2, "%15[^,],%u,%8[^,],%lf,%lf,%u", + &(Config.LoRaDevices[Channel].Payload), + &(Config.LoRaDevices[Channel].Counter), + &(Config.LoRaDevices[Channel].Time), + &(Config.LoRaDevices[Channel].Latitude), + &(Config.LoRaDevices[Channel].Longitude), + &(Config.LoRaDevices[Channel].Altitude)); + } } @@ -1039,9 +1094,7 @@ void DIO0_Interrupt(int Channel) digitalWrite(Config.LoRaDevices[Channel].ActivityLED, 1); LEDCounts[Channel] = 5; } - // LogMessage("Channel %d data available - %d bytes\n", Channel, Bytes); - // LogMessage("Line = '%s'\n", Message); - + if (Message[1] == '!') { ProcessUploadMessage(Channel, Message+1); @@ -1055,6 +1108,14 @@ void DIO0_Interrupt(int Channel) ProcessTelemetryMessage(Channel, Message+1); TestMessageForSMSAcknowledgement(Channel, Message+1); } + else if (Message[1] == '>') + { + LogMessage("Flight Controller message %d bytes = %s", Bytes, Message+1); + } + else if (Message[1] == '*') + { + LogMessage("Uplink Command message %d bytes = %s", Bytes, Message+1); + } else if (Message[1] == 0x66) { ProcessSSDVMessage(Channel, Message); @@ -1173,17 +1234,12 @@ int receiveMessage(int Channel, unsigned char *message) ShowPacketCounts(Channel); } else - { + { int8_t SNR; int RSSI; currentAddr = readRegister(Channel, REG_FIFO_RX_CURRENT_ADDR); - // LogMessage("currentAddr = %d\n", currentAddr); Bytes = readRegister(Channel, REG_RX_NB_BYTES); - // LogMessage("%d bytes in packet\n", Bytes); - - // LogMessage("RSSI = %d\n", readRegister(Channel, REG_PACKET_RSSI) - 137); - SNR = readRegister(Channel, REG_PACKET_SNR); SNR /= 4; @@ -1192,6 +1248,7 @@ int receiveMessage(int Channel, unsigned char *message) { RSSI += SNR; } + ChannelPrintf(Channel, 10, 1, "Packet SNR = %d, RSSI = %d ", (int)SNR, RSSI); FreqError = FrequencyError(Channel) / 1000; @@ -1209,6 +1266,8 @@ int receiveMessage(int Channel, unsigned char *message) message[Bytes] = '\0'; + LogPacket(Channel, SNR, RSSI, FreqError, Bytes, message[1]); + if(Config.LoRaDevices[Channel].AFC && (fabs(FreqError)>0.5)) { ReTune(Channel, FreqError/1000); @@ -1299,6 +1358,7 @@ void LoadConfigFile() Config.EnableHabitat = 1; Config.EnableSSDV = 1; Config.EnableTelemetryLogging = 0; + Config.EnablePacketLogging = 0; Config.SSDVJpegFolder[0] = '\0'; Config.ftpServer[0] = '\0'; Config.ftpUser[0] = '\0'; @@ -1307,6 +1367,7 @@ void LoadConfigFile() Config.latitude = -999; Config.longitude = -999; Config.antenna[0] = '\0'; + Config.EnableDev = 0; if ((fp = fopen(filename, "r")) == NULL) { @@ -1322,8 +1383,11 @@ void LoadConfigFile() ReadBoolean(fp, "EnableHabitat", 0, &Config.EnableHabitat); ReadBoolean(fp, "EnableSSDV", 0, &Config.EnableSSDV); - // Enable logging + // Enable telemetry logging ReadBoolean(fp, "LogTelemetry", 0, &Config.EnableTelemetryLogging); + + // Enable packet logging + ReadBoolean(fp, "LogPackets", 0, &Config.EnablePacketLogging); // Calling mode Config.CallingTimeout = ReadInteger(fp, "CallingTimeout", 0, 300); @@ -1361,6 +1425,9 @@ void LoadConfigFile() Config.longitude = ReadFloat(fp, "Longitude"); ReadString(fp, "antenna", Config.antenna, sizeof(Config.antenna), 0); + // Dev mode + ReadBoolean(fp, "EnableDev", 0, &Config.EnableDev); + // SMS upload to tracker Config.SMSFolder[0] = '\0'; ReadString(fp, "SMSFolder", Config.SMSFolder, sizeof(Config.SMSFolder), 0); diff --git a/gateway.txt b/gateway.txt index b8cc305..750ea3f 100644 --- a/gateway.txt +++ b/gateway.txt @@ -1,27 +1,43 @@ -tracker=M0RPI/B +tracker=M0RPI/5 EnableHabitat=Y -EnableSSDV=N +EnableSSDV=Y JPGFolder=ssdv LogTelemetry=Y +LogPackets=Y CallingTimeout=60 ServerPort=6004 Latitude=51.95023 Longitude=-2.5445 -SMSFolder=tweets/ +Antenna=868MHz Yagi +#SMSFolder=./ +EnableDev=Y -#NetworkLED=22 -#InternetLED=23 -#ActivityLED_0=21 -#ActivityLED_1=29 +#NetworkLED=21 +#InternetLED=22 +#ActivityLED_0=23 +#ActivityLED_1=24 -#frequency_0=434.450 -#mode_1=1 -#DIO0_1=6 -#DIO5_1=5 -AFC_1=N +frequency_0=869.850 +mode_0=3 + +#frequency_0=869.500 +#mode_0= +#bandwidth_0=125K +#implicit_0=0 +#coding_0=5 +#sf_0=8 +#lowopt_0=0 +#power_0=255 +DIO0_0=31 +DIO5_0=26 +AFC_0=N +#UplinkTime_0=2 +#UplinkCycle_0=60 frequency_1=434.500 mode_1=1 -#DIO0_1=27 -#DIO5_1=26 -AFC_1=Y +DIO0_1=6 +DIO5_1=5 +#AFC_1=Y +#UplinkTime_1=5 +#UplinkCycle_1=60 diff --git a/global.h b/global.h index 9923147..239a780 100644 --- a/global.h +++ b/global.h @@ -59,6 +59,7 @@ struct TLoRaDevice char Payload[16], Time[12]; unsigned int Counter, LastCounter; unsigned long Seconds; + double PredictedLongitude, PredictedLatitude; double Longitude, Latitude; unsigned int Altitude, PreviousAltitude; unsigned int Satellites; @@ -68,6 +69,9 @@ struct TLoRaDevice time_t ReturnToCallingModeAt; int InCallingMode; int ActivityLED; + + int Speed, Heading, PredictedTime, CompassActual, CompassTarget, AirDirection, ServoLeft, ServoRight, ServoTime, FlightMode; + double cda, PredictedLandingSpeed, AirSpeed, GlideRatio; // Normal (non TDM) uplink int UplinkTime; @@ -83,6 +87,7 @@ struct TConfig int EnableHabitat; int EnableSSDV; int EnableTelemetryLogging; + int EnablePacketLogging; int CallingTimeout; char SSDVJpegFolder[100]; char ftpServer[100]; @@ -96,6 +101,7 @@ struct TConfig float latitude, longitude; char SMSFolder[64]; char antenna[64]; + int EnableDev; }; extern struct TConfig Config; diff --git a/server.c b/server.c index 42c39bb..adb7167 100644 --- a/server.c +++ b/server.c @@ -69,15 +69,45 @@ void *ServerLoop(void *some_void_ptr) // sprintf(sendBuff, "{\"class\":\"POSN\",\"time\":\"12:34:56\",\"lat\":54.12345,\"lon\":-2.12345,\"alt\":169}\r\n"); Channel = 1; - sprintf(sendBuff, "{\"class\":\"POSN\",\"payload\":\"%s\",\"time\":\"%s\",\"lat\":%.5lf,\"lon\":%.5lf,\"alt\":%d,\"rate\":%.1lf}\r\n", - Config.LoRaDevices[Channel].Payload, - Config.LoRaDevices[Channel].Time, - Config.LoRaDevices[Channel].Latitude, - Config.LoRaDevices[Channel].Longitude, - Config.LoRaDevices[Channel].Altitude, - Config.LoRaDevices[Channel].AscentRate); - + if (Config.EnableDev) + { + sprintf(sendBuff, "{\"class\":\"POSN\",\"payload\":\"%s\",\"time\":\"%s\",\"lat\":%.5lf,\"lon\":%.5lf,\"alt\":%d,\"predlat\":%.5lf,\"predlon\":%.5lf,\"speed\":%d," + "\"head\":%d,\"cda\":%.2lf,\"pls\":%.1lf,\"pt\":%d,\"ca\":%d,\"ct\":%d,\"as\":%.1lf,\"ad\":%d,\"sl\":%d,\"sr\":%d,\"st\":%d,\"gr\":%.2lf,\"fm\":%d}\r\n", + Config.LoRaDevices[Channel].Payload, + Config.LoRaDevices[Channel].Time, + Config.LoRaDevices[Channel].Latitude, + Config.LoRaDevices[Channel].Longitude, + Config.LoRaDevices[Channel].Altitude, + Config.LoRaDevices[Channel].PredictedLatitude, + Config.LoRaDevices[Channel].PredictedLongitude, + Config.LoRaDevices[Channel].Speed, + + Config.LoRaDevices[Channel].Heading, + Config.LoRaDevices[Channel].cda, + Config.LoRaDevices[Channel].PredictedLandingSpeed, + Config.LoRaDevices[Channel].PredictedTime, + Config.LoRaDevices[Channel].CompassActual, + Config.LoRaDevices[Channel].CompassTarget, + Config.LoRaDevices[Channel].AirSpeed, + Config.LoRaDevices[Channel].AirDirection, + Config.LoRaDevices[Channel].ServoLeft, + Config.LoRaDevices[Channel].ServoRight, + Config.LoRaDevices[Channel].ServoTime, + Config.LoRaDevices[Channel].GlideRatio, + Config.LoRaDevices[Channel].FlightMode); + } + else + { + sprintf(sendBuff, "{\"class\":\"POSN\",\"payload\":\"%s\",\"time\":\"%s\",\"lat\":%.5lf,\"lon\":%.5lf,\"alt\":%d,\"rate\":%.1lf}\r\n", + Config.LoRaDevices[Channel].Payload, + Config.LoRaDevices[Channel].Time, + Config.LoRaDevices[Channel].Latitude, + Config.LoRaDevices[Channel].Longitude, + Config.LoRaDevices[Channel].Altitude, + Config.LoRaDevices[Channel].AscentRate); + } + if (!run) { port_closed = 1;