Ejemplo n.º 1
0
int main( int argc, char **argv ) {
    //SQL vars
    char *sqlCmd;
    char *sqlErr;
    sqlite3 *requestDB;
    int rc = sqlite3_open("observations.db", &requestDB);
    cAssertMsg((rc <= 0), "Failed to open observations.db\n");
    // Buffer for received 80211 frames
    uint8_t pktBuff[PKT_BUFF_SZ];
    // Setup a scoket for *any* protocol
    struct sockaddr socketAddr;
    socklen_t addrLen = sizeof(socketAddr);
    // Family, type, protocol
    int sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    cAssertMsg( (sock >= 0), "Are you running as root?\n");

    while(TRUE) {
        // Receive without needing a socket connection 
        int recvDataSize = recvfrom( sock, pktBuff, PKT_BUFF_SZ,
                                     NOFLAG, &socketAddr, &addrLen);
        // Parse it if it's a probe request
        Request request = parseRaw(pktBuff, recvDataSize);
        // Print it if it's not null
        if( request != NULL ){ 
            printf("%s  %s  \"%s\"\t%ddBm %umHz  Dist: %.02fm\n", 
                request->timeStamp,
                request->clientMAC, 
                request->SSID,
                request->RSSI,
                request->frequency,
                request->distance);
            // Add a new entry to the db
            sqlCmd = sqlite3_mprintf("INSERT INTO obs "
                    "(TIMESTAMP, MAC, SSID, RSSI, FREQ, DIST)"
                    " VALUES ('%q', '%q', '%q', %d, %u, %.02f);", 
                    request->timeStamp,
                    request->clientMAC, 
                    request->SSID,
                    request->RSSI,
                    request->frequency,
                    request->distance);
            /* Execute SQL statement */
            rc = sqlite3_exec(requestDB, sqlCmd, sqlCallback, 0, &sqlErr);
            if( rc != SQLITE_OK ){
                // printf(stderr, "SQL error: %s\n", sqlErr);
                sqlite3_free(sqlErr);
            }
        }
        free(request);  
    }

    return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
/******************************************************************************
* Main function!
******************************************************************************/
int main(int argc, char **argv) {
  int res;
  char *file = NULL;
  micronucleus *my_device = NULL;

  // parse arguments
  int run = 0;
  int file_type = FILE_TYPE_INTEL_HEX;
  int arg_pointer = 1;
  char* usage = "usage: micronucleus [--run] [--dump-progress] [--type intel-hex|raw] [--no-ansi] [--timeout integer] filename";
  progress_step = 0;
  progress_total_steps = 5; // steps: waiting, connecting, parsing, erasing, writing, (running)?
  dump_progress = 0;
  timeout = 0; // no timeout by default
  //#if defined(WIN)
  //  use_ansi = 0;
  //#else
    use_ansi = 1;
  //#endif
  
  while (arg_pointer < argc) {
    if (strcmp(argv[arg_pointer], "--run") == 0) {
      run = 1;
      progress_total_steps += 1;
    } else if (strcmp(argv[arg_pointer], "--type") == 0) {
      arg_pointer += 1;
      if (strcmp(argv[arg_pointer], "intel-hex") == 0) {
        file_type = FILE_TYPE_INTEL_HEX;
      } else if (strcmp(argv[arg_pointer], "raw") == 0) {
        file_type = FILE_TYPE_RAW;
      } else {
        printf("Unknown File Type specified with --type option");
        return EXIT_FAILURE;
      }
    } else if (strcmp(argv[arg_pointer], "--help") == 0 || strcmp(argv[arg_pointer], "-h") == 0) {
      puts(usage);
      puts("");
      puts("  --type [intel-hex, raw]: Set upload file type to either intel hex or raw");
      puts("                           bytes (intel hex is default)");
      puts("          --dump-progress: Output progress data in computer-friendly form");
      puts("                           for driving GUIs");
      puts("                    --run: Ask bootloader to run the program when finished");
      puts("                           uploading provided program");
      //#ifndef WIN
      puts("                --no-ansi: Don't use ANSI in terminal output");
      //#endif
      puts("      --timeout [integer]: Timeout after waiting specified number of seconds");
      puts("                 filename: Path to intel hex or raw data file to upload,");
      puts("                           or \"-\" to read from stdin");
      return EXIT_SUCCESS;
    } else if (strcmp(argv[arg_pointer], "--dump-progress") == 0) {
      dump_progress = 1;
    } else if (strcmp(argv[arg_pointer], "--no-ansi") == 0) {
      use_ansi = 0;
    } else if (strcmp(argv[arg_pointer], "--timeout") == 0) {
      arg_pointer += 1;
      if (sscanf(argv[arg_pointer], "%d", &timeout) != 1) {
        printf("Did not understand --timeout value\n");
        return EXIT_FAILURE;
      }
    } else {
      file = argv[arg_pointer];
    }
    
    arg_pointer += 1;
  }
  
  if (argc < 2) {
    puts(usage);
    return EXIT_FAILURE;
  }
  
  setProgressData("waiting", 1);
  if (dump_progress) printProgress(0.5);
  printf("> Please plug in the device ... \n");
  printf("> Press CTRL+C to terminate the program.\n");
  
  
  time_t start_time, current_time;
  time(&start_time);
  
  while (my_device == NULL) {
    delay(100);
    my_device = micronucleus_connect();
    
    time(&current_time);
    if (timeout && start_time + timeout < current_time) {
      break;
    }
  }
  
  if (my_device == NULL) {
    printf("> Device search timed out\n");
    return EXIT_FAILURE;
  }
  
  printf("> Device is found!\n");
  
  // wait for CONNECT_WAIT milliseconds with progress output
  float wait = 0.0f;
  setProgressData("connecting", 2);
  while (wait < CONNECT_WAIT) {
    printProgress((wait / ((float) CONNECT_WAIT)) * 0.9f);
    wait += 50.0f;
    delay(50);
  }
  
  //my_device = micronucleus_connect();
  printProgress(1.0);
    
  // if (my_device->page_size == 64) {
  //   printf("> Device looks like ATtiny85!\n");
  // } else if (my_device->page_size == 32)  {
  //   printf("> Device looks like ATtiny45!\n");
  // } else {
  //   printf("> Unsupported device!\n");
  //   return EXIT_FAILURE;
  // }
  
  printf("> Available space for user application: %d bytes\n", my_device->flash_size);
  printf("> Suggested sleep time between sending pages: %ums\n", my_device->write_sleep);
  printf("> Whole page count: %d\n", my_device->pages);
  printf("> Erase function sleep duration: %dms\n", my_device->erase_sleep);
  
  setProgressData("parsing", 3);
  printProgress(0.0);
  memset(dataBuffer, 0xFF, sizeof(dataBuffer));
  
  int startAddress = 1, endAddress = 0;
  if (file_type == FILE_TYPE_INTEL_HEX) {
    if (parseIntelHex(file, dataBuffer, &startAddress, &endAddress)) {
      printf("> Error loading or parsing hex file.\n");
      return EXIT_FAILURE;
    }
  } else if (file_type == FILE_TYPE_RAW) {
    if (parseRaw(file, dataBuffer, &startAddress, &endAddress)) {
      printf("> Error loading raw file.\n");
      return EXIT_FAILURE;
    }
  }
  
  printProgress(1.0);

  if (startAddress >= endAddress) {
    printf("> No data in input file, exiting.\n");
    return EXIT_FAILURE;
  }
  
  if (endAddress > my_device->flash_size) {
    printf("> Program file is %d bytes too big for the bootloader!\n", endAddress - my_device->flash_size);
    return EXIT_FAILURE;
  }
  
  setProgressData("erasing", 4);
  printf("> Erasing the memory ...\n");
  res = micronucleus_eraseFlash(my_device, printProgress);
  
  if (res == 1) { // erase disconnection bug workaround
    printf(">> Eep! Connection to device lost during erase! Not to worry\n");
    printf(">> This happens on some computers - reconnecting...\n");
    my_device = NULL;
    
    delay(CONNECT_WAIT);
    
    int deciseconds_till_reconnect_notice = 50; // notice after 5 seconds
    while (my_device == NULL) {
      delay(100);
      my_device = micronucleus_connect();
      deciseconds_till_reconnect_notice -= 1;
      
      if (deciseconds_till_reconnect_notice == 0) {
        printf(">> (!) Automatic reconnection not working. Unplug and reconnect\n");
        printf("   device usb connector, or reset it some other way to continue.\n");
      }
    }
    
    printf(">> Reconnected! Continuing upload sequence...\n");
    
  } else if (res != 0) {
    printf(">> Abort mission! %d error has occured ...\n", res);
    printf(">> Please unplug the device and restart the program.\n");
    return EXIT_FAILURE;
  }
  printProgress(1.0);
  
  printf("> Starting to upload ...\n");
  setProgressData("writing", 5);
  res = micronucleus_writeFlash(my_device, endAddress, dataBuffer, printProgress);
  if (res != 0) {
    printf(">> Abort mission! An error has occured ...\n");
    printf(">> Please unplug the device and restart the program.\n");
    return EXIT_FAILURE;
  }
  
  if (run) {
    
    printf("> Starting the user app ...\n");
    setProgressData("running", 6);
    printProgress(0.0);
    
    res = micronucleus_startApp(my_device);
    
    if (res != 0) {
      printf(">> Abort mission! An error has occured ...\n");
      printf(">> Please unplug the device and restart the program. \n");
      return EXIT_FAILURE;
    }
    
    printProgress(1.0);
  }
  
  printf(">> Micronucleus done. Thank you!\n");
  
  return EXIT_SUCCESS;
}