Skip to content

Commit 7196a81

Browse files
authored
Merge pull request #193 from husigeza/Coap_doc_fix
CoAp: Addressing review findings
2 parents 0183a12 + e0931ae commit 7196a81

File tree

1 file changed

+37
-37
lines changed
  • content/firmwareapi/pycom/network

1 file changed

+37
-37
lines changed

content/firmwareapi/pycom/network/coap.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ aliases:
55
- firmwareapi/pycom/network/coap.md
66
- chapter/firmwareapi/pycom/network/coap
77
---
8-
This module implements a CoAp Server and Client, it operates as both at the same time.
8+
This module implements a CoAp Server and Client, operating as both at the same time.
99

1010
## Usage Example
1111

@@ -16,7 +16,7 @@ from network import Coap
1616
import uselect
1717
import _thread
1818

19-
# Callback handling the responses to the requests sent to a Coap Server
19+
# The callback that handles the responses generated from the requests sent to a CoAp Server
2020
def response_callback(code, id_param, type_param, token, payload):
2121
print("Code: {}".format(code))
2222
# The ID can be used to pair the requests with the responses
@@ -34,20 +34,20 @@ def socket_thread(p, coap_socket):
3434
sock = s[0]
3535
event = s[1]
3636
if(event & uselect.POLLIN):
37-
# Check if the socket belongs to Coap module
37+
# Check if the socket belongs to the CoAp module
3838
if(sock == coap_socket):
39-
# Call Coap.read() which parses the incoming Coap message
39+
# Call Coap.read() which parses the incoming CoAp message
4040
Coap.read()
4141

4242

4343
# Connect to the network
4444
wlan = WLAN(mode=WLAN.STA)
4545
wlan.connect('your-ssid', auth=(WLAN.WPA2, 'your-key'))
4646

47-
# Initialize Coap module
47+
# Initialise the CoAp module
4848
Coap.init(str(wlan.ifconfig()[0]), service_discovery=True)
4949

50-
# Add a resource with default value and plain text content format
50+
# Add a resource with a default value and a plain text content format
5151
r = Coap.add_resource("resource1", media_type=Coap.MEDIATYPE_TEXT_PLAIN, value="default_value")
5252
# Add an attribute to the resource
5353
r.add_attribute("title", "resource1")
@@ -61,21 +61,21 @@ r = Coap.add_resource("resource2", media_type=Coap.MEDIATYPE_APP_XML, etag=True)
6161
# Configure the possible operations on the resource
6262
r.callback(Coap.REQUEST_GET | Coap.REQUEST_POST | Coap.REQUEST_PUT | Coap.REQUEST_DELETE, True)
6363

64-
# Register the response handler for the requests the module initiates as a Coap Client
64+
# Register the response handler for the requests that the module initiates as a CoAp Client
6565
Coap.register_response_handler(response_callback)
6666

67-
# Get the UDP socket created for the Coap module
67+
# Get the UDP socket created for the CoAp module
6868
coap_server_socket = Coap.socket()
6969

7070
# Create a new poll object
7171
p = uselect.poll()
72-
# Register the Coap Module's socket to the poll
72+
# Register the CoAp module's socket to the poll
7373
p.register(coap_server_socket, uselect.POLLIN | uselect.POLLHUP | uselect.POLLERR)
7474

7575
# Start a new thread which will handle the sockets of "p" poll
7676
_thread.start_new_thread(socket_thread, (p, coap_server_socket))
7777

78-
# Send a request to a Coap server
78+
# Send a request to a CoAp server
7979
id = Coap.send_request("192.168.0.234", Coap.REQUEST_GET, uri_port=5683, uri_path=".well-known/core", payload="payload", token="token1", include_options=True)
8080
print(id)
8181

@@ -85,43 +85,43 @@ print(id)
8585

8686
#### Coap.init(address, *, port=5683, service_discovery=False)
8787

88-
Initialize the Coap module.
88+
Initialize the CoAp module.
8989

90-
Arguments are:
90+
The arguments are:
9191

92-
* `address` is the address where Coap Module will handle the communication .
93-
* `port` is the port where Coap Module will listen, if not given it is the default Coap UDP port: 5683.
94-
* `service_discovery` is a boolean argument to enable/disable service discovery. If enabled, the Coap Module will listen on the Coap Multicast address too: 224.0.1.187. By default it is disabled.
92+
* `address` is the address where the CoAp module handles communication.
93+
* `port` is the port where the CoAp module listens. If not set, the default CoAp UDP port is 5683.
94+
* `service_discovery` is a Boolean argument that enables/disables service discovery. If enabled, the CoAp module will listen on the CoAp multicast address: 224.0.1.187. This is disabled by default.
9595

96-
## Module's methods
96+
## Methods:
9797

9898
#### Coap.socket()
9999

100-
Returns with the socket assigned to the given `address` and `port` during `Coap.init()` (= assigned to the Coap Module).
100+
Returns with the socket assigned to the given address and port during Coap.init() (= assigned to the CoAp module).
101101

102102
#### Coap.add_resource(uri, *, media_type=-1, max_age=-1, value=0, etag=False)
103103

104-
Creates a resource object and adds it to the Coap Module to operate as a server.
104+
Creates a resource object and adds it to the CoAp module to operate as a server.
105105

106106
* `uri` is the full path of the resource.
107-
* `media_type` is the media type (Coap option: Content-Format) of the resource. If not given, no defined media type is associated with the resource.
108-
* `max_age` is the maximum time in seconds when the value of the resource is considered fresh (Coap option: Max-Age). If not given, no fresh time is associated with the resource.
109-
* `value` is the default value of the resource. If not given it is initialized to decimal 0.
110-
* `etag` is a boolean argument to enable/disable entity tag calculation (Coap option: ETag). By default it is turned off.
107+
* `media_type` is the media type (CoAp option: Content-Format) of the resource. If not given, no defined media type is associated with the resource.
108+
* `max_age` is the maximum time in seconds that the value of the resource is considered fresh (CoAp option: Max-Age). If not given, no fresh time is associated with the resource.
109+
* `value` is the default value of the resource. If not given, it is initialised to decimal 0.
110+
* `etag` is a Boolean argument that enables/disables entity tag calculation (CoAp option: ETag). By default it is turned off.
111111

112112

113113
{{% hint style="info" %}}
114-
Media type argument should be one of the standard defined value which are available via Coap Module's constants.
114+
Media-type argument is one of the standard defined values that is available via CoAp module's constants.
115115
{{% /hint %}}
116116

117117
{{% hint style="info" %}}
118-
Entity tag calculation is a simple counter increment between value 1-65535 with overflow but without value 0. Incremented each time the value of the resource is changed.
118+
Entity tag calculation is a simple counter increment between value 1-65535 with overflow, it doesn't include the value 0. It is incremented each time and the value of the resource is changed.
119119
{{% /hint %}}
120120

121121

122122
#### Coap.remove_resource(uri)
123123

124-
Removes the resource defined by `uri` argument.
124+
Removes the resource defined by the `uri` argument.
125125

126126
* `uri` is the full path of the resource to be removed.
127127

@@ -133,11 +133,11 @@ Returns with the resource defined by `uri` argument.
133133

134134
#### Coap.read()
135135

136-
Must be called when a packet is received on the socket assigned to the Coap Module. This function parses the incoming request, composes and sends out the response if needed.
136+
Must be called when a packet is received on the socket assigned to the CoAp module. This function passes on the incoming request, whilst also composing and sending out the response if needed.
137137

138138
#### Coap.register_response_handler(callback)
139139

140-
Registers a callback function which will be called when a remote Coap Server responses to our request.
140+
Registers a callback function which will be called when a remote CoAp Server responses to the local CoAp client's request.
141141

142142
* `callback` is the callback to be registered. It must have the following arguments:
143143
* `code` is the response code from the received message
@@ -148,20 +148,20 @@ Registers a callback function which will be called when a remote Coap Server res
148148

149149
#### Coap.send_request(uri_host, method, *, uri_port=5683, uri_path, content_format, payload, token, include_options=true)
150150

151-
Creates and sends a request to a Coap server.
151+
Creates and sends a request to a CoAp server.
152152

153153
* `uri_host` is the IP address of the server, included in the message as an "URI-HOST" option
154154
* `method` is the method to be sent to the server, can be: `Coap.REQUEST_GET`, `Coap.REQUEST_PUT`, `Coap.REQUEST_POST`, `Coap.REQUEST_DELETE`
155-
* `uri_port` is the port of the server, included in the message as an "URI-PORT" option, by default it is 5683
155+
* `uri_port` is the port of the server, included in the message as an "URI-PORT" option. By default it is 5683
156156
* `uri_path` is the full path of the resource in the server, included in the message as an "URI-PATH" option. If nothing is given the request will not have URI-PATH option.
157157
* `content_format` is the Content-Format option of the request, can be: `Coap.MEDIATYPE_TEXT_PLAIN`, `Coap.MEDIATYPE_APP_LINK_FORMAT`, `Coap.MEDIATYPE_APP_XML`, `Coap.MEDIATYPE_APP_OCTET_STREAM`, `Coap.MEDIATYPE_APP_RDF_XML`, `Coap.MEDIATYPE_APP_EXI`, `Coap.MEDIATYPE_APP_JSON`, `Coap.MEDIATYPE_APP_CBOR`. If nothing is given the request will not have Content-Format option.
158158
* `payload` is the payload of the request. If nothing is given the request will not have payload.
159159
* `token` is the token field of the request. If nothing is given the request will not have token field.
160-
* `include_options` decides whether put any options (including the ones above) into the message or not. It can be used to send special requests to servers accepting Coap formed requests without options, e.g. to a Dish Telemetry server. By default the options are included.
160+
* `include_options` decides whether put any options (including the ones above) into the message or not. It can be used to send special requests to servers accepting CoAp formed requests without options, e.g. to a Dish Telemetry server. By default, the options are included.
161161

162162
## Class resource
163163

164-
The resource class represents a resource in the scope of the Coap Module when acting as a server. A new resource can be only created with the `Coap.add_resource` function.
164+
The resource class represents a resource in the scope of the CoAp module when acting as a server. A new resource can only be created with the `Coap.add_resource` function.
165165

166166
#### Class methods
167167

@@ -175,7 +175,7 @@ Adds a new attribute to the resource. Attributes are used to explain the resourc
175175
* `value` is the value of the resource.
176176

177177
{{% hint style="info" %}}
178-
During service discovery, GET request to ".well-know/core", the attributes are returned with the belonging values.
178+
During service discovery, GET request to ".well-know/core", the attributes are returned with the relevant values.
179179
E.g. using the "libcoap's" command line coap-client to fetch the resource from our server:
180180

181181
coap-client -m get coap://<Coap-Server's address>/.well-known/core
@@ -188,22 +188,22 @@ coap-client -m get coap://<Coap-Server's address>/.well-known/core
188188

189189
Updates or fetches the value of the resource.
190190

191-
* `value` is the value to update the current value with.
192-
If the method is called without parameter the current value is returned.
191+
* `value` is the new value to update the current value with.
192+
If the method is called without a parameter, the current value is returned.
193193

194194
#### resource.callback(operation, enable)
195195
To enable or disable a specific operation (GET, PUT, POST, DELETE) on the resource.
196196

197197
* `operation` is the operation to enable/disable, can be ORED of the followings: `Coap.REQUEST_GET`, `Coap.REQUEST_PUT`, `Coap.REQUEST_POST`, `Coap.REQUEST_DELETE`
198-
* `enable` is boolean parameter to enable/disable the operations specified by `operation`
198+
* `enable` is Boolean parameter that enables/disables the operations specified by `operation`
199199

200200

201201
{{% hint style="info" %}}
202-
During a GET request, only the first occurance of an ETAG or Accept option is parsed and interpreted, the others of the same type are dropped (if any).
202+
During a GET request, only the first occurrence of an ETAG or Accept option is passed on and interpreted; others of the same type are dropped (if any).
203203
{{% /hint %}}
204204

205205
{{% hint style="info" %}}
206-
During a PUT request, only the first occurance of an If-Match option is parsed and interpreted, the others of the same type are dropped (if any).
206+
During a PUT request, only the first occurrence of an If-Match option is passed on and interpreted; others of the same type are dropped (if any).
207207
{{% /hint %}}
208208

209209
{{% hint style="danger" %}}

0 commit comments

Comments
 (0)