Skip to content

Commit

Permalink
Implement Send
Browse files Browse the repository at this point in the history
Implement Send in server side.
Update of sbertin-telular work
  • Loading branch information
semhoun committed Jun 21, 2024
1 parent 8473123 commit 3a7a80a
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 4 deletions.
11 changes: 8 additions & 3 deletions core/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,13 @@ typedef struct
} bs_data_t;
#endif

typedef enum
{
typedef enum {
LWM2M_REQUEST_TYPE_UNKNOWN,
LWM2M_REQUEST_TYPE_DM,
LWM2M_REQUEST_TYPE_REGISTRATION,
LWM2M_REQUEST_TYPE_BOOTSTRAP,
LWM2M_REQUEST_TYPE_DELETE_ALL
LWM2M_REQUEST_TYPE_DELETE_ALL,
LWM2M_REQUEST_TYPE_SEND,
} lwm2m_request_type_t;

#ifdef LWM2M_SUPPORT_SENML_CBOR
Expand Down Expand Up @@ -600,4 +600,9 @@ lwm2m_server_t * utils_findBootstrapServer(lwm2m_context_t * contextP, void * fr
lwm2m_client_t * utils_findClient(lwm2m_context_t * contextP, void * fromSessionH);
#endif

// defined in reporting.c
#if defined(LWM2M_SERVER_MODE) && !defined(LWM2M_VERSION_1_0)
uint8_t reporting_handleSend(lwm2m_context_t *contextP, void *fromSessionH, coap_packet_t *message);
#endif

#endif
5 changes: 5 additions & 0 deletions core/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ static uint8_t handle_request(lwm2m_context_t * contextP,
case LWM2M_REQUEST_TYPE_REGISTRATION:
result = registration_handleRequest(contextP, &uri, fromSessionH, message, response);
break;
#ifndef LWM2M_VERSION_1_0
case LWM2M_REQUEST_TYPE_SEND:
result = reporting_handleSend(contextP, fromSessionH, message);
break;
#endif
#endif
#ifdef LWM2M_BOOTSTRAP_SERVER_MODE
case LWM2M_REQUEST_TYPE_BOOTSTRAP:
Expand Down
57 changes: 57 additions & 0 deletions core/reporting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
*
* Copyright (c) 2024
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* The Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Nathanaël Semhoun - Please refer to git log
*
*******************************************************************************/

#include "internals.h"
#include <stdio.h>

#ifdef LWM2M_SERVER_MODE

#ifndef LWM2M_VERSION_1_0
uint8_t reporting_handleSend(lwm2m_context_t *contextP, void *fromSessionH, coap_packet_t *message) {
lwm2m_client_t *clientP;
lwm2m_media_type_t format;

LOG_DBG("Entering");

if (message->code != COAP_POST)
return COAP_400_BAD_REQUEST;

for (clientP = contextP->clientList; clientP != NULL; clientP = clientP->next) {
if (clientP->sessionH == fromSessionH)
break;
}
if (clientP == NULL)
return COAP_400_BAD_REQUEST;

format = utils_convertMediaType(message->content_type);

if (contextP->reportingSendCallback != NULL) {
contextP->reportingSendCallback(contextP, clientP->internalID, NULL, message->code, NULL, format,
message->payload, message->payload_len, contextP->reportingSendUserData);
}

return COAP_204_CHANGED;
}
void lwm2m_reporting_set_send_callback(lwm2m_context_t *contextP, lwm2m_result_callback_t callback, void *userData) {
LOG_DBG("Entering");
contextP->reportingSendCallback = callback;
contextP->reportingSendUserData = userData;
}
#endif

#endif
7 changes: 6 additions & 1 deletion core/uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@ lwm2m_request_type_t uri_decode(char * altPath,
uriPath = uriPath->next;
if (uriPath != NULL) goto error;
return LWM2M_REQUEST_TYPE_BOOTSTRAP;
} else if (NULL != uriPath && URI_SEND_SEGMENT_LEN == uriPath->len &&
0 == strncmp(URI_SEND_SEGMENT, (char *)uriPath->data, uriPath->len)) {
uriPath = uriPath->next;
if (uriPath != NULL)
goto error;
return LWM2M_REQUEST_TYPE_SEND;
}

if (requestType != LWM2M_REQUEST_TYPE_REGISTRATION)
{
// Read altPath if any
Expand Down
19 changes: 19 additions & 0 deletions examples/server/lwm2mserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,21 @@ static void prv_monitor_callback(lwm2m_context_t *lwm2mH, uint16_t clientID, lwm
fflush(stdout);
}

#ifndef LWM2M_VERSION_1_0
static void prv_reporting_send_callback(lwm2m_context_t *lwm2mH, uint16_t clientID, lwm2m_uri_t *uriP, int status,
block_info_t *block_info, lwm2m_media_type_t format, uint8_t *data,
size_t dataLength, void *userData) {
/* unused parameter */
(void)userData;

fprintf(stdout, "\r\nClient #%d send.\r\n", clientID);
output_data(stdout, block_info, format, data, dataLength, 1);

fprintf(stdout, "\r\n> ");
fflush(stdout);
}
#endif

static void prv_quit(lwm2m_context_t *lwm2mH,
char * buffer,
void * user_data)
Expand Down Expand Up @@ -1159,6 +1174,10 @@ int main(int argc, char *argv[])

lwm2m_set_monitoring_callback(lwm2mH, prv_monitor_callback, NULL);

#ifndef LWM2M_VERSION_1_0
lwm2m_reporting_set_send_callback(lwm2mH, prv_reporting_send_callback, NULL);
#endif

while (0 == g_quit)
{
FD_ZERO(&readfds);
Expand Down
5 changes: 5 additions & 0 deletions include/liblwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ struct _lwm2m_context_
#ifdef LWM2M_SERVER_MODE
lwm2m_result_callback_t monitorCallback;
void * monitorUserData;
lwm2m_result_callback_t reportingSendCallback;
void *reportingSendUserData;
#endif
#ifdef LWM2M_BOOTSTRAP_SERVER_MODE
lwm2m_bootstrap_callback_t bootstrapCallback;
Expand Down Expand Up @@ -906,6 +908,9 @@ int lwm2m_dm_delete(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t *
// Information Reporting APIs
int lwm2m_observe(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
int lwm2m_observe_cancel(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);

// Send resources Reporting API.
void lwm2m_reporting_set_send_callback(lwm2m_context_t *contextP, lwm2m_result_callback_t callback, void *userData);
#endif

#ifdef LWM2M_BOOTSTRAP_SERVER_MODE
Expand Down
1 change: 1 addition & 0 deletions wakaama.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ function(target_sources_wakaama target)
${WAKAAMA_TOP_LEVEL_DIRECTORY}/core/registration.c
${WAKAAMA_TOP_LEVEL_DIRECTORY}/core/uri.c
${WAKAAMA_TOP_LEVEL_DIRECTORY}/core/utils.c
${WAKAAMA_TOP_LEVEL_DIRECTORY}/core/reporting.c
)

target_include_directories(${target} PRIVATE ${WAKAAMA_TOP_LEVEL_DIRECTORY}/include)
Expand Down

0 comments on commit 3a7a80a

Please sign in to comment.