/** * REST API to initiate request for a change of password process * * @param emailAddress specifies the username of the user * @return returns the result received from server, otherwise NULL */ char *requestChangePassword(char *emailAddress) { struct curl_slist *headers = NULL; char *url; char body[BODY_SIZE_MIN]; char *authorizationHeader = (char *)getConfigAuthorizationToken(); HttpResponse *response = NULL; if(authorizationHeader == NULL) { fprintf(stderr, "requestChangePassword::Authorization Token not available\n"); return NULL; } response = (HttpResponse *)malloc(sizeof(HttpResponse)); response->code = 0; response->data = NULL; if(!emailAddress) { fprintf(stderr, "requestChangePassword::Parameter email address cannot be NULL"); return false; } if(prepareUrl(&url, configurations.base_url, configurations.request_change_password, NULL)) { appendHttpHeader(&headers, HEADER_CONTENT_TYPE_NAME, HEADER_CONTENT_TYPE_JSON); appendHttpHeader(&headers, HEADER_AUTHORIZATION, authorizationHeader); sprintf(body, "{\"email\":\"%s\"}", emailAddress); #if DEBUG printf("Prepared BODY is %s\n", body); #endif doHttpPost(url, headers, body, response); return createHttpResponseJson(response); } return NULL; }
/** * REST API to create an user * * @param emailAddress specifies the username (email address) for the user * @param password specifies the password for the user * @param acceptTermsAndConditions specifies acceptance for terms and conditions * @return returns the result received from server, otherwise NULL */ char *createAnUser(char *emailAddress, char *password, bool acceptTermsAndConditions) { struct curl_slist *headers = NULL; char *url; char body[BODY_SIZE_MIN]; HttpResponse *response = (HttpResponse *)malloc(sizeof(HttpResponse)); response->code = 0; response->data = NULL; if(!emailAddress || !password) { fprintf(stderr, "createAnUser::Mandatory parameters cannot be NULL"); return NULL; } if(prepareUrl(&url, configurations.base_url, configurations.create_a_user, NULL)) { appendHttpHeader(&headers, HEADER_CONTENT_TYPE_NAME, HEADER_CONTENT_TYPE_JSON); sprintf(body, "{\"email\":\"%s\",\"password\":\"%s\",", emailAddress, password); if(acceptTermsAndConditions) { strcat(body, "\"termsAndConditions\": true"); } else { strcat(body, "\"termsAndConditions\": false"); } strcat(body, "}"); #if DEBUG printf("Prepared BODY is %s\n", body); #endif doHttpPost(url, headers, body, response); return createHttpResponseJson(response); } return NULL; }
/** * REST API to submit data along with sensor location * * @param cname specifies the component name * @param value specifies the value * @param latitide specifies the geo location - latitude of the measurement * @param longitude specifies the geo location - longitude of the measurement * @param height specifies the physical height of the component during the measurement * @return returns the result received from server, otherwise NULL */ char *submitDataWithLoc(char *cname, char *value, char *latitude, char *longitude, char *height) { struct curl_slist *headers = NULL; char *url; char body[BODY_SIZE_MED]; char *cid = NULL; char currentTimeInMills[BODY_SIZE_MED]; char *deviceAuthorizationHeader = (char *)getDeviceAuthorizationToken(); HttpResponse *response = NULL; if(deviceAuthorizationHeader == NULL) { fprintf(stderr, "submitData::Device Authorization Token not available\n"); return NULL; } response = (HttpResponse *)malloc(sizeof(HttpResponse)); response->code = 0; response->data = NULL; if(!cname) { fprintf(stderr, "submitData::Component Name cannot be NULL\n"); return NULL; } cid = getSensorComponentId(cname); if(!cid) { fprintf(stderr, "submitData::Component is not registered\n"); return NULL; } if(!value) { fprintf(stderr, "submitData::Value cannot be NULL\n"); return NULL; } if(!configurations.data_account_id) { fprintf(stderr, "submitData::Account is NULL. Device appears to be unactivated\n"); return NULL; } if(prepareUrl(&url, configurations.base_url, configurations.submit_data, NULL)) { appendHttpHeader(&headers, HEADER_CONTENT_TYPE_NAME, HEADER_CONTENT_TYPE_JSON); appendHttpHeader(&headers, HEADER_AUTHORIZATION, deviceAuthorizationHeader); sprintf(currentTimeInMills, "%lld", (long long) getCurrentTimeInSeconds() * 1000L); strcpy(body, "{"); strcat(body, "\"on\":"); strcat(body, currentTimeInMills); strcat(body, ",\"accountId\":\""); strcat(body, configurations.data_account_id); strcat(body, "\",\"data\":[{\"componentId\":\""); strcat(body, cid); strcat(body, "\",\"on\":"); strcat(body, currentTimeInMills); if(latitude != NULL && longitude != NULL) { strcat(body, ",\"loc\":["); strcat(body, latitude); strcat(body, ","); strcat(body, longitude); if(height) { strcat(body, ","); strcat(body, height); } strcat(body, "]"); } strcat(body, ",\"value\":\""); strcat(body, value); strcat(body, "\"}]}"); #if DEBUG printf("Prepared BODY is %s\n", body); #endif doHttpPost(url, headers, body, response); return createHttpResponseJson(response); } return NULL; }
/** * REST API to retrieve data using device token instead of authorization token. * This is helpful when a device is activated directly using activation code from portal. * * @param retrieveObj the object created using createRetrieveDataObject() * @return returns the result received from server, otherwise NULL */ char *retrieveData2(RetrieveData *retrieveObj) { struct curl_slist *headers = NULL; char *url; char body[BODY_SIZE_MED]; char fromTimeInMillis[BODY_SIZE_MIN]; char toTimeInMillis[BODY_SIZE_MIN]; StringList *traverse = NULL; char *deviceAuthorizationHeader = (char *)getDeviceAuthorizationToken(); HttpResponse *response = NULL; if(deviceAuthorizationHeader == NULL) { fprintf(stderr, "retrieveData::Device Token not available\n"); return NULL; } response = (HttpResponse *)malloc(sizeof(HttpResponse)); response->code = 0; response->data = NULL; if(!retrieveObj->deviceList) { fprintf(stderr, "retrieveData::Device ID cannot be NULL"); return NULL; } if(!retrieveObj->componentId) { fprintf(stderr, "retrieveData::Component ID cannot be NULL"); return NULL; } if(prepareUrl(&url, configurations.base_url, configurations.retrieve_data, NULL)) { appendHttpHeader(&headers, HEADER_CONTENT_TYPE_NAME, HEADER_CONTENT_TYPE_JSON); appendHttpHeader(&headers, HEADER_AUTHORIZATION, deviceAuthorizationHeader); sprintf(fromTimeInMillis, "%lld", (long long) retrieveObj->fromMillis * 1000L); sprintf(toTimeInMillis, "%lld", (long long) retrieveObj->toMillis * 1000L); strcpy(body, "{"); strcat(body, "\"from\":"); strcat(body, fromTimeInMillis); strcat(body, ",\"to\":"); strcat(body, toTimeInMillis); strcat(body, ",\"targetFilter\":{\"deviceList\":["); traverse = retrieveObj->deviceList; while(traverse != NULL) { strcat(body, "\""); strcat(body, traverse->data); strcat(body, "\""); traverse = traverse->next; if(traverse) { strcat(body, ","); } } strcat(body, "]}"); strcat(body, ",\"metrics\":["); traverse = retrieveObj->componentId; while(traverse != NULL) { strcat(body, "{\"id\":\""); strcat(body, traverse->data); strcat(body, "\",\"op\":\"none\"}"); traverse = traverse->next; if(traverse) { strcat(body, ","); } } strcat(body, "]}"); #if DEBUG printf("Prepared BODY is %s\n", body); #endif doHttpPost(url, headers, body, response); return createHttpResponseJson(response); } return NULL; }
am_status_t LogService::flushBuffer() throw() { ScopeLock scopeLock(mLock); if(bufferCount <= 0 || !remoteBodyChunkListInitialized) { return AM_SUCCESS; } am_status_t status = AM_FAILURE; remoteBodyChunkList.push_back(requestSetSuffixChunk); Http::Response response; status = doHttpPost(serviceInfo, std::string(), cookieList, remoteBodyChunkList, response); if (status == AM_SUCCESS) { try { std::vector<std::string> loggingResponses; if (remoteRequest != NULL) { loggingResponses = parseGenericResponse(response, remoteRequest->getGlobalId()); } status = AM_ERROR_PARSING_XML; if (loggingResponses.empty()) { status = AM_ERROR_PARSING_XML; // logging response is empty } else { status = AM_SUCCESS; // What if there are more than one logging response ? // status is success only if all responses are success. // otherwise set status to the first error encountered. for (std::size_t i = 0; i < loggingResponses.size(); ++i) { if (strstr(loggingResponses[i].c_str(), "OK")) { continue; } else if (strstr(loggingResponses[i].c_str(), "ERROR")) { status = AM_REMOTE_LOG_FAILURE; break; } else if (strstr(loggingResponses[i].c_str(), "INVALID_SESSION")) { status = AM_ACCESS_DENIED; break; } else if (strstr(loggingResponses[i].c_str(), "UNAUTHORIZED")) { status = AM_ACCESS_DENIED; break; } else { // unknown response. status = AM_ERROR_PARSING_XML; break; } } } } catch (const XMLTree::ParseException& exc) { Log::log(logModule, Log::LOG_ERROR, "LogService::flushBuffer() caught exception: %s", exc.getMessage().c_str()); status = AM_ERROR_PARSING_XML; } catch (std::exception& exs) { Log::log(logModule, Log::LOG_ERROR, "LogService::flushBuffer() caught exception: %s", exs.what()); status = AM_ERROR_PARSING_XML; } catch (...) { Log::log(logModule, Log::LOG_ERROR, "LogService::flushBuffer() caught unknown exception."); status = AM_ERROR_PARSING_XML; } } bufferCount = 0; remoteBodyChunkListInitialized = false; remoteBodyChunkList.clear(); if (remoteRequest != NULL) { delete remoteRequest; remoteRequest = NULL; } return status; }
// this does not throw any exceptions since it returns a status. am_status_t LogService::logMessage(const ServiceInfo& service, const SSOToken& logged_by_token, const Http::CookieList& ckieList, const std::string& message, const std::string& logname) throw() { am_status_t status = AM_SUCCESS; encodedMessage = NULL; //The encoded log message needs to be in multiple of 4 bytes. encodedMessage = (char *)malloc(((message.size() * 4/3 + 1)/4 + 1)*4 + 4); if(encodedMessage != NULL) { encode_base64(message.c_str(), message.size(), encodedMessage); } else { status = AM_NO_MEMORY; } if (status == AM_SUCCESS) { if(logged_by_token.isValid()) { const std::size_t NUM_EXTRA_CHUNKS = 10; Request request(*this, requestPrefixChunk, logRequestPrefixChunk, NUM_EXTRA_CHUNKS); Http::Response response; BodyChunkList& bodyChunkList = request.getBodyChunkList(); bodyChunkList.push_back(logLogPrefixChunk); bodyChunkList.push_back(BodyChunk(logname)); bodyChunkList.push_back(logSidPrefixChunk); bodyChunkList.push_back(BodyChunk(logged_by_token.getString())); bodyChunkList.push_back(logLogSuffixChunk); bodyChunkList.push_back(logRecordPrefixChunk); bodyChunkList.push_back(logRecordTypeChunk); bodyChunkList.push_back(logRecMsgPrefixChunk); /* Ensuring message is correctly entity ref'ed. */ std::string t_string(encodedMessage); free(encodedMessage); Utils::expandEntityRefs(t_string); bodyChunkList.push_back(BodyChunk(t_string)); bodyChunkList.push_back(logRecMsgSuffixChunk); bodyChunkList.push_back(logRecordSuffixChunk); bodyChunkList.push_back(requestSuffixChunk); bodyChunkList.push_back(requestSetSuffixChunk); status = doHttpPost(service, std::string(), ckieList, bodyChunkList, response); if (AM_SUCCESS == status) { try { std::vector<std::string> loggingResponses; // don't know if the log response xml shares the basic/generic // format. loggingResponses = parseGenericResponse(response, request.getGlobalId()); status = AM_ERROR_PARSING_XML; if (loggingResponses.empty()) { status = AM_ERROR_PARSING_XML; } else { status = AM_SUCCESS; // return success only if all responses are successful for (std::size_t i = 0; i < loggingResponses.size(); ++i) { if (strstr(loggingResponses[i].c_str(), "OK")) { continue; } else if (strstr(loggingResponses[i].c_str(), "INVALID_SESSION")) { status = AM_ACCESS_DENIED; break; } else if (strstr(loggingResponses[i].c_str(), "UNAUTHORIZED")) { status = AM_ACCESS_DENIED; break; } else if (strstr(loggingResponses[i].c_str(), "ERROR")) { status = AM_REMOTE_LOG_FAILURE; break; } else { // unrecognized response status = AM_ERROR_PARSING_XML; break; } } } } catch (const XMLTree::ParseException& exc) { Log::log(logModule, Log::LOG_ERROR, "LogService::logMessage() caught exception: %s", exc.getMessage().c_str()); status = AM_ERROR_PARSING_XML; } catch (std::exception& exs) { Log::log(logModule, Log::LOG_ERROR, "LogService::logMessage() caught exception: %s", exs.what()); status = AM_ERROR_PARSING_XML; } catch (...) { Log::log(logModule, Log::LOG_ERROR, "LogService::logMessage() caught unknown exception"); status = AM_ERROR_PARSING_XML; } } } else { status = AM_INVALID_ARGUMENT; } } else { status = AM_NO_MEMORY; } return status; }
/** * REST API for advanced data inquiry * * @param advancedDataInquiryObject object created using createAdvancedDataInquiryObject"()" * @return returns the result received from server, otherwise NULL */ char *advancedDataInquiry(AdvancedDataInquiry *advancedDataInquiryObject) { struct curl_slist *headers = NULL; char *url; char body[BODY_SIZE_MED]; StringList *traverse = NULL; char *authorizationHeader = (char *)getConfigAuthorizationToken(); HttpResponse *response = NULL; if(authorizationHeader == NULL) { fprintf(stderr, "advancedDataInquiry::Authorization Token not available\n"); return NULL; } response = (HttpResponse *)malloc(sizeof(HttpResponse)); response->code = 0; response->data = NULL; if(prepareUrl(&url, configurations.base_url, configurations.advanced_data_inquiry, NULL)) { appendHttpHeader(&headers, HEADER_CONTENT_TYPE_NAME, HEADER_CONTENT_TYPE_JSON); appendHttpHeader(&headers, HEADER_AUTHORIZATION, authorizationHeader); strcpy(body, "{"); if(advancedDataInquiryObject->gatewayIds) { strcat(body, "\"gatewayIds\":["); traverse = advancedDataInquiryObject->gatewayIds; while (traverse != NULL) { strcat(body, "\""); strcat(body, traverse->data); strcat(body, "\""); traverse = traverse->next; if(traverse) { strcat(body, ","); } } strcat(body, "],"); } if(advancedDataInquiryObject->deviceIds) { strcat(body, "\"deviceIds\":["); traverse = advancedDataInquiryObject->deviceIds; while (traverse != NULL) { strcat(body, "\""); strcat(body, traverse->data); strcat(body, "\""); traverse = traverse->next; if(traverse) { strcat(body, ","); } } strcat(body, "],"); } if(advancedDataInquiryObject->componentIds) { strcat(body, "\"componentIds\":["); traverse = advancedDataInquiryObject->componentIds; while (traverse != NULL) { strcat(body, "\""); strcat(body, traverse->data); strcat(body, "\""); traverse = traverse->next; if(traverse) { strcat(body, ","); } } strcat(body, "],"); } // if(advancedDataInquiryObject->startTimestamp > 0L) { char timeStamp[BODY_SIZE_MIN]; sprintf(timeStamp, "%lld", advancedDataInquiryObject->startTimestamp); strcat(body, "\"from\":"); strcat(body, timeStamp); } // if(advancedDataInquiryObject->endTimestamp> 0L) { char timeStamp[BODY_SIZE_MIN]; sprintf(timeStamp, "%lld", advancedDataInquiryObject->endTimestamp); strcat(body, ",\"to\":"); strcat(body, timeStamp); } if(advancedDataInquiryObject->returnedMeasureAttributes) { strcat(body, ",\"returnedMeasureAttributes\":["); traverse = advancedDataInquiryObject->returnedMeasureAttributes; while (traverse != NULL) { strcat(body, "\""); strcat(body, traverse->data); strcat(body, "\""); traverse = traverse->next; if(traverse) { strcat(body, ","); } } strcat(body, "]"); } if(advancedDataInquiryObject->showMeasureLocation) { strcat(body, ",\"showMeasureLocation\": true"); } if(advancedDataInquiryObject->componentRowLimit) { char rowLimit[BODY_SIZE_MIN]; sprintf(rowLimit, "%d", advancedDataInquiryObject->componentRowLimit); strcat(body, ",\"componentRowLimit\":"); strcat(body, rowLimit); } if(advancedDataInquiryObject->sort) { KeyValueParams *traverseKeyValues = advancedDataInquiryObject->sort; strcat(body, ",\"sort\":["); while(traverseKeyValues != NULL) { strcat(body, "{\""); strcat(body, traverseKeyValues->name); strcat(body, "\":\""); strcat(body, traverseKeyValues->value); strcat(body, "\"}"); traverseKeyValues = traverseKeyValues->next; if(traverseKeyValues) { strcat(body, ","); } } strcat(body, "]"); } if(advancedDataInquiryObject->countOnly) { strcat(body, ",\"countOnly\": true"); } if(advancedDataInquiryObject->devCompAttributeFilter) { AttributeFilterList *attributesList = advancedDataInquiryObject->devCompAttributeFilter; strcat(body, ",\"devCompAttributeFilter\":{"); while(attributesList) { AttributeFilter *attributes = attributesList->filterData; #if DEBUG printf("Attributes filter is %d\n", attributesList->filterData); #endif while(attributes) { #if DEBUG printf("Attributes filter Name is %s\n", attributes->filterName); #endif strcat(body, "\""); strcat(body, attributes->filterName); strcat(body, "\":["); traverse = attributes->filterValues; while (traverse != NULL) { strcat(body, "\""); strcat(body, traverse->data); strcat(body, "\""); traverse = traverse->next; if(traverse) { strcat(body, ","); } } strcat(body, "]"); attributes = attributes->next; if(attributes) { strcat(body, ","); } } attributesList = attributesList->next; if(attributesList) { strcat(body, ","); } } strcat(body, "}"); } if(advancedDataInquiryObject->measurementAttributeFilter) { AttributeFilterList *attributesList = advancedDataInquiryObject->measurementAttributeFilter; strcat(body, ",\"measurementAttributeFilter\":{"); while(attributesList) { AttributeFilter *attributes = attributesList->filterData; while(attributes) { strcat(body, "\""); strcat(body, attributes->filterName); strcat(body, "\":["); traverse = attributes->filterValues; while (traverse != NULL) { strcat(body, "\""); strcat(body, traverse->data); strcat(body, "\""); traverse = traverse->next; if(traverse) { strcat(body, ","); } } strcat(body, "]"); attributes = attributes->next; if(attributes) { strcat(body, ","); } } attributesList = attributesList->next; if(attributesList) { strcat(body, ","); } } strcat(body, "}"); } if(advancedDataInquiryObject->valueFilter) { strcat(body, ",\"valueFilter\":{\""); strcat(body, advancedDataInquiryObject->valueFilter->filterName); strcat(body, "\":["); traverse = advancedDataInquiryObject->valueFilter->filterValues; while (traverse != NULL) { strcat(body, "\""); strcat(body, traverse->data); strcat(body, "\""); traverse = traverse->next; if(traverse) { strcat(body, ","); } } strcat(body, "]}"); } strcat(body, "}"); #if DEBUG printf("Prepared BODY is %s\n", body); #endif doHttpPost(url, headers, body, response); return createHttpResponseJson(response); } return NULL; }