# Code coverage report for lib/iotagent-lwm2m-client.js

## Statements: 75.61% (62 / 82) Branches: 50% (7 / 14) Functions: 73.68% (14 / 19) Lines: 75.61% (62 / 82)

All files » lib/ » iotagent-lwm2m-client.js

    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
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272

    1

    1

    1
    21

    1
    21

    1
    21
    21

    21

    21

    21
    21

    21

    1
    21
    20
    20

    20
    20
    20

    20

    1
    21
    6

    6
    4

    6

    21

    21

    1
    21

    21

    1
    21

    21
    21
    21

    21

    1
    21

    21

    21
    21

    21

    1
    20

    20

    20
    20

    20

    20

    1

    1

    1
    1
    1
    1
    1
    1
    1
    1

     /*
     * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
     *
     * This file is part of iotagent-lwm2m-lib
     *
     * iotagent-lwm2m-lib is free software: you can redistribute it and/or
     * modify it under the terms of the GNU Affero General Public License as
     * published by the Free Software Foundation, either version 3 of the License,
     * or (at your option) any later version.
     *
     * iotagent-lwm2m-lib is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     * See the GNU Affero General Public License for more details.
     *
     * You should have received a copy of the GNU Affero General Public
     * License along with iotagent-lwm2m-lib.
     * If not, seehttp://www.gnu.org/licenses/.
     *
     * For those usages not covered by the GNU Affero General Public License
     * please contact with::[contacto@tid.es]
     */

    'use strict';

    var objRegistry = require('./services/client/objectRegistry'),
        coap = require('coap'),
        errors = require('./errors'),
        async = require('async'),
        apply = async.apply,
        readService = require('./services/client/read'),
        coapRouter = require('./services/coapRouter'),
        Readable = require('stream').Readable,
        logger = require('logops'),
        config = require('../config'),
        context = {
            op: 'LWM2MLib.Client'
        };

    /**
     * Load the internal handlers for each kind of operation. Each handler is implemented in a separated module. This
     * module will be, in time, in charge of executing the user handler for that operation with all the data extracted
     * from the request (and completed with internal data if needed).
     *
     * @param {Object} serverInfo      Object containing all the information of the current server.
     */
    function loadHandlers(serverInfo) {
        serverInfo.handlers = {
            write: {
                lib: require('./services/client/write').handle,
                user: coapRouter.defaultHandler
            },
            read: {
                lib: readService.handle,
                user: coapRouter.defaultHandler
            }
        };
    }

    /**
     * Load the tables of available routes. For each route, the method, a regexp for the path and the name of the operation
     * is indicated (the name of the operation will be used to select the internal and user handlers to execute for each
     * route).
     *
     * @param {Object} serverInfo      Object containing all the information of the current server.
     */
    function loadRoutes(serverInfo) {
        serverInfo.routes = [
            ['PUT', /(\/\d+)+/, 'write'],
            ['GET', /(\/\d+)+/, 'read']
        ];
    }

    function startListener(host, port, callback) {
        coapRouter.start(config.client, function (error, serverInfo) {
            Iif (error) {
                logger.error(context, 'Failed to start COAP Router for client.');

                callback(error);
            } else {
                logger.debug(context, 'COAP Router started successfully');

                var deviceInformation = {
                    currentHost: host,
                    currentPort: port,
                    location: '',
                    serverInfo: serverInfo
                };

                loadHandlers(deviceInformation.serverInfo);
                loadRoutes(deviceInformation.serverInfo);

                callback(null, deviceInformation);
            }
        });
    }

    function createRegisterResponseHandler(deviceInformation, callback) {
        return function responseHandler(res) {
            Eif (res.code === '2.01') {
                logger.debug(context, 'Registration succeeded to host [%s] and port [%s]',
                    deviceInformation.host, deviceInformation.port);

                for (var i = 0; i < res.options.length; i++) {
                    Eif (res.options[i].name === 'Location-Path') {
                        deviceInformation.location = res.options[i].value;
                    }
                }

                callback(null, deviceInformation);
            } else {
                logger.error(context, 'Registration failed with code: ' + res.code);
                callback(new errors.RegistrationFailed(res.code));
            }
        };
    }

    /**
     * Creates a COAP Text representation of the objects passed as a parameter.
     *
     * @param {Array} objects       Array containing LWM2M Object instances.
     */
    function generatePayload(objects, innerCallback) {
        var result = objects.reduce(function(previous, current, index, array) {
            var result = previous + '<' + current.objectUri + '>';

            if (index !== array.length -1) {
                result += ',';
            }

            return result;
        }, '');

        logger.debug(context, 'Object list generated:\n%s', objects);

        innerCallback(null, result);
    }

    /**
     * Register the client in the Lightweight M2M Server in the seleted host and port, with the given endpoint name. If the
     * registration is successful, a deviceInformation object is returned with the host and port of the connected server
     * and the device location in that server (usually with the form '/rd/<deviceId>').
     *
     * @param {String} host                  Host of the LWTM2M Server
     * @param {String} port                  Port of the LWTM2M Server
     * @param {String} url                   URL of the LWTM2M Server (optional)
     * @param {String} endpointName          Name the client will be registered under
     */
    function register(host, port, url, endpointName, callback) {
        var rs = new Readable(),
            errorEmitted = false,
            creationRequest =  {
                host: host,
                port: port,
                method: 'POST',
                pathname: ((url)? url : '') + '/rd',
                query: 'ep=' +  endpointName + '&lt=' + config.client.lifetime + '&lwm2m=' + config.client.version + '&b=U'
            },
            req = coap.request(creationRequest);

        Iif (config.logLevel) {
            logger.setLevel(config.client.logLevel);
        }

        function sendRequest(payload, innerCallback) {
            logger.debug(context, 'Sending registration request');

            rs.push(payload);
            rs.push(null);
            rs.pipe(req);

            innerCallback(null);
        }

        function registerResponseListener(deviceInformation, innerCallback) {
            req.on('response', createRegisterResponseHandler(deviceInformation, callback));

            innerCallback();
        }

        startListener(host, port, function (error, deviceInformation) {
            async.waterfall([
                apply(registerResponseListener, deviceInformation),
                objRegistry.list,
                generatePayload,
                sendRequest
            ]);

            req.on('error', function (error) {
                logger.error(context, 'Registration response finished with an error:\n%s', error);

                coapRouter.stop(deviceInformation.serverInfo, function (error) {
                    logger.error('COAP Router stopped with result: ' + error);
                });

                if (!errorEmitted) {
                    errorEmitted = true;
                    callback(new errors.ServerNotFound(host + ':' + port));
                }
            });
        });
    }

    /**
     * Unregisters the client from the given server.
     *
     * @param {Object} deviceInformation        Device information object retrieved during the connection
     */
    function unregister(deviceInformation, callback) {
        var creationRequest =  {
                host: deviceInformation.currentHost,
                port: deviceInformation.currentPort,
                method: 'DELETE',
                pathname: deviceInformation.location,
                agent: false
            },
            req = coap.request(creationRequest);

        logger.debug(context, 'Unregistration request:\n%s', JSON.stringify(creationRequest, null, 4));

        req.on('response', function(res) {
            logger.debug(context, 'Unregistration response code:\n%s', res.code);

            coapRouter.stop(deviceInformation.serverInfo, callback);
        });

        req.end();
    }

    function updateRegistration(deviceInformation, callback) {
        var rs = new Readable(),
            creationRequest =  {
                host: deviceInformation.currentHost,
                port: deviceInformation.currentPort,
                method: 'PUT',
                pathname: deviceInformation.location,
                query: 'lt=' + config.client.lifetime + '&lwm2m=' + config.client.version + '&b=U'
            },
            req = coap.request(creationRequest);

        logger.debug(context, 'Update registration request:\n%s', JSON.stringify(creationRequest, null, 4));

        function sendRequest(payload, callback) {
            rs.push(payload);
            rs.push(null);
            rs.pipe(req);
        }

        req.on('response', function(res) {
            logger.debug(context, 'Update registration response code:\n%s', res.code);

            callback(null, deviceInformation);
        });

        async.waterfall([
            objRegistry.list,
            generatePayload,
            sendRequest
        ]);
    }

    exports.registry = objRegistry;
    exports.register = register;
    exports.unregister = unregister;
    exports.update = updateRegistration;
    exports.setHandler = coapRouter.setHandler;
    exports.cancelObserver = readService.cancel;
    exports.cancellAllObservers = readService.cancelAll;
    exports.listObservers = readService.list;