forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrivacy.cs
177 lines (156 loc) · 8.44 KB
/
Privacy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using MissionPlanner.Comms;
namespace MissionPlanner.Utilities
{
public class Privacy
{
public static void anonymise(string logfile, string outputfile)
{
if (!File.Exists(logfile))
return;
var rand = new Random();
var random = (rand.NextDouble() * 10);
// TLOG
if (logfile.ToLower().EndsWith(".tlog"))
{
Comms.CommsFile tlogFile = new CommsFile();
tlogFile.Open(logfile);
using (var stream = new CommsStream(tlogFile, tlogFile.BytesToRead))
using (var outfilestream = File.Open(outputfile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
{
MAVLink.MavlinkParse parse = new MAVLink.MavlinkParse(true);
var block = new Type[]
{
typeof(MAVLink.mavlink_fence_point_t),
typeof(MAVLink.mavlink_simstate_t),
typeof(MAVLink.mavlink_rally_point_t),
typeof(MAVLink.mavlink_ahrs2_t),
typeof(MAVLink.mavlink_camera_feedback_t),
typeof(MAVLink.mavlink_ahrs3_t),
typeof(MAVLink.mavlink_deepstall_t),
typeof(MAVLink.mavlink_gps_raw_int_t),
typeof(MAVLink.mavlink_global_position_int_t),
typeof(MAVLink.mavlink_set_gps_global_origin_t),
typeof(MAVLink.mavlink_gps_global_origin_t),
typeof(MAVLink.mavlink_global_position_int_cov_t),
typeof(MAVLink.mavlink_set_position_target_global_int_t),
typeof(MAVLink.mavlink_hil_state_t),
typeof(MAVLink.mavlink_sim_state_t),
typeof(MAVLink.mavlink_hil_gps_t),
typeof(MAVLink.mavlink_hil_state_quaternion_t),
typeof(MAVLink.mavlink_gps2_raw_t),
typeof(MAVLink.mavlink_terrain_request_t),
typeof(MAVLink.mavlink_terrain_check_t),
typeof(MAVLink.mavlink_terrain_report_t),
typeof(MAVLink.mavlink_follow_target_t),
typeof(MAVLink.mavlink_gps_input_t),
typeof(MAVLink.mavlink_high_latency_t),
typeof(MAVLink.mavlink_home_position_t),
typeof(MAVLink.mavlink_set_home_position_t),
typeof(MAVLink.mavlink_adsb_vehicle_t),
typeof(MAVLink.mavlink_camera_image_captured_t),
typeof(MAVLink.mavlink_uavionix_adsb_out_dynamic_t),
typeof(MAVLink.mavlink_global_position_int_t),
typeof(MAVLink.mavlink_set_home_position_t),
typeof(MAVLink.mavlink_home_position_t),
typeof(MAVLink.mavlink_set_position_target_global_int_t),
typeof(MAVLink.mavlink_local_position_ned_t),
typeof(MAVLink.mavlink_command_long_t),
typeof(MAVLink.mavlink_mission_item_t),
typeof(MAVLink.mavlink_mission_item_int_t),
typeof(MAVLink.mavlink_uavionix_adsb_out_cfg_t)
};
var checks = new string[]
{
"lat", "latitude", "lat_int", "landing_lat",
"path_lat", "arc_entry_lat", "gpsLat", "gpsOffsetLat",
"lon", "longitude", "lon_int", "landing_lon",
"path_lon", "arc_entry_lon", "gpsLon", "gpsOffsetLon"
};
while (stream.Position < stream.Length)
{
var packet = parse.ReadPacket(stream);
if (packet == null)
continue;
var msginfo = MAVLink.MAVLINK_MESSAGE_INFOS.GetMessageInfo(packet.msgid);
if (block.Contains(msginfo.type))
{
bool valid = false;
var oldrxtime = packet.rxtime;
foreach (var check in checks)
{
var field = msginfo.type.GetField(check);
if (field != null)
{
var pkt = packet.data;
var value = field.GetValue(pkt);
if (value is Int32)
{
if ((int) value != 0)
field.SetValue(pkt, (int) ((int) value + random * 1e7));
packet = new MAVLink.MAVLinkMessage(
parse.GenerateMAVLinkPacket20((MAVLink.MAVLINK_MSG_ID) msginfo.msgid,
pkt, false, packet.sysid, packet.compid, packet.seq));
valid = true;
}
else if (value is Single)
{
if ((Single) value != 0)
field.SetValue(pkt, (Single) value + random);
packet = new MAVLink.MAVLinkMessage(
parse.GenerateMAVLinkPacket20((MAVLink.MAVLINK_MSG_ID) msginfo.msgid,
pkt, false, packet.sysid, packet.compid, packet.seq));
valid = true;
}
else
{
continue;
}
}
}
packet.rxtime = oldrxtime;
}
byte[] datearray =
BitConverter.GetBytes(
(UInt64) ((packet.rxtime.ToUniversalTime() - new DateTime(1970, 1, 1))
.TotalMilliseconds * 1000));
Array.Reverse(datearray);
outfilestream.Write(datearray, 0, datearray.Length);
outfilestream.Write(packet.buffer, 0, packet.Length);
}
}
}
else //LOG
{
using (DFLogBuffer col = new DFLogBuffer(File.OpenRead(logfile)))
using (var outfilestream = File.Open(outputfile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
{
foreach (var dfItem in col.GetEnumeratorTypeAll())
{
var index = col.dflog.FindMessageOffset(dfItem.msgtype, "Lat");
var index2 = col.dflog.FindMessageOffset(dfItem.msgtype, "Lng");
if (index != -1)
{
var lat = Double.Parse(dfItem.items[index], CultureInfo.InvariantCulture);
if (lat != 0)
dfItem.items[index] =(lat + random).ToString(CultureInfo.InvariantCulture);
}
if (index2 != -1)
{
var lon = Double.Parse(dfItem.items[index2], CultureInfo.InvariantCulture);
if (lon != 0)
dfItem.items[index2] =(lon + random).ToString(CultureInfo.InvariantCulture);
}
var str = String.Join(",", dfItem.items) + "\r\n";
outfilestream.Write(ASCIIEncoding.ASCII.GetBytes(str), 0, str.Length);
}
}
}
}
}
}