Example #1
0
JNIEXPORT jlong JNICALL Java_lejos_pc_comm_NXTCommLibnxt_jlibnxt_1find(JNIEnv *env, jobject obj) {
  nxt_t *nxt;
  nxt_error_t nxt_err;
	
  nxt_err = nxt_init(&nxt);
	
  if (nxt_err == NXT_OK) {
    nxt_err = nxt_find(nxt);
    if (nxt_err == NXT_OK && !(nxt_in_reset_mode(nxt))) {
      return (jlong) (unsigned long) nxt;
    }
  }
  return (jlong) 0;
}
Example #2
0
static nxweb_result tmpl_on_request(nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp) {
  nxweb_set_response_content_type(resp, "text/html");

  const char* tmpl="{% extends \"ttt\"%} {% block title %}The Very {% parent %}{% endblock %}";
  tmpl=nxb_copy_obj(req->nxb, tmpl, strlen(tmpl)+1);
  nxt_context ctx;
  nxt_init(&ctx, req->nxb, tmpl_load);
  nxt_parse(&ctx, req->uri, (char*)tmpl, strlen(tmpl));
  nxt_merge(&ctx);
  resp->content=nxt_serialize(&ctx);
  resp->content_length=strlen(resp->content);

  return NXWEB_OK;
}
Example #3
0
int main(int argc, char *argv[])
{
  nxt_t *nxt;
  nxt_error_t err;
  char *bios_file;

  printf("#==========================================================#\n");
  printf("#                  NXT BIOS Flash Utility                  #\n");
  printf("#==========================================================#\n\n");

  if (argc == 1)
    {
      bios_file = (char *) calloc(1,256);
      strcat(bios_file,"../ecrobot/bios/nxt_bios_rom.bin");
    }
  else if (argc != 2)
    {
      printf("Syntax: %s [<NXT BIOS binary>]\n"
             "\n"
             "Example: %s nxt_bios_rom.bin\n", argv[0], argv[0]);
      exit(1);
    }
  else
    {
      bios_file = argv[1];     
    }

  printf("Checking NXT BIOS %s ... ", bios_file);
  NXT_HANDLE_ERR(nxt_firmware_validate(bios_file, MAX_BIOS_FLASH_PAGE ), NULL,
                 "Error in NXT BIOS file");
  printf("NXT BIOS OK.\n");

  NXT_HANDLE_ERR(nxt_init(&nxt), NULL,
                 "Error during library initialization");

  err = nxt_find(nxt);
  if (err)
    {
      if (err == NXT_NOT_PRESENT)
        printf("NXT not found. Is it properly plugged in via USB?\n");
      else
        NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT");
      exit(1);
    }

  if (!nxt_in_reset_mode(nxt))
    {
      printf("NXT found, but not running in reset mode.\n");
      printf("Please reset your NXT manually and restart this program.\n");
      exit(2);
    }

  NXT_HANDLE_ERR(nxt_open(nxt), NULL, "Error while connecting to NXT");

  printf("NXT device in reset mode located and opened.\n"
         "Starting NXT BIOS flash procedure now...\n");

  NXT_HANDLE_ERR(nxt_firmware_flash(nxt, bios_file, 0, MAX_BIOS_FLASH_PAGE, 1, 0), nxt,
                 "Error flashing NXT BIOS");
  printf("NXT BIOS flash complete.\n");

  NXT_HANDLE_ERR(nxt_jump(nxt, 0x00100000), nxt, "Error booting new NXT BIOS");
  printf("New NXT BIOS started!\n");

  NXT_HANDLE_ERR(nxt_close(nxt), NULL,
                 "Error while closing connection to NXT");
  return 0;
}
Example #4
0
int main (int argc, char **argv)
{
    const char *filename;
    tx_data_t data;
    nxt_link_t nxt;
    uint8_t buffer[USB_BUFSIZE];
    size_t read, sent;
    ssize_t total;
    FILE *ro_data;

    if (argc < 2) {
        fprintf(stderr, "Provide the ROM image as first parameter.\n");
        exit(1);
    }
    filename = argv[1];

    // FIXME this should be parametrized. Basically it's the address of
    // the first unlocked flash memory location.
    data.start_address = 0x108000;

    total = data.length = get_ro_size(filename);
    if (total == -1) {
        fprintf(stderr, "Invalid ROM image\n");
        exit(1);
    }
    
    if (nxt_init(&nxt)) {
        fprintf(stderr, "Initialization failed\n");
        exit(1);
    }

    ro_data = fopen(filename, "rb");
    if (ro_data == NULL) {
        fprintf(stderr, "Error in opening %s\n", filename);
        nxt_free(&nxt);
    }

    fprintf(stdout, "Sync sent: %d bytes\n"
                    "           start=%p\n"
                    "           length=%d\n",
            (int)nxt_send(&nxt, (void *)&data, sizeof(tx_data_t)),
            (void *)(uintmax_t)data.start_address,
            data.length);

    while ((read = fread((void *)buffer, 1, USB_BUFSIZE, ro_data)) > 0) {
        sent = nxt_send(&nxt, (void *)buffer, read);
        data.length -= sent;
        data.start_address += sent;
        display_percentage(total, &data);
        if (sent == -1) {
            fprintf(stderr, "Send failed: %s\r", nxt_libusb_strerr(&nxt));
            break;
        }
        usleep(DELAY_uSECS);
    }
    fprintf(stdout, "\nCompleted\n");

    if (ferror(ro_data)) {
        fprintf(stderr, "File error in %s\n", filename);
    }

    fclose(ro_data);
    nxt_free(&nxt);
    exit(0);
}
Example #5
0
int main(int argc, char *argv[])
{
  nxt_t *nxt;
  nxt_error_t err;
  char *fw_file;
  FILE *f;
  int ret;
  char *buf;
  long lsize;

  if (argc != 2)
    {
      printf("Syntax: %s <C program image to write>\n"
             "\n"
             "Example: %s flashled.bin\n", argv[0], argv[0]);
      exit(1);
    }

  fw_file = argv[1];

  NXT_HANDLE_ERR(nxt_init(&nxt), NULL,
                 "Error during library initialization");

  err = nxt_find(nxt);
  if (err)
    {
      if (err == NXT_NOT_PRESENT)
        printf("NXT not found. Is it properly plugged in via USB?\n");
      else
        NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT");
      exit(1);
    }

  f = fopen(fw_file, "rb");
  if (f == NULL) NXT_HANDLE_ERR(8, NULL, "Error opening file");

  fseek (f, 0, SEEK_END);
  lsize=ftell (f);

  rewind(f);

  buf = (char *) malloc(lsize);
  if (buf == NULL) NXT_HANDLE_ERR(8, NULL, "Error allocating memory");

  ret = fread(buf, 1, lsize, f);

  if (ret != (int) lsize) NXT_HANDLE_ERR(8, NULL, "Error reading file");

  printf("File size is %ld\n", lsize);

  fclose(f);

  if (!nxt_in_reset_mode(nxt))
    {
      printf("NXT found, but not running in reset mode.\n");
      printf("Please reset your NXT manually and restart this program.\n");
      exit(2);
    }

  NXT_HANDLE_ERR(nxt_open(nxt), NULL, "Error while connecting to NXT");

  printf("NXT device in reset mode located and opened.\n"
         "Starting C program now...\n");

  // Send the C program
  NXT_HANDLE_ERR(nxt_send_file(nxt, 0x202000, buf, (int) lsize), nxt, 
                  "Error Sending file");

  NXT_HANDLE_ERR(nxt_jump(nxt, 0x202000), nxt,
                 "Error jumping to C program");

  NXT_HANDLE_ERR(nxt_close(nxt), NULL,
                 "Error while closing connection to NXT");
  return 0;
}
Example #6
0
int main(int argc, char *argv[])
{
  nxt_t *nxt;
  nxt_error_t err;
  char *fw_file, *menu_file;
  char *nxj_home;

  if (argc == 1)
    {
      nxj_home = getenv("NXJ_HOME");
      
      if (nxj_home == NULL || strlen(nxj_home) == 0)
        {
          printf("NXJ_HOME is not defined\n");
          exit(1);
        }
      printf("NXJ_HOME is %s\n", nxj_home);
      
      fw_file = (char *) calloc(1,256);
      strcpy(fw_file, nxj_home);
      strcat(fw_file,"/bin/lejos_nxt_rom.bin");
      menu_file = calloc(1,256);
      strcpy(menu_file,nxj_home);
      strcat(menu_file,"/bin/StartUpText.bin");
    }
  else if (argc != 3)
    {
      printf("Syntax: %s [<VM binary> <java menu binary>]\n"
             "\n"
             "Example: %s lejos_nxt_rom.bin StartUpText.bin\n", argv[0], argv[0]);
      exit(1);
    }
  else
    {
      fw_file = argv[1];     
      menu_file = argv[2];
    }
  
  printf("Checking VM %s ... ", fw_file);
  NXT_HANDLE_ERR(nxt_firmware_validate(fw_file, MAX_VM_PAGES ), NULL,
                 "Error in VM file");
  printf("VM OK.\n");
 
  printf("Checking Menu %s ... ", menu_file);
  NXT_HANDLE_ERR(nxt_firmware_validate(menu_file, (MAX_MENU_PAGES ) - 4), NULL,
                 "Error in Menu file");
  printf("Menu OK.\n");

  NXT_HANDLE_ERR(nxt_init(&nxt), NULL,
                 "Error during library initialization");

  err = nxt_find(nxt);
  if (err)
    {
      if (err == NXT_NOT_PRESENT)
        printf("NXT not found. Is it properly plugged in via USB?\n");
      else
        NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT");
      exit(1);
    }

  if (!nxt_in_reset_mode(nxt))
    {
      printf("NXT found, but not running in reset mode.\n");
      printf("Please reset your NXT manually and restart this program.\n");
      exit(2);
    }

  NXT_HANDLE_ERR(nxt_open(nxt), NULL, "Error while connecting to NXT");

  printf("NXT device in reset mode located and opened.\n"
         "Starting VM flash procedure now...\n");

  NXT_HANDLE_ERR(nxt_firmware_flash(nxt, fw_file, 0, MAX_VM_PAGES, 1, 0), nxt,
                 "Error flashing VM");
  printf("VM flash complete.\n");

  printf("Starting menu flash procedure now...\n");
  
  NXT_HANDLE_ERR(nxt_firmware_flash(nxt, menu_file, MAX_VM_PAGES, MAX_MENU_PAGES, 0, 1), nxt,
                 "Error flashing menu");
  printf("Menu flash complete.\n");
  
  NXT_HANDLE_ERR(nxt_jump(nxt, 0x00100000), nxt,
                 "Error booting new firmware");
  printf("New firmware started!\n");
  printf("If battery level reads 0.0, remove and re-insert a battery\n");

  NXT_HANDLE_ERR(nxt_close(nxt), NULL,
                 "Error while closing connection to NXT");
  return 0;
}