int64_t M2MDevice::resource_value_int(DeviceResource resource, uint16_t instance_id) const { int64_t value = -1; 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) { // Get the value and convert it into integer uint8_t* buffer = NULL; uint32_t length = 0; res->get_value(buffer,length); if(buffer) { value = atoi((const char*)buffer); free(buffer); } } } return value; }
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; }
bool M2MDevice::set_resource_value(DeviceResource resource, const String &value, uint16_t instance_id) { bool success = false; M2MResourceInstance* res = get_resource_instance(resource,instance_id); if(res && value.size() <= MAX_ALLOWED_STRING_LENGTH) { if(M2MDevice::Manufacturer == resource || M2MDevice::ModelNumber == resource || M2MDevice::DeviceType == resource || M2MDevice::SerialNumber == resource || M2MDevice::HardwareVersion == resource || M2MDevice::FirmwareVersion == resource || M2MDevice::SoftwareVersion == resource || M2MDevice::UTCOffset == resource || M2MDevice::Timezone == resource) { if (value.empty()) { res->clear_value(); success = true; } else { success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length()); } } } return success; }
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. char *buffer = (char*)memory_alloc(20); if(buffer) { int size = snprintf(buffer, 20,"%lld",value); success = res->set_value((const uint8_t*)buffer, (const uint32_t)size); memory_free(buffer); } } } return success; }
bool M2MDevice::is_resource_present(DeviceResource resource) const { bool success = false; M2MResourceInstance* res = get_resource_instance(resource,0); if(res) { success = true; } return success; }
bool M2MDevice::set_resource_value(DeviceResource resource, const String &value, uint16_t instance_id) { bool success = false; M2MResourceInstance* res = get_resource_instance(resource,instance_id); if(res) { if(M2MDevice::Manufacturer == resource || M2MDevice::ModelNumber == resource || M2MDevice::DeviceType == resource || M2MDevice::SerialNumber == resource || M2MDevice::HardwareVersion == resource || M2MDevice::FirmwareVersion == resource || M2MDevice::SoftwareVersion == resource || M2MDevice::UTCOffset == resource || M2MDevice::Timezone == resource) { success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length()); } } return success; }
String M2MDevice::resource_value_string(DeviceResource resource, uint16_t instance_id) const { String value = ""; M2MResourceInstance* res = get_resource_instance(resource,instance_id); if(res) { if(M2MDevice::Manufacturer == resource || M2MDevice::ModelNumber == resource || M2MDevice::DeviceType == resource || M2MDevice::SerialNumber == resource || M2MDevice::HardwareVersion == resource || M2MDevice::FirmwareVersion == resource || M2MDevice::SoftwareVersion == resource || M2MDevice::UTCOffset == resource || M2MDevice::Timezone == resource) { uint8_t* buffer = NULL; uint32_t length = 0; res->get_value(buffer,length); char *char_buffer = (char*)malloc(length+1); if(char_buffer) { memset(char_buffer,0,length+1); memcpy(char_buffer,(char*)buffer,length); String s_name(char_buffer); value = s_name; if(char_buffer) { free(char_buffer); } } if(buffer) { free(buffer); } } } return value; }