Etherios Cloud Connector very first porting for mbed. Tested in an LPC1768
Etherios Cloud Connector for Embedded v2.1.0.3 library for mbed. Early porting.
This port is centered mainly in the platform code. So it should work properly with the provided examples of send_data, device_request, data_points, RCI and firmware_update (stub implementation, not a real one... yet ;-)). Filesystem is not implemented yet, and some examples might need changes.
To run, it needs the following libraries: - mbed - mbed-rtos - EthernetInterface
Find more information (and the source code!) about Etherios Cloud Connector for Embedded here: http://www.etherios.com/products/devicecloud/support/connector and in: http://www.etherios.com
private/connector_edp_util.h@0:1c358ea10753, 2013-12-03 (annotated)
- Committer:
- spastor
- Date:
- Tue Dec 03 13:34:02 2013 +0000
- Revision:
- 0:1c358ea10753
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
spastor | 0:1c358ea10753 | 1 | /* |
spastor | 0:1c358ea10753 | 2 | * Copyright (c) 2013 Digi International Inc., |
spastor | 0:1c358ea10753 | 3 | * All rights not expressly granted are reserved. |
spastor | 0:1c358ea10753 | 4 | * |
spastor | 0:1c358ea10753 | 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
spastor | 0:1c358ea10753 | 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
spastor | 0:1c358ea10753 | 7 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
spastor | 0:1c358ea10753 | 8 | * |
spastor | 0:1c358ea10753 | 9 | * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
spastor | 0:1c358ea10753 | 10 | * ======================================================================= |
spastor | 0:1c358ea10753 | 11 | */ |
spastor | 0:1c358ea10753 | 12 | |
spastor | 0:1c358ea10753 | 13 | #define edp_set_active_state(connector_ptr, value) (connector_ptr)->edp_data.state.active = (value) |
spastor | 0:1c358ea10753 | 14 | #define edp_get_active_state(connector_ptr) (connector_ptr)->edp_data.state.active |
spastor | 0:1c358ea10753 | 15 | |
spastor | 0:1c358ea10753 | 16 | #define edp_set_initiate_state(connector_ptr, value) (connector_ptr)->edp_data.state.initiate = (value) |
spastor | 0:1c358ea10753 | 17 | #define edp_get_initiate_state(connector_ptr) (connector_ptr)->edp_data.state.initiate |
spastor | 0:1c358ea10753 | 18 | |
spastor | 0:1c358ea10753 | 19 | /* #define edp_get_error(connector_ptr) (connector_ptr)->edp_data.error_code |
spastor | 0:1c358ea10753 | 20 | #define edp_set_error(connector_ptr, error) (connector_ptr)->edp_data.error_code = (error) */ |
spastor | 0:1c358ea10753 | 21 | |
spastor | 0:1c358ea10753 | 22 | #define edp_get_edp_state(connector_ptr) (connector_ptr)->edp_data.state.edp |
spastor | 0:1c358ea10753 | 23 | #define edp_set_edp_state(connector_ptr, value) (connector_ptr)->edp_data.state.edp = (value) |
spastor | 0:1c358ea10753 | 24 | |
spastor | 0:1c358ea10753 | 25 | #define edp_get_close_status(connector_ptr) (connector_ptr)->edp_data.close_status |
spastor | 0:1c358ea10753 | 26 | #define edp_set_close_status(connector_ptr, value) (connector_ptr)->edp_data.close_status = (value) |
spastor | 0:1c358ea10753 | 27 | |
spastor | 0:1c358ea10753 | 28 | #define edp_set_stop_condition(connector_ptr, value) (connector_ptr)->edp_data.stop.condition = (value) |
spastor | 0:1c358ea10753 | 29 | #define edp_is_stop_immediately(connector_ptr) connector_bool((connector_ptr)->edp_data.stop.condition == connector_stop_immediately) |
spastor | 0:1c358ea10753 | 30 | #define edp_is_stop_request(connector_ptr) connector_bool((connector_ptr)->edp_data.stop.is_set == connector_true) |
spastor | 0:1c358ea10753 | 31 | #define edp_set_stop_request(connector_ptr, context) (connector_ptr)->edp_data.stop.is_set = connector_true; (connector_ptr)->edp_data.stop.user_context = (context) |
spastor | 0:1c358ea10753 | 32 | #define edp_clear_stop_request(connector_ptr) (connector_ptr)->edp_data.stop.is_set = connector_false |
spastor | 0:1c358ea10753 | 33 | |
spastor | 0:1c358ea10753 | 34 | static void edp_reset_initial_data(connector_data_t * const connector_ptr) |
spastor | 0:1c358ea10753 | 35 | { |
spastor | 0:1c358ea10753 | 36 | edp_set_edp_state(connector_ptr, edp_communication_connect_to_cloud); |
spastor | 0:1c358ea10753 | 37 | |
spastor | 0:1c358ea10753 | 38 | connector_ptr->edp_data.facilities.current = NULL; |
spastor | 0:1c358ea10753 | 39 | connector_ptr->edp_data.keepalive.last_rx_sent_time = 0; |
spastor | 0:1c358ea10753 | 40 | connector_ptr->edp_data.keepalive.last_tx_received_time = 0; |
spastor | 0:1c358ea10753 | 41 | connector_ptr->edp_data.keepalive.miss_tx_count = 0; |
spastor | 0:1c358ea10753 | 42 | |
spastor | 0:1c358ea10753 | 43 | connector_ptr->edp_data.send_packet.total_length = 0; |
spastor | 0:1c358ea10753 | 44 | connector_ptr->edp_data.send_packet.bytes_sent = 0; |
spastor | 0:1c358ea10753 | 45 | connector_ptr->edp_data.send_packet.ptr = NULL; |
spastor | 0:1c358ea10753 | 46 | connector_ptr->edp_data.send_packet.complete_cb = NULL; |
spastor | 0:1c358ea10753 | 47 | |
spastor | 0:1c358ea10753 | 48 | connector_ptr->edp_data.receive_packet.total_length = 0; |
spastor | 0:1c358ea10753 | 49 | connector_ptr->edp_data.receive_packet.bytes_received = 0; |
spastor | 0:1c358ea10753 | 50 | connector_ptr->edp_data.receive_packet.index = 0; |
spastor | 0:1c358ea10753 | 51 | connector_ptr->edp_data.receive_packet.ptr = NULL; |
spastor | 0:1c358ea10753 | 52 | connector_ptr->edp_data.receive_packet.data_packet = NULL; |
spastor | 0:1c358ea10753 | 53 | connector_ptr->edp_data.receive_packet.timeout = MAX_RECEIVE_TIMEOUT_IN_SECONDS; |
spastor | 0:1c358ea10753 | 54 | connector_ptr->edp_data.close_status = (connector_close_status_t)0; |
spastor | 0:1c358ea10753 | 55 | |
spastor | 0:1c358ea10753 | 56 | edp_set_stop_condition(connector_ptr, connector_stop_immediately); |
spastor | 0:1c358ea10753 | 57 | |
spastor | 0:1c358ea10753 | 58 | } |
spastor | 0:1c358ea10753 | 59 | |
spastor | 0:1c358ea10753 | 60 | static connector_bool_t is_valid_timing_limit(connector_data_t * const connector_ptr, unsigned long const start, unsigned long const limit) |
spastor | 0:1c358ea10753 | 61 | { |
spastor | 0:1c358ea10753 | 62 | unsigned long elapsed = start; |
spastor | 0:1c358ea10753 | 63 | connector_bool_t rc = connector_false; |
spastor | 0:1c358ea10753 | 64 | |
spastor | 0:1c358ea10753 | 65 | if (get_system_time(connector_ptr, &elapsed) == connector_working) |
spastor | 0:1c358ea10753 | 66 | { |
spastor | 0:1c358ea10753 | 67 | elapsed -= start; |
spastor | 0:1c358ea10753 | 68 | rc = (elapsed < limit) ? connector_true : connector_false; |
spastor | 0:1c358ea10753 | 69 | } |
spastor | 0:1c358ea10753 | 70 | |
spastor | 0:1c358ea10753 | 71 | return rc; |
spastor | 0:1c358ea10753 | 72 | } |
spastor | 0:1c358ea10753 | 73 | |
spastor | 0:1c358ea10753 | 74 | static void * get_facility_data(connector_data_t * const connector_ptr, uint16_t const facility_num) |
spastor | 0:1c358ea10753 | 75 | { |
spastor | 0:1c358ea10753 | 76 | connector_facility_t * fac_ptr; |
spastor | 0:1c358ea10753 | 77 | void * ptr = NULL; |
spastor | 0:1c358ea10753 | 78 | |
spastor | 0:1c358ea10753 | 79 | /* search and return the facility data which allocated from add_facility_data() */ |
spastor | 0:1c358ea10753 | 80 | for (fac_ptr = connector_ptr->edp_data.facilities.list; fac_ptr != NULL; fac_ptr = fac_ptr->next) |
spastor | 0:1c358ea10753 | 81 | { |
spastor | 0:1c358ea10753 | 82 | if (fac_ptr->facility_num == facility_num) |
spastor | 0:1c358ea10753 | 83 | { |
spastor | 0:1c358ea10753 | 84 | ptr = fac_ptr->facility_data; |
spastor | 0:1c358ea10753 | 85 | break; |
spastor | 0:1c358ea10753 | 86 | } |
spastor | 0:1c358ea10753 | 87 | } |
spastor | 0:1c358ea10753 | 88 | |
spastor | 0:1c358ea10753 | 89 | return ptr; |
spastor | 0:1c358ea10753 | 90 | } |
spastor | 0:1c358ea10753 | 91 | |
spastor | 0:1c358ea10753 | 92 | static connector_static_buffer_id_t get_facility_buffer_id(uint16_t const facility_num) |
spastor | 0:1c358ea10753 | 93 | { |
spastor | 0:1c358ea10753 | 94 | connector_static_buffer_id_t id; |
spastor | 0:1c358ea10753 | 95 | |
spastor | 0:1c358ea10753 | 96 | switch(facility_num) |
spastor | 0:1c358ea10753 | 97 | { |
spastor | 0:1c358ea10753 | 98 | case E_MSG_FAC_CC_NUM: |
spastor | 0:1c358ea10753 | 99 | id = named_buffer_id(cc_facility); |
spastor | 0:1c358ea10753 | 100 | break; |
spastor | 0:1c358ea10753 | 101 | case E_MSG_FAC_FW_NUM: |
spastor | 0:1c358ea10753 | 102 | id = named_buffer_id(fw_facility); |
spastor | 0:1c358ea10753 | 103 | break; |
spastor | 0:1c358ea10753 | 104 | case E_MSG_FAC_MSG_NUM: |
spastor | 0:1c358ea10753 | 105 | id = named_buffer_id(msg_facility); |
spastor | 0:1c358ea10753 | 106 | break; |
spastor | 0:1c358ea10753 | 107 | default: |
spastor | 0:1c358ea10753 | 108 | id = named_buffer_id(none); |
spastor | 0:1c358ea10753 | 109 | } |
spastor | 0:1c358ea10753 | 110 | return id; |
spastor | 0:1c358ea10753 | 111 | } |
spastor | 0:1c358ea10753 | 112 | |
spastor | 0:1c358ea10753 | 113 | static connector_status_t add_facility_data(connector_data_t * const connector_ptr, unsigned int const service_index, |
spastor | 0:1c358ea10753 | 114 | uint16_t const facility_num, void ** fac_ptr, size_t const size) |
spastor | 0:1c358ea10753 | 115 | { |
spastor | 0:1c358ea10753 | 116 | connector_status_t status; |
spastor | 0:1c358ea10753 | 117 | connector_facility_t * facility; |
spastor | 0:1c358ea10753 | 118 | void * ptr; |
spastor | 0:1c358ea10753 | 119 | size_t const facility_size = sizeof *facility; |
spastor | 0:1c358ea10753 | 120 | |
spastor | 0:1c358ea10753 | 121 | /* allocate facility data and buffer*/ |
spastor | 0:1c358ea10753 | 122 | |
spastor | 0:1c358ea10753 | 123 | *fac_ptr = NULL; |
spastor | 0:1c358ea10753 | 124 | status = malloc_data_buffer(connector_ptr, size + facility_size, get_facility_buffer_id(facility_num), &ptr); |
spastor | 0:1c358ea10753 | 125 | if (status == connector_working) |
spastor | 0:1c358ea10753 | 126 | { |
spastor | 0:1c358ea10753 | 127 | /* add facility to the facility list */ |
spastor | 0:1c358ea10753 | 128 | facility = ptr; |
spastor | 0:1c358ea10753 | 129 | facility->facility_num = facility_num; |
spastor | 0:1c358ea10753 | 130 | facility->size = size; |
spastor | 0:1c358ea10753 | 131 | facility->service_index = service_index; |
spastor | 0:1c358ea10753 | 132 | |
spastor | 0:1c358ea10753 | 133 | add_node(&connector_ptr->edp_data.facilities.list, facility); |
spastor | 0:1c358ea10753 | 134 | |
spastor | 0:1c358ea10753 | 135 | /* setup facility data which is at the end of connector_facility_t */ |
spastor | 0:1c358ea10753 | 136 | facility->facility_data = facility + 1; |
spastor | 0:1c358ea10753 | 137 | *fac_ptr = facility->facility_data; |
spastor | 0:1c358ea10753 | 138 | } |
spastor | 0:1c358ea10753 | 139 | |
spastor | 0:1c358ea10753 | 140 | return status; |
spastor | 0:1c358ea10753 | 141 | } |
spastor | 0:1c358ea10753 | 142 | |
spastor | 0:1c358ea10753 | 143 | static connector_status_t del_facility_data(connector_data_t * const connector_ptr, uint16_t const facility_num) |
spastor | 0:1c358ea10753 | 144 | { |
spastor | 0:1c358ea10753 | 145 | connector_status_t status = connector_working; |
spastor | 0:1c358ea10753 | 146 | connector_facility_t * fac_ptr; |
spastor | 0:1c358ea10753 | 147 | |
spastor | 0:1c358ea10753 | 148 | /* find and free the facility */ |
spastor | 0:1c358ea10753 | 149 | for (fac_ptr = connector_ptr->edp_data.facilities.list; fac_ptr != NULL; fac_ptr = fac_ptr->next) |
spastor | 0:1c358ea10753 | 150 | { |
spastor | 0:1c358ea10753 | 151 | if (fac_ptr->facility_num == facility_num) |
spastor | 0:1c358ea10753 | 152 | { |
spastor | 0:1c358ea10753 | 153 | |
spastor | 0:1c358ea10753 | 154 | remove_node(&connector_ptr->edp_data.facilities.list, fac_ptr); |
spastor | 0:1c358ea10753 | 155 | /* Ignore the return status. Just continue freeing next facility */ |
spastor | 0:1c358ea10753 | 156 | free_data_buffer(connector_ptr, get_facility_buffer_id(facility_num), fac_ptr); |
spastor | 0:1c358ea10753 | 157 | break; |
spastor | 0:1c358ea10753 | 158 | } |
spastor | 0:1c358ea10753 | 159 | } |
spastor | 0:1c358ea10753 | 160 | return status; |
spastor | 0:1c358ea10753 | 161 | } |
spastor | 0:1c358ea10753 | 162 | |
spastor | 0:1c358ea10753 | 163 | static connector_status_t notify_status(connector_callback_t const callback, connector_tcp_status_t const status) |
spastor | 0:1c358ea10753 | 164 | { |
spastor | 0:1c358ea10753 | 165 | connector_status_t result; |
spastor | 0:1c358ea10753 | 166 | connector_request_id_t request_id; |
spastor | 0:1c358ea10753 | 167 | connector_status_tcp_event_t tcp_event; |
spastor | 0:1c358ea10753 | 168 | |
spastor | 0:1c358ea10753 | 169 | tcp_event.status = status; |
spastor | 0:1c358ea10753 | 170 | |
spastor | 0:1c358ea10753 | 171 | request_id.status_request = connector_request_id_status_tcp; |
spastor | 0:1c358ea10753 | 172 | |
spastor | 0:1c358ea10753 | 173 | { |
spastor | 0:1c358ea10753 | 174 | connector_callback_status_t const callback_status = connector_callback(callback, connector_class_id_status, request_id, &tcp_event); |
spastor | 0:1c358ea10753 | 175 | |
spastor | 0:1c358ea10753 | 176 | switch (callback_status) |
spastor | 0:1c358ea10753 | 177 | { |
spastor | 0:1c358ea10753 | 178 | case connector_callback_continue: |
spastor | 0:1c358ea10753 | 179 | case connector_callback_unrecognized: |
spastor | 0:1c358ea10753 | 180 | result = connector_working; |
spastor | 0:1c358ea10753 | 181 | break; |
spastor | 0:1c358ea10753 | 182 | default: |
spastor | 0:1c358ea10753 | 183 | result = connector_abort; |
spastor | 0:1c358ea10753 | 184 | break; |
spastor | 0:1c358ea10753 | 185 | } |
spastor | 0:1c358ea10753 | 186 | } |
spastor | 0:1c358ea10753 | 187 | |
spastor | 0:1c358ea10753 | 188 | return result; |
spastor | 0:1c358ea10753 | 189 | } |
spastor | 0:1c358ea10753 | 190 | |
spastor | 0:1c358ea10753 | 191 | static void edp_get_device_cloud(connector_data_t * const connector_ptr) |
spastor | 0:1c358ea10753 | 192 | { |
spastor | 0:1c358ea10753 | 193 | memcpy(connector_ptr->edp_data.config.cloud_url, connector_ptr->device_cloud_url, connector_ptr->device_cloud_url_length); |
spastor | 0:1c358ea10753 | 194 | connector_ptr->edp_data.config.cloud_url[connector_ptr->device_cloud_url_length] = 0x0; |
spastor | 0:1c358ea10753 | 195 | connector_ptr->edp_data.config.cloud_url_length = connector_ptr->device_cloud_url_length; |
spastor | 0:1c358ea10753 | 196 | } |
spastor | 0:1c358ea10753 | 197 | |
spastor | 0:1c358ea10753 | 198 |