Search in sources :

Example 16 with LwM2mModel

use of org.eclipse.leshan.core.model.LwM2mModel in project leshan by eclipse.

the class ObserveTest method can_observe_timestamped_object.

@Test
public void can_observe_timestamped_object() throws InterruptedException {
    TestObservationListener listener = new TestObservationListener();
    helper.server.getObservationService().addListener(listener);
    // observe device timezone
    ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3));
    assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
    assertNotNull(observeResponse.getCoapResponse());
    assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
    // an observation response should have been sent
    Observation observation = observeResponse.getObservation();
    assertEquals("/3", observation.getPath().toString());
    assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
    Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
    assertTrue("We should have only on observation", observations.size() == 1);
    assertTrue("New observation is not there", observations.contains(observation));
    // *** HACK send time-stamped notification as Leshan client does not support it *** //
    // create time-stamped nodes
    TimestampedLwM2mNode mostRecentNode = new TimestampedLwM2mNode(System.currentTimeMillis(), new LwM2mObject(3, new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Paris"))));
    List<TimestampedLwM2mNode> timestampedNodes = new ArrayList<>();
    timestampedNodes.add(mostRecentNode);
    timestampedNodes.add(new TimestampedLwM2mNode(mostRecentNode.getTimestamp() - 2, new LwM2mObject(3, new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Londres")))));
    byte[] payload = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, new LwM2mPath("/3"), new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
    Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
    sendNotification(getConnector(helper.client), payload, firstCoapResponse, ContentFormat.JSON_CODE);
    // *** Hack End *** //
    // verify result
    listener.waitForNotification(2000);
    assertTrue(listener.receivedNotify().get());
    assertEquals(mostRecentNode.getNode(), listener.getResponse().getContent());
    assertEquals(timestampedNodes, listener.getResponse().getTimestampedLwM2mNode());
    assertNotNull(listener.getResponse().getCoapResponse());
    assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : ArrayList(java.util.ArrayList) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) DefaultLwM2mValueConverter(org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Test(org.junit.Test)

Example 17 with LwM2mModel

use of org.eclipse.leshan.core.model.LwM2mModel in project leshan by eclipse.

the class LeshanBootstrapServerBuilder method build.

public LeshanBootstrapServer build() {
    if (localAddress == null)
        localAddress = new InetSocketAddress(LwM2m.DEFAULT_COAP_PORT);
    // TODO we should have default implementation for BootstrapStore in leshan.server project.
    if (configStore == null)
        throw new IllegalStateException("BootstrapStore is mandatory");
    if (sessionManager == null)
        sessionManager = new DefaultBootstrapSessionManager(securityStore);
    if (model == null)
        model = new LwM2mModel(ObjectLoader.loadDefault());
    if (coapConfig == null) {
        coapConfig = createDefaultNetworkConfig();
    }
    // handle dtlsConfig
    DtlsConnectorConfig dtlsConfig = null;
    if (!noSecuredEndpoint) {
        if (dtlsConfigBuilder == null) {
            dtlsConfigBuilder = new DtlsConnectorConfig.Builder();
        }
        DtlsConnectorConfig incompleteConfig = dtlsConfigBuilder.getIncompleteConfig();
        // Handle PSK Store
        if (incompleteConfig.getPskStore() == null && securityStore != null) {
            dtlsConfigBuilder.setPskStore(new LwM2mBootstrapPskStore(securityStore));
        } else {
            LOG.warn("PskStore should be automatically set by Leshan. Using a custom implementation is not advised.");
        }
        // Handle secure address
        if (incompleteConfig.getAddress() == null) {
            if (localAddressSecure == null) {
                localAddressSecure = new InetSocketAddress(0);
            }
            dtlsConfigBuilder.setAddress(localAddressSecure);
        } else if (localAddressSecure != null && !localAddressSecure.equals(incompleteConfig.getAddress())) {
            throw new IllegalStateException(String.format("Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder for secure address: %s != %s", localAddressSecure, incompleteConfig.getAddress()));
        }
        // Handle active peers
        if (incompleteConfig.getMaxConnections() == null)
            dtlsConfigBuilder.setMaxConnections(coapConfig.getInt(Keys.MAX_ACTIVE_PEERS));
        if (incompleteConfig.getStaleConnectionThreshold() == null)
            dtlsConfigBuilder.setStaleConnectionThreshold(coapConfig.getLong(Keys.MAX_PEER_INACTIVITY_PERIOD));
        // we try to build the dtlsConfig, if it fail we will just not create the secured endpoint
        try {
            dtlsConfig = dtlsConfigBuilder.build();
        } catch (IllegalStateException e) {
        }
    }
    CoapEndpoint unsecuredEndpoint = null;
    if (!noUnsecuredEndpoint) {
        if (endpointFactory != null) {
            unsecuredEndpoint = endpointFactory.createUnsecuredEndpoint(localAddress, coapConfig, null);
        } else {
            CoapEndpoint.CoapEndpointBuilder builder = new CoapEndpoint.CoapEndpointBuilder();
            builder.setInetSocketAddress(localAddress);
            builder.setNetworkConfig(coapConfig);
            unsecuredEndpoint = builder.build();
        }
    }
    CoapEndpoint securedEndpoint = null;
    if (!noSecuredEndpoint && dtlsConfig != null) {
        if (endpointFactory != null) {
            securedEndpoint = endpointFactory.createSecuredEndpoint(dtlsConfig, coapConfig, null);
        } else {
            CoapEndpoint.CoapEndpointBuilder builder = new CoapEndpoint.CoapEndpointBuilder();
            builder.setConnector(new DTLSConnector(dtlsConfig));
            builder.setNetworkConfig(coapConfig);
            builder.setEndpointContextMatcher(new Lwm2mEndpointContextMatcher());
            securedEndpoint = builder.build();
        }
    }
    if (securedEndpoint == null && unsecuredEndpoint == null) {
        throw new IllegalStateException("All CoAP enpoints are deactivated, at least one endpoint should be activated");
    }
    return new LeshanBootstrapServer(unsecuredEndpoint, securedEndpoint, configStore, securityStore, sessionManager, model, coapConfig);
}
Also used : LeshanBootstrapServer(org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer) InetSocketAddress(java.net.InetSocketAddress) LwM2mBootstrapPskStore(org.eclipse.leshan.server.californium.impl.LwM2mBootstrapPskStore) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) Builder(org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) DefaultBootstrapSessionManager(org.eclipse.leshan.server.impl.DefaultBootstrapSessionManager) Lwm2mEndpointContextMatcher(org.eclipse.leshan.core.californium.Lwm2mEndpointContextMatcher) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint)

Example 18 with LwM2mModel

use of org.eclipse.leshan.core.model.LwM2mModel in project leshan by eclipse.

the class CaliforniumLwM2mRequestSender method send.

@Override
public <T extends LwM2mResponse> T send(final Registration destination, final DownlinkRequest<T> request, long timeout) throws InterruptedException {
    // Retrieve the objects definition
    final LwM2mModel model = modelProvider.getObjectModel(destination);
    // Create the CoAP request from LwM2m request
    CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination.getIdentity(), destination.getRootPath(), destination.getId(), destination.getEndpoint(), model, encoder);
    request.accept(coapRequestBuilder);
    final Request coapRequest = coapRequestBuilder.getRequest();
    // Send CoAP request synchronously
    SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {

        @Override
        public T buildResponse(Response coapResponse) {
            // Build LwM2m response
            LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse, destination, model, observationService, decoder);
            request.accept(lwm2mResponseBuilder);
            return lwm2mResponseBuilder.getResponse();
        }
    };
    coapRequest.addMessageObserver(syncMessageObserver);
    // Store pending request to cancel it on de-registration
    addPendingRequest(destination.getId(), coapRequest);
    // Send CoAP request asynchronously
    Endpoint endpoint = getEndpointForClient(destination);
    endpoint.sendRequest(coapRequest);
    // Wait for response, then return it
    return syncMessageObserver.waitForResponse();
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) Endpoint(org.eclipse.californium.core.network.Endpoint) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) SyncRequestObserver(org.eclipse.leshan.core.californium.SyncRequestObserver) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel)

Example 19 with LwM2mModel

use of org.eclipse.leshan.core.model.LwM2mModel in project leshan by eclipse.

the class ObjectSpecServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (req.getPathInfo() == null) {
        LwM2mModel model = modelProvider.getObjectModel(null);
        resp.setContentType("application/json");
        resp.getOutputStream().write(serializer.bSerialize(model.getObjectModels()));
        resp.setStatus(HttpServletResponse.SC_OK);
        return;
    }
}
Also used : LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel)

Aggregations

LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)19 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)7 Response (org.eclipse.californium.core.coap.Response)6 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)6 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)6 Observation (org.eclipse.leshan.core.observation.Observation)5 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)5 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)5 ArrayList (java.util.ArrayList)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4 LeshanClientBuilder (org.eclipse.leshan.client.californium.LeshanClientBuilder)3 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)3 Server (org.eclipse.leshan.client.object.Server)3 ServerIdentity (org.eclipse.leshan.client.request.ServerIdentity)3 LwM2mObjectEnabler (org.eclipse.leshan.client.resource.LwM2mObjectEnabler)3 ObjectsInitializer (org.eclipse.leshan.client.resource.ObjectsInitializer)3 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)3 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)3 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3