void Test_M2MResource::test_copy_constructor() { u_int8_t value[] = {"value"}; resource->set_value(value,(u_int32_t)sizeof(value)); M2MResourceInstance *res = new M2MResourceInstance("name","type",M2MResourceInstance::STRING,*callback); resource->add_resource_instance(res); M2MResource* copy = new M2MResource(*resource); u_int8_t* out_value = (u_int8_t*)malloc(sizeof(u_int8_t)); u_int32_t out_size; uint8_t* ptr = (uint8_t*)malloc((uint32_t)sizeof(value)); m2mresourceinstance_stub::value = ptr; memset(m2mresourceinstance_stub::value,0,(uint32_t)sizeof(value)); memcpy(m2mresourceinstance_stub::value,value,sizeof(value)); m2mresourceinstance_stub::int_value = (uint32_t)sizeof(value); copy->get_value(out_value,out_size); CHECK(out_size == sizeof(value)); free(out_value); free(ptr); delete copy; }
String M2MSecurity::resource_value_string(SecurityResource resource) const { String value = ""; M2MResource* res = get_resource(resource); if(res) { if(M2MSecurity::M2MServerUri == 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); if(buffer) { memcpy(char_buffer,(char*)buffer,length); } String s_name(char_buffer); value = s_name; free(char_buffer); } if(buffer) { free(buffer); } } } return value; }
String M2MServer::resource_value_string(ServerResource resource) const { String value = ""; M2MResource* res = get_resource(resource); if(res && (M2MServer::Binding == 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; }
uint32_t M2MFirmware::resource_value_buffer(FirmwareResource resource, uint8_t *&data) const { uint32_t size = 0; M2MResource* res = get_resource(resource); if(res) { if(M2MFirmware::Package == resource) { res->get_value(data,size); } } return size; }
uint32_t M2MSecurity::resource_value_buffer(SecurityResource resource, uint8_t *&data) const { uint32_t size = 0; M2MResource* res = get_resource(resource); if(res) { if(M2MSecurity::PublicKey == resource || M2MSecurity::ServerPublicKey == resource || M2MSecurity::Secretkey == resource) { res->get_value(data,size); } } return size; }
void blink(void *argument) { // read the value of 'Pattern' status_ticker.detach(); green_led = LED_OFF; M2MObjectInstance* inst = led_object->object_instance(); M2MResource* res = inst->resource("5853"); // Clear previous blink data blink_args->clear(); // values in mbed Client are all buffers, and we need a vector of int's uint8_t* buffIn = NULL; uint32_t sizeIn; res->get_value(buffIn, sizeIn); // turn the buffer into a string, and initialize a vector<int> on the heap std::string s((char*)buffIn, sizeIn); free(buffIn); printf("led_execute_callback pattern=%s\n", s.c_str()); // our pattern is something like 500:200:500, so parse that std::size_t found = s.find_first_of(":"); while (found!=std::string::npos) { blink_args->blink_pattern.push_back(atoi((const char*)s.substr(0,found).c_str())); s = s.substr(found+1); found=s.find_first_of(":"); if(found == std::string::npos) { blink_args->blink_pattern.push_back(atoi((const char*)s.c_str())); } } // check if POST contains payload if (argument) { M2MResource::M2MExecuteParameter* param = (M2MResource::M2MExecuteParameter*)argument; String object_name = param->get_argument_object_name(); uint16_t object_instance_id = param->get_argument_object_instance_id(); String resource_name = param->get_argument_resource_name(); int payload_length = param->get_argument_value_length(); uint8_t* payload = param->get_argument_value(); printf("Resource: %s/%d/%s executed\n", object_name.c_str(), object_instance_id, resource_name.c_str()); printf("Payload: %.*s\n", payload_length, payload); } // do_blink is called with the vector, and starting at -1 blinky_thread.start(callback(this, &LedResource::do_blink)); }
uint32_t M2MSecurity::resource_value_int(SecurityResource resource) const { uint32_t value = 0; M2MResource* res = get_resource(resource); if(res) { if(M2MSecurity::SecurityMode == resource || M2MSecurity::SMSSecurityMode == resource || M2MSecurity::M2MServerSMSNumber == resource || M2MSecurity::ShortServerID == resource || M2MSecurity::ClientHoldOffTime == 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; }
uint32_t M2MServer::resource_value_int(ServerResource resource) const { uint32_t value = 0; M2MResource* res = get_resource(resource); if(res) { if(M2MServer::ShortServerID == resource || M2MServer::Lifetime == resource || M2MServer::DefaultMinPeriod == resource || M2MServer::DefaultMaxPeriod == resource || M2MServer::DisableTimeout == resource || M2MServer::NotificationStorage == 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; }