M2MResource* M2MDevice::create_resource(DeviceResource resource, int64_t value) { M2MResource* res = NULL; String device_id = ""; M2MBase::Operation operation = M2MBase::GET_ALLOWED; if(!is_resource_present(resource)) { switch(resource) { case BatteryLevel: if(check_value_range(resource, value)) { device_id = DEVICE_BATTERY_LEVEL; } break; case BatteryStatus: if(check_value_range(resource, value)) { device_id = DEVICE_BATTERY_STATUS; } break; case MemoryFree: device_id = DEVICE_MEMORY_FREE; break; case MemoryTotal: device_id = DEVICE_MEMORY_TOTAL; break; case CurrentTime: device_id = DEVICE_CURRENT_TIME; operation = M2MBase::GET_PUT_ALLOWED; break; default: break; } } if(!device_id.empty()) { if(_device_instance) { res = _device_instance->create_dynamic_resource(device_id, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, true); if(res) { char *buffer = (char*)malloc(BUFFER_SIZE); if(buffer) { uint32_t size = m2m::itoa_c(value, buffer); if (size <= BUFFER_SIZE) { res->set_operation(operation); res->set_value((const uint8_t*)buffer, size); } free(buffer); } res->set_register_uri(false); } } } return res; }
bool M2MDevice::set_resource_value(DeviceResource resource, int64_t value, uint16_t instance_id) { bool success = false; M2MResourceInstance* res = get_resource_instance(resource,instance_id); if(res) { if(M2MDevice::BatteryLevel == resource || M2MDevice::BatteryStatus == resource || M2MDevice::MemoryFree == resource || M2MDevice::MemoryTotal == resource || M2MDevice::ErrorCode == resource || M2MDevice::CurrentTime == resource || M2MDevice::AvailablePowerSources == resource || M2MDevice::PowerSourceVoltage == resource || M2MDevice::PowerSourceCurrent == resource) { // If it is any of the above resource // set the value of the resource. if (check_value_range(resource, value)) { char *buffer = (char*)malloc(BUFFER_SIZE); if(buffer) { uint32_t size = m2m::itoa_c(value, buffer); if (size <= BUFFER_SIZE) success = res->set_value((const uint8_t*)buffer, size); free(buffer); } } } } return success; }
M2MResourceInstance* M2MDevice::create_resource_instance(DeviceResource resource, int64_t value, uint16_t instance_id) { M2MResourceInstance* res = NULL; String device_id = ""; // For these resources multiple instance can exist if(AvailablePowerSources == resource) { if(check_value_range(resource, value)) { device_id = DEVICE_AVAILABLE_POWER_SOURCES; } } else if(PowerSourceVoltage == resource) { device_id = DEVICE_POWER_SOURCE_VOLTAGE; } else if(PowerSourceCurrent == resource) { device_id = DEVICE_POWER_SOURCE_CURRENT; } else if(ErrorCode == resource) { if(check_value_range(resource, value)) { device_id = DEVICE_ERROR_CODE; } } if(!device_id.empty()) { if(_device_instance) { res = _device_instance->create_dynamic_resource_instance(device_id,OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, true, instance_id); M2MResource *resource = _device_instance->resource(device_id); if(resource) { resource->set_register_uri(false); } if(res) { char *buffer = (char*)malloc(BUFFER_SIZE); if(buffer) { uint32_t size = m2m::itoa_c(value, buffer); if (size <= BUFFER_SIZE) { res->set_value((const uint8_t*)buffer, size); // Only read operation is allowed for above resources res->set_operation(M2MBase::GET_ALLOWED); } free(buffer); } res->set_register_uri(false); } } } return res; }
bool M2MFirmware::set_resource_value(FirmwareResource resource, int64_t value) { bool success = false; M2MResource* res = get_resource(resource); if(res) { if(M2MFirmware::State == resource || M2MFirmware::UpdateSupportedObjects == resource || M2MFirmware::UpdateResult == resource) { // If it is any of the above resource // set the value of the resource. if (check_value_range(resource, value)) { success = res->set_value(value); } } } return success; }
M2MResource* M2MFirmware::create_resource(FirmwareResource resource, int64_t value) { M2MResource* res = NULL; const char* firmware_id_ptr = ""; M2MBase::Operation operation = M2MBase::GET_ALLOWED; if(!is_resource_present(resource)) { switch(resource) { case UpdateSupportedObjects: if(check_value_range(resource, value)) { firmware_id_ptr = FIRMWARE_UPDATE_SUPPORTED_OBJECTS; operation = M2MBase::GET_PUT_ALLOWED; } break; default: break; } } const String firmware_id(firmware_id_ptr); if(!firmware_id.empty()) { if(_firmware_instance) { res = _firmware_instance->create_dynamic_resource(firmware_id, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, false); if(res) { res->set_register_uri(false); res->set_operation(operation); res->set_value(value); } } } return res; }