forked from PelionIoT/mbed-cloud-client-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
232 lines (196 loc) · 8.38 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// ----------------------------------------------------------------------------
// Copyright 2016-2018 ARM Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------
#include "simplem2mclient.h"
#ifdef TARGET_LIKE_MBED
#include "mbed.h"
#endif
#include "application_init.h"
#include "mcc_common_button_and_led.h"
#include "blinky.h"
#ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
#include "certificate_enrollment_user_cb.h"
#endif
// event based LED blinker, controlled via pattern_resource
static Blinky blinky;
static void main_application(void);
int i = 0;
int main(void)
{
mcc_platform_run_program(main_application);
}
// Pointers to the resources that will be created in main_application().
static M2MResource* button_res;
static M2MResource* pattern_res;
static M2MResource* blink_res;
// Pointer to mbedClient, used for calling close function.
static SimpleM2MClient *client;
void pattern_updated(const char *)
{
printf("PUT received, new value: %s\n", pattern_res->get_value_string().c_str());
}
void blinky_completed(void)
{
printf("Blinky completed \n");
// Send response to backend
blink_res->send_delayed_post_response();
}
void blink_callback(void *)
{
String pattern_string = pattern_res->get_value_string();
const char *pattern = pattern_string.c_str();
printf("LED pattern = %s\n", pattern);
// The pattern is something like 500:200:500, so parse that.
// LED blinking is done while parsing.
const bool restart_pattern = false;
if (blinky.start((char*)pattern_res->value(), pattern_res->value_length(), restart_pattern, blinky_completed) == false) {
printf("out of memory error\n");
}
}
void button_status_callback(const M2MBase& object,
const M2MBase::MessageDeliveryStatus status,
const M2MBase::MessageType /*type*/)
{
switch(status) {
case M2MBase::MESSAGE_STATUS_BUILD_ERROR:
printf("Message status callback: (%s) error when building CoAP message\n", object.uri_path());
break;
case M2MBase::MESSAGE_STATUS_RESEND_QUEUE_FULL:
printf("Message status callback: (%s) CoAP resend queue full\n", object.uri_path());
break;
case M2MBase::MESSAGE_STATUS_SENT:
printf("Message status callback: (%s) Message sent to server\n", object.uri_path());
break;
case M2MBase::MESSAGE_STATUS_DELIVERED:
printf("Message status callback: (%s) Message delivered\n", object.uri_path());
break;
case M2MBase::MESSAGE_STATUS_SEND_FAILED:
printf("Message status callback: (%s) Message sending failed\n", object.uri_path());
break;
case M2MBase::MESSAGE_STATUS_SUBSCRIBED:
printf("Message status callback: (%s) subscribed\n", object.uri_path());
break;
case M2MBase::MESSAGE_STATUS_UNSUBSCRIBED:
printf("Message status callback: (%s) subscription removed\n", object.uri_path());
break;
case M2MBase::MESSAGE_STATUS_REJECTED:
printf("Message status callback: (%s) server has rejected the message\n", object.uri_path());
break;
default:
break;
}
}
// This function is called when a POST request is received for resource 5000/0/1.
void unregister(void *)
{
printf("Unregister resource executed\n");
client->close();
}
// This function is called when a POST request is received for resource 5000/0/2.
void factory_reset(void *)
{
printf("Factory reset resource executed\n");
client->close();
kcm_status_e kcm_status = kcm_factory_reset();
if (kcm_status != KCM_STATUS_SUCCESS) {
printf("Failed to do factory reset - %d\n", kcm_status);
} else {
printf("Factory reset completed. Now restart the device\n");
}
}
void main_application(void)
{
#if defined(__linux__) && (MBED_CONF_MBED_TRACE_ENABLE == 0)
// make sure the line buffering is on as non-trace builds do
// not produce enough output to fill the buffer
setlinebuf(stdout);
#endif
// Initialize trace-library first
if (application_init_mbed_trace() != 0) {
printf("Failed initializing mbed trace\n" );
return;
}
// Initialize storage
if (mcc_platform_storage_init() != 0) {
printf("Failed to initialize storage\n" );
return;
}
// Initialize platform-specific components
if(mcc_platform_init() != 0) {
printf("ERROR - platform_init() failed!\n");
return;
}
// Print platform information
mcc_platform_sw_build_info();
// Print some statistics of the object sizes and their heap memory consumption.
// NOTE: This *must* be done before creating MbedCloudClient, as the statistic calculation
// creates and deletes M2MSecurity and M2MDevice singleton objects, which are also used by
// the MbedCloudClient.
#ifdef MBED_HEAP_STATS_ENABLED
print_m2mobject_stats();
#endif
// SimpleClient is used for registering and unregistering resources to a server.
SimpleM2MClient mbedClient;
// application_init() runs the following initializations:
// 1. platform initialization
// 2. print memory statistics if MBED_HEAP_STATS_ENABLED is defined
// 3. FCC initialization.
if (!application_init()) {
printf("Initialization failed, exiting application!\n");
return;
}
// Save pointer to mbedClient so that other functions can access it.
client = &mbedClient;
#ifdef MBED_HEAP_STATS_ENABLED
printf("Client initialized\r\n");
print_heap_stats();
#endif
#ifdef MBED_STACK_STATS_ENABLED
print_stack_statistics();
#endif
// Create resource for button count. Path of this resource will be: 3200/0/5501.
button_res = mbedClient.add_cloud_resource(3200, 0, 5501, "button_resource", M2MResourceInstance::INTEGER,
M2MBase::GET_ALLOWED, 0, true, NULL, (void*)button_status_callback);
// Create resource for led blinking pattern. Path of this resource will be: 3201/0/5853.
pattern_res = mbedClient.add_cloud_resource(3201, 0, 5853, "pattern_resource", M2MResourceInstance::STRING,
M2MBase::GET_PUT_ALLOWED, "500:500:500:500", false, (void*)pattern_updated, NULL);
// Create resource for starting the led blinking. Path of this resource will be: 3201/0/5850.
blink_res = mbedClient.add_cloud_resource(3201, 0, 5850, "blink_resource", M2MResourceInstance::STRING,
M2MBase::POST_ALLOWED, "", false, (void*)blink_callback, (void*)button_status_callback);
// Use delayed response
blink_res->set_delayed_response(true);
// Create resource for unregistering the device. Path of this resource will be: 5000/0/1.
mbedClient.add_cloud_resource(5000, 0, 1, "unregister", M2MResourceInstance::STRING,
M2MBase::POST_ALLOWED, NULL, false, (void*)unregister, NULL);
// Create resource for running factory reset for the device. Path of this resource will be: 5000/0/2.
mbedClient.add_cloud_resource(5000, 0, 2, "factory_reset", M2MResourceInstance::STRING,
M2MBase::POST_ALLOWED, NULL, false, (void*)factory_reset, NULL);
mbedClient.register_and_connect();
#ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
// Add certificate renewal callback
mbedClient.get_cloud_client().on_certificate_renewal(certificate_renewal_cb);
#endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
// Check if client is registering or registered, if true sleep and repeat.
while (mbedClient.is_register_called()) {
static int button_count = 0;
mcc_platform_do_wait(100);
if (mcc_platform_button_clicked()) {
button_res->set_value(++button_count);
}
}
// Client unregistered, exit program.
}