Sebastián Pastor / EtheriosCloudConnector
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers network_tcp.cpp Source File

network_tcp.cpp

00001 /*
00002  * Copyright (c) 2013 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "mbed.h"
00014 #include "EthernetInterface.h"
00015 #include "Socket.h"
00016 #include "TCPSocketConnection.h"
00017 
00018 extern "C" {
00019 #include "connector_api.h"
00020 #include "ecc_platform.h"
00021 }
00022 #if defined CONNECTOR_TRANSPORT_TCP
00023 
00024 
00025 TCPSocketConnection socket;
00026 
00027 static connector_callback_status_t app_network_tcp_close(connector_network_close_t * const data)
00028 {
00029     socket.close();
00030 
00031     return connector_callback_continue;
00032 }
00033 
00034 
00035 /*
00036  * This routine reads a specified number of bytes from Device Cloud.  This
00037  * function must not block. If it encounters EAGAIN  error, return
00038  * connector_callback_busy and Cloud Connector will ignore the read_data and read_length
00039  * and continue calling this function.
00040  */
00041 static connector_callback_status_t app_network_tcp_receive(connector_network_receive_t * const data)
00042 {
00043     int bytes_read;
00044     connector_callback_status_t status = connector_callback_continue;
00045     
00046     bytes_read = socket.receive((char *)data->buffer, data->bytes_available);
00047     
00048     if (bytes_read <= 0)
00049     {
00050         data->bytes_used = 0;
00051         status = connector_callback_busy;
00052     }
00053     else
00054     {
00055         data->bytes_used = bytes_read;
00056         status = connector_callback_continue;
00057     }
00058     return status;
00059 }
00060 
00061 /*
00062  * Sends data to Device Cloud, this routine must not block.  If it encounters
00063  * EAGAIN  error, return connector_callback_busy and Cloud Connector will ignore the
00064  * sent_length and continue calling this function.
00065  */
00066 static connector_callback_status_t app_network_tcp_send(connector_network_send_t * const data)
00067 {
00068     data->bytes_used = socket.send((char *)data->buffer, data->bytes_available);
00069 
00070     return data->bytes_used <= 0 ? connector_callback_busy : connector_callback_continue;
00071 }
00072 
00073 
00074 static connector_callback_status_t app_network_tcp_open(connector_network_open_t * const data)
00075 {
00076     socket.connect("login.etherios.com", CONNECTOR_PORT);
00077     socket.set_blocking(false, 500);
00078     return connector_callback_continue;
00079 }
00080 
00081 
00082 /*
00083  *  Callback routine to handle all networking related calls.
00084  */
00085 connector_callback_status_t app_network_tcp_handler(connector_request_id_network_t const request_id,
00086                                                     void * const data)
00087 {
00088     connector_callback_status_t status;
00089 
00090     switch (request_id)
00091     {
00092     case connector_request_id_network_open:
00093         status = app_network_tcp_open((connector_network_open_t *)data);
00094         break;
00095 
00096     case connector_request_id_network_send:
00097         status = app_network_tcp_send((connector_network_send_t *)data);
00098         break;
00099 
00100     case connector_request_id_network_receive:
00101         status = app_network_tcp_receive((connector_network_receive_t *)data);
00102         break;
00103 
00104     case connector_request_id_network_close:
00105         status = app_network_tcp_close((connector_network_close_t *)data);
00106         break;
00107 
00108     default:
00109         APP_DEBUG("app_network_tcp_handler: unrecognized callback request_id [%d]\n", request_id);
00110         status = connector_callback_unrecognized;
00111         break;
00112 
00113     }
00114 
00115     return status;
00116 }
00117 #endif
00118 
00119