Skip to content

Commit

Permalink
examples/lte_lwm2m: Implement device object
Browse files Browse the repository at this point in the history
Implement some resources in device object
- manufacture
- model number
- serial number
- firmware version
- reboot
- memory free
- memory total
  • Loading branch information
SPRESENSE committed Dec 17, 2021
1 parent 96110ba commit 24dec30
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 13 deletions.
1 change: 1 addition & 0 deletions examples/lte_lwm2m/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ CSRCS += object_connectivity_stat.c
CSRCS += system_api.c
CSRCS += object_test.c
CSRCS += lwm2m_lte_connection.c
CSRCS += system_device.c
MAINSRC = lwm2mclient.c

include $(APPDIR)/Application.mk
2 changes: 1 addition & 1 deletion examples/lte_lwm2m/lwm2mclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ int main(int argc, char FAR *argv[])
* Message should normally be lost with reboot ...
*/
fprintf(stderr, "reboot time expired, rebooting ...");
system_reboot();
device_reboot();
}
else
{
Expand Down
10 changes: 10 additions & 0 deletions examples/lte_lwm2m/lwm2mclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ void handle_value_changed(lwm2m_context_t* lwm2mH, lwm2m_uri_t* uri, const char
*/
void init_value_change(lwm2m_context_t * lwm2m);
void system_reboot(void);
/*
* system_device.c
*/
const char * get_manufacture(void);
const char * get_model_number(void);
const char * get_serial_number(void);
const char * get_firmware_version(void);
void device_reboot(void);
int get_free_memory(void);
int get_total_memory(void);

/*
* object_security.c
Expand Down
31 changes: 19 additions & 12 deletions examples/lte_lwm2m/object_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@
#include <time.h>


#define PRV_MANUFACTURER "Open Mobile Alliance"
#define PRV_MODEL_NUMBER "Lightweight M2M Client"
#define PRV_SERIAL_NUMBER "345000123"
#define PRV_FIRMWARE_VERSION "1.0"
#define PRV_POWER_SOURCE_1 1
#define PRV_POWER_SOURCE_2 5
#define PRV_POWER_VOLTAGE_1 3800
Expand Down Expand Up @@ -118,6 +114,7 @@ typedef struct
int64_t time;
uint8_t battery_level;
char time_offset[PRV_OFFSET_MAXLEN];
int64_t total_memory;
} device_data_t;


Expand Down Expand Up @@ -173,22 +170,22 @@ static uint8_t prv_set_value(lwm2m_data_t * dataP,
{
case RES_O_MANUFACTURER:
if (dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE) return COAP_404_NOT_FOUND;
lwm2m_data_encode_string(PRV_MANUFACTURER, dataP);
lwm2m_data_encode_string(get_manufacture(), dataP);
return COAP_205_CONTENT;

case RES_O_MODEL_NUMBER:
if (dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE) return COAP_404_NOT_FOUND;
lwm2m_data_encode_string(PRV_MODEL_NUMBER, dataP);
lwm2m_data_encode_string(get_model_number(), dataP);
return COAP_205_CONTENT;

case RES_O_SERIAL_NUMBER:
if (dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE) return COAP_404_NOT_FOUND;
lwm2m_data_encode_string(PRV_SERIAL_NUMBER, dataP);
lwm2m_data_encode_string(get_serial_number(), dataP);
return COAP_205_CONTENT;

case RES_O_FIRMWARE_VERSION:
if (dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE) return COAP_404_NOT_FOUND;
lwm2m_data_encode_string(PRV_FIRMWARE_VERSION, dataP);
lwm2m_data_encode_string(get_firmware_version(), dataP);
return COAP_205_CONTENT;

case RES_M_REBOOT:
Expand Down Expand Up @@ -303,6 +300,7 @@ static uint8_t prv_set_value(lwm2m_data_t * dataP,

case RES_O_MEMORY_FREE:
if (dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE) return COAP_404_NOT_FOUND;
devDataP->free_memory = get_free_memory();
lwm2m_data_encode_int(devDataP->free_memory, dataP);
return COAP_205_CONTENT;

Expand Down Expand Up @@ -358,6 +356,11 @@ static uint8_t prv_set_value(lwm2m_data_t * dataP,
lwm2m_data_encode_string(PRV_BINDING_MODE, dataP);
return COAP_205_CONTENT;

case RES_O_MEMORY_TOTAL:
if (dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE) return COAP_404_NOT_FOUND;
lwm2m_data_encode_int(devDataP->total_memory, dataP);
return COAP_205_CONTENT;

default:
return COAP_404_NOT_FOUND;
}
Expand Down Expand Up @@ -401,7 +404,8 @@ static uint8_t prv_device_read(lwm2m_context_t *contextP,
RES_O_CURRENT_TIME,
RES_O_UTC_OFFSET,
RES_O_TIMEZONE,
RES_M_BINDING_MODES
RES_M_BINDING_MODES,
RES_O_MEMORY_TOTAL
};
int nbRes = sizeof(resList)/sizeof(uint16_t);

Expand Down Expand Up @@ -464,7 +468,8 @@ static uint8_t prv_device_discover(lwm2m_context_t *contextP,
RES_O_CURRENT_TIME,
RES_O_UTC_OFFSET,
RES_O_TIMEZONE,
RES_M_BINDING_MODES
RES_M_BINDING_MODES,
RES_O_MEMORY_TOTAL
};
int nbRes = sizeof(resList) / sizeof(uint16_t);

Expand Down Expand Up @@ -499,6 +504,7 @@ static uint8_t prv_device_discover(lwm2m_context_t *contextP,
case RES_O_UTC_OFFSET:
case RES_O_TIMEZONE:
case RES_M_BINDING_MODES:
case RES_O_MEMORY_TOTAL:
break;
default:
result = COAP_404_NOT_FOUND;
Expand Down Expand Up @@ -682,10 +688,11 @@ lwm2m_object_t * get_object_device()
if (NULL != deviceObj->userData)
{
((device_data_t*)deviceObj->userData)->battery_level = PRV_BATTERY_LEVEL;
((device_data_t*)deviceObj->userData)->free_memory = PRV_MEMORY_FREE;
((device_data_t*)deviceObj->userData)->free_memory = get_free_memory();
((device_data_t*)deviceObj->userData)->error = PRV_ERROR_CODE;
((device_data_t*)deviceObj->userData)->time = 1367491215;
((device_data_t*)deviceObj->userData)->time = 0;
strcpy(((device_data_t*)deviceObj->userData)->time_offset, "+01:00");
((device_data_t*)deviceObj->userData)->total_memory = get_total_memory();
}
else
{
Expand Down
114 changes: 114 additions & 0 deletions examples/lte_lwm2m/system_device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/****************************************************************************
* lte_lwm2m/system_device.c
*
* Copyright 2021 Sony Semiconductor Solutions Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of Sony Semiconductor Solutions Corporation nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/boardctl.h>
#include <sys/utsname.h>

#include "lwm2mclient.h"
#include "liblwm2m.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define PRV_MANUFACTURER "SONY SPRESENSE"
#define PRV_MODEL_NUMBER "CXD5602PWBMAIN1"
#define PRV_MEMORY_TOTAL (1536 * 1024 / 1000)

/****************************************************************************
* Public Functions
****************************************************************************/

const char *get_manufacture(void)
{
return PRV_MANUFACTURER;
}

const char *get_model_number(void)
{
return PRV_MODEL_NUMBER;
}

const char *get_serial_number(void)
{
uint8_t uid[CONFIG_BOARDCTL_UNIQUEID_SIZE];
static char boardid[CONFIG_BOARDCTL_UNIQUEID_SIZE * 2 + 1];

memset(boardid, 0, sizeof(boardid));
boardctl(BOARDIOC_UNIQUEID, (uintptr_t)uid);
snprintf(boardid, sizeof(boardid), "%02X%02X%02X%02X%02X",
uid[0], uid[1], uid[2], uid[3], uid[4]);

return boardid;
}

const char *get_firmware_version(void)
{
static struct utsname name;

uname(&name);

return name.version;
}

void device_reboot(void)
{
boardctl(BOARDIOC_RESET, EXIT_SUCCESS);
exit(1);
}

int get_free_memory(void)
{
struct mallinfo info;

info = mallinfo();

return info.fordblks / 1000;
}

int get_total_memory(void)
{
return PRV_MEMORY_TOTAL;
}

0 comments on commit 24dec30

Please sign in to comment.