Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mongoose
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ganil-acq
GANILinux
linux-service
library
mongoose
Commits
fecb11f2
Commit
fecb11f2
authored
8 years ago
by
Alexander Alashkin
Committed by
Cesanta Bot
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add CoAP server example doc. Closes cesanta/dev#4917
PUBLISHED_FROM=ee7728db61721c0628a00ed702a47f7382ad7cd7
parent
56e50c2d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
docs/coap/server_example.md
+60
-1
60 additions, 1 deletion
docs/coap/server_example.md
with
60 additions
and
1 deletion
docs/coap/server_example.md
+
60
−
1
View file @
fecb11f2
...
@@ -2,4 +2,63 @@
...
@@ -2,4 +2,63 @@
title
:
CoAP server example
title
:
CoAP server example
---
---
TBD
To create a CoAP server, follow this pattern:
1.
Create a listening connection by calling
`mg_bind()`
or
`mg_bind_opt()`
2.
2. Call
`mg_set_protocol_coap()`
for that listening connection.
3.
Create an event handler function that handles the following events:
-
`MG_EV_COAP_CON`
-
`MG_EV_COAP_NOC`
-
`MG_EV_COAP_ACK`
-
`MG_EV_COAP_RST`
Here's an example of the simplest CoAP server. Error checking is omitted for the sake of clarity:
```
c
#include
"mongoose.h"
static
char
*
s_default_address
=
"udp://:5683"
;
static
void
coap_handler
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
p
)
{
switch
(
ev
)
{
case
MG_EV_COAP_CON
:
{
uint32_t
res
;
struct
mg_coap_message
*
cm
=
(
struct
mg_coap_message
*
)
p
;
printf
(
"CON with msg_id = %d received
\n
"
,
cm
->
msg_id
);
res
=
mg_coap_send_ack
(
nc
,
cm
->
msg_id
);
if
(
res
==
0
)
{
printf
(
"Successfully sent ACK for message with msg_id = %d
\n
"
,
cm
->
msg_id
);
}
else
{
printf
(
"Error: %d
\n
"
,
res
);
}
break
;
}
case
MG_EV_COAP_NOC
:
case
MG_EV_COAP_ACK
:
case
MG_EV_COAP_RST
:
{
struct
mg_coap_message
*
cm
=
(
struct
mg_coap_message
*
)
p
;
printf
(
"ACK/RST/NOC with msg_id = %d received
\n
"
,
cm
->
msg_id
);
break
;
}
}
}
int
main
(
void
)
{
struct
mg_mgr
mgr
;
struct
mg_connection
*
nc
;
mg_mgr_init
(
&
mgr
,
0
);
nc
=
mg_bind
(
&
mgr
,
s_default_address
,
coap_handler
);
mg_set_protocol_coap
(
nc
);
while
(
1
)
{
mg_mgr_poll
(
&
mgr
,
1000
);
}
mg_mgr_free
(
&
mgr
);
return
0
;
}
```
See full source code at
[
CoAP server example
](
https://github.com/cesanta/mongoose/tree/master/examples/coap_server
)
.
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment