/* This function is executed by the first task that is started
 * by pico]OS ( see the nosInit()-call in main(), file ex_init4.c ).
 */
void firsttask(void *arg)
{
  NOSTASK_t  t;

  /* Avoid compiler warning. "arg" is the "NULL" in the nosInit()-call */
  (void) arg;

  nosPrint("First task started\n");

  /* start a second task */
  t = nosTaskCreate(secondtask, /* pointer to new task-function            */
                    "Task 2",   /* optional argument for the task-function */
                    2,          /* priority level of the new task          */
                    0,          /* stack size (0 = default size)           */
                    "task2");   /* optional name of the second task        */

  if (t == NULL)
  {
    nosPrint("Failed to start second task!\n");
  }

  for(;;)
  {
    /* print string: "Task 1" */
    nosPrint("Task 1\n");

    /* sleep (=do nothing) for one second */
    nosTaskSleep(MS(1000));
  }
}
/* This is the first function that is called in the multitasking context.
 * (See file ex_init4.c for how to setup pico]OS).
 */
void firsttask(void *arg)
{
  POSTASK_t  t;
  VAR_t      status;

  (void) arg;

  /* create a semaphore, initialize to 0 */
  event = posSemaCreate(0);

  if (event == NULL)
  {
    nosPrint("Failed to create a semaphore!\n");
    return;
  }

  /* start a second task */
  t = nosTaskCreate(task2,      /* pointer to new task-function            */
                    event,      /* optional argument for the task-function */
                    2,          /* priority level of the new task          */
                    0,          /* stack size (0 = default size)           */
                    "task2");   /* optional name of the second task        */

  if (t == NULL)
  {
    nosPrint("Failed to start second task!\n");
    return;
  }

  /* install software interrupt handler (no. 4) */
  status = posSoftIntSetHandler(4, softisr);

  if (status != E_OK)
  {
    nosPrint("Failed to install software interrupt handler!\n");
    return;
  }


  /* Signal the interrupt every second.
   * Usually, the software interrupt would be raised
   * asynchronously through eg. a hardware isr handler.
   */
  for(;;)
  {
    /* raise the software interrupt no. 4 */
    posSoftInt(4, 0);

    /* wait 1 second */
    posTaskSleep(MS(1000));
  }
}
Exemple #3
0
void firsttask(void *arg) {
	POSTASK_t  t;
	UVAR_t i;

	posAtomicSet(&counter, 0);

	/* creating the flag */
	flagset = posFlagCreate();
  
	if (flagset == NULL)
	{
		nosPrint("Failed to create a set of flags!\n");
		return;
	}

	/* creating semaphore object with set one default value */
	semaphore = posSemaCreate(1);

	if (semaphore == NULL){
		
		nosPrint("Failed to create a semaphore!\n");
		return;
	}

	t = nosTaskCreate(task2,    /* ptr to function:  task2 that is executed 	*/
		NULL,					/* optional argument, not used here             */
		1,						/* priority of the first task                   */
		0,						/* stack size for the first task, 0 = default   */
		"task2");				/* task name       								*/

	if (t == NULL) {
		nosPrint("Failed to start second task!\n");
	}

	t = nosTaskCreate(task3,    /* ptr to function: task3 that is executed */
                    NULL,       /* optional argument, not used here             */
                    1,          /* priority of the first task                   */
                    0,          /* stack size for the first task, 0 = default   */
                    "task3");   /* task name       				*/

	if (t == NULL) {
		nosPrint("Failed to start third task!\n");
	}

	/* first flag status change */
	posFlagSet(flagset, 1);
	
  	/* task1 handled */
	task1(arg);
}
/* This is the first function that is called in the multitasking context.
 * (See file ex_init4.c for how to setup pico]OS).
 */
void firsttask(void *arg)
{
  POSTASK_t  t;

  posAtomicSet(&counter, 0);

  /* Create a semaphore, initialize to 2.
   * You may vary the initialization count between
   * 1 and 3 and observe the output of this program.
   *
   * The initialization semaphore count limits the number
   * of tasks that are allowed to access a shared resource
   * at the same time.
   */
  semaphore = posSemaCreate(2);

  if (semaphore == NULL)
  {
    nosPrint("Failed to create a semaphore!\n");
    return;
  }

  /* start a second task */
  t = nosTaskCreate(task2,      /* pointer to new task-function            */
                    NULL,       /* optional argument for the task-function */
                    2,          /* priority level of the new task          */
                    0,          /* stack size (0 = default size)           */
                    "task2");   /* optional name of the second task        */

  if (t == NULL)
  {
    nosPrint("Failed to start second task!\n");
  }

  /* start a second task */
  t = nosTaskCreate(task3,      /* pointer to new task-function            */
                    NULL,       /* optional argument for the task-function */
                    3,          /* priority level of the new task          */
                    0,          /* stack size (0 = default size)           */
                    "task3");   /* optional name of the third task         */

  if (t == NULL)
  {
    nosPrint("Failed to start third task!\n");
  }

  /* continue execution in function task1 */
  task1(arg);
}
Exemple #5
0
void uosBootDiag()
{
#if NOSCFG_FEATURE_CONOUT == 1 && NOSCFG_FEATURE_PRINTF == 1
  nosPrint("\n" POS_STARTUPSTRING "\n");
#if !defined(unix) && !defined(__PIC32MX)
  nosPrintf("Ram:    data+bss %u, heap %u, irq stack %u\n", (int)((char*)_end - (char*)__data_start),
                                                             (int)((char*)__heap_end - (char*)__heap_start),
                                                             PORTCFG_IRQ_STACK_SIZE);
#endif
  nosPrintf("Limits: %d tasks, %d events\n", POSCFG_MAX_TASKS, POSCFG_MAX_EVENTS);
#endif
}
Exemple #6
0
void httpdTask(void* arg)
{
  int lsn;
  socklen_t addrlen;

#if UIP_CONF_IPV6

  struct sockaddr_in6 me;
  struct sockaddr_in6 peer;

  me.sin6_family = AF_INET6;
  me.sin6_addr = in6addr_any;
  me.sin6_port = htons(80);

#else

  struct sockaddr_in me;
  struct sockaddr_in peer;

  me.sin_family = AF_INET;
  me.sin_addr.s_addr = INADDR_ANY;
  me.sin_port = htons(80);

#endif

  lsn = socket(AF_INET, SOCK_STREAM, 0);
  
  bind(lsn, (struct sockaddr*)&me, sizeof(me));
  listen(lsn, 5);

  while (true) {

    int s = accept(lsn, (struct sockaddr*)&peer, &addrlen);

    if (s == -1)
      continue;

    POSTASK_t task;
    task = posTaskCreate(httpClientTask, (void*)(intptr_t)s, 1, 1200);
    if (task == NULL) {

#if NOSCFG_FEATURE_CONOUT == 1
      nosPrint(TRACENAME "out of tasks.");
#endif
      closesocket(s);
    }

    POS_SETTASKNAME(task, "httpc");
  }
}
Exemple #7
0
/* This is the first function that is called in the multitasking context.
 * (See file ex_init4.c for how to setup pico]OS).
 */
void firsttask(void *arg)
{
  POSTASK_t  t;

  /* Note: You may uncomment the source below by setting the #if to
   *       zero and then observe the output of this program.
   *       This is a nice example of how synchronization works.
   */
#if 1

  /* create a mutex */
  mutex = posMutexCreate();

  if (mutex == NULL)
  {
    nosPrint("Failed to create a mutex!\n");
    return;
  }

#endif

  /* start a second task */
  t = nosTaskCreate(task2,      /* pointer to new task-function            */
                    NULL,       /* optional argument for the task-function */
                    2,          /* priority level of the new task          */
                    0,          /* stack size (0 = default size)           */
                    "task2");   /* optional name of the second task        */

  if (t == NULL)
  {
    nosPrint("Failed to start second task!\n");
  }

  /* continue execution in function task1 */
  task1(arg);
}
Exemple #8
0
void uosResourceDiag()
{
#if NOSCFG_FEATURE_CONOUT == 1 && NOSCFG_FEATURE_PRINTF == 1

#ifdef POS_DEBUGHELP
  int taskCount = 0;
  int eventCount = 0;
  int i;
  struct PICOTASK* task;
  struct PICOEVENT* event;
#endif

#if POSCFG_ARGCHECK > 1

#if UOSCFG_NEWLIB_SYSCALLS == 1 && NOSCFG_MEM_MANAGER_TYPE != 1

  uint32_t heapUsed = (char*)sbrk(0) - (char*)__heap_start;
  uint32_t heapSize = (char*)__heap_end - (char*)__heap_start;
  nosPrintf("Heap used: %u (%d %%)\n", heapUsed, 100 * heapUsed / heapSize);

#endif

  nosPrint("Stack unused amounts:\n");

  int freeStack;
  unsigned char* sp;

  freeStack = 0;

#ifndef unix
  sp = portIrqStack;
  while (*sp == PORT_STACK_MAGIC) {
    ++sp;
    ++freeStack;
  }

  nosPrintf("  IRQ %d\n", freeStack);
#endif

#endif

#ifdef POS_DEBUGHELP

  struct PICOTASK* allTasks[POSCFG_MAX_TASKS];
  const char* name;

  memset(allTasks, '\0', sizeof(allTasks));

  posTaskSchedLock();
  task = picodeb_tasklist;
  while (task != NULL) {

    allTasks[taskCount] = task;
    taskCount++;
    task = task->next;
  }

  posTaskSchedUnlock();

#if POSCFG_ARGCHECK > 1

  for (i = 0; i < taskCount; i++) {

    task = allTasks[i];

    if (task->state == task_notExisting)
      continue;

    freeStack = 0;

    sp = task->handle->stack;
    while (*sp == PORT_STACK_MAGIC) {
      ++sp;
      ++freeStack;
    }

    name = (task->name != NULL) ? task->name : "?";
    nosPrintf("  %06X task %s %d\n", task->handle, name, freeStack);
  }

#endif

  posTaskSchedLock();

  event = picodeb_eventlist;
  while (event != NULL) {

    eventCount++;
    event = event->next;
  }

  posTaskSchedUnlock();

  nosPrintf("%d tasks, %d events in use\n", taskCount, eventCount);
  nosPrintf("%d tasks, %d events conf max\n", POSCFG_MAX_TASKS, POSCFG_MAX_EVENTS);

#else
#if POSCFG_ARGCHECK > 1 && POSCFG_FEATURE_GETTASK == 1
  
  POSTASK_t current = posTaskGetCurrent();

  freeStack = 0;
  sp = current->stack;
  while (*sp == PORT_STACK_MAGIC) {
    ++sp;
    ++freeStack;
  }

  nosPrintf("  current task %d\n", freeStack);

#endif

#endif
#endif
}
Exemple #9
0
static int http(UosFile* sock)
{
  char buf[SOCK_BUF_SIZE];
  char* ptr;
  char* tok;
  char req[20];
  char url[80];
  int i;
  int bytes;
  bool cgi;
  bool gz;
  UosFile* file;
  int len;

  i = netSockReadLine(sock, buf, sizeof(buf), MS(5000));
  if (i <= 0) {

    nosPrint(TRACENAME "timeout.\n");
    return -1;
  }

  if (buf[i - 1] != '\n') {
   
    nosPrintf(TRACENAME "request too long, %d bytes: %s\n", i, buf);
    return -1;
  }

  buf[i - 1] = '\0';
  nosPrintf(TRACENAME "%s  mss %d\n", buf, UIP_TCP_MSS);
  ptr = strtok_r(buf, " ", &tok);
  if (ptr == NULL)
    return -1;

  if (strlen(ptr) > sizeof(req) - 1) {

    nosPrint(TRACENAME "verb too long\n");
    return -1;
  }

  strcpy(req, ptr);

  ptr = strtok_r(NULL, " ", &tok);
  if (ptr == NULL)
    return -1;

  if (strlen(ptr) > sizeof(url) - 1) {

    nosPrint(TRACENAME "url too long\n");
    return -1;
  }

  strcpy(url, ptr);

  do {

    i = netSockReadLine(sock, buf, sizeof(buf) - 1, MS(5000));
    if (i <= 0) {
      nosPrint(TRACENAME "timeout reading headers.\n");
      return -1;
    }

    if (buf[i - 1] == '\n')
      buf[i - 1] = '\0';

  } while (buf[0] != '\0');

  if (strcmp(req, "GET")) {

    strcpy(buf, "HTTP/1.1 500 Bad verb\r\n");
    uosFileWrite(sock, buf, strlen(buf));
    return 0;
  }

  if (!strcmp(url, "/"))
    strcpy(url, "/index.html");

  ptr = strchr(url, '?');
  if (ptr != NULL)
    *ptr = '\0';
  if (!strcmp(url + strlen(url) - 4, ".cgi"))
    cgi = true;
  else {
    cgi = false;

    gz = false;
    file = uosFileOpen(url, 0, 0);
    if (file == NULL) {

      strcpy(buf, url);
      strcat(buf, ".gz");
      file = uosFileOpen(buf, 0, 0);
      gz = true;
    }
  }

  if (!cgi && file == NULL) {

    buf[0] = '\0';
    strcat(buf, "HTTP/1.1 500 Not found\r\n");
    strcat(buf, "Server: Pico[OS " POS_VER_S "\r\n");
    strcat(buf, "Connection: close\r\n");
    strcat(buf, "\r\n");
    strcat(buf, "not found");
    uosFileWrite(sock, buf, strlen(buf));
  }
  else {

    buf[0] = '\0';
    strcat(buf, "HTTP/1.1 200 Ok\r\n");
    strcat(buf, "Server: Pico[OS " POS_VER_S "\r\n");
    strcat(buf, "Connection: close\r\n");

    if (!cgi) {
      ptr = strrchr(url, '.');
      if (ptr != NULL) {

        for (i = 0; i < (int)(sizeof(mimeTypes) / sizeof(Mime)); i++)
          if (!strcmp(mimeTypes[i].fileType, ptr + 1)) {

            strcat(buf, "Content-Type: ");
            strcat(buf, mimeTypes[i].mimeType);
            strcat(buf, "\r\n");
            break;
          }
      }

      if (gz)
        strcat(buf, "Content-Encoding: gzip\r\n");
    }
    else
      strcat(buf, "Content-Type: text/plain\r\n");

    if (cgi)
      strcat(buf, "Cache-control: max-age=10\r\n");
    else
      strcat(buf, "Cache-control: max-age=3600\r\n");

    strcat(buf, "\r\n");
    uosFileWrite(sock, buf, strlen(buf));

    if (cgi)
      bytes = serveCgi(sock, url, buf);
    else {

      bytes = 0;
      do {
   
        len = uosFileRead(file, buf, sizeof(buf));
        if (len > 0) {
          bytes += len;
          i = uosFileWrite(sock, buf, len);
        }
      }while (len > 0);

      uosFileClose(file);
    }
    return bytes;
  }

  return 0;
}