[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/harvard/spaulding/src/gui/sessions/readSD Makefile, NONE, 1.1 MultiChanSampling.h, NONE, 1.1 downloadSessions.pl, NONE, 1.1 nodeInfo.c, NONE, 1.1 readSD.c, NONE, 1.1

Konrad Lorincz konradlorincz at users.sourceforge.net
Wed Dec 12 12:44:12 PST 2007


Update of /cvsroot/tinyos/tinyos-1.x/contrib/harvard/spaulding/src/gui/sessions/readSD
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv28220/src/gui/sessions/readSD

Added Files:
	Makefile MultiChanSampling.h downloadSessions.pl nodeInfo.c 
	readSD.c 
Log Message:
Added readSD code

--- NEW FILE: Makefile ---
FILE=readSD

all:
	gcc -Wall $(FILE).c -o $(FILE)

run:
	./$(FILE)

clean:
	rm $(FILE)


--- NEW FILE: MultiChanSampling.h ---
/*
 * Copyright (c) 2006
 *	The President and Fellows of Harvard College.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/**
 * <pre>URL: http://www.eecs.harvard.edu/~konrad/projects/shimmer</pre>
 * @author Konrad Lorincz
 * @version 1.0, November 10, 2006
 */
#ifndef MULTICHANSAMPLING_H
#define MULTICHANSAMPLING_H

//typedef uint32_t  time_t;
typedef uint16_t  sample_t;
typedef uint8_t   channelID_t;

// The maximum number of channels supported by the system
enum {MCS_MAX_NBR_CHANNELS_SAMPLED = 6};
enum {CHAN_INVALID = 255};

#endif

--- NEW FILE: downloadSessions.pl ---
#!/usr/bin/perl
use strict;
use Cwd;
use File::Basename;

my $SD_DEVICE_PATH = "/dev/sda";
#my $SD_DEVICE_PATH = "/dev/sdd";

my $node = @ARGV[0];
if ($node eq "") {
    my $progName = basename($0);
    print "Usage: $progName <nodeID>\n\n";
    print "  Example: ./$progName 10 \n";
    print "  Example: ./$progName 11\n";
    exit(1);
}


# (1) - Read in all files in current directory
opendir(DIR, '.') || die "Cannot open . for reading\n";
my @subDirs = sort( grep(/subj-/ && -d $_, readdir(DIR)) );
closedir(DIR);

foreach my $currDir (@subDirs) {
    #print "Processing $currDir ...\n";
    system("./readSD $currDir sessionInfo.txt $node $SD_DEVICE_PATH");
}

--- NEW FILE: nodeInfo.c ---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>


/**
 * Returns 1 if the headblockID for nodeId was found, 0 otherwise; 
 */
int getNodeInfo(char *fileName, uint16_t nodeID, uint32_t *headBlockID, uint32_t *nbrBlocks)
{
    char word[50];
    int atNodes = 0;
    int atNbrBlocks = 0;
    int returnVal = 0;
    FILE *filePtr;
 
    filePtr = fopen(fileName, "r");

    
    while (fscanf(filePtr, "%s", word) != EOF) 
    {
        if (atNbrBlocks == 1) {
            *nbrBlocks = strtoul(strtok(word, ","), NULL, 0);
            atNbrBlocks = 0;
        }
        else if (atNodes == 1) {
            int node;
            int block;
            const char delimeters[] = "<,>";
            //printf("nodeInfo= %s\n", word);
            node  = strtoul(strtok(word, delimeters), NULL, 0);
            block = strtoul(strtok(NULL, delimeters), NULL, 0);
            //printf("  nodeID= %u\n", node);
            //printf("  blockID= %u\n", block);
            if (node == nodeID) {
                *headBlockID = block;
                
                returnVal = 1;
            }
        }
        else if (strcmp(word, "nbrBlocksToDownload=") == 0) {
            atNbrBlocks = 1;
        }
        else if (strcmp(word, "<nodeID,headBlockID>=") == 0) {
            atNodes = 1;
        }
        
    }

    return returnVal;
}

/*
int main(int argc, char** argv)
{
    char *fileName = argv[1];
    uint16_t nodeID = strtoul(argv[2], NULL, 0);
    uint32_t headBlockID;
    uint32_t nbrBlocks;
    
    
    if (getNodeInfo(fileName, nodeID, &headBlockID, &nbrBlocks))        
        printf("--> nodeID= %u, headBlockID= %u, nbrBlocks= %u\n", nodeID, headBlockID, nbrBlocks);
    else
        printf("--> nodeID= %u, NOT FOUND\n", nodeID);
    
    
    return 0;
}
*/

--- NEW FILE: readSD.c ---
/**
 * Konrad Lorincz
 * November 15, 2007
 */
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include "MultiChanSampling.h"
#include "nodeInfo.c"

#define FLASH_BLOCK_SIZE 512ul
#define DATASTORE_BLOCK_SIZE 256ul
#define BLOCK_DATA_SIZE (256-sizeof(uint32_t))

#define GLOBALTIME_RATE_HZ 32768
#define SAMPLING_RATE_HZ 100

typedef struct FlashInfo
{
    uint32_t hash;
    uint32_t tail;
    uint32_t head;
} FlashInfo;

typedef struct Block {
    uint32_t sqnNbr;
    uint8_t data[256-sizeof(uint32_t)];
} Block;


#define SAMPLE_CHUNK_NUM_SAMPLES ((BLOCK_DATA_SIZE - 2*sizeof(uint32_t) - 2*sizeof(uint16_t) - sizeof(channelID_t)*MCS_MAX_NBR_CHANNELS_SAMPLED) / sizeof(sample_t))


typedef struct SampleChunk {
    uint32_t    localTime;
    uint32_t    globalTime;
    uint16_t    timeSynched;         // true or false 
    uint16_t    nbrMultiChanSamples; // Number of samples

    channelID_t channelIDs[MCS_MAX_NBR_CHANNELS_SAMPLED];            
    sample_t    samples[SAMPLE_CHUNK_NUM_SAMPLES];
} SampleChunk;         



// sudo dd if=/dev/sdd of=out.raw count=1

void getFlashInfo(FlashInfo *flashInfoPtr, FILE *filePtr)
{
    fseek(filePtr, 0, SEEK_SET);
    fread(flashInfoPtr, sizeof(FlashInfo), 1, filePtr);
}

void getBlock(Block *blockPtr, uint32_t blockID, uint32_t nbrBlocks, FILE *filePtr)
{
//    uint32_t blocksStartOffset = FLASH_BLOCK_SIZE;
    uint32_t blocksStartOffset = 0;
    uint32_t offset = blocksStartOffset + (blockID-nbrBlocks)*FLASH_BLOCK_SIZE;

    fseek(filePtr, offset, SEEK_SET);
    fread(blockPtr, sizeof(Block), 1, filePtr);
}

void printBlock(FILE *filePtr, Block *blockPtr)
{
    uint16_t i = 0;
    uint16_t samp = 0;
    SampleChunk *scPtr = (SampleChunk*) blockPtr->data;
    fprintf(filePtr, "# blockID= %u:  localTime= %u, globalTime= %u, timeSynched= %u, nbrMultiChanSamples= %u, channelIDs= {",
           blockPtr->sqnNbr, scPtr->localTime, scPtr->globalTime, scPtr->timeSynched, scPtr->nbrMultiChanSamples);

    for (i = 0; i < MCS_MAX_NBR_CHANNELS_SAMPLED; ++i) {
        if (i < MCS_MAX_NBR_CHANNELS_SAMPLED - 1)
            fprintf(filePtr, "%u ", scPtr->channelIDs[i]);
        else
            fprintf(filePtr, "%u}", scPtr->channelIDs[i]);
    }    
    
    
    for (samp = 0; samp < scPtr->nbrMultiChanSamples &&
                   samp < SAMPLE_CHUNK_NUM_SAMPLES; ++samp) {
        if (samp % MCS_MAX_NBR_CHANNELS_SAMPLED == 0) {
            double globalTimeSec = scPtr->globalTime / (double)GLOBALTIME_RATE_HZ;
            double sampleTimeSec = globalTimeSec + (double)(samp/MCS_MAX_NBR_CHANNELS_SAMPLED)/
                                                   (double)SAMPLING_RATE_HZ;
            fprintf(filePtr, "\n%.4f ", sampleTimeSec);
        }
        fprintf(filePtr, "%u ", scPtr->samples[samp]);
    }
    fprintf(filePtr, "\n");

    #if 0
    {    // Hex Dump
        uint32_t i = 0;
        uint8_t hexVal = 0x0;
        for (i = 0; i < sizeof(blockPtr->data); ++i) {
            hexVal = (uint8_t) *(((uint8_t*)&blockPtr->data) + i);
            if (hexVal <= 0x0f)
                {fprintf(filePtr, "0%x ", hexVal);} 
            else
                {fprintf(filePtr, "%x ", hexVal);}
            
            if ( (i+1) == sizeof(blockPtr->data) ||
                 (i+1) % 16 == 0 ) 
                {fprintf(filePtr, "\n");}
            else if ((i+1) % 4 == 0)
                {fprintf(filePtr, "   ");}        
        }
    }
    #endif


}


int main(int argc, char** argv)
{
    char *sessionDir = argv[1];
    char *sessionInfoFile = argv[2];
    uint16_t nodeID = strtoul(argv[3], NULL, 0);
    char *devicePath = argv[4];
    uint32_t startBlockID;
    uint32_t nbrBlocks;
    char infoFilePath[128];
    sprintf(infoFilePath, "%s/%s", sessionDir, sessionInfoFile);

    printf("=> nodeID= %d: %s\n", nodeID, infoFilePath);

    if (getNodeInfo(infoFilePath, nodeID, &startBlockID, &nbrBlocks)) {     
        // (1) - Open the SD card 
        //FlashInfo flashInfo;
        Block block;
        uint32_t i = 0;
        FILE *sdFilePtr = fopen(devicePath, "rb");
        FILE *outFilePtr;
        char outFile[128];
        sprintf(outFile, "%s/node-%u.samplesSD", sessionDir, nodeID);
        outFilePtr = fopen(outFile, "w");

        printf("   found sessionInfo for nodeID= %u, startBlockID= %u, nbrBlocks= %u\n", nodeID, startBlockID, nbrBlocks);

        for (i = startBlockID; i < (startBlockID + nbrBlocks); ++i) {
            getBlock(&block, i, nbrBlocks, sdFilePtr);
            printBlock(outFilePtr, &block);
        }
    
        fclose(outFilePtr);
        fclose(sdFilePtr);
    }
    else
        printf("  nodeID= %u, NOT FOUND\n", nodeID);

        
    return 0;
}



/* int main(int argc, char** argv) */
/* { */
/*     FlashInfo flashInfo; */
/*     Block block; */
/*     int i = 0; */
/*     FILE *filePtr; */
/*     uint32_t startBlockID = 1; */
/*     uint32_t nbrBlocks = 0; */

/*     if (argc != 3) { */
/*         printf("ERROR: Wrong number of arguments\n"); */
/*         return 1; */
/*     } */
/*     else { */
/*         startBlockID = atoi(argv[1]); */
/*         nbrBlocks = atoi(argv[2]); */
/*         if (startBlockID < 1 || nbrBlocks < 1) { */
/*             printf("ERROR: Bad blockID or nmbrBlocks\n"); */
/*             return 1; */
/*         } */
/*     } */

/*     printf("startBlockID= %u, nbr= %u\n", startBlockID, nbrBlocks); */
/*     //filePtr = fopen("sd.img","rb"); */
/*     //filePtr = fopen("/dev/sdd","rb"); */
/*     filePtr = fopen("/dev/sdb","rb"); */

/*     getFlashInfo(&flashInfo, filePtr); */
/*     printf("hash= %u, tail= %u, head= %u\n", flashInfo.hash, flashInfo.tail, flashInfo.head); */

/*     for (i = startBlockID; i < (startBlockID + nbrBlocks); ++i) { */
/*         getBlock(&block, i, filePtr); */
/*         printBlock(&block); */
/*     } */

/*     printf("sizeof(uint32_t) = %d\n", sizeof(uint32_t)); */
/*     fclose(filePtr); */
/*     return 0; */
/* } */



More information about the Tinyos-contrib-commits mailing list