joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbedclient.cpp Source File

mbedclient.cpp

00001 /*
00002  * Copyright (c) 2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbedclient.h"
00018 
00019 #include "mbed-client/m2minterfacefactory.h"
00020 #include "mbed-client/m2mdevice.h"
00021 #include "mbed-client/m2mobjectinstance.h"
00022 #include "mbed-client/m2mresource.h"
00023 
00024 #ifdef TARGET_LIKE_MBED
00025 #include "minar/minar.h"
00026 #include "test_env.h"
00027 #endif
00028 
00029 // Enter your mbed Device Server's IPv4 address and Port number in
00030 // mentioned format like 192.168.0.1:5693
00031 const String &BOOTSTRAP_SERVER_ADDRESS = "coap://10.45.3.10:5693";
00032 const String &BOOTSTRAP_SERVER_DTLS_ADDRESS = "coap://10.45.3.10:5694";
00033 
00034 #ifdef SIXLOWPAN_INTERFACE
00035 const String &MBED_SERVER_ADDRESS = "coap://FD00:FF1:CE0B:A5E1:1068:AF13:9B61:D557:5683";
00036 const String &MBED_SERVER_DTLS_ADDRESS = "coap://FD00:FF1:CE0B:A5E1:1068:AF13:9B61:D557:5684";
00037 #else
00038 const String &MBED_SERVER_ADDRESS = "coap://10.45.3.10:5683";
00039 //const String &MBED_SERVER_ADDRESS = "coap://10.45.0.152:5683";
00040 const String &MBED_SERVER_DTLS_ADDRESS = "coap://10.45.3.10:5684";
00041 #endif
00042 const String CLIENT_NAME = "secure-client";
00043 
00044 const String &MANUFACTURER = "ARM";
00045 const String &TYPE = "type";
00046 const String &MODEL_NUMBER = "2015";
00047 const String &SERIAL_NUMBER = "12345";
00048 const uint8_t STATIC_VALUE[] = "Static value";
00049 
00050 #ifdef TARGET_LIKE_LINUX
00051 #include <unistd.h>
00052 #else
00053 
00054 #endif
00055 
00056 MbedClient::MbedClient()
00057 #ifndef TARGET_LIKE_LINUX
00058 :_led(LED3)
00059 #endif
00060 {
00061     _security = NULL;
00062     _interface = NULL;
00063     _register_security = NULL;
00064     _device = NULL;
00065     _object = NULL;
00066     _bootstrapped = false;
00067     _error = false;
00068     _registered = false;
00069     _unregistered = false;
00070     _registration_updated = false;
00071     _value = 0;
00072 }
00073 
00074 MbedClient::~MbedClient() {
00075     if(_security) {
00076         delete _security;
00077     }
00078     if(_register_security){
00079         delete _register_security;
00080     }
00081     if(_device) {
00082         M2MDevice::delete_instance();
00083     }
00084     if(_object) {
00085         delete _object;
00086     }
00087     if(_interface) {
00088         delete _interface;
00089     }
00090 }
00091 
00092 bool MbedClient::create_interface() {
00093 
00094     M2MInterface::NetworkStack stack = M2MInterface::LwIP_IPv4;
00095     #ifdef SIXLOWPAN_INTERFACE
00096             stack = M2MInterface::Nanostack_IPv6;
00097     #endif
00098 
00099     /* From http://www.iana.org/assignments/port-numbers:
00100        "The Dynamic and/or Private Ports are those from 49152 through 65535" */
00101     srand(time(NULL));
00102     uint16_t port = (rand() % (65535-49152)) + 49152;
00103 
00104     _interface = M2MInterfaceFactory::create_interface(*this,
00105                                               CLIENT_NAME,
00106                                               "test",
00107                                               60,
00108                                               port,
00109                                               "",
00110                                               M2MInterface::UDP,
00111                                               stack,
00112                                               "");
00113 
00114     return (_interface == NULL) ? false : true;
00115 }
00116 
00117 bool MbedClient::bootstrap_successful() {
00118 #ifdef TARGET_LIKE_LINUX
00119     while(!_bootstrapped && !_error) {
00120         sleep(1);
00121     }
00122 #endif
00123     return _bootstrapped;
00124 }
00125 
00126 bool MbedClient::register_successful() {
00127 #ifdef TARGET_LIKE_LINUX
00128     while(!_registered && !_error) {
00129         sleep(1);
00130     }
00131 #endif
00132     return _registered;
00133 }
00134 
00135 bool MbedClient::unregister_successful() {
00136 #ifdef TARGET_LIKE_LINUX
00137     while(!_unregistered && !_error) {
00138         sleep(1);
00139     }
00140 #endif
00141     return _unregistered;
00142 }
00143 
00144 bool MbedClient::registration_update_successful() {
00145 #ifdef TARGET_LIKE_LINUX
00146     while(!_registration_updated && !_error) {
00147         sleep(1);
00148     }
00149 #endif
00150     return _registration_updated;
00151 }
00152 
00153 M2MSecurity* MbedClient::create_bootstrap_object(bool useSecureConnection) {
00154     // Creates bootstrap server object with Bootstrap server address and other parameters
00155     // required for client to connect to bootstrap server.
00156     M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::Bootstrap);
00157     if(security) {
00158         if(useSecureConnection){
00159             security->set_resource_value(M2MSecurity::M2MServerUri, BOOTSTRAP_SERVER_DTLS_ADDRESS);
00160             //TODO: remove these, when bootstrapping server supports DTLS
00161             delete security;
00162             return NULL;
00163         }else{
00164             security->set_resource_value(M2MSecurity::M2MServerUri, BOOTSTRAP_SERVER_ADDRESS);
00165             //TODO: refactor this out
00166             security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity);
00167         }
00168     }
00169     return security;
00170 }
00171 
00172 M2MSecurity* MbedClient::create_register_object(bool useSecureConnection) {
00173     // Creates bootstrap server object with Bootstrap server address and other parameters
00174     // required for client to connect to bootstrap server.
00175     M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
00176     if(security) {
00177         if(useSecureConnection){
00178             security->set_resource_value(M2MSecurity::M2MServerUri, MBED_SERVER_DTLS_ADDRESS);
00179         }else{
00180             security->set_resource_value(M2MSecurity::M2MServerUri, MBED_SERVER_ADDRESS);
00181         }
00182     }
00183     return security;
00184 }
00185 
00186 void MbedClient::test_bootstrap(M2MSecurity *security) {
00187     if(_interface) {
00188          // Bootstrap function.
00189         _interface->bootstrap(security);
00190     }
00191 }
00192 
00193 M2MDevice* MbedClient::create_device_object() {
00194     // Creates device object which contains mandatory resources linked with
00195     // device endpoint.
00196     M2MDevice *device = M2MInterfaceFactory::create_device();
00197     if(device) {
00198         device->create_resource(M2MDevice::Manufacturer,MANUFACTURER);
00199         device->create_resource(M2MDevice::DeviceType,TYPE);
00200         device->create_resource(M2MDevice::ModelNumber,MODEL_NUMBER);
00201         device->create_resource(M2MDevice::SerialNumber,SERIAL_NUMBER);
00202     }
00203     return device;
00204 }
00205 
00206 void MbedClient::execute_function(void *argument) {
00207 #ifdef TARGET_LIKE_LINUX
00208     if(argument) {
00209         char* arguments = (char*)argument;
00210         printf("Received %s!!\n", arguments);
00211     }
00212     printf("I am executed !!\n");
00213 #else
00214     _led == 0 ? _led = 1 : _led = 0;
00215 #endif
00216 }
00217 
00218 M2MObject* MbedClient::create_generic_object() {
00219     _object = M2MInterfaceFactory::create_object("Test");
00220     if(_object) {
00221         M2MObjectInstance* inst = _object->create_object_instance();
00222         if(inst) {
00223             inst->set_operation(M2MBase::GET_ALLOWED);
00224 
00225             M2MResource* res = inst->create_dynamic_resource("Dynamic",
00226                                                              "ResourceTest",
00227                                                              M2MResourceInstance::INTEGER,
00228                                                              true);
00229 
00230             char buffer[20];
00231             int size = sprintf(buffer,"%d",_value);
00232             res->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
00233             res->set_value((const uint8_t*)buffer,
00234                            (const uint32_t)size);
00235             res->set_execute_function(execute_callback (this,&MbedClient::execute_function));
00236             _value++;
00237 
00238             inst->create_static_resource("Static",
00239                                          "ResourceTest",
00240                                          M2MResourceInstance::STRING,
00241                                          STATIC_VALUE,
00242                                          sizeof(STATIC_VALUE)-1);
00243         }
00244     }
00245     return _object;
00246 }
00247 
00248 void MbedClient::update_resource() {
00249     if(_object) {
00250         M2MObjectInstance* inst = _object->object_instance();
00251         if(inst) {
00252             M2MResource* res = inst->resource("Dynamic");
00253             if( res ){
00254                 char buffer[20];
00255                 int size = sprintf(buffer,"%d",_value);
00256                 res->set_value((const uint8_t*)buffer,
00257                                (const uint32_t)size);
00258                 _value++;
00259             }
00260         }
00261     }
00262 }
00263 
00264 void MbedClient::test_register(M2MObjectList object_list){
00265     if(_interface) {
00266         _interface->register_object(_register_security, object_list);
00267     }
00268 }
00269 
00270 void MbedClient::set_register_object(M2MSecurity *&register_object){
00271     if(_register_security) {
00272         delete _register_security;
00273         _register_security = NULL;
00274     }
00275     _register_security = register_object;
00276 
00277 }
00278 
00279 void MbedClient::test_update_register() {
00280     uint32_t updated_lifetime = 20;
00281     _registered = false;
00282     _unregistered = false;
00283     if(_interface){
00284         _interface->update_registration(_register_security,updated_lifetime);
00285     }
00286 }
00287 
00288 void MbedClient::test_unregister() {
00289     if(_interface) {
00290         _interface->unregister_object(NULL);
00291     }
00292 }
00293 
00294 void MbedClient::bootstrap_done(M2MSecurity *server_object){
00295     if(server_object) {
00296         set_register_object(server_object);
00297         _bootstrapped = true;
00298         _error = false;
00299         printf("\nBootstrapped\n");
00300     }
00301 }
00302 
00303 void MbedClient::object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){
00304     _registered = true;
00305     _unregistered = false;
00306     printf("\nRegistered\n");
00307 }
00308 
00309 void MbedClient::object_unregistered(M2MSecurity */*server_object*/){
00310     _unregistered = true;
00311     _registered = false;
00312 
00313 #ifdef TARGET_LIKE_MBED
00314     notify_completion(_unregistered);
00315     minar::Scheduler::stop();
00316 #endif
00317     printf("\nUnregistered\n");
00318 }
00319 
00320 void MbedClient::registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){
00321     _registration_updated = true;
00322     printf("\nregistration updated\n");
00323 }
00324 
00325 void MbedClient::error(M2MInterface::Error error){
00326     _error = true;
00327     printf("\nError occured Error code %d\n", (int)error);
00328 }
00329 
00330 void MbedClient::value_updated(M2MBase *base, M2MBase::BaseType type) {
00331     printf("\nValue updated of Object name %s and Type %d\n",
00332            base->name().c_str(), type);
00333 }