예제 #1
0
//Flash rate of 0 means no FLASH.
//up to +/- 99
void DispMan_Print7SegInt(int8_t number, uint8_t flashRate)
{
	uint8_t outputStr[4];

	DispMan_7SegDisplay.rate = flashRate;
	DispMan_7SegDisplay.number = 0;
	DispMan_7SegDisplay.type = DISP_ALPHANUMERIC;
	DispMan_7SegDisplay.state = DISPLAY_MODE_NORMAL;

	outputStr[0] = ' ';
	if (number < 0)
	{
		outputStr[0] = '-';
	}

	number = abs(number);
	if (number < 10)
	{
		uint16toa(number, (char*) &outputStr[2], 0);
	}
	else
	{
		uint16toa(number, (char*) &outputStr[1], 0);
	}
	memcpy( &DispMan_7SegDisplay.ch, outputStr, DISPLAY_COUNT);

}
예제 #2
0
int TembooSession::executeChoreo(
        const char* accountName, 
        const char* appKeyName, 
        const char* appKeyValue, 
        const char* path, 
        const ChoreoInputSet& inputSet, 
        const ChoreoOutputSet& outputSet, 
        const ChoreoPreset& preset) {

    DataFormatter fmt(&inputSet, &outputSet, &preset);
    char auth[HMAC_HEX_SIZE_BYTES + 1];
    char buffer[11];
    
    // We use the current time-of-day as salt on the app key.
    // We keep track of time-of-day by getting the current time
    // from the server and applying an offset (the length of time
    // we've been running.) 
    uint32toa((uint32_t)TembooSession::getTime(), buffer);

    uint16_t contentLength = getAuth(fmt, appKeyValue, buffer, auth);

    m_client.stop();
    m_client.flush();

    int connected = 0;
    TEMBOO_TRACE("Connecting: ");

    // reserve space for the "host" string sufficient to hold either the 
    // (dotted-quad) IP address + port, or the default <account>.temboolive.com
    // host string.
    int hostLen = (m_addr == INADDR_NONE ? (strlen_P(TEMBOO_DOMAIN) + strlen(accountName) + 1):21);
    char host[hostLen];

    // If no explicit IP address was specified (the normal case), construct
    // the "host" string from the account name and the temboo domain name.
    if (m_addr == INADDR_NONE) {
        strcpy(host, accountName);
        strcat_P(host, TEMBOO_DOMAIN);
        TEMBOO_TRACELN(host);
        connected = m_client.connect(host, m_port);
    } else {

        // If an IP address was explicitly specified (presumably for testing purposes),
        // convert it to a dotted-quad text string.
        host[0] = '\0';
        for(int i = 0; i < 4; i++) {
            uint16toa(m_addr[i], &host[strlen(host)]);
            strcat(host, ".");
        }

        // replace the last '.' with ':'
        host[strlen(host)-1] = ':';
        
        // append the port number
        uint16toa(m_port, &host[strlen(host)]);
        
        TEMBOO_TRACELN(host);
        connected = m_client.connect(m_addr, m_port);
    }

    if (connected) {

        TEMBOO_TRACELN("OK. req:");
        qsendProgmem(POST);
        qsendProgmem(BASE_CHOREO_URI);
        qsend(path);
        qsendProgmem(SDK_ID);
        qsendlnProgmem(POSTAMBLE);
        
        // Send our custom authentication header
        // (app-key-name:hmac)
        qsendProgmem(HEADER_AUTH);
        qsend(appKeyName);
        qsend(":");
        qsendln(auth);
        
        // send the standard host header
        qsendProgmem(HEADER_HOST);
        qsendln(host);
        
        // send the standard accept header
        qsendlnProgmem(HEADER_ACCEPT);
        
        // send our custom account name neader
        qsendProgmem(HEADER_ORG);
        qsend(accountName);
        qsendlnProgmem(HEADER_DOM);
        
        // send the standard content type header
        qsendlnProgmem(HEADER_CONTENT_TYPE);
        
        // send our custom client time header
        qsendProgmem(HEADER_TIME);
        qsendln(buffer);
        
        // send the standard content length header
        qsendProgmem(HEADER_CONTENT_LENGTH);
        qsendln(uint16toa(contentLength, buffer));

        qsendProgmem(EOL);
        
        // Format and send the body of the request
        fmt.reset();
        while(fmt.hasNext()) {
            qsend(fmt.next());
        }

        qsendProgmem(EOL);
        qflush();
        return 0;
    } else {
        TEMBOO_TRACELN("FAIL");
        return 1;
    }
}
예제 #3
0
int TembooChoreo::run(IPAddress addr, uint16_t port) {

    m_nextChar = NULL;

    if (m_accountName == NULL || *m_accountName == '\0') {
        return TEMBOO_ERROR_ACCOUNT_MISSING;
    }

    if (m_path == NULL || *m_path == '\0') {
        return TEMBOO_ERROR_CHOREO_MISSING;
    }

    if (m_appKeyName == NULL || *m_appKeyName == '\0') {
        return TEMBOO_ERROR_APPKEY_NAME_MISSING;
    }

    if (m_appKeyValue == NULL || *m_appKeyValue == '\0') {
        return TEMBOO_ERROR_APPKEY_MISSING;
    }


    TembooSession session(m_client, addr, port);
    uint16_t httpCode = 0;
    
    for (int i = 0; i < 2; i++) {
        if (0 != session.executeChoreo(m_accountName, m_appKeyName, m_appKeyValue, m_path, m_inputs, m_outputs, m_preset)) {
            httpCode = 0;
            break;
        }

        while(!m_client.available()) {
            if (!m_client.connected()) {
                TEMBOO_TRACELN("Disconnected");
                return TEMBOO_ERROR_HTTP_ERROR;
            }
            delay(10);
        }

        if (!m_client.find("HTTP/1.1 ")) {
            TEMBOO_TRACELN("No HTTP");
            return TEMBOO_ERROR_HTTP_ERROR;
        }

        httpCode = (uint16_t)m_client.parseInt();

        // We expect HTTP response codes to be <= 599, but 
        // we need to be prepared for anything.
        if (httpCode >= 600) {
            TEMBOO_TRACELN("Invalid HTTP");
            httpCode = 0;
        }

        // if we get an auth error AND there was an x-temboo-time header,
        // update the session timeOffset
        if ((httpCode == 401) && (i == 0)) {
            if (m_client.find("x-temboo-time:")) {
                TembooSession::setTime((unsigned long)m_client.parseInt());
                while(m_client.available()) {
                    m_client.read();
                }
                m_client.stop();
            }
        } else {
            break;
        }
    }

    uint16toa(httpCode, m_httpCodeStr);
    strcat_P(m_httpCodeStr, PSTR("\x0A\x1E"));
    m_nextState = START;
    m_nextChar = HTTP_CODE;

    if (httpCode < 200 || httpCode >= 300) {
        return TEMBOO_ERROR_HTTP_ERROR;
    }

    if (!m_client.find("\x0D\x0A\x0D\x0A")) {
        return TEMBOO_ERROR_HTTP_ERROR;
    }

    return TEMBOO_ERROR_OK;
}
예제 #4
0
int TembooSession::executeChoreo(
        const char* accountName, 
        const char* appKeyName, 
        const char* appKeyValue, 
        const char* path, 
        const ChoreoInputSet& inputSet, 
        const ChoreoOutputSet& outputSet, 
        const ChoreoPreset& preset) {

    DataFormatter fmt(&inputSet, &outputSet, &preset);
    char auth[HMAC_HEX_SIZE_BYTES + 1];
    char buffer[11];
    
    // We use the current time-of-day as salt on the app key.
    // We keep track of time-of-day by getting the current time
    // from the server and applying an offset (the length of time
    // we've been running.) 
    uint32toa((uint32_t)TembooSession::getTime(), buffer);

    uint16_t contentLength = getAuth(fmt, appKeyValue, buffer, auth);

    m_client.stop();
    m_client.flush();

    int connected = 0;
    TEMBOO_TRACE("Connecting: ");

    // reserve space for the "host" string sufficient to hold either the 
    // (dotted-quad) IP address + port, or the default <account>.temboolive.com
    // host string.
    int hostLen = strlen_P(TEMBOO_DOMAIN) + strlen(accountName) + 1;
    char host[hostLen];
    
    // Construct the "host" string from the account name and the temboo domain name.

    strcpy(host, accountName);
    strcat_P(host, TEMBOO_DOMAIN);
    
    bool useProxy = false;
    
    if (m_addr == INADDR_NONE) {
        TEMBOO_TRACELN(host);
        connected = m_client.connect(host, m_port);
    } else {
        
        TEMBOO_TRACELN(host);
        connected = m_client.connect(m_addr, m_port);
        
        useProxy = true;
    }

    if (connected) {

        TEMBOO_TRACELN("OK. req:");
        qsendProgmem(POST);
        if(useProxy) {
            qsendProgmem(HTTP);
            qsend(host);
        }
        qsendProgmem(BASE_CHOREO_URI);
        qsend(path);
        qsendProgmem(SDK_ID);
        qsendlnProgmem(POSTAMBLE);
        
        // Send our custom authentication header
        // (app-key-name:hmac)
        qsendProgmem(HEADER_AUTH);
        qsend(appKeyName);
        qsend(":");
        qsendln(auth);
        
        // send the standard host header
        qsendProgmem(HEADER_HOST);
        qsendln(host);
        
        // send the standard accept header
        qsendlnProgmem(HEADER_ACCEPT);
        
        // send our custom account name neader
        qsendProgmem(HEADER_ORG);
        qsend(accountName);
        qsendlnProgmem(HEADER_DOM);
        
        // send the standard content type header
        qsendlnProgmem(HEADER_CONTENT_TYPE);
        
        // send our custom client time header
        qsendProgmem(HEADER_TIME);
        qsendln(buffer);
        
        // send the standard content length header
        qsendProgmem(HEADER_CONTENT_LENGTH);
        qsendln(uint16toa(contentLength, buffer));

        qsendProgmem(EOL);
        
        // Format and send the body of the request
        fmt.reset();
        while(fmt.hasNext()) {
            qsend(fmt.next());
        }

        qsendProgmem(EOL);
        qflush();
        return 0;
    } else {
        TEMBOO_TRACELN("FAIL");
        return 1;
    }
}