forked from ARMmbed/mbed-ethernet-sample-kone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
148 lines (121 loc) · 5.82 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
/**
* @file main.cpp
* @brief mbed Endpoint Sample main
* @author Doug Anson
* @version 1.0
* @see
*
* Copyright (c) 2014
*
* 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.
*/
// CUSTOMIZE ME: Define the core Device Types, Firmware, Hardware, Software information
#define ENABLE_DEVICE_MANAGER true // true - enable, false - disable
#define MY_DEVICE_MFG "NXP"
#define MY_DEVICE_TYPE "mbed-endpoint"
#define MY_DEVICE_MODEL "K64F"
#define MY_DEVICE_SERIAL "0123456789"
#define MY_FIRMWARE_VERSION "1.0.0"
#define MY_HARDWARE_VERSION "1.0.0"
#define MY_SOFTWARE_VERSION "1.0.0"
// Passphrase to supply for data management authentication
#define MY_DM_PASSPHRASE "arm1234"
// Include security.h
#include "security.h"
// mbed Endpoint Network
#include "mbed-connector-interface/mbedEndpointNetwork.h"
// Logger
#include "mbed-connector-interface/Logger.h"
Serial pc(USBTX,USBRX);
Logger logger(&pc);
// ConnectionHandler support
#define ENABLE_CONNECTION_HANDLER true // true - enable, false - disable
#include "ConnectionHandler.h"
// Include the default Device Management Responders
#include "dm-responders/ResponderFunctions.h"
// Our Device Management Authenticator (trivial passphrase authenticator used)
#include "mbed-connector-interface/PassphraseAuthenticator.h"
// Our Device Management Responder
#include "mbed-connector-interface/DeviceManagementResponder.h"
// Our Device Manager
#include "mbed-connector-interface/DeviceManager.h"
// Sample Static Resource
#include "mbed-connector-interface/StaticResource.h"
StaticResource static_sample(&logger,"101","1010","hello mbed");
// Sample Dynamic Resource (a counter)
#include "mbed-endpoint-resources/SampleDynamicResource.h"
SampleDynamicResource sample_counter(&logger,"123","4567",true); // "true" -> resource is observable
// Light Resource
#include "mbed-endpoint-resources/LightResource.h"
LightResource light(&logger,"311","5850");
// Accelerometer Resource
#include "mbed-endpoint-resources/AccelerometerResource.h"
AccelerometerResource accel(&logger,"888","7700",true); // "true" --> resource is observable
// called from the Endpoint::start() below to create resources and the endpoint internals...
Connector::Options *configure_endpoint(Connector::OptionsBuilder &config)
{
// Build the endpoint configuration parameters
logger.log("Endpoint::main (%s): customizing endpoint configuration...",net_get_type());
return config
// PROVISIONING: set the Provisioning Credentials (all from security.h)
.setEndpointNodename(MBED_ENDPOINT_NAME)
.setDomain(MBED_DOMAIN)
.setEndpointType(MY_DEVICE_TYPE)
.setServerCertificate((uint8_t *)SERVER_CERT,(int)sizeof(SERVER_CERT))
.setClientCertificate((uint8_t *)CERT,(int)sizeof(CERT))
.setClientKey((uint8_t *)KEY,(int)sizeof(KEY))
// WiFi Setup (must set "network-interface" to "WIFI" in mbed_app.json)
.setWiFiSSID((char *)"changeme") // WiFi: SSID
.setWiFiAuthType(WIFI_WPA2_PERSONAL) // WiFi: Auth Type
.setWiFiAuthKey((char *)"changeme") // WiFi: WPA Password
// add a Sample Static Resource
.addResource(&static_sample)
// add a Sample Counter (Dynamic Resource)
.addResource(&sample_counter,10000) // observe every 10 seconds
// Add my specific physical dynamic resources...
.addResource(&light)
.addResource(&accel,(bool)false) // on demand observation
// finalize the configuration...
.build();
}
// main entry point...
int main()
{
// set Serial
pc.baud(115200);
// Announce
logger.log("\r\n\r\nmbed Connector Sample Endpoint - KONE (%s)",net_get_type());
// Configure Device Manager (if enabled)
DeviceManager *device_manager = NULL;
if (ENABLE_DEVICE_MANAGER) {
// Allocate the Device Management Components
PassphraseAuthenticator *authenticator = new PassphraseAuthenticator(&logger,MY_DM_PASSPHRASE);
DeviceManagementResponder *dm_processor = new DeviceManagementResponder(&logger,authenticator);
device_manager = new DeviceManager(&logger,dm_processor,MY_DEVICE_MFG,MY_DEVICE_TYPE,MY_DEVICE_MODEL,MY_DEVICE_SERIAL,MY_FIRMWARE_VERSION,MY_HARDWARE_VERSION,MY_SOFTWARE_VERSION);
// Register the default Device Management Responders
dm_processor->setInitializeHandler(dm_initialize);
dm_processor->setRebootResponderHandler(dm_reboot_responder);
dm_processor->setResetResponderHandler(dm_reset_responder);
dm_processor->setFOTAManifestHandler(dm_set_manifest);
dm_processor->setFOTAImageHandler(dm_set_fota_image);
dm_processor->setFOTAInvocationHandler(dm_invoke_fota);
}
// we have to plumb our network first
Connector::Endpoint::plumbNetwork((void *)device_manager);
// Set our ConnectionHandler instance (after plumbing the network...)
if (ENABLE_CONNECTION_HANDLER) {
Connector::Endpoint::setConnectionStatusInterface(new ConnectionHandler());
}
// starts the endpoint by finalizing its configuration (configure_endpoint() above called),creating a Thread and reading mbed Cloud events...
Connector::Endpoint::start();
}