-
Notifications
You must be signed in to change notification settings - Fork 7
/
object_temperature.cpp
158 lines (130 loc) · 4.59 KB
/
object_temperature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
*
* Copyright (c) 2013, 2014 Intel Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* The Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Simon Bernard
*******************************************************************************/
#include "liblwm2m.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mbed.h"
#include "dbg.h"
#include "LM75B.h"
#define LWM2M_TEMPERATURE_OBJECT_ID 3303
#define PRV_TEMPERATURE_SENSOR_UNITS "Cel"
// Resource Id's:
#define RES_SENSOR_VALUE 5700
#define RES_SENSOR_UNITS 5701
LM75B sensor(p28, p27);
bool opened = false;
static uint8_t prv_set_value(lwm2m_tlv_t * tlvP) {
// a simple switch structure is used to respond at the specified resource asked
switch (tlvP->id) {
case RES_SENSOR_VALUE:
lwm2m_tlv_encode_float((float) sensor, tlvP);
tlvP->type = LWM2M_TYPE_RESOURCE;
return COAP_205_CONTENT ;
case RES_SENSOR_UNITS:
tlvP->value = (uint8_t*) PRV_TEMPERATURE_SENSOR_UNITS;
tlvP->length = strlen(PRV_TEMPERATURE_SENSOR_UNITS);
tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
tlvP->type = LWM2M_TYPE_RESOURCE;
tlvP->dataType = LWM2M_TYPE_STRING;
return COAP_205_CONTENT ;
default:
return COAP_404_NOT_FOUND ;
}
}
static uint8_t prv_temperature_read(uint16_t instanceId, int * numDataP, lwm2m_tlv_t ** dataArrayP,
lwm2m_object_t * objectP) {
uint8_t result;
int i;
// this is a single instance object
if (instanceId != 0) {
return COAP_404_NOT_FOUND ;
}
// is the server asking for the full object ?
if (*numDataP == 0) {
uint16_t resList[] = { RES_SENSOR_VALUE,
RES_SENSOR_UNITS, };
int nbRes = sizeof(resList) / sizeof(uint16_t);
*dataArrayP = lwm2m_tlv_new(nbRes);
if (*dataArrayP == NULL)
return COAP_500_INTERNAL_SERVER_ERROR ;
*numDataP = nbRes;
for (i = 0; i < nbRes; i++) {
(*dataArrayP)[i].id = resList[i];
}
}
i = 0;
do {
result = prv_set_value((*dataArrayP) + i);
i++;
} while (i < *numDataP && result == COAP_205_CONTENT );
return result;
}
static void prv_temperature_close(lwm2m_object_t * objectP) {
if (NULL != objectP->instanceList) {
lwm2m_free(objectP->instanceList);
objectP->instanceList = NULL;
}
}
lwm2m_object_t * get_object_temperature() {
/*
* The get_object_tem function create the object itself and return a pointer to the structure that represent it.
*/
lwm2m_object_t * temperatureObj;
if (!sensor.open()) {
ERR("Unable to open temperature sensor.");
return NULL;
}
opened = true;
temperatureObj = (lwm2m_object_t *) lwm2m_malloc(sizeof(lwm2m_object_t));
if (NULL != temperatureObj) {
memset(temperatureObj, 0, sizeof(lwm2m_object_t));
/*
* It assigns his unique ID
* The 3313 is the standard ID for the mandatory object "IPSO Accelerometer".
*/
temperatureObj->objID = LWM2M_TEMPERATURE_OBJECT_ID;
/*
* there is only one instance of accelerometer on the Application Board for mbed NXP LPC1768.
*
*/
temperatureObj->instanceList = (lwm2m_list_t *) lwm2m_malloc(sizeof(lwm2m_list_t));
if (NULL != temperatureObj->instanceList) {
memset(temperatureObj->instanceList, 0, sizeof(lwm2m_list_t));
} else {
lwm2m_free(temperatureObj);
return NULL;
}
/*
* And the private function that will access the object.
* Those function will be called when a read/write/execute query is made by the server. In fact the library don't need to
* know the resources of the object, only the server does.
*/
temperatureObj->readFunc = prv_temperature_read;
temperatureObj->writeFunc = NULL;
temperatureObj->executeFunc = NULL;
temperatureObj->closeFunc = prv_temperature_close;
temperatureObj->userData = NULL;
}
return temperatureObj;
}
bool isTempSensorOpened() {
return opened;
}
float getCurrentTemp() {
return (float) sensor;
}