/*****************************************************************************
*
* 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;
}
Exemplo n.º 2
0
int
Exosite_SyncTime()
{
  char day[11];
  char time[9];
  int http_status = 0;
  int strLen;
  char *testStr = "GET /ip HTTP/1.1\r\n";
  unsigned char serverAddr[6] = {54, 183, 115, 21, 0, 80};
  char strBuf[RX_SIZE];
  DateTime datetime;

  if (!exosite_initialized)
  {
    return -1;
  }

  long sock = connect_to_exosite_with_server_addr("", serverAddr);
  if (sock < 0)
  {
    return -1;
  }

  exoHAL_SocketSend(sock, testStr, strlen(testStr));
  sendLine(sock, HOST_LINE, NULL);
  sendLine(sock, ACCEPT_LINE, "\r\n");

  http_status = get_http_status(sock);
  if (200 != http_status)
     return -1;

  strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE);

  exoHAL_SocketClose(sock);

  if(strLen <= 0)
    return -1;
     
  if(parse_datetime(strBuf, strLen, &datetime) < 0)
    return -1;

  sprintf(day, "%s/%02d/%s", datetime.dayStr, month_to_num(datetime.monStr), datetime.yearStr);
  sprintf(time, "%s:%s:%s", datetime.hourStr, datetime.minStr, datetime.secStr);

  if(AtLibGs_SetTime(day, time) != ATLIBGS_MSG_ID_OK)
    return -1;

  return 0;
}
Exemplo n.º 3
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 http_status = 0;
  unsigned char len, vlen;
  char *p, *pcheck;
  char temp[10];

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

  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')


  itoa((int)buflen, temp, 10); //make a string for length

  strLen = strlen(STR_GET_URL);
  memcpy(strBuf,STR_GET_URL,strLen);
  memcpy(&strBuf[strLen],palias, strlen(palias));
  strLen += strlen(palias);
  memcpy(&strBuf[strLen],STR_HTTP, strlen(STR_HTTP));
  strLen += strlen(STR_HTTP);

  strBuf[strLen] = 0;

  send(sock, strBuf, strLen, 0);
  send(sock, STR_HOST, 22, 0);
  send(sock, STR_CIK_HEADER, 15, 0);
  send(sock, USER_CIK, CIK_LENGTH+2, 0);
  send(sock, STR_ACCEPT, 60, 0);

  pcheck = palias;
  vlen = 0;

  http_status = get_http_status(sock);
  if (200 == http_status)
  {
    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;
}
Exemplo n.º 4
0
/*****************************************************************************
*
* 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 char bufsize)
{
  int success = 0;
  int http_status = 0;
  char temp[10];

  if (!exosite_initialized) {
    status_code = EXO_STATUS_INIT;
    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, temp, 10); //make a string for length
  //combine length line in HTTP POST request
  strLen = strlen(STR_CONTENT_LENGTH);
  memcpy(strBuf,STR_CONTENT_LENGTH,strLen);
  memcpy(&strBuf[strLen],temp, strlen(temp));
  strLen += strlen(temp);
  memcpy(&strBuf[strLen],STR_CRLF, 2);
  strLen += 2;
  memcpy(&strBuf[strLen],STR_CRLF, 2);
  strLen += 2;

  send(sock, STR_POST_HEADER, 36, 0);
  send(sock, STR_HOST, 22, 0);
  send(sock, STR_CIK_HEADER, 15, 0);
  send(sock, USER_CIK, CIK_LENGTH+2, 0);
  send(sock, STR_CONTENT, 64, 0);
  send(sock, strBuf, strLen, 0);
  send(sock, pbuf, bufsize, 0);

//  exoHAL_SocketSend(sock, STR_POST_HEADER, 36);
//  exoHAL_SocketSend(sock, STR_HOST, 22);
//  exoHAL_SocketSend(sock, STR_CIK_HEADER, 15);
//  exoHAL_SocketSend(sock, USER_CIK, CIK_LENGTH+2);
//  exoHAL_SocketSend(sock, STR_CONTENT, 64);
//  exoHAL_SocketSend(sock, strBuf, strLen);
//  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;
}
Exemplo n.º 5
0
/*****************************************************************************
*
* Exosite_Activate
*
*  \param  None
*
*  \return 1  - activation success
*          0  - activation failure
*
*  \brief  Called after Init has been run in the past, but maybe comms were
*          down and we have to keep trying
*
*****************************************************************************/
int
Exosite_Activate(void)
{
  int length;
  char temp[5];
  int newcik = 0;
  int http_status = 0;
  char cmp_ss[18] = "Content-Length: 40";
  char *cmp = cmp_ss;

  if (!exosite_initialized) {
    status_code = EXO_STATUS_INIT;
    return newcik;
  }
  //update_m2ip();        //check our IP api to see if the old IP is advertising a new one
  //exoHAL_Updatm2ip();

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

  // Get activation Serial Number
  length = strlen(exosite_provision_info);
  itoa(length, temp, 10); //make a string for length
  //combine length line in HTTP POST request
  strLen = strlen(STR_CONTENT_LENGTH);
  memcpy(strBuf,STR_CONTENT_LENGTH,strLen);
  memcpy(&strBuf[strLen],temp, strlen(temp));
  strLen += strlen(temp);
  memcpy(&strBuf[strLen],STR_CRLF, 2);
  strLen += 2;
  memcpy(&strBuf[strLen],STR_CRLF, 2);
  strLen += 2;

  //Socket send HTTP Request
  send(sock, STR_POST_ACTIVATE, 35, 0);
  send(sock, STR_HOST, 22, 0);
  send(sock, STR_CONTENT, 64, 0);
  send(sock, strBuf, strLen, 0);
  send(sock, exosite_provision_info, length, 0);

  http_status = get_http_status(sock);

  if (200 == http_status)
  {
    unsigned char len;
    unsigned char cik_len_valid = 0;
    unsigned char cik_ctrl = 0;
    char *p;
    unsigned char crlf = 0;
    unsigned char ciklen = 0;
    char NCIK[CIK_LENGTH + 3];

    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;
          if (*cmp == *p)
          {
            // check the cik length from http response
            cmp++;
            if (cmp > &cmp_ss[17])// + strlen(cmp_ss))
          	cik_len_valid = 1;
          }
          else
            cmp = cmp_ss;
    	}
        ++p;
        --len;
      }

      // The body is the cik
      if (0 < len && 4 == crlf && CIK_LENGTH > ciklen)
      {
        // TODO, be more robust - match Content-Length header value to CIK_LENGTH
        unsigned char need, part;
        if (!(cik_len_valid == 1)) // cik length != 40
        {
          status_code = EXO_STATUS_CONFLICT;
          exoHAL_SocketClose(sock);
          return newcik;
        }
        need = CIK_LENGTH - ciklen;
        part = need < len ? need : len;
        strncpy(NCIK + ciklen, p, part);
        ciklen += part;
      }
    } while (RX_SIZE == strLen);

    if (CIK_LENGTH == ciklen)
    {
      NCIK[40] = '\r';
      NCIK[41] = '\n';
      NCIK[42] = 0;
      Exosite_SetCIK(NCIK);
      newcik = 1;
    }
  }

  exoHAL_SocketClose(sock);

  if (200 == http_status)
  {
    status_code = EXO_STATUS_OK;
    sendString("200");
  }
  if (404 == http_status)
    status_code = EXO_STATUS_BAD_SN;
  if (409 == http_status || 408 == http_status)
  {
    status_code = EXO_STATUS_CONFLICT;
  }

  return newcik;
}
Exemplo n.º 6
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;
}
Exemplo n.º 7
0
/*****************************************************************************
*
* Exosite_Activate
*
*  \param  None
*
*  \return 1  - activation success
*          0  - activation failure
*
*  \brief  Called after Init has been run in the past, but maybe comms were
*          down and we have to keep trying
*
*****************************************************************************/
int
Exosite_Activate(void)
{
  int length;
  char strLen[5];
  char *cmp_ss = "Content-Length: 40";
  char *cmp = cmp_ss;
  int newcik = 0;
  int http_status = 0;

  if (!exosite_initialized) {
    status_code = EXO_STATUS_INIT;
    return newcik;
  }
  update_m2ip();        //check our IP api to see if the old IP is advertising a new one

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

  // Get activation Serial Number
  length = strlen(exosite_provision_info);
  sprintf(strLen, "%d", length); //make a string for length

  sendLine(sock, POSTDATA_LINE, "/provision/activate");
  sendLine(sock, HOST_LINE, NULL);
  sendLine(sock, USER_AGENT_LINE, NULL);
  sendLine(sock, CONNECTION_LINE, NULL);
  sendLine(sock, CONTENT_LINE, NULL);
  sendLine(sock, LENGTH_LINE, strLen);

  exoHAL_SocketSend(sock, exosite_provision_info, length);

  http_status = get_http_status(sock);

  if (200 == http_status)
  {
    char strBuf[RX_SIZE];
    unsigned char strLen, len;
    unsigned char cik_len_valid = 0;
    char *p;
    unsigned char crlf = 0;
    unsigned char ciklen = 0;
    char NCIK[CIK_LENGTH + 1];

    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;
          if (*cmp == *p)
          {
            // check the cik length from http response
            cmp++;
            if (cmp > cmp_ss + strlen(cmp_ss) - 1)
              cik_len_valid = 1;
          }
          else
            cmp = cmp_ss;
        }
        ++p;
        --len;
      }

      // The body is the cik
      if (0 < len && 4 == crlf && CIK_LENGTH > ciklen)
      {
        // TODO, be more robust - match Content-Length header value to CIK_LENGTH
        unsigned char need, part;
        if (!(cik_len_valid == 1)) // cik length != 40
        {
          status_code = EXO_STATUS_CONFLICT;
          exoHAL_SocketClose(sock);
          return newcik;
        }
        need = CIK_LENGTH - ciklen;
        part = need < len ? need : len;
        strncpy(NCIK + ciklen, p, part);
        ciklen += part;
      }
    } while (RX_SIZE == strLen);

    if (CIK_LENGTH == ciklen)
    {
      NCIK[40] = 0;
      Exosite_SetCIK(NCIK);
      newcik = 1;
    }
  }

  exoHAL_SocketClose(sock);

  if (200 == http_status)
    status_code = EXO_STATUS_OK;
  if (404 == http_status)
    status_code = EXO_STATUS_BAD_SN;
  if (409 == http_status || 408 == http_status)
  {
    status_code = EXO_STATUS_CONFLICT;
  }

  return newcik;
}
/*****************************************************************************
*
* 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;
}