char* ortc_getVersionVerbose(void){ char *ortc_version = ortc_getVersion(); const char *lws_version = lws_get_library_version(); char *msg = "Realtime Messaging SDK version: %s; Libwebsockets version: %s;"; int len = strlen(ortc_version) + strlen(lws_version) + strlen(msg) +1; char *version = (char *) calloc(len, sizeof(char)); sprintf(version, msg, ortc_version, lws_version); free(ortc_version); return version; };
openGalaxy::openGalaxy(context_options& options) : m_options(options) { m_mutex.lock(); m_quit = false; exit_status = EXIT_STATUS_AS_REQUESTED; m_Receiver_exptr = nullptr; m_Websocket_exptr = nullptr; m_Commander_exptr = nullptr; m_Output_exptr = nullptr; m_Poll_exptr = nullptr; // Initialize the mutex for the exit() and isQuit() member functions m_lock = new std::unique_lock<std::mutex>(m_lock_mutex); // Create instances of Syslog and Settings objects m_Syslog = new Syslog(); // Syslog must be 1st // Output banner to the log syslog().write(Syslog::Level::Always, header1); // name/description syslog().write(Syslog::Level::Always, header2); // copyright syslog().write(Syslog::Level::Always, header3); // license syslog().write(Syslog::Level::Always, ""); // This also loads the settings from the configuration file m_Settings = new Settings(this); // Settings must be 2nd syslog().info("Compiled with libwebsockets version %s", lws_get_library_version()); syslog().info("Compiled with %s", SSLeay_version(SSLEAY_VERSION)); switch(syslog().get_level()){ case Syslog::Level::Error: syslog().info("Log level set to 1 (errors and warnings only)."); break; case Syslog::Level::Info: syslog().info("Log level set to 2 (default)."); break; case Syslog::Level::Debug: syslog().info("Log level set to 3 (All messages)."); break; default: break; } if( m_options.no_ssl ){ syslog().error("Warning: Disabling SSL !"); } else { if( m_options.no_client_certs ){ syslog().error("Warning: Disabling SSL Client Authentication!"); } else { if( m_options.no_password ){ syslog().error("Warning: Disabling username/password authorization!"); } } if( m_options.auto_logoff ){ syslog().info( "Automaticly logging off clients after %u seconds of inactivity.", m_Settings->session_timeout_seconds ); } } if(m_Settings->galaxy_dip8) syslog().info("Galaxy dipswitch 8 position configured as 'ON'."); m_Galaxy = new Galaxy(*this); m_SIA = new SIA(*this); m_Output = new Output(*this); // Open the serial port // (this blocks for a while on windows if the port does not exist) syslog().info( "Using serial port %s (%u Baud 8N1).", m_Settings->receiver_tty.c_str(), m_Settings->receiver_baudrate ); m_Serial = new SerialPort(*this); if(m_Serial->isOpen() == false){ syslog().error( "WARNING: CONTINUING WITHOUT SERIAL PORT CONNECTION!" ); } m_Receiver = new Receiver(*this); m_Commander = new Commander(*this); m_Poll = new Poll(*this); m_Websocket = new Websocket(this); m_mutex.unlock(); }
int callback_lws_status(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { struct per_session_data__lws_status *pss = (struct per_session_data__lws_status *)user, **pp; char name[128], rip[128]; int m; switch (reason) { case LWS_CALLBACK_PROTOCOL_INIT: /* * Prepare the static server info */ server_info_len = sprintf((char *)server_info, "\"version\":\"%s\"," " \"hostname\":\"%s\"", lws_get_library_version(), lws_canonical_hostname( lws_get_context(wsi))); break; case LWS_CALLBACK_ESTABLISHED: /* * we keep a linked list of live pss, so we can walk it */ pss->last = 0; pss->list = list; list = pss; live_wsi++; lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), name, sizeof(name), rip, sizeof(rip)); sprintf(pss->ip, "%s (%s)", name, rip); gettimeofday(&pss->tv_established, NULL); strcpy(pss->user_agent, "unknown"); lws_hdr_copy(wsi, pss->user_agent, sizeof(pss->user_agent), WSI_TOKEN_HTTP_USER_AGENT); update_status(wsi, pss); break; case LWS_CALLBACK_SERVER_WRITEABLE: m = lws_write(wsi, (unsigned char *)cache + LWS_PRE, cache_len, LWS_WRITE_TEXT); if (m < server_info_len) { lwsl_err("ERROR %d writing to di socket\n", m); return -1; } break; case LWS_CALLBACK_CLOSED: pp = &list; while (*pp) { if (*pp == pss) { *pp = pss->list; pss->list = NULL; live_wsi--; break; } pp = &((*pp)->list); } update_status(wsi, pss); break; default: break; } return 0; }
/*=========================================================================*\ Get context info as allocated JSON object string including device list (no unicode escaping of strings) caller must free result returns NULL on error \*=========================================================================*/ static char *_ickContextStateJson( ickP2pContext_t *ictx, int indent ) { ickDevice_t *device; char *devices = NULL; ickInterface_t *interface; char *interfaces = NULL; int i; int rc; char *result; debug( "_ickContextStateJson (%p): %s", ictx, ictx->deviceUuid ); indent += JSON_INDENT; /*------------------------------------------------------------------------*\ Compile array of interfaces \*------------------------------------------------------------------------*/ _ickLibInterfaceListLock( ictx ); interfaces = strdup( "[" ); for( i=0,interface=ictx->interfaces; interface&&interfaces; i++,interface=interface->next ) { char *interfaceInfo; // Get debug info interfaceInfo = _ickInterfaceStateJson( interface, indent+JSON_INDENT ); if( !interfaceInfo ) { Sfree( interfaces ); break; } // Add to list rc = asprintf( &result, "%s%s\n%*s%s", interfaces, i?",":"", indent+JSON_INDENT, "", interfaceInfo ); Sfree( interfaces ); Sfree( interfaceInfo ); if( rc<0 ) break; else interfaces = result; } _ickLibInterfaceListUnlock( ictx ); // Close list if( interfaces ) { rc = asprintf( &result, "%s\n%*s]", interfaces, indent, "" ); Sfree( interfaces ); if( rc>0 ) interfaces = result; } // Error ? if( !interfaces ) { logerr( "_ickContextStateJson: out of memory" ); return NULL; } /*------------------------------------------------------------------------*\ Compile array of devices \*------------------------------------------------------------------------*/ _ickLibDeviceListLock( ictx ); devices = strdup( "[" ); for( i=0,device=ictx->deviceList; device&&devices; i++,device=device->next ) { char *deviceInfo; // Get debug info deviceInfo = _ickDeviceStateJson( device, indent+JSON_INDENT ); if( !deviceInfo ) { Sfree( devices ); break; } // Add to list rc = asprintf( &result, "%s%s\n%*s%s", devices, i?",":"", indent+JSON_INDENT, "", deviceInfo ); Sfree( devices ); Sfree( deviceInfo ); if( rc<0 ) break; else devices = result; } _ickLibDeviceListUnlock( ictx ); // Close list if( devices ) { rc = asprintf( &result, "%s\n%*s]", devices, indent, "" ); Sfree( devices ); if( rc>0 ) devices = result; } // Error ? if( !devices ) { Sfree( interfaces ); logerr( "_ickContextStateJson: out of memory" ); return NULL; } /*------------------------------------------------------------------------*\ Compile all context debug info \*------------------------------------------------------------------------*/ rc = asprintf( &result, "{\n" "%*s\"pid\": %d,\n" "%*s\"uuid\": \"%s\",\n" "%*s\"name\": \"%s\",\n" "%*s\"services\": %d,\n" "%*s\"upnpListenerPort\": %d,\n" "%*s\"wsPort\": %d,\n" "%*s\"folder\": \"%s\",\n" "%*s\"lifetime\": %d,\n" "%*s\"state\": \"%s\",\n" "%*s\"loopback\": %s,\n" "%*s\"customConnectMatrix\": %s,\n" "%*s\"tCreation\": %f,\n" "%*s\"tResume\": %f,\n" "%*s\"osName\": \"%s\",\n" "%*s\"p2pVersion\": \"%s\",\n" "%*s\"p2pLevel\": %d,\n" "%*s\"lwsVersion\": \"%s\",\n" "%*s\"bootId\": %ld,\n" "%*s\"configId\": %ld,\n" "%*s\"interfaces\": %s\n" "%*s\"devices\": %s\n" "%*s}\n", indent, "", JSON_INTEGER( getpid() ), indent, "", JSON_STRING( ictx->deviceUuid ), indent, "", JSON_STRING( ictx->deviceName ), indent, "", JSON_INTEGER( ictx->ickServices ), indent, "", JSON_INTEGER( ictx->upnpListenerPort ), indent, "", JSON_INTEGER( ictx->lwsPort ), indent, "", JSON_STRING( ictx->upnpFolder), indent, "", JSON_INTEGER( ictx->lifetime ), indent, "", JSON_STRING( ickLibState2Str(ictx->state) ), indent, "", JSON_BOOL( ictx->upnpLoopback ), indent, "", JSON_BOOL( ictx->lwsConnectMatrixCb==ickP2pDefaultConnectMatrixCb ), indent, "", JSON_REAL( ictx->tCreation ), indent, "", JSON_REAL( ictx->tResume ), indent, "", JSON_STRING( ictx->osName ), indent, "", JSON_STRING( ickP2pGetVersion(NULL,NULL) ), indent, "", JSON_INTEGER( ICKP2PLEVEL_SUPPORTED ), indent, "", JSON_STRING( lws_get_library_version() ), indent, "", JSON_LONG( ictx->upnpBootId ), indent, "", JSON_LONG( ictx->upnpConfigId ), indent, "", JSON_OBJECT( interfaces ), indent, "", JSON_OBJECT( devices ), indent-JSON_INDENT, "" ); Sfree( devices ); Sfree( interfaces ); /*------------------------------------------------------------------------*\ Error \*------------------------------------------------------------------------*/ if( rc<0 ) { logerr( "_ickContextStateJson: out of memory" ); return NULL; } /*------------------------------------------------------------------------*\ Return result \*------------------------------------------------------------------------*/ return result; }