Example #1
0
 private void Register(Lwm2mServerReference Server, string Links, bool Update)
 {
     if (Update && !string.IsNullOrEmpty(Server.LocationPath) &&
         Server.LocationPath.StartsWith("/"))
     {
         if (Links != this.lastLinks)
         {
             this.coapEndpoint.POST(Server.Uri + Server.LocationPath.Substring(1),
                                    true, Encoding.UTF8.GetBytes(Links), 64, Server.Credentials,
                                    this.RegisterResponse, new object[] { Server, true },
                                    new CoapOptionContentFormat(CoreLinkFormat.ContentFormatCode));
         }
         else
         {
             this.coapEndpoint.POST(Server.Uri + Server.LocationPath.Substring(1),
                                    true, null, 64, Server.Credentials, this.RegisterResponse,
                                    new object[] { Server, true });
         }
     }
     else
     {
         this.coapEndpoint.POST(Server.Uri + "rd?ep=" + this.clientName +
                                "&lt=" + this.lifetimeSeconds.ToString() + "&lwm2m=1.0", true,
                                Encoding.UTF8.GetBytes(Links), 64, Server.Credentials,
                                this.RegisterResponse, new object[] { Server, false },
                                new CoapOptionContentFormat(CoreLinkFormat.ContentFormatCode));
     }
 }
Example #2
0
        internal bool Register(Lwm2mClient Client)
        {
            if (!this.shortServerId.IntegerValue.HasValue)
            {
                return(false);
            }

            Lwm2mSecurityObjectInstance SecurityInfo = Client.GetSecurityInfo(
                (ushort)this.shortServerId.IntegerValue.Value);

            if (SecurityInfo is null)
            {
                return(false);
            }

            Lwm2mServerReference Ref = SecurityInfo.GetServerReference(false);

            if (Ref is null)
            {
                return(false);
            }

            Client.Register(this.lifetimeSeconds.IntegerValue.HasValue ?
                            (int)this.lifetimeSeconds.IntegerValue.Value : 86400, Ref);

            return(true);
        }
Example #3
0
        /// <summary>
        /// Sends a BOOTSTRAP-REQUEST to the current LWM2M Bootstram Server, to request the servers
        /// initialize bootstrapping. When bootstrapping is completed, server registration is
        /// performed to the servers reported during bootstrapping.
        /// </summary>
        /// <param name="Callback">Callback method when bootstrap request has completed.</param>
        /// <param name="State">State object to pass on to the callback method.</param>
        /// <returns>If a bootstrap server was found, and request initiated.</returns>
        public async Task <bool> RequestBootstrap(CoapResponseEventHandler Callback, object State)
        {
            foreach (Lwm2mObject Object in this.objects.Values)
            {
                if (Object is Lwm2mSecurityObject SecurityObject)
                {
                    foreach (Lwm2mSecurityObjectInstance Instance in SecurityObject.Instances)
                    {
                        Lwm2mServerReference Ref = Instance.GetServerReference(true);
                        if (Ref != null)
                        {
                            if (Instance.clientHoldOffTimeSeconds.IntegerValue.HasValue &&
                                Instance.clientHoldOffTimeSeconds.IntegerValue.Value > 0)
                            {
                                this.coapEndpoint.ScheduleEvent(
                                    async(P) => await this.RequestBootstrap((Lwm2mServerReference)P, Callback, State),
                                    DateTime.Now.AddSeconds(Instance.clientHoldOffTimeSeconds.IntegerValue.Value),
                                    Ref);
                            }
                            else
                            {
                                await this.RequestBootstrap(Ref, Callback, State);
                            }

                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Example #4
0
        /// <summary>
        /// Sends a BOOTSTRAP-REQUEST to the LWM2M Bootstram Server, to request the servers
        /// initialize bootstrapping. When bootstrapping is completed, server registration is
        /// performed to the servers reported during bootstrapping.
        /// </summary>
        /// <param name="BootstrapServer">Reference to the bootstrap server.</param>
        /// <param name="Callback">Callback method when bootstrap request has completed.</param>
        /// <param name="State">State object to pass on to the callback method.</param>
        public async Task RequestBootstrap(Lwm2mServerReference BootstrapServer,
                                           CoapResponseEventHandler Callback, object State)
        {
            this.bootstrapSever = BootstrapServer;

            Uri BsUri = new Uri(this.bootstrapSever.Uri);
            int Port;

            if (BsUri.IsDefaultPort)
            {
                switch (BsUri.Scheme.ToLower())
                {
                case "coaps":
                    Port = CoapEndpoint.DefaultCoapsPort;
                    break;

                case "coap":
                    Port = CoapEndpoint.DefaultCoapPort;
                    break;

                default:
                    throw new ArgumentException("Unrecognized URI scheme.", nameof(BootstrapServer));
                }
            }
            else
            {
                Port = BsUri.Port;
            }

            if (IPAddress.TryParse(BsUri.Host, out IPAddress Addr))
            {
                this.bootstrapSeverIp = new IPEndPoint[] { new IPEndPoint(Addr, Port) }
            }
            ;
            else
            {
                IPAddress[] Addresses = await Dns.GetHostAddressesAsync(BsUri.Host);

                int i, c = Addresses.Length;

                this.bootstrapSeverIp = new IPEndPoint[c];

                for (i = 0; i < c; i++)
                {
                    this.bootstrapSeverIp[i] = new IPEndPoint(Addresses[i], Port);
                }
            }

            await this.coapEndpoint.POST(this.bootstrapSever.Uri + "bs?ep=" + this.clientName, true,
                                         null, 64, this.bootstrapSever.Credentials, this.BootstrapResponse, new object[] { Callback, State });
        }
Example #5
0
        private void RegisterTimeout(object State)
        {
            object[]             P      = (object[])State;
            Lwm2mServerReference Server = (Lwm2mServerReference)P[0];
            bool Update = (bool)P[1];
            int  Epoch  = (int)P[2];

            if (Epoch != this.registrationEpoch)
            {
                return;
            }

            if (Update)
            {
                this.Register(Server, this.lastLinks, true);
            }
            else
            {
                this.Register(Server, this.lastLinks, false);
            }
        }
Example #6
0
        private void DeregisterResponse(object Sender, CoapResponseEventArgs e)
        {
            Lwm2mServerReference Server = (Lwm2mServerReference)e.State;

            Server.Registered = false;

            try
            {
                if (e.Ok)
                {
                    this.OnDeregistrationSuccessful?.Invoke(this, new Lwm2mServerReferenceEventArgs(Server));
                }
                else
                {
                    this.OnDeregistrationFailed?.Invoke(this, new Lwm2mServerReferenceEventArgs(Server));
                }
            }
            catch (Exception ex)
            {
                Log.Critical(ex);
            }
        }
Example #7
0
        private void RegisterResponse(object Sender, CoapResponseEventArgs e)
        {
            object[]             P      = (object[])e.State;
            Lwm2mServerReference Server = (Lwm2mServerReference)P[0];
            bool Update = (bool)P[1];

            Server.Registered = e.Ok;
            this.coapEndpoint.ScheduleEvent(this.RegisterTimeout,
                                            DateTime.Now.AddSeconds(this.lifetimeSeconds * 0.5), new object[] { Server, e.Ok, this.registrationEpoch });

            try
            {
                if (e.Ok && this.state == Lwm2mState.Registration)
                {
                    this.State = Lwm2mState.Operation;
                }

                if (e.Ok)
                {
                    if (!Update)
                    {
                        Server.LocationPath = e.Message.LocationPath;
                    }

                    this.OnRegistrationSuccessful?.Invoke(this, new Lwm2mServerReferenceEventArgs(Server));
                }
                else
                {
                    this.OnRegistrationFailed?.Invoke(this, new Lwm2mServerReferenceEventArgs(Server));
                }
            }
            catch (Exception ex)
            {
                Log.Critical(ex);
            }
        }
Example #8
0
 /// <summary>
 /// Event arguments for LWM2M server reference events.
 /// </summary>
 /// <param name="Server">Server reference.</param>
 public Lwm2mServerReferenceEventArgs(Lwm2mServerReference Server)
 {
     this.server = Server;
 }
Example #9
0
 /// <summary>
 /// Sends a BOOTSTRAP-REQUEST to the LWM2M Bootstram Server, to request the servers
 /// initialize bootstrapping. When bootstrapping is completed, server registration is
 /// performed to the servers reported during bootstrapping.
 /// </summary>
 /// <param name="BootstrapServer">Reference to the bootstrap server.</param>
 public Task RequestBootstrap(Lwm2mServerReference BootstrapServer)
 {
     return(this.RequestBootstrap(BootstrapServer, null, null));
 }