/*****************************************************************************
*
* Exosite_Write
*
*  \param  pbuf - string buffer containing data to be sent
*          bufsize - number of bytes to send
*
*  \return 1 success; 0 failure
*
*  \brief  Writes data to Exosite cloud
*
*****************************************************************************/
int
Exosite_Write(char * pbuf, unsigned int bufsize)
{
  int success = 0;
  int http_status = 0;
  char bufCIK[41];
  char strBuf[10];

  if (!exosite_initialized) {
    status_code = EXO_STATUS_INIT;
    return success;
  }

  if (!Exosite_GetCIK(bufCIK))
  {
    return success;
  }


  long sock = connect_to_exosite();
  if (sock < 0) {
    status_code = EXO_STATUS_BAD_TCP;
    return 0;
  }


// This is an example write POST...
//  s.send('POST /onep:v1/stack/alias HTTP/1.1\r\n')
//  s.send('Host: m2.exosite.com\r\n')
//  s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n')
//  s.send('Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n')
//  s.send('Content-Length: 6\r\n\r\n')
//  s.send('temp=2')

  itoa((int)bufsize, strBuf, 10); //make a string for length

  sendLine(sock, POSTDATA_LINE, "/onep:v1/stack/alias");
  sendLine(sock, HOST_LINE, NULL);
  sendLine(sock, CIK_LINE, bufCIK);
  sendLine(sock, CONTENT_LINE, NULL);
  sendLine(sock, LENGTH_LINE, strBuf);
  exoHAL_SocketSend(sock, pbuf, bufsize);

  http_status = get_http_status(sock);

  exoHAL_SocketClose(sock);

  if (401 == http_status)
  {
    status_code = EXO_STATUS_NOAUTH;
  }
  if (204 == http_status)
  {
    success = 1;
    status_code = EXO_STATUS_OK;
  }

  return success;
}
Exemple #2
0
/*****************************************************************************
*
* Exosite_Read
*
*  \param  palias - string, name of the datasource alias to read from
*          pbuf - read buffer to put the read response into
*          buflen - size of the input buffer
*
*  \return number of bytes read
*
*  \brief  Reads data from Exosite cloud
*
*****************************************************************************/
int
Exosite_Read(char * palias, char * pbuf, unsigned char buflen)
{
  int success = 0;
  int http_status = 0;
  char bufCIK[41];
  unsigned char strLen, len, vlen;
  char *p, *pcheck;

  if (!exosite_initialized) {
    status_code = EXO_STATUS_INIT;
    return success;
  }

  if (!Exosite_GetCIK(bufCIK))
  {
    return success;
  }


  long sock = connect_to_exosite();
  if (sock < 0) {
    status_code = EXO_STATUS_BAD_TCP;
    return 0;
  }

// This is an example read GET
//  s.send('GET /onep:v1/stack/alias?temp HTTP/1.1\r\n')
//  s.send('Host: m2.exosite.com\r\n')
//  s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n')
//  s.send('Accept: application/x-www-form-urlencoded; charset=utf-8\r\n\r\n')

  sendLine(sock, GETDATA_LINE, palias);
  sendLine(sock, HOST_LINE, NULL);
  sendLine(sock, USER_AGENT_LINE, NULL);
  sendLine(sock, CONNECTION_LINE, NULL);
  sendLine(sock, CIK_LINE, bufCIK);
  sendLine(sock, ACCEPT_LINE, "\r\n");

  pcheck = palias;
  vlen = 0;

  http_status = get_http_status(sock);
  if (200 == http_status)
  {
    char strBuf[RX_SIZE];
    unsigned char crlf = 0;

    do
    {
      strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE);
      len = strLen;
      p = strBuf;

      // Find 4 consecutive \r or \n - should be: \r\n\r\n
      while (0 < len && 4 > crlf)
      {
        if ('\r' == *p || '\n' == *p)
        {
          ++crlf;
        }
        else
        {
          crlf = 0;
        }
        ++p;
        --len;
      }

      // The body is "<key>=<value>"
      if (0 < len && 4 == crlf && buflen > vlen)
      {
        // Move past "<key>"
        while (0 < len && 0 != *pcheck)
        {
          if (*pcheck == *p)
          {
            ++pcheck;
          }
          else
          {
            pcheck = palias;
          }
          ++p;
          --len;
        }

        // Match '=',  we should now have '<key>='
        if (0 < len && 0 == *pcheck && '=' == *p)
        {
          ++p;
          --len;
        }

        // read in the rest of the body as the value
        while (0 < len && buflen > vlen)
        {
          pbuf[vlen++] = *p++;
          --len;
        }
      }
    } while (RX_SIZE == strLen);
  }

  exoHAL_SocketClose(sock);

  if (200 == http_status)
  {
    status_code = EXO_STATUS_OK;
  }
  if (204 == http_status)
  {
    status_code = EXO_STATUS_OK;
  }
  if (401 == http_status)
  {
    status_code = EXO_STATUS_NOAUTH;
  }

  return vlen;
}
/*****************************************************************************
*
* Exosite_Read
*
*  \param  palias - string, name of the datasource alias to read from
*          pbuf - read buffer to put the read response into
*          buflen - size of the input buffer
*
*  \return number of bytes read
*
*  \brief  Reads data from Exosite cloud
*
*****************************************************************************/
int
Exosite_Read(char * palias, char * pbuf, unsigned int buflen)
{
    //
    // Modified by Texas Instruments, DGT, changed buflen from unsigned char to
    // unsigned int. comment out declaration of *pcheck to prevent warnings
    // created by CAJ changes below.
    //
  int success = 0;
  int http_status = 0;
  char bufCIK[41];
  unsigned char strLen, len, vlen;
  char *p;
  //char *pcheck;

  if (!exosite_initialized) {
    status_code = EXO_STATUS_INIT;
    return success;
  }

  if (!Exosite_GetCIK(bufCIK))
  {
    return success;
  }


  long sock = connect_to_exosite();
  if (sock < 0) {
    status_code = EXO_STATUS_BAD_TCP;
    return 0;
  }

// This is an example read GET
//  s.send('GET /onep:v1/stack/alias?temp HTTP/1.1\r\n')
//  s.send('Host: m2.exosite.com\r\n')
//  s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n')
//  s.send('Accept: application/x-www-form-urlencoded; charset=utf-8\r\n\r\n')

  sendLine(sock, GETDATA_LINE, palias);
  sendLine(sock, HOST_LINE, NULL);
  sendLine(sock, CIK_LINE, bufCIK);
  sendLine(sock, ACCEPT_LINE, "\r\n");

  //
  // Modified by Texas Instruments DGT comment reference to pcheck no longer
  // used. See TI CAJ modification below.
  //
  //pcheck = palias;
  vlen = 0;

  http_status = get_http_status(sock);
  if (200 == http_status)
  {
    char strBuf[RX_SIZE];
    unsigned char crlf = 0;

    do
    {
      strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE);
      len = strLen;
      p = strBuf;

      // Find 4 consecutive \r or \n - should be: \r\n\r\n
      while (0 < len && 4 > crlf)
      {
        if ('\r' == *p || '\n' == *p)
        {
          ++crlf;
        }
        else
        {
          crlf = 0;
        }
        ++p;
        --len;
      }

      // The body is "<key>=<value>"
      if (0 < len && 4 == crlf && buflen > vlen)
      {
          // Code below removed by CAJ. Removing the key works for a single
          // READ request, but doesn't work if multiple values were requested.
          // For multiple values, the server is not guaranteed to return every
          // value in the same order that they were sent. This means that the
          // caller will need the "key" to be able to determine which value
          // belongs with which alias.

        // Move past "<key>"
//        while (0 < len && 0 != *pcheck)
//        {
//          if (*pcheck == *p)
//          {
//            ++pcheck;
//          }
//          else
//          {
//            pcheck = palias;
//          }
//          ++p;
//          --len;
//        }
//
//        // Match '=',  we should now have '<key>='
//        if (0 < len && 0 == *pcheck && '=' == *p)
//        {
//          ++p;
//          --len;
//        }
//
        // read in the rest of the body as the value
        while (0 < len && buflen > vlen)
        {
          pbuf[vlen++] = *p++;
          --len;
        }
      }
    } while (RX_SIZE == strLen);
  }

  exoHAL_SocketClose(sock);

  if (200 == http_status)
  {
    status_code = EXO_STATUS_OK;
  }
  if (204 == http_status)
  {
    status_code = EXO_STATUS_OK;
  }
  if (401 == http_status)
  {
    status_code = EXO_STATUS_NOAUTH;
  }

  return vlen;
}
int  main(void)
{
    AppMode_T AppMode; APP_STATE_E state=UPDATE_TEMPERATURE; 
    char LCDString[30], temp_char[2]; uint16_t temp; float ftemp;
  
    HardwareSetup();

    /************************initializa LCD module********************************/
    SPI2_Init();
    InitialiseLCD();
    led_init();
    MSTimerInit();

    /* Default app mode */
    AppMode = GAINSPAN_DEMO;
    
    /* If the CIK is exist, auto into the Exosite mode */
    NVSettingsLoad(&GNV_Setting);
    
    /* Determine if SW1 & SW3 is pressed at power up to enter programming mode */
    if (Switch1IsPressed() && Switch3IsPressed()) {
         AppMode = PROGRAM_MODE;
    }
    else if(Switch3IsPressed() && Switch2IsPressed())
    {
         AppMode = EXOSITE_ERASE;
    }
    else if(Switch1IsPressed())
    {
        AppMode = RUN_EXOSITE;
    }
    else if(Switch2IsPressed())
    {
        AppMode = RUN_PROVISIONING;
    }
    else if(Switch3IsPressed())
    {
        AppMode = RUN_OVER_AIR_DOWNLOAD;
    }
    
    if(AppMode == GAINSPAN_DEMO) {
        LCDDisplayLogo();
        LCDSelectFont(FONT_SMALL);
        DisplayLCD(LCD_LINE3, "RL78G14 RDK    V2.0");
        DisplayLCD(LCD_LINE4, "   Wi-Fi & Cloud   ");
        DisplayLCD(LCD_LINE5, "     demos by:     ");
        DisplayLCD(LCD_LINE6, "Gainspan           ");
        DisplayLCD(LCD_LINE7, "Exosite            ");
        DisplayLCD(LCD_LINE8, "Future Designs, Inc");
        MSTimerDelay(3500);
        ClearLCD();
        DisplayLCD(LCD_LINE1, "Demo Modes:        ");
        DisplayLCD(LCD_LINE2, "-RST no key:       ");
        DisplayLCD(LCD_LINE3, "   GS Web Server   ");
        DisplayLCD(LCD_LINE4, "-RST + SW1:        ");
        DisplayLCD(LCD_LINE5, "   Exosite Cloud   ");
        DisplayLCD(LCD_LINE6, "-RST + SW2:        ");
        DisplayLCD(LCD_LINE7, "   AP Provisioning ");
        DisplayLCD(LCD_LINE8, "-RST + SW3: OTA    ");
        MSTimerDelay(3000);
        ClearLCD();
        
        LCDSelectFont(FONT_LARGE);
        if(Exosite_GetCIK(NULL))
        {
          AppMode = RUN_EXOSITE;
        }
    }
    
    DisplayLCD(LCD_LINE1, "Starting..."); 
    /*****************************************************************************/  
    SPI_Init(GAINSPAN_SPI_RATE);  
   /* Setup LCD SPI channel for Chip Select P10, active low, active per byte  */
    SPI_ChannelSetup(GAINSPAN_SPI_CHANNEL, false, true);
    GainSpan_SPI_Start();

    PM15 &= ~(1 << 2);
    P15 &= ~(1 << 2);
    
    if(AppMode == PROGRAM_MODE) {
        App_ProgramMode();
    }
    else if (AppMode == RUN_EXOSITE)
    {          
        DisplayLCD(LCD_LINE1, " CLOUD DEMO ");
        Temperature_Init();
        Potentiometer_Init();  
        App_Exosite();
    }
    else if(AppMode == RUN_PROVISIONING)
    {
      App_WebProvisioning();
    }
     else if(AppMode == RUN_OVER_AIR_DOWNLOAD)
    {
       App_OverTheAirProgrammingPushMetheod();
    }
    else if (AppMode == EXOSITE_ERASE)
    {
       ClearLCD();
       LCDSelectFont(FONT_SMALL);
       DisplayLCD(LCD_LINE3, "EEPROM ERASING ... ");
       MSTimerDelay(2000);
       Exosite_Init("renesas", "rl78g14", IF_WIFI, 1);
       DisplayLCD(LCD_LINE3, "                   ");
       DisplayLCD(LCD_LINE4, "Please reset device");
       while(1);
    }
    else{
        UART0_Start(GAINSPAN_CONSOLE_BAUD);
       // UART2_Start(GAINSPAN_UART_BAUD);
 
        Temperature_Init();
        Potentiometer_Init();
    
       // sprintf(LCDString, "RDK Demo %s", VERSION_TEXT);
       // DisplayLCD(LCD_LINE1, (const uint8_t *)LCDString);
   
        /* Before doing any tests or apps, startup the module */
        /* and nonvolatile stettings */
        App_Startup();
        // Now connect to the system
        //App_Connect(&G_nvsettings.webprov);
     
       //  App_PassThroughSPI();
         
         /******************Start Processing Sensor data******************/
         
         uint32_t start = MSTimerGet();  uint8_t c;
         Accelerometer_Init();
         while(1) 
         { 
          // if (GainSpan_SPI_ReceiveByte(GAINSPAN_SPI_CHANNEL, &c)) 
           if(App_Read(&c, 1, 0)) 
             AtLibGs_ReceiveDataProcess(c);
                   
        /* Timeout? */
           if (MSTimerDelta(start) >= 100)     // every 100 ms, read sensor data
           {  
              led_task();
              switch(state)
              {              
                case UPDATE_TEMPERATURE:         
                // Temperature sensor reading
                  temp = Temperature_Get();
#if 0                 
                   // Get the temperature and show it on the LCD
                  temp_char[0] = (int16_t)temp / 16;
                  temp_char[1] = (int16_t)((temp & 0x000F) * 10) / 16;
#endif 
                  temp_char[1] = (temp & 0xFF00)>>8;
                  temp_char[0] = temp & 0xFF;
                  
                  ftemp = *(uint16_t *)temp_char;
                  
                  gTemp_F = ((ftemp/5)*9)/128 + 22;
              
                  // Display the contents of lcd_buffer onto the debug LCD 
                  //sprintf((char *)LCDString, "TEMP: %d.%d C", temp_char[0], temp_char[1]);
                  sprintf((char *)LCDString, "TEMP: %.1fF", gTemp_F);
                  DisplayLCD(LCD_LINE6, (const uint8_t *)LCDString);  
                  state = UPDATE_LIGHT;
                break;
                
                case UPDATE_LIGHT:
                 // Light sensor reading
                  gAmbientLight = LightSensor_Get();
                    // Display the contents of lcd_buffer onto the debug LCD 
                  sprintf((char *)LCDString, "Light: %d ", gAmbientLight);
                  DisplayLCD(LCD_LINE7, (const uint8_t *)LCDString);
                  state = UPDATE_ACCELEROMETER;
                break;
                
                case UPDATE_ACCELEROMETER: 
                 // 3-axis accelerometer reading
                  Accelerometer_Get();
                  sprintf((char *)LCDString, "x%2d y%2d z%2d", gAccData[0], gAccData[1], gAccData[2]);
                  DisplayLCD(LCD_LINE8, (const uint8_t *)LCDString); 
                  state = UPDATE_TEMPERATURE;
                break;
              }
              start = MSTimerGet();
           }
         }          
    }