// // 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 "../defs.h" #include "../common/apptimer.h" #include "../jni_log.h" #include "../mcu/mcu_if.h" #include "../test_common/car_sensor.h" #include #include #include #include #include #define DEBUG(fmt, args...) LOGD(" <%s>: " fmt, __func__, ##args) using namespace rapidjson; using namespace std; #define ID_SM_NDK_START 0x0001 #define ID_MS_NDK_ACK 0x8001 #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_MS_ROAD_MAP 0x8014 #define ID_MS_ROAD_MAP2 0x8013 #define ID_SM_READ_CAR 0x0007 #define ID_MS_CAR 0x8007 #define ID_SM_MCU_BRIEF 0x0008 #define ID_MS_SENSOR_CFG 0x8008 #define ID_MS_START_EXAM 0x8009 #define ID_SM_EXAM_STATUS 0x0009 #define ID_MS_IND_ONOFF 0x800A #define ID_SM_GPS_BRIEF 0x000A #define ID_SM_RTK_BRIEF 0x000B #define ID_SM_EXAM_BRIEF 0x000C #define ID_SM_ENTER_MAP 0x000D #define ID_MS_EXAM_MAP 0x800D #define ID_SM_CAR 0x000E #define ID_SM_RTCM_IND 0x000F #define ID_SM_DEBUG_INFO 0x0010 #define ID_MS_FILE 0x8100 #define ID_MS_READ_CARD 0x8011 #define ID_SM_PUT_CARD 0x0011 #define ID_MS_SYS_SHUTDOWN 0x8010 #define ID_SM_LIGHT_EXAM_REQ 0x0012 #define ID_MS_LIGHT_EXAM_RES 0x8012 #define ID_SM_DISTANCE 0x0020 #define ID_SM_CARSENSOR 0x0013 #define ID_MS_ROAD_BRIEF 0x0015 // 科目三项目标定,进出某条路的提示 #define ID_MS_CROSSING_BRIEF 0x0016 // 科目三项目标定,接近/驶离某各路口的提示 #define MA_OUT_GPS_BRIEF 0x0001 #define MA_OUT_RTK_BRIEF 0x0002 #define MA_OUT_CAR_BRIEF 0x0004 #define MA_OUT_RTCM_IND 0x0008 #define MA_OUT_DBG_INFO 0x0010 static int OnOff = 0;//0xFFFF; struct msg_2_main_t { int cmd; string value; }; static list MessageBuffer; static sem_t sem_msg_income; static pthread_mutex_t msg_mutex = PTHREAD_MUTEX_INITIALIZER; static void SendMsgToMainProcIndep(int cmd, const char *value); static void *SendMsgToMainProcThread(void *p); static void SendMsgToMainProcIndep(int cmd, const char *value) { struct msg_2_main_t msg; msg.cmd = cmd; if (value != NULL) { msg.value = value; } else { msg.value.clear(); } pthread_mutex_lock(&msg_mutex); MessageBuffer.push_front(msg); pthread_mutex_unlock(&msg_mutex); sem_post(&sem_msg_income); } static void *SendMsgToMainProcThread(void *p) { while (true) { sem_wait(&sem_msg_income); if (MessageBuffer.size() > 0) { struct msg_2_main_t msg; pthread_mutex_lock(&msg_mutex); msg = MessageBuffer.back(); MessageBuffer.pop_back(); pthread_mutex_unlock(&msg_mutex); if (msg.value.length() > 0) SendMsgToMainProc(msg.cmd, msg.value.c_str()); else SendMsgToMainProc(msg.cmd, NULL); } } } void MA_Init(void) { sem_init(&sem_msg_income, 0, 0); MessageBuffer.clear(); pthread_mutex_init(&msg_mutex, NULL); pthread_t pid; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_create(&pid, &attr, SendMsgToMainProcThread, NULL); } void MA_NdkStart(void) { SendMsgToMainProcIndep(ID_SM_NDK_START, NULL); } void MA_ReqRtkPlatformConfig(void) { SendMsgToMainProcIndep(ID_SM_REQ_RTK_PLAT_CFG, NULL); } void MA_RtkPlatformConnect(int conn, const char *ip, int port) { StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("connected"); writer.Int(conn); writer.Key("ip"); writer.String(ip); writer.Key("port"); writer.Int(port); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_RTK_PLAT_CONN, sb.GetString()); } void MA_RtkPlatformRegister(int reg, const uint8_t *pwd, int length) { StringBuffer sb; Writer 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(); SendMsgToMainProcIndep(ID_SM_RTK_PLAT_REG, sb.GetString()); } void MA_RtkPlatformLogin(int login) { StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("login_code"); writer.Int(login); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_RTK_PLAT_LOGIN, sb.GetString()); } void MA_ReadMap(void) { SendMsgToMainProcIndep(ID_SM_READ_MAP, NULL); } void MA_ReadCar(void) { SendMsgToMainProcIndep(ID_SM_READ_CAR, NULL); } void MA_ReadSensor(void) { ReadCard(); } void MA_SendExamStatus(int start, int errorCode) { StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("exam"); writer.Int(start); writer.Key("error"); writer.Int(errorCode); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_EXAM_STATUS, sb.GetString()); } void MA_SendCardBrief(const struct cardBrief *brief) { StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("result"); writer.Int(brief->result); writer.Key("serialno"); writer.String(brief->card); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_PUT_CARD, sb.GetString()); } void MA_SendMcuBrief(const struct mcuBrief *brief) { StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("version"); writer.String(brief->version); writer.Key("selftest"); writer.Int(brief->selftest); // writer.Key("gpio"); // writer.Int(brief->gpio); // writer.Key("speed"); // writer.Int(brief->speed); // writer.Key("engine"); // writer.Int(brief->engine); writer.Key("sn"); writer.String(brief->sn); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_MCU_BRIEF, sb.GetString()); } void MA_SendCarSensorBrief(const struct carSensorBrief *brief) { StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("odo"); writer.Int(brief->odo); writer.Key("trip"); writer.Int(brief->trip); writer.Key("trip_time"); writer.Int(brief->tripTime); writer.Key("cell_volt"); writer.Double(brief->cellVolt); writer.Key("speed"); writer.Double(brief->speed); writer.Key("engine"); writer.Int(brief->engine); writer.Key("sas"); writer.Int(brief->sas); writer.Key("key"); writer.Int(brief->key); writer.Key("gear"); writer.Int(brief->gear); writer.Key("aps"); writer.Int(brief->aps); writer.Key("lock"); writer.Int(brief->door); writer.Key("seat_belt"); writer.Int(brief->seatBelt); writer.Key("clutch"); writer.Int(brief->clutch); writer.Key("horn"); writer.Int(brief->horn); writer.Key("wiper"); writer.Int(brief->wiper); writer.Key("hand_break"); writer.Int(brief->handBreak); writer.Key("main_break"); writer.Int(brief->mainBreak); writer.Key("left_turn_lamp"); writer.Int(brief->leftTurnLamp); writer.Key("right_turn_lamp"); writer.Int(brief->rightTurnLamp); writer.Key("clearance_lamp"); writer.Int(brief->clearanceLamp); writer.Key("dipped_beam_lamp"); writer.Int(brief->dippedBeamLamp); writer.Key("main_beam_lamp"); writer.Int(brief->mainBeamLamp); writer.Key("fog_lamp"); writer.Int(brief->fogLamp); writer.Key("assist_break"); writer.Int(brief->assBreak); writer.Key("surround1"); writer.Int(brief->surround1); writer.Key("surround2"); writer.Int(brief->surround2); writer.Key("surround3"); writer.Int(brief->surround3); writer.Key("surround4"); writer.Int(brief->surround4); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_CARSENSOR, sb.GetString()); } void MA_SendGpsBrief(const struct gpsBrief *brief) { if (!(OnOff & MA_OUT_GPS_BRIEF)) return; StringBuffer sb; Writer 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(); SendMsgToMainProcIndep(ID_SM_GPS_BRIEF, sb.GetString()); } void MA_SendRtkBrief(const struct rtkBrief *brief) { if (!(OnOff & MA_OUT_RTK_BRIEF)) return; char a[2] = {brief->coord_x_dir, 0}; char b[2] = {brief->coord_y_dir, 0}; StringBuffer sb; Writer 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.Key("sat_num"); writer.Int(brief->sat_num); 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.Key("track_ture"); writer.Double(brief->trackTure); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_RTK_BRIEF, sb.GetString()); } void MA_SendDistance(double l, double r) { StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("left"); writer.Double(l); writer.Key("right"); writer.Double(r); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_DISTANCE, sb.GetString()); } void MA_SendRoadStatus(const struct roadStatusBrief *brief) { StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("road_id"); writer.Int(brief->road_id); writer.Key("status"); writer.Int(brief->status); writer.EndObject(); SendMsgToMainProcIndep(ID_MS_ROAD_BRIEF, sb.GetString()); } void MA_SendCrossingStatus(const struct crossingStatusBrief *brief) { StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("road_id"); writer.Int(brief->road_id); writer.Key("crossing_index"); writer.Int(brief->crossing_index); writer.Key("status"); writer.Int(brief->status); writer.EndObject(); SendMsgToMainProcIndep(ID_MS_CROSSING_BRIEF, sb.GetString()); } void MA_SendExamWrong(vector &ExamFaultList) { StringBuffer sb; Writer writer(sb); writer.StartArray(); for (vector::iterator iter = ExamFaultList.begin(); iter != ExamFaultList.end(); ++iter) { writer.StartObject(); writer.Key("sn"); writer.Int(iter->sn); writer.Key("utc"); writer.String(iter->utc); writer.Key("wrong_id"); writer.Int(iter->wrong_id); writer.EndObject(); } writer.EndArray(); SendMsgToMainProcIndep(ID_SM_EXAM_BRIEF, sb.GetString()); } void MA_SendCarPosition(const struct carBrief *brief) { if (!(OnOff & MA_OUT_CAR_BRIEF)) return; StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("utc"); writer.String(brief->utc); writer.Key("qf"); writer.Int(brief->qf); writer.Key("map_id"); writer.Int(brief->map_id); writer.Key("move"); writer.Int(brief->move); writer.Key("speed"); writer.Double(brief->speed); writer.Key("heading"); writer.Double(brief->heading); writer.Key("main_ant"); writer.StartArray(); writer.Double(brief->main_ant[0]); writer.Double(brief->main_ant[1]); writer.EndArray(); writer.Key("axial"); writer.StartArray(); writer.Int(brief->axial[0]); writer.Int(brief->axial[1]); writer.EndArray(); writer.Key("left_front_tire"); writer.StartArray(); writer.Int(brief->left_front_tire[0]); writer.Int(brief->left_front_tire[1]); writer.EndArray(); writer.Key("right_front_tire"); writer.StartArray(); writer.Int(brief->right_front_tire[0]); writer.Int(brief->right_front_tire[1]); writer.EndArray(); writer.Key("left_rear_tire"); writer.StartArray(); writer.Int(brief->left_rear_tire[0]); writer.Int(brief->left_rear_tire[1]); writer.EndArray(); writer.Key("right_rear_tire"); writer.StartArray(); writer.Int(brief->right_rear_tire[0]); writer.Int(brief->right_rear_tire[1]); writer.EndArray(); writer.Key("body"); writer.StartArray(); for (int i = 0; i < brief->bodyNum; ++i) { writer.Int(brief->body[i]); } writer.EndArray(); writer.Key("point"); writer.StartArray(); for (int i = 0; i < brief->pointNum; ++i) { writer.Double(brief->point[i*2]); writer.Double(brief->point[i*2+1]); } writer.EndArray(); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_CAR, sb.GetString()); } void MA_SendRtcmInd(int length) { if (!(OnOff & MA_OUT_RTCM_IND)) return; StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("length"); writer.Int(length); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_RTCM_IND, sb.GetString()); } void MA_SendDebugInfo(const char *str, ...) { if (!(OnOff & MA_OUT_DBG_INFO)) return; va_list argp; char buffer[1024]; va_start(argp, str); vsprintf(buffer, str, argp); va_end(argp); StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("info"); writer.String(buffer); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_DEBUG_INFO, sb.GetString()); } void MA_MainProcMsgEntry(int cmd, const char *value) { switch (cmd) { case ID_MS_RTK_PLAT_CFG: { if (OnOff == 0) OnOff = MA_OUT_GPS_BRIEF + MA_OUT_RTK_BRIEF + MA_OUT_CAR_BRIEF; 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_ROAD_MAP: { Document doc; doc.Parse(value); if (!doc.HasParseError()) { CleanRoadMap(); DEBUG("开始解析路考地图"); vector mapPoints; mapPoints.clear(); if (doc.HasMember("points")) { const Value &s = doc["points"]; DEBUG("得到所有点"); // X-Y坐标集合 for (Value::ConstValueIterator itr2 = s.Begin(); itr2 != s.End(); ++itr2) { mapPoints.push_back((*itr2).GetDouble()); } } if (doc.HasMember("maps")) { const Value &a = doc["maps"]; for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) { DEBUG("得到各子地图"); if (itr->IsObject()) { // a Map int id, type; int stop_flag = 0; string tts; vector> redLines; vector> greenLines; vector> triggerLines; vector> redAreas; vector> roadEdgeLines; vector area; vector stopLine; roadEdgeLines.clear(); stopLine.clear(); area.clear(); tts.clear(); redLines.clear(); greenLines.clear(); triggerLines.clear(); redAreas.clear(); vector points; if (itr->HasMember("red_line")) { const Value &s = (*itr)["red_line"]; for (Value::ConstValueIterator itrLine = s.Begin(); itrLine != s.End(); ++itrLine) { points.clear(); for (Value::ConstValueIterator itrPoint = (*itrLine).Begin(); itrPoint != (*itrLine).End(); ++itrPoint) { points.push_back((*itrPoint).GetInt()); } redLines.push_back(points); } } if (itr->HasMember("green_line")) { const Value &s = (*itr)["green_line"]; for (Value::ConstValueIterator itrLine = s.Begin(); itrLine != s.End(); ++itrLine) { points.clear(); for (Value::ConstValueIterator itrPoint = (*itrLine).Begin(); itrPoint != (*itrLine).End(); ++itrPoint) { points.push_back((*itrPoint).GetInt()); } greenLines.push_back(points); } } if (itr->HasMember("all_trigger_line")) { const Value &s = (*itr)["all_trigger_line"]; for (Value::ConstValueIterator itrLine = s.Begin(); itrLine != s.End(); ++itrLine) { points.clear(); for (Value::ConstValueIterator itrPoint = (*itrLine).Begin(); itrPoint != (*itrLine).End(); ++itrPoint) { points.push_back((*itrPoint).GetInt()); } triggerLines.push_back(points); } } if (itr->HasMember("red_area")) { const Value &s = (*itr)["red_area"]; for (Value::ConstValueIterator itrLine = s.Begin(); itrLine != s.End(); ++itrLine) { points.clear(); for (Value::ConstValueIterator itrPoint = (*itrLine).Begin(); itrPoint != (*itrLine).End(); ++itrPoint) { points.push_back((*itrPoint).GetInt()); } redAreas.push_back(points); } } if (itr->HasMember("road_edge_line")) { const Value &s = (*itr)["road_edge_line"]; for (Value::ConstValueIterator itrLine = s.Begin(); itrLine != s.End(); ++itrLine) { points.clear(); for (Value::ConstValueIterator itrPoint = (*itrLine).Begin(); itrPoint != (*itrLine).End(); ++itrPoint) { points.push_back((*itrPoint).GetInt()); } roadEdgeLines.push_back(points); } } if (itr->HasMember("area")) { const Value &s = (*itr)["area"]; for (Value::ConstValueIterator itrPoint = s.Begin(); itrPoint != s.End(); ++itrPoint) { area.push_back((*itrPoint).GetInt()); } } if (itr->HasMember("stop_line")) { const Value &s = (*itr)["stop_line"]; for (Value::ConstValueIterator itrPoint = s.Begin(); itrPoint != s.End(); ++itrPoint) { stopLine.push_back((*itrPoint).GetInt()); } } 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("stop_flag")) { const Value &s = (*itr)["stop_flag"]; stop_flag = s.GetInt(); } if (itr->HasMember("tts")) { const Value &s = (*itr)["tts"]; tts = s.GetString(); } } } } } else { DEBUG("############## 地图解析出错###################"); } break; } case ID_MS_ROAD_MAP2: { Document doc; doc.Parse(value); if (!doc.HasParseError()) { DEBUG("开始解析路考地图"); vector mapPoints; mapPoints.clear(); if (doc.HasMember("points")) { const Value &s = doc["points"]; DEBUG("得到所有点 共 %d", s.Size()/2); int n = 0; PointF temp; // X-Y坐标集合 for (Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr, ++n) { if ((n % 2) == 0) { temp.X = (*itr).GetDouble(); } else { temp.Y = (*itr).GetDouble(); mapPoints.push_back(temp); } } } road_exam_map map; if (doc.HasMember("road")) { const Value &a = doc["road"]; if (a.IsArray()) { for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) { road_t road; if (itr->HasMember("id")) { const Value &s = (*itr)["id"]; DEBUG("路id %d", s.GetInt()); road.id = s.GetInt(); } if (itr->HasMember("crossing")) { const Value &a2 = (*itr)["crossing"]; vector crossing; for (Value::ConstValueIterator itr2 = a2.Begin(); itr2 != a2.End(); ++itr2) { stop_line_t temp; if (!itr2->IsObject()) { break; } if (itr2->HasMember("stop_flag")) { const Value &s = (*itr2)["stop_flag"]; temp.stopFlag = s.GetInt(); DEBUG("路口停车 %d", temp.stopFlag); } else { temp.stopFlag = false; } if (itr2->HasMember("line")) { const Value &s = (*itr2)["line"]; PointF p1, p2; int n = 0; if (s.IsArray() && s.Size() >= 2) { for (Value::ConstValueIterator itr3 = s.Begin(); itr3 != s.End(); ++itr3, ++n) { if (n == 0) { p1 = mapPoints[(*itr3).GetInt()]; } else if (n == 1) { p2 = mapPoints[(*itr3).GetInt()]; } } MakeLine(&temp.line, &p1, &p2); } } if (itr2->HasMember("center_point")) { const Value &s = (*itr2)["center_point"]; temp.centrePoint = mapPoints[s.GetInt()]; temp.centrePointValid = true; } else { temp.centrePointValid = false; } crossing.push_back(temp); } road.stopLine.assign(crossing.begin(), crossing.end()); } if (itr->HasMember("next_road")) { const Value &s = (*itr)["next_road"]; road.targetRoad = s.GetInt(); } if (itr->HasMember("left_edge")) { const Value &a2 = (*itr)["left_edge"]; DEBUG("左边线"); if (a2.IsArray()) { for (Value::ConstValueIterator itr2 = a2.Begin(); itr2 != a2.End(); ++itr2) { edge_t edge; if (itr2->HasMember("type")) { const Value &s = (*itr2)["type"]; DEBUG("\t类型 %d", s.GetInt()); edge.character = s.GetInt(); } if (itr2->HasMember("line")) { const Value &a3 = (*itr2)["line"]; if (a3.IsArray()) { for (Value::ConstValueIterator itr3 = a3.Begin(); itr3 != a3.End(); ++itr3) { DEBUG("\t线点 %d", (*itr3).GetInt()); edge.points.push_back(mapPoints[(*itr3).GetInt()]); } } } road.leftEdge.push_back(edge); } } } if (itr->HasMember("right_edge")) { const Value &a2 = (*itr)["right_edge"]; DEBUG("右边线"); if (a2.IsArray()) { for (Value::ConstValueIterator itr2 = a2.Begin(); itr2 != a2.End(); ++itr2) { edge_t edge; if (itr2->HasMember("type")) { const Value &s = (*itr2)["type"]; DEBUG("\t类型 %d", s.GetInt()); edge.character = s.GetInt(); } if (itr2->HasMember("line")) { const Value &a3 = (*itr2)["line"]; if (a3.IsArray()) { for (Value::ConstValueIterator itr3 = a3.Begin(); itr3 != a3.End(); ++itr3) { DEBUG("\t线点 %d", (*itr3).GetInt()); edge.points.push_back(mapPoints[(*itr3).GetInt()]); } } } road.rightEdge.push_back(edge); } } } if (itr->HasMember("separate")) { const Value &a2 = (*itr)["separate"]; DEBUG("分道数量 %d", a2.Size()); for (Value::ConstValueIterator itr2 = a2.Begin(); itr2 != a2.End(); ++itr2) { separate_t sep; if (!itr2->IsObject()) break; if (itr2->HasMember("lane_guide")) { const Value &a3 = (*itr2)["lane_guide"]; for (Value::ConstValueIterator itr3 = a3.Begin(); itr3 != a3.End(); ++itr3) { lane_direct_t temp; if (itr3->HasMember("head_tail")) { const Value &a4 = (*itr3)["head_tail"]; int n = 0; for (Value::ConstValueIterator itr4 = a4.Begin(); itr4 != a4.End(); ++itr4, ++n) { if (n == 0) temp.start = mapPoints[(*itr4).GetInt()]; else if (n == 1) temp.end = mapPoints[(*itr4).GetInt()]; } } if (itr3->HasMember("guide")) { const Value &a4 = (*itr3)["guide"]; for (Value::ConstValueIterator itr4 = a4.Begin(); itr4 != a4.End(); ++itr4) { temp.direct.push_back((*itr4).GetInt()); } } DEBUG("\t得到一组导向线"); sep.lane_direct.push_back(temp); } } if (itr2->HasMember("lane_line")) { const Value &a3 = (*itr2)["lane_line"]; DEBUG("\t线数量 %d", a3.Size()); for (Value::ConstValueIterator itr3 = a3.Begin(); itr3 != a3.End(); ++itr3) { DEBUG("\t\t节数量 %d", (*itr3).Size()); vector sline; for (Value::ConstValueIterator itr4 = (*itr3).Begin(); itr4 != (*itr3).End(); ++itr4) { const Value &type = (*itr4)["type"]; const Value &line = (*itr4)["line"]; segment_t seg; DEBUG("\t\t\t节类型 = %d", type.GetInt()); seg.character = type.GetInt(); for (Value::ConstValueIterator itr5 = line.Begin(); itr5 != line.End(); ++itr5) { DEBUG("\t\t\t点 = %d", (*itr5).GetInt()); seg.points.push_back(mapPoints[(*itr5).GetInt()]); } sline.push_back(seg); } sep.lines.push_back(sline); } } road.separate.push_back(sep); } } map.roads.push_back(road); } } } if (doc.HasMember("special_area")) { const Value &a = doc["special_area"]; for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) { special_area_t specialArea; if (itr->HasMember("type")) { const Value &s = (*itr)["type"]; specialArea.type = s.GetInt(); } if (itr->HasMember("id")) { const Value &s = (*itr)["id"]; specialArea.id = s.GetInt(); } if (itr->HasMember("road")) { const Value &s = (*itr)["road"]; specialArea.road = s.GetInt(); } if (itr->HasMember("area")) { const Value &a2 = (*itr)["area"]; for (Value::ConstValueIterator itr2 = a2.Begin(); itr2 != a2.End(); ++itr2) { specialArea.area.push_back(mapPoints[(*itr2).GetInt()]); } } map.specialAreas.push_back(specialArea); } } /*if (doc.HasMember("trigger_line")) { const Value &a = doc["trigger_line"]; for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) { trigger_line_t trigger; if (itr->HasMember("type")) { const Value &s = (*itr)["type"]; trigger.active = s.GetInt(); } if (itr->HasMember("id")) { const Value &s = (*itr)["id"]; trigger.id = s.GetInt(); } if (itr->HasMember("road")) { const Value &s = (*itr)["road"]; trigger.road = s.GetInt(); } if (itr->HasMember("tts")) { const Value &s = (*itr)["tts"]; trigger.tts = s.GetString(); } if (itr->HasMember("time")) { const Value &s = (*itr)["time"]; trigger.time = s.GetInt(); } else { trigger.time = 0; } if (itr->HasMember("distance")) { const Value &s = (*itr)["distance"]; trigger.distance = s.GetInt(); } else { trigger.distance = 0; } if (itr->HasMember("line")) { const Value &a2 = (*itr)["line"]; for (Value::ConstValueIterator itr2 = a2.Begin(); itr2 != a2.End(); ++itr2) { trigger.points.push_back(mapPoints[(*itr2).GetInt()]); } } map.triggerLines.push_back(trigger); } }*/ if (doc.HasMember("red_line")) { const Value &a = doc["red_line"]; for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) { forbid_line_t forbid; if (itr->HasMember("type")) { const Value &s = (*itr)["type"]; forbid.type = s.GetInt(); DEBUG("禁止线 type %d", forbid.type); } if (itr->HasMember("id")) { const Value &s = (*itr)["id"]; forbid.id = s.GetInt(); } if (itr->HasMember("points")) { const Value &a2 = (*itr)["points"]; for (Value::ConstValueIterator itr2 = a2.Begin(); itr2 != a2.End(); ++itr2) { forbid.points.push_back(mapPoints[(*itr2).GetInt()]); } } map.forbidLines.push_back(forbid); } } vector schemes; if (doc.HasMember("scheme")) { const Value &a = doc["scheme"]; for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) { scheme_t scheme; if (itr->HasMember("name")) { const Value &s = (*itr)["name"]; scheme.name = s.GetString(); } if (itr->HasMember("crossing_active")) { const Value &a2 = (*itr)["crossing_active"]; for (Value::ConstValueIterator itr2 = a2.Begin(); itr2 != a2.End(); ++itr2) { crossing_active_t act; if (itr2->HasMember("road")) { const Value &s = (*itr2)["road"]; act.road_id = s.GetInt(); } if (itr2->HasMember("idx")) { const Value &s = (*itr2)["idx"]; act.index = s.GetInt(); } if (itr2->HasMember("active")) { const Value &s = (*itr2)["active"]; act.active = s.GetInt(); } scheme.crossingActive.push_back(act); } } if (itr->HasMember("trigger_line")) { const Value &a2 = (*itr)["trigger_line"]; for (Value::ConstValueIterator itr2 = a2.Begin(); itr2 != a2.End(); ++itr2) { trigger_line_t ins; if (itr2->HasMember("x_y")) { const Value &a3 = (*itr2)["x_y"]; if (a3.IsArray()) { PointF point; int n = 0; for (Value::ConstValueIterator itr3 = a3.Begin(); itr3 != a3.End(); ++itr3) { if (n == 0) { point.X = (*itr3).GetDouble(); n = 1; } else if (n == 1) { point.Y = (*itr3).GetDouble(); ins.points.push_back(point); n = 0; } } } } if (itr2->HasMember("road")) { const Value &s = (*itr2)["road"]; ins.road = s.GetInt(); } if (itr2->HasMember("type")) { const Value &s = (*itr2)["type"]; ins.active = s.GetInt(); } scheme.triggerLines.push_back(ins); } } schemes.push_back(scheme); } } DEBUG("地图解析完毕"); CleanRoadMap(); SetRoadMap(map, schemes); } else { DEBUG("############## 地图解析出错###################"); } break; } case ID_MS_MAP: { Document doc; doc.Parse(value); if (!doc.HasParseError()) { ClearAreaMap(); if (doc.HasMember("items")) { const Value &a = doc["items"]; 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 = (double (*)[2]) new double[pointNum][2]; // map = (double (*)[2]) malloc(pointNum * 2 * sizeof(double)); 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 = (double (*)[2]) new double[s2.Size()][2]; // map2 = (double (*)[2]) malloc(point2Num * 2 * sizeof(double)); for (Value::ConstValueIterator itr3 = s2.Begin(); itr3 != s2.End(); ++itr3) { map2[i][j] = (*itr3).GetDouble(); if (++j == 2) { j = 0; i++; } } } } } AddAreaMap(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 bodyNum = 0; int *body = NULL; int pointNum = 0; double antPitch = 0; double antHeight = 0; double groundHeight = 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("body")) { const Value& s = doc["body"]; if (s.IsArray()) { int i = 0; bodyNum = s.Size(); body = new int[bodyNum]; for(Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr) { body[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 = (double (*)[2]) new double[pointNum][2]; // point = (double (*)[2])malloc(pointNum * 2 * sizeof(double)); for(Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr) { point[i][j] = itr->GetDouble(); if (++j == 2) { j = 0; i++; } } } } if (doc.HasMember("ant_pitch")) { const Value& s = doc["ant_pitch"]; antPitch = s.GetDouble(); } if (doc.HasMember("ant_height")) { const Value& s = doc["ant_height"]; antHeight = s.GetDouble(); } if (doc.HasMember("ground_height")) { const Value& s = doc["ground_height"]; groundHeight = s.GetDouble(); } SetCarMeasurePoint(basePoint, axial, left_front_tire, right_front_tire, left_rear_tire, right_rear_tire, body, bodyNum, point, pointNum, antPitch, antHeight, groundHeight); if (body != NULL) delete []body; 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)[3] = (int (*)[3]) new int[n][3]; 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(); } if (itr->HasMember("level")) { const Value &s = (*itr)["level"]; sensor[i][2] = s.GetInt(); } ++i; } SetSensorCfg(sensor, n); delete []sensor; } } break; } case ID_MS_START_EXAM: { Document doc; doc.Parse(value); if (!doc.HasParseError()) { int start = 0, type = 0; if (doc.HasMember("exam")) { Value& s = doc["exam"]; start = s.GetInt(); } if (doc.HasMember("type")) { Value& s = doc["type"]; type = s.GetInt(); } StartDriverExam(start, type); } break; } case ID_MS_EXAM_MAP: { Document doc; doc.Parse(value); if (!doc.HasParseError()) { if (doc.HasMember("map_id") && doc.HasMember("exam")) { Value& s = doc["map_id"]; Value& s2 = doc["exam"]; } } break; } case ID_MS_IND_ONOFF: { Document doc; doc.Parse(value); if (!doc.HasParseError()) { if (doc.HasMember("on_off")) { Value& s = doc["on_off"]; do { OnOff = s.GetInt(); } while (OnOff != s.GetInt()); } } break; } case ID_MS_READ_CARD : { ReadCard(); break; } case ID_MS_SYS_SHUTDOWN: { Document doc; doc.Parse(value); if (!doc.HasParseError()) { int event = 0, timeout = 15; if (doc.HasMember("event")) { Value& s = doc["event"]; event = s.GetInt(); } if (doc.HasMember("timeout")) { Value& s = doc["timeout"]; timeout = s.GetInt(); } SystemShutdown(event, timeout); } break; } case ID_MS_LIGHT_EXAM_RES: { Document doc; doc.Parse(value); if (!doc.HasParseError()) { if (doc.HasMember("exam")) { const Value& s = doc["exam"]; s.GetInt(); } if (doc.HasMember("question")) { const Value& s = doc["question"]; if (s.IsArray() && s.Size() > 0) { int n = 0; struct dummy_light_exam *content = new struct dummy_light_exam[s.Size()]; for(Value::ConstValueIterator itr = s.Begin(); itr != s.End(); ++itr) { if (itr->HasMember("item") && itr->HasMember("tts")) { const Value &s1 = (*itr)["item"]; const Value &s2 = (*itr)["tts"]; content[n].item = s1.GetInt(); strcpy(content[n].tts, s2.GetString()); n++; } } SetDummyLightExam(n, content); delete []content; } } } break; } default:break; } } void MA_MainProcBinMsgEntry(int cmd, const uint8_t *value, int length) { switch (cmd) { case ID_MS_FILE: { UploadDfuFile(value, length); break; } default: break; } } void MA_EnterMap(int map_id, int type, int enter) { StringBuffer sb; Writer writer(sb); writer.StartObject(); writer.Key("map_id"); writer.Int(map_id); writer.Key("type"); writer.Int(type); writer.Key("enter"); writer.Int(enter); writer.EndObject(); SendMsgToMainProcIndep(ID_SM_ENTER_MAP, sb.GetString()); } void MA_ExamLight(void) { SendMsgToMainProcIndep(ID_SM_LIGHT_EXAM_REQ, NULL); }