Skip to content

Commit 44b1a9e

Browse files
committed
add lobaroCoAP Demo and CONFIG_COAP_DEMO_ENABLED definition (aws#2390)
1 parent a80631f commit 44b1a9e

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

demos/coap/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# C SDK COAP demo
2+
afr_demo_module(coap)
3+
4+
afr_set_demo_metadata(ID "COAP_DEMO")
5+
afr_set_demo_metadata(DESCRIPTION "An example that demonstrates COAP")
6+
afr_set_demo_metadata(DISPLAY_NAME "COAP Hello World")
7+
8+
afr_module_sources(
9+
${AFR_CURRENT_MODULE}
10+
INTERFACE
11+
"${CMAKE_CURRENT_LIST_DIR}/iot_demo_coap.c"
12+
)
13+
afr_module_dependencies(
14+
${AFR_CURRENT_MODULE}
15+
INTERFACE
16+
AFR::coap
17+
)
18+
19+
20+

demos/coap/iot_demo_coap.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
/*
3+
* Created on: Nov 10, 2020
4+
* Authors: Mohammed Abdelmaksoud & Hatim Jamali
5+
* 1NCE GmbH
6+
*
7+
*/
8+
9+
/**
10+
* @file iot_demo_coap.c
11+
* @brief Demonstrates usage of lobaro COAP library.
12+
*/
13+
14+
/* The config header is always included first. */
15+
#include "iot_config.h"
16+
17+
/* Standard includes. */
18+
#include <stdbool.h>
19+
#include <stdio.h>
20+
#include <stdlib.h>
21+
#include <string.h>
22+
23+
/* Set up logging for this demo. */
24+
#include "iot_demo_logging.h"
25+
26+
/* Platform layer includes. */
27+
#include "platform/iot_clock.h"
28+
#include "platform/iot_threads.h"
29+
#include "platform/iot_network.h"
30+
31+
/* COAP include. */
32+
#include "coap_main.h"
33+
34+
35+
/*-----------------------------------------------------------*/
36+
37+
38+
39+
CoAP_RespHandler_fn_t CoAP_Resp_handler(CoAP_Message_t* pRespMsg, NetEp_t* Sender) {
40+
configPRINTF(
41+
( "MESSAGE Payload : %s \r\n" , pRespMsg->Payload ));
42+
PrintEndpoint(Sender);
43+
}
44+
45+
/* Declaration of demo function. */
46+
int RuncoapDemo( bool awsIotMqttMode,
47+
const char * pIdentifier,
48+
void * pNetworkServerInfo,
49+
void * pNetworkCredentialInfo,
50+
const IotNetworkInterface_t * pNetworkInterface );
51+
52+
/*-----------------------------------------------------------*/
53+
/**
54+
* @brief The function that runs the COAP demo, called by the demo runner.
55+
* @return `EXIT_SUCCESS` if the demo completes successfully; `EXIT_FAILURE` otherwise.
56+
*/
57+
int RuncoapDemo(bool awsIotMqttMode,
58+
const char * pIdentifier,
59+
void * pNetworkServerInfo,
60+
void * pNetworkCredentialInfo,
61+
const IotNetworkInterface_t * pNetworkInterface)
62+
{
63+
/* Return value of this function and the exit status of this program. */
64+
int status = EXIT_FAILURE;
65+
66+
SocketsSockaddr_t ServerAddress;
67+
NetPacket_t pPacket;
68+
69+
configPRINTF(( "Connecting to CoAP server\r\n" ));
70+
ServerAddress.usPort = SOCKETS_htons(configCOAP_PORT);
71+
ServerAddress.ulAddress = SOCKETS_inet_addr_quick(configCOAP_SERVER_ADDR0,
72+
configCOAP_SERVER_ADDR1, configCOAP_SERVER_ADDR2,
73+
configCOAP_SERVER_ADDR3);
74+
const NetEp_t ServerEp = { .NetType = IPV4, .NetPort =
75+
configCOAP_PORT, .NetAddr = { .IPv4 = { .u8 = {
76+
configCOAP_SERVER_ADDR0, configCOAP_SERVER_ADDR1,
77+
configCOAP_SERVER_ADDR2, configCOAP_SERVER_ADDR3 } } } };
78+
79+
80+
/* Create UDP Socket */
81+
Socket_t udp = SOCKETS_Socket( SOCKETS_AF_INET, SOCKETS_SOCK_DGRAM,
82+
SOCKETS_IPPROTO_UDP);
83+
CoAP_Socket_t *socket = AllocSocket();
84+
85+
/* Connect to CoAP server */
86+
if ( SOCKETS_Connect(udp, &ServerAddress, sizeof(ServerAddress)) == 0) {
87+
configPRINTF(( "Connected to CoAP server \r\n" ));
88+
89+
/* Add payload */
90+
char pcTransmittedString[] = "hello from CoAP";
91+
92+
/* Add socket and Network Interface configuration */
93+
socket->Handle = udp;
94+
socket->Tx = CoAP_Send_Wifi;
95+
96+
/* Create confirmable CoAP POST packet with URI Path option */
97+
CoAP_Message_t *pReqMsg = CoAP_CreateMessage(CON, REQ_POST,
98+
CoAP_GetNextMid(), CoAP_GenerateToken());
99+
100+
CoAP_addNewPayloadToMessage(pReqMsg, pcTransmittedString,
101+
strlen(pcTransmittedString));
102+
CoAP_AddOption(pReqMsg, OPT_NUM_URI_PATH, configCOAP_URI_PATH , strlen(configCOAP_URI_PATH));
103+
104+
105+
/* Create CoAP Client Interaction to send a confirmable POST Request */
106+
CoAP_StartNewClientInteraction( pReqMsg,socket->Handle, &ServerEp, CoAP_Resp_handler);
107+
CoAP_Interaction_t* pIA = CoAP_GetLongestPendingInteraction();
108+
109+
110+
/* Execute the Interaction list */
111+
112+
while (pIA != NULL) {
113+
114+
CoAP_doWork();
115+
116+
if (pIA->State == COAP_STATE_WAITING_RESPONSE){
117+
CoAP_Recv_Wifi(socket->Handle, &pPacket,ServerEp);
118+
}
119+
pIA = CoAP_GetLongestPendingInteraction();
120+
121+
if (pIA->State == COAP_STATE_FINISHED)
122+
{
123+
status = EXIT_SUCCESS;
124+
}
125+
}
126+
127+
}
128+
129+
130+
131+
132+
return status;
133+
}
134+
135+
/*-----------------------------------------------------------*/

demos/include/iot_demo_runner.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#undef democonfigDEMO_PRIORITY
4949
#define democonfigDEMO_PRIORITY democonfigMQTT_ECHO_TASK_PRIORITY
5050
#endif
51+
#elif defined( CONFIG_COAP_DEMO_ENABLED )
52+
#define DEMO_entryFUNCTION RuncoapDemo
5153
#elif defined( CONFIG_CORE_MQTT_BASIC_TLS_DEMO_ENABLED )
5254
#define DEMO_entryFUNCTION RunCoreMqttBasicTLSDemo
5355
#if defined( democonfigMQTT_ECHO_TASK_STACK_SIZE )

0 commit comments

Comments
 (0)