Пример #1
0
int main ( int argc, char *argv[] ) {
	int lockfd;
	uint8_t rErr = 0;

	if ( argc != 2 )
		printf ( "%s <pin>\n    pin : wiringPi pin No (default %d)\n",argv[0], DHTPIN );
	else
		DHTPIN = atoi( argv[1] );
	
	lockfd = open_lockfile( LOCKFILE );

	if ( wiringPiSetup () == -1 )
		exit( EXIT_FAILURE ) ;
		
	if ( setuid( getuid() ) < 0 ) {
		perror( "Dropping privileges failed\n" );
		exit( EXIT_FAILURE );
	}

	while ( read_dht22_dat() == 0 && rErr++ < 10 ) {
		delay( 1000 ); // wait 1sec to refresh
	}

	delay( 1500 );	// No retry within 1 sec
	close_lockfile( lockfd );

	return 0 ;
}
Пример #2
0
int main (void)
{
  int lockfd;


  lockfd = open_lockfile(LOCKFILE);

  if (wiringPiSetup () == -1)
    exit(EXIT_FAILURE) ;
	
  if (setuid(getuid()) < 0)
  {
    perror("Dropping privileges failed\n");
    exit(EXIT_FAILURE);
  }

  while (read_dht22_dat() == 0) 
  {
     delay(1000); // wait 1sec to refresh
  }

  delay(1500);
  close_lockfile(lockfd);

  return 0 ;
}
Пример #3
0
int main (int argc, char *argv[])
{
  int lockfd;

  if (argc != 2)
    printf ("{\"err\": {\"message\": \"NOT_ENOUGH_ARGUMENTS\"}}\n");
  else
    DHTPIN = atoi(argv[1]);
    
  lockfd = open_lockfile(LOCKFILE);

  if (wiringPiSetup () == -1)
    exit(EXIT_FAILURE) ;
	
  if (setuid(getuid()) < 0)
  {
    perror("{\"err\": {\"message\": \"DROPPING_PRIVILEGES_FAILED\"}}\n");
    exit(EXIT_FAILURE);
  }

  while (1) 
  {
     read_dht22_dat();
     fflush(stdout);
     delay(500); // wait 0.5s to refresh
  }

  delay(500);
  close_lockfile(lockfd);

  return 0 ;
}
static int remote_monitoring_init(void)
{
    int result;

    Lock_fd = open_lockfile(LOCKFILE);

    if (setuid(getuid()) < 0)
    {
        perror("Dropping privileges failed. (did you use sudo?)n");
        result = EXIT_FAILURE;
    }
    else
    {
        result = wiringPiSetup();
        if (result != 0)
        {
            perror("Wiring Pi setup failed.");
        }
        else
        {
            result = wiringPiSPISetup(Spi_channel, Spi_clock);
            if (result < 0)
            {
                printf("Can't setup SPI, error %i calling wiringPiSPISetup(%i, %i)  %sn",
                    result, Spi_channel, Spi_clock, strerror(result));
            }
            else
            {
                int sensorResult = bme280_init(Spi_channel);
                if (sensorResult != 1)
                {
                    printf("It appears that no BMP280 module on Chip Enable %i is attached. Aborting.\n", Spi_channel);
                    result = 1;
                }
                else
                {
                    // Read the Temp & Pressure module.
                    float tempC = -300.0;
                    float pressurePa = -300;
                    float humidityPct = -300;
                    sensorResult = bme280_read_sensors(&tempC, &pressurePa, &humidityPct);
                    if (sensorResult == 1)
                    {
                        printf("Temperature = %.1f *C  Pressure = %.1f Pa  Humidity = %1f %%\n",
                            tempC, pressurePa, humidityPct);
                        result = 0;
                    }
                    else
                    {
                        printf("Unable to read BME280 on pin %i. Aborting.\n", Spi_channel);
                        result = 1;
                    }
                }
            }
        }
    }
    return result;
}
Пример #5
0
int main (int argc, char *argv[])
{
  int lockfd = 0; //initialize to suppress warning
  int tries = 100;
  int lock = 1;

  if (argc < 2)
    printf ("usage: %s <pin> (<tries> <lock>)\ndescription: pin is the wiringPi pin number\nusing 7 (GPIO 4)\nOptional: tries is the number of times to try to obtain a read (default 100)\n          lock: 0 disables the lockfile (for running as non-root user)\n",argv[0]);
  else
    DHTPIN = atoi(argv[1]);
   

  if (argc >= 3)
    tries = atoi(argv[2]);

  if (tries < 1) {
    printf("Invalid tries supplied\n");
    exit(EXIT_FAILURE);
  }


  if (argc >= 4)
    lock = atoi(argv[3]);

  if (lock != 0 && lock != 1) {
    printf("Invalid lock state supplied\n");
    exit(EXIT_FAILURE);
  }

  printf ("Raspberry Pi wiringPi DHT22 reader\nwww.lolware.net\n") ;

  if(lock)
    lockfd = open_lockfile(LOCKFILE);

  if (wiringPiSetup () == -1)
    exit(EXIT_FAILURE) ;
	
  if (setuid(getuid()) < 0)
  {
    perror("Dropping privileges failed\n");
    exit(EXIT_FAILURE);
  }

  while (read_dht22_dat() == 0 && tries--) 
  {
     delay(1000); // wait 1sec to refresh
  }

  delay(1500);
  if(lock)
    close_lockfile(lockfd);

  return 0 ;
}
Пример #6
0
void parse_opts( int argc, char** argv, magma_opts *opts )
{
    // negative flag indicating -m, -n, -k not given
    int m = -1;
    int n = -1;
    int k = -1;
    
    // fill in default values
    opts->device   = 0;
    opts->pad      = 32;
    opts->nb       = 0;  // auto
    opts->nrhs     = 1;
    opts->nstream  = 1;
    opts->ngpu     = magma_num_gpus();
    opts->niter    = 1;
    opts->nthread  = 1;
    opts->itype    = 1;
    opts->svd_work = 0;
    opts->version  = 1;
    opts->fraction = 1.;
    opts->tolerance = 30.;
    opts->panel_nthread = 1;
    opts->fraction_dcpu = 0.0;
    opts->check     = (getenv("MAGMA_TESTINGS_CHECK") != NULL);
    opts->lapack    = (getenv("MAGMA_RUN_LAPACK")     != NULL);
    opts->warmup    = (getenv("MAGMA_WARMUP")         != NULL);
    opts->all       = (getenv("MAGMA_RUN_ALL")        != NULL);
    opts->verbose   = false;
    
    opts->uplo      = MagmaLower;      // potrf, etc.
    opts->transA    = MagmaNoTrans;    // gemm, etc.
    opts->transB    = MagmaNoTrans;    // gemm
    opts->side      = MagmaLeft;       // trsm, etc.
    opts->diag      = MagmaNonUnit;    // trsm, etc.
    opts->jobu      = MagmaNoVec;  // gesvd: no left  singular vectors
    opts->jobvt     = MagmaNoVec;  // gesvd: no right singular vectors
    opts->jobz      = MagmaNoVec;  // heev:  no eigen vectors
    opts->jobvr     = MagmaNoVec;  // geev:  no right eigen vectors
    opts->jobvl     = MagmaNoVec;  // geev:  no left  eigen vectors
    
    #ifdef USE_FLOCK
    opts->flock_op = LOCK_SH;  // default shared lock
    #endif
    
    printf( usage_short, argv[0] );
    
    int ndevices;
    cudaGetDeviceCount( &ndevices );
    
    int info;
    int ntest = 0;
    for( int i = 1; i < argc; ++i ) {
        // ----- matrix size
        // each -N fills in next entry of msize, nsize, ksize and increments ntest
        if ( strcmp("-N", argv[i]) == 0 && i+1 < argc ) {
            magma_assert( ntest < MAX_NTEST, "error: -N %s, max number of tests exceeded, ntest=%d.\n",
                          argv[i], ntest );
            i++;
            int m2, n2, k2;
            info = sscanf( argv[i], "%d,%d,%d", &m2, &n2, &k2 );
            if ( info == 3 && m2 >= 0 && n2 >= 0 && k2 >= 0 ) {
                opts->msize[ ntest ] = m2;
                opts->nsize[ ntest ] = n2;
                opts->ksize[ ntest ] = k2;
            }
            else if ( info == 2 && m2 >= 0 && n2 >= 0 ) {
                opts->msize[ ntest ] = m2;
                opts->nsize[ ntest ] = n2;
                opts->ksize[ ntest ] = n2;  // implicitly
            }
            else if ( info == 1 && m2 >= 0 ) {
                opts->msize[ ntest ] = m2;
                opts->nsize[ ntest ] = m2;  // implicitly
                opts->ksize[ ntest ] = m2;  // implicitly
            }
            else {
                fprintf( stderr, "error: -N %s is invalid; ensure m >= 0, n >= 0, k >= 0.\n",
                         argv[i] );
                exit(1);
            }
            ntest++;
        }
        // --range start:stop:step fills in msize[ntest:], nsize[ntest:], ksize[ntest:]
        // with given range and updates ntest
        else if ( strcmp("--range", argv[i]) == 0 && i+1 < argc ) {
            i++;
            int start, stop, step;
            info = sscanf( argv[i], "%d:%d:%d", &start, &stop, &step );
            if ( info == 3 && start >= 0 && stop >= 0 && step != 0 ) {
                for( int n = start; (step > 0 ? n <= stop : n >= stop); n += step ) {
                    if ( ntest >= MAX_NTEST ) {
                        printf( "warning: --range %s, max number of tests reached, ntest=%d.\n",
                                argv[i], ntest );
                        break;
                    }
                    opts->msize[ ntest ] = n;
                    opts->nsize[ ntest ] = n;
                    opts->ksize[ ntest ] = n;
                    ntest++;
                }
            }
            else {
                fprintf( stderr, "error: --range %s is invalid; ensure start >= 0, stop >= start, step > 0.\n",
                         argv[i] );
                exit(1);
            }
        }
        // save m, n, k if -m, -n, -k is given; applied after loop
        else if ( strcmp("-m", argv[i]) == 0 && i+1 < argc ) {
            m = atoi( argv[++i] );
            magma_assert( m >= 0, "error: -m %s is invalid; ensure m >= 0.\n", argv[i] );
        }
        else if ( strcmp("-n", argv[i]) == 0 && i+1 < argc ) {
            n = atoi( argv[++i] );
            magma_assert( n >= 0, "error: -n %s is invalid; ensure n >= 0.\n", argv[i] );
        }
        else if ( strcmp("-k", argv[i]) == 0 && i+1 < argc ) {
            k = atoi( argv[++i] );
            magma_assert( k >= 0, "error: -k %s is invalid; ensure k >= 0.\n", argv[i] );
        }
        
        // ----- scalar arguments
        else if ( strcmp("--dev", argv[i]) == 0 && i+1 < argc ) {
            opts->device = atoi( argv[++i] );
            magma_assert( opts->device >= 0 && opts->device < ndevices,
                          "error: --dev %s is invalid; ensure dev in [0,%d].\n", argv[i], ndevices-1 );
        }
        else if ( strcmp("--pad", argv[i]) == 0 && i+1 < argc ) {
            opts->pad = atoi( argv[++i] );
            magma_assert( opts->pad >= 1 && opts->pad <= 4096,
                          "error: --pad %s is invalid; ensure pad in [1,4096].\n", argv[i] );
        }
        else if ( strcmp("--nrhs",    argv[i]) == 0 && i+1 < argc ) {
            opts->nrhs = atoi( argv[++i] );
            magma_assert( opts->nrhs >= 0,
                          "error: --nrhs %s is invalid; ensure nrhs >= 0.\n", argv[i] );
        }
        else if ( strcmp("--nb",      argv[i]) == 0 && i+1 < argc ) {
            opts->nb = atoi( argv[++i] );
            magma_assert( opts->nb > 0,
                          "error: --nb %s is invalid; ensure nb > 0.\n", argv[i] );
        }
        else if ( strcmp("--ngpu",    argv[i]) == 0 && i+1 < argc ) {
            opts->ngpu = atoi( argv[++i] );
            magma_assert( opts->ngpu <= MagmaMaxGPUs,
                          "error: --ngpu %s exceeds MagmaMaxGPUs, %d.\n", argv[i], MagmaMaxGPUs );
            magma_assert( opts->ngpu <= ndevices,
                          "error: --ngpu %s exceeds number of CUDA devices, %d.\n", argv[i], ndevices );
            magma_assert( opts->ngpu > 0,
                          "error: --ngpu %s is invalid; ensure ngpu > 0.\n", argv[i] );
            #ifndef _MSC_VER  // not Windows
            // save in environment variable, so magma_num_gpus() picks it up
            setenv( "MAGMA_NUM_GPUS", argv[i], true );
            #endif
        }
        else if ( strcmp("--nstream", argv[i]) == 0 && i+1 < argc ) {
            opts->nstream = atoi( argv[++i] );
            magma_assert( opts->nstream > 0,
                          "error: --nstream %s is invalid; ensure nstream > 0.\n", argv[i] );
        }
        else if ( strcmp("--niter",   argv[i]) == 0 && i+1 < argc ) {
            opts->niter = atoi( argv[++i] );
            magma_assert( opts->niter > 0,
                          "error: --niter %s is invalid; ensure niter > 0.\n", argv[i] );
        }
        else if ( strcmp("--nthread", argv[i]) == 0 && i+1 < argc ) {
            opts->nthread = atoi( argv[++i] );
            magma_assert( opts->nthread > 0,
                          "error: --nthread %s is invalid; ensure nthread > 0.\n", argv[i] );
        }
        else if ( strcmp("--itype",   argv[i]) == 0 && i+1 < argc ) {
            opts->itype = atoi( argv[++i] );
            magma_assert( opts->itype >= 1 && opts->itype <= 3,
                          "error: --itype %s is invalid; ensure itype in [1,2,3].\n", argv[i] );
        }
        else if ( strcmp("--svd_work", argv[i]) == 0 && i+1 < argc ) {
            opts->svd_work = atoi( argv[++i] );
            magma_assert( opts->svd_work >= 0 && opts->svd_work <= 3,
                          "error: --svd_work %s is invalid; ensure svd_work in [0,1,2,3].\n", argv[i] );
        }
        else if ( strcmp("--version", argv[i]) == 0 && i+1 < argc ) {
            opts->version = atoi( argv[++i] );
            magma_assert( opts->version >= 1,
                          "error: --version %s is invalid; ensure version > 0.\n", argv[i] );
        }
        else if ( strcmp("--fraction", argv[i]) == 0 && i+1 < argc ) {
            opts->fraction = atof( argv[++i] );
            magma_assert( opts->fraction >= 0 && opts->fraction <= 1,
                          "error: --fraction %s is invalid; ensure fraction in [0,1].\n", argv[i] );
        }
        else if ( strcmp("--tolerance", argv[i]) == 0 && i+1 < argc ) {
            opts->tolerance = atof( argv[++i] );
            magma_assert( opts->tolerance >= 0 && opts->tolerance <= 1000,
                          "error: --tolerance %s is invalid; ensure tolerance in [0,1000].\n", argv[i] );
        }
        else if ( strcmp("--panel_nthread", argv[i]) == 0 && i+1 < argc ) {
            opts->panel_nthread = atoi( argv[++i] );
            magma_assert( opts->panel_nthread > 0,
                          "error: --panel_nthread %s is invalid; ensure panel_nthread > 0.\n", argv[i] );
        }
        else if ( strcmp("--fraction_dcpu", argv[i]) == 0 && i+1 < argc ) {
            opts->fraction_dcpu = atof( argv[++i] );
            magma_assert( opts->fraction_dcpu > 0 && opts->fraction_dcpu<=1,
                          "error: --fraction_dcpu %s is invalid; ensure fraction_dcpu in [0, 1]\n", argv[i] );
        }
        // ----- boolean arguments
        // check results
        else if ( strcmp("-c",         argv[i]) == 0 ||
                  strcmp("--check",    argv[i]) == 0 ) { opts->check  = 1; }
        else if ( strcmp("-c2",        argv[i]) == 0 ||
                  strcmp("--check2",   argv[i]) == 0 ) { opts->check  = 2; }
        else if ( strcmp("--nocheck",  argv[i]) == 0 ) { opts->check  = 0; }
        else if ( strcmp("-l",         argv[i]) == 0 ||
                  strcmp("--lapack",   argv[i]) == 0 ) { opts->lapack = true;  }
        else if ( strcmp("--nolapack", argv[i]) == 0 ) { opts->lapack = false; }
        else if ( strcmp("--warmup",   argv[i]) == 0 ) { opts->warmup = true;  }
        else if ( strcmp("--nowarmup", argv[i]) == 0 ) { opts->warmup = false; }
        else if ( strcmp("--all",      argv[i]) == 0 ) { opts->all    = true;  }
        else if ( strcmp("--notall",   argv[i]) == 0 ) { opts->all    = false; }
        else if ( strcmp("--verbose",  argv[i]) == 0 ) { opts->verbose= true;  }
        
        // ----- lapack flag arguments
        else if ( strcmp("-L",  argv[i]) == 0 ) { opts->uplo = MagmaLower; }
        else if ( strcmp("-U",  argv[i]) == 0 ) { opts->uplo = MagmaUpper; }
        else if ( strcmp("-F",  argv[i]) == 0 ) { opts->uplo = MagmaUpperLower; }
        
        else if ( strcmp("-NN", argv[i]) == 0 ) { opts->transA = MagmaNoTrans;   opts->transB = MagmaNoTrans;   }
        else if ( strcmp("-NT", argv[i]) == 0 ) { opts->transA = MagmaNoTrans;   opts->transB = MagmaTrans;     }
        else if ( strcmp("-NC", argv[i]) == 0 ) { opts->transA = MagmaNoTrans;   opts->transB = MagmaConjTrans; }
        else if ( strcmp("-TN", argv[i]) == 0 ) { opts->transA = MagmaTrans;     opts->transB = MagmaNoTrans;   }
        else if ( strcmp("-TT", argv[i]) == 0 ) { opts->transA = MagmaTrans;     opts->transB = MagmaTrans;     }
        else if ( strcmp("-TC", argv[i]) == 0 ) { opts->transA = MagmaTrans;     opts->transB = MagmaConjTrans; }
        else if ( strcmp("-CN", argv[i]) == 0 ) { opts->transA = MagmaConjTrans; opts->transB = MagmaNoTrans;   }
        else if ( strcmp("-CT", argv[i]) == 0 ) { opts->transA = MagmaConjTrans; opts->transB = MagmaTrans;     }
        else if ( strcmp("-CC", argv[i]) == 0 ) { opts->transA = MagmaConjTrans; opts->transB = MagmaConjTrans; }
        else if ( strcmp("-T",  argv[i]) == 0 ) { opts->transA = MagmaTrans;     }
        else if ( strcmp("-C",  argv[i]) == 0 ) { opts->transA = MagmaConjTrans; }
        
        else if ( strcmp("-SL", argv[i]) == 0 ) { opts->side  = MagmaLeft;  }
        else if ( strcmp("-SR", argv[i]) == 0 ) { opts->side  = MagmaRight; }
        
        else if ( strcmp("-DN", argv[i]) == 0 ) { opts->diag  = MagmaNonUnit; }
        else if ( strcmp("-DU", argv[i]) == 0 ) { opts->diag  = MagmaUnit;    }
        
        else if ( strcmp("-UA", argv[i]) == 0 ) { opts->jobu  = MagmaAllVec;       }
        else if ( strcmp("-US", argv[i]) == 0 ) { opts->jobu  = MagmaSomeVec;      }
        else if ( strcmp("-UO", argv[i]) == 0 ) { opts->jobu  = MagmaOverwriteVec; }
        else if ( strcmp("-UN", argv[i]) == 0 ) { opts->jobu  = MagmaNoVec;        }
        
        else if ( strcmp("-VA", argv[i]) == 0 ) { opts->jobvt = MagmaAllVec;       }
        else if ( strcmp("-VS", argv[i]) == 0 ) { opts->jobvt = MagmaSomeVec;      }
        else if ( strcmp("-VO", argv[i]) == 0 ) { opts->jobvt = MagmaOverwriteVec; }
        else if ( strcmp("-VN", argv[i]) == 0 ) { opts->jobvt = MagmaNoVec;        }
        
        else if ( strcmp("-JN", argv[i]) == 0 ) { opts->jobz  = MagmaNoVec; }
        else if ( strcmp("-JV", argv[i]) == 0 ) { opts->jobz  = MagmaVec;   }
        
        else if ( strcmp("-LN", argv[i]) == 0 ) { opts->jobvl = MagmaNoVec; }
        else if ( strcmp("-LV", argv[i]) == 0 ) { opts->jobvl = MagmaVec;   }
        
        else if ( strcmp("-RN", argv[i]) == 0 ) { opts->jobvr = MagmaNoVec; }
        else if ( strcmp("-RV", argv[i]) == 0 ) { opts->jobvr = MagmaVec;   }
        
        // ----- misc
        else if ( strcmp("-x",          argv[i]) == 0 ||
                  strcmp("--exclusive", argv[i]) == 0 ) {
            #ifdef USE_FLOCK
            opts->flock_op = LOCK_EX;
            #else
            fprintf( stderr, "ignoring %s: USE_FLOCK not defined; flock not supported.\n", argv[i] );
            #endif
        }
        
        // ----- usage
        else if ( strcmp("-h",     argv[i]) == 0 ||
                  strcmp("--help", argv[i]) == 0 ) {
            fprintf( stderr, usage, argv[0], MAX_NTEST );
            exit(0);
        }
        else {
            fprintf( stderr, "error: unrecognized option %s\n", argv[i] );
            exit(1);
        }
    }
    
    // if -N or --range not given, use default range
    if ( ntest == 0 ) {
        int n2 = 1024 + 64;
        for( int i = 0; i < MAX_NTEST; ++i ) {
            opts->msize[i] = n2;
            opts->nsize[i] = n2;
            opts->ksize[i] = n2;
            n2 += 1024;
        }
        ntest = 10;
    }
    assert( ntest <= MAX_NTEST );
    opts->ntest = ntest;
    
    // fill in msize[:], nsize[:], ksize[:] if -m, -n, -k were given
    if ( m >= 0 ) {
        for( int j = 0; j < MAX_NTEST; ++j ) {
            opts->msize[j] = m;
        }
    }
    if ( n >= 0 ) {
        for( int j = 0; j < MAX_NTEST; ++j ) {
            opts->nsize[j] = n;
        }
    }
    if ( k >= 0 ) {
        for( int j = 0; j < MAX_NTEST; ++j ) {
            opts->ksize[j] = k;
        }
    }
    
    // find max dimensions
    opts->mmax = 0;
    opts->nmax = 0;
    opts->kmax = 0;
    for( int i = 0; i < ntest; ++i ) {
        opts->mmax = max( opts->mmax, opts->msize[i] );
        opts->nmax = max( opts->nmax, opts->nsize[i] );
        opts->kmax = max( opts->kmax, opts->ksize[i] );
    }

    // disallow jobu=O, jobvt=O
    if ( opts->jobu == MagmaOverwriteVec && opts->jobvt == MagmaOverwriteVec ) {
        printf( "jobu and jobvt cannot both be Overwrite.\n" );
        exit(1);
    }
    
    // lock file
    #ifdef USE_FLOCK
    opts->flock_fd = open_lockfile( lockfile, opts->flock_op );
    #endif
    
    // set device
    magma_setdevice( opts->device );
}
Пример #7
0
ledger_status ledger_partition_open(ledger_partition *partition, const char *topic_path,
                                    unsigned int partition_number, ledger_partition_options *options) {
    ledger_status rc;
    int fd = 0;
    int lock_fd = 0;
    char part_num[5];
    ssize_t path_len;
    char *partition_path = NULL;
    struct stat st;

    partition->path = NULL;
    partition->opened = false;
    partition->number = partition_number;

    rc = snprintf(part_num, 5, "%d", partition_number);
    ledger_check_rc(rc > 0, LEDGER_ERR_GENERAL, "Error building partition dir part");

    path_len = ledger_concat_path(topic_path, part_num, &partition_path);
    ledger_check_rc(path_len > 0, path_len, "Failed to construct partition directory path");

    memcpy(&partition->options, options, sizeof(ledger_partition_options));
    partition->path = partition_path;
    partition->path_len = path_len;

    rc = mkdir(partition_path, 0755);
    ledger_check_rc(rc == 0 || errno == EEXIST, LEDGER_ERR_MKDIR, "Failed to create partition directory");

    init_meta(&partition->meta);

    fd = open_meta(partition);
    ledger_check_rc(fd > 0, rc, "Failed to open meta file");

    rc = fstat(fd, &st);
    ledger_check_rc(rc == 0, LEDGER_ERR_IO, "Failed to stat meta file");

    if(st.st_size == 0) {
        rc = add_journal(partition, fd);
        ledger_check_rc(rc == LEDGER_OK, rc, "Failed to add initial journal");

        rc = fstat(fd, &st);
        ledger_check_rc(rc == 0, LEDGER_ERR_IO, "Failed to restat meta file");
    }

    rc = remap_meta(partition, fd, st.st_size);
    ledger_check_rc(rc == LEDGER_OK, rc, "Failed to read memory mapped meta file");

    lock_fd = open_lockfile(partition);
    ledger_check_rc(lock_fd > 0, rc, "Failed to open lockfile");

    rc = fstat(lock_fd, &st);
    ledger_check_rc(rc == 0, LEDGER_ERR_IO, "Failed to stat lockfile");

    if(st.st_size == 0) {
        rc = create_locks(partition, lock_fd);
        ledger_check_rc(rc == LEDGER_OK, rc, "Failed to create partition locks");

        rc = fstat(lock_fd, &st);
        ledger_check_rc(rc == 0, LEDGER_ERR_IO, "Failed to restat lock file");
    }

    rc = map_lockfile(partition, lock_fd, st.st_size);
    ledger_check_rc(rc == LEDGER_OK, rc, "Failed to read memory mapped lock file");

    close(fd);
    close(lock_fd);

    ledger_signal_init(&partition->message_signal);

    partition->opened = true;

    return LEDGER_OK;

error:
    if(partition_path) {
        free(partition_path);
    }
    if(fd) {
        close(fd);
    }
    if(lock_fd) {
        close(lock_fd);
    }
    return rc;
}
void simplesample_amqp_run(void)
{

	if (platform_init() != 0)
    {
        printf("Failed to initialize the platform.\r\n");
        exit(1);
    }
    
	if (serializer_init(NULL) != SERIALIZER_OK)
	{
		(void)printf("Failed on serializer_init\r\n");
	}
	else
	{
		/* Setup IoTHub client configuration */
		IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol);
		srand((unsigned int)time(NULL));
		int avgMTemperature = 10;

		if (iotHubClientHandle == NULL)
		{
			(void)printf("Failed on IoTHubClient_Create\r\n");
		}
		else
		{
			#ifdef MBED_BUILD_TIMESTAMP
			// For mbed add the certificate information
			if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
			{
				(void)printf("failure to set option \"TrustedCerts\"\r\n");
			}
			#endif // MBED_BUILD_TIMESTAMP

			ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
			if (myWeather == NULL)
			{
				(void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
			}
			else
			{
				unsigned char* destination;
				size_t destinationSize;

				if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
				{
					printf("unable to IoTHubClient_SetMessageCallback\r\n");
				}
				else
				{
					int Lock_fd__i = open_lockfile(LOCKFILE);
					if (setuid(getuid()) < 0)
					{
						perror("Dropping privileges failed. (did you use sudo?)\n");
						exit(EXIT_FAILURE);
					}

					int Result__i = wiringPiSetup();
					if (Result__i != 0) exit(Result__i);

					int Spi_fd__i = wiringPiSPISetup(Spi_channel__i, Spi_clock__i);
					if (Spi_fd__i < 0)
					{
						printf("Can't setup SPI, error %i calling wiringPiSPISetup(%i, %i)  %s\n",
							Spi_fd__i, Spi_channel__i, Spi_clock__i, strerror(Spi_fd__i));
						exit(Spi_fd__i);
					}

					int Init_result__i = bme280_init(Spi_channel__i);
					if (Init_result__i != 1)
					{
						printf("It appears that no BMP280 module on Chip Enable %i is attached. Aborting.\n", Spi_channel__i);
						exit(1);
					}

					pinMode(Red_led_pin__i, OUTPUT);
					pinMode(Grn_led_pin__i, OUTPUT);

					////////////////


					// Read the Temp & Pressure module.
					float Temp_C__f = -300.0;
					float Pressure_Pa__f = -300;
					float Humidity_pct__f = -300;
					Result__i = bme280_read_sensors(&Temp_C__f, &Pressure_Pa__f,
						&Humidity_pct__f);

					if (Result__i == 1)
					{
						printf("Temperature = %.1f *C  Pressure = %.1f Pa  Humidity = %1f %%\n",
							Temp_C__f, Pressure_Pa__f, Humidity_pct__f);
					}
					else
					{
						printf("Unable to read BME280 on pin %i\n", Spi_channel__i);
					}

					char buff[11];
					int timeNow = 0;

					int c;
					while (1)
					{
						timeNow = (int)time(NULL);

						sprintf(buff, "%d", timeNow);

						myWeather->DeviceId = "raspy";
						myWeather->EventTime = buff;

						if (Result__i == 1)
						{
							myWeather->MTemperature = Temp_C__f;
							printf("Humidity = %.1f%% Temperature = %.1f*C \n",
								Humidity_pct__f, Temp_C__f);
						}
						else
						{
							myWeather->MTemperature = 404.0;
							printf("Unable to read BME280 on pin %i\n", Spi_channel__i);
							pinMode(Red_led_pin__i, OUTPUT);
						}


						if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->EventTime, myWeather->MTemperature) != IOT_AGENT_OK)
						{
							(void)printf("Failed to serialize\r\n");
						}
						else
						{
							sendMessage(iotHubClientHandle, destination, destinationSize);
						}

						delay(5000);
					}
					/* wait for commands */
				  // (void)getchar();

					close_lockfile(Lock_fd__i);

				}
				DESTROY_MODEL_INSTANCE(myWeather);
			}
			IoTHubClient_Destroy(iotHubClientHandle);
		}
		serializer_deinit();
	}
}