Esempio n. 1
0
int InitGetRequest(OCQualityOfService qos)
{
    std::ostringstream query;
    //Get most recently inserted resource
    const ResourceNode * resource  = getResource();

    if(!resource)
    {
        OC_LOG_V(ERROR, TAG, "Resource null, can't do GET request\n");
        return -1;
    }
    query << resource->uri;
    OC_LOG_V(INFO, TAG,"Executing InitGetRequest, Query: %s", query.str().c_str());

    return (InvokeOCDoResource(query, OC_REST_GET, &resource->endpoint,
            (qos == OC_HIGH_QOS)?OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
}
int InitGetRequest(OCQualityOfService qos)
{
    OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
    std::ostringstream query;
    query << coapServerResource;
    return (InvokeOCDoResource(query, OC_REST_GET,
                (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, restRequestCB, NULL, 0));
}
int InitPutRequest()
{
    OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
    std::ostringstream query;
    query << (coapSecureResource ? "coaps://" : "coap://") << coapServerIP
        << ":" << coapServerPort << coapServerResource;
    return (InvokeOCDoResource(query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0));
}
Esempio n. 4
0
OCEntityHandlerResult LcdOCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest,
                                        void *callbackParam)
{
	OCEntityHandlerResult ehRet = OC_EH_OK;
	OCEntityHandlerResponse response = {0};
	OCRepPayload* payload = OCRepPayloadCreate();
	if(!payload) {
		OC_LOG(ERROR, TAG, ("Failed to allocate Payload"));
		return OC_EH_ERROR;
	}

	if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) {
		OC_LOG (INFO, TAG, ("Flag includes OC_REQUEST_FLAG"));

		if(OC_REST_GET == entityHandlerRequest->method) {
			OCRepPayloadSetUri(payload, "/grove/lcd");
			OCRepPayloadSetPropString(payload, "lcd", (const char *)lcd.str);
		} else if(OC_REST_PUT == entityHandlerRequest->method) {
			OC_LOG(INFO, TAG, ("PUT request"));
			OCRepPayload *rep = (OCRepPayload *)entityHandlerRequest->payload;
			OCRepPayloadGetPropString(rep, "lcd", &lcd.str);
			OC_LOG_V(INFO, TAG, "LCD string: %s", lcd.str);
			lcd_put();
			OCRepPayloadSetPropString(payload, "lcd", (const char *)lcd.str);
		}

		if (ehRet == OC_EH_OK) {
			// Format the response.  Note this requires some info about the request
			response.requestHandle = entityHandlerRequest->requestHandle;
			response.resourceHandle = entityHandlerRequest->resource;
			response.ehResult = ehRet;
			response.payload = (OCPayload*) payload;
			response.numSendVendorSpecificHeaderOptions = 0;
			memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions);
			memset(response.resourceUri, 0, sizeof response.resourceUri);
			// Indicate that response is NOT in a persistent buffer
			response.persistentBufferFlag = 0;

			// Send the response
			if (OCDoResponse(&response) != OC_STACK_OK) {
				OC_LOG(ERROR, TAG, "Error sending response");
				ehRet = OC_EH_ERROR;
			}
		}
	}

	if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG)) {
		if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) {
			OC_LOG (INFO, TAG, ("Received OC_OBSERVE_REGISTER from client"));
			gLightUnderObservation = 1;
		} else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) {
			OC_LOG (INFO, TAG, ("Received OC_OBSERVE_DEREGISTER from client"));
			gLightUnderObservation = 0;
		}
	}
	OCRepPayloadDestroy(payload);
	return ehRet;
}
Esempio n. 5
0
static void printCred(const OicSecCred_t * cred)
{
    EXPECT_TRUE(NULL != cred);

    const OicSecCred_t *credTmp1 = NULL;
    for(credTmp1 = cred; credTmp1; credTmp1 = credTmp1->next)
    {
        OC_LOG_V(INFO, TAG, "\ncred->credId = %d", credTmp1->credId);
        OC_LOG_V(INFO, TAG, "cred->subject.id = %s", credTmp1->subject.id);
        OC_LOG_V(INFO, TAG, "cred->credType = %d", credTmp1->credType);
        if(credTmp1->privateData.data)
        {
            OC_LOG_V(INFO, TAG, "cred->privateData.data = %s", credTmp1->privateData.data);
        }
        if(credTmp1->publicData.data)
        {
           OC_LOG_V(INFO, TAG, "cred->publicData.data = %s", credTmp1->publicData.data);
        }
        OC_LOG_V(INFO, TAG, "cred->ownersLen = %zu", credTmp1->ownersLen);
        for(size_t i = 0; i < cred->ownersLen; i++)
        {
            OC_LOG_V(INFO, TAG, "cred->owners[%zu].id = %s", i, credTmp1->owners[i].id);
        }
    }
}
Esempio n. 6
0
OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag,
        OCEntityHandlerRequest *entityHandlerRequest, void* callbackParam)
{
    OCEntityHandlerResult result = OC_EH_ERROR;
    OCEntityHandlerRequest *request = NULL;

    OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);

    if (flag & OC_REQUEST_FLAG)
    {
        OC_LOG(INFO, TAG, "Flag includes OC_REQUEST_FLAG");
        if (entityHandlerRequest)
        {
            OC_LOG_V (INFO, TAG, "request query %s from client",
                                        entityHandlerRequest->query);
            OC_LOG_PAYLOAD (INFO, TAG, entityHandlerRequest->payload);

            // Make deep copy of received request and queue it for slow processing
            request = CopyRequest(entityHandlerRequest);

            if (request)
            {

                OC_LOG(INFO, TAG, "Scheduling slow response for received request");
                gRequestList.push_back(request);
                // Indicate to the stack that this is a slow response
                result = OC_EH_SLOW;
                // Start the slow response alarm
                alarm(SLOW_RESPONSE_DELAY_SEC);
            }
            else
            {
                OC_LOG(ERROR, TAG, "Error queuing request for slow response");
                // Indicate to the stack that this is a slow response
                result = OC_EH_ERROR;
            }
        }
        else
        {
            OC_LOG(ERROR, TAG, "Invalid request");
            result = OC_EH_ERROR;
        }
    }
    return result;
}
Esempio n. 7
0
int InitPutRequest(OCQualityOfService qos)
{
    std::ostringstream query;
    //Get most recently inserted resource
    const ResourceNode * resource  = getResource();

    if(!resource)
    {
        OC_LOG_V(ERROR, TAG, "Resource null, can't do PUT request\n");
        return -1;
    }
    query << "coap://" << resource->ip << ":" << resource->port << resource->uri ;
    OC_LOG_V(INFO, TAG,"Executing InitPutRequest, Query: %s", query.str().c_str());

    return (InvokeOCDoResource(query, OC_REST_PUT, resource->connType,
           ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
            putReqCB, NULL, 0));
}
// This is a function called back when a device is discovered
OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
        OCClientResponse * clientResponse)
{
    if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
    {
        OC_LOG(INFO, TAG, "DISCOVER  callback recvd");
    }

    if (!clientResponse)
    {
        OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
    }

    OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
    OC_LOG_PAYLOAD(INFO, clientResponse->payload);

    responseAddr = clientResponse->devAddr;

    switch(TEST_CASE)
    {
        OC_LOG_V(INFO, TAG, "TEST_CASE %u\n", TEST_CASE);
        case TEST_GET_REQ_NON:
            InitGetRequest(OC_LOW_QOS);
            break;
        case TEST_PUT_REQ_NON:
            InitPutRequest(OC_LOW_QOS);
            break;
        case TEST_POST_REQ_NON:
            InitPostRequest(OC_LOW_QOS);
            break;
        case TEST_DELETE_REQ_NON:
            InitDeleteRequest(OC_LOW_QOS);
            break;
        case TEST_OBS_REQ_NON:
        case TEST_OBS_REQ_NON_CANCEL_IMM:
            InitObserveRequest(OC_LOW_QOS);
            break;
        default:
            PrintUsage();
            break;
    }
    SET_BUT_NOT_USED(handle);
    return OC_STACK_KEEP_TRANSACTION;
}
/**
 * Internal Function to store results in result array during ACL provisioning.
 */
static void registerResultForACLProvisioning(ACLData_t *aclData,
                                             OCStackResult stackresult)
{
   OC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
                       aclData->numOfResults);
   memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
          aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
   aclData->resArr[(aclData->numOfResults)].res = stackresult;
   ++(aclData->numOfResults);
}
Esempio n. 10
0
OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
                                  OCClientResponse * clientResponse)
{
    if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
    {
        OC_LOG(INFO, TAG, "<====Callback Context for GET received successfully====>");
    }
    else
    {
        OC_LOG(ERROR, TAG, "<====Callback Context for GET fail====>");
    }

    if (clientResponse)
    {
        OC_LOG_V(INFO, TAG, "StackResult: %s",  getResult(clientResponse->result));
        OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
        OC_LOG_PAYLOAD(INFO, clientResponse->payload);
        OC_LOG(INFO, TAG, ("=============> Get Response"));

        if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0 )
        {
            OC_LOG (INFO, TAG, "Received vendor specific options");
            uint8_t i = 0;
            OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
            for (i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
            {
                if (((OCHeaderOption) rcvdOptions[i]).protocolID == OC_COAP_ID)
                {
                    OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
                            ((OCHeaderOption)rcvdOptions[i]).optionID );

                    OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
                        MAX_HEADER_OPTION_DATA_LENGTH);
                }
            }
        }
    }
    else
    {
        OC_LOG(ERROR, TAG, "<====GET Callback fail to receive clientResponse====>\n");
    }
    return OC_STACK_DELETE_TRANSACTION;
}
Esempio n. 11
0
int InitGetRequest(OCQualityOfService qos)
{
    OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
    std::ostringstream query;
    query << (coapSecureResource ? "coaps://" : "coap://") << coapServerIP
        << ":" << coapServerPort << coapServerResource;

    return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
            OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
}
Esempio n. 12
0
OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse)
{
    if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
    {
        OC_LOG(INFO, TAG, "Callback Context for POST recvd successfully");
    }

    if(clientResponse)
    {
        OC_LOG_V(INFO, TAG, "StackResult: %s",  getResult(clientResponse->result));
        OC_LOG_V(INFO, TAG, "JSON = %s =============> Post Response",
                clientResponse->resJSONPayload);
    }
    else
    {
        OC_LOG_V(INFO, TAG, "postReqCB received Null clientResponse");
    }
    return OC_STACK_DELETE_TRANSACTION;
}
Esempio n. 13
0
int InitGetRequest( struct cmd *command )
{
    int queryLen = strlen( command->coapuri )+1;
   
    char *query = malloc( queryLen );
    OCStackResult result;

    printf("query len=%d\n", queryLen);

    OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
	snprintf( query, queryLen+1, "%s", command->coapuri );

	OC_LOG_V(INFO, TAG, "query in InitGetRequest: %s", command->coapuri);

    result = InvokeOCDoResource(query, OC_REST_GET, OC_HIGH_QOS, getReqCB, NULL, 0, command);
    free(query);

    return result;
}
Esempio n. 14
0
OCStackResult ZigbeeDiscover(PIPlugin_Zigbee * plugin)
{
    OCStackResult result = OC_STACK_ERROR;
    (void)plugin;
    TWSetDiscoveryCallback(foundZigbeeCallback);
    result = TWDiscover(NULL);
    OC_LOG_V (DEBUG, TAG, "ZigbeeDiscover : Status = %d\n", result);

    return result;
}
Esempio n. 15
0
static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx)
{
    OC_LOG(DEBUG, TAG, "IN PutOwnershipInformation");

    if(!otmCtx || !otmCtx->selectedDeviceInfo)
    {
        OC_LOG(ERROR, TAG, "Invailed parameters");
        return OC_STACK_INVALID_PARAM;
    }

    OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
    char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
    if(!PMGenerateQuery(true,
                        deviceInfo->endpoint.addr, deviceInfo->securePort,
                        deviceInfo->connType,
                        query, sizeof(query), OIC_RSRC_DOXM_URI))
    {
        OC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
        return OC_STACK_ERROR;
    }
    OC_LOG_V(DEBUG, TAG, "Query=%s", query);

    //OwnershipInformationHandler
    OicSecOxm_t selOxm = deviceInfo->doxm->oxmSel;
    OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
    if(!secPayload)
    {
        OC_LOG(ERROR, TAG, "Failed to memory allocation");
        return OC_STACK_NO_MEMORY;
    }
    secPayload->base.type = PAYLOAD_TYPE_SECURITY;
    secPayload->securityData = g_OTMDatas[selOxm].createOwnerTransferPayloadCB(otmCtx);
    if (NULL == secPayload->securityData)
    {
        OICFree(secPayload);
        OC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
        return OC_STACK_INVALID_PARAM;
    }

    OCCallbackData cbData;
    cbData.cb = &OwnershipInformationHandler;
    cbData.context = (void *)otmCtx;
    cbData.cd = NULL;

    OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
                                     deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
    if (res != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack resource error");
    }

    OC_LOG(DEBUG, TAG, "OUT PutOwnershipInformation");

    return res;
}
Esempio n. 16
0
//This function takes the request as an input and returns the response
//in JSON format.
OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
{
    LEDResource *currLEDResource = &LED;

    OC_LOG(INFO, TAG, "Entering constructResponse");

    if (ehRequest->resource == gLedInstance[0].handle)
    {
        OC_LOG(INFO, TAG, "handle 0");
        currLEDResource = &gLedInstance[0];
        gResourceUri = const_cast<char *>("a/led/0");
    }
    else if (ehRequest->resource == gLedInstance[1].handle)
    {
        OC_LOG(INFO, TAG, "handle 1");
        currLEDResource = &gLedInstance[1];
        gResourceUri = const_cast<char *>("a/led/1");
    }

    if(OC_REST_PUT == ehRequest->method)
    {
        if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
        {
            OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
            return nullptr;
        }

        OCRepPayload *putPayload = reinterpret_cast<OCRepPayload*> (ehRequest->payload);

        int64_t power;
        bool state;

        if (OCRepPayloadGetPropBool(putPayload, "state", &state))
        {
            currLEDResource->state = state;
        }
        if (OCRepPayloadGetPropInt (putPayload, "power", &power))
        {
            currLEDResource->power = power;
        }
    }

    OCRepPayload *response = OCRepPayloadCreate();

    if (!response)
    {
        OC_LOG_V(ERROR, TAG, "Memory allocation for response payload failed.");
    }

    OCRepPayloadSetUri (response, gResourceUri);
    OCRepPayloadSetPropBool(response, "state", currLEDResource->state);
    OCRepPayloadSetPropInt(response, "power", currLEDResource->power);

    return response;
}
Esempio n. 17
0
// This is a function called back when a device is discovered
OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
        OCClientResponse * clientResponse)
{
    uint8_t remoteIpAddr[4];
    uint16_t remotePortNu;

    OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");

    if (clientResponse)
    {
        OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));

        OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
                remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
        OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);

        OC_LOG_V(INFO, TAG,
                "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
                clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
                remoteIpAddr[2], remoteIpAddr[3], remotePortNu);

        if (parseClientResponse(clientResponse) != -1)
        {
            switch(TEST_CASE)
            {
                case TEST_NON_CON_OP:
                    InitGetRequest(OC_LOW_QOS);
                    InitPutRequest();
                    //InitPostRequest(OC_LOW_QOS);
                    break;
                case TEST_CON_OP:
                    InitGetRequest(OC_HIGH_QOS);
                    InitPutRequest();
                    //InitPostRequest(OC_HIGH_QOS);
                    break;
            }
        }
    }

    return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;

}
static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx,
                                    OicSecDpom_t selectedOperationMode)
{
    OC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");

    if(!otmCtx || !otmCtx->selectedDeviceInfo)
    {
        return OC_STACK_INVALID_PARAM;
    }

    OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
    char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
    if(!PMGenerateQuery(false,
                        deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
                        deviceInfo->connType,
                        query, sizeof(query), OIC_RSRC_PSTAT_URI))
    {
        OC_LOG(ERROR, TAG, "PutUpdateOperationMode : Failed to generate query");
        return OC_STACK_ERROR;
    }
    OC_LOG_V(DEBUG, TAG, "Query=%s", query);

    deviceInfo->pstat->om = selectedOperationMode;

    OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
    if(!secPayload)
    {
        OC_LOG(ERROR, TAG, "Failed to memory allocation");
        return OC_STACK_NO_MEMORY;
    }
    secPayload->base.type = PAYLOAD_TYPE_SECURITY;
    secPayload->securityData = BinToPstatJSON(deviceInfo->pstat);
    if (NULL == secPayload->securityData)
    {
        OICFree(secPayload);
        OC_LOG(ERROR, TAG, "Error while converting pstat bin to json");
        return OC_STACK_INVALID_PARAM;
    }

    OCCallbackData cbData;
    cbData.cb = &OperationModeUpdateHandler;
    cbData.context = (void *)otmCtx;
    cbData.cd = NULL;
    OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
                                     deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
    if (res != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack resource error");
    }

    OC_LOG(DEBUG, TAG, "OUT PutUpdateOperationMode");

    return res;
}
Esempio n. 19
0
void createLightResource()
{
    Light.state = false;
    OCStackResult res = OCCreateResource(&Light.handle,
            "core.light",
            "oc.mi.def",
            "/a/light",
            OCEntityHandlerCb,
            OC_DISCOVERABLE|OC_OBSERVABLE);
    OC_LOG_V(INFO, TAG, "Created Light resource with result: %s", getResult(res));
}
Esempio n. 20
0
void createResource()
{

    OCStackResult res = OCCreateResource(&m_handle,
                                         "SSManager.Sensor",
                                         "oc.mi.def",
                                         "/Tracker_Thing",
                                         OCEntityHandlerCb,
                                         OC_DISCOVERABLE | OC_OBSERVABLE);
    OC_LOG_V(INFO, TAG, "Created PROXI resource with result: %s", getResult(res));
}
Esempio n. 21
0
OCEntityHandlerResult ProcessNonExistingResourceRequest(OCEntityHandlerRequest *ehRequest, char *payload, uint16_t maxPayloadSize)
{
    OC_LOG_V(INFO, TAG, "\n\nExecuting %s ", __func__);

    const char* response = NULL;
    response = responsePayloadResourceDoesNotExist;

    if ( (ehRequest != NULL) &&
         (maxPayloadSize > strlen ((char *)response)) )
    {
        strncpy((char *)payload, response, strlen((char *)response));
    }
    else
    {
        OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
                maxPayloadSize);
    }

    return OC_EH_RESOURCE_DELETED;
}
Esempio n. 22
0
TEST(BinToCredJSONTest, BinToCredJSONValidCred)
{
    char* json = NULL;
    OicSecCred_t * cred = getCredList();

    json = BinToCredJSON(cred);

    OC_LOG_V(INFO, TAG, "BinToCredJSON:%s\n", json);
    EXPECT_TRUE(json != NULL);
    DeleteCredList(cred);
    OICFree(json);
}
Esempio n. 23
0
int InitPutRequest( struct cmd *command )
{
    int queryLen= strlen( command->coapuri)+1;
    char *query = malloc( queryLen );
    int returnVal;
	
    OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
	snprintf( query, queryLen, "%s", command->coapuri );
    returnVal = InvokeOCDoResource( query, OC_REST_PUT, OC_LOW_QOS, putReqCB, NULL, 0, command);
    free(query);
    return returnVal;
}
Esempio n. 24
0
void createLightResource()
{
    Light.state = false;
    OCStackResult res = OCCreateResource(&Light.handle,
            "core.light",
            OC_RSRVD_INTERFACE_DEFAULT,
            "/a/light",
            OCEntityHandlerCb,
            NULL,
            OC_DISCOVERABLE|OC_OBSERVABLE);
    OC_LOG_V(INFO, TAG, "Created Light resource with result: %s", getResult(res));
}
Esempio n. 25
0
int InitPostRequest(OCQualityOfService qos)
{
    OCStackResult result;
    std::ostringstream query;
    //Get most recently inserted resource
    const ResourceNode *resource  = getResource();

    if(!resource)
    {
        OC_LOG_V(ERROR, TAG, "Resource null, can't do POST request\n");
        return -1;
    }

    query << resource->uri;
    OC_LOG_V(INFO, TAG,"Executing InitPostRequest, Query: %s", query.str().c_str());

    // First POST operation (to create an LED instance)
    result = InvokeOCDoResource(query, OC_REST_POST, &resource->endpoint,
            ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
            postReqCB, NULL, 0);
    if (OC_STACK_OK != result)
    {
        // Error can happen if for example, network connectivity is down
        OC_LOG(ERROR, TAG, "First POST call did not succeed");
    }

    // Second POST operation (to create an LED instance)
    result = InvokeOCDoResource(query, OC_REST_POST, &resource->endpoint,
            ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
            postReqCB, NULL, 0);
    if (OC_STACK_OK != result)
    {
        OC_LOG(ERROR, TAG, "Second POST call did not succeed");
    }

    // This POST operation will update the original resourced /a/led
    return (InvokeOCDoResource(query, OC_REST_POST, &resource->endpoint,
                ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
                postReqCB, NULL, 0));
}
Esempio n. 26
0
void collectUniqueResource(const OCClientResponse * clientResponse)
{
    OCResourcePayload* res = ((OCDiscoveryPayload*)clientResponse->payload)->resources;

    // Including the NUL terminator, length of UUID string of the form:
    //   "a62389f7-afde-00b6-cd3e-12b97d2fcf09"
#   define UUID_LENGTH 37

    char sidStr[UUID_LENGTH];

    while(res) {

        int ret = snprintf(sidStr, UUID_LENGTH,
                "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                res->sid[0], res->sid[1], res->sid[2], res->sid[3],
                res->sid[4], res->sid[5], res->sid[6], res->sid[7],
                res->sid[8], res->sid[9], res->sid[10], res->sid[11],
                res->sid[12], res->sid[13], res->sid[14], res->sid[15]
                );

        if (ret == UUID_LENGTH - 1)
        {
            if(insertResource(sidStr, res->uri, clientResponse) == 1)
            {
                OC_LOG_V(INFO,TAG,"%s%s%s%s\n",sidStr, ":", res->uri, " is new");
                printResourceList();
                queryResource();
            }
            else {
                OC_LOG_V(INFO,TAG,"%s%s%s%s\n",sidStr, ":", res->uri, " is old");
            }
        }
        else
        {
            OC_LOG(ERROR, TAG, "Could Not Retrieve the Server ID");
        }

        res = res->next;
    }
}
Esempio n. 27
0
void sensor_get()
{
	// Get the (raw) value of the temperature sensor.
	int val = analogRead(pinTemp);

	// Determine the current resistance of the thermistor based on the sensor value.
	float resistance = (float)(1023-val)*10000/val;

	// Calculate the temperature based on the resistance value.
	float temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15;
	// Print the temperature to the serial console.
	sensor.temp = (double)temperature;
	OC_LOG_V(INFO, TAG, "Temperature: %f", sensor.temp);

	sensor.light = analogRead(pinLight);
	OC_LOG_V(INFO, TAG, "Light: %d", sensor.light);
	
	sensor.sound = analogRead(pinSound);
	OC_LOG_V(INFO, TAG, "Sound: %d", sensor.sound);

	return;	
}
Esempio n. 28
0
OCStackResult OCConvertPayload(OCPayload* payload, uint8_t** outPayload, size_t* size)
{
    OC_LOG_V(INFO, TAG, "Converting payload of type %d", payload->type);
    switch(payload->type)
    {
        case PAYLOAD_TYPE_DISCOVERY:
            return OCConvertDiscoveryPayload((OCDiscoveryPayload*)payload, outPayload, size);
        case PAYLOAD_TYPE_DEVICE:
            return OCConvertDevicePayload((OCDevicePayload*)payload, outPayload, size);
        case PAYLOAD_TYPE_PLATFORM:
            return OCConvertPlatformPayload((OCPlatformPayload*)payload, outPayload, size);
        case PAYLOAD_TYPE_REPRESENTATION:
            return OCConvertRepPayload((OCRepPayload*)payload, outPayload, size);
        case PAYLOAD_TYPE_PRESENCE:
            return OCConvertPresencePayload((OCPresencePayload*)payload, outPayload, size);
        case PAYLOAD_TYPE_SECURITY:
            return OCConvertSecurityPayload((OCSecurityPayload*)payload, outPayload, size);
        default:
            OC_LOG_V(INFO,TAG, "ConvertPayload default %d", payload->type);
            return OC_STACK_NOTIMPL;
    }
}
Esempio n. 29
0
OCStackResult SendAclReq(PEContext_t *context, OCDevAddr *devAddr, OCConnectivityType connType,
        uint16_t securedPort)
{
    OCStackResult ret = OC_STACK_ERROR;
    const char GET_ACE_QUERY_FMT[] = "%s?%s=%s;%s=%s";
    char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {};
    uint32_t outLen = 0;
    char uri[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {};
    OCCallbackData cbData = {.context=NULL};
    OCDevAddr destAddr = {.adapter = OC_ADAPTER_IP};
    B64Result b64Ret;

    VERIFY_NON_NULL(TAG, context, ERROR);
    VERIFY_NON_NULL(TAG, devAddr, ERROR);

    b64Ret = b64Encode(context->subject.id, sizeof(context->subject.id),
                       base64Buff, sizeof(base64Buff), &outLen);
    VERIFY_SUCCESS(TAG, B64_OK == b64Ret, ERROR);

    snprintf(uri, sizeof(uri), GET_ACE_QUERY_FMT, OIC_RSRC_ACL_URI,
                                    OIC_JSON_SUBJECT_NAME, base64Buff,
                                    OIC_JSON_RESOURCES_NAME, context->resource);

    cbData.cb = &AmsMgrAclReqCallback;
    cbData.context = context;

    destAddr = *devAddr;
    //update port info
    destAddr.flags = (OCTransportFlags)(destAddr.flags | OC_FLAG_SECURE);
    destAddr.port = securedPort;

    OC_LOG_V(INFO, TAG, "AMS Manager Sending Unicast ACL request with URI = %s", uri);
    ret = OCDoResource(NULL, OC_REST_GET, uri, &destAddr, NULL,
            connType, OC_LOW_QOS, &cbData, NULL, 0);

exit:
    OC_LOG_V(INFO, TAG, "%s returns %d ", __func__, ret);
    return ret;
}
Esempio n. 30
0
void FreeCARequestInfo(CARequestInfo_t *requestInfo)
{
    if(NULL == requestInfo)
    {
        OC_LOG_V(ERROR, TAG, "%s: Can't free memory. Received NULL requestInfo", __func__);
        return;
    }
    OICFree(requestInfo->info.token);
    OICFree(requestInfo->info.options);
    OICFree(requestInfo->info.payload);
    OICFree(requestInfo->info.resourceUri);
    OICFree(requestInfo);
}