Пример #1
0
        /// <summary>
        /// Called when a CoAP request is received...we will only support CON requests
        /// of type GET... the path is sensors/temp
        /// </summary>
        /// <param name="coapReq">CoAPRequest object</param>
        private static void OnCoAPRequestReceived(CoAPRequest coapReq)
        {
            string reqPath = (coapReq.GetPath() != null) ? coapReq.GetPath().ToLower() : "";

            if (coapReq.MessageType.Value == CoAPMessageType.CON)
            {
                if (coapReq.Code.Value != CoAPMessageCode.GET)
                {
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                         CoAPMessageCode.METHOD_NOT_ALLOWED,
                                                         coapReq /*Copy all necessary values from request in the response*/);
                    //When you use the constructor that accepts a request, then automatically
                    //the message id , token and remote sender values are copied over to the response
                    coapServer.Send(resp);
                }
                else if (reqPath != "sensors/temp")
                {
                    //We do not understand this..
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                         CoAPMessageCode.NOT_FOUND,
                                                         coapReq /*Copy all necessary values from request in the response*/);
                    coapServer.Send(resp);
                }
                else
                {
                    Debug.WriteLine(coapReq.ToString());
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                         CoAPMessageCode.CONTENT,
                                                         coapReq /*Copy all necessary values from request in the response*/);
                    //The payload will be JSON
                    Hashtable ht = new Hashtable();
                    ht.Add("temp", GetRoomTemperature());
                    string jsonStr = JSONResult.ToJSON(ht);
                    resp.AddPayload(jsonStr);
                    //Tell recipient about the content-type of the response
                    resp.AddOption(CoAPHeaderOption.CONTENT_FORMAT, AbstractByteUtils.GetBytes(CoAPContentFormatOption.APPLICATION_JSON));
                    //send it
                    coapServer.Send(resp);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Called when a CoAP request is received...we will only support CON requests
        /// of type GET... the path is sensors/temp
        /// </summary>
        /// <param name="coapReq">CoAPRequest object</param>
        static void OnCoAPRequestReceived(CoAPRequest coapReq)
        {
            string reqPath = (coapReq.GetPath() != null) ? coapReq.GetPath().ToLower() : "";

            /*We have skipped error handling in code below to just focus on observe option*/
            if (coapReq.MessageType.Value == CoAPMessageType.CON && coapReq.Code.Value == CoAPMessageCode.GET)
            {
                if (!coapReq.IsObservable() /*Does the request have "Observe" option*/)
                {
                    /*Request is not to observe...we do not support anything no-observable*/
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                         CoAPMessageCode.NOT_IMPLEMENTED,
                                                         coapReq /*Copy all necessary values from request in the response*/);
                    coapServer.Send(resp);
                }
                else if (!coapServer.ObserversList.IsResourceBeingObserved(coapReq.GetURL()) /*do we support observation on this path*/)
                {
                    //Observation is not supported on this path..just to tell you how to check
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                         CoAPMessageCode.NOT_FOUND,
                                                         coapReq /*Copy all necessary values from request in the response*/);
                    coapServer.Send(resp);
                }
                else
                {
                    //This is a request to observe this resource...register this client
                    coapServer.ObserversList.AddResourceObserver(coapReq);

                    /*Request contains observe option and path is correct*/
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                         CoAPMessageCode.EMPTY,
                                                         coapReq /*Copy all necessary values from request in the response*/);

                    //send it..tell client we registered it's request to observe
                    coapServer.Send(resp);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// This is where we will get the PUT request
        /// </summary>
        /// <param name="coapReq">CoAPRequest</param>
        static void OnCoAPRequestReceived(CoAPRequest coapReq)
        {
            string path = coapReq.GetPath();

            /*Error checking not done to simplify example*/
            if (coapReq.MessageType.Value == CoAPMessageType.CON &&
                coapReq.Code.Value == CoAPMessageCode.PUT &&
                path == "largedata/blockput")
            {
                if (_rxBytes == null)
                {
                    _rxBytes = new Hashtable();
                }
                CoAPBlockOption rxBlockOption = coapReq.GetBlockOption(CoAPHeaderOption.BLOCK1);
                if (rxBlockOption != null)
                {
                    byte[] rxBytes = coapReq.Payload.Value;
                    if (_rxBytes.Contains(rxBlockOption.SequenceNumber))
                    {
                        _rxBytes[rxBlockOption.SequenceNumber] = rxBytes;//Update
                    }
                    else
                    {
                        _rxBytes.Add(rxBlockOption.SequenceNumber, rxBytes);//Add
                    }
                    //Now send an ACK
                    CoAPBlockOption ackBlockOption = new CoAPBlockOption(rxBlockOption.SequenceNumber,
                                                                         false /*incidate to client that we have guzzled all the bytes*/,
                                                                         rxBlockOption.SizeExponent);
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.CONTENT, coapReq.ID.Value);
                    resp.Token        = coapReq.Token;
                    resp.RemoteSender = coapReq.RemoteSender;
                    resp.SetBlockOption(CoAPHeaderOption.BLOCK1, ackBlockOption);
                    _server.Send(resp);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Called when a CoAP request is received (NON, CON)
        /// </summary>
        /// <param name="coapReq">CoAPRequest object</param>
        private static void OnCoAPRequestReceived(CoAPRequest coapReq)
        {
            string reqPath = (coapReq.GetPath() != null) ? coapReq.GetPath().ToLower() : "";

            /*
             * For well-know path, we should support both CON and NON.
             * For NON request, we send back the details in another NON message
             * For CON request, we send back the details in an ACK
             */
            /*Well known should be a GET*/
            if (coapReq.Code.Value != CoAPMessageCode.GET)
            {
                if (coapReq.MessageType.Value == CoAPMessageType.CON)
                {
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                         CoAPMessageCode.METHOD_NOT_ALLOWED,
                                                         coapReq /*Copy all necessary values from request in the response*/);
                    //When you use the constructor that accepts a request, then automatically
                    //the message id , token and remote sender values are copied over to the response
                    coapServer.Send(resp);
                }
                else
                {
                    //For NON, we can only send back a RST
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.RST,
                                                         CoAPMessageCode.METHOD_NOT_ALLOWED,
                                                         coapReq /*Copy all necessary values from request in the response*/);
                    //When you use the constructor that accepts a request, then automatically
                    //the message id , token and remote sender values are copied over to the response
                    coapServer.Send(resp);
                }
            }
            else
            {
                //Message type is GET...check the path..this server only supports well-known path
                if (reqPath != ".well-known/core")
                {
                    if (coapReq.MessageType.Value == CoAPMessageType.CON)
                    {
                        CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                             CoAPMessageCode.NOT_FOUND,
                                                             coapReq /*Copy all necessary values from request in the response*/);
                        coapServer.Send(resp);
                    }
                    else
                    {
                        //For NON, we can only send back a RST
                        CoAPResponse resp = new CoAPResponse(CoAPMessageType.RST,
                                                             CoAPMessageCode.NOT_FOUND,
                                                             coapReq /*Copy all necessary values from request in the response*/);
                        coapServer.Send(resp);
                    }
                }
                else
                {
                    //Request is GET and path is right
                    if (coapReq.MessageType.Value == CoAPMessageType.CON)
                    {
                        CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                             CoAPMessageCode.CONTENT,
                                                             coapReq /*Copy all necessary values from request in the response*/);
                        //Add response payload
                        resp.AddPayload(GetSupportedResourceDescriptions());
                        //Tell recipient about the content-type of the response
                        resp.AddOption(CoAPHeaderOption.CONTENT_FORMAT, AbstractByteUtils.GetBytes(CoAPContentFormatOption.APPLICATION_LINK_FORMAT));
                        coapServer.Send(resp);
                    }
                    else
                    {
                        //Its a NON, send a NON back...in CoAPSharp, NON is always considered as request
                        CoAPResponse resp = new CoAPResponse(CoAPMessageType.NON,
                                                             CoAPMessageCode.CONTENT,
                                                             coapReq.ID.Value);
                        //Copy over other needed values from the reqeust
                        resp.Token        = coapReq.Token;
                        resp.RemoteSender = coapReq.RemoteSender;
                        resp.AddPayload(GetSupportedResourceDescriptions());
                        //Tell recipient about the content-type of the response
                        resp.AddOption(CoAPHeaderOption.CONTENT_FORMAT, AbstractByteUtils.GetBytes(CoAPContentFormatOption.APPLICATION_LINK_FORMAT));
                        //send it
                        coapServer.Send(resp);
                    }
                }
            }
        }