s
yy1717
2020-01-10 91534e59ac99449404bc9ad96696662044dd0afc
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//
// Created by YY on 2020/1/7.
//
 
#include "comm_if.h"
#include "../native-lib.h"
 
#include "../rapidjson/document.h"
#include "../rapidjson/writer.h"
#include "../rapidjson/stringbuffer.h"
#include "../rtk_platform/platform.h"
#include "../utils/xconvert.h"
#include "../driver_test.h"
 
#include <string>
#include <iostream>
#include <vector>
 
using namespace rapidjson;
using namespace std;
 
#define ID_SM_NDK_START         0x0001
#define ID_SM_REQ_RTK_PLAT_CFG         0x0002
#define ID_MS_RTK_PLAT_CFG         0x8002
#define ID_SM_RTK_PLAT_CONN     0x0003
#define ID_SM_RTK_PLAT_REG     0x0004
#define ID_SM_RTK_PLAT_LOGIN     0x0005
#define ID_SM_READ_MAP          0x0006
#define ID_MS_MAP               0x8006
#define ID_SM_READ_CAR          0x0007
#define ID_MS_CAR               0x8007
#define ID_SM_READ_SENSOR_CFG   0x0008
#define ID_MS_SENSOR_CFG        0x8008
#define ID_MS_START_EXAM        0x8009
#define ID_SM_EXAM_STATUS       0x0009
#define ID_SM_GPS_BRIEF         0x000A
#define ID_SM_RTK_BRIEF         0x000B
#define ID_SM_EXAM_BRIEF         0x000C
 
void MA_NdkStart(void)
{
    SendMsgToMainProc(ID_SM_NDK_START, NULL);
}
 
void MA_ReqRtkPlatformConfig(void)
{
    SendMsgToMainProc(ID_SM_REQ_RTK_PLAT_CFG, NULL);
}
 
void MA_RtkPlatformConnect(int conn, const char *ip, int port)
{
    StringBuffer sb;
    Writer<StringBuffer> writer(sb);
 
    writer.StartObject();
    writer.Key("connected");
    writer.Int(conn);
    writer.Key("ip");
    writer.String(ip);
    writer.Key("port");
    writer.Int(port);
 
    writer.EndObject();
 
    SendMsgToMainProc(ID_SM_RTK_PLAT_CONN, sb.GetString());
}
 
void MA_RtkPlatformRegister(int reg, const uint8_t *pwd, int length)
{
    StringBuffer sb;
    Writer<StringBuffer> writer(sb);
 
    writer.StartObject();
    writer.Key("register_code");
    writer.Int(reg);
 
    if (reg == 0 && length > 0) {
        writer.Key("password");
        char *str = new char[length*2 + 1];
        ConvertHex2String(str, pwd, length);
        writer.String(str);
        delete[]str;
    }
 
    writer.EndObject();
 
    SendMsgToMainProc(ID_SM_RTK_PLAT_REG, sb.GetString());
}
 
void MA_RtkPlatformLogin(int login)
{
    StringBuffer sb;
    Writer<StringBuffer> writer(sb);
 
    writer.StartObject();
 
    writer.Key("login_code");
    writer.Int(login);
 
    writer.EndObject();
 
    SendMsgToMainProc(ID_SM_RTK_PLAT_LOGIN, sb.GetString());
}
 
void MA_ReadMap(void)
{
    SendMsgToMainProc(ID_SM_READ_MAP, NULL);
}
 
void MA_ReadCar(void)
{
    SendMsgToMainProc(ID_SM_READ_CAR, NULL);
}
 
void MA_ReadSensor(void)
{
    SendMsgToMainProc(ID_SM_READ_SENSOR_CFG, NULL);
}
 
void MA_SendExamStatus(int start, int errorCode)
{
    StringBuffer sb;
    Writer<StringBuffer> writer(sb);
 
    writer.StartObject();
 
    writer.Key("exam");
    writer.Int(start);
    writer.Key("error");
    writer.Int(errorCode);
 
    writer.EndObject();
 
    SendMsgToMainProc(ID_SM_EXAM_STATUS, sb.GetString());
}
 
void MA_SendGpsBrief(const struct gpsBrief *brief)
{
    StringBuffer sb;
    Writer<StringBuffer> writer(sb);
 
    writer.StartObject();
 
    writer.Key("utc");
    writer.String(brief->utc);
    writer.Key("sat_num");
    writer.Int(brief->sat_num);
    writer.Key("qf");
    writer.Int(brief->qf);
    writer.Key("latitude");
    writer.Double(brief->latitude);
    writer.Key("longitude");
    writer.Double(brief->longitude);
    writer.Key("altitude");
    writer.Double(brief->altitude);
    writer.Key("speed");
    writer.Double(brief->speed);
 
    writer.EndObject();
 
    SendMsgToMainProc(ID_SM_GPS_BRIEF, sb.GetString());
}
 
void MA_SendRtkBrief(const struct rtkBrief *brief)
{
    char a[2] = {brief->coord_x_dir, 0};
    char b[2] = {brief->coord_y_dir, 0};
 
    StringBuffer sb;
    Writer<StringBuffer> writer(sb);
 
    writer.StartObject();
 
    writer.Key("utc");
    writer.String(brief->utc);
    writer.Key("qf");
    writer.Int(brief->qf);
    writer.Key("coord_x");
    writer.Double(brief->coord_x);
    writer.Key("coord_x_dir");
    writer.String(a);
    writer.Key("coord_y");
    writer.Double(brief->coord_y);
    writer.Key("coord_y_dir");
    writer.String(b);
    writer.Key("heading");
    writer.Double(brief->heading);
    writer.Key("pitch");
    writer.Double(brief->pitch);
    writer.Key("roll");
    writer.Double(brief->roll);
    writer.EndObject();
 
    SendMsgToMainProc(ID_SM_RTK_BRIEF, sb.GetString());
}
 
void MA_SendExamWrong(vector<int>&err)
{
 
}
 
void MA_MainProcMsgEntry(int cmd, const char *value)
{
    switch (cmd) {
        case ID_MS_RTK_PLAT_CFG: {
            rtk_platform_cfg_t cfg;
            memset(&cfg, 0, sizeof(cfg));
 
            Document d;
            d.Parse(value);
 
            if (d.HasMember("ip")) {
                Value& s = d["ip"];
                strcpy(cfg.domain_name, s.GetString());
            }
            if (d.HasMember("port")) {
                Value& s = d["port"];
                cfg.port = s.GetInt();
            }
            if (d.HasMember("province")) {
                Value& s = d["province"];
                cfg.province = s.GetInt();
            }
            if (d.HasMember("city")) {
                Value& s = d["city"];
                cfg.city = s.GetInt();
            }
            if (d.HasMember("model")) {
                Value& s = d["model"];
                strcpy(cfg.device_model, s.GetString());
            }
            if (d.HasMember("sn")) {
                Value& s = d["sn"];
                strcpy(cfg.device_sn, s.GetString());
            }
            if (d.HasMember("imei")) {
                Value& s = d["imei"];
                strcpy(cfg.imei, s.GetString());
            }
            if (d.HasMember("phone")) {
                Value& s = d["phone"];
                strcpy(cfg.phone, s.GetString());
            }
            if (d.HasMember("password")) {
                Value& s = d["password"];
                strcpy(cfg.password, s.GetString());
            }
            if (d.HasMember("registered")) {
                Value& s = d["registered"];
                cfg.registered = s.GetInt();
            }
            if (d.HasMember("interval")) {
                Value& s = d["interval"];
                cfg.rtk_interval = s.GetInt();
            }
 
            ConfigPlatform(&cfg);
            break;
        }
        case ID_MS_MAP: {
            Document doc;
            doc.Parse(value);
            if (!doc.HasParseError()) {
                ClearMap();
 
                const Value &a = doc.GetArray();
 
                if (a.IsArray()) {
                    for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) {
                        // a Map
                        int id, type, pointNum = 0, point2Num = 0;
                        double (*map)[2] = NULL, (*map2)[2] = NULL;
 
                        if (itr->IsObject()) {
                            if (itr->HasMember("id")) {
                                const Value &s = (*itr)["id"];
                                id = s.GetInt();
                            }
                            if (itr->HasMember("item")) {
                                const Value &s = (*itr)["item"];
                                type = s.GetInt();
                            }
                            if (itr->HasMember("point")) {
                                const Value &s = (*itr)["point"];
                                int map_index = 0;
 
                                for (Value::ConstValueIterator itr2 = s.Begin();
                                     itr2 != s.End(); ++itr2, ++map_index) {
                                    // 曲线驾驶有2组
                                    const Value &s2 = (*itr2)["x-y"];
 
                                    if (map_index == 0) {
                                        int i = 0, j = 0;
                                        pointNum = s2.Size()/2;
                                        map = new double[pointNum][2];
                                        for (Value::ConstValueIterator itr3 = s2.Begin();
                                             itr3 != s2.End(); ++itr3) {
                                            map[i][j] = (*itr3).GetDouble();
                                            if (++j == 2) {
                                                j = 0;
                                                i++;
                                            }
                                        }
                                    } else if (map_index == 1) {
                                        int i = 0, j = 0;
                                        point2Num = s2.Size()/2;
                                        map2 = new double[s2.Size()][2];
                                        for (Value::ConstValueIterator itr3 = s2.Begin();
                                             itr3 != s2.End(); ++itr3) {
                                            map2[i][j] = (*itr3).GetDouble();
                                            if (++j == 2) {
                                                j = 0;
                                                i++;
                                            }
                                        }
                                    }
                                }
                            }
 
                            AddMap(id, type, map, pointNum, map2, point2Num);
                            if (map) delete []map;
                            if (map2) delete []map2;
                        }
                    }
                }
            }
            break;
        }
        case ID_MS_CAR: {
            Document doc;
 
            doc.Parse(value);
 
            if (!doc.HasParseError()) {
                double basePoint[2];
                int axial[2], left_front_tire[2], right_front_tire[2], left_rear_tire[2], right_rear_tire[2];
                int pointNum = 0;
                double (*point)[2] = NULL;
 
                if (doc.HasMember("main_ant")) {
                    const Value& s = doc["main_ant"];
                    if (s.IsArray()) {
                        int i = 0;
                        for(Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr) {
                            if (i < 2) basePoint[i++] = itr->GetDouble();
                        }
                    }
                }
 
                if (doc.HasMember("axial")) {
                    const Value& s = doc["axial"];
                    if (s.IsArray()) {
                        int i = 0;
                        for(Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr) {
                            if (i < 2) axial[i++] = itr->GetInt();
                        }
                    }
                }
 
                if (doc.HasMember("left_front_tire")) {
                    const Value& s = doc["left_front_tire"];
                    if (s.IsArray()) {
                        int i = 0;
                        for(Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr) {
                            if (i < 2) left_front_tire[i++] = itr->GetInt();
                        }
                    }
                }
 
                if (doc.HasMember("right_front_tire")) {
                    const Value& s = doc["right_front_tire"];
                    if (s.IsArray()) {
                        int i = 0;
                        for(Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr) {
                            if (i < 2) right_front_tire[i++] = itr->GetInt();
                        }
                    }
                }
 
                if (doc.HasMember("left_rear_tire")) {
                    const Value& s = doc["left_rear_tire"];
                    if (s.IsArray()) {
                        int i = 0;
                        for(Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr) {
                            if (i < 2) left_rear_tire[i++] = itr->GetInt();
                        }
                    }
                }
 
                if (doc.HasMember("right_rear_tire")) {
                    const Value& s = doc["right_rear_tire"];
                    if (s.IsArray()) {
                        int i = 0;
                        for(Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr) {
                            if (i < 2) right_rear_tire[i++] = itr->GetInt();
                        }
                    }
                }
 
                if (doc.HasMember("point")) {
                    const Value& s = doc["point"];
                    if (s.IsArray()) {
                        int i = 0, j = 0;
                        pointNum = s.Size()/2;
                        point = new double[pointNum][2];
 
                        for(Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr) {
                            point[i][j] = itr->GetDouble();
                            if (++j == 2) {
                                j = 0;
                                i++;
                            }
                        }
                    }
                }
 
                SetCarMeasurePoint(basePoint, axial, left_front_tire, right_front_tire,
                        left_rear_tire, right_rear_tire, point, pointNum);
 
                if (point != NULL) delete []point;
            }
            break;
        }
        case ID_MS_SENSOR_CFG: {
            Document doc;
            doc.Parse(value);
            if (!doc.HasParseError()) {
                const Value &a = doc.GetArray();
 
                if (a.IsArray() && a.Size() > 0) {
                    int n = a.Size();
                    int i = 0;
                    int (*sensor)[2] = new int[n][2];
 
                    for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) {
                        // a gpio mapping
                        if (itr->HasMember("gpio_num")) {
                            const Value &s = (*itr)["gpio_num"];
                            sensor[i][0] = s.GetInt();
                        }
                        if (itr->HasMember("func_id")) {
                            const Value &s = (*itr)["func_id"];
                            sensor[i][1] = s.GetInt();
                        }
                        ++i;
                    }
                    SetSensorCfg(sensor, n);
                    delete []sensor;
                }
            }
            break;
        }
        case ID_MS_START_EXAM: {
            Document doc;
            doc.Parse(value);
            if (!doc.HasParseError()) {
                if (doc.HasMember("exam")) {
                    Value& s = doc["exam"];
                    StartDriverExam(s.GetInt());
                }
            }
            break;
        }
        default:break;
    }
}