int main (int argc, char *argv[]) { //Force line-only buffering so we can see the output during hangs setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stderr, NULL, _IOLBF, 0); GOOGLE_PROTOBUF_VERIFY_VERSION; hu_log_library_versions(); hu_install_crash_handler(); DBus::_init_threading(); gst_init(&argc, &argv); try { MazdaCommandServerCallbacks commandCallbacks; CommandServer commandServer(commandCallbacks); printf("headunit version: %s \n", commandCallbacks.GetVersion().c_str()); if (!commandServer.Start()) { loge("Command server failed to start"); return 1; } if (argc >= 2 && strcmp(argv[1], "test") == 0) { //test mode from the installer, if we got here it's ok printf("###TESTMODE_OK###\n"); return 0; } config::readConfig(); printf("Looping\n"); while (true) { //Make a new one instead of using the default so we can clean it up each run run_on_thread_main_context = g_main_context_new(); //Recreate this each time, it makes the error handling logic simpler DBus::Glib::BusDispatcher dispatcher; dispatcher.attach(run_on_thread_main_context); printf("DBus::Glib::BusDispatcher attached\n"); DBus::default_dispatcher = &dispatcher; printf("Making debug connections\n"); DBus::Connection hmiBus(HMI_BUS_ADDRESS, false); hmiBus.register_bus(); DBus::Connection serviceBus(SERVICE_BUS_ADDRESS, false); serviceBus.register_bus(); hud_start(); MazdaEventCallbacks callbacks(serviceBus, hmiBus); HUServer headunit(callbacks); g_hu = &headunit.GetAnyThreadInterface(); commandCallbacks.eventCallbacks = &callbacks; //Wait forever for a connection int ret = headunit.hu_aap_start(config::transport_type, true); if (ret < 0) { loge("Something bad happened"); continue; } gst_app.loop = g_main_loop_new(run_on_thread_main_context, FALSE); callbacks.connected = true; std::condition_variable quitcv; std::mutex quitmutex; std::mutex hudmutex; std::thread nm_thread([&quitcv, &quitmutex](){ nightmode_thread_func(quitcv, quitmutex); } ); std::thread gp_thread([&quitcv, &quitmutex](){ gps_thread_func(quitcv, quitmutex); } ); std::thread hud_thread([&quitcv, &quitmutex, &hudmutex](){ hud_thread_func(quitcv, quitmutex, hudmutex); } ); /* Start gstreamer pipeline and main loop */ printf("Starting Android Auto...\n"); g_main_loop_run (gst_app.loop); commandCallbacks.eventCallbacks = nullptr; callbacks.connected = false; callbacks.videoFocus = false; callbacks.audioFocus = AudioManagerClient::FocusType::NONE; callbacks.inCall = false; printf("quitting...\n"); //wake up night mode and gps polling threads quitcv.notify_all(); printf("waiting for nm_thread\n"); nm_thread.join(); printf("waiting for gps_thread\n"); gp_thread.join(); printf("waiting for hud_thread\n"); hud_thread.join(); printf("shutting down\n"); g_main_loop_unref(gst_app.loop); gst_app.loop = nullptr; /* Stop AA processing */ ret = headunit.hu_aap_shutdown(); if (ret < 0) { printf("hu_aap_shutdown() ret: %d\n", ret); return ret; } g_main_context_unref(run_on_thread_main_context); run_on_thread_main_context = nullptr; g_hu = nullptr; DBus::default_dispatcher = nullptr; } } catch(DBus::Error& error) { loge("DBUS Error: %s: %s", error.name(), error.message()); return 1; } return 0; }
int main(int argc, char *argv[]) { GOOGLE_PROTOBUF_VERIFY_VERSION; hu_log_library_versions(); hu_install_crash_handler(); #if defined GDK_VERSION_3_10 printf("GTK VERSION 3.10.0 or higher\n"); //Assuming we are on Gnome, what's the DPI scale factor? gdk_init(&argc, &argv); GdkScreen * primaryDisplay = gdk_screen_get_default(); if (primaryDisplay) { g_dpi_scalefactor = (float) gdk_screen_get_monitor_scale_factor(primaryDisplay, 0); printf("Got gdk_screen_get_monitor_scale_factor() == %f\n", g_dpi_scalefactor); } #else printf("Using hard coded scalefactor\n"); g_dpi_scalefactor = 1; #endif gst_app_t *app = &gst_app; int ret = 0; errno = 0; gst_init(NULL, NULL); struct sigaction action; sigaction(SIGINT, NULL, &action); if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { SDL_Log("Unable to initialize SDL: %s", SDL_GetError()); return 1; } sigaction(SIGINT, &action, NULL); DesktopCommandServerCallbacks commandCallbacks; CommandServer commandServer(commandCallbacks); if (!commandServer.Start()) { loge("Command server failed to start"); } //loop to emulate the caar while(true) { DesktopEventCallbacks callbacks; HUServer headunit(callbacks); /* Start AA processing */ ret = headunit.hu_aap_start(HU_TRANSPORT_TYPE::USB, true); if (ret < 0) { printf("Phone is not connected. Connect a supported phone and restart.\n"); return 0; } callbacks.connected = true; g_hu = &headunit.GetAnyThreadInterface(); commandCallbacks.eventCallbacks = &callbacks; /* Start gstreamer pipeline and main loop */ ret = gst_loop(app); if (ret < 0) { printf("STATUS:gst_loop() ret: %d\n", ret); } callbacks.connected = false; commandCallbacks.eventCallbacks = nullptr; /* Stop AA processing */ ret = headunit.hu_aap_shutdown(); if (ret < 0) { printf("STATUS:hu_aap_stop() ret: %d\n", ret); SDL_Quit(); return (ret); } g_hu = nullptr; } SDL_Quit(); return (ret); }