![](/media/cache/profiles/debfdc81854eac26ec993b55a659c6e1.jpg.50x50_q85.jpg)
mbed client lightswitch demo
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
Diff: simple_client.h
- Revision:
- 11:cada08fc8a70
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/simple_client.h Thu Jun 09 17:08:36 2016 +0000 @@ -0,0 +1,337 @@ +/* + * Copyright (c) 2015 ARM Limited. All rights reserved. + * 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. + */ + + #ifndef __SIMPLE_CLIENT_H__ + #define __SIMPLE_CLIENT_H__ + + #include "mbed.h" + #include "security.h" + #include "mbed-client/m2minterfacefactory.h" +#include "mbed-client/m2mdevice.h" +#include "mbed-client/m2minterfaceobserver.h" +#include "mbed-client/m2minterface.h" +#include "mbed-client/m2mobjectinstance.h" +#include "mbed-client/m2mresource.h" +//#include "mbed-client-classic/m2mnetwork.h" + +extern Serial output; +DigitalOut _relay(D5); +DigitalOut status_led(LED1); + +// This is address to mbed Device Connector +const String &MBED_SERVER_ADDRESS = "coap://api.connector.mbed.com:5684"; + +const uint8_t STATIC_VALUE[] = "Living Room Outlet"; +const String &MBED_USER_NAME_DOMAIN = MBED_DOMAIN; +const String &ENDPOINT_NAME = MBED_ENDPOINT_NAME; +const String &MANUFACTURER = "manufacturer"; +const String &TYPE = "connected-outlet"; +const String &MODEL_NUMBER = "2015"; +const String &SERIAL_NUMBER = "12345"; + +//Select binding mode: UDP or TCP +M2MInterface::BindingMode SOCKET_MODE = M2MInterface::UDP; + +class MbedClient: public M2MInterfaceObserver { +public: + MbedClient(){ + _interface = NULL; + _bootstrapped = false; + _error = false; + _registered = false; + _unregistered = false; + _register_security = NULL; + _value = 0; + _object = NULL; + } + + ~MbedClient() { + if(_interface) { + delete _interface; + } + if(_register_security){ + delete _register_security; + } + } + + void trace_printer(const char* str) { + output.printf("\r\n%s\r\n", str); + } + + void create_interface() { + // Creates M2MInterface using which endpoint can + // setup its name, resource type, life time, connection mode, + // Currently only LwIPv4 is supported. + + // Randomizing listening port for Certificate mode connectivity + srand(time(NULL)); + uint16_t port = rand() % 65535 + 12345; + + _interface = M2MInterfaceFactory::create_interface(*this, + ENDPOINT_NAME, + TYPE, + 3600, + port, + MBED_USER_NAME_DOMAIN, + SOCKET_MODE, + M2MInterface::LwIP_IPv4, + ""); + } + + bool register_successful() { + return _registered; + } + + bool unregister_successful() { + return _unregistered; + } + + M2MSecurity* create_register_object() { + // Creates register server object with mbed device server address and other parameters + // required for client to connect to mbed device server. + M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); + if(security) { + security->set_resource_value(M2MSecurity::M2MServerUri, MBED_SERVER_ADDRESS); + security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); + security->set_resource_value(M2MSecurity::ServerPublicKey,SERVER_CERT,sizeof(SERVER_CERT)); + security->set_resource_value(M2MSecurity::PublicKey,CERT,sizeof(CERT)); + security->set_resource_value(M2MSecurity::Secretkey,KEY,sizeof(KEY)); + } + return security; + } + + M2MDevice* create_device_object() { + // Creates device object which contains mandatory resources linked with + // device endpoint. + M2MDevice *device = M2MInterfaceFactory::create_device(); + if(device) { + device->create_resource(M2MDevice::Manufacturer,MANUFACTURER); + device->create_resource(M2MDevice::DeviceType,TYPE); + device->create_resource(M2MDevice::ModelNumber,MODEL_NUMBER); + device->create_resource(M2MDevice::SerialNumber,SERIAL_NUMBER); + } + return device; + } + + // PUT callback FN + void Ecallback(void *input){ + //output.printf("Put Triggered!\n"); + //output.printf("\tData: %d, 0x%x, %f, '%s'\n",*(int *)input, *(int *)input , *(float *)input, (char *)input); + int x = *(char *)input; + if(x == '1'){ + status_led = 0; + _relay = 1; + } else{ + status_led = 1; + _relay = 0; + } + //status_led = x; + //status_led = !status_led; + //_relay = !_relay; + //_relay = (int *)input; + } + + M2MObject* create_generic_object() { + _object = M2MInterfaceFactory::create_object("Test"); + if(_object) { + M2MObjectInstance* inst = _object->create_object_instance(); + if(inst) { + M2MResource* res = inst->create_dynamic_resource("E", + "LightControl", + M2MResourceInstance::INTEGER, + true); + char buffer[20]; + int size = sprintf(buffer,"%d",_value); + res->set_operation(M2MBase::GET_PUT_POST_ALLOWED); + res->set_value((const uint8_t*)buffer, + (const uint32_t)size); + //res->set_execute_function(&Ecallback); + res->set_execute_function(execute_callback(this, &MbedClient::Ecallback)); + _value++; + + inst->create_static_resource("N", + "Name", + M2MResourceInstance::STRING, + STATIC_VALUE, + sizeof(STATIC_VALUE)-1); + + M2MResource* res2 = inst->create_dynamic_resource("C", + "effective-current-ma", + M2MResourceInstance::INTEGER, + true); + char buffer2[20]; + int size2 = sprintf(buffer2,"%d",_value2); + res2->set_operation(M2MBase::GET_ALLOWED); + res2->set_value((const uint8_t*)buffer2, + (const uint32_t)size2); + } + } + return _object; + } + + void updateCurrent(float current_ma){ + if(_object) { + M2MObjectInstance* inst = _object->object_instance(); + if(inst) { + M2MResource* res = inst->resource("C"); + + char buffer[20]; + int x = current_ma; + int size = sprintf(buffer,"%f",current_ma); + res->set_value((const uint8_t*)buffer, + (const uint32_t)size); + _value2 = current_ma; + } + } + } + + void update_resource() { + if(_object) { + M2MObjectInstance* inst = _object->object_instance(); + if(inst) { + M2MResource* res = inst->resource("D"); + + char buffer[20]; + int size = sprintf(buffer,"%d",_value); + res->set_value((const uint8_t*)buffer, + (const uint32_t)size); + _value++; + } + } + } + + void test_register(M2MSecurity *register_object, M2MObjectList object_list){ + if(_interface) { + // Register function + _interface->register_object(register_object, object_list); + } + } + + void test_unregister() { + if(_interface) { + // Unregister function + _interface->unregister_object(NULL); + } + } + + //Callback from mbed client stack when the bootstrap + // is successful, it returns the mbed Device Server object + // which will be used for registering the resources to + // mbed Device server. + void bootstrap_done(M2MSecurity *server_object){ + if(server_object) { + _bootstrapped = true; + _error = false; + trace_printer("\nBootstrapped\n"); + } + } + + //Callback from mbed client stack when the registration + // is successful, it returns the mbed Device Server object + // to which the resources are registered and registered objects. + void object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){ + _registered = true; + _unregistered = false; + trace_printer("\nRegistered\n"); + } + + //Callback from mbed client stack when the unregistration + // is successful, it returns the mbed Device Server object + // to which the resources were unregistered. + void object_unregistered(M2MSecurity */*server_object*/){ + _unregistered = true; + _registered = false; + trace_printer("\nUnregistered\n"); + } + + void registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){ + } + + //Callback from mbed client stack if any error is encountered + // during any of the LWM2M operations. Error type is passed in + // the callback. + void error(M2MInterface::Error error){ + _error = true; + switch(error){ + case M2MInterface::AlreadyExists: + trace_printer("[ERROR:] M2MInterface::AlreadyExists\n"); + break; + case M2MInterface::BootstrapFailed: + trace_printer("[ERROR:] M2MInterface::BootstrapFailed\n"); + break; + case M2MInterface::InvalidParameters: + trace_printer("[ERROR:] M2MInterface::InvalidParameters\n"); + break; + case M2MInterface::NotRegistered: + trace_printer("[ERROR:] M2MInterface::NotRegistered\n"); + break; + case M2MInterface::Timeout: + trace_printer("[ERROR:] M2MInterface::Timeout\n"); + break; + case M2MInterface::NetworkError: + trace_printer("[ERROR:] M2MInterface::NetworkError\n"); + break; + case M2MInterface::ResponseParseFailed: + trace_printer("[ERROR:] M2MInterface::ResponseParseFailed\n"); + break; + case M2MInterface::UnknownError: + trace_printer("[ERROR:] M2MInterface::UnknownError\n"); + break; + case M2MInterface::MemoryFail: + trace_printer("[ERROR:] M2MInterface::MemoryFail\n"); + break; + case M2MInterface::NotAllowed: + trace_printer("[ERROR:] M2MInterface::NotAllowed\n"); + break; + default: + break; + } + } + + //Callback from mbed client stack if any value has changed + // during PUT operation. Object and its type is passed in + // the callback. + void value_updated(M2MBase *base, M2MBase::BaseType type) { + output.printf("\nValue updated of Object name %s and Type %d\n", + base->name().c_str(), type); + } + + void test_update_register() { + if (_registered) { + _interface->update_registration(_register_security, 3600); + } + } + + void set_register_object(M2MSecurity *register_object) { + if (_register_security == NULL) { + _register_security = register_object; + } + } + +private: + + M2MInterface *_interface; + M2MSecurity *_register_security; + M2MObject *_object; + volatile bool _bootstrapped; + volatile bool _error; + volatile bool _registered; + volatile bool _unregistered; + int _value; + int _value2; +}; + +#endif // __SIMPLE_CLIENT_H__ \ No newline at end of file