void app_start(int, char**) { // setup the EthernetInterface eth.init(); // DHCP eth.connect(); // initialize the ipv4 tcp/ip stack lwipv4_socket_init(); // setup the M2MInterface srand(time(NULL)); uint16_t port = rand() % 65535 + 12345; srv = M2MInterfaceFactory::create_interface(observer,endpoint,type, lifetime,port,domain,M2MInterface::UDP,M2MInterface::LwIP_IPv4,context_address); // setup the Security object sec = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); sec->set_resource_value(M2MSecurity::M2MServerUri,address); sec->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); // setup the Device object dev = M2MInterfaceFactory::create_device(); dev->create_resource(M2MDevice::Manufacturer,"Freescale"); dev->create_resource(M2MDevice::DeviceType,"frdm-k64f"); dev->create_resource(M2MDevice::ModelNumber,"M64FN1MOVLL12"); dev->create_resource(M2MDevice::SerialNumber,"EB1524XXXX"); // setup the sensor objects obj = M2MInterfaceFactory::create_object("loc"); M2MObjectInstance* ins = obj->create_object_instance(); M2MResource* resx = ins->create_dynamic_resource("x","accel",M2MResourceInstance::INTEGER,true); resx->set_operation(M2MBase::GET_PUT_ALLOWED); resx->set_value((const uint8_t*)"0",1); M2MResource* resy = ins->create_dynamic_resource("y","accel",M2MResourceInstance::INTEGER,true); resy->set_operation(M2MBase::GET_PUT_ALLOWED); resy->set_value((const uint8_t*)"0",1); // Assemble the list of objects to register M2MObjectList list; list.push_back(dev); list.push_back(obj); // setup registration event Ticker timer; timer.attach(®,&Registrar::update,20); // enable accelerometer printf("Initializied accelerometer\r\n"); accel.enable(); Ticker sampler; sampler.attach(sample,5); // schedule FunctionPointer1<void, M2MObjectList> fp(®, &Registrar::setup); minar::Scheduler::postCallback(fp.bind(list)); minar::Scheduler::postCallback(sample).period(minar::milliseconds(10000)); minar::Scheduler::start(); }
bool M2MFirmware::check_value_range(FirmwareResource resource, int64_t value) const { bool success = false; switch (resource) { case UpdateSupportedObjects: if(value == 0 || value == 1) { success = true; } break; case State: if (value >= 0 && value <= 3) { success = true; M2MResource* updateRes = get_resource(M2MFirmware::Update); if (updateRes){ if (value == M2MFirmware::Downloaded) { updateRes->set_operation(M2MBase::POST_ALLOWED); } else { updateRes->set_operation(M2MBase::NOT_ALLOWED); } } } break; case UpdateResult: if (value >= 0 && value <= 7) { success = true; } break; default: break; } return success; }
M2MDevice::M2MDevice() : M2MObject(M2M_DEVICE_ID) { M2MBase::set_register_uri(false); M2MBase::set_operation(M2MBase::GET_ALLOWED); _device_instance = M2MObject::create_object_instance(); _device_instance->set_operation(M2MBase::GET_ALLOWED); _device_instance->set_register_uri(true); if(_device_instance) { _device_instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE); M2MResource* res = _device_instance->create_dynamic_resource(DEVICE_REBOOT, OMA_RESOURCE_TYPE, M2MResourceInstance::OPAQUE, false); if(res) { res->set_operation(M2MBase::POST_ALLOWED); res->set_register_uri(false); } M2MResourceInstance* instance = _device_instance->create_dynamic_resource_instance(DEVICE_ERROR_CODE, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, true,0); if(instance) { M2MResource * dev_res = _device_instance->resource(DEVICE_ERROR_CODE); dev_res->set_register_uri(false); instance->set_operation(M2MBase::GET_ALLOWED); uint32_t size = 0; char *buffer = (char*)malloc(BUFFER_SIZE); if(buffer) { size = m2m::itoa_c(0, buffer); if (size <= BUFFER_SIZE) instance->set_value((const uint8_t*)buffer, size); free(buffer); } instance->set_register_uri(false); } res = _device_instance->create_dynamic_resource(DEVICE_SUPPORTED_BINDING_MODE, OMA_RESOURCE_TYPE, M2MResourceInstance::STRING, true); if(res) { res->set_operation(M2MBase::GET_ALLOWED); res->set_value((const uint8_t*)BINDING_MODE_UDP.c_str(), (uint32_t)BINDING_MODE_UDP.length()); res->set_register_uri(false); } } }
void M2MFirmware::create_mandatory_resources() { _firmware_instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE); M2MResource* res = _firmware_instance->create_dynamic_resource(FIRMWARE_PACKAGE, OMA_RESOURCE_TYPE, M2MResourceInstance::OPAQUE, false); if(res) { res->set_operation(M2MBase::PUT_ALLOWED); res->set_register_uri(false); } res = _firmware_instance->create_dynamic_resource(FIRMWARE_PACKAGE_URI, OMA_RESOURCE_TYPE, M2MResourceInstance::STRING, false); if(res) { res->set_operation(M2MBase::PUT_ALLOWED); res->set_register_uri(false); } res = _firmware_instance->create_dynamic_resource(FIRMWARE_UPDATE, OMA_RESOURCE_TYPE, M2MResourceInstance::OPAQUE, false); if(res) { res->set_operation(M2MBase::NOT_ALLOWED); res->set_register_uri(false); } res = _firmware_instance->create_dynamic_resource(FIRMWARE_STATE, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, true); set_zero_value(res); if(res) { res->set_operation(M2MBase::GET_ALLOWED); res->set_register_uri(false); } res = _firmware_instance->create_dynamic_resource(FIRMWARE_UPDATE_RESULT, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, true); set_zero_value(res); if(res) { res->set_operation(M2MBase::GET_ALLOWED); res->set_register_uri(false); } }
bool M2MLWClient::create_dynamic_resource_int(const char *name, bool observable, bool multiple_instance, uint16_t object_instance, uint8_t resource_operation) { bool success = false; String name_string; if(name) { name_string += name; } if(_object) { M2MObjectInstance *inst = _object->object_instance(object_instance); if(inst) { M2MResource *res = inst->create_dynamic_resource(name,"resource", M2MResourceInstance::INTEGER, observable, multiple_instance); if(res) { success = true; res->set_operation(int_to_operation(resource_operation)); } } } return success; }
M2MResource* M2MDevice::create_resource(DeviceResource resource) { M2MResource* res = NULL; if(!is_resource_present(resource)) { String device_Id; if(FactoryReset == resource) { device_Id = DEVICE_FACTORY_RESET; } else if(ResetErrorCode == resource) { device_Id = DEVICE_RESET_ERROR_CODE; } if(_device_instance && !device_Id.empty()) { res = _device_instance->create_dynamic_resource(device_Id, OMA_RESOURCE_TYPE, M2MResourceInstance::OPAQUE, true); M2MResource *resource = _device_instance->resource(device_Id); if(resource) { resource->set_register_uri(false); } if(res) { res->set_operation(M2MBase::POST_ALLOWED); res->set_register_uri(false); } } } return res; }
M2MObject* create_led_object() { _sdw_led_object = M2MInterfaceFactory::create_object("311"); if (_sdw_led_object) { M2MObjectInstance* inst = _sdw_led_object->create_object_instance(); if (inst) { M2MResource* res = inst->create_dynamic_resource("5850", "LED", M2MResourceInstance::STRING, false); if (res) { char buffer[10] = ""; memset(buffer,0,10); strcpy(buffer,"0"); if (__led == 1) strcpy(buffer,"1"); // set the value of the LED res->set_operation(M2MBase::GET_PUT_ALLOWED); // we allow GET and PUT of the LED res->set_value((const uint8_t*)buffer, (const uint32_t)strlen(buffer)); } } } return _sdw_led_object; }
M2MObject* create_generic_object() { _object = M2MInterfaceFactory::create_object("Test"); if(_object) { M2MObjectInstance* inst = _object->create_object_instance(); if(inst) { M2MResource* res = inst->create_dynamic_resource("D", "ResourceTest", M2MResourceInstance::INTEGER, true); char buffer[20]; int size = sprintf(buffer,"%d",_value); res->set_operation(M2MBase::GET_PUT_ALLOWED); res->set_value((const uint8_t*)buffer, (const uint32_t)size); _value++; inst->create_static_resource("S", "ResourceTest", M2MResourceInstance::STRING, STATIC_VALUE, sizeof(STATIC_VALUE)-1); } } return _object; }
M2MServer::M2MServer() : M2MObject(M2M_SERVER_ID) { M2MObject::create_object_instance(); _server_instance = object_instance(); if(_server_instance) { M2MResource* res = _server_instance->create_dynamic_resource(SERVER_SHORT_SERVER_ID, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, true); if(res) { res->set_operation(M2MBase::GET_PUT_ALLOWED); } res = _server_instance->create_dynamic_resource(SERVER_LIFETIME, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, true); if(res) { res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); } res = _server_instance->create_dynamic_resource(SERVER_NOTIFICATION_STORAGE, OMA_RESOURCE_TYPE, M2MResourceInstance::BOOLEAN, true); if(res) { res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); } res = _server_instance->create_dynamic_resource(SERVER_BINDING, OMA_RESOURCE_TYPE, M2MResourceInstance::STRING, true); if(res) { res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); } res = _server_instance->create_dynamic_resource(SERVER_REGISTRATION_UPDATE, OMA_RESOURCE_TYPE, M2MResourceInstance::OPAQUE, false); if(res) { res->set_operation(M2MBase::POST_ALLOWED); } } }
M2MResource* M2MDevice::create_resource(DeviceResource resource, const String &value) { M2MResource* res = NULL; String device_id = ""; M2MBase::Operation operation = M2MBase::GET_ALLOWED; if(!is_resource_present(resource)) { switch(resource) { case Manufacturer: device_id = DEVICE_MANUFACTURER; break; case DeviceType: device_id = DEVICE_DEVICE_TYPE; break; case ModelNumber: device_id = DEVICE_MODEL_NUMBER; break; case SerialNumber: device_id = DEVICE_SERIAL_NUMBER; break; case HardwareVersion: device_id = DEVICE_HARDWARE_VERSION; break; case FirmwareVersion: device_id = DEVICE_FIRMWARE_VERSION; break; case SoftwareVersion: device_id = DEVICE_SOFTWARE_VERSION; break; case UTCOffset: device_id = DEVICE_UTC_OFFSET; operation = M2MBase::GET_PUT_ALLOWED; break; case Timezone: device_id = DEVICE_TIMEZONE; 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::STRING, false); if(res ) { res->set_operation(operation); res->set_value((const uint8_t*)value.c_str(), (uint32_t)value.length()); } } } return res; }
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; }
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(0 <= value && value <= 100) { device_id = DEVICE_BATTERY_LEVEL; } break; case BatteryStatus: if(0 <= value && value <= 6) { 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, false); if(res) { char *buffer = (char*)memory_alloc(20); if(buffer) { int size = snprintf(buffer, 20,"%ld",value); res->set_operation(operation); res->set_value((const uint8_t*)buffer, (const uint32_t)size); memory_free(buffer); } } } } return res; }
M2MDevice::M2MDevice() : M2MObject(M2M_DEVICE_ID) { _device_instance = M2MObject::create_object_instance(); if(_device_instance) { _device_instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE); M2MResource* res = _device_instance->create_dynamic_resource(DEVICE_REBOOT, OMA_RESOURCE_TYPE, M2MResourceInstance::OPAQUE, false); if(res) { res->set_operation(M2MBase::POST_ALLOWED); } M2MResourceInstance* instance = _device_instance->create_dynamic_resource_instance(DEVICE_ERROR_CODE, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, false,0); if(instance) { instance->set_operation(M2MBase::GET_ALLOWED); instance->set_value((const uint8_t*)ERROR_CODE_VALUE.c_str(), (uint32_t)ERROR_CODE_VALUE.length()); } res = _device_instance->create_dynamic_resource(DEVICE_SUPPORTED_BINDING_MODE, OMA_RESOURCE_TYPE, M2MResourceInstance::STRING, false); if(res) { res->set_operation(M2MBase::GET_ALLOWED); res->set_value((const uint8_t*)BINDING_MODE_UDP.c_str(), (uint32_t)BINDING_MODE_UDP.length()); } } }
M2MResource* M2MServer::create_resource(ServerResource resource) { M2MResource* res = NULL; if(!is_resource_present(resource)) { if(M2MServer::Disable == resource) { if(_server_instance) { res = _server_instance->create_dynamic_resource(SERVER_DISABLE, OMA_RESOURCE_TYPE, M2MResourceInstance::OPAQUE, false); if(res) { res->set_operation(M2MBase::POST_ALLOWED); } } } } return res; }
M2MResource* M2MSecurity::create_resource(SecurityResource resource, uint32_t value) { M2MResource* res = NULL; String security_id = ""; if(!is_resource_present(resource)) { switch(resource) { case SMSSecurityMode: security_id = SECURITY_SMS_SECURITY_MODE; break; case M2MServerSMSNumber: security_id = SECURITY_M2M_SERVER_SMS_NUMBER; break; case ShortServerID: security_id = SECURITY_SHORT_SERVER_ID; break; case ClientHoldOffTime: security_id = SECURITY_CLIENT_HOLD_OFF_TIME; break; default: break; } } if(!security_id.empty()) { if(_server_instance) { res = _server_instance->create_dynamic_resource(security_id,OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, false); if(res) { res->set_operation(M2MBase::NOT_ALLOWED); 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); } free(buffer); } } } } return res; }
M2MResource* M2MFirmware::create_resource(FirmwareResource resource, const String &value) { M2MResource* res = NULL; const char* firmware_id_ptr = ""; M2MBase::Operation operation = M2MBase::GET_ALLOWED; if(!is_resource_present(resource)) { switch(resource) { case PackageName: firmware_id_ptr = FIRMWARE_PACKAGE_NAME; break; case PackageVersion: firmware_id_ptr = FIRMWARE_PACKAGE_VERSION; break; default: break; } } String firmware_id(firmware_id_ptr); if(!firmware_id.empty() && value.size() < 256) { if(_firmware_instance) { res = _firmware_instance->create_dynamic_resource(firmware_id, OMA_RESOURCE_TYPE, M2MResourceInstance::STRING, false); if(res) { res->set_register_uri(false); res->set_operation(operation); if(value.empty()) { res->clear_value(); } else { res->set_value((const uint8_t*)value.c_str(), (uint32_t)value.length()); } } } } return res; }
M2MResource* M2MServer::create_resource(ServerResource resource, uint32_t value) { M2MResource* res = NULL; String server_id = ""; if(!is_resource_present(resource)) { switch(resource) { case DefaultMinPeriod: server_id = SERVER_DEFAULT_MIN_PERIOD; break; case DefaultMaxPeriod: server_id = SERVER_DEFAULT_MAX_PERIOD; break; case DisableTimeout: server_id = SERVER_DISABLE_TIMEOUT; break; default: break; } } if(!server_id.empty()) { if(_server_instance) { res = _server_instance->create_dynamic_resource(server_id, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, true); if(res) { res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); // If resource is created then set the value. char *buffer = (char*)malloc(20); if(buffer) { int size = snprintf(buffer, 20,"%ld",(long int)value); res->set_value((const uint8_t*)buffer,(const uint32_t)size); free(buffer); } } } } return res; }
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; }
M2MSecurity::M2MSecurity(ServerType ser_type) : M2MObject(M2M_SECURITY_ID), _server_type(ser_type), _server_instance(NULL) { _server_instance = M2MObject::create_object_instance(); if(_server_instance) { M2MResource* res = _server_instance->create_dynamic_resource(SECURITY_M2M_SERVER_URI, OMA_RESOURCE_TYPE, M2MResourceInstance::STRING, false); if(res) { res->set_operation(M2MBase::NOT_ALLOWED); } res = _server_instance->create_dynamic_resource(SECURITY_BOOTSTRAP_SERVER, OMA_RESOURCE_TYPE, M2MResourceInstance::BOOLEAN, false); if(res) { res->set_operation(M2MBase::NOT_ALLOWED); } res = _server_instance->create_dynamic_resource(SECURITY_SECURITY_MODE, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, false); if(res) { res->set_operation(M2MBase::NOT_ALLOWED); } res = _server_instance->create_dynamic_resource(SECURITY_PUBLIC_KEY, OMA_RESOURCE_TYPE, M2MResourceInstance::OPAQUE, false); if(res) { res->set_operation(M2MBase::NOT_ALLOWED); } res = _server_instance->create_dynamic_resource(SECURITY_SERVER_PUBLIC_KEY, OMA_RESOURCE_TYPE, M2MResourceInstance::OPAQUE, false); if(res) { res->set_operation(M2MBase::NOT_ALLOWED); } res = _server_instance->create_dynamic_resource(SECURITY_SECRET_KEY, OMA_RESOURCE_TYPE, M2MResourceInstance::OPAQUE, false); if(res) { res->set_operation(M2MBase::NOT_ALLOWED); } if(M2MSecurity::M2MServer == ser_type) { res = _server_instance->create_dynamic_resource(SECURITY_SHORT_SERVER_ID, OMA_RESOURCE_TYPE, M2MResourceInstance::INTEGER, false); if(res) { res->set_operation(M2MBase::NOT_ALLOWED); } } } }