/// <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); }
/// <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 }); }
private void BootstrapResponse(object Sender, CoapResponseEventArgs e) { object[] P = (object[])e.State; CoapResponseEventHandler Callback = (CoapResponseEventHandler)P[0]; object State = P[1]; if (Callback != null) { try { Callback.Invoke(this, e); } catch (Exception ex) { Log.Critical(ex); } } }