Пример #1
0
/*=========================================================================*\
  Get device info as allocated JSON object string
    (no unicode escaping of strings)
    caller must free result and should lock the device
    returns NULL on error
\*=========================================================================*/
static char *_ickDeviceStateJson( ickDevice_t *device, int indent )
{
  int   rc;
  char *result;
  char *wsi;
  char *message;
  debug( "_ickDeviceStateJson: %s", device->uuid );
  indent += JSON_INDENT;

/*------------------------------------------------------------------------*\
    Empty data?
\*------------------------------------------------------------------------*/
  if( !device )
    return strdup( "null" );

/*------------------------------------------------------------------------*\
    Create websocket information (if any)
\*------------------------------------------------------------------------*/
  /* fixme
  psd  = // no way to get user data from a wsi outside a lws callback scope
  jWsi = _ickWsiStateJson( psd );
  */
  wsi = strdup( JSON_BOOL(device->wsi) );
  if( !wsi ) {
    logerr( "_ickDeviceStateJson: out of memory" );
    return NULL;
  }

/*------------------------------------------------------------------------*\
    Create message information (if any)
\*------------------------------------------------------------------------*/
  message = _ickMessageStateJson( device->outQueue, indent+JSON_INDENT );
  if( !message ) {
    Sfree( wsi );
    logerr( "_ickDeviceStateJson: out of memory" );
    return NULL;
  }

/*------------------------------------------------------------------------*\
    Compile all debug info
\*------------------------------------------------------------------------*/
  rc = asprintf( &result,
                  "{\n"
                  "%*s\"name\": \"%s\",\n"
                  "%*s\"tCreation\": %f,\n"
                  "%*s\"UUID\": \"%s\",\n"
                  "%*s\"location\": \"%s\",\n"
                  "%*s\"upnpVersion\": %d,\n"
                  "%*s\"p2pLevel\": %d,\n"
                  "%*s\"lifetime\": %d,\n"
                  "%*s\"services\": %d,\n"
                  "%*s\"getXml\": \"%s\",\n"
                  "%*s\"doConnect\": %s,\n"
                  "%*s\"ssdpState\": \"%s\",\n"
                  "%*s\"bootId\": \"%ld\",\n"
                  "%*s\"configId\": \"%ld\",\n"
                  "%*s\"connectionState\": \"%s\",\n"
                  "%*s\"tXmlComplete\": %f,\n"
                  "%*s\"tConnect\": %f,\n"
                  "%*s\"tDisconnect\": %f,\n"
                  "%*s\"rx\": %d,\n"
                  "%*s\"rxSegmented\": %d,\n"
                  "%*s\"rxPending\": %d,\n"
                  "%*s\"tx\": %d,\n"
                  "%*s\"txPending\": %d,\n"
                  "%*s\"rxLast\": %f,\n"
                  "%*s\"txLast\": %f,\n"
                  "%*s\"wsi\": %s,\n"
                  "%*s\"message\": %s\n"
                  "%*s}",
                  indent, "", JSON_STRING( device->friendlyName ),
                  indent, "", JSON_REAL( device->tCreation ),
                  indent, "", JSON_STRING( device->uuid ),
                  indent, "", JSON_STRING( device->location ),
                  indent, "", JSON_INTEGER( device->ickUpnpVersion ),
                  indent, "", JSON_INTEGER( device->ickP2pLevel ),
                  indent, "", JSON_INTEGER( device->lifetime ),
                  indent, "", JSON_INTEGER( device->services ),
                  indent, "", JSON_STRING( device->wget?_ickWGetUri(device->wget) : NULL ),
                  indent, "", JSON_BOOL( device->doConnect ),
                  indent, "", JSON_STRING( _ickDeviceSsdpState2Str(device->ssdpState) ),
                  indent, "", JSON_LONG( device->ssdpBootId ),
                  indent, "", JSON_LONG( device->ssdpConfigId ),
                  indent, "", JSON_STRING( _ickDeviceConnState2Str(device->connectionState) ),
                  indent, "", JSON_REAL( device->tXmlComplete ),
                  indent, "", JSON_REAL( device->tConnect ),
                  indent, "", JSON_REAL( device->tDisconnect ),
                  indent, "", JSON_INTEGER( device->nRx ),
                  indent, "", JSON_INTEGER( device->nRxSegmented ),
                  indent, "", JSON_INTEGER( _ickDevicePendingInMessages(device) ),
                  indent, "", JSON_INTEGER( device->nTx ),
                  indent, "", JSON_INTEGER( _ickDevicePendingOutMessages(device) ),
                  indent, "", JSON_REAL( device->tLastRx ),
                  indent, "", JSON_REAL( device->tLastTx ),
                  indent, "", JSON_OBJECT( wsi ),
                  indent, "", JSON_OBJECT( message ),
                  indent-JSON_INDENT, ""
                );
  Sfree( wsi );
  Sfree( message );

/*------------------------------------------------------------------------*\
    Error
\*------------------------------------------------------------------------*/
  if( rc<0 ) {
    logerr( "_ickDeviceStateJson: out of memory" );
    return NULL;
  }

/*------------------------------------------------------------------------*\
    Return result
\*------------------------------------------------------------------------*/
  return result;
}
Пример #2
0
 std::string ChessGame::toJSON() const
 {
     ostringstream oss;
     oss << JSON_OBJECT(JSON_COMMA JSON_KEYVALUE(history) JSON_COMMA JSON_KEYVALUE(chessBoard) JSON_COMMA JSON_KEYVALUE(config));
     return oss.str();
 }
Пример #3
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;
}