예제 #1
0
파일: h2swrapper.c 프로젝트: vprabu/iotsdk
/**
 * Wraps the message to the server inside an XML header and footer
 *
 * @param msg The message to wrap
 * @param maxSize The maximum size of the message buffer
 * @param sequenceNum Sequence number of the XML message
 * @return Number of bytes written or -1 for error
 */
int h2swrapper_wrap(char *dest, char *message, int destSize) {
    int bytesWritten = 0;
    char localAddress[EUI64_STRING_SIZE];

    assert(dest);
    assert(message);

    sequenceNum++;

    eui64_toString(localAddress, sizeof(localAddress));

    bytesWritten += snprintf(dest, destSize - bytesWritten,
                             "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
                             "<h2s ver=\"2\" hubId=\"%s\" seq=\"%u\">", localAddress, sequenceNum);

    strncpy(dest + bytesWritten, message, destSize - bytesWritten);

    bytesWritten += strlen(message);

    bytesWritten += snprintf(dest + bytesWritten, destSize - bytesWritten, "</h2s>");

    return bytesWritten;
}
예제 #2
0
/**
 * Register Device
 * @return
 */
error_t registerDevice(void) {
    char url[PATH_MAX];
    char baseUrl[PATH_MAX];
    char deviceType[8];
    char rxBuffer[PROXY_MAX_MSG_LEN];
    char headerApiKey[PROXY_HEADER_KEY_LEN];
    char eui64[EUI64_STRING_SIZE+8];
    http_param_t params;
    registrationinfo_info_t registrationInfo;

    bzero(&params, sizeof(params));

    xmlSAXHandler saxHandler = {
        NULL, // internalSubsetHandler,
        NULL, // isStandaloneHandler,
        NULL, // hasInternalSubsetHandler,
        NULL, // hasExternalSubsetHandler,
        NULL, // resolveEntityHandler,
        NULL, // getEntityHandler,
        NULL, // entityDeclHandler,
        NULL, // notationDeclHandler,
        NULL, // attributeDeclHandler,
        NULL, // elementDeclHandler,
        NULL, // unparsedEntityDeclHandler,
        NULL, // setDocumentLocatorHandler,
        NULL, // startDocument
        NULL, // endDocument
        _registrationinfo_xml_startElementHandler, // startElement
        NULL, // endElement
        NULL, // reference,
        _registrationinfo_xml_charactersHandler, //characters
        NULL, // ignorableWhitespace
        NULL, // processingInstructionHandler,
        NULL, // comment
        NULL, // warning
        NULL, // error
        NULL, // fatal
    };

    bzero(deviceType, sizeof(deviceType));
    bzero(&params, sizeof(params));
    snprintf(headerApiKey, sizeof(headerApiKey), "FABRUX_API_KEY: %s", login_getApiKey());

    // Read the device type from the configuration file
    if(libconfigio_read(proxycli_getConfigFilename(), CONFIGIO_PROXY_DEVICE_TYPE_TOKEN_NAME, deviceType, sizeof(deviceType)) == -1) {
        printf("Couldn't read %s in file %s, writing default value\n", CONFIGIO_PROXY_DEVICE_TYPE_TOKEN_NAME, proxycli_getConfigFilename());
        libconfigio_write(proxycli_getConfigFilename(), CONFIGIO_PROXY_DEVICE_TYPE_TOKEN_NAME, DEFAULT_PROXY_DEVICETYPE);
        strncpy(deviceType, DEFAULT_PROXY_DEVICETYPE, sizeof(deviceType));
    }

    // Read the activation URL from the configuration file
    if(libconfigio_read(proxycli_getConfigFilename(), CONFIGIO_ACTIVATION_URL_TOKEN_NAME, baseUrl, sizeof(baseUrl)) == -1) {
        printf("Couldn't read %s in file %s, writing default value\n", CONFIGIO_ACTIVATION_URL_TOKEN_NAME, proxycli_getConfigFilename());
        libconfigio_write(proxycli_getConfigFilename(), CONFIGIO_ACTIVATION_URL_TOKEN_NAME, DEFAULT_ACTIVATION_URL);
        strncpy(baseUrl, DEFAULT_ACTIVATION_URL, sizeof(baseUrl));
    }

    eui64_toString(eui64, sizeof(eui64));
    // https://developer.presencepro.com/cloud/json/devices/001C42DE23CF-4-33F?productId=4
    snprintf(url, sizeof(url), "%s/devices/%s?productId=%s", baseUrl, eui64, deviceType);

    SYSLOG_INFO("Register device...");
    SYSLOG_INFO("Contacting URL %s\n", url);

    params.verbose = TRUE;
    params.timeouts.connectTimeout = HTTPCOMM_DEFAULT_CONNECT_TIMEOUT_SEC;
    params.timeouts.transferTimeout = HTTPCOMM_DEFAULT_TRANSFER_TIMEOUT_SEC;
    params.key = headerApiKey;

    libhttpcomm_postMsg(NULL, CURLOPT_HTTPPOST, url, NULL, NULL, "", 0, rxBuffer, sizeof(rxBuffer), params, NULL);

    SYSLOG_INFO("Server returned: \n%s\n", rxBuffer);

    registrationInfo.resultCode = -1;

    if ( 0 == xmlSAXUserParseMemory(&saxHandler, &registrationInfo, rxBuffer, strlen(rxBuffer)) )
    {

        if(registrationInfo.resultCode == 0) {
            printf("Register device successful!\n");
            SYSLOG_INFO("Register device successful");
            return SUCCESS;

        } else {
            printf("Error register device\n");
            return FAIL;
        }
    }

    printf("Error register device\n");
    return FAIL;
}