Exemplo n.º 1
0
  public static byte[] encode(LwM2mNode node, LwM2mPath path, LwM2mModel model) {
    Validate.notNull(node);
    Validate.notNull(path);
    Validate.notNull(model);

    InternalEncoder internalEncoder = new InternalEncoder();
    internalEncoder.objectId = path.getObjectId();
    internalEncoder.model = model;
    node.accept(internalEncoder);
    return internalEncoder.out.toByteArray();
  }
Exemplo n.º 2
0
  public RegisterRequest(
      String endpointName,
      Long lifetime,
      String lwVersion,
      BindingMode bindingMode,
      String smsNumber,
      LinkObject[] objectLinks,
      Map<String, String> additionalAttributes) {

    Validate.notNull(endpointName);
    Validate.noNullElements(objectLinks);

    this.endpointName = endpointName;
    this.lifetime = lifetime;
    this.lwVersion = lwVersion;
    this.bindingMode = bindingMode;
    this.smsNumber = smsNumber;
    this.objectLinks = objectLinks;
    if (additionalAttributes == null)
      this.additionalAttributes = Collections.unmodifiableMap(new HashMap<String, String>());
    else this.additionalAttributes = Collections.unmodifiableMap(additionalAttributes);
  }
Exemplo n.º 3
0
  /**
   * Initialize a server which will bind to the specified address and port.
   *
   * @param localAddress the address to bind the CoAP server.
   * @param localAddressSecure the address to bind the CoAP server for DTLS connection.
   * @param privateKey for RPK authentication mode
   * @param publicKey for RPK authentication mode
   */
  public LeshanServer(
      InetSocketAddress localAddress,
      InetSocketAddress localAddressSecure,
      final ClientRegistry clientRegistry,
      final SecurityRegistry securityRegistry,
      final ObservationRegistry observationRegistry,
      final LwM2mModelProvider modelProvider) {
    Validate.notNull(localAddress, "IP address cannot be null");
    Validate.notNull(localAddressSecure, "Secure IP address cannot be null");
    Validate.notNull(clientRegistry, "clientRegistry cannot be null");
    Validate.notNull(securityRegistry, "securityRegistry cannot be null");
    Validate.notNull(observationRegistry, "observationRegistry cannot be null");
    Validate.notNull(modelProvider, "modelProvider cannot be null");

    // Init registries
    this.clientRegistry = clientRegistry;
    this.securityRegistry = securityRegistry;
    this.observationRegistry = observationRegistry;

    this.modelProvider = modelProvider;

    // Cancel observations on client unregistering
    this.clientRegistry.addListener(
        new ClientRegistryListener() {

          @Override
          public void updated(final Client clientUpdated) {}

          @Override
          public void unregistered(final Client client) {
            LeshanServer.this.observationRegistry.cancelObservations(client);
          }

          @Override
          public void registered(final Client client) {}
        });

    // default endpoint
    coapServer = new CoapServer();
    final Endpoint endpoint = new CoAPEndpoint(localAddress);
    coapServer.addEndpoint(endpoint);

    // secure endpoint
    DTLSConnector connector = new DTLSConnector(localAddressSecure);
    connector
        .getConfig()
        .setPskStore(new LwM2mPskStore(this.securityRegistry, this.clientRegistry));
    PrivateKey privateKey = this.securityRegistry.getServerPrivateKey();
    PublicKey publicKey = this.securityRegistry.getServerPublicKey();
    if (privateKey != null && publicKey != null) {
      connector.getConfig().setPrivateKey(privateKey, publicKey);
      // TODO this should be automatically done by scandium
      connector.getConfig().setPreferredCipherSuite(CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
    } else {
      // TODO this should be automatically done by scandium
      connector.getConfig().setPreferredCipherSuite(CipherSuite.TLS_PSK_WITH_AES_128_CCM_8);
    }

    final Endpoint secureEndpoint = new SecureEndpoint(connector);
    coapServer.addEndpoint(secureEndpoint);

    // define /rd resource
    final RegisterResource rdResource =
        new RegisterResource(new RegistrationHandler(this.clientRegistry, this.securityRegistry));
    coapServer.add(rdResource);

    // create sender
    final Set<Endpoint> endpoints = new HashSet<>();
    endpoints.add(endpoint);
    endpoints.add(secureEndpoint);
    requestSender =
        new CaliforniumLwM2mRequestSender(
            endpoints, this.clientRegistry, this.observationRegistry, modelProvider);
  }