Exemple #1
0
int main(void) {
  board_init(BOARD_MODE_RUN);
  board_console_init(BOARD_DEBUG_BAUD);

  SysTick_Config(BOARD_SYSTICK_100MS / 10);

  // Initialize modem
  modem_init();

  if (!modem_enable()) error("modem enable");
  if (!modem_register(60000)) error("network register");
  if (!modem_gprs_attach(CELL_APN, CELL_USER, CELL_PWD, 30000)) error("GPRS attach");

  double lat = 0.0, lon = 0.0;
  rtc_datetime_t date;
  modem_location(&lat, &lon, &date, 30000);

  PRINTF("%04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n",
         date.year, date.month, date.day, date.hour, date.minute, date.second);

  PRINTF("Location: lat=%f, lon=%f\r\n", lat, lon);

  if (!modem_tcp_connect("api.ubirch.com", 80, 5000)) error("TCP connect");

  const char *send_data = "GET / HTTP/1.1\r\n\r\n";
  if (!modem_tcp_send((const uint8_t *) send_data, (uint8_t) strlen(send_data), 30000))
    error("simulated HTTP GET");

  uint8_t buffer[8192];
  size_t received = modem_tcp_receive(buffer, 1500, 10000);
  PRINTF("received %d bytes\r\n", received);

  dbg_dump("RCV", buffer, received);

  modem_tcp_close(1000);

  modem_disable();

  while (true) {
    delay(1000);
  }
}
Exemple #2
0
int main(void) {
  board_init();
  board_console_init(BOARD_DEBUG_BAUD);

  SysTick_Config(BOARD_SYSTICK_100MS);

  while (true) {
    i2c_init(&i2c_config);

    for (uint8_t address = 0x01; address <= 0x7f; address++) {
      PRINTF("Scanning for device 0x%02x: \e[K", address);

      status_t status = i2c_ping(address);
      switch (status) {
        case kStatus_Success:
          PRINTF("FOUND\r\n");
              break;
        case kStatus_I2C_Nak:
          PRINTF("NOT FOUND\r");
              break;
        case kStatus_I2C_ArbitrationLost:
          PRINTF("ARBITRATION LOST\r\n");
              break;
        case kStatus_I2C_Busy:
          PRINTF("BUSY\r\n");
              break;
        case kStatus_I2C_Idle:
          PRINTF("IDLE\r\n");
              break;
        case kStatus_I2C_Timeout:
          PRINTF("TIMEOUT\r\n");
              break;
        default:
          PRINTF("ERROR %0d\r", status);
              break;
      }
      delay(100);
    }

    i2c_deinit();
  }
}
Exemple #3
0
int main(void) {
  board_init();
  board_console_init(BOARD_DEBUG_BAUD);

  SysTick_Config(BOARD_SYSTICK_100MS);

  PRINTF("ubirch #1 r0.2 RSA/ECC encryption/signature benchmark\r\n");
  if (init_ltc() != 0) PRINTF("No LTC, may crash\r\n");
  if (init_trng() != 0) error("failed to initialize TRNG");
  if (init_board_key(2048) != 0) error("failed to generate key pair");
  if (init_recipient_public_key(recipient_pubkey, recipient_pubkey_length))
    error("failed to load recipient public key");


  byte cipher[256]; // 256 bytes is large enough to store 2048 bit RSA ciphertext
  word32 plaintextLength = strlen(plaintext);
  word32 cipherLength = sizeof(cipher);

  PRINTF("- signing message with board private key\r\n");

  uint32_t total = 0;
  int signatureLength = 0;
  byte *signature = NULL;
  for (int i = 0; i < BENCHMARK_LOOPS; i++) {
    const uint32_t start = timer_read();
    signatureLength = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, &board_rsa_key, sizeof(board_rsa_key));
    signature = malloc((size_t) signatureLength);

    if (wc_SignatureGenerate(
      WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
      (const byte *) plaintext, plaintextLength,
      signature, (word32 *) &signatureLength,
      &board_rsa_key, sizeof(board_rsa_key),
      &rng) != 0)
      error("failed to sign plain text message");

    const uint32_t elapsed = timer_read() - start;
    total += elapsed;
    char loop_str[64];
    sprintf(loop_str, "%d", i);
    timestamp(loop_str, elapsed);
  }
  timestamp("Average:", total / BENCHMARK_LOOPS);
  PRINTF("-- SIGNATURE\r\n");

  PRINTF("- encrypting message\r\n");

  total = 0;
  int r = -1;
  for (int i = 0; i < BENCHMARK_LOOPS; i++) {
    const uint32_t start = timer_read();
    r = wc_RsaPublicEncrypt((const byte *) plaintext, plaintextLength, cipher, cipherLength, &recipient_public_key,
                            &rng);
    if (r < 0) error("failed to encrypt message");

    const uint32_t elapsed = timer_read() - start;
    total += elapsed;
    char loop_str[64];
    sprintf(loop_str, "%d", i);
    timestamp(loop_str, elapsed);
  }
  timestamp("Average:", total / BENCHMARK_LOOPS);
  PRINTF("-- CIPHER (%d bytes)\r\n", r);

  wc_FreeRsaKey(&board_rsa_key);
  wc_FreeRsaKey(&recipient_public_key);

  PRINTF("THE END\r\n");
  while (true) {
    uint8_t ch = (uint8_t) GETCHAR();
    if (ch == '\r') PUTCHAR('\n');
    PUTCHAR(ch);
  }
}
void cfe_main(int a,int b)
{

    /*
     * By the time this routine is called, the following things have
     * already been done:
     *
     * 1. The processor(s) is(are) initialized.
     * 2. The caches are initialized.
     * 3. The memory controller is initialized.
     * 4. BSS has been zeroed.
     * 5. The data has been moved to R/W space.
     * 6. The "C" Stack has been initialized.
     */

    cfe_bg_init();		       		/* init background processing */
    cfe_attach_init();
    cfe_timer_init();				/* Timer process */
    cfe_bg_add(cfe_device_poll,NULL);

    /*
     * Initialize the memory allocator
     */
        
    SETLEDS("KMEM");
    KMEMINIT((unsigned char *) (uintptr_t) mem_heapstart,
	     ((CFG_HEAP_SIZE)*1024));

    /*
     * Initialize the console.  It is done before the other devices
     * get turned on.  The console init also sets the variable that
     * contains the CPU speed.
     */

    SETLEDS("CONS");
    board_console_init();

    /*
     * Set up the exception vectors
     */

    cfe_setup_exceptions();

    /* 
     * Say hello.
     */

    SETLEDS("CIOK");
    cfe_say_hello();

    /*
     * Initialize the other devices.
     */

    SETLEDS("AREN");
#if CFG_XIP
	xprintf("CFE XIP\n");
#endif
    xprintf("Init Arena\n");
    cfe_arena_init();

#if CFG_PCI
    if (cfe_startflags & CFE_INIT_PCI) {
	pci_flags_t flags = PCI_FLG_NORMAL | PCI_FLG_LDT_PREFETCH;
#ifndef EB332
	char *str;
	extern cons_t pci_optnames[];

	flags = PCI_FLG_NORMAL | PCI_FLG_LDT_PREFETCH;
#if CFG_LDT_REV_017
	flags |= PCI_FLG_LDT_REV_017;
#endif
	str = env_getenv("PCI_OPTIONS");
	setoptions(pci_optnames,str,&flags);

	xprintf("Initializing PCI. [%s]\n",str ? str : "normal");
#endif
	pci_configure(flags);
	}
#endif

    SETLEDS("DEVI");
    xprintf("Init Devs.\n");
    board_device_init();

    cfe_startup_info();
    SETLEDS("ENVI");
    cfe_setup_default_env();

    xprintf("\n");

    cfe_init_ui();

    board_final_init();

    cfe_autostart();

    cfe_command_loop();
}