Example #1
0
/*
 * Initialization.
 *
 *      minithread_system_initialize:
 *       This procedure should be called from your C main procedure
 *       to turn a single threaded UNIX process into a multithreaded
 *       program.
 *
 *       Initialize any private data structures.
 *       Create the idle thread.
 *       Fork the thread which should call mainproc(mainarg)
 *       Start scheduling.
 *
 *       Note that the runnable_q is protected by disabling interrupts.
 *       All other data structures are protected with binary semaphores.
 *
 */
void
minithread_system_initialize(proc_t mainproc, arg_t mainarg) {
  minithread_t clean_up_thread = NULL;
  minithread_t process_packets_thread = NULL;
  int a = 0;
  void* dummy_ptr = NULL;
  dummy_ptr = (void*)&a;
  current_id = 0; // the next thread id to be assigned
  id_lock = semaphore_create();
  semaphore_initialize(id_lock,1); 
  runnable_q = multilevel_queue_new(4);
  blocked_q = queue_new();
  blocked_q_lock = semaphore_create();
  semaphore_initialize(blocked_q_lock,1);
  dead_q = queue_new();
  dead_q_lock = semaphore_create();
  semaphore_initialize(dead_q_lock,1);
  dead_sem = semaphore_create();
  semaphore_initialize(dead_sem,0);    
  clean_up_thread = minithread_create(clean_up, NULL);
  multilevel_queue_enqueue(runnable_q,
    clean_up_thread->priority,clean_up_thread);
  runnable_count++;
  minimsg_initialize();
  process_packets_thread =  minithread_create(process_packets, NULL);
  multilevel_queue_enqueue(runnable_q,
    process_packets_thread->priority,process_packets_thread);
  runnable_count++;
  minithread_clock_init(TIME_QUANTA, (interrupt_handler_t)clock_handler);
  network_initialize((network_handler_t) network_handler);
  init_alarm();
  current_thread = minithread_create(mainproc, mainarg);
  minithread_switch(&dummy_ptr, &(current_thread->stacktop));
  return;
}
Example #2
0
/*
 * Initialize the system to run the first minithread at
 * mainproc(mainarg).  This procedure should be called from your
 * main program with the callback procedure and argument specified
 * as arguments.
 */
void
minithread_system_initialize(proc_t mainproc, arg_t mainarg) {
  runnable_queue = multilevel_queue_new(MAX_LEVELS);
  
  stopped_queue = queue_new();
  scheduler_thread = scheduler_thread_create();
  assert(scheduler_thread);
  running_thread = scheduler_thread;
  int res = network_initialize((network_handler_t) network_handler);
  assert(res == 0);
  alarm_system_initialize();  
  minimsg_initialize();
  minisocket_initialize();
  reaper_thread = minithread_create(clean_stopped_threads, NULL);
  minithread_fork(mainproc, mainarg);
  interrupt_level_t prev_level = set_interrupt_level(ENABLED);
  minithread_clock_init(PERIOD * MILLISECOND, clock_handler);
  while (1) {
    if (!multilevel_queue_is_empty(runnable_queue)) {
      minithread_yield();
    }
  }
  set_interrupt_level(prev_level);
  multilevel_queue_free(runnable_queue);
  queue_free(stopped_queue);
}
Example #3
0
/*
 * Initialization.
 * Initializes reaper and idle threads, starts and initializes main thread.
 * Also creates the scheduler and other data 
 *
 */
void minithread_system_initialize(proc_t mainproc, arg_t mainarg) {
  //allocate room for schedule data (global)
  schedule_data = (scheduler *) malloc(sizeof(scheduler));
  if (schedule_data == NULL) {
    exit(1); //OOM
  }
  schedule_data->cleanup_queue = queue_new();
  schedule_data->multi_run_queue = multilevel_queue_new(num_levels);

  reaper_sema = semaphore_create();
  semaphore_initialize(reaper_sema, 0);

  // create main thread
  minithread_t* main_thread = minithread_fork(mainproc, mainarg);

  // initialize idle thread
  idle_thread = (minithread_t *) malloc(sizeof(minithread_t));
  idle_thread->stacktop = NULL;
  idle_thread->thread_id = -1;

  //initialize alarm bookeeping data structure (priority queue)
  alarm_init();

  //remove from run queue and run it
  schedule_data->running_thread = main_thread;
  main_thread->status = RUNNING;
  multilevel_queue_dequeue(schedule_data->multi_run_queue, 0, (void *) main_thread);

  //reaper thread init
  reaper_thread = minithread_create(reaper_queue_cleanup, NULL);
  minithread_start(reaper_thread);

  //Start clock
  minithread_clock_init(clock_period, clock_handler);

  //Initialize network
  network_initialize(network_handler);

  //START MAIN PROC
  //minithread_switch also enables clock interrupts
  minithread_switch(&idle_thread->stacktop, &main_thread->stacktop);
  //always comes back here to idle in the kernel level (allows freeing resources)
  while (1) {
    minithread_t* next = next_runnable();

    set_interrupt_level(DISABLED);
    next->status = RUNNING;
    schedule_data->running_thread = next;
    minithread_switch(&idle_thread->stacktop, &next->stacktop);
  }
}
Example #4
0
static void
network_constructed (GObject *object)
{
  Network *self = NETWORK (object);
  GError *local_error = NULL;
  GError **error = &local_error;

  if (!network_initialize (self, NULL, error))
    goto out;

out:
  if (local_error)
    g_warning ("Failed to initialize network: %s", local_error->message);
  g_clear_error (&local_error);

  if (G_OBJECT_CLASS (network_parent_class)->constructed != NULL)
    G_OBJECT_CLASS (network_parent_class)->constructed (object);
}
Example #5
0
/*
 * Initialization.
 *
 * 	minithread_system_initialize:
 *	 This procedure should be called from your C main procedure
 *	 to turn a single threaded UNIX process into a multithreaded
 *	 program.
 *
 *	 Initialize any private data structures.
 * 	 Create the idle thread.
 *       Fork the thread which should call mainproc(mainarg)
 * 	 Start scheduling.
 *
 */
void minithread_system_initialize(proc_t mainproc, arg_t mainarg) {
	// create an initial TCB
	minithread_t tcb = (minithread_t) malloc(sizeof(minithread));

	// Create a dummy pointer that points nowhere for the idle thread's stack
	stack_pointer_t* stack_top = (stack_pointer_t*) malloc(sizeof(stack_pointer_t));

	// Create the ready queue
	ready_queue = multilevel_queue_new(4);

	// There are currently 0 ready threads
	ready_threads = 0;

	// Set the current level we inspect in the ML queue to 0
	current_level = 0;

	// The first level of the queue has quanta of 1 period
	ticks_until_switch = 1;

	// The number of ticks to spend in the first ML queue level
	ticks_until_next_level = 80;

	// Create the waiting queue
	cleanup_queue = queue_new();

	// Create the alarm queue
	alarm_queue = queue_new();

	// Create the cleanup semaphore
	cleanup_sem = semaphore_create();
	semaphore_initialize(cleanup_sem, 0);

	// Initialize the TCB
	tcb->id = 0;
	tcb->priority = 0;
	tcb->proc = (proc_t) minithread_idle;
	tcb->arg = NULL; 
	tcb->dir_block_id = 0; // it'll never be used anyway
	// remember to update the initialize stack call a few lines down too - 
	// the first 2 nulls should also be proc/arg

	// Allocate the stack
	tcb->stack_base = NULL; // don't need this
	// just give it a dummy pointer so it can context switch
	tcb->stack_top = stack_top;

	// Set the global variables
	idle = tcb;
	running = idle;

	// Setup interrupts
	minithread_clock_init(&interrupt_handler);

	// Setup disk interrupts
	install_disk_handler((interrupt_handler_t) &disk_interrupt_handler);

	// Initialize the keyboard
	miniterm_initialize();

	// Initialize the minimsg layer
	minimsg_initialize();

	// Initialize the minisocket layer
	minisocket_initialize();

	// Initialize the miniroute layer
	miniroute_initialize();

	// Before interrupts are enabled, start the network handler
	network_initialize((interrupt_handler_t) &network_handler);

	// Initialize the filesystem
	minifile_initialize();

	// Fork the mainproc(mainarg) thread
	minithread_fork(mainproc, mainarg);

	// Fork a cleanup thread
	cleanup = minithread_fork((proc_t) minithread_cleanup, NULL);

	// Enable the interrupts
	set_interrupt_level(ENABLED);

	// Start the idle procedure
	minithread_idle(NULL);
}
Example #6
0
void main(void)
{
  static nabto_main_setup* nms;
  static char versionString[NABTO_DEVICE_VERSION_MAX_SIZE];
  static char idString[NABTO_DEVICE_NAME_MAX_SIZE];
#if NABTO_ENABLE_UCRYPTO
  static const far rom uint8_t dummySharedSecret[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  static const far rom uint8_t* sharedSecret;
#endif

  // <editor-fold desc="Load information from bootloader and application data areas.">

  // Clear the global TCP/IP data structure and load it with the boards unique preprogrammed MAC address.
  memset((void*) &AppConfig, 0x00, sizeof (AppConfig));
  memcpypgm2ram(&AppConfig.MyMACAddr, (const __ROM uint8_t*) bootloaderData.version1.mac, 6);

  if(bootloaderData.base.bootloaderDataVersion == 1)
  {
    strcpypgm2ram(idString, (const far rom char*) bootloaderData.version1.serialNumber);
    strcatpgm2ram(idString, ".nabduino.net");

#if NABTO_ENABLE_UCRYPTO
    //        sharedSecret = applicationData.sharedSecret; // the old pre-bootloader-version-2 way of storing the shared secret - obsolete!
#endif
  }
  else if(bootloaderData.base.bootloaderDataVersion == 2)
  {
    hardwareVersion = (uint8_t) bootloaderData.version2.hardwareVersionMajor;
    hardwareVersion <<= 8;
    hardwareVersion |= (uint8_t) bootloaderData.version2.hardwareVersionMinor;

    if(hardwareVersion == 0x0004)
    {
      hardwareVersionIndex = 0; // first version of the board that was released.
    }
    else if(hardwareVersion == 0x0102)
    {
      hardwareVersionIndex = 1; // second version of the board that was released (yes we jumped from 0.4 beta to 1.2).
    }
    else if(hardwareVersion == 0x0103)
    {
      hardwareVersionIndex = 2; // third version of the board
    }

    strcpypgm2ram(idString, (const far rom char*) bootloaderData.version2.deviceId);
    strcatpgm2ram(idString, (const far rom char*) bootloaderData.version2.productDomain);

#if NABTO_ENABLE_UCRYPTO
    sharedSecret = bootloaderData.version2.sharedSecret;
#endif
  }
  else
  { // unsupported version so it's probably safe to assume that bootloader data has been wiped - please fill in the appropriate values for your Nabduino board
    hardwareVersionIndex = 0; // hardware version 0.4 = 0, v1.2 = 1, v1.3 = 2

    AppConfig.MyMACAddr.v[0] = 0xBC; // Nabto owned MAC OUI is BC:A4:E1
    AppConfig.MyMACAddr.v[1] = 0xA4;
    AppConfig.MyMACAddr.v[2] = 0xE1;
    AppConfig.MyMACAddr.v[3] = 0x00;
    AppConfig.MyMACAddr.v[4] = 0x00;
    AppConfig.MyMACAddr.v[5] = 0x00; // If using more than one Nabduino on the same network make this byte unique for each board

    strcpypgm2ram(idString, "XXX"); // replace XXX with the id of the Nabduino board
    strcatpgm2ram(idString, ".nabduino.net");

#if NABTO_ENABLE_UCRYPTO
    sharedSecret = dummySharedSecret;
#endif
  }

  // </editor-fold>

  // Initialize IOs etc. taking into account the hardware version.
  hal_initialize();

  // Initialize the platform (timing, TCP/IP stack, DHCP and some PIC18 specific stuff)
  platform_initialize();

  network_initialize();

  nms = unabto_init_context();

  nms->id = (const char*) idString;

  // build version string: <application SVN version>/<bootloader SVN version>/<hardware major version>.<hardware minor version>
  itoa(RELEASE_MINOR, versionString);
  strcatpgm2ram(versionString + strlen(versionString), "/");
  itoa(bootloaderData.base.buildVersion, versionString + strlen(versionString));
  strcatpgm2ram(versionString + strlen(versionString), "/");
  itoa(hardwareVersion >> 8, versionString + strlen(versionString));
  strcatpgm2ram(versionString + strlen(versionString), ".");
  itoa(hardwareVersion & 0xff, versionString + strlen(versionString));
  nms->version = (const char*) versionString;

#if NABTO_ENABLE_UCRYPTO
  memcpypgm2ram(nms->presharedKey, (const __ROM void*) sharedSecret, 16);
  nms->secureAttach = true;
  nms->secureData = true;
  nms->cryptoSuite = CRYPT_W_AES_CBC_HMAC_SHA256;
#endif

  setup((char**) &nms->url);

  unabto_init();

  while(1)
  {
    hal_tick();
    platform_tick();
    network_tick();
    unabto_tick();
    loop();
  }
}
Example #7
0
int main (int argc,char *argv[]) {
  int opt;
  int ijob = -1;
  int action = ACTION_NONE;
  struct job job;
  int nRet = 0;

  if(network_initialize() != 0) {
    fprintf (stderr,"Could not initialize the network: %s\n", drerrno_str());
    nRet = 1;
    goto cleanup;
  }

  while ((opt = getopt (argc,argv,"sdcktj:vh")) != -1) {
    switch (opt) {
    case 's':
      action = ACTION_STOP;
      break;
    case 'd':
      action = ACTION_DEL;
      break;
    case 'c':
      action = ACTION_CONT;
      break;
    case 'k':
      action = ACTION_HSTOP;
      break;
    case 't':
      action = ACTION_STATUS;
      break;
    case 'j':
      ijob = atoi (optarg);
      break;
    case 'v':
      show_version (argv);
      goto cleanup;
    case '?':
    case 'h':
      usage();
      nRet = 1;
      goto cleanup;
    }
  }

  if ((ijob == -1) || (action == ACTION_NONE)) {
    usage ();
    nRet = 1;
      goto cleanup;
  }

  set_default_env();

  if (!common_environment_check()) {
    fprintf (stderr,"Error checking the environment: %s\n",drerrno_str());
    nRet = 1;
    goto cleanup;
  }

  switch (action) {
  case ACTION_STOP:
    printf ("Stopping job: %i\n",ijob);
    request_job_stop((uint32_t)ijob,CLIENT);
    break;
  case ACTION_HSTOP:
    printf ("Hard stopping job: %i\n",ijob);
    request_job_hstop((uint32_t)ijob,CLIENT);
    break;
  case ACTION_DEL:
    printf ("Deleting job: %i\n",ijob);
    request_job_delete((uint32_t)ijob,CLIENT);
    break;
  case ACTION_CONT:
    printf ("Continue job: %i\n",ijob);
    request_job_continue((uint32_t)ijob,CLIENT);
    break;
  case ACTION_STATUS:
    request_job_xfer((uint32_t)ijob,&job,CLIENT);
    printf ("%s\n",job_status_string(job.status));
    break;
  }

cleanup:
  network_shutdown();

  return nRet;
}