Search in sources :

Example 1 with DefaultLwM2mEncoder

use of org.eclipse.leshan.core.node.codec.DefaultLwM2mEncoder in project thingsboard by thingsboard.

the class LwM2MTestClient method init.

public void init(Security security, Configuration coapConfig, int port, boolean isRpc, boolean isBootstrap, int shortServerId, int shortServerIdBs, Security securityBs, DefaultLwM2mUplinkMsgHandler defaultLwM2mUplinkMsgHandler, LwM2mClientContext clientContext) throws InvalidDDFFileException, IOException {
    Assert.assertNull("client already initialized", leshanClient);
    this.defaultLwM2mUplinkMsgHandlerTest = defaultLwM2mUplinkMsgHandler;
    this.clientContext = clientContext;
    List<ObjectModel> models = new ArrayList<>();
    for (String resourceName : resources) {
        models.addAll(ObjectLoader.loadDdfFile(LwM2MTestClient.class.getClassLoader().getResourceAsStream("lwm2m/" + resourceName), resourceName));
    }
    LwM2mModel model = new StaticModel(models);
    ObjectsInitializer initializer = new ObjectsInitializer(model);
    if (securityBs == null) {
        initializer.setInstancesForObject(SECURITY, this.lwm2mSecurity = security);
    } else {
        securityBs.setId(0);
        security.setId(1);
        LwM2mInstanceEnabler[] instances = new LwM2mInstanceEnabler[] { this.lwm2mSecurityBs = securityBs, this.lwm2mSecurity = security };
        initializer.setClassForObject(SECURITY, Security.class);
        initializer.setInstancesForObject(SECURITY, instances);
    }
    if (isBootstrap) {
        initializer.setInstancesForObject(SERVER, lwm2mServerBs = new Lwm2mServer(shortServerIdBs, 300));
    } else {
        if (securityBs == null) {
            initializer.setInstancesForObject(SERVER, lwm2mServer = new Lwm2mServer(shortServerId, 300));
        } else {
            lwm2mServerBs = new Lwm2mServer(shortServerIdBs, 300);
            lwm2mServerBs.setId(0);
            lwm2mServer = new Lwm2mServer(shortServerId, 300);
            lwm2mServer.setId(1);
            LwM2mInstanceEnabler[] instances = new LwM2mInstanceEnabler[] { lwm2mServerBs, lwm2mServer };
            initializer.setClassForObject(SERVER, Server.class);
            initializer.setInstancesForObject(SERVER, instances);
        }
    }
    initializer.setInstancesForObject(DEVICE, lwM2MDevice = new SimpleLwM2MDevice(executor));
    initializer.setInstancesForObject(FIRMWARE, fwLwM2MDevice = new FwLwM2MDevice());
    initializer.setInstancesForObject(SOFTWARE_MANAGEMENT, swLwM2MDevice = new SwLwM2MDevice());
    initializer.setClassForObject(ACCESS_CONTROL, DummyInstanceEnabler.class);
    initializer.setInstancesForObject(BINARY_APP_DATA_CONTAINER, lwM2MBinaryAppDataContainer = new LwM2mBinaryAppDataContainer(executor, OBJECT_INSTANCE_ID_0), new LwM2mBinaryAppDataContainer(executor, OBJECT_INSTANCE_ID_1));
    locationParams = new LwM2MLocationParams();
    locationParams.getPos();
    initializer.setInstancesForObject(LOCATION, new LwM2mLocation(locationParams.getLatitude(), locationParams.getLongitude(), locationParams.getScaleFactor(), executor, OBJECT_INSTANCE_ID_0));
    initializer.setInstancesForObject(TEMPERATURE_SENSOR, lwM2MTemperatureSensor = new LwM2mTemperatureSensor(executor, OBJECT_INSTANCE_ID_0), new LwM2mTemperatureSensor(executor, OBJECT_INSTANCE_ID_12));
    DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder(coapConfig);
    dtlsConfig.set(DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
    DefaultRegistrationEngineFactory engineFactory = new DefaultRegistrationEngineFactory();
    engineFactory.setReconnectOnUpdate(false);
    engineFactory.setResumeOnConnect(true);
    LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
    builder.setLocalAddress("0.0.0.0", port);
    builder.setObjects(initializer.createAll());
    builder.setCoapConfig(coapConfig);
    builder.setDtlsConfig(dtlsConfig);
    builder.setRegistrationEngineFactory(engineFactory);
    builder.setSharedExecutor(executor);
    builder.setDecoder(new DefaultLwM2mDecoder(false));
    builder.setEncoder(new DefaultLwM2mEncoder(new LwM2mValueConverterImpl(), false));
    clientState = ON_INIT;
    clientStates = new HashSet<>();
    clientStates.add(clientState);
    leshanClient = builder.build();
    LwM2mClientObserver observer = new LwM2mClientObserver() {

        @Override
        public void onBootstrapStarted(ServerIdentity bsserver, BootstrapRequest request) {
            clientState = ON_BOOTSTRAP_STARTED;
            clientStates.add(clientState);
        }

        @Override
        public void onBootstrapSuccess(ServerIdentity bsserver, BootstrapRequest request) {
            clientState = ON_BOOTSTRAP_SUCCESS;
            clientStates.add(clientState);
        }

        @Override
        public void onBootstrapFailure(ServerIdentity bsserver, BootstrapRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
            clientState = ON_BOOTSTRAP_FAILURE;
            clientStates.add(clientState);
        }

        @Override
        public void onBootstrapTimeout(ServerIdentity bsserver, BootstrapRequest request) {
            clientState = ON_BOOTSTRAP_TIMEOUT;
            clientStates.add(clientState);
        }

        @Override
        public void onRegistrationStarted(ServerIdentity server, RegisterRequest request) {
            clientState = ON_REGISTRATION_STARTED;
            clientStates.add(clientState);
        }

        @Override
        public void onRegistrationSuccess(ServerIdentity server, RegisterRequest request, String registrationID) {
            clientState = ON_REGISTRATION_SUCCESS;
            clientStates.add(clientState);
        }

        @Override
        public void onRegistrationFailure(ServerIdentity server, RegisterRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
            clientState = ON_REGISTRATION_FAILURE;
            clientStates.add(clientState);
        }

        @Override
        public void onRegistrationTimeout(ServerIdentity server, RegisterRequest request) {
            clientState = ON_REGISTRATION_TIMEOUT;
            clientStates.add(clientState);
        }

        @Override
        public void onUpdateStarted(ServerIdentity server, UpdateRequest request) {
            clientState = ON_UPDATE_STARTED;
            clientStates.add(clientState);
        }

        @Override
        public void onUpdateSuccess(ServerIdentity server, UpdateRequest request) {
            clientState = ON_UPDATE_SUCCESS;
            clientStates.add(clientState);
        }

        @Override
        public void onUpdateFailure(ServerIdentity server, UpdateRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
            clientState = ON_UPDATE_FAILURE;
            clientStates.add(clientState);
        }

        @Override
        public void onUpdateTimeout(ServerIdentity server, UpdateRequest request) {
            clientState = ON_UPDATE_TIMEOUT;
            clientStates.add(clientState);
        }

        @Override
        public void onDeregistrationStarted(ServerIdentity server, DeregisterRequest request) {
            clientState = ON_DEREGISTRATION_STARTED;
            clientStates.add(clientState);
        }

        @Override
        public void onDeregistrationSuccess(ServerIdentity server, DeregisterRequest request) {
            clientState = ON_DEREGISTRATION_SUCCESS;
            clientStates.add(clientState);
        }

        @Override
        public void onDeregistrationFailure(ServerIdentity server, DeregisterRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
            clientState = ON_DEREGISTRATION_FAILURE;
            clientStates.add(clientState);
        }

        @Override
        public void onDeregistrationTimeout(ServerIdentity server, DeregisterRequest request) {
            clientState = ON_DEREGISTRATION_TIMEOUT;
            clientStates.add(clientState);
        }

        @Override
        public void onUnexpectedError(Throwable unexpectedError) {
            clientState = ON_EXPECTED_ERROR;
            clientStates.add(clientState);
        }
    };
    this.leshanClient.addObserver(observer);
    if (!isRpc) {
        this.start(true);
    }
}
Also used : BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) LwM2mValueConverterImpl(org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl) DefaultLwM2mEncoder(org.eclipse.leshan.core.node.codec.DefaultLwM2mEncoder) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) ArrayList(java.util.ArrayList) StaticModel(org.eclipse.leshan.core.model.StaticModel) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) LwM2mInstanceEnabler(org.eclipse.leshan.client.resource.LwM2mInstanceEnabler) ServerIdentity(org.eclipse.leshan.client.servers.ServerIdentity) LwM2mClientObserver(org.eclipse.leshan.client.observer.LwM2mClientObserver) RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) DefaultRegistrationEngineFactory(org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory) ResponseCode(org.eclipse.leshan.core.ResponseCode) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) UpdateRequest(org.eclipse.leshan.core.request.UpdateRequest) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) DefaultLwM2mDecoder(org.eclipse.leshan.core.node.codec.DefaultLwM2mDecoder) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest) InvalidDDFFileException(org.eclipse.leshan.core.model.InvalidDDFFileException) IOException(java.io.IOException)

Example 2 with DefaultLwM2mEncoder

use of org.eclipse.leshan.core.node.codec.DefaultLwM2mEncoder in project thingsboard by thingsboard.

the class DefaultLwM2mTransportService method getLhServer.

private LeshanServer getLhServer() {
    LeshanServerBuilder builder = new LeshanServerBuilder();
    builder.setLocalAddress(config.getHost(), config.getPort());
    builder.setLocalSecureAddress(config.getSecureHost(), config.getSecurePort());
    builder.setDecoder(new DefaultLwM2mDecoder());
    /* Use a magic converter to support bad type send by the UI. */
    builder.setEncoder(new DefaultLwM2mEncoder(LwM2mValueConverterImpl.getInstance()));
    /* Create CoAP Config */
    builder.setCoapConfig(getCoapConfig(config.getPort(), config.getSecurePort(), config));
    /* Define model provider (Create Models )*/
    builder.setObjectModelProvider(modelProvider);
    /* Set securityStore with new registrationStore */
    builder.setSecurityStore(securityStore);
    builder.setRegistrationStore(registrationStore);
    /* Create DTLS Config */
    DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder(getCoapConfig(config.getPort(), config.getSecurePort(), config));
    dtlsConfig.set(DtlsConfig.DTLS_ROLE, DtlsConfig.DtlsRole.SERVER_ONLY);
    dtlsConfig.set(DTLS_RECOMMENDED_CURVES_ONLY, config.isRecommendedSupportedGroups());
    dtlsConfig.set(DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, config.isRecommendedCiphers());
    /*  Create credentials */
    this.setServerWithCredentials(builder, dtlsConfig);
    /* Set DTLS Config */
    builder.setDtlsConfig(dtlsConfig);
    /* Create LWM2M server */
    return builder.build();
}
Also used : LeshanServerBuilder(org.eclipse.leshan.server.californium.LeshanServerBuilder) DefaultLwM2mEncoder(org.eclipse.leshan.core.node.codec.DefaultLwM2mEncoder) LeshanServerBuilder(org.eclipse.leshan.server.californium.LeshanServerBuilder) DefaultLwM2mDecoder(org.eclipse.leshan.core.node.codec.DefaultLwM2mDecoder) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig)

Aggregations

DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)2 DefaultLwM2mDecoder (org.eclipse.leshan.core.node.codec.DefaultLwM2mDecoder)2 DefaultLwM2mEncoder (org.eclipse.leshan.core.node.codec.DefaultLwM2mEncoder)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LeshanClientBuilder (org.eclipse.leshan.client.californium.LeshanClientBuilder)1 DefaultRegistrationEngineFactory (org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory)1 LwM2mClientObserver (org.eclipse.leshan.client.observer.LwM2mClientObserver)1 LwM2mInstanceEnabler (org.eclipse.leshan.client.resource.LwM2mInstanceEnabler)1 ObjectsInitializer (org.eclipse.leshan.client.resource.ObjectsInitializer)1 ServerIdentity (org.eclipse.leshan.client.servers.ServerIdentity)1 ResponseCode (org.eclipse.leshan.core.ResponseCode)1 InvalidDDFFileException (org.eclipse.leshan.core.model.InvalidDDFFileException)1 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)1 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)1 StaticModel (org.eclipse.leshan.core.model.StaticModel)1 BootstrapRequest (org.eclipse.leshan.core.request.BootstrapRequest)1 DeregisterRequest (org.eclipse.leshan.core.request.DeregisterRequest)1 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)1 UpdateRequest (org.eclipse.leshan.core.request.UpdateRequest)1