Connected switch with wifi for Project Trenton (weather control switch!). Targets NXP LPC1768 device with WiFly connectivity module.
Dependencies: WiflyInterface mbed nanoservice_client_1_12
Fork of cc3000_simple_socket_demo by
main.cpp@15:59f4cee0da79, 2014-03-25 (annotated)
- Committer:
- erigow01
- Date:
- Tue Mar 25 13:00:09 2014 +0000
- Revision:
- 15:59f4cee0da79
- Parent:
- 14:5f84f9ae168e
- Child:
- 16:3fb612af0dc5
Fixed CoAP protocol - tx_cb problem.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 6:6eaae34586b8 | 1 | /* mbed Microcontroller Library |
Kojto | 6:6eaae34586b8 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Kojto | 6:6eaae34586b8 | 3 | * |
Kojto | 6:6eaae34586b8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 6:6eaae34586b8 | 5 | * you may not use this file except in compliance with the License. |
Kojto | 6:6eaae34586b8 | 6 | * You may obtain a copy of the License at |
Kojto | 6:6eaae34586b8 | 7 | * |
Kojto | 6:6eaae34586b8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 6:6eaae34586b8 | 9 | * |
Kojto | 6:6eaae34586b8 | 10 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 6:6eaae34586b8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 6:6eaae34586b8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 6:6eaae34586b8 | 13 | * See the License for the specific language governing permissions and |
Kojto | 6:6eaae34586b8 | 14 | * limitations under the License. |
Kojto | 6:6eaae34586b8 | 15 | */ |
Kojto | 6:6eaae34586b8 | 16 | #include "mbed.h" |
Kojto | 6:6eaae34586b8 | 17 | #include "cc3000.h" |
Kojto | 6:6eaae34586b8 | 18 | #include "main.h" |
erigow01 | 15:59f4cee0da79 | 19 | #include "rtos.h" |
erigow01 | 15:59f4cee0da79 | 20 | #include "LPD8806.h" |
erigow01 | 15:59f4cee0da79 | 21 | // NanoService includes |
erigow01 | 15:59f4cee0da79 | 22 | #include "Endpoint.h" |
erigow01 | 15:59f4cee0da79 | 23 | #include "UDPSocket.h" |
erigow01 | 15:59f4cee0da79 | 24 | #include "sn_nsdl.h" |
erigow01 | 15:59f4cee0da79 | 25 | #include "sn_coap_header.h" |
erigow01 | 15:59f4cee0da79 | 26 | #include "sn_coap_protocol.h" |
erigow01 | 15:59f4cee0da79 | 27 | #include "sn_nsdl_lib.h" |
erigow01 | 15:59f4cee0da79 | 28 | #include "sn_grs.h" |
erigow01 | 15:59f4cee0da79 | 29 | #include <stdint.h> |
Kojto | 6:6eaae34586b8 | 30 | |
Kojto | 8:be68e827aa53 | 31 | #define STRINGIFY(x) #x |
Kojto | 8:be68e827aa53 | 32 | #define TO_STRING(x) STRINGIFY(x) |
Kojto | 8:be68e827aa53 | 33 | |
Kojto | 6:6eaae34586b8 | 34 | using namespace mbed_cc3000; |
Kojto | 6:6eaae34586b8 | 35 | |
Kojto | 6:6eaae34586b8 | 36 | tUserFS user_info; |
Kojto | 6:6eaae34586b8 | 37 | |
Kojto | 6:6eaae34586b8 | 38 | /* cc3000 module declaration specific for user's board. Check also init() */ |
Kojto | 6:6eaae34586b8 | 39 | #if (MY_BOARD == WIGO) |
Kojto | 14:5f84f9ae168e | 40 | cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5)); |
Kojto | 6:6eaae34586b8 | 41 | Serial pc(USBTX, USBRX); |
Kojto | 6:6eaae34586b8 | 42 | #elif (MY_BOARD == WIFI_DIPCORTEX) |
Kojto | 14:5f84f9ae168e | 43 | cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37)); |
Kojto | 6:6eaae34586b8 | 44 | Serial pc(UART_TX, UART_RX); |
Kojto | 14:5f84f9ae168e | 45 | #elif (MY_BOARD == MBED_BOARD_EXAMPLE) |
Kojto | 14:5f84f9ae168e | 46 | cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7)); |
Kojto | 14:5f84f9ae168e | 47 | Serial pc(USBTX, USBRX); |
Kojto | 6:6eaae34586b8 | 48 | #else |
Kojto | 6:6eaae34586b8 | 49 | |
Kojto | 6:6eaae34586b8 | 50 | #endif |
Kojto | 6:6eaae34586b8 | 51 | |
Kojto | 6:6eaae34586b8 | 52 | #ifndef CC3000_UNENCRYPTED_SMART_CONFIG |
Kojto | 6:6eaae34586b8 | 53 | const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36}; |
Kojto | 6:6eaae34586b8 | 54 | #else |
Kojto | 6:6eaae34586b8 | 55 | const uint8_t smartconfigkey = 0; |
Kojto | 6:6eaae34586b8 | 56 | #endif |
Kojto | 6:6eaae34586b8 | 57 | |
erigow01 | 15:59f4cee0da79 | 58 | // NSP configuration |
erigow01 | 15:59f4cee0da79 | 59 | /* Change this IP address to that of your NanoService Platform installation */ |
erigow01 | 15:59f4cee0da79 | 60 | Endpoint nsp; |
erigow01 | 15:59f4cee0da79 | 61 | UDPSocket server; |
erigow01 | 15:59f4cee0da79 | 62 | static const char* NSP_ADDRESS = "10.2.131.119"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ |
erigow01 | 15:59f4cee0da79 | 63 | static const int NSP_PORT = 5683; |
erigow01 | 15:59f4cee0da79 | 64 | char endpoint_name[] = {"umbrella"}; |
erigow01 | 15:59f4cee0da79 | 65 | uint8_t ep_type[] = {"mbed_kl25z"}; |
erigow01 | 15:59f4cee0da79 | 66 | uint8_t lifetime_ptr[] = {"86400"}; |
erigow01 | 15:59f4cee0da79 | 67 | |
erigow01 | 15:59f4cee0da79 | 68 | typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *); |
erigow01 | 15:59f4cee0da79 | 69 | |
erigow01 | 15:59f4cee0da79 | 70 | //LED Config |
erigow01 | 15:59f4cee0da79 | 71 | //Number of RGB LEDs in strand: |
erigow01 | 15:59f4cee0da79 | 72 | int nLEDs=80; |
erigow01 | 15:59f4cee0da79 | 73 | int datapin=1; |
erigow01 | 15:59f4cee0da79 | 74 | int clockpin=2; |
erigow01 | 15:59f4cee0da79 | 75 | LPD8806 strip = LPD8806(nLEDs,datapin,clockpin); |
erigow01 | 15:59f4cee0da79 | 76 | |
erigow01 | 15:59f4cee0da79 | 77 | |
Kojto | 6:6eaae34586b8 | 78 | /** |
Kojto | 6:6eaae34586b8 | 79 | * \brief Print cc3000 information |
Kojto | 6:6eaae34586b8 | 80 | * \param none |
Kojto | 6:6eaae34586b8 | 81 | * \return none |
Kojto | 6:6eaae34586b8 | 82 | */ |
Kojto | 6:6eaae34586b8 | 83 | void print_cc3000_info() { |
Kojto | 6:6eaae34586b8 | 84 | uint8_t myMAC[8]; |
Kojto | 6:6eaae34586b8 | 85 | |
Kojto | 6:6eaae34586b8 | 86 | printf("MAC address + cc3000 info \r\n"); |
Kojto | 6:6eaae34586b8 | 87 | wifi.get_user_file_info((uint8_t *)&user_info, sizeof(user_info)); |
Kojto | 6:6eaae34586b8 | 88 | wifi.get_mac_address(myMAC); |
Kojto | 6:6eaae34586b8 | 89 | printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x \r\n \r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]); |
Kojto | 6:6eaae34586b8 | 90 | |
Kojto | 6:6eaae34586b8 | 91 | printf(" FTC %i \r\n",user_info.FTC); |
Kojto | 6:6eaae34586b8 | 92 | printf(" PP_version %i.%i \r\n",user_info.PP_version[0], user_info.PP_version[1]); |
Kojto | 6:6eaae34586b8 | 93 | printf(" SERV_PACK %i.%i \r\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]); |
Kojto | 6:6eaae34586b8 | 94 | printf(" DRV_VER %i.%i.%i \r\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]); |
Kojto | 6:6eaae34586b8 | 95 | printf(" FW_VER %i.%i.%i \r\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]); |
Kojto | 6:6eaae34586b8 | 96 | } |
Kojto | 6:6eaae34586b8 | 97 | |
Kojto | 6:6eaae34586b8 | 98 | /** |
Kojto | 6:6eaae34586b8 | 99 | * \brief Connect to SSID with a timeout |
Kojto | 6:6eaae34586b8 | 100 | * \param ssid Name of SSID |
Kojto | 6:6eaae34586b8 | 101 | * \param key Password |
Kojto | 6:6eaae34586b8 | 102 | * \param sec_mode Security mode |
Kojto | 6:6eaae34586b8 | 103 | * \return none |
Kojto | 6:6eaae34586b8 | 104 | */ |
Kojto | 6:6eaae34586b8 | 105 | void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) { |
Kojto | 7:afaa17c11965 | 106 | printf("Connecting to SSID: %s. Timeout is 10s. \r\n",ssid); |
Kojto | 6:6eaae34586b8 | 107 | if (wifi.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) { |
Kojto | 7:afaa17c11965 | 108 | printf(" Connected. \r\n"); |
Kojto | 6:6eaae34586b8 | 109 | } else { |
Kojto | 7:afaa17c11965 | 110 | printf(" Connection timed-out (error). Please restart. \r\n"); |
Kojto | 6:6eaae34586b8 | 111 | while(1); |
Kojto | 6:6eaae34586b8 | 112 | } |
Kojto | 6:6eaae34586b8 | 113 | } |
Kojto | 6:6eaae34586b8 | 114 | |
Kojto | 6:6eaae34586b8 | 115 | /** |
Kojto | 6:6eaae34586b8 | 116 | * \brief Connect to SSID without security |
Kojto | 6:6eaae34586b8 | 117 | * \param ssid Name of SSID |
Kojto | 6:6eaae34586b8 | 118 | * \return none |
Kojto | 6:6eaae34586b8 | 119 | */ |
Kojto | 6:6eaae34586b8 | 120 | void connect_to_ssid(char *ssid) { |
Kojto | 6:6eaae34586b8 | 121 | wifi.connect_open((uint8_t *)ssid); |
Kojto | 6:6eaae34586b8 | 122 | } |
Kojto | 6:6eaae34586b8 | 123 | |
Kojto | 6:6eaae34586b8 | 124 | /** |
Kojto | 6:6eaae34586b8 | 125 | * \brief First time configuration |
Kojto | 6:6eaae34586b8 | 126 | * \param none |
Kojto | 6:6eaae34586b8 | 127 | * \return none |
Kojto | 6:6eaae34586b8 | 128 | */ |
Kojto | 6:6eaae34586b8 | 129 | void do_FTC(void) { |
Kojto | 6:6eaae34586b8 | 130 | printf("Running First Time Configuration \r\n"); |
Kojto | 6:6eaae34586b8 | 131 | wifi.start_smart_config(smartconfigkey); |
Kojto | 6:6eaae34586b8 | 132 | while (wifi.is_dhcp_configured() == false) { |
Kojto | 6:6eaae34586b8 | 133 | wait_ms(500); |
Kojto | 6:6eaae34586b8 | 134 | printf("Waiting for dhcp to be set. \r\n"); |
Kojto | 6:6eaae34586b8 | 135 | } |
Kojto | 6:6eaae34586b8 | 136 | user_info.FTC = 1; |
Kojto | 6:6eaae34586b8 | 137 | wifi.set_user_file_info((uint8_t *)&user_info, sizeof(user_info)); |
Kojto | 6:6eaae34586b8 | 138 | wifi._wlan.stop(); |
Kojto | 7:afaa17c11965 | 139 | printf("FTC finished. \r\n"); |
Kojto | 6:6eaae34586b8 | 140 | } |
Kojto | 6:6eaae34586b8 | 141 | |
Kojto | 8:be68e827aa53 | 142 | /** |
Kojto | 7:afaa17c11965 | 143 | * \brief Start smart config |
Kojto | 7:afaa17c11965 | 144 | * \param none |
Kojto | 6:6eaae34586b8 | 145 | * \return none |
Kojto | 6:6eaae34586b8 | 146 | */ |
Kojto | 6:6eaae34586b8 | 147 | void start_smart_config() { |
Kojto | 6:6eaae34586b8 | 148 | wifi.start_smart_config(smartconfigkey); |
Kojto | 6:6eaae34586b8 | 149 | } |
Kojto | 6:6eaae34586b8 | 150 | |
erigow01 | 15:59f4cee0da79 | 151 | // **************************************************************************** |
erigow01 | 15:59f4cee0da79 | 152 | // NSP initialization |
erigow01 | 15:59f4cee0da79 | 153 | |
erigow01 | 15:59f4cee0da79 | 154 | static void nsp_connect() |
erigow01 | 15:59f4cee0da79 | 155 | { |
erigow01 | 15:59f4cee0da79 | 156 | |
erigow01 | 15:59f4cee0da79 | 157 | printf("EP Name: %s", endpoint_name); |
erigow01 | 15:59f4cee0da79 | 158 | printf("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT); |
erigow01 | 15:59f4cee0da79 | 159 | |
erigow01 | 15:59f4cee0da79 | 160 | // Bind the port |
erigow01 | 15:59f4cee0da79 | 161 | //cellular->bind(EP_PORT); |
erigow01 | 15:59f4cee0da79 | 162 | server.init(); |
erigow01 | 15:59f4cee0da79 | 163 | server.bind(NSP_PORT); |
erigow01 | 15:59f4cee0da79 | 164 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
erigow01 | 15:59f4cee0da79 | 165 | |
erigow01 | 15:59f4cee0da79 | 166 | printf("UDP connection to NSP successful.\r\n"); |
erigow01 | 15:59f4cee0da79 | 167 | } |
erigow01 | 15:59f4cee0da79 | 168 | |
erigow01 | 15:59f4cee0da79 | 169 | extern "C" void *nsdl_alloc(uint16_t size) |
erigow01 | 15:59f4cee0da79 | 170 | { |
erigow01 | 15:59f4cee0da79 | 171 | return malloc(size); |
erigow01 | 15:59f4cee0da79 | 172 | } |
erigow01 | 15:59f4cee0da79 | 173 | |
erigow01 | 15:59f4cee0da79 | 174 | extern "C" void nsdl_free(void* ptr_to_free) |
erigow01 | 15:59f4cee0da79 | 175 | { |
erigow01 | 15:59f4cee0da79 | 176 | free(ptr_to_free); |
erigow01 | 15:59f4cee0da79 | 177 | } |
erigow01 | 15:59f4cee0da79 | 178 | |
erigow01 | 15:59f4cee0da79 | 179 | static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) |
erigow01 | 15:59f4cee0da79 | 180 | { |
erigow01 | 15:59f4cee0da79 | 181 | /* |
erigow01 | 15:59f4cee0da79 | 182 | int buffer_len = data_len+2; |
erigow01 | 15:59f4cee0da79 | 183 | printf("TX callback! Sending %d bytes\r\n", buffer_len); |
erigow01 | 15:59f4cee0da79 | 184 | char buffer[buffer_len]; |
erigow01 | 15:59f4cee0da79 | 185 | buffer[0] = data_len >> 8; |
erigow01 | 15:59f4cee0da79 | 186 | buffer[1] = data_len & 0xFF; |
erigow01 | 15:59f4cee0da79 | 187 | memcpy(buffer+2, data_ptr, data_len); |
erigow01 | 15:59f4cee0da79 | 188 | */ |
erigow01 | 15:59f4cee0da79 | 189 | //if(cellular->write((char*)buffer, (int)buffer_len, 1000) != buffer_len) |
erigow01 | 15:59f4cee0da79 | 190 | if(server.sendTo(nsp, (char*)data_ptr, (int)data_len) != data_len) |
erigow01 | 15:59f4cee0da79 | 191 | printf("Sending failed\r\n"); |
erigow01 | 15:59f4cee0da79 | 192 | |
erigow01 | 15:59f4cee0da79 | 193 | |
erigow01 | 15:59f4cee0da79 | 194 | return 1; |
erigow01 | 15:59f4cee0da79 | 195 | |
erigow01 | 15:59f4cee0da79 | 196 | } |
erigow01 | 15:59f4cee0da79 | 197 | |
erigow01 | 15:59f4cee0da79 | 198 | static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) |
erigow01 | 15:59f4cee0da79 | 199 | { |
erigow01 | 15:59f4cee0da79 | 200 | printf("RX callback!\r\n"); |
erigow01 | 15:59f4cee0da79 | 201 | return 0; |
erigow01 | 15:59f4cee0da79 | 202 | } |
erigow01 | 15:59f4cee0da79 | 203 | |
erigow01 | 15:59f4cee0da79 | 204 | void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len) |
erigow01 | 15:59f4cee0da79 | 205 | { |
erigow01 | 15:59f4cee0da79 | 206 | resource_structure->access = SN_GRS_GET_ALLOWED; |
erigow01 | 15:59f4cee0da79 | 207 | resource_structure->mode = SN_GRS_STATIC; |
erigow01 | 15:59f4cee0da79 | 208 | resource_structure->pathlen = pt_len; |
erigow01 | 15:59f4cee0da79 | 209 | resource_structure->path = pt; |
erigow01 | 15:59f4cee0da79 | 210 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
erigow01 | 15:59f4cee0da79 | 211 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
erigow01 | 15:59f4cee0da79 | 212 | resource_structure->resource = rsc; |
erigow01 | 15:59f4cee0da79 | 213 | resource_structure->resourcelen = rsc_len; |
erigow01 | 15:59f4cee0da79 | 214 | sn_nsdl_create_resource(resource_structure); |
erigow01 | 15:59f4cee0da79 | 215 | } |
erigow01 | 15:59f4cee0da79 | 216 | |
erigow01 | 15:59f4cee0da79 | 217 | void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right) |
erigow01 | 15:59f4cee0da79 | 218 | { |
erigow01 | 15:59f4cee0da79 | 219 | resource_structure->access = (sn_grs_resource_acl_e)access_right; |
erigow01 | 15:59f4cee0da79 | 220 | resource_structure->resource = 0; |
erigow01 | 15:59f4cee0da79 | 221 | resource_structure->resourcelen = 0; |
erigow01 | 15:59f4cee0da79 | 222 | resource_structure->sn_grs_dyn_res_callback = callback_ptr; |
erigow01 | 15:59f4cee0da79 | 223 | resource_structure->mode = SN_GRS_DYNAMIC; |
erigow01 | 15:59f4cee0da79 | 224 | resource_structure->pathlen = pt_len; |
erigow01 | 15:59f4cee0da79 | 225 | resource_structure->path = pt; |
erigow01 | 15:59f4cee0da79 | 226 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
erigow01 | 15:59f4cee0da79 | 227 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
erigow01 | 15:59f4cee0da79 | 228 | resource_structure->resource_parameters_ptr->observable = is_observable; |
erigow01 | 15:59f4cee0da79 | 229 | sn_nsdl_create_resource(resource_structure); |
erigow01 | 15:59f4cee0da79 | 230 | } |
erigow01 | 15:59f4cee0da79 | 231 | |
erigow01 | 15:59f4cee0da79 | 232 | sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr) |
erigow01 | 15:59f4cee0da79 | 233 | { |
erigow01 | 15:59f4cee0da79 | 234 | if (NULL == endpoint_structure) |
erigow01 | 15:59f4cee0da79 | 235 | { |
erigow01 | 15:59f4cee0da79 | 236 | endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); |
erigow01 | 15:59f4cee0da79 | 237 | } |
erigow01 | 15:59f4cee0da79 | 238 | if (endpoint_structure) |
erigow01 | 15:59f4cee0da79 | 239 | { |
erigow01 | 15:59f4cee0da79 | 240 | memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); |
erigow01 | 15:59f4cee0da79 | 241 | endpoint_structure->endpoint_name_ptr = name; |
erigow01 | 15:59f4cee0da79 | 242 | endpoint_structure->endpoint_name_len = strlen((char*)name); |
erigow01 | 15:59f4cee0da79 | 243 | endpoint_structure->type_ptr = typename_ptr; |
erigow01 | 15:59f4cee0da79 | 244 | endpoint_structure->type_len = strlen((char*)typename_ptr); |
erigow01 | 15:59f4cee0da79 | 245 | endpoint_structure->lifetime_ptr = lifetime_ptr; |
erigow01 | 15:59f4cee0da79 | 246 | endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); |
erigow01 | 15:59f4cee0da79 | 247 | } |
erigow01 | 15:59f4cee0da79 | 248 | return endpoint_structure; |
erigow01 | 15:59f4cee0da79 | 249 | } |
erigow01 | 15:59f4cee0da79 | 250 | |
erigow01 | 15:59f4cee0da79 | 251 | void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) |
erigow01 | 15:59f4cee0da79 | 252 | { |
erigow01 | 15:59f4cee0da79 | 253 | if (*endpoint_structure) |
erigow01 | 15:59f4cee0da79 | 254 | { |
erigow01 | 15:59f4cee0da79 | 255 | nsdl_free(*endpoint_structure); |
erigow01 | 15:59f4cee0da79 | 256 | *endpoint_structure = NULL; |
erigow01 | 15:59f4cee0da79 | 257 | } |
erigow01 | 15:59f4cee0da79 | 258 | } |
erigow01 | 15:59f4cee0da79 | 259 | |
erigow01 | 15:59f4cee0da79 | 260 | void nsdl_init() |
erigow01 | 15:59f4cee0da79 | 261 | { |
erigow01 | 15:59f4cee0da79 | 262 | uint8_t nsp_addr[4]; |
erigow01 | 15:59f4cee0da79 | 263 | sn_nsdl_mem_s memory_cbs; |
erigow01 | 15:59f4cee0da79 | 264 | memory_cbs.sn_nsdl_alloc = &nsdl_alloc; |
erigow01 | 15:59f4cee0da79 | 265 | memory_cbs.sn_nsdl_free = &nsdl_free; |
erigow01 | 15:59f4cee0da79 | 266 | if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) { |
erigow01 | 15:59f4cee0da79 | 267 | printf("libNsdl init failed\r\n"); |
erigow01 | 15:59f4cee0da79 | 268 | } else { |
erigow01 | 15:59f4cee0da79 | 269 | printf("libNsdl init done\r\n"); |
erigow01 | 15:59f4cee0da79 | 270 | } |
erigow01 | 15:59f4cee0da79 | 271 | /* Set nsp address for library */ |
erigow01 | 15:59f4cee0da79 | 272 | set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); |
erigow01 | 15:59f4cee0da79 | 273 | } |
erigow01 | 15:59f4cee0da79 | 274 | |
erigow01 | 15:59f4cee0da79 | 275 | static int create_resources() |
erigow01 | 15:59f4cee0da79 | 276 | { |
erigow01 | 15:59f4cee0da79 | 277 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
erigow01 | 15:59f4cee0da79 | 278 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
erigow01 | 15:59f4cee0da79 | 279 | |
erigow01 | 15:59f4cee0da79 | 280 | printf("Creating resources\r\n"); |
erigow01 | 15:59f4cee0da79 | 281 | |
erigow01 | 15:59f4cee0da79 | 282 | /* Create resources */ |
erigow01 | 15:59f4cee0da79 | 283 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
erigow01 | 15:59f4cee0da79 | 284 | if(!resource_ptr) |
erigow01 | 15:59f4cee0da79 | 285 | return 0; |
erigow01 | 15:59f4cee0da79 | 286 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
erigow01 | 15:59f4cee0da79 | 287 | |
erigow01 | 15:59f4cee0da79 | 288 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
erigow01 | 15:59f4cee0da79 | 289 | if(!resource_ptr->resource_parameters_ptr) |
erigow01 | 15:59f4cee0da79 | 290 | { |
erigow01 | 15:59f4cee0da79 | 291 | nsdl_free(resource_ptr); |
erigow01 | 15:59f4cee0da79 | 292 | return 0; |
erigow01 | 15:59f4cee0da79 | 293 | } |
erigow01 | 15:59f4cee0da79 | 294 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
erigow01 | 15:59f4cee0da79 | 295 | |
erigow01 | 15:59f4cee0da79 | 296 | // Static resources |
erigow01 | 15:59f4cee0da79 | 297 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "KL25Z Umbrella Stand", sizeof("KL25Z Umbrella Stand")-1); |
erigow01 | 15:59f4cee0da79 | 298 | |
erigow01 | 15:59f4cee0da79 | 299 | // Dynamic resources |
erigow01 | 15:59f4cee0da79 | 300 | // create_light_resource(resource_ptr); |
erigow01 | 15:59f4cee0da79 | 301 | // create_gps_resource(resource_ptr); |
erigow01 | 15:59f4cee0da79 | 302 | |
erigow01 | 15:59f4cee0da79 | 303 | /* Register with NSP */ |
erigow01 | 15:59f4cee0da79 | 304 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
erigow01 | 15:59f4cee0da79 | 305 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { |
erigow01 | 15:59f4cee0da79 | 306 | printf("NSP registering failed\r\n"); |
erigow01 | 15:59f4cee0da79 | 307 | } else { |
erigow01 | 15:59f4cee0da79 | 308 | printf("NSP registering OK\r\n"); |
erigow01 | 15:59f4cee0da79 | 309 | } |
erigow01 | 15:59f4cee0da79 | 310 | nsdl_clean_register_endpoint(&endpoint_ptr); |
erigow01 | 15:59f4cee0da79 | 311 | |
erigow01 | 15:59f4cee0da79 | 312 | nsdl_free(resource_ptr->resource_parameters_ptr); |
erigow01 | 15:59f4cee0da79 | 313 | nsdl_free(resource_ptr); |
erigow01 | 15:59f4cee0da79 | 314 | return 1; |
erigow01 | 15:59f4cee0da79 | 315 | } |
erigow01 | 15:59f4cee0da79 | 316 | |
erigow01 | 15:59f4cee0da79 | 317 | void nsp_register() |
erigow01 | 15:59f4cee0da79 | 318 | { |
erigow01 | 15:59f4cee0da79 | 319 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
erigow01 | 15:59f4cee0da79 | 320 | |
erigow01 | 15:59f4cee0da79 | 321 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
erigow01 | 15:59f4cee0da79 | 322 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { |
erigow01 | 15:59f4cee0da79 | 323 | printf("NSP re-registration failed\r\n"); |
erigow01 | 15:59f4cee0da79 | 324 | } else { |
erigow01 | 15:59f4cee0da79 | 325 | printf("NSP re-registration OK\r\n"); |
erigow01 | 15:59f4cee0da79 | 326 | } |
erigow01 | 15:59f4cee0da79 | 327 | nsdl_clean_register_endpoint(&endpoint_ptr); |
erigow01 | 15:59f4cee0da79 | 328 | } |
erigow01 | 15:59f4cee0da79 | 329 | |
erigow01 | 15:59f4cee0da79 | 330 | void socket_event_loop() |
erigow01 | 15:59f4cee0da79 | 331 | { |
erigow01 | 15:59f4cee0da79 | 332 | sn_nsdl_addr_s received_packet_address; |
erigow01 | 15:59f4cee0da79 | 333 | uint8_t received_address[4]; |
erigow01 | 15:59f4cee0da79 | 334 | char buffer[2048]; |
erigow01 | 15:59f4cee0da79 | 335 | int n; |
erigow01 | 15:59f4cee0da79 | 336 | Endpoint from; |
erigow01 | 15:59f4cee0da79 | 337 | |
erigow01 | 15:59f4cee0da79 | 338 | memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); |
erigow01 | 15:59f4cee0da79 | 339 | received_packet_address.addr_ptr = received_address; |
erigow01 | 15:59f4cee0da79 | 340 | |
erigow01 | 15:59f4cee0da79 | 341 | printf("Starting socket read loop...\r\n"); |
erigow01 | 15:59f4cee0da79 | 342 | while(1) |
erigow01 | 15:59f4cee0da79 | 343 | { |
erigow01 | 15:59f4cee0da79 | 344 | //n = cellular->read(buffer, sizeof(buffer), 1000); |
erigow01 | 15:59f4cee0da79 | 345 | n = server.receiveFrom(from, buffer, sizeof(buffer)); |
erigow01 | 15:59f4cee0da79 | 346 | printf("Received %d bytes", n); |
erigow01 | 15:59f4cee0da79 | 347 | if (n < 0) |
erigow01 | 15:59f4cee0da79 | 348 | { |
erigow01 | 15:59f4cee0da79 | 349 | printf("Socket error\r\n"); |
erigow01 | 15:59f4cee0da79 | 350 | } |
erigow01 | 15:59f4cee0da79 | 351 | else |
erigow01 | 15:59f4cee0da79 | 352 | { |
erigow01 | 15:59f4cee0da79 | 353 | uint16_t len = 0; |
erigow01 | 15:59f4cee0da79 | 354 | if (n > 2) { |
erigow01 | 15:59f4cee0da79 | 355 | len = 256 * buffer[0] + buffer[1]; |
erigow01 | 15:59f4cee0da79 | 356 | printf("CoAP length header = %d bytes\r\n", len); |
erigow01 | 15:59f4cee0da79 | 357 | sn_nsdl_process_coap((uint8_t*)buffer+2, len, &received_packet_address); |
erigow01 | 15:59f4cee0da79 | 358 | } |
erigow01 | 15:59f4cee0da79 | 359 | } |
erigow01 | 15:59f4cee0da79 | 360 | } |
erigow01 | 15:59f4cee0da79 | 361 | |
erigow01 | 15:59f4cee0da79 | 362 | } |
erigow01 | 15:59f4cee0da79 | 363 | |
erigow01 | 15:59f4cee0da79 | 364 | /************************************************************************************ |
erigow01 | 15:59f4cee0da79 | 365 | Use Color Fill to fill strips from bottom up in a 6 strip alternating configuration. |
erigow01 | 15:59f4cee0da79 | 366 | c The colour |
erigow01 | 15:59f4cee0da79 | 367 | delay Time between each pixel activation |
erigow01 | 15:59f4cee0da79 | 368 | numStrips Number of sub-strips |
erigow01 | 15:59f4cee0da79 | 369 | direction <0 implies top down, >0 bottom up, 0 all at once. Assumes first pixel is top. |
erigow01 | 15:59f4cee0da79 | 370 | alternating 0 implies first pixel of each strip is top, otherwise alternates top/bottom/top etc. |
erigow01 | 15:59f4cee0da79 | 371 | *************************************************************************************/ |
erigow01 | 15:59f4cee0da79 | 372 | void colorFillAlternatingSubStrips(uint32_t c, uint8_t delay, uint8_t numSubStrips, uint8_t alternating, int8_t direction) { |
erigow01 | 15:59f4cee0da79 | 373 | |
erigow01 | 15:59f4cee0da79 | 374 | int i,j; |
erigow01 | 15:59f4cee0da79 | 375 | int pixelsPerStrip = strip.numPixels() / numSubStrips; |
erigow01 | 15:59f4cee0da79 | 376 | |
erigow01 | 15:59f4cee0da79 | 377 | //set each pixel sequentially: |
erigow01 | 15:59f4cee0da79 | 378 | for (i=0; i < pixelsPerStrip; i++) { |
erigow01 | 15:59f4cee0da79 | 379 | for(j = 0; j < numSubStrips; j++) { |
erigow01 | 15:59f4cee0da79 | 380 | if(!alternating || j % 2 == 0) { |
erigow01 | 15:59f4cee0da79 | 381 | // Even strip |
erigow01 | 15:59f4cee0da79 | 382 | if(direction <= 0) { |
erigow01 | 15:59f4cee0da79 | 383 | //Down |
erigow01 | 15:59f4cee0da79 | 384 | strip.setPixelColor(j*pixelsPerStrip + i, c); |
erigow01 | 15:59f4cee0da79 | 385 | } else if (direction > 0) { |
erigow01 | 15:59f4cee0da79 | 386 | //Up |
erigow01 | 15:59f4cee0da79 | 387 | strip.setPixelColor((j+1)*pixelsPerStrip - 1 - i, c); |
erigow01 | 15:59f4cee0da79 | 388 | } |
erigow01 | 15:59f4cee0da79 | 389 | } else { |
erigow01 | 15:59f4cee0da79 | 390 | // Odd Strip |
erigow01 | 15:59f4cee0da79 | 391 | if(direction < 0) { |
erigow01 | 15:59f4cee0da79 | 392 | //Down |
erigow01 | 15:59f4cee0da79 | 393 | strip.setPixelColor((j+1)*pixelsPerStrip - 1 - i, c); |
erigow01 | 15:59f4cee0da79 | 394 | } else if (direction >= 0) { |
erigow01 | 15:59f4cee0da79 | 395 | //Up |
erigow01 | 15:59f4cee0da79 | 396 | strip.setPixelColor(j*pixelsPerStrip + i, c); |
erigow01 | 15:59f4cee0da79 | 397 | } |
erigow01 | 15:59f4cee0da79 | 398 | } |
erigow01 | 15:59f4cee0da79 | 399 | } |
erigow01 | 15:59f4cee0da79 | 400 | if(direction != 0) { |
erigow01 | 15:59f4cee0da79 | 401 | strip.show(); |
erigow01 | 15:59f4cee0da79 | 402 | wait_ms(delay); |
erigow01 | 15:59f4cee0da79 | 403 | } |
erigow01 | 15:59f4cee0da79 | 404 | } |
erigow01 | 15:59f4cee0da79 | 405 | if(direction == 0) { |
erigow01 | 15:59f4cee0da79 | 406 | strip.show(); |
erigow01 | 15:59f4cee0da79 | 407 | wait_ms(delay); |
erigow01 | 15:59f4cee0da79 | 408 | } |
erigow01 | 15:59f4cee0da79 | 409 | } |
erigow01 | 15:59f4cee0da79 | 410 | |
erigow01 | 15:59f4cee0da79 | 411 | void doNotificationIteration() { |
erigow01 | 15:59f4cee0da79 | 412 | colorFillAlternatingSubStrips(strip.Color(0,127,0), 100, 6, 1, 1); |
erigow01 | 15:59f4cee0da79 | 413 | wait_ms (3000); |
erigow01 | 15:59f4cee0da79 | 414 | colorFillAlternatingSubStrips(strip.Color(0,0,0), 100, 6, 1, 1); |
erigow01 | 15:59f4cee0da79 | 415 | wait_ms (1000); |
erigow01 | 15:59f4cee0da79 | 416 | } |
erigow01 | 15:59f4cee0da79 | 417 | |
erigow01 | 15:59f4cee0da79 | 418 | |
Kojto | 8:be68e827aa53 | 419 | /** |
Kojto | 7:afaa17c11965 | 420 | * \brief Simple socket demo |
Kojto | 7:afaa17c11965 | 421 | * \param none |
Kojto | 6:6eaae34586b8 | 422 | * \return int |
Kojto | 6:6eaae34586b8 | 423 | */ |
Kojto | 6:6eaae34586b8 | 424 | int main() { |
Kojto | 6:6eaae34586b8 | 425 | init(); /* board dependent init */ |
Kojto | 6:6eaae34586b8 | 426 | pc.baud(115200); |
Kojto | 6:6eaae34586b8 | 427 | |
Kojto | 6:6eaae34586b8 | 428 | wifi.start(0); |
Kojto | 7:afaa17c11965 | 429 | printf("cc3000 simple socket demo. \r\n"); |
Kojto | 6:6eaae34586b8 | 430 | print_cc3000_info(); |
Kojto | 6:6eaae34586b8 | 431 | |
erigow01 | 15:59f4cee0da79 | 432 | printf("Attempting SSID Connection. \r\n"); |
Kojto | 6:6eaae34586b8 | 433 | |
erigow01 | 15:59f4cee0da79 | 434 | wifi._wlan.ioctl_set_connection_policy(0, 0, 0); |
Kojto | 6:6eaae34586b8 | 435 | #ifndef CC3000_TINY_DRIVER |
erigow01 | 15:59f4cee0da79 | 436 | connect_to_ssid(SSID, AP_KEY, AP_SECURITY); |
Kojto | 6:6eaae34586b8 | 437 | #else |
erigow01 | 15:59f4cee0da79 | 438 | connect_to_ssid(SSID); |
Kojto | 6:6eaae34586b8 | 439 | #endif |
Kojto | 6:6eaae34586b8 | 440 | |
Kojto | 6:6eaae34586b8 | 441 | printf("DHCP request \r\n"); |
Kojto | 6:6eaae34586b8 | 442 | while (wifi.is_dhcp_configured() == false) { |
Kojto | 6:6eaae34586b8 | 443 | wait_ms(500); |
Kojto | 6:6eaae34586b8 | 444 | printf(" Waiting for dhcp to be set. \r\n"); |
Kojto | 6:6eaae34586b8 | 445 | } |
Kojto | 6:6eaae34586b8 | 446 | |
Kojto | 6:6eaae34586b8 | 447 | tNetappIpconfigRetArgs ipinfo2; |
Kojto | 6:6eaae34586b8 | 448 | wifi.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure |
Kojto | 6:6eaae34586b8 | 449 | printf("DHCP assigned IP Address = %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]); |
Kojto | 6:6eaae34586b8 | 450 | |
erigow01 | 15:59f4cee0da79 | 451 | // Bind the socket and configure NSP settings |
erigow01 | 15:59f4cee0da79 | 452 | nsp_connect(); |
erigow01 | 15:59f4cee0da79 | 453 | |
erigow01 | 15:59f4cee0da79 | 454 | // Initalize NanoService library |
erigow01 | 15:59f4cee0da79 | 455 | nsdl_init(); |
Kojto | 6:6eaae34586b8 | 456 | |
erigow01 | 15:59f4cee0da79 | 457 | // Create resources & register with NSP |
erigow01 | 15:59f4cee0da79 | 458 | create_resources(); |
erigow01 | 15:59f4cee0da79 | 459 | |
erigow01 | 15:59f4cee0da79 | 460 | // Start socket listening loop |
erigow01 | 15:59f4cee0da79 | 461 | socket_event_loop(); |
erigow01 | 15:59f4cee0da79 | 462 | |
Kojto | 6:6eaae34586b8 | 463 | } |